blob: c349015ba022c5bf5314f499488e165fa778691d [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 Moolenaar14f24742012-08-08 18:01:05 +02001408 int sw = (int)get_sw_value();
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001411 newindent -= newindent % sw;
1412 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001415 /* Copy the indent */
1416 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
1418 (void)copy_indent(newindent, saved_line);
1419
1420 /*
1421 * Set the 'preserveindent' option so that any further screwing
1422 * with the line doesn't entirely destroy our efforts to preserve
1423 * it. It gets restored at the function end.
1424 */
1425 curbuf->b_p_pi = TRUE;
1426 }
1427 else
1428 (void)set_indent(newindent, SIN_INSERT);
1429 less_cols -= curwin->w_cursor.col;
1430
1431 ai_col = curwin->w_cursor.col;
1432
1433 /*
1434 * In REPLACE mode, for each character in the new indent, there must
1435 * be a NUL on the replace stack, for when it is deleted with BS
1436 */
1437 if (REPLACE_NORMAL(State))
1438 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1439 replace_push(NUL);
1440 newcol += curwin->w_cursor.col;
1441#ifdef FEAT_SMARTINDENT
1442 if (no_si)
1443 did_si = FALSE;
1444#endif
1445 }
1446
1447#ifdef FEAT_COMMENTS
1448 /*
1449 * In REPLACE mode, for each character in the extra leader, there must be
1450 * a NUL on the replace stack, for when it is deleted with BS.
1451 */
1452 if (REPLACE_NORMAL(State))
1453 while (lead_len-- > 0)
1454 replace_push(NUL);
1455#endif
1456
1457 curwin->w_cursor = old_cursor;
1458
1459 if (dir == FORWARD)
1460 {
1461 if (trunc_line || (State & INSERT))
1462 {
1463 /* truncate current line at cursor */
1464 saved_line[curwin->w_cursor.col] = NUL;
1465 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1466 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1467 truncate_spaces(saved_line);
1468 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1469 saved_line = NULL;
1470 if (did_append)
1471 {
1472 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1473 curwin->w_cursor.lnum + 1, 1L);
1474 did_append = FALSE;
1475
1476 /* Move marks after the line break to the new line. */
1477 if (flags & OPENLINE_MARKFIX)
1478 mark_col_adjust(curwin->w_cursor.lnum,
1479 curwin->w_cursor.col + less_cols_off,
1480 1L, (long)-less_cols);
1481 }
1482 else
1483 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1484 }
1485
1486 /*
1487 * Put the cursor on the new line. Careful: the scrollup() above may
1488 * have moved w_cursor, we must use old_cursor.
1489 */
1490 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1491 }
1492 if (did_append)
1493 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1494
1495 curwin->w_cursor.col = newcol;
1496#ifdef FEAT_VIRTUALEDIT
1497 curwin->w_cursor.coladd = 0;
1498#endif
1499
1500#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1501 /*
1502 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1503 * fixthisline() from doing it (via change_indent()) by telling it we're in
1504 * normal INSERT mode.
1505 */
1506 if (State & VREPLACE_FLAG)
1507 {
1508 vreplace_mode = State; /* So we know to put things right later */
1509 State = INSERT;
1510 }
1511 else
1512 vreplace_mode = 0;
1513#endif
1514#ifdef FEAT_LISP
1515 /*
1516 * May do lisp indenting.
1517 */
1518 if (!p_paste
1519# ifdef FEAT_COMMENTS
1520 && leader == NULL
1521# endif
1522 && curbuf->b_p_lisp
1523 && curbuf->b_p_ai)
1524 {
1525 fixthisline(get_lisp_indent);
1526 p = ml_get_curline();
1527 ai_col = (colnr_T)(skipwhite(p) - p);
1528 }
1529#endif
1530#ifdef FEAT_CINDENT
1531 /*
1532 * May do indenting after opening a new line.
1533 */
1534 if (!p_paste
1535 && (curbuf->b_p_cin
1536# ifdef FEAT_EVAL
1537 || *curbuf->b_p_inde != NUL
1538# endif
1539 )
1540 && in_cinkeys(dir == FORWARD
1541 ? KEY_OPEN_FORW
1542 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1543 {
1544 do_c_expr_indent();
1545 p = ml_get_curline();
1546 ai_col = (colnr_T)(skipwhite(p) - p);
1547 }
1548#endif
1549#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1550 if (vreplace_mode != 0)
1551 State = vreplace_mode;
1552#endif
1553
1554#ifdef FEAT_VREPLACE
1555 /*
1556 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1557 * original line, and inserts the new stuff char by char, pushing old stuff
1558 * onto the replace stack (via ins_char()).
1559 */
1560 if (State & VREPLACE_FLAG)
1561 {
1562 /* Put new line in p_extra */
1563 p_extra = vim_strsave(ml_get_curline());
1564 if (p_extra == NULL)
1565 goto theend;
1566
1567 /* Put back original line */
1568 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1569
1570 /* Insert new stuff into line again */
1571 curwin->w_cursor.col = 0;
1572#ifdef FEAT_VIRTUALEDIT
1573 curwin->w_cursor.coladd = 0;
1574#endif
1575 ins_bytes(p_extra); /* will call changed_bytes() */
1576 vim_free(p_extra);
1577 next_line = NULL;
1578 }
1579#endif
1580
1581 retval = TRUE; /* success! */
1582theend:
1583 curbuf->b_p_pi = saved_pi;
1584 vim_free(saved_line);
1585 vim_free(next_line);
1586 vim_free(allocated);
1587 return retval;
1588}
1589
1590#if defined(FEAT_COMMENTS) || defined(PROTO)
1591/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001592 * get_leader_len() returns the length in bytes of the prefix of the given
1593 * string which introduces a comment. If this string is not a comment then
1594 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * When "flags" is not NULL, it is set to point to the flags of the recognized
1596 * comment leader.
1597 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001598 * If "include_space" is set, include trailing whitespace while calculating the
1599 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 */
1601 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001602get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 char_u *line;
1604 char_u **flags;
1605 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001606 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001609 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 int got_com = FALSE;
1611 int found_one;
1612 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1613 char_u *string; /* pointer to comment string */
1614 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001615 int middle_match_len = 0;
1616 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001617 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar81340392012-06-06 16:12:59 +02001619 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 while (vim_iswhite(line[i])) /* leading white space is ignored */
1621 ++i;
1622
1623 /*
1624 * Repeat to match several nested comment strings.
1625 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001626 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628 /*
1629 * scan through the 'comments' option for a match
1630 */
1631 found_one = FALSE;
1632 for (list = curbuf->b_p_com; *list; )
1633 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001634 /* Get one option part into part_buf[]. Advance "list" to next
1635 * one. Put "string" at start of string. */
1636 if (!got_com && flags != NULL)
1637 *flags = list; /* remember where flags started */
1638 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1640 string = vim_strchr(part_buf, ':');
1641 if (string == NULL) /* missing ':', ignore this part */
1642 continue;
1643 *string++ = NUL; /* isolate flags from string */
1644
Bram Moolenaara4271d52011-05-10 13:38:27 +02001645 /* If we found a middle match previously, use that match when this
1646 * is not a middle or end. */
1647 if (middle_match_len != 0
1648 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1649 && vim_strchr(part_buf, COM_END) == NULL)
1650 break;
1651
1652 /* When we already found a nested comment, only accept further
1653 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1655 continue;
1656
Bram Moolenaara4271d52011-05-10 13:38:27 +02001657 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1659 continue;
1660
Bram Moolenaara4271d52011-05-10 13:38:27 +02001661 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 * When string starts with white space, must have some white space
1663 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001664 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (vim_iswhite(string[0]))
1666 {
1667 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001668 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 while (vim_iswhite(string[0]))
1670 ++string;
1671 }
1672 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1673 ;
1674 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001675 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaara4271d52011-05-10 13:38:27 +02001677 /* When 'b' flag used, there must be white space or an
1678 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (vim_strchr(part_buf, COM_BLANK) != NULL
1680 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1681 continue;
1682
Bram Moolenaara4271d52011-05-10 13:38:27 +02001683 /* We have found a match, stop searching unless this is a middle
1684 * comment. The middle comment can be a substring of the end
1685 * comment in which case it's better to return the length of the
1686 * end comment and its flags. Thus we keep searching with middle
1687 * and end matches and use an end match if it matches better. */
1688 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1689 {
1690 if (middle_match_len == 0)
1691 {
1692 middle_match_len = j;
1693 saved_flags = prev_list;
1694 }
1695 continue;
1696 }
1697 if (middle_match_len != 0 && j > middle_match_len)
1698 /* Use this match instead of the middle match, since it's a
1699 * longer thus better match. */
1700 middle_match_len = 0;
1701
1702 if (middle_match_len == 0)
1703 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 found_one = TRUE;
1705 break;
1706 }
1707
Bram Moolenaara4271d52011-05-10 13:38:27 +02001708 if (middle_match_len != 0)
1709 {
1710 /* Use the previously found middle match after failing to find a
1711 * match with an end. */
1712 if (!got_com && flags != NULL)
1713 *flags = saved_flags;
1714 i += middle_match_len;
1715 found_one = TRUE;
1716 }
1717
1718 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (!found_one)
1720 break;
1721
Bram Moolenaar81340392012-06-06 16:12:59 +02001722 result = i;
1723
Bram Moolenaara4271d52011-05-10 13:38:27 +02001724 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 while (vim_iswhite(line[i]))
1726 ++i;
1727
Bram Moolenaar81340392012-06-06 16:12:59 +02001728 if (include_space)
1729 result = i;
1730
Bram Moolenaara4271d52011-05-10 13:38:27 +02001731 /* If this comment doesn't nest, stop here. */
1732 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (vim_strchr(part_buf, COM_NEST) == NULL)
1734 break;
1735 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001736 return result;
1737}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001738
Bram Moolenaar81340392012-06-06 16:12:59 +02001739/*
1740 * Return the offset at which the last comment in line starts. If there is no
1741 * comment in the whole line, -1 is returned.
1742 *
1743 * When "flags" is not null, it is set to point to the flags describing the
1744 * recognized comment leader.
1745 */
1746 int
1747get_last_leader_offset(line, flags)
1748 char_u *line;
1749 char_u **flags;
1750{
1751 int result = -1;
1752 int i, j;
1753 int lower_check_bound = 0;
1754 char_u *string;
1755 char_u *com_leader;
1756 char_u *com_flags;
1757 char_u *list;
1758 int found_one;
1759 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1760
1761 /*
1762 * Repeat to match several nested comment strings.
1763 */
1764 i = (int)STRLEN(line);
1765 while (--i >= lower_check_bound)
1766 {
1767 /*
1768 * scan through the 'comments' option for a match
1769 */
1770 found_one = FALSE;
1771 for (list = curbuf->b_p_com; *list; )
1772 {
1773 char_u *flags_save = list;
1774
1775 /*
1776 * Get one option part into part_buf[]. Advance list to next one.
1777 * put string at start of string.
1778 */
1779 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1780 string = vim_strchr(part_buf, ':');
1781 if (string == NULL) /* If everything is fine, this cannot actually
1782 * happen. */
1783 {
1784 continue;
1785 }
1786 *string++ = NUL; /* Isolate flags from string. */
1787 com_leader = string;
1788
1789 /*
1790 * Line contents and string must match.
1791 * When string starts with white space, must have some white space
1792 * (but the amount does not need to match, there might be a mix of
1793 * TABs and spaces).
1794 */
1795 if (vim_iswhite(string[0]))
1796 {
1797 if (i == 0 || !vim_iswhite(line[i - 1]))
1798 continue;
1799 while (vim_iswhite(string[0]))
1800 ++string;
1801 }
1802 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1803 /* do nothing */;
1804 if (string[j] != NUL)
1805 continue;
1806
1807 /*
1808 * When 'b' flag used, there must be white space or an
1809 * end-of-line after the string in the line.
1810 */
1811 if (vim_strchr(part_buf, COM_BLANK) != NULL
1812 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1813 {
1814 continue;
1815 }
1816
1817 /*
1818 * We have found a match, stop searching.
1819 */
1820 found_one = TRUE;
1821
1822 if (flags)
1823 *flags = flags_save;
1824 com_flags = flags_save;
1825
1826 break;
1827 }
1828
1829 if (found_one)
1830 {
1831 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1832 int len1, len2, off;
1833
1834 result = i;
1835 /*
1836 * If this comment nests, continue searching.
1837 */
1838 if (vim_strchr(part_buf, COM_NEST) != NULL)
1839 continue;
1840
1841 lower_check_bound = i;
1842
1843 /* Let's verify whether the comment leader found is a substring
1844 * of other comment leaders. If it is, let's adjust the
1845 * lower_check_bound so that we make sure that we have determined
1846 * the comment leader correctly.
1847 */
1848
1849 while (vim_iswhite(*com_leader))
1850 ++com_leader;
1851 len1 = (int)STRLEN(com_leader);
1852
1853 for (list = curbuf->b_p_com; *list; )
1854 {
1855 char_u *flags_save = list;
1856
1857 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1858 if (flags_save == com_flags)
1859 continue;
1860 string = vim_strchr(part_buf2, ':');
1861 ++string;
1862 while (vim_iswhite(*string))
1863 ++string;
1864 len2 = (int)STRLEN(string);
1865 if (len2 == 0)
1866 continue;
1867
1868 /* Now we have to verify whether string ends with a substring
1869 * beginning the com_leader. */
1870 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1871 {
1872 --off;
1873 if (!STRNCMP(string + off, com_leader, len2 - off))
1874 {
1875 if (i - off < lower_check_bound)
1876 lower_check_bound = i - off;
1877 }
1878 }
1879 }
1880 }
1881 }
1882 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883}
1884#endif
1885
1886/*
1887 * Return the number of window lines occupied by buffer line "lnum".
1888 */
1889 int
1890plines(lnum)
1891 linenr_T lnum;
1892{
1893 return plines_win(curwin, lnum, TRUE);
1894}
1895
1896 int
1897plines_win(wp, lnum, winheight)
1898 win_T *wp;
1899 linenr_T lnum;
1900 int winheight; /* when TRUE limit to window height */
1901{
1902#if defined(FEAT_DIFF) || defined(PROTO)
1903 /* Check for filler lines above this buffer line. When folded the result
1904 * is one line anyway. */
1905 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1906}
1907
1908 int
1909plines_nofill(lnum)
1910 linenr_T lnum;
1911{
1912 return plines_win_nofill(curwin, lnum, TRUE);
1913}
1914
1915 int
1916plines_win_nofill(wp, lnum, winheight)
1917 win_T *wp;
1918 linenr_T lnum;
1919 int winheight; /* when TRUE limit to window height */
1920{
1921#endif
1922 int lines;
1923
1924 if (!wp->w_p_wrap)
1925 return 1;
1926
1927#ifdef FEAT_VERTSPLIT
1928 if (wp->w_width == 0)
1929 return 1;
1930#endif
1931
1932#ifdef FEAT_FOLDING
1933 /* A folded lines is handled just like an empty line. */
1934 /* NOTE: Caller must handle lines that are MAYBE folded. */
1935 if (lineFolded(wp, lnum) == TRUE)
1936 return 1;
1937#endif
1938
1939 lines = plines_win_nofold(wp, lnum);
1940 if (winheight > 0 && lines > wp->w_height)
1941 return (int)wp->w_height;
1942 return lines;
1943}
1944
1945/*
1946 * Return number of window lines physical line "lnum" will occupy in window
1947 * "wp". Does not care about folding, 'wrap' or 'diff'.
1948 */
1949 int
1950plines_win_nofold(wp, lnum)
1951 win_T *wp;
1952 linenr_T lnum;
1953{
1954 char_u *s;
1955 long col;
1956 int width;
1957
1958 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
1959 if (*s == NUL) /* empty line */
1960 return 1;
1961 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
1962
1963 /*
1964 * If list mode is on, then the '$' at the end of the line may take up one
1965 * extra column.
1966 */
1967 if (wp->w_p_list && lcs_eol != NUL)
1968 col += 1;
1969
1970 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001971 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 */
1973 width = W_WIDTH(wp) - win_col_off(wp);
1974 if (width <= 0)
1975 return 32000;
1976 if (col <= width)
1977 return 1;
1978 col -= width;
1979 width += win_col_off2(wp);
1980 return (col + (width - 1)) / width + 1;
1981}
1982
1983/*
1984 * Like plines_win(), but only reports the number of physical screen lines
1985 * used from the start of the line to the given column number.
1986 */
1987 int
1988plines_win_col(wp, lnum, column)
1989 win_T *wp;
1990 linenr_T lnum;
1991 long column;
1992{
1993 long col;
1994 char_u *s;
1995 int lines = 0;
1996 int width;
1997
1998#ifdef FEAT_DIFF
1999 /* Check for filler lines above this buffer line. When folded the result
2000 * is one line anyway. */
2001 lines = diff_check_fill(wp, lnum);
2002#endif
2003
2004 if (!wp->w_p_wrap)
2005 return lines + 1;
2006
2007#ifdef FEAT_VERTSPLIT
2008 if (wp->w_width == 0)
2009 return lines + 1;
2010#endif
2011
2012 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2013
2014 col = 0;
2015 while (*s != NUL && --column >= 0)
2016 {
2017 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002018 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020
2021 /*
2022 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2023 * INSERT mode, then col must be adjusted so that it represents the last
2024 * screen position of the TAB. This only fixes an error when the TAB wraps
2025 * from one screen line to the next (when 'columns' is not a multiple of
2026 * 'ts') -- webb.
2027 */
2028 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
2029 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1;
2030
2031 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002032 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 */
2034 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002035 if (width <= 0)
2036 return 9999;
2037
2038 lines += 1;
2039 if (col > width)
2040 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2041 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042}
2043
2044 int
2045plines_m_win(wp, first, last)
2046 win_T *wp;
2047 linenr_T first, last;
2048{
2049 int count = 0;
2050
2051 while (first <= last)
2052 {
2053#ifdef FEAT_FOLDING
2054 int x;
2055
2056 /* Check if there are any really folded lines, but also included lines
2057 * that are maybe folded. */
2058 x = foldedCount(wp, first, NULL);
2059 if (x > 0)
2060 {
2061 ++count; /* count 1 for "+-- folded" line */
2062 first += x;
2063 }
2064 else
2065#endif
2066 {
2067#ifdef FEAT_DIFF
2068 if (first == wp->w_topline)
2069 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2070 else
2071#endif
2072 count += plines_win(wp, first, TRUE);
2073 ++first;
2074 }
2075 }
2076 return (count);
2077}
2078
2079#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2080/*
2081 * Insert string "p" at the cursor position. Stops at a NUL byte.
2082 * Handles Replace mode and multi-byte characters.
2083 */
2084 void
2085ins_bytes(p)
2086 char_u *p;
2087{
2088 ins_bytes_len(p, (int)STRLEN(p));
2089}
2090#endif
2091
2092#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2093 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2094/*
2095 * Insert string "p" with length "len" at the cursor position.
2096 * Handles Replace mode and multi-byte characters.
2097 */
2098 void
2099ins_bytes_len(p, len)
2100 char_u *p;
2101 int len;
2102{
2103 int i;
2104# ifdef FEAT_MBYTE
2105 int n;
2106
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002107 if (has_mbyte)
2108 for (i = 0; i < len; i += n)
2109 {
2110 if (enc_utf8)
2111 /* avoid reading past p[len] */
2112 n = utfc_ptr2len_len(p + i, len - i);
2113 else
2114 n = (*mb_ptr2len)(p + i);
2115 ins_char_bytes(p + i, n);
2116 }
2117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002119 for (i = 0; i < len; ++i)
2120 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121}
2122#endif
2123
2124/*
2125 * Insert or replace a single character at the cursor position.
2126 * When in REPLACE or VREPLACE mode, replace any existing character.
2127 * Caller must have prepared for undo.
2128 * For multi-byte characters we get the whole character, the caller must
2129 * convert bytes to a character.
2130 */
2131 void
2132ins_char(c)
2133 int c;
2134{
2135#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002136 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 int n;
2138
2139 n = (*mb_char2bytes)(c, buf);
2140
2141 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2142 * Happens for CTRL-Vu9900. */
2143 if (buf[0] == 0)
2144 buf[0] = '\n';
2145
2146 ins_char_bytes(buf, n);
2147}
2148
2149 void
2150ins_char_bytes(buf, charlen)
2151 char_u *buf;
2152 int charlen;
2153{
2154 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#endif
2156 int newlen; /* nr of bytes inserted */
2157 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2158 char_u *p;
2159 char_u *newp;
2160 char_u *oldp;
2161 int linelen; /* length of old line including NUL */
2162 colnr_T col;
2163 linenr_T lnum = curwin->w_cursor.lnum;
2164 int i;
2165
2166#ifdef FEAT_VIRTUALEDIT
2167 /* Break tabs if needed. */
2168 if (virtual_active() && curwin->w_cursor.coladd > 0)
2169 coladvance_force(getviscol());
2170#endif
2171
2172 col = curwin->w_cursor.col;
2173 oldp = ml_get(lnum);
2174 linelen = (int)STRLEN(oldp) + 1;
2175
2176 /* The lengths default to the values for when not replacing. */
2177 oldlen = 0;
2178#ifdef FEAT_MBYTE
2179 newlen = charlen;
2180#else
2181 newlen = 1;
2182#endif
2183
2184 if (State & REPLACE_FLAG)
2185 {
2186#ifdef FEAT_VREPLACE
2187 if (State & VREPLACE_FLAG)
2188 {
2189 colnr_T new_vcol = 0; /* init for GCC */
2190 colnr_T vcol;
2191 int old_list;
2192#ifndef FEAT_MBYTE
2193 char_u buf[2];
2194#endif
2195
2196 /*
2197 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2198 * Returns the old value of list, so when finished,
2199 * curwin->w_p_list should be set back to this.
2200 */
2201 old_list = curwin->w_p_list;
2202 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2203 curwin->w_p_list = FALSE;
2204
2205 /*
2206 * In virtual replace mode each character may replace one or more
2207 * characters (zero if it's a TAB). Count the number of bytes to
2208 * be deleted to make room for the new character, counting screen
2209 * cells. May result in adding spaces to fill a gap.
2210 */
2211 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2212#ifndef FEAT_MBYTE
2213 buf[0] = c;
2214 buf[1] = NUL;
2215#endif
2216 new_vcol = vcol + chartabsize(buf, vcol);
2217 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2218 {
2219 vcol += chartabsize(oldp + col + oldlen, vcol);
2220 /* Don't need to remove a TAB that takes us to the right
2221 * position. */
2222 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2223 break;
2224#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002225 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226#else
2227 ++oldlen;
2228#endif
2229 /* Deleted a bit too much, insert spaces. */
2230 if (vcol > new_vcol)
2231 newlen += vcol - new_vcol;
2232 }
2233 curwin->w_p_list = old_list;
2234 }
2235 else
2236#endif
2237 if (oldp[col] != NUL)
2238 {
2239 /* normal replace */
2240#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002241 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#else
2243 oldlen = 1;
2244#endif
2245 }
2246
2247
2248 /* Push the replaced bytes onto the replace stack, so that they can be
2249 * put back when BS is used. The bytes of a multi-byte character are
2250 * done the other way around, so that the first byte is popped off
2251 * first (it tells the byte length of the character). */
2252 replace_push(NUL);
2253 for (i = 0; i < oldlen; ++i)
2254 {
2255#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002256 if (has_mbyte)
2257 i += replace_push_mb(oldp + col + i) - 1;
2258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002260 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262 }
2263
2264 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2265 if (newp == NULL)
2266 return;
2267
2268 /* Copy bytes before the cursor. */
2269 if (col > 0)
2270 mch_memmove(newp, oldp, (size_t)col);
2271
2272 /* Copy bytes after the changed character(s). */
2273 p = newp + col;
2274 mch_memmove(p + newlen, oldp + col + oldlen,
2275 (size_t)(linelen - col - oldlen));
2276
2277 /* Insert or overwrite the new character. */
2278#ifdef FEAT_MBYTE
2279 mch_memmove(p, buf, charlen);
2280 i = charlen;
2281#else
2282 *p = c;
2283 i = 1;
2284#endif
2285
2286 /* Fill with spaces when necessary. */
2287 while (i < newlen)
2288 p[i++] = ' ';
2289
2290 /* Replace the line in the buffer. */
2291 ml_replace(lnum, newp, FALSE);
2292
2293 /* mark the buffer as changed and prepare for displaying */
2294 changed_bytes(lnum, col);
2295
2296 /*
2297 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2298 * show the match for right parens and braces.
2299 */
2300 if (p_sm && (State & INSERT)
2301 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002302#ifdef FEAT_INS_EXPAND
2303 && !ins_compl_active()
2304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002306 {
2307#ifdef FEAT_MBYTE
2308 if (has_mbyte)
2309 showmatch(mb_ptr2char(buf));
2310 else
2311#endif
2312 showmatch(c);
2313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
2315#ifdef FEAT_RIGHTLEFT
2316 if (!p_ri || (State & REPLACE_FLAG))
2317#endif
2318 {
2319 /* Normal insert: move cursor right */
2320#ifdef FEAT_MBYTE
2321 curwin->w_cursor.col += charlen;
2322#else
2323 ++curwin->w_cursor.col;
2324#endif
2325 }
2326 /*
2327 * TODO: should try to update w_row here, to avoid recomputing it later.
2328 */
2329}
2330
2331/*
2332 * Insert a string at the cursor position.
2333 * Note: Does NOT handle Replace mode.
2334 * Caller must have prepared for undo.
2335 */
2336 void
2337ins_str(s)
2338 char_u *s;
2339{
2340 char_u *oldp, *newp;
2341 int newlen = (int)STRLEN(s);
2342 int oldlen;
2343 colnr_T col;
2344 linenr_T lnum = curwin->w_cursor.lnum;
2345
2346#ifdef FEAT_VIRTUALEDIT
2347 if (virtual_active() && curwin->w_cursor.coladd > 0)
2348 coladvance_force(getviscol());
2349#endif
2350
2351 col = curwin->w_cursor.col;
2352 oldp = ml_get(lnum);
2353 oldlen = (int)STRLEN(oldp);
2354
2355 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2356 if (newp == NULL)
2357 return;
2358 if (col > 0)
2359 mch_memmove(newp, oldp, (size_t)col);
2360 mch_memmove(newp + col, s, (size_t)newlen);
2361 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2362 ml_replace(lnum, newp, FALSE);
2363 changed_bytes(lnum, col);
2364 curwin->w_cursor.col += newlen;
2365}
2366
2367/*
2368 * Delete one character under the cursor.
2369 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2370 * Caller must have prepared for undo.
2371 *
2372 * return FAIL for failure, OK otherwise
2373 */
2374 int
2375del_char(fixpos)
2376 int fixpos;
2377{
2378#ifdef FEAT_MBYTE
2379 if (has_mbyte)
2380 {
2381 /* Make sure the cursor is at the start of a character. */
2382 mb_adjust_cursor();
2383 if (*ml_get_cursor() == NUL)
2384 return FAIL;
2385 return del_chars(1L, fixpos);
2386 }
2387#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002388 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389}
2390
2391#if defined(FEAT_MBYTE) || defined(PROTO)
2392/*
2393 * Like del_bytes(), but delete characters instead of bytes.
2394 */
2395 int
2396del_chars(count, fixpos)
2397 long count;
2398 int fixpos;
2399{
2400 long bytes = 0;
2401 long i;
2402 char_u *p;
2403 int l;
2404
2405 p = ml_get_cursor();
2406 for (i = 0; i < count && *p != NUL; ++i)
2407 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002408 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 bytes += l;
2410 p += l;
2411 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002412 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413}
2414#endif
2415
2416/*
2417 * Delete "count" bytes under the cursor.
2418 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2419 * Caller must have prepared for undo.
2420 *
2421 * return FAIL for failure, OK otherwise
2422 */
2423 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002424del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002426 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002427 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429 char_u *oldp, *newp;
2430 colnr_T oldlen;
2431 linenr_T lnum = curwin->w_cursor.lnum;
2432 colnr_T col = curwin->w_cursor.col;
2433 int was_alloced;
2434 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002435 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436
2437 oldp = ml_get(lnum);
2438 oldlen = (int)STRLEN(oldp);
2439
2440 /*
2441 * Can't do anything when the cursor is on the NUL after the line.
2442 */
2443 if (col >= oldlen)
2444 return FAIL;
2445
2446#ifdef FEAT_MBYTE
2447 /* If 'delcombine' is set and deleting (less than) one character, only
2448 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002449 if (p_deco && use_delcombine && enc_utf8
2450 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002452 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 int n;
2454
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002455 (void)utfc_ptr2char(oldp + col, cc);
2456 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 /* Find the last composing char, there can be several. */
2459 n = col;
2460 do
2461 {
2462 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002463 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 n += count;
2465 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2466 fixpos = 0;
2467 }
2468 }
2469#endif
2470
2471 /*
2472 * When count is too big, reduce it.
2473 */
2474 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2475 if (movelen <= 1)
2476 {
2477 /*
2478 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002479 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2480 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002482 if (col > 0 && fixpos && restart_edit == 0
2483#ifdef FEAT_VIRTUALEDIT
2484 && (ve_flags & VE_ONEMORE) == 0
2485#endif
2486 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 --curwin->w_cursor.col;
2489#ifdef FEAT_VIRTUALEDIT
2490 curwin->w_cursor.coladd = 0;
2491#endif
2492#ifdef FEAT_MBYTE
2493 if (has_mbyte)
2494 curwin->w_cursor.col -=
2495 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2496#endif
2497 }
2498 count = oldlen - col;
2499 movelen = 1;
2500 }
2501
2502 /*
2503 * If the old line has been allocated the deletion can be done in the
2504 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002505 * Can't do this when using Netbeans, because we would need to invoke
2506 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002507 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002510 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002511 was_alloced = FALSE;
2512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002514 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (was_alloced)
2516 newp = oldp; /* use same allocated memory */
2517 else
2518 { /* need to allocate a new line */
2519 newp = alloc((unsigned)(oldlen + 1 - count));
2520 if (newp == NULL)
2521 return FAIL;
2522 mch_memmove(newp, oldp, (size_t)col);
2523 }
2524 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2525 if (!was_alloced)
2526 ml_replace(lnum, newp, FALSE);
2527
2528 /* mark the buffer as changed and prepare for displaying */
2529 changed_bytes(lnum, curwin->w_cursor.col);
2530
2531 return OK;
2532}
2533
2534/*
2535 * Delete from cursor to end of line.
2536 * Caller must have prepared for undo.
2537 *
2538 * return FAIL for failure, OK otherwise
2539 */
2540 int
2541truncate_line(fixpos)
2542 int fixpos; /* if TRUE fix the cursor position when done */
2543{
2544 char_u *newp;
2545 linenr_T lnum = curwin->w_cursor.lnum;
2546 colnr_T col = curwin->w_cursor.col;
2547
2548 if (col == 0)
2549 newp = vim_strsave((char_u *)"");
2550 else
2551 newp = vim_strnsave(ml_get(lnum), col);
2552
2553 if (newp == NULL)
2554 return FAIL;
2555
2556 ml_replace(lnum, newp, FALSE);
2557
2558 /* mark the buffer as changed and prepare for displaying */
2559 changed_bytes(lnum, curwin->w_cursor.col);
2560
2561 /*
2562 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2563 */
2564 if (fixpos && curwin->w_cursor.col > 0)
2565 --curwin->w_cursor.col;
2566
2567 return OK;
2568}
2569
2570/*
2571 * Delete "nlines" lines at the cursor.
2572 * Saves the lines for undo first if "undo" is TRUE.
2573 */
2574 void
2575del_lines(nlines, undo)
2576 long nlines; /* number of lines to delete */
2577 int undo; /* if TRUE, prepare for undo */
2578{
2579 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002580 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 if (nlines <= 0)
2583 return;
2584
2585 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002586 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 return;
2588
2589 for (n = 0; n < nlines; )
2590 {
2591 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2592 break;
2593
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002594 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 ++n;
2596
2597 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002598 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
2600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002602 /* Correct the cursor position before calling deleted_lines_mark(), it may
2603 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 curwin->w_cursor.col = 0;
2605 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002606
2607 /* adjust marks, mark the buffer as changed and prepare for displaying */
2608 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609}
2610
2611 int
2612gchar_pos(pos)
2613 pos_T *pos;
2614{
2615 char_u *ptr = ml_get_pos(pos);
2616
2617#ifdef FEAT_MBYTE
2618 if (has_mbyte)
2619 return (*mb_ptr2char)(ptr);
2620#endif
2621 return (int)*ptr;
2622}
2623
2624 int
2625gchar_cursor()
2626{
2627#ifdef FEAT_MBYTE
2628 if (has_mbyte)
2629 return (*mb_ptr2char)(ml_get_cursor());
2630#endif
2631 return (int)*ml_get_cursor();
2632}
2633
2634/*
2635 * Write a character at the current cursor position.
2636 * It is directly written into the block.
2637 */
2638 void
2639pchar_cursor(c)
2640 int c;
2641{
2642 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2643 + curwin->w_cursor.col) = c;
2644}
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646/*
2647 * When extra == 0: Return TRUE if the cursor is before or on the first
2648 * non-blank in the line.
2649 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2650 * the line.
2651 */
2652 int
2653inindent(extra)
2654 int extra;
2655{
2656 char_u *ptr;
2657 colnr_T col;
2658
2659 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2660 ++ptr;
2661 if (col >= curwin->w_cursor.col + extra)
2662 return TRUE;
2663 else
2664 return FALSE;
2665}
2666
2667/*
2668 * Skip to next part of an option argument: Skip space and comma.
2669 */
2670 char_u *
2671skip_to_option_part(p)
2672 char_u *p;
2673{
2674 if (*p == ',')
2675 ++p;
2676 while (*p == ' ')
2677 ++p;
2678 return p;
2679}
2680
2681/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002682 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 *
2684 * Most often called through changed_bytes() and changed_lines(), which also
2685 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002686 *
2687 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
2689 void
2690changed()
2691{
2692#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2693 /* The text of the preediting area is inserted, but this doesn't
2694 * mean a change of the buffer yet. That is delayed until the
2695 * text is committed. (this means preedit becomes empty) */
2696 if (im_is_preediting() && !xim_changed_while_preediting)
2697 return;
2698 xim_changed_while_preediting = FALSE;
2699#endif
2700
2701 if (!curbuf->b_changed)
2702 {
2703 int save_msg_scroll = msg_scroll;
2704
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002705 /* Give a warning about changing a read-only file. This may also
2706 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 /* Create a swap file if that is wanted.
2710 * Don't do this for "nofile" and "nowrite" buffer types. */
2711 if (curbuf->b_may_swap
2712#ifdef FEAT_QUICKFIX
2713 && !bt_dontwrite(curbuf)
2714#endif
2715 )
2716 {
2717 ml_open_file(curbuf);
2718
2719 /* The ml_open_file() can cause an ATTENTION message.
2720 * Wait two seconds, to make sure the user reads this unexpected
2721 * message. Since we could be anywhere, call wait_return() now,
2722 * and don't let the emsg() set msg_scroll. */
2723 if (need_wait_return && emsg_silent == 0)
2724 {
2725 out_flush();
2726 ui_delay(2000L, TRUE);
2727 wait_return(TRUE);
2728 msg_scroll = save_msg_scroll;
2729 }
2730 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002731 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734}
2735
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002736/*
2737 * Internal part of changed(), no user interaction.
2738 */
2739 void
2740changed_int()
2741{
2742 curbuf->b_changed = TRUE;
2743 ml_setflags(curbuf);
2744#ifdef FEAT_WINDOWS
2745 check_status(curbuf);
2746 redraw_tabline = TRUE;
2747#endif
2748#ifdef FEAT_TITLE
2749 need_maketitle = TRUE; /* set window title later */
2750#endif
2751}
2752
Bram Moolenaardba8a912005-04-24 22:08:39 +00002753static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2754static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2756
2757/*
2758 * Changed bytes within a single line for the current buffer.
2759 * - marks the windows on this buffer to be redisplayed
2760 * - marks the buffer changed by calling changed()
2761 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002762 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 */
2764 void
2765changed_bytes(lnum, col)
2766 linenr_T lnum;
2767 colnr_T col;
2768{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002769 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002771
2772#ifdef FEAT_DIFF
2773 /* Diff highlighting in other diff windows may need to be updated too. */
2774 if (curwin->w_p_diff)
2775 {
2776 win_T *wp;
2777 linenr_T wlnum;
2778
2779 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2780 if (wp->w_p_diff && wp != curwin)
2781 {
2782 redraw_win_later(wp, VALID);
2783 wlnum = diff_lnum_win(lnum, wp);
2784 if (wlnum > 0)
2785 changedOneline(wp->w_buffer, wlnum);
2786 }
2787 }
2788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789}
2790
2791 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002792changedOneline(buf, lnum)
2793 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 linenr_T lnum;
2795{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002796 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
2798 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002799 if (lnum < buf->b_mod_top)
2800 buf->b_mod_top = lnum;
2801 else if (lnum >= buf->b_mod_bot)
2802 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 else
2805 {
2806 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002807 buf->b_mod_set = TRUE;
2808 buf->b_mod_top = lnum;
2809 buf->b_mod_bot = lnum + 1;
2810 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812}
2813
2814/*
2815 * Appended "count" lines below line "lnum" in the current buffer.
2816 * Must be called AFTER the change and after mark_adjust().
2817 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2818 */
2819 void
2820appended_lines(lnum, count)
2821 linenr_T lnum;
2822 long count;
2823{
2824 changed_lines(lnum + 1, 0, lnum + 1, count);
2825}
2826
2827/*
2828 * Like appended_lines(), but adjust marks first.
2829 */
2830 void
2831appended_lines_mark(lnum, count)
2832 linenr_T lnum;
2833 long count;
2834{
2835 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2836 changed_lines(lnum + 1, 0, lnum + 1, count);
2837}
2838
2839/*
2840 * Deleted "count" lines at line "lnum" in the current buffer.
2841 * Must be called AFTER the change and after mark_adjust().
2842 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2843 */
2844 void
2845deleted_lines(lnum, count)
2846 linenr_T lnum;
2847 long count;
2848{
2849 changed_lines(lnum, 0, lnum + count, -count);
2850}
2851
2852/*
2853 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002854 * Make sure the cursor is on a valid line before calling, a GUI callback may
2855 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
2857 void
2858deleted_lines_mark(lnum, count)
2859 linenr_T lnum;
2860 long count;
2861{
2862 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2863 changed_lines(lnum, 0, lnum + count, -count);
2864}
2865
2866/*
2867 * Changed lines for the current buffer.
2868 * Must be called AFTER the change and after mark_adjust().
2869 * - mark the buffer changed by calling changed()
2870 * - mark the windows on this buffer to be redisplayed
2871 * - invalidate cached values
2872 * "lnum" is the first line that needs displaying, "lnume" the first line
2873 * below the changed lines (BEFORE the change).
2874 * When only inserting lines, "lnum" and "lnume" are equal.
2875 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002876 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 */
2878 void
2879changed_lines(lnum, col, lnume, xtra)
2880 linenr_T lnum; /* first line with change */
2881 colnr_T col; /* column in first line with change */
2882 linenr_T lnume; /* line below last changed line */
2883 long xtra; /* number of extra lines (negative when deleting) */
2884{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002885 changed_lines_buf(curbuf, lnum, lnume, xtra);
2886
2887#ifdef FEAT_DIFF
2888 if (xtra == 0 && curwin->w_p_diff)
2889 {
2890 /* When the number of lines doesn't change then mark_adjust() isn't
2891 * called and other diff buffers still need to be marked for
2892 * displaying. */
2893 win_T *wp;
2894 linenr_T wlnum;
2895
2896 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2897 if (wp->w_p_diff && wp != curwin)
2898 {
2899 redraw_win_later(wp, VALID);
2900 wlnum = diff_lnum_win(lnum, wp);
2901 if (wlnum > 0)
2902 changed_lines_buf(wp->w_buffer, wlnum,
2903 lnume - lnum + wlnum, 0L);
2904 }
2905 }
2906#endif
2907
2908 changed_common(lnum, col, lnume, xtra);
2909}
2910
2911 static void
2912changed_lines_buf(buf, lnum, lnume, xtra)
2913 buf_T *buf;
2914 linenr_T lnum; /* first line with change */
2915 linenr_T lnume; /* line below last changed line */
2916 long xtra; /* number of extra lines (negative when deleting) */
2917{
2918 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
2920 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002921 if (lnum < buf->b_mod_top)
2922 buf->b_mod_top = lnum;
2923 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
2925 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002926 buf->b_mod_bot += xtra;
2927 if (buf->b_mod_bot < lnum)
2928 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002930 if (lnume + xtra > buf->b_mod_bot)
2931 buf->b_mod_bot = lnume + xtra;
2932 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934 else
2935 {
2936 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002937 buf->b_mod_set = TRUE;
2938 buf->b_mod_top = lnum;
2939 buf->b_mod_bot = lnume + xtra;
2940 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
2943
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002944/*
2945 * Common code for when a change is was made.
2946 * See changed_lines() for the arguments.
2947 * Careful: may trigger autocommands that reload the buffer.
2948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 static void
2950changed_common(lnum, col, lnume, xtra)
2951 linenr_T lnum;
2952 colnr_T col;
2953 linenr_T lnume;
2954 long xtra;
2955{
2956 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002957#ifdef FEAT_WINDOWS
2958 tabpage_T *tp;
2959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 int i;
2961#ifdef FEAT_JUMPLIST
2962 int cols;
2963 pos_T *p;
2964 int add;
2965#endif
2966
2967 /* mark the buffer as modified */
2968 changed();
2969
2970 /* set the '. mark */
2971 if (!cmdmod.keepjumps)
2972 {
2973 curbuf->b_last_change.lnum = lnum;
2974 curbuf->b_last_change.col = col;
2975
2976#ifdef FEAT_JUMPLIST
2977 /* Create a new entry if a new undo-able change was started or we
2978 * don't have an entry yet. */
2979 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
2980 {
2981 if (curbuf->b_changelistlen == 0)
2982 add = TRUE;
2983 else
2984 {
2985 /* Don't create a new entry when the line number is the same
2986 * as the last one and the column is not too far away. Avoids
2987 * creating many entries for typing "xxxxx". */
2988 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
2989 if (p->lnum != lnum)
2990 add = TRUE;
2991 else
2992 {
2993 cols = comp_textwidth(FALSE);
2994 if (cols == 0)
2995 cols = 79;
2996 add = (p->col + cols < col || col + cols < p->col);
2997 }
2998 }
2999 if (add)
3000 {
3001 /* This is the first of a new sequence of undo-able changes
3002 * and it's at some distance of the last change. Use a new
3003 * position in the changelist. */
3004 curbuf->b_new_change = FALSE;
3005
3006 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3007 {
3008 /* changelist is full: remove oldest entry */
3009 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3010 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3011 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003012 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
3014 /* Correct position in changelist for other windows on
3015 * this buffer. */
3016 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3017 --wp->w_changelistidx;
3018 }
3019 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003020 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 {
3022 /* For other windows, if the position in the changelist is
3023 * at the end it stays at the end. */
3024 if (wp->w_buffer == curbuf
3025 && wp->w_changelistidx == curbuf->b_changelistlen)
3026 ++wp->w_changelistidx;
3027 }
3028 ++curbuf->b_changelistlen;
3029 }
3030 }
3031 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3032 curbuf->b_last_change;
3033 /* The current window is always after the last change, so that "g,"
3034 * takes you back to it. */
3035 curwin->w_changelistidx = curbuf->b_changelistlen;
3036#endif
3037 }
3038
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003039 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 {
3041 if (wp->w_buffer == curbuf)
3042 {
3043 /* Mark this window to be redrawn later. */
3044 if (wp->w_redr_type < VALID)
3045 wp->w_redr_type = VALID;
3046
3047 /* Check if a change in the buffer has invalidated the cached
3048 * values for the cursor. */
3049#ifdef FEAT_FOLDING
3050 /*
3051 * Update the folds for this window. Can't postpone this, because
3052 * a following operator might work on the whole fold: ">>dd".
3053 */
3054 foldUpdate(wp, lnum, lnume + xtra - 1);
3055
3056 /* The change may cause lines above or below the change to become
3057 * included in a fold. Set lnum/lnume to the first/last line that
3058 * might be displayed differently.
3059 * Set w_cline_folded here as an efficient way to update it when
3060 * inserting lines just above a closed fold. */
3061 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3062 if (wp->w_cursor.lnum == lnum)
3063 wp->w_cline_folded = i;
3064 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3065 if (wp->w_cursor.lnum == lnume)
3066 wp->w_cline_folded = i;
3067
3068 /* If the changed line is in a range of previously folded lines,
3069 * compare with the first line in that range. */
3070 if (wp->w_cursor.lnum <= lnum)
3071 {
3072 i = find_wl_entry(wp, lnum);
3073 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3074 changed_line_abv_curs_win(wp);
3075 }
3076#endif
3077
3078 if (wp->w_cursor.lnum > lnum)
3079 changed_line_abv_curs_win(wp);
3080 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3081 changed_cline_bef_curs_win(wp);
3082 if (wp->w_botline >= lnum)
3083 {
3084 /* Assume that botline doesn't change (inserted lines make
3085 * other lines scroll down below botline). */
3086 approximate_botline_win(wp);
3087 }
3088
3089 /* Check if any w_lines[] entries have become invalid.
3090 * For entries below the change: Correct the lnums for
3091 * inserted/deleted lines. Makes it possible to stop displaying
3092 * after the change. */
3093 for (i = 0; i < wp->w_lines_valid; ++i)
3094 if (wp->w_lines[i].wl_valid)
3095 {
3096 if (wp->w_lines[i].wl_lnum >= lnum)
3097 {
3098 if (wp->w_lines[i].wl_lnum < lnume)
3099 {
3100 /* line included in change */
3101 wp->w_lines[i].wl_valid = FALSE;
3102 }
3103 else if (xtra != 0)
3104 {
3105 /* line below change */
3106 wp->w_lines[i].wl_lnum += xtra;
3107#ifdef FEAT_FOLDING
3108 wp->w_lines[i].wl_lastlnum += xtra;
3109#endif
3110 }
3111 }
3112#ifdef FEAT_FOLDING
3113 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3114 {
3115 /* change somewhere inside this range of folded lines,
3116 * may need to be redrawn */
3117 wp->w_lines[i].wl_valid = FALSE;
3118 }
3119#endif
3120 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003121
3122#ifdef FEAT_FOLDING
3123 /* Take care of side effects for setting w_topline when folds have
3124 * changed. Esp. when the buffer was changed in another window. */
3125 if (hasAnyFolding(wp))
3126 set_topline(wp, wp->w_topline);
3127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 }
3130
3131 /* Call update_screen() later, which checks out what needs to be redrawn,
3132 * since it notices b_mod_set and then uses b_mod_*. */
3133 if (must_redraw < VALID)
3134 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003135
3136#ifdef FEAT_AUTOCMD
3137 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003138 if (lnum <= curwin->w_cursor.lnum
3139 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003140 last_cursormoved.lnum = 0;
3141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144/*
3145 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3146 */
3147 void
3148unchanged(buf, ff)
3149 buf_T *buf;
3150 int ff; /* also reset 'fileformat' */
3151{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003152 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003155 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (ff)
3157 save_file_ff(buf);
3158#ifdef FEAT_WINDOWS
3159 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003160 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162#ifdef FEAT_TITLE
3163 need_maketitle = TRUE; /* set window title later */
3164#endif
3165 }
3166 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167#ifdef FEAT_NETBEANS_INTG
3168 netbeans_unmodified(buf);
3169#endif
3170}
3171
3172#if defined(FEAT_WINDOWS) || defined(PROTO)
3173/*
3174 * check_status: called when the status bars for the buffer 'buf'
3175 * need to be updated
3176 */
3177 void
3178check_status(buf)
3179 buf_T *buf;
3180{
3181 win_T *wp;
3182
3183 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3184 if (wp->w_buffer == buf && wp->w_status_height)
3185 {
3186 wp->w_redr_status = TRUE;
3187 if (must_redraw < VALID)
3188 must_redraw = VALID;
3189 }
3190}
3191#endif
3192
3193/*
3194 * If the file is readonly, give a warning message with the first change.
3195 * Don't do this for autocommands.
3196 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003197 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003199 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 */
3201 void
3202change_warning(col)
3203 int col; /* column for message; non-zero when in insert
3204 mode and 'showmode' is on */
3205{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003206 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (curbuf->b_did_warn == FALSE
3209 && curbufIsChanged() == 0
3210#ifdef FEAT_AUTOCMD
3211 && !autocmd_busy
3212#endif
3213 && curbuf->b_p_ro)
3214 {
3215#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003216 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003218 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (!curbuf->b_p_ro)
3220 return;
3221#endif
3222 /*
3223 * Do what msg() does, but with a column offset if the warning should
3224 * be after the mode message.
3225 */
3226 msg_start();
3227 if (msg_row == Rows - 1)
3228 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003229 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003230 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3231#ifdef FEAT_EVAL
3232 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 msg_clr_eos();
3235 (void)msg_end();
3236 if (msg_silent == 0 && !silent_mode)
3237 {
3238 out_flush();
3239 ui_delay(1000L, TRUE); /* give the user time to think about it */
3240 }
3241 curbuf->b_did_warn = TRUE;
3242 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3243 if (msg_row < Rows - 1)
3244 showmode();
3245 }
3246}
3247
3248/*
3249 * Ask for a reply from the user, a 'y' or a 'n'.
3250 * No other characters are accepted, the message is repeated until a valid
3251 * reply is entered or CTRL-C is hit.
3252 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3253 * from any buffers but directly from the user.
3254 *
3255 * return the 'y' or 'n'
3256 */
3257 int
3258ask_yesno(str, direct)
3259 char_u *str;
3260 int direct;
3261{
3262 int r = ' ';
3263 int save_State = State;
3264
3265 if (exiting) /* put terminal in raw mode for this question */
3266 settmode(TMODE_RAW);
3267 ++no_wait_return;
3268#ifdef USE_ON_FLY_SCROLL
3269 dont_scroll = TRUE; /* disallow scrolling here */
3270#endif
3271 State = CONFIRM; /* mouse behaves like with :confirm */
3272#ifdef FEAT_MOUSE
3273 setmouse(); /* disables mouse for xterm */
3274#endif
3275 ++no_mapping;
3276 ++allow_keys; /* no mapping here, but recognize keys */
3277
3278 while (r != 'y' && r != 'n')
3279 {
3280 /* same highlighting as for wait_return */
3281 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3282 if (direct)
3283 r = get_keystroke();
3284 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003285 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (r == Ctrl_C || r == ESC)
3287 r = 'n';
3288 msg_putchar(r); /* show what you typed */
3289 out_flush();
3290 }
3291 --no_wait_return;
3292 State = save_State;
3293#ifdef FEAT_MOUSE
3294 setmouse();
3295#endif
3296 --no_mapping;
3297 --allow_keys;
3298
3299 return r;
3300}
3301
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003302#if defined(FEAT_MOUSE) || defined(PROTO)
3303/*
3304 * Return TRUE if "c" is a mouse key.
3305 */
3306 int
3307is_mouse_key(c)
3308 int c;
3309{
3310 return c == K_LEFTMOUSE
3311 || c == K_LEFTMOUSE_NM
3312 || c == K_LEFTDRAG
3313 || c == K_LEFTRELEASE
3314 || c == K_LEFTRELEASE_NM
3315 || c == K_MIDDLEMOUSE
3316 || c == K_MIDDLEDRAG
3317 || c == K_MIDDLERELEASE
3318 || c == K_RIGHTMOUSE
3319 || c == K_RIGHTDRAG
3320 || c == K_RIGHTRELEASE
3321 || c == K_MOUSEDOWN
3322 || c == K_MOUSEUP
3323 || c == K_MOUSELEFT
3324 || c == K_MOUSERIGHT
3325 || c == K_X1MOUSE
3326 || c == K_X1DRAG
3327 || c == K_X1RELEASE
3328 || c == K_X2MOUSE
3329 || c == K_X2DRAG
3330 || c == K_X2RELEASE;
3331}
3332#endif
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
3335 * Get a key stroke directly from the user.
3336 * Ignores mouse clicks and scrollbar events, except a click for the left
3337 * button (used at the more prompt).
3338 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3339 * Disadvantage: typeahead is ignored.
3340 * Translates the interrupt character for unix to ESC.
3341 */
3342 int
3343get_keystroke()
3344{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003345 char_u *buf = NULL;
3346 int buflen = 150;
3347 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 int len = 0;
3349 int n;
3350 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003351 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
3353 mapped_ctrl_c = FALSE; /* mappings are not used here */
3354 for (;;)
3355 {
3356 cursor_on();
3357 out_flush();
3358
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003359 /* Leave some room for check_termcode() to insert a key code into (max
3360 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3361 * bytes. */
3362 maxlen = (buflen - 6 - len) / 3;
3363 if (buf == NULL)
3364 buf = alloc(buflen);
3365 else if (maxlen < 10)
3366 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003367 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003368 * escape sequence. */
3369 buflen += 100;
3370 buf = vim_realloc(buf, buflen);
3371 maxlen = (buflen - 6 - len) / 3;
3372 }
3373 if (buf == NULL)
3374 {
3375 do_outofmem_msg((long_u)buflen);
3376 return ESC; /* panic! */
3377 }
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003380 * terminal code to complete. */
3381 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (n > 0)
3383 {
3384 /* Replace zero and CSI by a special key code. */
3385 n = fix_input_buffer(buf + len, n, FALSE);
3386 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003387 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003389 else if (len > 0)
3390 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar4395a712006-09-05 18:57:57 +00003392 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003393 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003394 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003396
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003397 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003398 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003399 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003400 {
3401 /* Redrawing was postponed, do it now. */
3402 update_screen(0);
3403 setcursor(); /* put cursor back where it belongs */
3404 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003405 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003406 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003407 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003409 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 continue;
3411
3412 /* Handle modifier and/or special key code. */
3413 n = buf[0];
3414 if (n == K_SPECIAL)
3415 {
3416 n = TO_SPECIAL(buf[1], buf[2]);
3417 if (buf[1] == KS_MODIFIER
3418 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003419#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003420 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003421#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003422#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 || n == K_VER_SCROLLBAR
3424 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#endif
3426 )
3427 {
3428 if (buf[1] == KS_MODIFIER)
3429 mod_mask = buf[2];
3430 len -= 3;
3431 if (len > 0)
3432 mch_memmove(buf, buf + 3, (size_t)len);
3433 continue;
3434 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437#ifdef FEAT_MBYTE
3438 if (has_mbyte)
3439 {
3440 if (MB_BYTE2LEN(n) > len)
3441 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003442 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 n = (*mb_ptr2char)(buf);
3444 }
3445#endif
3446#ifdef UNIX
3447 if (n == intr_char)
3448 n = ESC;
3449#endif
3450 break;
3451 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003452 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 mapped_ctrl_c = save_mapped_ctrl_c;
3455 return n;
3456}
3457
3458/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003459 * Get a number from the user.
3460 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 */
3462 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003463get_number(colon, mouse_used)
3464 int colon; /* allow colon to abort */
3465 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 int n = 0;
3468 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003469 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003471 if (mouse_used != NULL)
3472 *mouse_used = FALSE;
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 /* When not printing messages, the user won't know what to type, return a
3475 * zero (as if CR was hit). */
3476 if (msg_silent != 0)
3477 return 0;
3478
3479#ifdef USE_ON_FLY_SCROLL
3480 dont_scroll = TRUE; /* disallow scrolling here */
3481#endif
3482 ++no_mapping;
3483 ++allow_keys; /* no mapping here, but recognize keys */
3484 for (;;)
3485 {
3486 windgoto(msg_row, msg_col);
3487 c = safe_vgetc();
3488 if (VIM_ISDIGIT(c))
3489 {
3490 n = n * 10 + c - '0';
3491 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003492 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3495 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003496 if (typed > 0)
3497 {
3498 MSG_PUTS("\b \b");
3499 --typed;
3500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003503#ifdef FEAT_MOUSE
3504 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3505 {
3506 *mouse_used = TRUE;
3507 n = mouse_row + 1;
3508 break;
3509 }
3510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 else if (n == 0 && c == ':' && colon)
3512 {
3513 stuffcharReadbuff(':');
3514 if (!exmode_active)
3515 cmdline_row = msg_row;
3516 skip_redraw = TRUE; /* skip redraw once */
3517 do_redraw = FALSE;
3518 break;
3519 }
3520 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3521 break;
3522 }
3523 --no_mapping;
3524 --allow_keys;
3525 return n;
3526}
3527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003528/*
3529 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003530 * When "mouse_used" is not NULL allow using the mouse and in that case return
3531 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 */
3533 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003534prompt_for_number(mouse_used)
3535 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003536{
3537 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003538 int save_cmdline_row;
3539 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003540
3541 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003542 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003543 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003544 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003545 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003546
Bram Moolenaar203335e2006-09-03 14:35:42 +00003547 /* Set the state such that text can be selected/copied/pasted and we still
3548 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003549 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003550 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003551 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003552 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003553
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003554 i = get_number(TRUE, mouse_used);
3555 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003557 /* don't call wait_return() now */
3558 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003559 cmdline_row = msg_row - 1;
3560 need_wait_return = FALSE;
3561 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003562 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003563 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003564 else
3565 cmdline_row = save_cmdline_row;
3566 State = save_State;
3567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568 return i;
3569}
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 void
3572msgmore(n)
3573 long n;
3574{
3575 long pn;
3576
3577 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3579 return;
3580
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003581 /* We don't want to overwrite another important message, but do overwrite
3582 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3583 * then "put" reports the last action. */
3584 if (keep_msg != NULL && !keep_msg_more)
3585 return;
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (n > 0)
3588 pn = n;
3589 else
3590 pn = -n;
3591
3592 if (pn > p_report)
3593 {
3594 if (pn == 1)
3595 {
3596 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003597 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3598 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003600 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3601 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603 else
3604 {
3605 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003606 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3607 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003609 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3610 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003613 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (msg(msg_buf))
3615 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003616 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003617 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 }
3620}
3621
3622/*
3623 * flush map and typeahead buffers and give a warning for an error
3624 */
3625 void
3626beep_flush()
3627{
3628 if (emsg_silent == 0)
3629 {
3630 flush_buffers(FALSE);
3631 vim_beep();
3632 }
3633}
3634
3635/*
3636 * give a warning for an error
3637 */
3638 void
3639vim_beep()
3640{
3641 if (emsg_silent == 0)
3642 {
3643 if (p_vb
3644#ifdef FEAT_GUI
3645 /* While the GUI is starting up the termcap is set for the GUI
3646 * but the output still goes to a terminal. */
3647 && !(gui.in_use && gui.starting)
3648#endif
3649 )
3650 {
3651 out_str(T_VB);
3652 }
3653 else
3654 {
3655#ifdef MSDOS
3656 /*
3657 * The number of beeps outputted is reduced to avoid having to wait
3658 * for all the beeps to finish. This is only a problem on systems
3659 * where the beeps don't overlap.
3660 */
3661 if (beep_count == 0 || beep_count == 10)
3662 {
3663 out_char(BELL);
3664 beep_count = 1;
3665 }
3666 else
3667 ++beep_count;
3668#else
3669 out_char(BELL);
3670#endif
3671 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003672
3673 /* When 'verbose' is set and we are sourcing a script or executing a
3674 * function give the user a hint where the beep comes from. */
3675 if (vim_strchr(p_debug, 'e') != NULL)
3676 {
3677 msg_source(hl_attr(HLF_W));
3678 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681}
3682
3683/*
3684 * To get the "real" home directory:
3685 * - get value of $HOME
3686 * For Unix:
3687 * - go to that directory
3688 * - do mch_dirname() to get the real name of that directory.
3689 * This also works with mounts and links.
3690 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3691 */
3692static char_u *homedir = NULL;
3693
3694 void
3695init_homedir()
3696{
3697 char_u *var;
3698
Bram Moolenaar05159a02005-02-26 23:04:13 +00003699 /* In case we are called a second time (when 'encoding' changes). */
3700 vim_free(homedir);
3701 homedir = NULL;
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#ifdef VMS
3704 var = mch_getenv((char_u *)"SYS$LOGIN");
3705#else
3706 var = mch_getenv((char_u *)"HOME");
3707#endif
3708
3709 if (var != NULL && *var == NUL) /* empty is same as not set */
3710 var = NULL;
3711
3712#ifdef WIN3264
3713 /*
3714 * Weird but true: $HOME may contain an indirect reference to another
3715 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3716 * when $HOME is being set.
3717 */
3718 if (var != NULL && *var == '%')
3719 {
3720 char_u *p;
3721 char_u *exp;
3722
3723 p = vim_strchr(var + 1, '%');
3724 if (p != NULL)
3725 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003726 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 exp = mch_getenv(NameBuff);
3728 if (exp != NULL && *exp != NUL
3729 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3730 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003731 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 var = NameBuff;
3733 /* Also set $HOME, it's needed for _viminfo. */
3734 vim_setenv((char_u *)"HOME", NameBuff);
3735 }
3736 }
3737 }
3738
3739 /*
3740 * Typically, $HOME is not defined on Windows, unless the user has
3741 * specifically defined it for Vim's sake. However, on Windows NT
3742 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3743 * each user. Try constructing $HOME from these.
3744 */
3745 if (var == NULL)
3746 {
3747 char_u *homedrive, *homepath;
3748
3749 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3750 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003751 if (homepath == NULL || *homepath == NUL)
3752 homepath = "\\";
3753 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3755 {
3756 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3757 if (NameBuff[0] != NUL)
3758 {
3759 var = NameBuff;
3760 /* Also set $HOME, it's needed for _viminfo. */
3761 vim_setenv((char_u *)"HOME", NameBuff);
3762 }
3763 }
3764 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003765
3766# if defined(FEAT_MBYTE)
3767 if (enc_utf8 && var != NULL)
3768 {
3769 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003770 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003771
3772 /* Convert from active codepage to UTF-8. Other conversions are
3773 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003774 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003775 if (pp != NULL)
3776 {
3777 homedir = pp;
3778 return;
3779 }
3780 }
3781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#endif
3783
3784#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3785 /*
3786 * Default home dir is C:/
3787 * Best assumption we can make in such a situation.
3788 */
3789 if (var == NULL)
3790 var = "C:/";
3791#endif
3792 if (var != NULL)
3793 {
3794#ifdef UNIX
3795 /*
3796 * Change to the directory and get the actual path. This resolves
3797 * links. Don't do it when we can't return.
3798 */
3799 if (mch_dirname(NameBuff, MAXPATHL) == OK
3800 && mch_chdir((char *)NameBuff) == 0)
3801 {
3802 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3803 var = IObuff;
3804 if (mch_chdir((char *)NameBuff) != 0)
3805 EMSG(_(e_prev_dir));
3806 }
3807#endif
3808 homedir = vim_strsave(var);
3809 }
3810}
3811
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003812#if defined(EXITFREE) || defined(PROTO)
3813 void
3814free_homedir()
3815{
3816 vim_free(homedir);
3817}
Bram Moolenaar24305862012-08-15 14:05:05 +02003818
3819# ifdef FEAT_CMDL_COMPL
3820 void
3821free_users()
3822{
3823 ga_clear_strings(&ga_users);
3824}
3825# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003826#endif
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003829 * Call expand_env() and store the result in an allocated string.
3830 * This is not very memory efficient, this expects the result to be freed
3831 * again soon.
3832 */
3833 char_u *
3834expand_env_save(src)
3835 char_u *src;
3836{
3837 return expand_env_save_opt(src, FALSE);
3838}
3839
3840/*
3841 * Idem, but when "one" is TRUE handle the string as one file name, only
3842 * expand "~" at the start.
3843 */
3844 char_u *
3845expand_env_save_opt(src, one)
3846 char_u *src;
3847 int one;
3848{
3849 char_u *p;
3850
3851 p = alloc(MAXPATHL);
3852 if (p != NULL)
3853 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3854 return p;
3855}
3856
3857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 * Expand environment variable with path name.
3859 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003860 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 * If anything fails no expansion is done and dst equals src.
3862 */
3863 void
3864expand_env(src, dst, dstlen)
3865 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3866 char_u *dst; /* where to put the result */
3867 int dstlen; /* maximum length of the result */
3868{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003869 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870}
3871
3872 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003873expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003874 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 char_u *dst; /* where to put the result */
3876 int dstlen; /* maximum length of the result */
3877 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003878 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003879 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003881 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 char_u *tail;
3883 int c;
3884 char_u *var;
3885 int copy_char;
3886 int mustfree; /* var was allocated, need to free it later */
3887 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003888 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003890 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003891 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003892
3893 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 --dstlen; /* leave one char space for "\," */
3895 while (*src && dstlen > 0)
3896 {
3897 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003898 if ((*src == '$'
3899#ifdef VMS
3900 && at_start
3901#endif
3902 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3904 || *src == '%'
3905#endif
3906 || (*src == '~' && at_start))
3907 {
3908 mustfree = FALSE;
3909
3910 /*
3911 * The variable name is copied into dst temporarily, because it may
3912 * be a string in read-only memory and a NUL needs to be appended.
3913 */
3914 if (*src != '~') /* environment var */
3915 {
3916 tail = src + 1;
3917 var = dst;
3918 c = dstlen - 1;
3919
3920#ifdef UNIX
3921 /* Unix has ${var-name} type environment vars */
3922 if (*tail == '{' && !vim_isIDc('{'))
3923 {
3924 tail++; /* ignore '{' */
3925 while (c-- > 0 && *tail && *tail != '}')
3926 *var++ = *tail++;
3927 }
3928 else
3929#endif
3930 {
3931 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3932#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3933 || (*src == '%' && *tail != '%')
3934#endif
3935 ))
3936 {
3937#ifdef OS2 /* env vars only in uppercase */
3938 *var++ = TOUPPER_LOC(*tail);
3939 tail++; /* toupper() may be a macro! */
3940#else
3941 *var++ = *tail++;
3942#endif
3943 }
3944 }
3945
3946#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3947# ifdef UNIX
3948 if (src[1] == '{' && *tail != '}')
3949# else
3950 if (*src == '%' && *tail != '%')
3951# endif
3952 var = NULL;
3953 else
3954 {
3955# ifdef UNIX
3956 if (src[1] == '{')
3957# else
3958 if (*src == '%')
3959#endif
3960 ++tail;
3961#endif
3962 *var = NUL;
3963 var = vim_getenv(dst, &mustfree);
3964#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3965 }
3966#endif
3967 }
3968 /* home directory */
3969 else if ( src[1] == NUL
3970 || vim_ispathsep(src[1])
3971 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3972 {
3973 var = homedir;
3974 tail = src + 1;
3975 }
3976 else /* user directory */
3977 {
3978#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
3979 /*
3980 * Copy ~user to dst[], so we can put a NUL after it.
3981 */
3982 tail = src;
3983 var = dst;
3984 c = dstlen - 1;
3985 while ( c-- > 0
3986 && *tail
3987 && vim_isfilec(*tail)
3988 && !vim_ispathsep(*tail))
3989 *var++ = *tail++;
3990 *var = NUL;
3991# ifdef UNIX
3992 /*
3993 * If the system supports getpwnam(), use it.
3994 * Otherwise, or if getpwnam() fails, the shell is used to
3995 * expand ~user. This is slower and may fail if the shell
3996 * does not support ~user (old versions of /bin/sh).
3997 */
3998# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
3999 {
4000 struct passwd *pw;
4001
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004002 /* Note: memory allocated by getpwnam() is never freed.
4003 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 pw = getpwnam((char *)dst + 1);
4005 if (pw != NULL)
4006 var = (char_u *)pw->pw_dir;
4007 else
4008 var = NULL;
4009 }
4010 if (var == NULL)
4011# endif
4012 {
4013 expand_T xpc;
4014
4015 ExpandInit(&xpc);
4016 xpc.xp_context = EXPAND_FILES;
4017 var = ExpandOne(&xpc, dst, NULL,
4018 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 mustfree = TRUE;
4020 }
4021
4022# else /* !UNIX, thus VMS */
4023 /*
4024 * USER_HOME is a comma-separated list of
4025 * directories to search for the user account in.
4026 */
4027 {
4028 char_u test[MAXPATHL], paths[MAXPATHL];
4029 char_u *path, *next_path, *ptr;
4030 struct stat st;
4031
4032 STRCPY(paths, USER_HOME);
4033 next_path = paths;
4034 while (*next_path)
4035 {
4036 for (path = next_path; *next_path && *next_path != ',';
4037 next_path++);
4038 if (*next_path)
4039 *next_path++ = NUL;
4040 STRCPY(test, path);
4041 STRCAT(test, "/");
4042 STRCAT(test, dst + 1);
4043 if (mch_stat(test, &st) == 0)
4044 {
4045 var = alloc(STRLEN(test) + 1);
4046 STRCPY(var, test);
4047 mustfree = TRUE;
4048 break;
4049 }
4050 }
4051 }
4052# endif /* UNIX */
4053#else
4054 /* cannot expand user's home directory, so don't try */
4055 var = NULL;
4056 tail = (char_u *)""; /* for gcc */
4057#endif /* UNIX || VMS */
4058 }
4059
4060#ifdef BACKSLASH_IN_FILENAME
4061 /* If 'shellslash' is set change backslashes to forward slashes.
4062 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4063 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4064 {
4065 char_u *p = vim_strsave(var);
4066
4067 if (p != NULL)
4068 {
4069 if (mustfree)
4070 vim_free(var);
4071 var = p;
4072 mustfree = TRUE;
4073 forward_slash(var);
4074 }
4075 }
4076#endif
4077
4078 /* If "var" contains white space, escape it with a backslash.
4079 * Required for ":e ~/tt" when $HOME includes a space. */
4080 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4081 {
4082 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4083
4084 if (p != NULL)
4085 {
4086 if (mustfree)
4087 vim_free(var);
4088 var = p;
4089 mustfree = TRUE;
4090 }
4091 }
4092
4093 if (var != NULL && *var != NUL
4094 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4095 {
4096 STRCPY(dst, var);
4097 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004098 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 /* if var[] ends in a path separator and tail[] starts
4100 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004101 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4103 && dst[-1] != ':'
4104#endif
4105 && vim_ispathsep(*tail))
4106 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004107 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 src = tail;
4109 copy_char = FALSE;
4110 }
4111 if (mustfree)
4112 vim_free(var);
4113 }
4114
4115 if (copy_char) /* copy at least one char */
4116 {
4117 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004118 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004119 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4120 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 */
4122 at_start = FALSE;
4123 if (src[0] == '\\' && src[1] != NUL)
4124 {
4125 *dst++ = *src++;
4126 --dstlen;
4127 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004128 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 at_start = TRUE;
4130 *dst++ = *src++;
4131 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004132
4133 if (startstr != NULL && src - startstr_len >= srcp
4134 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4135 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 }
4138 *dst = NUL;
4139}
4140
4141/*
4142 * Vim's version of getenv().
4143 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004144 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004145 * "mustfree" is set to TRUE when returned is allocated, it must be
4146 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
4148 char_u *
4149vim_getenv(name, mustfree)
4150 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004151 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 char_u *p;
4154 char_u *pend;
4155 int vimruntime;
4156
4157#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4158 /* use "C:/" when $HOME is not set */
4159 if (STRCMP(name, "HOME") == 0)
4160 return homedir;
4161#endif
4162
4163 p = mch_getenv(name);
4164 if (p != NULL && *p == NUL) /* empty is the same as not set */
4165 p = NULL;
4166
4167 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004168 {
4169#if defined(FEAT_MBYTE) && defined(WIN3264)
4170 if (enc_utf8)
4171 {
4172 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004173 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004174
4175 /* Convert from active codepage to UTF-8. Other conversions are
4176 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004177 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004178 if (pp != NULL)
4179 {
4180 p = pp;
4181 *mustfree = TRUE;
4182 }
4183 }
4184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4189 if (!vimruntime && STRCMP(name, "VIM") != 0)
4190 return NULL;
4191
4192 /*
4193 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4194 * Don't do this when default_vimruntime_dir is non-empty.
4195 */
4196 if (vimruntime
4197#ifdef HAVE_PATHDEF
4198 && *default_vimruntime_dir == NUL
4199#endif
4200 )
4201 {
4202 p = mch_getenv((char_u *)"VIM");
4203 if (p != NULL && *p == NUL) /* empty is the same as not set */
4204 p = NULL;
4205 if (p != NULL)
4206 {
4207 p = vim_version_dir(p);
4208 if (p != NULL)
4209 *mustfree = TRUE;
4210 else
4211 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212
4213#if defined(FEAT_MBYTE) && defined(WIN3264)
4214 if (enc_utf8)
4215 {
4216 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004217 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004218
4219 /* Convert from active codepage to UTF-8. Other conversions
4220 * are not done, because they would fail for non-ASCII
4221 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004222 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004223 if (pp != NULL)
4224 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004225 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226 vim_free(p);
4227 p = pp;
4228 *mustfree = TRUE;
4229 }
4230 }
4231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 }
4234
4235 /*
4236 * When expanding $VIM or $VIMRUNTIME fails, try using:
4237 * - the directory name from 'helpfile' (unless it contains '$')
4238 * - the executable name from argv[0]
4239 */
4240 if (p == NULL)
4241 {
4242 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4243 p = p_hf;
4244#ifdef USE_EXE_NAME
4245 /*
4246 * Use the name of the executable, obtained from argv[0].
4247 */
4248 else
4249 p = exe_name;
4250#endif
4251 if (p != NULL)
4252 {
4253 /* remove the file name */
4254 pend = gettail(p);
4255
4256 /* remove "doc/" from 'helpfile', if present */
4257 if (p == p_hf)
4258 pend = remove_tail(p, pend, (char_u *)"doc");
4259
4260#ifdef USE_EXE_NAME
4261# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004262 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (p == exe_name)
4264 {
4265 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004266 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004268 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4269 if (pend1 != pend)
4270 {
4271 pnew = alloc((unsigned)(pend1 - p) + 15);
4272 if (pnew != NULL)
4273 {
4274 STRNCPY(pnew, p, (pend1 - p));
4275 STRCPY(pnew + (pend1 - p), "Resources/vim");
4276 p = pnew;
4277 pend = p + STRLEN(p);
4278 }
4279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281# endif
4282 /* remove "src/" from exe_name, if present */
4283 if (p == exe_name)
4284 pend = remove_tail(p, pend, (char_u *)"src");
4285#endif
4286
4287 /* for $VIM, remove "runtime/" or "vim54/", if present */
4288 if (!vimruntime)
4289 {
4290 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4291 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4292 }
4293
4294 /* remove trailing path separator */
4295#ifndef MACOS_CLASSIC
4296 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004297 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004298 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 --pend;
4300#endif
4301
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004302#ifdef MACOS_X
4303 if (p == exe_name || p == p_hf)
4304#endif
4305 /* check that the result is a directory name */
4306 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 if (p != NULL && !mch_isdir(p))
4309 {
4310 vim_free(p);
4311 p = NULL;
4312 }
4313 else
4314 {
4315#ifdef USE_EXE_NAME
4316 /* may add "/vim54" or "/runtime" if it exists */
4317 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4318 {
4319 vim_free(p);
4320 p = pend;
4321 }
4322#endif
4323 *mustfree = TRUE;
4324 }
4325 }
4326 }
4327
4328#ifdef HAVE_PATHDEF
4329 /* When there is a pathdef.c file we can use default_vim_dir and
4330 * default_vimruntime_dir */
4331 if (p == NULL)
4332 {
4333 /* Only use default_vimruntime_dir when it is not empty */
4334 if (vimruntime && *default_vimruntime_dir != NUL)
4335 {
4336 p = default_vimruntime_dir;
4337 *mustfree = FALSE;
4338 }
4339 else if (*default_vim_dir != NUL)
4340 {
4341 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4342 *mustfree = TRUE;
4343 else
4344 {
4345 p = default_vim_dir;
4346 *mustfree = FALSE;
4347 }
4348 }
4349 }
4350#endif
4351
4352 /*
4353 * Set the environment variable, so that the new value can be found fast
4354 * next time, and others can also use it (e.g. Perl).
4355 */
4356 if (p != NULL)
4357 {
4358 if (vimruntime)
4359 {
4360 vim_setenv((char_u *)"VIMRUNTIME", p);
4361 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 else
4364 {
4365 vim_setenv((char_u *)"VIM", p);
4366 didset_vim = TRUE;
4367 }
4368 }
4369 return p;
4370}
4371
4372/*
4373 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4374 * Return NULL if not, return its name in allocated memory otherwise.
4375 */
4376 static char_u *
4377vim_version_dir(vimdir)
4378 char_u *vimdir;
4379{
4380 char_u *p;
4381
4382 if (vimdir == NULL || *vimdir == NUL)
4383 return NULL;
4384 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4385 if (p != NULL && mch_isdir(p))
4386 return p;
4387 vim_free(p);
4388 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4389 if (p != NULL && mch_isdir(p))
4390 return p;
4391 vim_free(p);
4392 return NULL;
4393}
4394
4395/*
4396 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4397 * the length of "name/". Otherwise return "pend".
4398 */
4399 static char_u *
4400remove_tail(p, pend, name)
4401 char_u *p;
4402 char_u *pend;
4403 char_u *name;
4404{
4405 int len = (int)STRLEN(name) + 1;
4406 char_u *newend = pend - len;
4407
4408 if (newend >= p
4409 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004410 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return newend;
4412 return pend;
4413}
4414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 * Our portable version of setenv.
4417 */
4418 void
4419vim_setenv(name, val)
4420 char_u *name;
4421 char_u *val;
4422{
4423#ifdef HAVE_SETENV
4424 mch_setenv((char *)name, (char *)val, 1);
4425#else
4426 char_u *envbuf;
4427
4428 /*
4429 * Putenv does not copy the string, it has to remain
4430 * valid. The allocated memory will never be freed.
4431 */
4432 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4433 if (envbuf != NULL)
4434 {
4435 sprintf((char *)envbuf, "%s=%s", name, val);
4436 putenv((char *)envbuf);
4437 }
4438#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004439#ifdef FEAT_GETTEXT
4440 /*
4441 * When setting $VIMRUNTIME adjust the directory to find message
4442 * translations to $VIMRUNTIME/lang.
4443 */
4444 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4445 {
4446 char_u *buf = concat_str(val, (char_u *)"/lang");
4447
4448 if (buf != NULL)
4449 {
4450 bindtextdomain(VIMPACKAGE, (char *)buf);
4451 vim_free(buf);
4452 }
4453 }
4454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455}
4456
4457#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4458/*
4459 * Function given to ExpandGeneric() to obtain an environment variable name.
4460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 char_u *
4462get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004463 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 int idx;
4465{
4466# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4467 /*
4468 * No environ[] on the Amiga and on the Mac (using MPW).
4469 */
4470 return NULL;
4471# else
4472# ifndef __WIN32__
4473 /* Borland C++ 5.2 has this in a header file. */
4474 extern char **environ;
4475# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004476# define ENVNAMELEN 100
4477 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 char_u *str;
4479 int n;
4480
4481 str = (char_u *)environ[idx];
4482 if (str == NULL)
4483 return NULL;
4484
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004485 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
4487 if (str[n] == '=' || str[n] == NUL)
4488 break;
4489 name[n] = str[n];
4490 }
4491 name[n] = NUL;
4492 return name;
4493# endif
4494}
Bram Moolenaar24305862012-08-15 14:05:05 +02004495
4496/*
4497 * Find all user names for user completion.
4498 * Done only once and then cached.
4499 */
4500 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004501init_users()
4502{
Bram Moolenaar24305862012-08-15 14:05:05 +02004503 static int lazy_init_done = FALSE;
4504
4505 if (lazy_init_done)
4506 return;
4507
4508 lazy_init_done = TRUE;
4509 ga_init2(&ga_users, sizeof(char_u *), 20);
4510
4511# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4512 {
4513 char_u* user;
4514 struct passwd* pw;
4515
4516 setpwent();
4517 while ((pw = getpwent()) != NULL)
4518 /* pw->pw_name shouldn't be NULL but just in case... */
4519 if (pw->pw_name != NULL)
4520 {
4521 if (ga_grow(&ga_users, 1) == FAIL)
4522 break;
4523 user = vim_strsave((char_u*)pw->pw_name);
4524 if (user == NULL)
4525 break;
4526 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4527 }
4528 endpwent();
4529 }
4530# endif
4531}
4532
4533/*
4534 * Function given to ExpandGeneric() to obtain an user names.
4535 */
4536 char_u*
4537get_users(xp, idx)
4538 expand_T *xp UNUSED;
4539 int idx;
4540{
4541 init_users();
4542 if (idx < ga_users.ga_len)
4543 return ((char_u **)ga_users.ga_data)[idx];
4544 return NULL;
4545}
4546
4547/*
4548 * Check whether name matches a user name. Return:
4549 * 0 if name does not match any user name.
4550 * 1 if name partially matches the beginning of a user name.
4551 * 2 is name fully matches a user name.
4552 */
4553int match_user(name)
4554 char_u* name;
4555{
4556 int i;
4557 int n = (int)STRLEN(name);
4558 int result = 0;
4559
4560 init_users();
4561 for (i = 0; i < ga_users.ga_len; i++)
4562 {
4563 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4564 return 2; /* full match */
4565 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4566 result = 1; /* partial match */
4567 }
4568 return result;
4569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570#endif
4571
4572/*
4573 * Replace home directory by "~" in each space or comma separated file name in
4574 * 'src'.
4575 * If anything fails (except when out of space) dst equals src.
4576 */
4577 void
4578home_replace(buf, src, dst, dstlen, one)
4579 buf_T *buf; /* when not NULL, check for help files */
4580 char_u *src; /* input file name */
4581 char_u *dst; /* where to put the result */
4582 int dstlen; /* maximum length of the result */
4583 int one; /* if TRUE, only replace one file name, include
4584 spaces and commas in the file name. */
4585{
4586 size_t dirlen = 0, envlen = 0;
4587 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004588 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 char_u *p;
4590
4591 if (src == NULL)
4592 {
4593 *dst = NUL;
4594 return;
4595 }
4596
4597 /*
4598 * If the file is a help file, remove the path completely.
4599 */
4600 if (buf != NULL && buf->b_help)
4601 {
4602 STRCPY(dst, gettail(src));
4603 return;
4604 }
4605
4606 /*
4607 * We check both the value of the $HOME environment variable and the
4608 * "real" home directory.
4609 */
4610 if (homedir != NULL)
4611 dirlen = STRLEN(homedir);
4612
4613#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004614 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004616 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4617#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004618 /* Empty is the same as not set. */
4619 if (homedir_env != NULL && *homedir_env == NUL)
4620 homedir_env = NULL;
4621
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004622#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004623 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004624 {
4625 int usedlen = 0;
4626 int flen;
4627 char_u *fbuf = NULL;
4628
4629 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004630 (void)modify_fname((char_u *)":p", &usedlen,
4631 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004632 flen = (int)STRLEN(homedir_env);
4633 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4634 /* Remove the trailing / that is added to a directory. */
4635 homedir_env[flen - 1] = NUL;
4636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (homedir_env != NULL)
4640 envlen = STRLEN(homedir_env);
4641
4642 if (!one)
4643 src = skipwhite(src);
4644 while (*src && dstlen > 0)
4645 {
4646 /*
4647 * Here we are at the beginning of a file name.
4648 * First, check to see if the beginning of the file name matches
4649 * $HOME or the "real" home directory. Check that there is a '/'
4650 * after the match (so that if e.g. the file is "/home/pieter/bla",
4651 * and the home directory is "/home/piet", the file does not end up
4652 * as "~er/bla" (which would seem to indicate the file "bla" in user
4653 * er's home directory)).
4654 */
4655 p = homedir;
4656 len = dirlen;
4657 for (;;)
4658 {
4659 if ( len
4660 && fnamencmp(src, p, len) == 0
4661 && (vim_ispathsep(src[len])
4662 || (!one && (src[len] == ',' || src[len] == ' '))
4663 || src[len] == NUL))
4664 {
4665 src += len;
4666 if (--dstlen > 0)
4667 *dst++ = '~';
4668
4669 /*
4670 * If it's just the home directory, add "/".
4671 */
4672 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4673 *dst++ = '/';
4674 break;
4675 }
4676 if (p == homedir_env)
4677 break;
4678 p = homedir_env;
4679 len = envlen;
4680 }
4681
4682 /* if (!one) skip to separator: space or comma */
4683 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4684 *dst++ = *src++;
4685 /* skip separator */
4686 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4687 *dst++ = *src++;
4688 }
4689 /* if (dstlen == 0) out of space, what to do??? */
4690
4691 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004692
4693 if (homedir_env != homedir_env_orig)
4694 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695}
4696
4697/*
4698 * Like home_replace, store the replaced string in allocated memory.
4699 * When something fails, NULL is returned.
4700 */
4701 char_u *
4702home_replace_save(buf, src)
4703 buf_T *buf; /* when not NULL, check for help files */
4704 char_u *src; /* input file name */
4705{
4706 char_u *dst;
4707 unsigned len;
4708
4709 len = 3; /* space for "~/" and trailing NUL */
4710 if (src != NULL) /* just in case */
4711 len += (unsigned)STRLEN(src);
4712 dst = alloc(len);
4713 if (dst != NULL)
4714 home_replace(buf, src, dst, len, TRUE);
4715 return dst;
4716}
4717
4718/*
4719 * Compare two file names and return:
4720 * FPC_SAME if they both exist and are the same file.
4721 * FPC_SAMEX if they both don't exist and have the same file name.
4722 * FPC_DIFF if they both exist and are different files.
4723 * FPC_NOTX if they both don't exist.
4724 * FPC_DIFFX if one of them doesn't exist.
4725 * For the first name environment variables are expanded
4726 */
4727 int
4728fullpathcmp(s1, s2, checkname)
4729 char_u *s1, *s2;
4730 int checkname; /* when both don't exist, check file names */
4731{
4732#ifdef UNIX
4733 char_u exp1[MAXPATHL];
4734 char_u full1[MAXPATHL];
4735 char_u full2[MAXPATHL];
4736 struct stat st1, st2;
4737 int r1, r2;
4738
4739 expand_env(s1, exp1, MAXPATHL);
4740 r1 = mch_stat((char *)exp1, &st1);
4741 r2 = mch_stat((char *)s2, &st2);
4742 if (r1 != 0 && r2 != 0)
4743 {
4744 /* if mch_stat() doesn't work, may compare the names */
4745 if (checkname)
4746 {
4747 if (fnamecmp(exp1, s2) == 0)
4748 return FPC_SAMEX;
4749 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4750 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4751 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4752 return FPC_SAMEX;
4753 }
4754 return FPC_NOTX;
4755 }
4756 if (r1 != 0 || r2 != 0)
4757 return FPC_DIFFX;
4758 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4759 return FPC_SAME;
4760 return FPC_DIFF;
4761#else
4762 char_u *exp1; /* expanded s1 */
4763 char_u *full1; /* full path of s1 */
4764 char_u *full2; /* full path of s2 */
4765 int retval = FPC_DIFF;
4766 int r1, r2;
4767
4768 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4769 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4770 {
4771 full1 = exp1 + MAXPATHL;
4772 full2 = full1 + MAXPATHL;
4773
4774 expand_env(s1, exp1, MAXPATHL);
4775 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4776 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4777
4778 /* If vim_FullName() fails, the file probably doesn't exist. */
4779 if (r1 != OK && r2 != OK)
4780 {
4781 if (checkname && fnamecmp(exp1, s2) == 0)
4782 retval = FPC_SAMEX;
4783 else
4784 retval = FPC_NOTX;
4785 }
4786 else if (r1 != OK || r2 != OK)
4787 retval = FPC_DIFFX;
4788 else if (fnamecmp(full1, full2))
4789 retval = FPC_DIFF;
4790 else
4791 retval = FPC_SAME;
4792 vim_free(exp1);
4793 }
4794 return retval;
4795#endif
4796}
4797
4798/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004799 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004800 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004801 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
4803 char_u *
4804gettail(fname)
4805 char_u *fname;
4806{
4807 char_u *p1, *p2;
4808
4809 if (fname == NULL)
4810 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004811 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004813 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004815 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
4817 return p1;
4818}
4819
Bram Moolenaar31710262010-08-13 13:36:15 +02004820#if defined(FEAT_SEARCHPATH)
4821static char_u *gettail_dir __ARGS((char_u *fname));
4822
4823/*
4824 * Return the end of the directory name, on the first path
4825 * separator:
4826 * "/path/file", "/path/dir/", "/path//dir", "/file"
4827 * ^ ^ ^ ^
4828 */
4829 static char_u *
4830gettail_dir(fname)
4831 char_u *fname;
4832{
4833 char_u *dir_end = fname;
4834 char_u *next_dir_end = fname;
4835 int look_for_sep = TRUE;
4836 char_u *p;
4837
4838 for (p = fname; *p != NUL; )
4839 {
4840 if (vim_ispathsep(*p))
4841 {
4842 if (look_for_sep)
4843 {
4844 next_dir_end = p;
4845 look_for_sep = FALSE;
4846 }
4847 }
4848 else
4849 {
4850 if (!look_for_sep)
4851 dir_end = next_dir_end;
4852 look_for_sep = TRUE;
4853 }
4854 mb_ptr_adv(p);
4855 }
4856 return dir_end;
4857}
4858#endif
4859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004861 * Get pointer to tail of "fname", including path separators. Putting a NUL
4862 * here leaves the directory name. Takes care of "c:/" and "//".
4863 * Always returns a valid pointer.
4864 */
4865 char_u *
4866gettail_sep(fname)
4867 char_u *fname;
4868{
4869 char_u *p;
4870 char_u *t;
4871
4872 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4873 t = gettail(fname);
4874 while (t > p && after_pathsep(fname, t))
4875 --t;
4876#ifdef VMS
4877 /* path separator is part of the path */
4878 ++t;
4879#endif
4880 return t;
4881}
4882
4883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 * get the next path component (just after the next path separator).
4885 */
4886 char_u *
4887getnextcomp(fname)
4888 char_u *fname;
4889{
4890 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004891 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (*fname)
4893 ++fname;
4894 return fname;
4895}
4896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897/*
4898 * Get a pointer to one character past the head of a path name.
4899 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4900 * If there is no head, path is returned.
4901 */
4902 char_u *
4903get_past_head(path)
4904 char_u *path;
4905{
4906 char_u *retval;
4907
4908#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4909 /* may skip "c:" */
4910 if (isalpha(path[0]) && path[1] == ':')
4911 retval = path + 2;
4912 else
4913 retval = path;
4914#else
4915# if defined(AMIGA)
4916 /* may skip "label:" */
4917 retval = vim_strchr(path, ':');
4918 if (retval == NULL)
4919 retval = path;
4920# else /* Unix */
4921 retval = path;
4922# endif
4923#endif
4924
4925 while (vim_ispathsep(*retval))
4926 ++retval;
4927
4928 return retval;
4929}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004932 * Return TRUE if 'c' is a path separator.
4933 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
4935 int
4936vim_ispathsep(c)
4937 int c;
4938{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004939#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004941#else
4942# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004944# else
4945# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4947 return (c == ':' || c == '[' || c == ']' || c == '/'
4948 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004949# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004951# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954}
4955
Bram Moolenaar69c35002013-11-04 02:54:12 +01004956/*
4957 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4958 */
4959 int
4960vim_ispathsep_nocolon(c)
4961 int c;
4962{
4963 return vim_ispathsep(c)
4964#ifdef BACKSLASH_IN_FILENAME
4965 && c != ':'
4966#endif
4967 ;
4968}
4969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4971/*
4972 * return TRUE if 'c' is a path list separator.
4973 */
4974 int
4975vim_ispathlistsep(c)
4976 int c;
4977{
4978#ifdef UNIX
4979 return (c == ':');
4980#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004981 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#endif
4983}
4984#endif
4985
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004986#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4987 || defined(FEAT_EVAL) || defined(PROTO)
4988/*
4989 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4990 * It's done in-place.
4991 */
4992 void
4993shorten_dir(str)
4994 char_u *str;
4995{
4996 char_u *tail, *s, *d;
4997 int skip = FALSE;
4998
4999 tail = gettail(str);
5000 d = str;
5001 for (s = str; ; ++s)
5002 {
5003 if (s >= tail) /* copy the whole tail */
5004 {
5005 *d++ = *s;
5006 if (*s == NUL)
5007 break;
5008 }
5009 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5010 {
5011 *d++ = *s;
5012 skip = FALSE;
5013 }
5014 else if (!skip)
5015 {
5016 *d++ = *s; /* copy next char */
5017 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5018 skip = TRUE;
5019# ifdef FEAT_MBYTE
5020 if (has_mbyte)
5021 {
5022 int l = mb_ptr2len(s);
5023
5024 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005025 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005026 }
5027# endif
5028 }
5029 }
5030}
5031#endif
5032
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005033/*
5034 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5035 * Also returns TRUE if there is no directory name.
5036 * "fname" must be writable!.
5037 */
5038 int
5039dir_of_file_exists(fname)
5040 char_u *fname;
5041{
5042 char_u *p;
5043 int c;
5044 int retval;
5045
5046 p = gettail_sep(fname);
5047 if (p == fname)
5048 return TRUE;
5049 c = *p;
5050 *p = NUL;
5051 retval = mch_isdir(fname);
5052 *p = c;
5053 return retval;
5054}
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005057 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5058 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 */
5060 int
5061vim_fnamecmp(x, y)
5062 char_u *x, *y;
5063{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005064#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005066#else
5067 if (p_fic)
5068 return MB_STRICMP(x, y);
5069 return STRCMP(x, y);
5070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071}
5072
5073 int
5074vim_fnamencmp(x, y, len)
5075 char_u *x, *y;
5076 size_t len;
5077{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005078#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005079 char_u *px = x;
5080 char_u *py = y;
5081 int cx = NUL;
5082 int cy = NUL;
5083
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005084 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005086 cx = PTR2CHAR(px);
5087 cy = PTR2CHAR(py);
5088 if (cx == NUL || cy == NUL
5089 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5090 && !(cx == '/' && cy == '\\')
5091 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005093 len -= MB_PTR2LEN(px);
5094 px += MB_PTR2LEN(px);
5095 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 if (len == 0)
5098 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005099 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005100#else
5101 if (p_fic)
5102 return MB_STRNICMP(x, y, len);
5103 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107/*
5108 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005109 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
5111 char_u *
5112concat_fnames(fname1, fname2, sep)
5113 char_u *fname1;
5114 char_u *fname2;
5115 int sep;
5116{
5117 char_u *dest;
5118
5119 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5120 if (dest != NULL)
5121 {
5122 STRCPY(dest, fname1);
5123 if (sep)
5124 add_pathsep(dest);
5125 STRCAT(dest, fname2);
5126 }
5127 return dest;
5128}
5129
Bram Moolenaard6754642005-01-17 22:18:45 +00005130/*
5131 * Concatenate two strings and return the result in allocated memory.
5132 * Returns NULL when out of memory.
5133 */
5134 char_u *
5135concat_str(str1, str2)
5136 char_u *str1;
5137 char_u *str2;
5138{
5139 char_u *dest;
5140 size_t l = STRLEN(str1);
5141
5142 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5143 if (dest != NULL)
5144 {
5145 STRCPY(dest, str1);
5146 STRCPY(dest + l, str2);
5147 }
5148 return dest;
5149}
Bram Moolenaard6754642005-01-17 22:18:45 +00005150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
5152 * Add a path separator to a file name, unless it already ends in a path
5153 * separator.
5154 */
5155 void
5156add_pathsep(p)
5157 char_u *p;
5158{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005159 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 STRCAT(p, PATHSEPSTR);
5161}
5162
5163/*
5164 * FullName_save - Make an allocated copy of a full file name.
5165 * Returns NULL when out of memory.
5166 */
5167 char_u *
5168FullName_save(fname, force)
5169 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005170 int force; /* force expansion, even when it already looks
5171 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 char_u *buf;
5174 char_u *new_fname = NULL;
5175
5176 if (fname == NULL)
5177 return NULL;
5178
5179 buf = alloc((unsigned)MAXPATHL);
5180 if (buf != NULL)
5181 {
5182 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5183 new_fname = vim_strsave(buf);
5184 else
5185 new_fname = vim_strsave(fname);
5186 vim_free(buf);
5187 }
5188 return new_fname;
5189}
5190
5191#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5192
5193static char_u *skip_string __ARGS((char_u *p));
5194
5195/*
5196 * Find the start of a comment, not knowing if we are in a comment right now.
5197 * Search starts at w_cursor.lnum and goes backwards.
5198 */
5199 pos_T *
5200find_start_comment(ind_maxcomment) /* XXX */
5201 int ind_maxcomment;
5202{
5203 pos_T *pos;
5204 char_u *line;
5205 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005206 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005208 for (;;)
5209 {
5210 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5211 if (pos == NULL)
5212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005214 /*
5215 * Check if the comment start we found is inside a string.
5216 * If it is then restrict the search to below this line and try again.
5217 */
5218 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005219 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005220 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005221 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005222 break;
5223 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5224 if (cur_maxcomment <= 0)
5225 {
5226 pos = NULL;
5227 break;
5228 }
5229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 return pos;
5231}
5232
5233/*
5234 * Skip to the end of a "string" and a 'c' character.
5235 * If there is no string or character, return argument unmodified.
5236 */
5237 static char_u *
5238skip_string(p)
5239 char_u *p;
5240{
5241 int i;
5242
5243 /*
5244 * We loop, because strings may be concatenated: "date""time".
5245 */
5246 for ( ; ; ++p)
5247 {
5248 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5249 {
5250 if (!p[1]) /* ' at end of line */
5251 break;
5252 i = 2;
5253 if (p[1] == '\\') /* '\n' or '\000' */
5254 {
5255 ++i;
5256 while (vim_isdigit(p[i - 1])) /* '\000' */
5257 ++i;
5258 }
5259 if (p[i] == '\'') /* check for trailing ' */
5260 {
5261 p += i;
5262 continue;
5263 }
5264 }
5265 else if (p[0] == '"') /* start of string */
5266 {
5267 for (++p; p[0]; ++p)
5268 {
5269 if (p[0] == '\\' && p[1] != NUL)
5270 ++p;
5271 else if (p[0] == '"') /* end of string */
5272 break;
5273 }
5274 if (p[0] == '"')
5275 continue;
5276 }
5277 break; /* no string found */
5278 }
5279 if (!*p)
5280 --p; /* backup from NUL */
5281 return p;
5282}
5283#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5284
5285#if defined(FEAT_CINDENT) || defined(PROTO)
5286
5287/*
5288 * Do C or expression indenting on the current line.
5289 */
5290 void
5291do_c_expr_indent()
5292{
5293# ifdef FEAT_EVAL
5294 if (*curbuf->b_p_inde != NUL)
5295 fixthisline(get_expr_indent);
5296 else
5297# endif
5298 fixthisline(get_c_indent);
5299}
5300
5301/*
5302 * Functions for C-indenting.
5303 * Most of this originally comes from Eric Fischer.
5304 */
5305/*
5306 * Below "XXX" means that this function may unlock the current line.
5307 */
5308
5309static char_u *cin_skipcomment __ARGS((char_u *));
5310static int cin_nocode __ARGS((char_u *));
5311static pos_T *find_line_comment __ARGS((void));
5312static int cin_islabel_skip __ARGS((char_u **));
5313static int cin_isdefault __ARGS((char_u *));
5314static char_u *after_label __ARGS((char_u *l));
5315static int get_indent_nolabel __ARGS((linenr_T lnum));
5316static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment));
5317static int cin_first_id_amount __ARGS((void));
5318static int cin_get_equal_amount __ARGS((linenr_T lnum));
5319static int cin_ispreproc __ARGS((char_u *));
5320static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5321static int cin_iscomment __ARGS((char_u *));
5322static int cin_islinecomment __ARGS((char_u *));
5323static int cin_isterminated __ARGS((char_u *, int, int));
5324static int cin_isinit __ARGS((void));
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005325static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326static int cin_isif __ARGS((char_u *));
5327static int cin_iselse __ARGS((char_u *));
5328static int cin_isdo __ARGS((char_u *));
5329static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005330static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005331static int cin_iswhileofdo_end __ARGS((int terminated, int ind_maxparen, int ind_maxcomment));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005333static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005334static int get_baseclass_amount __ARGS((int col, int ind_maxparen, int ind_maxcomment, int ind_cpp_baseclass));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005336static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337static int cin_skip2pos __ARGS((pos_T *trypos));
5338static pos_T *find_start_brace __ARGS((int));
5339static pos_T *find_match_paren __ARGS((int, int));
5340static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos));
5341static int find_last_paren __ARGS((char_u *l, int start, int end));
5342static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005343static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005345static int ind_hash_comment = 0; /* # starts a comment */
5346
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347/*
5348 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005349 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 */
5351 static char_u *
5352cin_skipcomment(s)
5353 char_u *s;
5354{
5355 while (*s)
5356 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005357 char_u *prev_s = s;
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005360
5361 /* Perl/shell # comment comment continues until eol. Require a space
5362 * before # to avoid recognizing $#array. */
5363 if (ind_hash_comment != 0 && s != prev_s && *s == '#')
5364 {
5365 s += STRLEN(s);
5366 break;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (*s != '/')
5369 break;
5370 ++s;
5371 if (*s == '/') /* slash-slash comment continues till eol */
5372 {
5373 s += STRLEN(s);
5374 break;
5375 }
5376 if (*s != '*')
5377 break;
5378 for (++s; *s; ++s) /* skip slash-star comment */
5379 if (s[0] == '*' && s[1] == '/')
5380 {
5381 s += 2;
5382 break;
5383 }
5384 }
5385 return s;
5386}
5387
5388/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005389 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 * not considered code.
5391 */
5392 static int
5393cin_nocode(s)
5394 char_u *s;
5395{
5396 return *cin_skipcomment(s) == NUL;
5397}
5398
5399/*
5400 * Check previous lines for a "//" line comment, skipping over blank lines.
5401 */
5402 static pos_T *
5403find_line_comment() /* XXX */
5404{
5405 static pos_T pos;
5406 char_u *line;
5407 char_u *p;
5408
5409 pos = curwin->w_cursor;
5410 while (--pos.lnum > 0)
5411 {
5412 line = ml_get(pos.lnum);
5413 p = skipwhite(line);
5414 if (cin_islinecomment(p))
5415 {
5416 pos.col = (int)(p - line);
5417 return &pos;
5418 }
5419 if (*p != NUL)
5420 break;
5421 }
5422 return NULL;
5423}
5424
5425/*
5426 * Check if string matches "label:"; move to character after ':' if true.
5427 */
5428 static int
5429cin_islabel_skip(s)
5430 char_u **s;
5431{
5432 if (!vim_isIDc(**s)) /* need at least one ID character */
5433 return FALSE;
5434
5435 while (vim_isIDc(**s))
5436 (*s)++;
5437
5438 *s = cin_skipcomment(*s);
5439
5440 /* "::" is not a label, it's C++ */
5441 return (**s == ':' && *++*s != ':');
5442}
5443
5444/*
5445 * Recognize a label: "label:".
5446 * Note: curwin->w_cursor must be where we are looking for the label.
5447 */
5448 int
5449cin_islabel(ind_maxcomment) /* XXX */
5450 int ind_maxcomment;
5451{
5452 char_u *s;
5453
5454 s = cin_skipcomment(ml_get_curline());
5455
5456 /*
5457 * Exclude "default" from labels, since it should be indented
5458 * like a switch label. Same for C++ scope declarations.
5459 */
5460 if (cin_isdefault(s))
5461 return FALSE;
5462 if (cin_isscopedecl(s))
5463 return FALSE;
5464
5465 if (cin_islabel_skip(&s))
5466 {
5467 /*
5468 * Only accept a label if the previous line is terminated or is a case
5469 * label.
5470 */
5471 pos_T cursor_save;
5472 pos_T *trypos;
5473 char_u *line;
5474
5475 cursor_save = curwin->w_cursor;
5476 while (curwin->w_cursor.lnum > 1)
5477 {
5478 --curwin->w_cursor.lnum;
5479
5480 /*
5481 * If we're in a comment now, skip to the start of the comment.
5482 */
5483 curwin->w_cursor.col = 0;
5484 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
5485 curwin->w_cursor = *trypos;
5486
5487 line = ml_get_curline();
5488 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5489 continue;
5490 if (*(line = cin_skipcomment(line)) == NUL)
5491 continue;
5492
5493 curwin->w_cursor = cursor_save;
5494 if (cin_isterminated(line, TRUE, FALSE)
5495 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005496 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 || (cin_islabel_skip(&line) && cin_nocode(line)))
5498 return TRUE;
5499 return FALSE;
5500 }
5501 curwin->w_cursor = cursor_save;
5502 return TRUE; /* label at start of file??? */
5503 }
5504 return FALSE;
5505}
5506
5507/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005508 * Recognize structure initialization and enumerations:
5509 * "[typedef] [static|public|protected|private] enum"
5510 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 */
5512 static int
5513cin_isinit(void)
5514{
5515 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005516 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518 s = cin_skipcomment(ml_get_curline());
5519
Bram Moolenaar75342212013-03-07 13:13:52 +01005520 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 s = cin_skipcomment(s + 7);
5522
Bram Moolenaar75342212013-03-07 13:13:52 +01005523 for (;;)
5524 {
5525 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005526
Bram Moolenaar75342212013-03-07 13:13:52 +01005527 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5528 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005529 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005530 if (cin_starts_with(s, skip[i]))
5531 {
5532 s = cin_skipcomment(s + l);
5533 l = 0;
5534 break;
5535 }
5536 }
5537 if (l != 0)
5538 break;
5539 }
5540
5541 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 return TRUE;
5543
5544 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5545 return TRUE;
5546
5547 return FALSE;
5548}
5549
5550/*
5551 * Recognize a switch label: "case .*:" or "default:".
5552 */
5553 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005554cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005556 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005559 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
5561 for (s += 4; *s; ++s)
5562 {
5563 s = cin_skipcomment(s);
5564 if (*s == ':')
5565 {
5566 if (s[1] == ':') /* skip over "::" for C++ */
5567 ++s;
5568 else
5569 return TRUE;
5570 }
5571 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005572 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5574 return FALSE; /* stop at comment */
5575 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005576 {
5577 /* JS etc. */
5578 if (strict)
5579 return FALSE; /* stop at string */
5580 else
5581 return TRUE;
5582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584 return FALSE;
5585 }
5586
5587 if (cin_isdefault(s))
5588 return TRUE;
5589 return FALSE;
5590}
5591
5592/*
5593 * Recognize a "default" switch label.
5594 */
5595 static int
5596cin_isdefault(s)
5597 char_u *s;
5598{
5599 return (STRNCMP(s, "default", 7) == 0
5600 && *(s = cin_skipcomment(s + 7)) == ':'
5601 && s[1] != ':');
5602}
5603
5604/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005605 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 */
5607 int
5608cin_isscopedecl(s)
5609 char_u *s;
5610{
5611 int i;
5612
5613 s = cin_skipcomment(s);
5614 if (STRNCMP(s, "public", 6) == 0)
5615 i = 6;
5616 else if (STRNCMP(s, "protected", 9) == 0)
5617 i = 9;
5618 else if (STRNCMP(s, "private", 7) == 0)
5619 i = 7;
5620 else
5621 return FALSE;
5622 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5623}
5624
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005625/* Maximum number of lines to search back for a "namespace" line. */
5626#define FIND_NAMESPACE_LIM 20
5627
5628/*
5629 * Recognize a "namespace" scope declaration.
5630 */
5631 static int
5632cin_is_cpp_namespace(s)
5633 char_u *s;
5634{
5635 char_u *p;
5636 int has_name = FALSE;
5637
5638 s = cin_skipcomment(s);
5639 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5640 {
5641 p = cin_skipcomment(skipwhite(s + 9));
5642 while (*p != NUL)
5643 {
5644 if (vim_iswhite(*p))
5645 {
5646 has_name = TRUE; /* found end of a name */
5647 p = cin_skipcomment(skipwhite(p));
5648 }
5649 else if (*p == '{')
5650 {
5651 break;
5652 }
5653 else if (vim_iswordc(*p))
5654 {
5655 if (has_name)
5656 return FALSE; /* word character after skipping past name */
5657 ++p;
5658 }
5659 else
5660 {
5661 return FALSE;
5662 }
5663 }
5664 return TRUE;
5665 }
5666 return FALSE;
5667}
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669/*
5670 * Return a pointer to the first non-empty non-comment character after a ':'.
5671 * Return NULL if not found.
5672 * case 234: a = b;
5673 * ^
5674 */
5675 static char_u *
5676after_label(l)
5677 char_u *l;
5678{
5679 for ( ; *l; ++l)
5680 {
5681 if (*l == ':')
5682 {
5683 if (l[1] == ':') /* skip over "::" for C++ */
5684 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005685 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 break;
5687 }
5688 else if (*l == '\'' && l[1] && l[2] == '\'')
5689 l += 2; /* skip over 'x' */
5690 }
5691 if (*l == NUL)
5692 return NULL;
5693 l = cin_skipcomment(l + 1);
5694 if (*l == NUL)
5695 return NULL;
5696 return l;
5697}
5698
5699/*
5700 * Get indent of line "lnum", skipping a label.
5701 * Return 0 if there is nothing after the label.
5702 */
5703 static int
5704get_indent_nolabel(lnum) /* XXX */
5705 linenr_T lnum;
5706{
5707 char_u *l;
5708 pos_T fp;
5709 colnr_T col;
5710 char_u *p;
5711
5712 l = ml_get(lnum);
5713 p = after_label(l);
5714 if (p == NULL)
5715 return 0;
5716
5717 fp.col = (colnr_T)(p - l);
5718 fp.lnum = lnum;
5719 getvcol(curwin, &fp, &col, NULL, NULL);
5720 return (int)col;
5721}
5722
5723/*
5724 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005725 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 * label: if (asdf && asdfasdf)
5727 * ^
5728 */
5729 static int
5730skip_label(lnum, pp, ind_maxcomment)
5731 linenr_T lnum;
5732 char_u **pp;
5733 int ind_maxcomment;
5734{
5735 char_u *l;
5736 int amount;
5737 pos_T cursor_save;
5738
5739 cursor_save = curwin->w_cursor;
5740 curwin->w_cursor.lnum = lnum;
5741 l = ml_get_curline();
5742 /* XXX */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005743 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)
5744 || cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
5746 amount = get_indent_nolabel(lnum);
5747 l = after_label(ml_get_curline());
5748 if (l == NULL) /* just in case */
5749 l = ml_get_curline();
5750 }
5751 else
5752 {
5753 amount = get_indent();
5754 l = ml_get_curline();
5755 }
5756 *pp = l;
5757
5758 curwin->w_cursor = cursor_save;
5759 return amount;
5760}
5761
5762/*
5763 * Return the indent of the first variable name after a type in a declaration.
5764 * int a, indent of "a"
5765 * static struct foo b, indent of "b"
5766 * enum bla c, indent of "c"
5767 * Returns zero when it doesn't look like a declaration.
5768 */
5769 static int
5770cin_first_id_amount()
5771{
5772 char_u *line, *p, *s;
5773 int len;
5774 pos_T fp;
5775 colnr_T col;
5776
5777 line = ml_get_curline();
5778 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005779 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5781 {
5782 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005783 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
5785 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5786 p = skipwhite(p + 6);
5787 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5788 p = skipwhite(p + 4);
5789 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5790 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5791 {
5792 s = skipwhite(p + len);
5793 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5794 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5795 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5796 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5797 p = s;
5798 }
5799 for (len = 0; vim_isIDc(p[len]); ++len)
5800 ;
5801 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5802 return 0;
5803
5804 p = skipwhite(p + len);
5805 fp.lnum = curwin->w_cursor.lnum;
5806 fp.col = (colnr_T)(p - line);
5807 getvcol(curwin, &fp, &col, NULL, NULL);
5808 return (int)col;
5809}
5810
5811/*
5812 * Return the indent of the first non-blank after an equal sign.
5813 * char *foo = "here";
5814 * Return zero if no (useful) equal sign found.
5815 * Return -1 if the line above "lnum" ends in a backslash.
5816 * foo = "asdf\
5817 * asdf\
5818 * here";
5819 */
5820 static int
5821cin_get_equal_amount(lnum)
5822 linenr_T lnum;
5823{
5824 char_u *line;
5825 char_u *s;
5826 colnr_T col;
5827 pos_T fp;
5828
5829 if (lnum > 1)
5830 {
5831 line = ml_get(lnum - 1);
5832 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5833 return -1;
5834 }
5835
5836 line = s = ml_get(lnum);
5837 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5838 {
5839 if (cin_iscomment(s)) /* ignore comments */
5840 s = cin_skipcomment(s);
5841 else
5842 ++s;
5843 }
5844 if (*s != '=')
5845 return 0;
5846
5847 s = skipwhite(s + 1);
5848 if (cin_nocode(s))
5849 return 0;
5850
5851 if (*s == '"') /* nice alignment for continued strings */
5852 ++s;
5853
5854 fp.lnum = lnum;
5855 fp.col = (colnr_T)(s - line);
5856 getvcol(curwin, &fp, &col, NULL, NULL);
5857 return (int)col;
5858}
5859
5860/*
5861 * Recognize a preprocessor statement: Any line that starts with '#'.
5862 */
5863 static int
5864cin_ispreproc(s)
5865 char_u *s;
5866{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005867 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 return TRUE;
5869 return FALSE;
5870}
5871
5872/*
5873 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5874 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5875 * start and return the line in "*pp".
5876 */
5877 static int
5878cin_ispreproc_cont(pp, lnump)
5879 char_u **pp;
5880 linenr_T *lnump;
5881{
5882 char_u *line = *pp;
5883 linenr_T lnum = *lnump;
5884 int retval = FALSE;
5885
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005886 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
5888 if (cin_ispreproc(line))
5889 {
5890 retval = TRUE;
5891 *lnump = lnum;
5892 break;
5893 }
5894 if (lnum == 1)
5895 break;
5896 line = ml_get(--lnum);
5897 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5898 break;
5899 }
5900
5901 if (lnum != *lnump)
5902 *pp = ml_get(*lnump);
5903 return retval;
5904}
5905
5906/*
5907 * Recognize the start of a C or C++ comment.
5908 */
5909 static int
5910cin_iscomment(p)
5911 char_u *p;
5912{
5913 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5914}
5915
5916/*
5917 * Recognize the start of a "//" comment.
5918 */
5919 static int
5920cin_islinecomment(p)
5921 char_u *p;
5922{
5923 return (p[0] == '/' && p[1] == '/');
5924}
5925
5926/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005927 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5928 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005930 * If a line begins with an "else", only consider it terminated if no unmatched
5931 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 * Return the character terminating the line (ending char's have precedence if
5933 * both apply in order to determine initializations).
5934 */
5935 static int
5936cin_isterminated(s, incl_open, incl_comma)
5937 char_u *s;
5938 int incl_open; /* include '{' at the end as terminator */
5939 int incl_comma; /* recognize a trailing comma */
5940{
Bram Moolenaar496f9512011-05-19 16:35:09 +02005941 char_u found_start = 0;
5942 unsigned n_open = 0;
5943 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 s = cin_skipcomment(s);
5946
5947 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
5948 found_start = *s;
5949
Bram Moolenaar496f9512011-05-19 16:35:09 +02005950 if (!found_start)
5951 is_else = cin_iselse(s);
5952
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 while (*s)
5954 {
5955 /* skip over comments, "" strings and 'c'haracters */
5956 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005957 if (*s == '}' && n_open > 0)
5958 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02005959 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005960 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 && cin_nocode(s + 1))
5962 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005963 else if (*s == '{')
5964 {
5965 if (incl_open && cin_nocode(s + 1))
5966 return *s;
5967 else
5968 ++n_open;
5969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
5971 if (*s)
5972 s++;
5973 }
5974 return found_start;
5975}
5976
5977/*
5978 * Recognize the basic picture of a function declaration -- it needs to
5979 * have an open paren somewhere and a close paren at the end of the line and
5980 * no semicolons anywhere.
5981 * When a line ends in a comma we continue looking in the next line.
5982 * "sp" points to a string with the line. When looking at other lines it must
5983 * be restored to the line. When it's NULL fetch lines here.
5984 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005985 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 */
5987 static int
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005988cin_isfuncdecl(sp, first_lnum, min_lnum, ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 char_u **sp;
5990 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005991 linenr_T min_lnum;
5992 int ind_maxparen;
5993 int ind_maxcomment;
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, '(', ')')
6007 && (trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL)
6008 {
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
6115cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */
6116 char_u *p;
6117 linenr_T lnum;
6118 int ind_maxparen;
6119{
6120 pos_T cursor_save;
6121 pos_T *trypos;
6122 int retval = FALSE;
6123
6124 p = cin_skipcomment(p);
6125 if (*p == '}') /* accept "} while (cond);" */
6126 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006127 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 cursor_save = curwin->w_cursor;
6130 curwin->w_cursor.lnum = lnum;
6131 curwin->w_cursor.col = 0;
6132 p = ml_get_curline();
6133 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6134 {
6135 ++p;
6136 ++curwin->w_cursor.col;
6137 }
6138 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL
6139 && *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
6201cin_iswhileofdo_end(terminated, ind_maxparen, ind_maxcomment)
6202 int terminated;
6203 int ind_maxparen;
6204 int ind_maxcomment;
6205{
6206 char_u *line;
6207 char_u *p;
6208 char_u *s;
6209 pos_T *trypos;
6210 int i;
6211
6212 if (terminated != ';') /* there must be a ';' at the end */
6213 return FALSE;
6214
6215 p = line = ml_get_curline();
6216 while (*p != NUL)
6217 {
6218 p = cin_skipcomment(p);
6219 if (*p == ')')
6220 {
6221 s = skipwhite(p + 1);
6222 if (*s == ';' && cin_nocode(s + 1))
6223 {
6224 /* Found ");" at end of the line, now check there is "while"
6225 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006226 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006227 curwin->w_cursor.col = i;
6228 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
6229 if (trypos != NULL)
6230 {
6231 s = cin_skipcomment(ml_get(trypos->lnum));
6232 if (*s == '}') /* accept "} while (cond);" */
6233 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006234 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006235 {
6236 curwin->w_cursor.lnum = trypos->lnum;
6237 return TRUE;
6238 }
6239 }
6240
6241 /* Searching may have made "line" invalid, get it again. */
6242 line = ml_get_curline();
6243 p = line + i;
6244 }
6245 }
6246 if (*p != NUL)
6247 ++p;
6248 }
6249 return FALSE;
6250}
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 static int
6253cin_isbreak(p)
6254 char_u *p;
6255{
6256 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6257}
6258
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006259/*
6260 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 * constructor-initialization. eg:
6262 *
6263 * class MyClass :
6264 * baseClass <-- here
6265 * class MyClass : public baseClass,
6266 * anotherBaseClass <-- here (should probably lineup ??)
6267 * MyClass::MyClass(...) :
6268 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006269 *
6270 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 */
6272 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006273cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006274 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
6276 char_u *s;
6277 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006278 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006279 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
6281 *col = 0;
6282
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006283 s = skipwhite(line);
6284 if (*s == '#') /* skip #define FOO x ? (x) : x */
6285 return FALSE;
6286 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 if (*s == NUL)
6288 return FALSE;
6289
6290 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6291
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006292 /* Search for a line starting with '#', empty, ending in ';' or containing
6293 * '{' or '}' and start below it. This handles the following situations:
6294 * a = cond ?
6295 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006296 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006297 * func::foo()
6298 * : something
6299 * {}
6300 * Foo::Foo (int one, int two)
6301 * : something(4),
6302 * somethingelse(3)
6303 * {}
6304 */
6305 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006307 line = ml_get(lnum - 1);
6308 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006309 if (*s == '#' || *s == NUL)
6310 break;
6311 while (*s != NUL)
6312 {
6313 s = cin_skipcomment(s);
6314 if (*s == '{' || *s == '}'
6315 || (*s == ';' && cin_nocode(s + 1)))
6316 break;
6317 if (*s != NUL)
6318 ++s;
6319 }
6320 if (*s != NUL)
6321 break;
6322 --lnum;
6323 }
6324
Bram Moolenaare7c56862007-08-04 10:14:52 +00006325 line = ml_get(lnum);
6326 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006327 for (;;)
6328 {
6329 if (*s == NUL)
6330 {
6331 if (lnum == curwin->w_cursor.lnum)
6332 break;
6333 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006334 line = ml_get(++lnum);
6335 s = cin_skipcomment(line);
6336 if (*s == NUL)
6337 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006338 }
6339
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006340 if (s[0] == '"')
6341 s = skip_string(s) + 1;
6342 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 {
6344 if (s[1] == ':')
6345 {
6346 /* skip double colon. It can't be a constructor
6347 * initialization any more */
6348 lookfor_ctor_init = FALSE;
6349 s = cin_skipcomment(s + 2);
6350 }
6351 else if (lookfor_ctor_init || class_or_struct)
6352 {
6353 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006354 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 cpp_base_class = TRUE;
6356 lookfor_ctor_init = class_or_struct = FALSE;
6357 *col = 0;
6358 s = cin_skipcomment(s + 1);
6359 }
6360 else
6361 s = cin_skipcomment(s + 1);
6362 }
6363 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6364 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6365 {
6366 class_or_struct = TRUE;
6367 lookfor_ctor_init = FALSE;
6368
6369 if (*s == 'c')
6370 s = cin_skipcomment(s + 5);
6371 else
6372 s = cin_skipcomment(s + 6);
6373 }
6374 else
6375 {
6376 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6377 {
6378 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6379 }
6380 else if (s[0] == ')')
6381 {
6382 /* Constructor-initialization is assumed if we come across
6383 * something like "):" */
6384 class_or_struct = FALSE;
6385 lookfor_ctor_init = TRUE;
6386 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006387 else if (s[0] == '?')
6388 {
6389 /* Avoid seeing '() :' after '?' as constructor init. */
6390 return FALSE;
6391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 else if (!vim_isIDc(s[0]))
6393 {
6394 /* if it is not an identifier, we are wrong */
6395 class_or_struct = FALSE;
6396 lookfor_ctor_init = FALSE;
6397 }
6398 else if (*col == 0)
6399 {
6400 /* it can't be a constructor-initialization any more */
6401 lookfor_ctor_init = FALSE;
6402
6403 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006404 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 *col = (colnr_T)(s - line);
6406 }
6407
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006408 /* When the line ends in a comma don't align with it. */
6409 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6410 *col = 0;
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 s = cin_skipcomment(s + 1);
6413 }
6414 }
6415
6416 return cpp_base_class;
6417}
6418
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006419 static int
6420get_baseclass_amount(col, ind_maxparen, ind_maxcomment, ind_cpp_baseclass)
6421 int col;
6422 int ind_maxparen;
6423 int ind_maxcomment;
6424 int ind_cpp_baseclass;
6425{
6426 int amount;
6427 colnr_T vcol;
6428 pos_T *trypos;
6429
6430 if (col == 0)
6431 {
6432 amount = get_indent();
6433 if (find_last_paren(ml_get_curline(), '(', ')')
6434 && (trypos = find_match_paren(ind_maxparen,
6435 ind_maxcomment)) != NULL)
6436 amount = get_indent_lnum(trypos->lnum); /* XXX */
6437 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
6438 amount += ind_cpp_baseclass;
6439 }
6440 else
6441 {
6442 curwin->w_cursor.col = col;
6443 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6444 amount = (int)vcol;
6445 }
6446 if (amount < ind_cpp_baseclass)
6447 amount = ind_cpp_baseclass;
6448 return amount;
6449}
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451/*
6452 * Return TRUE if string "s" ends with the string "find", possibly followed by
6453 * white space and comments. Skip strings and comments.
6454 * Ignore "ignore" after "find" if it's not NULL.
6455 */
6456 static int
6457cin_ends_in(s, find, ignore)
6458 char_u *s;
6459 char_u *find;
6460 char_u *ignore;
6461{
6462 char_u *p = s;
6463 char_u *r;
6464 int len = (int)STRLEN(find);
6465
6466 while (*p != NUL)
6467 {
6468 p = cin_skipcomment(p);
6469 if (STRNCMP(p, find, len) == 0)
6470 {
6471 r = skipwhite(p + len);
6472 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6473 r = skipwhite(r + STRLEN(ignore));
6474 if (cin_nocode(r))
6475 return TRUE;
6476 }
6477 if (*p != NUL)
6478 ++p;
6479 }
6480 return FALSE;
6481}
6482
6483/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006484 * Return TRUE when "s" starts with "word" and then a non-ID character.
6485 */
6486 static int
6487cin_starts_with(s, word)
6488 char_u *s;
6489 char *word;
6490{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006491 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006492
6493 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6494}
6495
6496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 * Skip strings, chars and comments until at or past "trypos".
6498 * Return the column found.
6499 */
6500 static int
6501cin_skip2pos(trypos)
6502 pos_T *trypos;
6503{
6504 char_u *line;
6505 char_u *p;
6506
6507 p = line = ml_get(trypos->lnum);
6508 while (*p && (colnr_T)(p - line) < trypos->col)
6509 {
6510 if (cin_iscomment(p))
6511 p = cin_skipcomment(p);
6512 else
6513 {
6514 p = skip_string(p);
6515 ++p;
6516 }
6517 }
6518 return (int)(p - line);
6519}
6520
6521/*
6522 * Find the '{' at the start of the block we are in.
6523 * Return NULL if no match found.
6524 * Ignore a '{' that is in a comment, makes indenting the next three lines
6525 * work. */
6526/* foo() */
6527/* { */
6528/* } */
6529
6530 static pos_T *
6531find_start_brace(ind_maxcomment) /* XXX */
6532 int ind_maxcomment;
6533{
6534 pos_T cursor_save;
6535 pos_T *trypos;
6536 pos_T *pos;
6537 static pos_T pos_copy;
6538
6539 cursor_save = curwin->w_cursor;
6540 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6541 {
6542 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6543 trypos = &pos_copy;
6544 curwin->w_cursor = *trypos;
6545 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006546 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
6548 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */
6549 break;
6550 if (pos != NULL)
6551 curwin->w_cursor.lnum = pos->lnum;
6552 }
6553 curwin->w_cursor = cursor_save;
6554 return trypos;
6555}
6556
6557/*
6558 * Find the matching '(', failing if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006559 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 */
6561 static pos_T *
6562find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */
6563 int ind_maxparen;
6564 int ind_maxcomment;
6565{
6566 pos_T cursor_save;
6567 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006568 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 cursor_save = curwin->w_cursor;
6571 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6572 {
6573 /* check if the ( is in a // comment */
6574 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6575 trypos = NULL;
6576 else
6577 {
6578 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6579 trypos = &pos_copy;
6580 curwin->w_cursor = *trypos;
6581 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */
6582 trypos = NULL;
6583 }
6584 }
6585 curwin->w_cursor = cursor_save;
6586 return trypos;
6587}
6588
6589/*
6590 * Return ind_maxparen corrected for the difference in line number between the
6591 * cursor position and "startpos". This makes sure that searching for a
6592 * matching paren above the cursor line doesn't find a match because of
6593 * looking a few lines further.
6594 */
6595 static int
6596corr_ind_maxparen(ind_maxparen, startpos)
6597 int ind_maxparen;
6598 pos_T *startpos;
6599{
6600 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6601
6602 if (n > 0 && n < ind_maxparen / 2)
6603 return ind_maxparen - (int)n;
6604 return ind_maxparen;
6605}
6606
6607/*
6608 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006609 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 */
6611 static int
6612find_last_paren(l, start, end)
6613 char_u *l;
6614 int start, end;
6615{
6616 int i;
6617 int retval = FALSE;
6618 int open_count = 0;
6619
6620 curwin->w_cursor.col = 0; /* default is start of line */
6621
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006622 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 {
6624 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6625 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6626 if (l[i] == start)
6627 ++open_count;
6628 else if (l[i] == end)
6629 {
6630 if (open_count > 0)
6631 --open_count;
6632 else
6633 {
6634 curwin->w_cursor.col = i;
6635 retval = TRUE;
6636 }
6637 }
6638 }
6639 return retval;
6640}
6641
6642 int
6643get_c_indent()
6644{
Bram Moolenaar14f24742012-08-08 18:01:05 +02006645 int sw = (int)get_sw_value();
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 /*
6648 * spaces from a block's opening brace the prevailing indent for that
6649 * block should be
6650 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006651
6652 int ind_level = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654 /*
6655 * spaces from the edge of the line an open brace that's at the end of a
6656 * line is imagined to be.
6657 */
6658 int ind_open_imag = 0;
6659
6660 /*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006661 * spaces from the prevailing indent for a line that is not preceded by
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 * an opening brace.
6663 */
6664 int ind_no_brace = 0;
6665
6666 /*
6667 * column where the first { of a function should be located }
6668 */
6669 int ind_first_open = 0;
6670
6671 /*
6672 * spaces from the prevailing indent a leftmost open brace should be
6673 * located
6674 */
6675 int ind_open_extra = 0;
6676
6677 /*
6678 * spaces from the matching open brace (real location for one at the left
6679 * edge; imaginary location from one that ends a line) the matching close
6680 * brace should be located
6681 */
6682 int ind_close_extra = 0;
6683
6684 /*
6685 * spaces from the edge of the line an open brace sitting in the leftmost
6686 * column is imagined to be
6687 */
6688 int ind_open_left_imag = 0;
6689
6690 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006691 * Spaces jump labels should be shifted to the left if N is non-negative,
6692 * otherwise the jump label will be put to column 1.
6693 */
6694 int ind_jump_label = -1;
6695
6696 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 * spaces from the switch() indent a "case xx" label should be located
6698 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006699 int ind_case = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701 /*
6702 * spaces from the "case xx:" code after a switch() should be located
6703 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006704 int ind_case_code = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706 /*
6707 * lineup break at end of case in switch() with case label
6708 */
6709 int ind_case_break = 0;
6710
6711 /*
6712 * spaces from the class declaration indent a scope declaration label
6713 * should be located
6714 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006715 int ind_scopedecl = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
6717 /*
6718 * spaces from the scope declaration label code should be located
6719 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006720 int ind_scopedecl_code = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722 /*
6723 * amount K&R-style parameters should be indented
6724 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006725 int ind_param = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727 /*
6728 * amount a function type spec should be indented
6729 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006730 int ind_func_type = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731
6732 /*
6733 * amount a cpp base class declaration or constructor initialization
6734 * should be indented
6735 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006736 int ind_cpp_baseclass = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
6738 /*
6739 * additional spaces beyond the prevailing indent a continuation line
6740 * should be located
6741 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006742 int ind_continuation = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744 /*
6745 * spaces from the indent of the line with an unclosed parentheses
6746 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006747 int ind_unclosed = sw * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749 /*
6750 * spaces from the indent of the line with an unclosed parentheses, which
6751 * itself is also unclosed
6752 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006753 int ind_unclosed2 = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
6755 /*
6756 * suppress ignoring spaces from the indent of a line starting with an
6757 * unclosed parentheses.
6758 */
6759 int ind_unclosed_noignore = 0;
6760
6761 /*
6762 * If the opening paren is the last nonwhite character on the line, and
6763 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6764 * context (for very long lines).
6765 */
6766 int ind_unclosed_wrapped = 0;
6767
6768 /*
6769 * suppress ignoring white space when lining up with the character after
6770 * an unclosed parentheses.
6771 */
6772 int ind_unclosed_whiteok = 0;
6773
6774 /*
6775 * indent a closing parentheses under the line start of the matching
6776 * opening parentheses.
6777 */
6778 int ind_matching_paren = 0;
6779
6780 /*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006781 * indent a closing parentheses under the previous line.
6782 */
6783 int ind_paren_prev = 0;
6784
6785 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 * Extra indent for comments.
6787 */
6788 int ind_comment = 0;
6789
6790 /*
6791 * spaces from the comment opener when there is nothing after it.
6792 */
6793 int ind_in_comment = 3;
6794
6795 /*
6796 * boolean: if non-zero, use ind_in_comment even if there is something
6797 * after the comment opener.
6798 */
6799 int ind_in_comment2 = 0;
6800
6801 /*
6802 * max lines to search for an open paren
6803 */
6804 int ind_maxparen = 20;
6805
6806 /*
6807 * max lines to search for an open comment
6808 */
6809 int ind_maxcomment = 70;
6810
6811 /*
6812 * handle braces for java code
6813 */
6814 int ind_java = 0;
6815
6816 /*
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006817 * not to confuse JS object properties with labels
6818 */
6819 int ind_js = 0;
6820
6821 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 * handle blocked cases correctly
6823 */
6824 int ind_keep_case_label = 0;
6825
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006826 /*
6827 * handle C++ namespace
6828 */
6829 int ind_cpp_namespace = 0;
6830
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006831 /*
6832 * handle continuation lines containing conditions of if(), for() and
6833 * while()
6834 */
6835 int ind_if_for_while = 0;
6836
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 pos_T cur_curpos;
6838 int amount;
6839 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006840 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 colnr_T col;
6842 char_u *theline;
6843 char_u *linecopy;
6844 pos_T *trypos;
6845 pos_T *tryposBrace = NULL;
6846 pos_T our_paren_pos;
6847 char_u *start;
6848 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006849#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850#define BRACE_AT_START 2 /* '{' is at start of line */
6851#define BRACE_AT_END 3 /* '{' is at end of line */
6852 linenr_T ourscope;
6853 char_u *l;
6854 char_u *look;
6855 char_u terminated;
6856 int lookfor;
6857#define LOOKFOR_INITIAL 0
6858#define LOOKFOR_IF 1
6859#define LOOKFOR_DO 2
6860#define LOOKFOR_CASE 3
6861#define LOOKFOR_ANY 4
6862#define LOOKFOR_TERM 5
6863#define LOOKFOR_UNTERM 6
6864#define LOOKFOR_SCOPEDECL 7
6865#define LOOKFOR_NOBREAK 8
6866#define LOOKFOR_CPP_BASECLASS 9
6867#define LOOKFOR_ENUM_OR_INIT 10
6868
6869 int whilelevel;
6870 linenr_T lnum;
6871 char_u *options;
Bram Moolenaar48d27922012-06-13 13:40:48 +02006872 char_u *digits;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 int fraction = 0; /* init for GCC */
6874 int divider;
6875 int n;
6876 int iscase;
6877 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006878 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006880 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006881 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
6883 for (options = curbuf->b_p_cino; *options; )
6884 {
6885 l = options++;
6886 if (*options == '-')
6887 ++options;
Bram Moolenaar48d27922012-06-13 13:40:48 +02006888 digits = options; /* remember where the digits start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 n = getdigits(&options);
6890 divider = 0;
6891 if (*options == '.') /* ".5s" means a fraction */
6892 {
6893 fraction = atol((char *)++options);
6894 while (VIM_ISDIGIT(*options))
6895 {
6896 ++options;
6897 if (divider)
6898 divider *= 10;
6899 else
6900 divider = 10;
6901 }
6902 }
6903 if (*options == 's') /* "2s" means two times 'shiftwidth' */
6904 {
Bram Moolenaar48d27922012-06-13 13:40:48 +02006905 if (options == digits)
Bram Moolenaar14f24742012-08-08 18:01:05 +02006906 n = sw; /* just "s" is one 'shiftwidth' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 else
6908 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02006909 n *= sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 if (divider)
Bram Moolenaar14f24742012-08-08 18:01:05 +02006911 n += (sw * fraction + divider / 2) / divider;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
6913 ++options;
6914 }
6915 if (l[1] == '-')
6916 n = -n;
6917 /* When adding an entry here, also update the default 'cinoptions' in
Bram Moolenaar39353fd2007-03-27 09:02:11 +00006918 * doc/indent.txt, and add explanation for it! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 switch (*l)
6920 {
6921 case '>': ind_level = n; break;
6922 case 'e': ind_open_imag = n; break;
6923 case 'n': ind_no_brace = n; break;
6924 case 'f': ind_first_open = n; break;
6925 case '{': ind_open_extra = n; break;
6926 case '}': ind_close_extra = n; break;
6927 case '^': ind_open_left_imag = n; break;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006928 case 'L': ind_jump_label = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 case ':': ind_case = n; break;
6930 case '=': ind_case_code = n; break;
6931 case 'b': ind_case_break = n; break;
6932 case 'p': ind_param = n; break;
6933 case 't': ind_func_type = n; break;
6934 case '/': ind_comment = n; break;
6935 case 'c': ind_in_comment = n; break;
6936 case 'C': ind_in_comment2 = n; break;
6937 case 'i': ind_cpp_baseclass = n; break;
6938 case '+': ind_continuation = n; break;
6939 case '(': ind_unclosed = n; break;
6940 case 'u': ind_unclosed2 = n; break;
6941 case 'U': ind_unclosed_noignore = n; break;
6942 case 'W': ind_unclosed_wrapped = n; break;
6943 case 'w': ind_unclosed_whiteok = n; break;
6944 case 'm': ind_matching_paren = n; break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006945 case 'M': ind_paren_prev = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 case ')': ind_maxparen = n; break;
6947 case '*': ind_maxcomment = n; break;
6948 case 'g': ind_scopedecl = n; break;
6949 case 'h': ind_scopedecl_code = n; break;
6950 case 'j': ind_java = n; break;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006951 case 'J': ind_js = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 case 'l': ind_keep_case_label = n; break;
Bram Moolenaar39353fd2007-03-27 09:02:11 +00006953 case '#': ind_hash_comment = n; break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006954 case 'N': ind_cpp_namespace = n; break;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006955 case 'k': ind_if_for_while = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 }
Bram Moolenaardfdf3c42010-03-23 18:22:46 +01006957 if (*options == ',')
6958 ++options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960
6961 /* remember where the cursor was when we started */
6962 cur_curpos = curwin->w_cursor;
6963
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006964 /* if we are at line 1 0 is fine, right? */
6965 if (cur_curpos.lnum == 1)
6966 return 0;
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 /* Get a copy of the current contents of the line.
6969 * This is required, because only the most recent line obtained with
6970 * ml_get is valid! */
6971 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6972 if (linecopy == NULL)
6973 return 0;
6974
6975 /*
6976 * In insert mode and the cursor is on a ')' truncate the line at the
6977 * cursor position. We don't want to line up with the matching '(' when
6978 * inserting new stuff.
6979 * For unknown reasons the cursor might be past the end of the line, thus
6980 * check for that.
6981 */
6982 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006983 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 && linecopy[curwin->w_cursor.col] == ')')
6985 linecopy[curwin->w_cursor.col] = NUL;
6986
6987 theline = skipwhite(linecopy);
6988
6989 /* move the cursor to the start of the line */
6990
6991 curwin->w_cursor.col = 0;
6992
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006993 original_line_islabel = cin_islabel(ind_maxcomment); /* XXX */
6994
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 /*
6996 * #defines and so on always go at the left when included in 'cinkeys'.
6997 */
6998 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
6999 {
7000 amount = 0;
7001 }
7002
7003 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007004 * Is it a non-case label? Then that goes at the left margin too unless:
7005 * - JS flag is set.
7006 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007008 else if (original_line_islabel && !ind_js && ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {
7010 amount = 0;
7011 }
7012
7013 /*
7014 * If we're inside a "//" comment and there is a "//" comment in a
7015 * previous line, lineup with that one.
7016 */
7017 else if (cin_islinecomment(theline)
7018 && (trypos = find_line_comment()) != NULL) /* XXX */
7019 {
7020 /* find how indented the line beginning the comment is */
7021 getvcol(curwin, trypos, &col, NULL, NULL);
7022 amount = col;
7023 }
7024
7025 /*
7026 * If we're inside a comment and not looking at the start of the
7027 * comment, try using the 'comments' option.
7028 */
7029 else if (!cin_iscomment(theline)
7030 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
7031 {
7032 int lead_start_len = 2;
7033 int lead_middle_len = 1;
7034 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7035 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7036 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7037 char_u *p;
7038 int start_align = 0;
7039 int start_off = 0;
7040 int done = FALSE;
7041
7042 /* find how indented the line beginning the comment is */
7043 getvcol(curwin, trypos, &col, NULL, NULL);
7044 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007045 *lead_start = NUL;
7046 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
7048 p = curbuf->b_p_com;
7049 while (*p != NUL)
7050 {
7051 int align = 0;
7052 int off = 0;
7053 int what = 0;
7054
7055 while (*p != NUL && *p != ':')
7056 {
7057 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7058 what = *p++;
7059 else if (*p == COM_LEFT || *p == COM_RIGHT)
7060 align = *p++;
7061 else if (VIM_ISDIGIT(*p) || *p == '-')
7062 off = getdigits(&p);
7063 else
7064 ++p;
7065 }
7066
7067 if (*p == ':')
7068 ++p;
7069 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7070 if (what == COM_START)
7071 {
7072 STRCPY(lead_start, lead_end);
7073 lead_start_len = (int)STRLEN(lead_start);
7074 start_off = off;
7075 start_align = align;
7076 }
7077 else if (what == COM_MIDDLE)
7078 {
7079 STRCPY(lead_middle, lead_end);
7080 lead_middle_len = (int)STRLEN(lead_middle);
7081 }
7082 else if (what == COM_END)
7083 {
7084 /* If our line starts with the middle comment string, line it
7085 * up with the comment opener per the 'comments' option. */
7086 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7087 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7088 {
7089 done = TRUE;
7090 if (curwin->w_cursor.lnum > 1)
7091 {
7092 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007093 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 * the middle comment string matches in the previous
7095 * line, use the indent of that line. XXX */
7096 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7097 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7098 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7099 else if (STRNCMP(look, lead_middle,
7100 lead_middle_len) == 0)
7101 {
7102 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7103 break;
7104 }
7105 /* If the start comment string doesn't match with the
7106 * start of the comment, skip this entry. XXX */
7107 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7108 lead_start, lead_start_len) != 0)
7109 continue;
7110 }
7111 if (start_off != 0)
7112 amount += start_off;
7113 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007114 amount += vim_strsize(lead_start)
7115 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 break;
7117 }
7118
7119 /* If our line starts with the end comment string, line it up
7120 * with the middle comment */
7121 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7122 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7123 {
7124 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7125 /* XXX */
7126 if (off != 0)
7127 amount += off;
7128 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007129 amount += vim_strsize(lead_start)
7130 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 done = TRUE;
7132 break;
7133 }
7134 }
7135 }
7136
7137 /* If our line starts with an asterisk, line up with the
7138 * asterisk in the comment opener; otherwise, line up
7139 * with the first character of the comment text.
7140 */
7141 if (done)
7142 ;
7143 else if (theline[0] == '*')
7144 amount += 1;
7145 else
7146 {
7147 /*
7148 * If we are more than one line away from the comment opener, take
7149 * the indent of the previous non-empty line. If 'cino' has "CO"
7150 * and we are just below the comment opener and there are any
7151 * white characters after it line up with the text after it;
7152 * otherwise, add the amount specified by "c" in 'cino'
7153 */
7154 amount = -1;
7155 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7156 {
7157 if (linewhite(lnum)) /* skip blank lines */
7158 continue;
7159 amount = get_indent_lnum(lnum); /* XXX */
7160 break;
7161 }
7162 if (amount == -1) /* use the comment opener */
7163 {
7164 if (!ind_in_comment2)
7165 {
7166 start = ml_get(trypos->lnum);
7167 look = start + trypos->col + 2; /* skip / and * */
7168 if (*look != NUL) /* if something after it */
7169 trypos->col = (colnr_T)(skipwhite(look) - start);
7170 }
7171 getvcol(curwin, trypos, &col, NULL, NULL);
7172 amount = col;
7173 if (ind_in_comment2 || *look == NUL)
7174 amount += ind_in_comment;
7175 }
7176 }
7177 }
7178
7179 /*
7180 * Are we inside parentheses or braces?
7181 */ /* XXX */
7182 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL
7183 && ind_java == 0)
7184 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL
7185 || trypos != NULL)
7186 {
7187 if (trypos != NULL && tryposBrace != NULL)
7188 {
7189 /* Both an unmatched '(' and '{' is found. Use the one which is
7190 * closer to the current cursor position, set the other to NULL. */
7191 if (trypos->lnum != tryposBrace->lnum
7192 ? trypos->lnum < tryposBrace->lnum
7193 : trypos->col < tryposBrace->col)
7194 trypos = NULL;
7195 else
7196 tryposBrace = NULL;
7197 }
7198
7199 if (trypos != NULL)
7200 {
7201 /*
7202 * If the matching paren is more than one line away, use the indent of
7203 * a previous non-empty line that matches the same paren.
7204 */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007205 if (theline[0] == ')' && ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007207 /* Line up with the start of the matching paren line. */
7208 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7209 }
7210 else
7211 {
7212 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007213 our_paren_pos = *trypos;
7214 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007216 l = skipwhite(ml_get(lnum));
7217 if (cin_nocode(l)) /* skip comment lines */
7218 continue;
7219 if (cin_ispreproc_cont(&l, &lnum))
7220 continue; /* ignore #define, #if, etc. */
7221 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007223 /* Skip a comment. XXX */
7224 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7225 {
7226 lnum = trypos->lnum + 1;
7227 continue;
7228 }
7229
7230 /* XXX */
7231 if ((trypos = find_match_paren(
7232 corr_ind_maxparen(ind_maxparen, &cur_curpos),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 ind_maxcomment)) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007234 && trypos->lnum == our_paren_pos.lnum
7235 && trypos->col == our_paren_pos.col)
7236 {
7237 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007239 if (theline[0] == ')')
7240 {
7241 if (our_paren_pos.lnum != lnum
7242 && cur_amount > amount)
7243 cur_amount = amount;
7244 amount = -1;
7245 }
7246 break;
7247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
7249 }
7250
7251 /*
7252 * Line up with line where the matching paren is. XXX
7253 * If the line starts with a '(' or the indent for unclosed
7254 * parentheses is zero, line up with the unclosed parentheses.
7255 */
7256 if (amount == -1)
7257 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007258 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007259 int is_if_for_while = 0;
7260
7261 if (ind_if_for_while)
7262 {
7263 /* Look for the outermost opening parenthesis on this line
7264 * and check whether it belongs to an "if", "for" or "while". */
7265
7266 pos_T cursor_save = curwin->w_cursor;
7267 pos_T outermost;
7268 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007269
7270 trypos = &our_paren_pos;
7271 do {
7272 outermost = *trypos;
7273 curwin->w_cursor.lnum = outermost.lnum;
7274 curwin->w_cursor.col = outermost.col;
7275
7276 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
7277 } while (trypos && trypos->lnum == outermost.lnum);
7278
7279 curwin->w_cursor = cursor_save;
7280
7281 line = ml_get(outermost.lnum);
7282
7283 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007284 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007285 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007286
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007288 look = skipwhite(look);
7289 if (*look == '(')
7290 {
7291 linenr_T save_lnum = curwin->w_cursor.lnum;
7292 char_u *line;
7293 int look_col;
7294
7295 /* Ignore a '(' in front of the line that has a match before
7296 * our matching '('. */
7297 curwin->w_cursor.lnum = our_paren_pos.lnum;
7298 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007299 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007300 curwin->w_cursor.col = look_col + 1;
7301 if ((trypos = findmatchlimit(NULL, ')', 0, ind_maxparen))
7302 != NULL
7303 && trypos->lnum == our_paren_pos.lnum
7304 && trypos->col < our_paren_pos.col)
7305 ignore_paren_col = trypos->col + 1;
7306
7307 curwin->w_cursor.lnum = save_lnum;
7308 look = ml_get(our_paren_pos.lnum) + look_col;
7309 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007310 if (theline[0] == ')' || (ind_unclosed == 0 && is_if_for_while == 0)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007311 || (!ind_unclosed_noignore && *look == '('
7312 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 {
7314 /*
7315 * If we're looking at a close paren, line up right there;
7316 * otherwise, line up with the next (non-white) character.
7317 * When ind_unclosed_wrapped is set and the matching paren is
7318 * the last nonwhite character of the line, use either the
7319 * indent of the current line or the indentation of the next
7320 * outer paren and add ind_unclosed_wrapped (for very long
7321 * lines).
7322 */
7323 if (theline[0] != ')')
7324 {
7325 cur_amount = MAXCOL;
7326 l = ml_get(our_paren_pos.lnum);
7327 if (ind_unclosed_wrapped
7328 && cin_ends_in(l, (char_u *)"(", NULL))
7329 {
7330 /* look for opening unmatched paren, indent one level
7331 * for each additional level */
7332 n = 1;
7333 for (col = 0; col < our_paren_pos.col; ++col)
7334 {
7335 switch (l[col])
7336 {
7337 case '(':
7338 case '{': ++n;
7339 break;
7340
7341 case ')':
7342 case '}': if (n > 1)
7343 --n;
7344 break;
7345 }
7346 }
7347
7348 our_paren_pos.col = 0;
7349 amount += n * ind_unclosed_wrapped;
7350 }
7351 else if (ind_unclosed_whiteok)
7352 our_paren_pos.col++;
7353 else
7354 {
7355 col = our_paren_pos.col + 1;
7356 while (vim_iswhite(l[col]))
7357 col++;
7358 if (l[col] != NUL) /* In case of trailing space */
7359 our_paren_pos.col = col;
7360 else
7361 our_paren_pos.col++;
7362 }
7363 }
7364
7365 /*
7366 * Find how indented the paren is, or the character after it
7367 * if we did the above "if".
7368 */
7369 if (our_paren_pos.col > 0)
7370 {
7371 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7372 if (cur_amount > (int)col)
7373 cur_amount = col;
7374 }
7375 }
7376
7377 if (theline[0] == ')' && ind_matching_paren)
7378 {
7379 /* Line up with the start of the matching paren line. */
7380 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007381 else if ((ind_unclosed == 0 && is_if_for_while == 0)
7382 || (!ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007383 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
7385 if (cur_amount != MAXCOL)
7386 amount = cur_amount;
7387 }
7388 else
7389 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007390 /* Add ind_unclosed2 for each '(' before our matching one, but
7391 * ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007393 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
7395 --our_paren_pos.col;
7396 switch (*ml_get_pos(&our_paren_pos))
7397 {
7398 case '(': amount += ind_unclosed2;
7399 col = our_paren_pos.col;
7400 break;
7401 case ')': amount -= ind_unclosed2;
7402 col = MAXCOL;
7403 break;
7404 }
7405 }
7406
7407 /* Use ind_unclosed once, when the first '(' is not inside
7408 * braces */
7409 if (col == MAXCOL)
7410 amount += ind_unclosed;
7411 else
7412 {
7413 curwin->w_cursor.lnum = our_paren_pos.lnum;
7414 curwin->w_cursor.col = col;
Bram Moolenaar367bec82011-04-11 14:26:19 +02007415 if (find_match_paren(ind_maxparen, ind_maxcomment) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 amount += ind_unclosed2;
7417 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007418 {
7419 if (is_if_for_while)
7420 amount += ind_if_for_while;
7421 else
7422 amount += ind_unclosed;
7423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425 /*
7426 * For a line starting with ')' use the minimum of the two
7427 * positions, to avoid giving it more indent than the previous
7428 * lines:
7429 * func_long_name( if (x
7430 * arg && yy
7431 * ) ^ not here ) ^ not here
7432 */
7433 if (cur_amount < amount)
7434 amount = cur_amount;
7435 }
7436 }
7437
7438 /* add extra indent for a comment */
7439 if (cin_iscomment(theline))
7440 amount += ind_comment;
7441 }
7442
7443 /*
7444 * Are we at least inside braces, then?
7445 */
7446 else
7447 {
7448 trypos = tryposBrace;
7449
7450 ourscope = trypos->lnum;
7451 start = ml_get(ourscope);
7452
7453 /*
7454 * Now figure out how indented the line is in general.
7455 * If the brace was at the start of the line, we use that;
7456 * otherwise, check out the indentation of the line as
7457 * a whole and then add the "imaginary indent" to that.
7458 */
7459 look = skipwhite(start);
7460 if (*look == '{')
7461 {
7462 getvcol(curwin, trypos, &col, NULL, NULL);
7463 amount = col;
7464 if (*start == '{')
7465 start_brace = BRACE_IN_COL0;
7466 else
7467 start_brace = BRACE_AT_START;
7468 }
7469 else
7470 {
7471 /*
7472 * that opening brace might have been on a continuation
7473 * line. if so, find the start of the line.
7474 */
7475 curwin->w_cursor.lnum = ourscope;
7476
7477 /*
7478 * position the cursor over the rightmost paren, so that
7479 * matching it will take us back to the start of the line.
7480 */
7481 lnum = ourscope;
7482 if (find_last_paren(start, '(', ')')
7483 && (trypos = find_match_paren(ind_maxparen,
7484 ind_maxcomment)) != NULL)
7485 lnum = trypos->lnum;
7486
7487 /*
7488 * It could have been something like
7489 * case 1: if (asdf &&
7490 * ldfd) {
7491 * }
7492 */
Bram Moolenaar6ec154b2011-06-12 21:51:08 +02007493 if (ind_js || (ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007494 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 amount = get_indent();
7496 else
7497 amount = skip_label(lnum, &l, ind_maxcomment);
7498
7499 start_brace = BRACE_AT_END;
7500 }
7501
7502 /*
7503 * if we're looking at a closing brace, that's where
7504 * we want to be. otherwise, add the amount of room
7505 * that an indent is supposed to be.
7506 */
7507 if (theline[0] == '}')
7508 {
7509 /*
7510 * they may want closing braces to line up with something
7511 * other than the open brace. indulge them, if so.
7512 */
7513 amount += ind_close_extra;
7514 }
7515 else
7516 {
7517 /*
7518 * If we're looking at an "else", try to find an "if"
7519 * to match it with.
7520 * If we're looking at a "while", try to find a "do"
7521 * to match it with.
7522 */
7523 lookfor = LOOKFOR_INITIAL;
7524 if (cin_iselse(theline))
7525 lookfor = LOOKFOR_IF;
7526 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen))
7527 /* XXX */
7528 lookfor = LOOKFOR_DO;
7529 if (lookfor != LOOKFOR_INITIAL)
7530 {
7531 curwin->w_cursor.lnum = cur_curpos.lnum;
7532 if (find_match(lookfor, ourscope, ind_maxparen,
7533 ind_maxcomment) == OK)
7534 {
7535 amount = get_indent(); /* XXX */
7536 goto theend;
7537 }
7538 }
7539
7540 /*
7541 * We get here if we are not on an "while-of-do" or "else" (or
7542 * failed to find a matching "if").
7543 * Search backwards for something to line up with.
7544 * First set amount for when we don't find anything.
7545 */
7546
7547 /*
7548 * if the '{' is _really_ at the left margin, use the imaginary
7549 * location of a left-margin brace. Otherwise, correct the
7550 * location for ind_open_extra.
7551 */
7552
7553 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7554 {
7555 amount = ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007556 lookfor_cpp_namespace = TRUE;
7557 }
7558 else if (start_brace == BRACE_AT_START &&
7559 lookfor_cpp_namespace) /* '{' is at start */
7560 {
7561
7562 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 }
7564 else
7565 {
7566 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 amount += ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007569
7570 l = skipwhite(ml_get_curline());
7571 if (cin_is_cpp_namespace(l))
7572 amount += ind_cpp_namespace;
7573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 else
7575 {
7576 /* Compensate for adding ind_open_extra later. */
7577 amount -= ind_open_extra;
7578 if (amount < 0)
7579 amount = 0;
7580 }
7581 }
7582
7583 lookfor_break = FALSE;
7584
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007585 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {
7587 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
7588 amount += ind_case;
7589 }
7590 else if (cin_isscopedecl(theline)) /* private:, ... */
7591 {
7592 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
7593 amount += ind_scopedecl;
7594 }
7595 else
7596 {
7597 if (ind_case_break && cin_isbreak(theline)) /* break; ... */
7598 lookfor_break = TRUE;
7599
7600 lookfor = LOOKFOR_INITIAL;
7601 amount += ind_level; /* ind_level from start of block */
7602 }
7603 scope_amount = amount;
7604 whilelevel = 0;
7605
7606 /*
7607 * Search backwards. If we find something we recognize, line up
7608 * with that.
7609 *
7610 * if we're looking at an open brace, indent
7611 * the usual amount relative to the conditional
7612 * that opens the block.
7613 */
7614 curwin->w_cursor = cur_curpos;
7615 for (;;)
7616 {
7617 curwin->w_cursor.lnum--;
7618 curwin->w_cursor.col = 0;
7619
7620 /*
7621 * If we went all the way back to the start of our scope, line
7622 * up with it.
7623 */
7624 if (curwin->w_cursor.lnum <= ourscope)
7625 {
7626 /* we reached end of scope:
7627 * if looking for a enum or structure initialization
7628 * go further back:
7629 * if it is an initializer (enum xxx or xxx =), then
7630 * don't add ind_continuation, otherwise it is a variable
7631 * declaration:
7632 * int x,
7633 * here; <-- add ind_continuation
7634 */
7635 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7636 {
7637 if (curwin->w_cursor.lnum == 0
7638 || curwin->w_cursor.lnum
7639 < ourscope - ind_maxparen)
7640 {
7641 /* nothing found (abuse ind_maxparen as limit)
7642 * assume terminated line (i.e. a variable
7643 * initialization) */
7644 if (cont_amount > 0)
7645 amount = cont_amount;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007646 else if (!ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 amount += ind_continuation;
7648 break;
7649 }
7650
7651 l = ml_get_curline();
7652
7653 /*
7654 * If we're in a comment now, skip to the start of the
7655 * comment.
7656 */
7657 trypos = find_start_comment(ind_maxcomment);
7658 if (trypos != NULL)
7659 {
7660 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007661 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 continue;
7663 }
7664
7665 /*
7666 * Skip preprocessor directives and blank lines.
7667 */
7668 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7669 continue;
7670
7671 if (cin_nocode(l))
7672 continue;
7673
7674 terminated = cin_isterminated(l, FALSE, TRUE);
7675
7676 /*
7677 * If we are at top level and the line looks like a
7678 * function declaration, we are done
7679 * (it's a variable declaration).
7680 */
7681 if (start_brace != BRACE_IN_COL0
Bram Moolenaarc367faa2011-12-14 20:21:35 +01007682 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum,
7683 0, ind_maxparen, ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {
7685 /* if the line is terminated with another ','
7686 * it is a continued variable initialization.
7687 * don't add extra indent.
7688 * TODO: does not work, if a function
7689 * declaration is split over multiple lines:
7690 * cin_isfuncdecl returns FALSE then.
7691 */
7692 if (terminated == ',')
7693 break;
7694
7695 /* if it es a enum declaration or an assignment,
7696 * we are done.
7697 */
7698 if (terminated != ';' && cin_isinit())
7699 break;
7700
7701 /* nothing useful found */
7702 if (terminated == 0 || terminated == '{')
7703 continue;
7704 }
7705
7706 if (terminated != ';')
7707 {
7708 /* Skip parens and braces. Position the cursor
7709 * over the rightmost paren, so that matching it
7710 * will take us back to the start of the line.
7711 */ /* XXX */
7712 trypos = NULL;
7713 if (find_last_paren(l, '(', ')'))
7714 trypos = find_match_paren(ind_maxparen,
7715 ind_maxcomment);
7716
7717 if (trypos == NULL && find_last_paren(l, '{', '}'))
7718 trypos = find_start_brace(ind_maxcomment);
7719
7720 if (trypos != NULL)
7721 {
7722 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007723 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 continue;
7725 }
7726 }
7727
7728 /* it's a variable declaration, add indentation
7729 * like in
7730 * int a,
7731 * b;
7732 */
7733 if (cont_amount > 0)
7734 amount = cont_amount;
7735 else
7736 amount += ind_continuation;
7737 }
7738 else if (lookfor == LOOKFOR_UNTERM)
7739 {
7740 if (cont_amount > 0)
7741 amount = cont_amount;
7742 else
7743 amount += ind_continuation;
7744 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007745 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007746 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007747 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007749 {
7750 amount = scope_amount;
7751 if (theline[0] == '{')
7752 {
7753 amount += ind_open_extra;
7754 added_to_amount = ind_open_extra;
7755 }
7756 }
7757
7758 if (lookfor_cpp_namespace)
7759 {
7760 /*
7761 * Looking for C++ namespace, need to look further
7762 * back.
7763 */
7764 if (curwin->w_cursor.lnum == ourscope)
7765 continue;
7766
7767 if (curwin->w_cursor.lnum == 0
7768 || curwin->w_cursor.lnum
7769 < ourscope - FIND_NAMESPACE_LIM)
7770 break;
7771
7772 l = ml_get_curline();
7773
7774 /* If we're in a comment now, skip to the start of
7775 * the comment. */
7776 trypos = find_start_comment(ind_maxcomment);
7777 if (trypos != NULL)
7778 {
7779 curwin->w_cursor.lnum = trypos->lnum + 1;
7780 curwin->w_cursor.col = 0;
7781 continue;
7782 }
7783
7784 /* Skip preprocessor directives and blank lines. */
7785 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7786 continue;
7787
7788 /* Finally the actual check for "namespace". */
7789 if (cin_is_cpp_namespace(l))
7790 {
7791 amount += ind_cpp_namespace - added_to_amount;
7792 break;
7793 }
7794
7795 if (cin_nocode(l))
7796 continue;
7797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
7799 break;
7800 }
7801
7802 /*
7803 * If we're in a comment now, skip to the start of the comment.
7804 */ /* XXX */
7805 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7806 {
7807 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007808 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 continue;
7810 }
7811
7812 l = ml_get_curline();
7813
7814 /*
7815 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007816 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007818 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (iscase || cin_isscopedecl(l))
7820 {
7821 /* we are only looking for cpp base class
7822 * declaration/initialization any longer */
7823 if (lookfor == LOOKFOR_CPP_BASECLASS)
7824 break;
7825
7826 /* When looking for a "do" we are not interested in
7827 * labels. */
7828 if (whilelevel > 0)
7829 continue;
7830
7831 /*
7832 * case xx:
7833 * c = 99 + <- this indent plus continuation
7834 *-> here;
7835 */
7836 if (lookfor == LOOKFOR_UNTERM
7837 || lookfor == LOOKFOR_ENUM_OR_INIT)
7838 {
7839 if (cont_amount > 0)
7840 amount = cont_amount;
7841 else
7842 amount += ind_continuation;
7843 break;
7844 }
7845
7846 /*
7847 * case xx: <- line up with this case
7848 * x = 333;
7849 * case yy:
7850 */
7851 if ( (iscase && lookfor == LOOKFOR_CASE)
7852 || (iscase && lookfor_break)
7853 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7854 {
7855 /*
7856 * Check that this case label is not for another
7857 * switch()
7858 */ /* XXX */
7859 if ((trypos = find_start_brace(ind_maxcomment)) ==
7860 NULL || trypos->lnum == ourscope)
7861 {
7862 amount = get_indent(); /* XXX */
7863 break;
7864 }
7865 continue;
7866 }
7867
7868 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7869
7870 /*
7871 * case xx: if (cond) <- line up with this if
7872 * y = y + 1;
7873 * -> s = 99;
7874 *
7875 * case xx:
7876 * if (cond) <- line up with this line
7877 * y = y + 1;
7878 * -> s = 99;
7879 */
7880 if (lookfor == LOOKFOR_TERM)
7881 {
7882 if (n)
7883 amount = n;
7884
7885 if (!lookfor_break)
7886 break;
7887 }
7888
7889 /*
7890 * case xx: x = x + 1; <- line up with this x
7891 * -> y = y + 1;
7892 *
7893 * case xx: if (cond) <- line up with this if
7894 * -> y = y + 1;
7895 */
7896 if (n)
7897 {
7898 amount = n;
7899 l = after_label(ml_get_curline());
7900 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007901 {
7902 if (theline[0] == '{')
7903 amount += ind_open_extra;
7904 else
7905 amount += ind_level + ind_no_brace;
7906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 break;
7908 }
7909
7910 /*
7911 * Try to get the indent of a statement before the switch
7912 * label. If nothing is found, line up relative to the
7913 * switch label.
7914 * break; <- may line up with this line
7915 * case xx:
7916 * -> y = 1;
7917 */
7918 scope_amount = get_indent() + (iscase /* XXX */
7919 ? ind_case_code : ind_scopedecl_code);
7920 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
7921 continue;
7922 }
7923
7924 /*
7925 * Looking for a switch() label or C++ scope declaration,
7926 * ignore other lines, skip {}-blocks.
7927 */
7928 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7929 {
7930 if (find_last_paren(l, '{', '}') && (trypos =
7931 find_start_brace(ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007934 curwin->w_cursor.col = 0;
7935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 continue;
7937 }
7938
7939 /*
7940 * Ignore jump labels with nothing after them.
7941 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007942 if (!ind_js && cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {
7944 l = after_label(ml_get_curline());
7945 if (l == NULL || cin_nocode(l))
7946 continue;
7947 }
7948
7949 /*
7950 * Ignore #defines, #if, etc.
7951 * Ignore comment and empty lines.
7952 * (need to get the line again, cin_islabel() may have
7953 * unlocked it)
7954 */
7955 l = ml_get_curline();
7956 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7957 || cin_nocode(l))
7958 continue;
7959
7960 /*
7961 * Are we at the start of a cpp base class declaration or
7962 * constructor initialization?
7963 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007964 n = FALSE;
7965 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0)
7966 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007967 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007968 l = ml_get_curline();
7969 }
7970 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {
7972 if (lookfor == LOOKFOR_UNTERM)
7973 {
7974 if (cont_amount > 0)
7975 amount = cont_amount;
7976 else
7977 amount += ind_continuation;
7978 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007979 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007981 /* Need to find start of the declaration. */
7982 lookfor = LOOKFOR_UNTERM;
7983 ind_continuation = 0;
7984 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 }
7986 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007987 /* XXX */
7988 amount = get_baseclass_amount(col, ind_maxparen,
7989 ind_maxcomment, ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 break;
7991 }
7992 else if (lookfor == LOOKFOR_CPP_BASECLASS)
7993 {
7994 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00007995 * declaration or initialization before the opening brace.
7996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (cin_isterminated(l, TRUE, FALSE))
7998 break;
7999 else
8000 continue;
8001 }
8002
8003 /*
8004 * What happens next depends on the line being terminated.
8005 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008006 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 * 123,
8008 * sizeof
8009 * here
8010 * Otherwise check whether it is a enumeration or structure
8011 * initialisation (not indented) or a variable declaration
8012 * (indented).
8013 */
8014 terminated = cin_isterminated(l, FALSE, TRUE);
8015
8016 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8017 && terminated == ','))
8018 {
8019 /*
8020 * if we're in the middle of a paren thing,
8021 * go back to the line that starts it so
8022 * we can get the right prevailing indent
8023 * if ( foo &&
8024 * bar )
8025 */
8026 /*
8027 * position the cursor over the rightmost paren, so that
8028 * matching it will take us back to the start of the line.
8029 */
8030 (void)find_last_paren(l, '(', ')');
8031 trypos = find_match_paren(
8032 corr_ind_maxparen(ind_maxparen, &cur_curpos),
8033 ind_maxcomment);
8034
8035 /*
8036 * If we are looking for ',', we also look for matching
8037 * braces.
8038 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008039 if (trypos == NULL && terminated == ','
8040 && find_last_paren(l, '{', '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 trypos = find_start_brace(ind_maxcomment);
8042
8043 if (trypos != NULL)
8044 {
8045 /*
8046 * Check if we are on a case label now. This is
8047 * handled above.
8048 * case xx: if ( asdf &&
8049 * asdf)
8050 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008051 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008053 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {
8055 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008056 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 continue;
8058 }
8059 }
8060
8061 /*
8062 * Skip over continuation lines to find the one to get the
8063 * indent from
8064 * char *usethis = "bla\
8065 * bla",
8066 * here;
8067 */
8068 if (terminated == ',')
8069 {
8070 while (curwin->w_cursor.lnum > 1)
8071 {
8072 l = ml_get(curwin->w_cursor.lnum - 1);
8073 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8074 break;
8075 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008076 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
8078 }
8079
8080 /*
8081 * Get indent and pointer to text for current line,
8082 * ignoring any jump label. XXX
8083 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008084 if (!ind_js)
8085 cur_amount = skip_label(curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 &l, ind_maxcomment);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008087 else
8088 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 /*
8090 * If this is just above the line we are indenting, and it
8091 * starts with a '{', line it up with this line.
8092 * while (not)
8093 * -> {
8094 * }
8095 */
8096 if (terminated != ',' && lookfor != LOOKFOR_TERM
8097 && theline[0] == '{')
8098 {
8099 amount = cur_amount;
8100 /*
8101 * Only add ind_open_extra when the current line
8102 * doesn't start with a '{', which must have a match
8103 * in the same line (scope is the same). Probably:
8104 * { 1, 2 },
8105 * -> { 3, 4 }
8106 */
8107 if (*skipwhite(l) != '{')
8108 amount += ind_open_extra;
8109
8110 if (ind_cpp_baseclass)
8111 {
8112 /* have to look back, whether it is a cpp base
8113 * class declaration or initialization */
8114 lookfor = LOOKFOR_CPP_BASECLASS;
8115 continue;
8116 }
8117 break;
8118 }
8119
8120 /*
8121 * Check if we are after an "if", "while", etc.
8122 * Also allow " } else".
8123 */
8124 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8125 {
8126 /*
8127 * Found an unterminated line after an if (), line up
8128 * with the last one.
8129 * if (cond)
8130 * 100 +
8131 * -> here;
8132 */
8133 if (lookfor == LOOKFOR_UNTERM
8134 || lookfor == LOOKFOR_ENUM_OR_INIT)
8135 {
8136 if (cont_amount > 0)
8137 amount = cont_amount;
8138 else
8139 amount += ind_continuation;
8140 break;
8141 }
8142
8143 /*
8144 * If this is just above the line we are indenting, we
8145 * are finished.
8146 * while (not)
8147 * -> here;
8148 * Otherwise this indent can be used when the line
8149 * before this is terminated.
8150 * yyy;
8151 * if (stat)
8152 * while (not)
8153 * xxx;
8154 * -> here;
8155 */
8156 amount = cur_amount;
8157 if (theline[0] == '{')
8158 amount += ind_open_extra;
8159 if (lookfor != LOOKFOR_TERM)
8160 {
8161 amount += ind_level + ind_no_brace;
8162 break;
8163 }
8164
8165 /*
8166 * Special trick: when expecting the while () after a
8167 * do, line up with the while()
8168 * do
8169 * x = 1;
8170 * -> here
8171 */
8172 l = skipwhite(ml_get_curline());
8173 if (cin_isdo(l))
8174 {
8175 if (whilelevel == 0)
8176 break;
8177 --whilelevel;
8178 }
8179
8180 /*
8181 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008182 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 * Need to use the scope of this "else". XXX
8184 * If whilelevel != 0 continue looking for a "do {".
8185 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008186 if (cin_iselse(l) && whilelevel == 0)
8187 {
8188 /* If we're looking at "} else", let's make sure we
8189 * find the opening brace of the enclosing scope,
8190 * not the one from "if () {". */
8191 if (*l == '}')
8192 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008193 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008194
8195 if ((trypos = find_start_brace(ind_maxcomment))
8196 == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 || find_match(LOOKFOR_IF, trypos->lnum,
Bram Moolenaar334adf02011-05-25 13:34:04 +02008198 ind_maxparen, ind_maxcomment) == FAIL)
8199 break;
8200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 }
8202
8203 /*
8204 * If we're below an unterminated line that is not an
8205 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008206 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 * the line before this one.
8208 */
8209 else
8210 {
8211 /*
8212 * Found two unterminated lines on a row, line up with
8213 * the last one.
8214 * c = 99 +
8215 * 100 +
8216 * -> here;
8217 */
8218 if (lookfor == LOOKFOR_UNTERM)
8219 {
8220 /* When line ends in a comma add extra indent */
8221 if (terminated == ',')
8222 amount += ind_continuation;
8223 break;
8224 }
8225
8226 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8227 {
8228 /* Found two lines ending in ',', lineup with the
8229 * lowest one, but check for cpp base class
8230 * declaration/initialization, if it is an
8231 * opening brace or we are looking just for
8232 * enumerations/initializations. */
8233 if (terminated == ',')
8234 {
8235 if (ind_cpp_baseclass == 0)
8236 break;
8237
8238 lookfor = LOOKFOR_CPP_BASECLASS;
8239 continue;
8240 }
8241
8242 /* Ignore unterminated lines in between, but
8243 * reduce indent. */
8244 if (amount > cur_amount)
8245 amount = cur_amount;
8246 }
8247 else
8248 {
8249 /*
8250 * Found first unterminated line on a row, may
8251 * line up with this line, remember its indent
8252 * 100 +
8253 * -> here;
8254 */
8255 amount = cur_amount;
8256
8257 /*
8258 * If previous line ends in ',', check whether we
8259 * are in an initialization or enum
8260 * struct xxx =
8261 * {
8262 * sizeof a,
8263 * 124 };
8264 * or a normal possible continuation line.
8265 * but only, of no other statement has been found
8266 * yet.
8267 */
8268 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8269 {
8270 lookfor = LOOKFOR_ENUM_OR_INIT;
8271 cont_amount = cin_first_id_amount();
8272 }
8273 else
8274 {
8275 if (lookfor == LOOKFOR_INITIAL
8276 && *l != NUL
8277 && l[STRLEN(l) - 1] == '\\')
8278 /* XXX */
8279 cont_amount = cin_get_equal_amount(
8280 curwin->w_cursor.lnum);
8281 if (lookfor != LOOKFOR_TERM)
8282 lookfor = LOOKFOR_UNTERM;
8283 }
8284 }
8285 }
8286 }
8287
8288 /*
8289 * Check if we are after a while (cond);
8290 * If so: Ignore until the matching "do".
8291 */
8292 /* XXX */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008293 else if (cin_iswhileofdo_end(terminated, ind_maxparen,
8294 ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {
8296 /*
8297 * Found an unterminated line after a while ();, line up
8298 * with the last one.
8299 * while (cond);
8300 * 100 + <- line up with this one
8301 * -> here;
8302 */
8303 if (lookfor == LOOKFOR_UNTERM
8304 || lookfor == LOOKFOR_ENUM_OR_INIT)
8305 {
8306 if (cont_amount > 0)
8307 amount = cont_amount;
8308 else
8309 amount += ind_continuation;
8310 break;
8311 }
8312
8313 if (whilelevel == 0)
8314 {
8315 lookfor = LOOKFOR_TERM;
8316 amount = get_indent(); /* XXX */
8317 if (theline[0] == '{')
8318 amount += ind_open_extra;
8319 }
8320 ++whilelevel;
8321 }
8322
8323 /*
8324 * We are after a "normal" statement.
8325 * If we had another statement we can stop now and use the
8326 * indent of that other statement.
8327 * Otherwise the indent of the current statement may be used,
8328 * search backwards for the next "normal" statement.
8329 */
8330 else
8331 {
8332 /*
8333 * Skip single break line, if before a switch label. It
8334 * may be lined up with the case label.
8335 */
8336 if (lookfor == LOOKFOR_NOBREAK
8337 && cin_isbreak(skipwhite(ml_get_curline())))
8338 {
8339 lookfor = LOOKFOR_ANY;
8340 continue;
8341 }
8342
8343 /*
8344 * Handle "do {" line.
8345 */
8346 if (whilelevel > 0)
8347 {
8348 l = cin_skipcomment(ml_get_curline());
8349 if (cin_isdo(l))
8350 {
8351 amount = get_indent(); /* XXX */
8352 --whilelevel;
8353 continue;
8354 }
8355 }
8356
8357 /*
8358 * Found a terminated line above an unterminated line. Add
8359 * the amount for a continuation line.
8360 * x = 1;
8361 * y = foo +
8362 * -> here;
8363 * or
8364 * int x = 1;
8365 * int foo,
8366 * -> here;
8367 */
8368 if (lookfor == LOOKFOR_UNTERM
8369 || lookfor == LOOKFOR_ENUM_OR_INIT)
8370 {
8371 if (cont_amount > 0)
8372 amount = cont_amount;
8373 else
8374 amount += ind_continuation;
8375 break;
8376 }
8377
8378 /*
8379 * Found a terminated line above a terminated line or "if"
8380 * etc. line. Use the amount of the line below us.
8381 * x = 1; x = 1;
8382 * if (asdf) y = 2;
8383 * while (asdf) ->here;
8384 * here;
8385 * ->foo;
8386 */
8387 if (lookfor == LOOKFOR_TERM)
8388 {
8389 if (!lookfor_break && whilelevel == 0)
8390 break;
8391 }
8392
8393 /*
8394 * First line above the one we're indenting is terminated.
8395 * To know what needs to be done look further backward for
8396 * a terminated line.
8397 */
8398 else
8399 {
8400 /*
8401 * position the cursor over the rightmost paren, so
8402 * that matching it will take us back to the start of
8403 * the line. Helps for:
8404 * func(asdr,
8405 * asdfasdf);
8406 * here;
8407 */
8408term_again:
8409 l = ml_get_curline();
8410 if (find_last_paren(l, '(', ')')
8411 && (trypos = find_match_paren(ind_maxparen,
8412 ind_maxcomment)) != NULL)
8413 {
8414 /*
8415 * Check if we are on a case label now. This is
8416 * handled above.
8417 * case xx: if ( asdf &&
8418 * asdf)
8419 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008420 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008422 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {
8424 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008425 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 continue;
8427 }
8428 }
8429
8430 /* When aligning with the case statement, don't align
8431 * with a statement after it.
8432 * case 1: { <-- don't use this { position
8433 * stat;
8434 * }
8435 * case 2:
8436 * stat;
8437 * }
8438 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008439 iscase = (ind_keep_case_label && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
8441 /*
8442 * Get indent and pointer to text for current line,
8443 * ignoring any jump label.
8444 */
8445 amount = skip_label(curwin->w_cursor.lnum,
8446 &l, ind_maxcomment);
8447
8448 if (theline[0] == '{')
8449 amount += ind_open_extra;
8450 /* See remark above: "Only add ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008451 l = skipwhite(l);
8452 if (*l == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 amount -= ind_open_extra;
8454 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8455
8456 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008457 * When a terminated line starts with "else" skip to
8458 * the matching "if":
8459 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008460 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008461 * Need to use the scope of this "else". XXX
8462 * If whilelevel != 0 continue looking for a "do {".
8463 */
8464 if (lookfor == LOOKFOR_TERM
8465 && *l != '}'
8466 && cin_iselse(l)
8467 && whilelevel == 0)
8468 {
8469 if ((trypos = find_start_brace(ind_maxcomment))
8470 == NULL
8471 || find_match(LOOKFOR_IF, trypos->lnum,
8472 ind_maxparen, ind_maxcomment) == FAIL)
8473 break;
8474 continue;
8475 }
8476
8477 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 * If we're at the end of a block, skip to the start of
8479 * that block.
8480 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008481 l = ml_get_curline();
Bram Moolenaar50f42ca2011-07-15 14:12:30 +02008482 if (find_last_paren(l, '{', '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 && (trypos = find_start_brace(ind_maxcomment))
8484 != NULL) /* XXX */
8485 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008486 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 /* if not "else {" check for terminated again */
8488 /* but skip block for "} else {" */
8489 l = cin_skipcomment(ml_get_curline());
8490 if (*l == '}' || !cin_iselse(l))
8491 goto term_again;
8492 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008493 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495 }
8496 }
8497 }
8498 }
8499 }
8500
8501 /* add extra indent for a comment */
8502 if (cin_iscomment(theline))
8503 amount += ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008504
8505 /* subtract extra left-shift for jump labels */
8506 if (ind_jump_label > 0 && original_line_islabel)
8507 amount -= ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
8509
8510 /*
8511 * ok -- we're not inside any sort of structure at all!
8512 *
8513 * this means we're at the top level, and everything should
8514 * basically just match where the previous line is, except
8515 * for the lines immediately following a function declaration,
8516 * which are K&R-style parameters and need to be indented.
8517 */
8518 else
8519 {
8520 /*
8521 * if our line starts with an open brace, forget about any
8522 * prevailing indent and make sure it looks like the start
8523 * of a function
8524 */
8525
8526 if (theline[0] == '{')
8527 {
8528 amount = ind_first_open;
8529 }
8530
8531 /*
8532 * If the NEXT line is a function declaration, the current
8533 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008534 * Don't do this if the current line looks like a comment or if the
8535 * current line is terminated, ie. ends in ';', or if the current line
8536 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 */
8538 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8539 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008540 && vim_strchr(theline, '{') == NULL
8541 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 && !cin_ends_in(theline, (char_u *)":", NULL)
8543 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008544 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8545 cur_curpos.lnum + 1,
8546 ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 && !cin_isterminated(theline, FALSE, TRUE))
8548 {
8549 amount = ind_func_type;
8550 }
8551 else
8552 {
8553 amount = 0;
8554 curwin->w_cursor = cur_curpos;
8555
8556 /* search backwards until we find something we recognize */
8557
8558 while (curwin->w_cursor.lnum > 1)
8559 {
8560 curwin->w_cursor.lnum--;
8561 curwin->w_cursor.col = 0;
8562
8563 l = ml_get_curline();
8564
8565 /*
8566 * If we're in a comment now, skip to the start of the comment.
8567 */ /* XXX */
8568 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
8569 {
8570 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008571 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 continue;
8573 }
8574
8575 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008576 * Are we at the start of a cpp base class declaration or
8577 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008579 n = FALSE;
8580 if (ind_cpp_baseclass != 0 && theline[0] != '{')
8581 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008582 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008583 l = ml_get_curline();
8584 }
8585 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008587 /* XXX */
8588 amount = get_baseclass_amount(col, ind_maxparen,
8589 ind_maxcomment, ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 break;
8591 }
8592
8593 /*
8594 * Skip preprocessor directives and blank lines.
8595 */
8596 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8597 continue;
8598
8599 if (cin_nocode(l))
8600 continue;
8601
8602 /*
8603 * If the previous line ends in ',', use one level of
8604 * indentation:
8605 * int foo,
8606 * bar;
8607 * do this before checking for '}' in case of eg.
8608 * enum foobar
8609 * {
8610 * ...
8611 * } foo,
8612 * bar;
8613 */
8614 n = 0;
8615 if (cin_ends_in(l, (char_u *)",", NULL)
8616 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8617 {
8618 /* take us back to opening paren */
8619 if (find_last_paren(l, '(', ')')
8620 && (trypos = find_match_paren(ind_maxparen,
8621 ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008622 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
8624 /* For a line ending in ',' that is a continuation line go
8625 * back to the first line with a backslash:
8626 * char *foo = "bla\
8627 * bla",
8628 * here;
8629 */
8630 while (n == 0 && curwin->w_cursor.lnum > 1)
8631 {
8632 l = ml_get(curwin->w_cursor.lnum - 1);
8633 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8634 break;
8635 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008636 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
8638
8639 amount = get_indent(); /* XXX */
8640
8641 if (amount == 0)
8642 amount = cin_first_id_amount();
8643 if (amount == 0)
8644 amount = ind_continuation;
8645 break;
8646 }
8647
8648 /*
8649 * If the line looks like a function declaration, and we're
8650 * not in a comment, put it the left margin.
8651 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008652 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0,
8653 ind_maxparen, ind_maxcomment)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 break;
8655 l = ml_get_curline();
8656
8657 /*
8658 * Finding the closing '}' of a previous function. Put
8659 * current line at the left margin. For when 'cino' has "fs".
8660 */
8661 if (*skipwhite(l) == '}')
8662 break;
8663
8664 /* (matching {)
8665 * If the previous line ends on '};' (maybe followed by
8666 * comments) align at column 0. For example:
8667 * char *string_array[] = { "foo",
8668 * / * x * / "b};ar" }; / * foobar * /
8669 */
8670 if (cin_ends_in(l, (char_u *)"};", NULL))
8671 break;
8672
8673 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008674 * Find a line only has a semicolon that belongs to a previous
8675 * line ending in '}', e.g. before an #endif. Don't increase
8676 * indent then.
8677 */
8678 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8679 {
8680 pos_T curpos_save = curwin->w_cursor;
8681
8682 while (curwin->w_cursor.lnum > 1)
8683 {
8684 look = ml_get(--curwin->w_cursor.lnum);
8685 if (!(cin_nocode(look) || cin_ispreproc_cont(
8686 &look, &curwin->w_cursor.lnum)))
8687 break;
8688 }
8689 if (curwin->w_cursor.lnum > 0
8690 && cin_ends_in(look, (char_u *)"}", NULL))
8691 break;
8692
8693 curwin->w_cursor = curpos_save;
8694 }
8695
8696 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 * If the PREVIOUS line is a function declaration, the current
8698 * line (and the ones that follow) needs to be indented as
8699 * parameters.
8700 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008701 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0,
8702 ind_maxparen, ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 {
8704 amount = ind_param;
8705 break;
8706 }
8707
8708 /*
8709 * If the previous line ends in ';' and the line before the
8710 * previous line ends in ',' or '\', ident to column zero:
8711 * int foo,
8712 * bar;
8713 * indent_to_0 here;
8714 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008715 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 {
8717 l = ml_get(curwin->w_cursor.lnum - 1);
8718 if (cin_ends_in(l, (char_u *)",", NULL)
8719 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8720 break;
8721 l = ml_get_curline();
8722 }
8723
8724 /*
8725 * Doesn't look like anything interesting -- so just
8726 * use the indent of this line.
8727 *
8728 * Position the cursor over the rightmost paren, so that
8729 * matching it will take us back to the start of the line.
8730 */
8731 find_last_paren(l, '(', ')');
8732
8733 if ((trypos = find_match_paren(ind_maxparen,
8734 ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008735 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 amount = get_indent(); /* XXX */
8737 break;
8738 }
8739
8740 /* add extra indent for a comment */
8741 if (cin_iscomment(theline))
8742 amount += ind_comment;
8743
8744 /* add extra indent if the previous line ended in a backslash:
8745 * "asdfasdf\
8746 * here";
8747 * char *foo = "asdf\
8748 * here";
8749 */
8750 if (cur_curpos.lnum > 1)
8751 {
8752 l = ml_get(cur_curpos.lnum - 1);
8753 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8754 {
8755 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8756 if (cur_amount > 0)
8757 amount = cur_amount;
8758 else if (cur_amount == 0)
8759 amount += ind_continuation;
8760 }
8761 }
8762 }
8763 }
8764
8765theend:
8766 /* put the cursor back where it belongs */
8767 curwin->w_cursor = cur_curpos;
8768
8769 vim_free(linecopy);
8770
8771 if (amount < 0)
8772 return 0;
8773 return amount;
8774}
8775
8776 static int
8777find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment)
8778 int lookfor;
8779 linenr_T ourscope;
8780 int ind_maxparen;
8781 int ind_maxcomment;
8782{
8783 char_u *look;
8784 pos_T *theirscope;
8785 char_u *mightbeif;
8786 int elselevel;
8787 int whilelevel;
8788
8789 if (lookfor == LOOKFOR_IF)
8790 {
8791 elselevel = 1;
8792 whilelevel = 0;
8793 }
8794 else
8795 {
8796 elselevel = 0;
8797 whilelevel = 1;
8798 }
8799
8800 curwin->w_cursor.col = 0;
8801
8802 while (curwin->w_cursor.lnum > ourscope + 1)
8803 {
8804 curwin->w_cursor.lnum--;
8805 curwin->w_cursor.col = 0;
8806
8807 look = cin_skipcomment(ml_get_curline());
8808 if (cin_iselse(look)
8809 || cin_isif(look)
8810 || cin_isdo(look) /* XXX */
8811 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8812 {
8813 /*
8814 * if we've gone outside the braces entirely,
8815 * we must be out of scope...
8816 */
8817 theirscope = find_start_brace(ind_maxcomment); /* XXX */
8818 if (theirscope == NULL)
8819 break;
8820
8821 /*
8822 * and if the brace enclosing this is further
8823 * back than the one enclosing the else, we're
8824 * out of luck too.
8825 */
8826 if (theirscope->lnum < ourscope)
8827 break;
8828
8829 /*
8830 * and if they're enclosed in a *deeper* brace,
8831 * then we can ignore it because it's in a
8832 * different scope...
8833 */
8834 if (theirscope->lnum > ourscope)
8835 continue;
8836
8837 /*
8838 * if it was an "else" (that's not an "else if")
8839 * then we need to go back to another if, so
8840 * increment elselevel
8841 */
8842 look = cin_skipcomment(ml_get_curline());
8843 if (cin_iselse(look))
8844 {
8845 mightbeif = cin_skipcomment(look + 4);
8846 if (!cin_isif(mightbeif))
8847 ++elselevel;
8848 continue;
8849 }
8850
8851 /*
8852 * if it was a "while" then we need to go back to
8853 * another "do", so increment whilelevel. XXX
8854 */
8855 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8856 {
8857 ++whilelevel;
8858 continue;
8859 }
8860
8861 /* If it's an "if" decrement elselevel */
8862 look = cin_skipcomment(ml_get_curline());
8863 if (cin_isif(look))
8864 {
8865 elselevel--;
8866 /*
8867 * When looking for an "if" ignore "while"s that
8868 * get in the way.
8869 */
8870 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8871 whilelevel = 0;
8872 }
8873
8874 /* If it's a "do" decrement whilelevel */
8875 if (cin_isdo(look))
8876 whilelevel--;
8877
8878 /*
8879 * if we've used up all the elses, then
8880 * this must be the if that we want!
8881 * match the indent level of that if.
8882 */
8883 if (elselevel <= 0 && whilelevel <= 0)
8884 {
8885 return OK;
8886 }
8887 }
8888 }
8889 return FAIL;
8890}
8891
8892# if defined(FEAT_EVAL) || defined(PROTO)
8893/*
8894 * Get indent level from 'indentexpr'.
8895 */
8896 int
8897get_expr_indent()
8898{
8899 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008900 pos_T save_pos;
8901 colnr_T save_curswant;
8902 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008904 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8905 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008907 /* Save and restore cursor position and curswant, in case it was changed
8908 * via :normal commands */
8909 save_pos = curwin->w_cursor;
8910 save_curswant = curwin->w_curswant;
8911 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008913 if (use_sandbox)
8914 ++sandbox;
8915 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008917 if (use_sandbox)
8918 --sandbox;
8919 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
8921 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8922 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8923 * command. */
8924 save_State = State;
8925 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008926 curwin->w_cursor = save_pos;
8927 curwin->w_curswant = save_curswant;
8928 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 check_cursor();
8930 State = save_State;
8931
8932 /* If there is an error, just keep the current indent. */
8933 if (indent < 0)
8934 indent = get_indent();
8935
8936 return indent;
8937}
8938# endif
8939
8940#endif /* FEAT_CINDENT */
8941
8942#if defined(FEAT_LISP) || defined(PROTO)
8943
8944static int lisp_match __ARGS((char_u *p));
8945
8946 static int
8947lisp_match(p)
8948 char_u *p;
8949{
8950 char_u buf[LSIZE];
8951 int len;
8952 char_u *word = p_lispwords;
8953
8954 while (*word != NUL)
8955 {
8956 (void)copy_option_part(&word, buf, LSIZE, ",");
8957 len = (int)STRLEN(buf);
8958 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8959 return TRUE;
8960 }
8961 return FALSE;
8962}
8963
8964/*
8965 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8966 * The incompatible newer method is quite a bit better at indenting
8967 * code in lisp-like languages than the traditional one; it's still
8968 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8969 *
8970 * TODO:
8971 * Findmatch() should be adapted for lisp, also to make showmatch
8972 * work correctly: now (v5.3) it seems all C/C++ oriented:
8973 * - it does not recognize the #\( and #\) notations as character literals
8974 * - it doesn't know about comments starting with a semicolon
8975 * - it incorrectly interprets '(' as a character literal
8976 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008977 * Update from Sergey Khorev:
8978 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 */
8980 int
8981get_lisp_indent()
8982{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008983 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 int amount;
8985 char_u *that;
8986 colnr_T col;
8987 colnr_T firsttry;
8988 int parencount, quotecount;
8989 int vi_lisp;
8990
8991 /* Set vi_lisp to use the vi-compatible method */
8992 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8993
8994 realpos = curwin->w_cursor;
8995 curwin->w_cursor.col = 0;
8996
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008997 if ((pos = findmatch(NULL, '(')) == NULL)
8998 pos = findmatch(NULL, '[');
8999 else
9000 {
9001 paren = *pos;
9002 pos = findmatch(NULL, '[');
9003 if (pos == NULL || ltp(pos, &paren))
9004 pos = &paren;
9005 }
9006 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 {
9008 /* Extra trick: Take the indent of the first previous non-white
9009 * line that is at the same () level. */
9010 amount = -1;
9011 parencount = 0;
9012
9013 while (--curwin->w_cursor.lnum >= pos->lnum)
9014 {
9015 if (linewhite(curwin->w_cursor.lnum))
9016 continue;
9017 for (that = ml_get_curline(); *that != NUL; ++that)
9018 {
9019 if (*that == ';')
9020 {
9021 while (*(that + 1) != NUL)
9022 ++that;
9023 continue;
9024 }
9025 if (*that == '\\')
9026 {
9027 if (*(that + 1) != NUL)
9028 ++that;
9029 continue;
9030 }
9031 if (*that == '"' && *(that + 1) != NUL)
9032 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009033 while (*++that && *that != '"')
9034 {
9035 /* skipping escaped characters in the string */
9036 if (*that == '\\')
9037 {
9038 if (*++that == NUL)
9039 break;
9040 if (that[1] == NUL)
9041 {
9042 ++that;
9043 break;
9044 }
9045 }
9046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009048 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009050 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 --parencount;
9052 }
9053 if (parencount == 0)
9054 {
9055 amount = get_indent();
9056 break;
9057 }
9058 }
9059
9060 if (amount == -1)
9061 {
9062 curwin->w_cursor.lnum = pos->lnum;
9063 curwin->w_cursor.col = pos->col;
9064 col = pos->col;
9065
9066 that = ml_get_curline();
9067
9068 if (vi_lisp && get_indent() == 0)
9069 amount = 2;
9070 else
9071 {
9072 amount = 0;
9073 while (*that && col)
9074 {
9075 amount += lbr_chartabsize_adv(&that, (colnr_T)amount);
9076 col--;
9077 }
9078
9079 /*
9080 * Some keywords require "body" indenting rules (the
9081 * non-standard-lisp ones are Scheme special forms):
9082 *
9083 * (let ((a 1)) instead (let ((a 1))
9084 * (...)) of (...))
9085 */
9086
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009087 if (!vi_lisp && (*that == '(' || *that == '[')
9088 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 amount += 2;
9090 else
9091 {
9092 that++;
9093 amount++;
9094 firsttry = amount;
9095
9096 while (vim_iswhite(*that))
9097 {
9098 amount += lbr_chartabsize(that, (colnr_T)amount);
9099 ++that;
9100 }
9101
9102 if (*that && *that != ';') /* not a comment line */
9103 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009104 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009106 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 firsttry++;
9108
9109 parencount = 0;
9110 quotecount = 0;
9111
9112 if (vi_lisp
9113 || (*that != '"'
9114 && *that != '\''
9115 && *that != '#'
9116 && (*that < '0' || *that > '9')))
9117 {
9118 while (*that
9119 && (!vim_iswhite(*that)
9120 || quotecount
9121 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009122 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 && !quotecount
9124 && !parencount
9125 && vi_lisp)))
9126 {
9127 if (*that == '"')
9128 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009129 if ((*that == '(' || *that == '[')
9130 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009132 if ((*that == ')' || *that == ']')
9133 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 --parencount;
9135 if (*that == '\\' && *(that+1) != NUL)
9136 amount += lbr_chartabsize_adv(&that,
9137 (colnr_T)amount);
9138 amount += lbr_chartabsize_adv(&that,
9139 (colnr_T)amount);
9140 }
9141 }
9142 while (vim_iswhite(*that))
9143 {
9144 amount += lbr_chartabsize(that, (colnr_T)amount);
9145 that++;
9146 }
9147 if (!*that || *that == ';')
9148 amount = firsttry;
9149 }
9150 }
9151 }
9152 }
9153 }
9154 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009155 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156
9157 curwin->w_cursor = realpos;
9158
9159 return amount;
9160}
9161#endif /* FEAT_LISP */
9162
9163 void
9164prepare_to_exit()
9165{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009166#if defined(SIGHUP) && defined(SIG_IGN)
9167 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9168 * makes Vim exit and then handling SIGHUP causes various reentrance
9169 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009170 signal(SIGHUP, SIG_IGN);
9171#endif
9172
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173#ifdef FEAT_GUI
9174 if (gui.in_use)
9175 {
9176 gui.dying = TRUE;
9177 out_trash(); /* trash any pending output */
9178 }
9179 else
9180#endif
9181 {
9182 windgoto((int)Rows - 1, 0);
9183
9184 /*
9185 * Switch terminal mode back now, so messages end up on the "normal"
9186 * screen (if there are two screens).
9187 */
9188 settmode(TMODE_COOK);
9189#ifdef WIN3264
9190 if (can_end_termcap_mode(FALSE) == TRUE)
9191#endif
9192 stoptermcap();
9193 out_flush();
9194 }
9195}
9196
9197/*
9198 * Preserve files and exit.
9199 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009200 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9201 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 */
9203 void
9204preserve_exit()
9205{
9206 buf_T *buf;
9207
9208 prepare_to_exit();
9209
Bram Moolenaar4770d092006-01-12 23:22:24 +00009210 /* Setting this will prevent free() calls. That avoids calling free()
9211 * recursively when free() was invoked with a bad pointer. */
9212 really_exiting = TRUE;
9213
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 out_str(IObuff);
9215 screen_start(); /* don't know where cursor is now */
9216 out_flush();
9217
9218 ml_close_notmod(); /* close all not-modified buffers */
9219
9220 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9221 {
9222 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9223 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009224 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 screen_start(); /* don't know where cursor is now */
9226 out_flush();
9227 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9228 break;
9229 }
9230 }
9231
9232 ml_close_all(FALSE); /* close all memfiles, without deleting */
9233
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009234 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235
9236 getout(1);
9237}
9238
9239/*
9240 * return TRUE if "fname" exists.
9241 */
9242 int
9243vim_fexists(fname)
9244 char_u *fname;
9245{
9246 struct stat st;
9247
9248 if (mch_stat((char *)fname, &st))
9249 return FALSE;
9250 return TRUE;
9251}
9252
9253/*
9254 * Check for CTRL-C pressed, but only once in a while.
9255 * Should be used instead of ui_breakcheck() for functions that check for
9256 * each line in the file. Calling ui_breakcheck() each time takes too much
9257 * time, because it can be a system call.
9258 */
9259
9260#ifndef BREAKCHECK_SKIP
9261# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9262# define BREAKCHECK_SKIP 200
9263# else
9264# define BREAKCHECK_SKIP 32
9265# endif
9266#endif
9267
9268static int breakcheck_count = 0;
9269
9270 void
9271line_breakcheck()
9272{
9273 if (++breakcheck_count >= BREAKCHECK_SKIP)
9274 {
9275 breakcheck_count = 0;
9276 ui_breakcheck();
9277 }
9278}
9279
9280/*
9281 * Like line_breakcheck() but check 10 times less often.
9282 */
9283 void
9284fast_breakcheck()
9285{
9286 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9287 {
9288 breakcheck_count = 0;
9289 ui_breakcheck();
9290 }
9291}
9292
9293/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009294 * Invoke expand_wildcards() for one pattern.
9295 * Expand items like "%:h" before the expansion.
9296 * Returns OK or FAIL.
9297 */
9298 int
9299expand_wildcards_eval(pat, num_file, file, flags)
9300 char_u **pat; /* pointer to input pattern */
9301 int *num_file; /* resulting number of files */
9302 char_u ***file; /* array of resulting files */
9303 int flags; /* EW_DIR, etc. */
9304{
9305 int ret = FAIL;
9306 char_u *eval_pat = NULL;
9307 char_u *exp_pat = *pat;
9308 char_u *ignored_msg;
9309 int usedlen;
9310
9311 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9312 {
9313 ++emsg_off;
9314 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9315 NULL, &ignored_msg, NULL);
9316 --emsg_off;
9317 if (eval_pat != NULL)
9318 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9319 }
9320
9321 if (exp_pat != NULL)
9322 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9323
9324 if (eval_pat != NULL)
9325 {
9326 vim_free(exp_pat);
9327 vim_free(eval_pat);
9328 }
9329
9330 return ret;
9331}
9332
9333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9335 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009336 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 */
9338 int
9339expand_wildcards(num_pat, pat, num_file, file, flags)
9340 int num_pat; /* number of input patterns */
9341 char_u **pat; /* array of input patterns */
9342 int *num_file; /* resulting number of files */
9343 char_u ***file; /* array of resulting files */
9344 int flags; /* EW_DIR, etc. */
9345{
9346 int retval;
9347 int i, j;
9348 char_u *p;
9349 int non_suf_match; /* number without matching suffix */
9350
9351 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9352
9353 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009354 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 return retval;
9356
9357#ifdef FEAT_WILDIGN
9358 /*
9359 * Remove names that match 'wildignore'.
9360 */
9361 if (*p_wig)
9362 {
9363 char_u *ffname;
9364
9365 /* check all files in (*file)[] */
9366 for (i = 0; i < *num_file; ++i)
9367 {
9368 ffname = FullName_save((*file)[i], FALSE);
9369 if (ffname == NULL) /* out of memory */
9370 break;
9371# ifdef VMS
9372 vms_remove_version(ffname);
9373# endif
9374 if (match_file_list(p_wig, (*file)[i], ffname))
9375 {
9376 /* remove this matching file from the list */
9377 vim_free((*file)[i]);
9378 for (j = i; j + 1 < *num_file; ++j)
9379 (*file)[j] = (*file)[j + 1];
9380 --*num_file;
9381 --i;
9382 }
9383 vim_free(ffname);
9384 }
9385 }
9386#endif
9387
9388 /*
9389 * Move the names where 'suffixes' match to the end.
9390 */
9391 if (*num_file > 1)
9392 {
9393 non_suf_match = 0;
9394 for (i = 0; i < *num_file; ++i)
9395 {
9396 if (!match_suffix((*file)[i]))
9397 {
9398 /*
9399 * Move the name without matching suffix to the front
9400 * of the list.
9401 */
9402 p = (*file)[i];
9403 for (j = i; j > non_suf_match; --j)
9404 (*file)[j] = (*file)[j - 1];
9405 (*file)[non_suf_match++] = p;
9406 }
9407 }
9408 }
9409
9410 return retval;
9411}
9412
9413/*
9414 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9415 */
9416 int
9417match_suffix(fname)
9418 char_u *fname;
9419{
9420 int fnamelen, setsuflen;
9421 char_u *setsuf;
9422#define MAXSUFLEN 30 /* maximum length of a file suffix */
9423 char_u suf_buf[MAXSUFLEN];
9424
9425 fnamelen = (int)STRLEN(fname);
9426 setsuflen = 0;
9427 for (setsuf = p_su; *setsuf; )
9428 {
9429 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009430 if (setsuflen == 0)
9431 {
9432 char_u *tail = gettail(fname);
9433
9434 /* empty entry: match name without a '.' */
9435 if (vim_strchr(tail, '.') == NULL)
9436 {
9437 setsuflen = 1;
9438 break;
9439 }
9440 }
9441 else
9442 {
9443 if (fnamelen >= setsuflen
9444 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9445 (size_t)setsuflen) == 0)
9446 break;
9447 setsuflen = 0;
9448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 }
9450 return (setsuflen != 0);
9451}
9452
9453#if !defined(NO_EXPANDPATH) || defined(PROTO)
9454
9455# ifdef VIM_BACKTICK
9456static int vim_backtick __ARGS((char_u *p));
9457static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9458# endif
9459
9460# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9461/*
9462 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9463 * it's shared between these systems.
9464 */
9465# if defined(DJGPP) || defined(PROTO)
9466# define _cdecl /* DJGPP doesn't have this */
9467# else
9468# ifdef __BORLANDC__
9469# define _cdecl _RTLENTRYF
9470# endif
9471# endif
9472
9473/*
9474 * comparison function for qsort in dos_expandpath()
9475 */
9476 static int _cdecl
9477pstrcmp(const void *a, const void *b)
9478{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009479 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480}
9481
9482# ifndef WIN3264
9483 static void
9484namelowcpy(
9485 char_u *d,
9486 char_u *s)
9487{
9488# ifdef DJGPP
9489 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9490 while (*s)
9491 *d++ = *s++;
9492 else
9493# endif
9494 while (*s)
9495 *d++ = TOLOWER_LOC(*s++);
9496 *d = NUL;
9497}
9498# endif
9499
9500/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009501 * Recursively expand one path component into all matching files and/or
9502 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 * Return the number of matches found.
9504 * "path" has backslashes before chars that are not to be expanded, starting
9505 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009506 * Return the number of matches found.
9507 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 */
9509 static int
9510dos_expandpath(
9511 garray_T *gap,
9512 char_u *path,
9513 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009514 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009515 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009517 char_u *buf;
9518 char_u *path_end;
9519 char_u *p, *s, *e;
9520 int start_len = gap->ga_len;
9521 char_u *pat;
9522 regmatch_T regmatch;
9523 int starts_with_dot;
9524 int matches;
9525 int len;
9526 int starstar = FALSE;
9527 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528#ifdef WIN3264
9529 WIN32_FIND_DATA fb;
9530 HANDLE hFind = (HANDLE)0;
9531# ifdef FEAT_MBYTE
9532 WIN32_FIND_DATAW wfb;
9533 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9534# endif
9535#else
9536 struct ffblk fb;
9537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009539 int ok;
9540
9541 /* Expanding "**" may take a long time, check for CTRL-C. */
9542 if (stardepth > 0)
9543 {
9544 ui_breakcheck();
9545 if (got_int)
9546 return 0;
9547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
9549 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009550 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 if (buf == NULL)
9552 return 0;
9553
9554 /*
9555 * Find the first part in the path name that contains a wildcard or a ~1.
9556 * Copy it into buf, including the preceding characters.
9557 */
9558 p = buf;
9559 s = buf;
9560 e = NULL;
9561 path_end = path;
9562 while (*path_end != NUL)
9563 {
9564 /* May ignore a wildcard that has a backslash before it; it will
9565 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9566 if (path_end >= path + wildoff && rem_backslash(path_end))
9567 *p++ = *path_end++;
9568 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9569 {
9570 if (e != NULL)
9571 break;
9572 s = p + 1;
9573 }
9574 else if (path_end >= path + wildoff
9575 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9576 e = p;
9577#ifdef FEAT_MBYTE
9578 if (has_mbyte)
9579 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009580 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 STRNCPY(p, path_end, len);
9582 p += len;
9583 path_end += len;
9584 }
9585 else
9586#endif
9587 *p++ = *path_end++;
9588 }
9589 e = p;
9590 *e = NUL;
9591
9592 /* now we have one wildcard component between s and e */
9593 /* Remove backslashes between "wildoff" and the start of the wildcard
9594 * component. */
9595 for (p = buf + wildoff; p < s; ++p)
9596 if (rem_backslash(p))
9597 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009598 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 --e;
9600 --s;
9601 }
9602
Bram Moolenaar231334e2005-07-25 20:46:57 +00009603 /* Check for "**" between "s" and "e". */
9604 for (p = s; p < e; ++p)
9605 if (p[0] == '*' && p[1] == '*')
9606 starstar = TRUE;
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 starts_with_dot = (*s == '.');
9609 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9610 if (pat == NULL)
9611 {
9612 vim_free(buf);
9613 return 0;
9614 }
9615
9616 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009617 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009618 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 regmatch.rm_ic = TRUE; /* Always ignore case */
9620 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009621 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009622 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 vim_free(pat);
9624
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009625 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 {
9627 vim_free(buf);
9628 return 0;
9629 }
9630
9631 /* remember the pattern or file name being looked for */
9632 matchname = vim_strsave(s);
9633
Bram Moolenaar231334e2005-07-25 20:46:57 +00009634 /* If "**" is by itself, this is the first time we encounter it and more
9635 * is following then find matches without any directory. */
9636 if (!didstar && stardepth < 100 && starstar && e - s == 2
9637 && *path_end == '/')
9638 {
9639 STRCPY(s, path_end + 1);
9640 ++stardepth;
9641 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9642 --stardepth;
9643 }
9644
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 /* Scan all files in the directory with "dir/ *.*" */
9646 STRCPY(s, "*.*");
9647#ifdef WIN3264
9648# ifdef FEAT_MBYTE
9649 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9650 {
9651 /* The active codepage differs from 'encoding'. Attempt using the
9652 * wide function. If it fails because it is not implemented fall back
9653 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009654 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 if (wn != NULL)
9656 {
9657 hFind = FindFirstFileW(wn, &wfb);
9658 if (hFind == INVALID_HANDLE_VALUE
9659 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9660 {
9661 vim_free(wn);
9662 wn = NULL;
9663 }
9664 }
9665 }
9666
9667 if (wn == NULL)
9668# endif
9669 hFind = FindFirstFile(buf, &fb);
9670 ok = (hFind != INVALID_HANDLE_VALUE);
9671#else
9672 /* If we are expanding wildcards we try both files and directories */
9673 ok = (findfirst((char *)buf, &fb,
9674 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9675#endif
9676
9677 while (ok)
9678 {
9679#ifdef WIN3264
9680# ifdef FEAT_MBYTE
9681 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009682 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 else
9684# endif
9685 p = (char_u *)fb.cFileName;
9686#else
9687 p = (char_u *)fb.ff_name;
9688#endif
9689 /* Ignore entries starting with a dot, unless when asked for. Accept
9690 * all entries found with "matchname". */
9691 if ((p[0] != '.' || starts_with_dot)
9692 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009693 || (regmatch.regprog != NULL
9694 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009695 || ((flags & EW_NOTWILD)
9696 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 {
9698#ifdef WIN3264
9699 STRCPY(s, p);
9700#else
9701 namelowcpy(s, p);
9702#endif
9703 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009704
9705 if (starstar && stardepth < 100)
9706 {
9707 /* For "**" in the pattern first go deeper in the tree to
9708 * find matches. */
9709 STRCPY(buf + len, "/**");
9710 STRCPY(buf + len + 3, path_end);
9711 ++stardepth;
9712 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9713 --stardepth;
9714 }
9715
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 STRCPY(buf + len, path_end);
9717 if (mch_has_exp_wildcard(path_end))
9718 {
9719 /* need to expand another component of the path */
9720 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009721 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 }
9723 else
9724 {
9725 /* no more wildcards, check if there is a match */
9726 /* remove backslashes for the remaining components only */
9727 if (*path_end != 0)
9728 backslash_halve(buf + len + 1);
9729 if (mch_getperm(buf) >= 0) /* add existing file */
9730 addfile(gap, buf, flags);
9731 }
9732 }
9733
9734#ifdef WIN3264
9735# ifdef FEAT_MBYTE
9736 if (wn != NULL)
9737 {
9738 vim_free(p);
9739 ok = FindNextFileW(hFind, &wfb);
9740 }
9741 else
9742# endif
9743 ok = FindNextFile(hFind, &fb);
9744#else
9745 ok = (findnext(&fb) == 0);
9746#endif
9747
9748 /* If no more matches and no match was used, try expanding the name
9749 * itself. Finds the long name of a short filename. */
9750 if (!ok && matchname != NULL && gap->ga_len == start_len)
9751 {
9752 STRCPY(s, matchname);
9753#ifdef WIN3264
9754 FindClose(hFind);
9755# ifdef FEAT_MBYTE
9756 if (wn != NULL)
9757 {
9758 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009759 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 if (wn != NULL)
9761 hFind = FindFirstFileW(wn, &wfb);
9762 }
9763 if (wn == NULL)
9764# endif
9765 hFind = FindFirstFile(buf, &fb);
9766 ok = (hFind != INVALID_HANDLE_VALUE);
9767#else
9768 ok = (findfirst((char *)buf, &fb,
9769 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9770#endif
9771 vim_free(matchname);
9772 matchname = NULL;
9773 }
9774 }
9775
9776#ifdef WIN3264
9777 FindClose(hFind);
9778# ifdef FEAT_MBYTE
9779 vim_free(wn);
9780# endif
9781#endif
9782 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009783 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 vim_free(matchname);
9785
9786 matches = gap->ga_len - start_len;
9787 if (matches > 0)
9788 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9789 sizeof(char_u *), pstrcmp);
9790 return matches;
9791}
9792
9793 int
9794mch_expandpath(
9795 garray_T *gap,
9796 char_u *path,
9797 int flags) /* EW_* flags */
9798{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009799 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800}
9801# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9802
Bram Moolenaar231334e2005-07-25 20:46:57 +00009803#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9804 || defined(PROTO)
9805/*
9806 * Unix style wildcard expansion code.
9807 * It's here because it's used both for Unix and Mac.
9808 */
9809static int pstrcmp __ARGS((const void *, const void *));
9810
9811 static int
9812pstrcmp(a, b)
9813 const void *a, *b;
9814{
9815 return (pathcmp(*(char **)a, *(char **)b, -1));
9816}
9817
9818/*
9819 * Recursively expand one path component into all matching files and/or
9820 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9821 * "path" has backslashes before chars that are not to be expanded, starting
9822 * at "path + wildoff".
9823 * Return the number of matches found.
9824 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9825 */
9826 int
9827unix_expandpath(gap, path, wildoff, flags, didstar)
9828 garray_T *gap;
9829 char_u *path;
9830 int wildoff;
9831 int flags; /* EW_* flags */
9832 int didstar; /* expanded "**" once already */
9833{
9834 char_u *buf;
9835 char_u *path_end;
9836 char_u *p, *s, *e;
9837 int start_len = gap->ga_len;
9838 char_u *pat;
9839 regmatch_T regmatch;
9840 int starts_with_dot;
9841 int matches;
9842 int len;
9843 int starstar = FALSE;
9844 static int stardepth = 0; /* depth for "**" expansion */
9845
9846 DIR *dirp;
9847 struct dirent *dp;
9848
9849 /* Expanding "**" may take a long time, check for CTRL-C. */
9850 if (stardepth > 0)
9851 {
9852 ui_breakcheck();
9853 if (got_int)
9854 return 0;
9855 }
9856
9857 /* make room for file name */
9858 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9859 if (buf == NULL)
9860 return 0;
9861
9862 /*
9863 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009864 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009865 * Copy it into "buf", including the preceding characters.
9866 */
9867 p = buf;
9868 s = buf;
9869 e = NULL;
9870 path_end = path;
9871 while (*path_end != NUL)
9872 {
9873 /* May ignore a wildcard that has a backslash before it; it will
9874 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9875 if (path_end >= path + wildoff && rem_backslash(path_end))
9876 *p++ = *path_end++;
9877 else if (*path_end == '/')
9878 {
9879 if (e != NULL)
9880 break;
9881 s = p + 1;
9882 }
9883 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009884 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009885 || (!p_fic && (flags & EW_ICASE)
9886 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009887 e = p;
9888#ifdef FEAT_MBYTE
9889 if (has_mbyte)
9890 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009891 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009892 STRNCPY(p, path_end, len);
9893 p += len;
9894 path_end += len;
9895 }
9896 else
9897#endif
9898 *p++ = *path_end++;
9899 }
9900 e = p;
9901 *e = NUL;
9902
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009903 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009904 /* Remove backslashes between "wildoff" and the start of the wildcard
9905 * component. */
9906 for (p = buf + wildoff; p < s; ++p)
9907 if (rem_backslash(p))
9908 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009909 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009910 --e;
9911 --s;
9912 }
9913
9914 /* Check for "**" between "s" and "e". */
9915 for (p = s; p < e; ++p)
9916 if (p[0] == '*' && p[1] == '*')
9917 starstar = TRUE;
9918
9919 /* convert the file pattern to a regexp pattern */
9920 starts_with_dot = (*s == '.');
9921 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9922 if (pat == NULL)
9923 {
9924 vim_free(buf);
9925 return 0;
9926 }
9927
9928 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009929 if (flags & EW_ICASE)
9930 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9931 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009932 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009933 if (flags & (EW_NOERROR | EW_NOTWILD))
9934 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009935 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009936 if (flags & (EW_NOERROR | EW_NOTWILD))
9937 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009938 vim_free(pat);
9939
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009940 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009941 {
9942 vim_free(buf);
9943 return 0;
9944 }
9945
9946 /* If "**" is by itself, this is the first time we encounter it and more
9947 * is following then find matches without any directory. */
9948 if (!didstar && stardepth < 100 && starstar && e - s == 2
9949 && *path_end == '/')
9950 {
9951 STRCPY(s, path_end + 1);
9952 ++stardepth;
9953 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9954 --stardepth;
9955 }
9956
9957 /* open the directory for scanning */
9958 *s = NUL;
9959 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9960
9961 /* Find all matching entries */
9962 if (dirp != NULL)
9963 {
9964 for (;;)
9965 {
9966 dp = readdir(dirp);
9967 if (dp == NULL)
9968 break;
9969 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009970 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9971 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009972 || ((flags & EW_NOTWILD)
9973 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009974 {
9975 STRCPY(s, dp->d_name);
9976 len = STRLEN(buf);
9977
9978 if (starstar && stardepth < 100)
9979 {
9980 /* For "**" in the pattern first go deeper in the tree to
9981 * find matches. */
9982 STRCPY(buf + len, "/**");
9983 STRCPY(buf + len + 3, path_end);
9984 ++stardepth;
9985 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9986 --stardepth;
9987 }
9988
9989 STRCPY(buf + len, path_end);
9990 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9991 {
9992 /* need to expand another component of the path */
9993 /* remove backslashes for the remaining components only */
9994 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9995 }
9996 else
9997 {
9998 /* no more wildcards, check if there is a match */
9999 /* remove backslashes for the remaining components only */
10000 if (*path_end != NUL)
10001 backslash_halve(buf + len + 1);
10002 if (mch_getperm(buf) >= 0) /* add existing file */
10003 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010004#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010005 size_t precomp_len = STRLEN(buf)+1;
10006 char_u *precomp_buf =
10007 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010008
Bram Moolenaar231334e2005-07-25 20:46:57 +000010009 if (precomp_buf)
10010 {
10011 mch_memmove(buf, precomp_buf, precomp_len);
10012 vim_free(precomp_buf);
10013 }
10014#endif
10015 addfile(gap, buf, flags);
10016 }
10017 }
10018 }
10019 }
10020
10021 closedir(dirp);
10022 }
10023
10024 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010025 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010026
10027 matches = gap->ga_len - start_len;
10028 if (matches > 0)
10029 qsort(((char_u **)gap->ga_data) + start_len, matches,
10030 sizeof(char_u *), pstrcmp);
10031 return matches;
10032}
10033#endif
10034
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010035#if defined(FEAT_SEARCHPATH)
10036static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10037static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010038static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10039static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010040static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10041static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10042
10043/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010044 * Moves "*psep" back to the previous path separator in "path".
10045 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010046 */
10047 static int
10048find_previous_pathsep(path, psep)
10049 char_u *path;
10050 char_u **psep;
10051{
10052 /* skip the current separator */
10053 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010054 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010055
10056 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010057 while (*psep > path)
10058 {
10059 if (vim_ispathsep(**psep))
10060 return OK;
10061 mb_ptr_back(path, *psep);
10062 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010063
10064 return FAIL;
10065}
10066
10067/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010068 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10069 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010070 */
10071 static int
10072is_unique(maybe_unique, gap, i)
10073 char_u *maybe_unique;
10074 garray_T *gap;
10075 int i;
10076{
10077 int j;
10078 int candidate_len;
10079 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010080 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010081 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010082
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010083 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010084 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010085 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010086 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010087
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010088 candidate_len = (int)STRLEN(maybe_unique);
10089 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010090 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010091 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010092
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010093 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010094 if (fnamecmp(maybe_unique, rival) == 0
10095 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010096 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010097 }
10098
Bram Moolenaar162bd912010-07-28 22:29:10 +020010099 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010100}
10101
10102/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010103 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010104 * paths are expanded to their equivalent fullpath. This includes the "."
10105 * (relative to current buffer directory) and empty path (relative to current
10106 * directory) notations.
10107 *
10108 * TODO: handle upward search (;) and path limiter (**N) notations by
10109 * expanding each into their equivalent path(s).
10110 */
10111 static void
10112expand_path_option(curdir, gap)
10113 char_u *curdir;
10114 garray_T *gap;
10115{
10116 char_u *path_option = *curbuf->b_p_path == NUL
10117 ? p_path : curbuf->b_p_path;
10118 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010119 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010120 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010121
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010122 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010123 return;
10124
10125 while (*path_option != NUL)
10126 {
10127 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10128
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010129 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010130 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010131 /* Relative to current buffer:
10132 * "/path/file" + "." -> "/path/"
10133 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010134 if (curbuf->b_ffname == NULL)
10135 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010136 p = gettail(curbuf->b_ffname);
10137 len = (int)(p - curbuf->b_ffname);
10138 if (len + (int)STRLEN(buf) >= MAXPATHL)
10139 continue;
10140 if (buf[1] == NUL)
10141 buf[len] = NUL;
10142 else
10143 STRMOVE(buf + len, buf + 2);
10144 mch_memmove(buf, curbuf->b_ffname, len);
10145 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010146 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010147 else if (buf[0] == NUL)
10148 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010149 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010150 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010151 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010152 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010153 else if (!mch_isFullName(buf))
10154 {
10155 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010156 len = (int)STRLEN(curdir);
10157 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010158 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010159 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010160 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010161 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010162 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010163 }
10164
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010165 if (ga_grow(gap, 1) == FAIL)
10166 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010167
10168# if defined(MSWIN) || defined(MSDOS)
10169 /* Avoid the path ending in a backslash, it fails when a comma is
10170 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010171 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010172 if (buf[len - 1] == '\\')
10173 buf[len - 1] = '/';
10174# endif
10175
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010176 p = vim_strsave(buf);
10177 if (p == NULL)
10178 break;
10179 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010180 }
10181
10182 vim_free(buf);
10183}
10184
10185/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010186 * Returns a pointer to the file or directory name in "fname" that matches the
10187 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010188 *
10189 * path: /foo/bar/baz
10190 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010191 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010192 */
10193 static char_u *
10194get_path_cutoff(fname, gap)
10195 char_u *fname;
10196 garray_T *gap;
10197{
10198 int i;
10199 int maxlen = 0;
10200 char_u **path_part = (char_u **)gap->ga_data;
10201 char_u *cutoff = NULL;
10202
10203 for (i = 0; i < gap->ga_len; i++)
10204 {
10205 int j = 0;
10206
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010207 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010208# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010209 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10210#endif
10211 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010212 j++;
10213 if (j > maxlen)
10214 {
10215 maxlen = j;
10216 cutoff = &fname[j];
10217 }
10218 }
10219
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010220 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010221 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010222 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010223 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010224
10225 return cutoff;
10226}
10227
10228/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010229 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10230 * that they are unique with respect to each other while conserving the part
10231 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010232 */
10233 static void
10234uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010235 garray_T *gap;
10236 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010237{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010238 int i;
10239 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010240 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010241 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010242 char_u *pat;
10243 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010244 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010245 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010246 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010247 char_u **in_curdir = NULL;
10248 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010249
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010250 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010251 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010252
10253 /*
10254 * We need to prepend a '*' at the beginning of file_pattern so that the
10255 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010256 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010257 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010258 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010259 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010260 if (file_pattern == NULL)
10261 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010262 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010263 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010264 STRCAT(file_pattern, pattern);
10265 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10266 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010267 if (pat == NULL)
10268 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010269
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010270 regmatch.rm_ic = TRUE; /* always ignore case */
10271 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10272 vim_free(pat);
10273 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010274 return;
10275
Bram Moolenaar162bd912010-07-28 22:29:10 +020010276 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010277 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010278 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010279 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010280
10281 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010282 if (in_curdir == NULL)
10283 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010284
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010285 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010286 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010287 char_u *path = fnames[i];
10288 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010289 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010290 char_u *pathsep_p;
10291 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010292
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010293 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010294 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010295 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010296 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010297 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010298
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010299 /* Shorten the filename while maintaining its uniqueness */
10300 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010301
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010302 /* we start at the end of the path */
10303 pathsep_p = path + len - 1;
10304
10305 while (find_previous_pathsep(path, &pathsep_p))
10306 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10307 && is_unique(pathsep_p + 1, gap, i)
10308 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10309 {
10310 sort_again = TRUE;
10311 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10312 break;
10313 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010314
10315 if (mch_isFullName(path))
10316 {
10317 /*
10318 * Last resort: shorten relative to curdir if possible.
10319 * 'possible' means:
10320 * 1. It is under the current directory.
10321 * 2. The result is actually shorter than the original.
10322 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010323 * Before curdir After
10324 * /foo/bar/file.txt /foo/bar ./file.txt
10325 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10326 * /file.txt / /file.txt
10327 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010328 */
10329 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010330 if (short_name != NULL && short_name > path + 1
10331#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010332 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010333 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010334 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010335 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010336 && !vim_ispathsep(*short_name)
10337#endif
10338 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010339 {
10340 STRCPY(path, ".");
10341 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010342 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010343 }
10344 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010345 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010346 }
10347
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010348 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010349 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010350 {
10351 char_u *rel_path;
10352 char_u *path = in_curdir[i];
10353
10354 if (path == NULL)
10355 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010356
10357 /* If the {filename} is not unique, change it to ./{filename}.
10358 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010359 short_name = shorten_fname(path, curdir);
10360 if (short_name == NULL)
10361 short_name = path;
10362 if (is_unique(short_name, gap, i))
10363 {
10364 STRCPY(fnames[i], short_name);
10365 continue;
10366 }
10367
10368 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10369 if (rel_path == NULL)
10370 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010371 STRCPY(rel_path, ".");
10372 add_pathsep(rel_path);
10373 STRCAT(rel_path, short_name);
10374
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010375 vim_free(fnames[i]);
10376 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010377 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010378 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010379 }
10380
Bram Moolenaar162bd912010-07-28 22:29:10 +020010381theend:
10382 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010383 if (in_curdir != NULL)
10384 {
10385 for (i = 0; i < gap->ga_len; i++)
10386 vim_free(in_curdir[i]);
10387 vim_free(in_curdir);
10388 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010389 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010390 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010391
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010392 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010393 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010394}
10395
10396/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010397 * Calls globpath() with 'path' values for the given pattern and stores the
10398 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010399 * Returns the total number of matches.
10400 */
10401 static int
10402expand_in_path(gap, pattern, flags)
10403 garray_T *gap;
10404 char_u *pattern;
10405 int flags; /* EW_* flags */
10406{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010407 char_u *curdir;
10408 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010409 char_u *files = NULL;
10410 char_u *s; /* start */
10411 char_u *e; /* end */
10412 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010413
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010414 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010415 return 0;
10416 mch_dirname(curdir, MAXPATHL);
10417
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010418 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010419 expand_path_option(curdir, &path_ga);
10420 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010421 if (path_ga.ga_len == 0)
10422 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010423
10424 paths = ga_concat_strings(&path_ga);
10425 ga_clear_strings(&path_ga);
10426 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010427 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010428
Bram Moolenaar94950a92010-12-02 16:01:29 +010010429 files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010430 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010431 if (files == NULL)
10432 return 0;
10433
10434 /* Copy each path in files into gap */
10435 s = e = files;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010436 while (*s != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010437 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010438 while (*e != '\n' && *e != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010439 e++;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010440 if (*e == NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010441 {
10442 addfile(gap, s, flags);
10443 break;
10444 }
10445 else
10446 {
10447 /* *e is '\n' */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010448 *e = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010449 addfile(gap, s, flags);
10450 e++;
10451 s = e;
10452 }
10453 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010454 vim_free(files);
10455
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010456 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010457}
10458#endif
10459
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010460#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10461/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010462 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10463 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010464 */
10465 void
10466remove_duplicates(gap)
10467 garray_T *gap;
10468{
10469 int i;
10470 int j;
10471 char_u **fnames = (char_u **)gap->ga_data;
10472
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010473 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010474 for (i = gap->ga_len - 1; i > 0; --i)
10475 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10476 {
10477 vim_free(fnames[i]);
10478 for (j = i + 1; j < gap->ga_len; ++j)
10479 fnames[j - 1] = fnames[j];
10480 --gap->ga_len;
10481 }
10482}
10483#endif
10484
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010485static int has_env_var __ARGS((char_u *p));
10486
10487/*
10488 * Return TRUE if "p" contains what looks like an environment variable.
10489 * Allowing for escaping.
10490 */
10491 static int
10492has_env_var(p)
10493 char_u *p;
10494{
10495 for ( ; *p; mb_ptr_adv(p))
10496 {
10497 if (*p == '\\' && p[1] != NUL)
10498 ++p;
10499 else if (vim_strchr((char_u *)
10500#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
10501 "$%"
10502#else
10503 "$"
10504#endif
10505 , *p) != NULL)
10506 return TRUE;
10507 }
10508 return FALSE;
10509}
10510
10511#ifdef SPECIAL_WILDCHAR
10512static int has_special_wildchar __ARGS((char_u *p));
10513
10514/*
10515 * Return TRUE if "p" contains a special wildcard character.
10516 * Allowing for escaping.
10517 */
10518 static int
10519has_special_wildchar(p)
10520 char_u *p;
10521{
10522 for ( ; *p; mb_ptr_adv(p))
10523 {
10524 if (*p == '\\' && p[1] != NUL)
10525 ++p;
10526 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10527 return TRUE;
10528 }
10529 return FALSE;
10530}
10531#endif
10532
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533/*
10534 * Generic wildcard expansion code.
10535 *
10536 * Characters in "pat" that should not be expanded must be preceded with a
10537 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10538 *
10539 * Return FAIL when no single file was found. In this case "num_file" is not
10540 * set, and "file" may contain an error message.
10541 * Return OK when some files found. "num_file" is set to the number of
10542 * matches, "file" to the array of matches. Call FreeWild() later.
10543 */
10544 int
10545gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10546 int num_pat; /* number of input patterns */
10547 char_u **pat; /* array of input patterns */
10548 int *num_file; /* resulting number of files */
10549 char_u ***file; /* array of resulting files */
10550 int flags; /* EW_* flags */
10551{
10552 int i;
10553 garray_T ga;
10554 char_u *p;
10555 static int recursive = FALSE;
10556 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010557#if defined(FEAT_SEARCHPATH)
10558 int did_expand_in_path = FALSE;
10559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560
10561 /*
10562 * expand_env() is called to expand things like "~user". If this fails,
10563 * it calls ExpandOne(), which brings us back here. In this case, always
10564 * call the machine specific expansion function, if possible. Otherwise,
10565 * return FAIL.
10566 */
10567 if (recursive)
10568#ifdef SPECIAL_WILDCHAR
10569 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10570#else
10571 return FAIL;
10572#endif
10573
10574#ifdef SPECIAL_WILDCHAR
10575 /*
10576 * If there are any special wildcard characters which we cannot handle
10577 * here, call machine specific function for all the expansion. This
10578 * avoids starting the shell for each argument separately.
10579 * For `=expr` do use the internal function.
10580 */
10581 for (i = 0; i < num_pat; i++)
10582 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010583 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584# ifdef VIM_BACKTICK
10585 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10586# endif
10587 )
10588 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10589 }
10590#endif
10591
10592 recursive = TRUE;
10593
10594 /*
10595 * The matching file names are stored in a growarray. Init it empty.
10596 */
10597 ga_init2(&ga, (int)sizeof(char_u *), 30);
10598
10599 for (i = 0; i < num_pat; ++i)
10600 {
10601 add_pat = -1;
10602 p = pat[i];
10603
10604#ifdef VIM_BACKTICK
10605 if (vim_backtick(p))
10606 add_pat = expand_backtick(&ga, p, flags);
10607 else
10608#endif
10609 {
10610 /*
10611 * First expand environment variables, "~/" and "~user/".
10612 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010613 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010615 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 if (p == NULL)
10617 p = pat[i];
10618#ifdef UNIX
10619 /*
10620 * On Unix, if expand_env() can't expand an environment
10621 * variable, use the shell to do that. Discard previously
10622 * found file names and start all over again.
10623 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010624 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
10626 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010627 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10629 flags);
10630 recursive = FALSE;
10631 return i;
10632 }
10633#endif
10634 }
10635
10636 /*
10637 * If there are wildcards: Expand file names and add each match to
10638 * the list. If there is no match, and EW_NOTFOUND is given, add
10639 * the pattern.
10640 * If there are no wildcards: Add the file name if it exists or
10641 * when EW_NOTFOUND is given.
10642 */
10643 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644 {
10645#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010646 if ((flags & EW_PATH)
10647 && !mch_isFullName(p)
10648 && !(p[0] == '.'
10649 && (vim_ispathsep(p[1])
10650 || (p[1] == '.' && vim_ispathsep(p[2]))))
10651 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010652 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010653 /* :find completion where 'path' is used.
10654 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010655 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010656 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010657 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010658 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010659 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010660 else
10661#endif
10662 add_pat = mch_expandpath(&ga, p, flags);
10663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 }
10665
10666 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10667 {
10668 char_u *t = backslash_halve_save(p);
10669
10670#if defined(MACOS_CLASSIC)
10671 slash_to_colon(t);
10672#endif
10673 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10674 * "vim c:/" work. */
10675 if (flags & EW_NOTFOUND)
10676 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10677 else if (mch_getperm(t) >= 0)
10678 addfile(&ga, t, flags);
10679 vim_free(t);
10680 }
10681
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010682#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010683 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010684 uniquefy_paths(&ga, p);
10685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 if (p != pat[i])
10687 vim_free(p);
10688 }
10689
10690 *num_file = ga.ga_len;
10691 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10692
10693 recursive = FALSE;
10694
10695 return (ga.ga_data != NULL) ? OK : FAIL;
10696}
10697
10698# ifdef VIM_BACKTICK
10699
10700/*
10701 * Return TRUE if we can expand this backtick thing here.
10702 */
10703 static int
10704vim_backtick(p)
10705 char_u *p;
10706{
10707 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10708}
10709
10710/*
10711 * Expand an item in `backticks` by executing it as a command.
10712 * Currently only works when pat[] starts and ends with a `.
10713 * Returns number of file names found.
10714 */
10715 static int
10716expand_backtick(gap, pat, flags)
10717 garray_T *gap;
10718 char_u *pat;
10719 int flags; /* EW_* flags */
10720{
10721 char_u *p;
10722 char_u *cmd;
10723 char_u *buffer;
10724 int cnt = 0;
10725 int i;
10726
10727 /* Create the command: lop off the backticks. */
10728 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10729 if (cmd == NULL)
10730 return 0;
10731
10732#ifdef FEAT_EVAL
10733 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010734 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 else
10736#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010737 buffer = get_cmd_output(cmd, NULL,
10738 (flags & EW_SILENT) ? SHELL_SILENT : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 vim_free(cmd);
10740 if (buffer == NULL)
10741 return 0;
10742
10743 cmd = buffer;
10744 while (*cmd != NUL)
10745 {
10746 cmd = skipwhite(cmd); /* skip over white space */
10747 p = cmd;
10748 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10749 ++p;
10750 /* add an entry if it is not empty */
10751 if (p > cmd)
10752 {
10753 i = *p;
10754 *p = NUL;
10755 addfile(gap, cmd, flags);
10756 *p = i;
10757 ++cnt;
10758 }
10759 cmd = p;
10760 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10761 ++cmd;
10762 }
10763
10764 vim_free(buffer);
10765 return cnt;
10766}
10767# endif /* VIM_BACKTICK */
10768
10769/*
10770 * Add a file to a file list. Accepted flags:
10771 * EW_DIR add directories
10772 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010773 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 * EW_NOTFOUND add even when it doesn't exist
10775 * EW_ADDSLASH add slash after directory name
10776 */
10777 void
10778addfile(gap, f, flags)
10779 garray_T *gap;
10780 char_u *f; /* filename */
10781 int flags;
10782{
10783 char_u *p;
10784 int isdir;
10785
10786 /* if the file/dir doesn't exist, may not add it */
10787 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10788 return;
10789
10790#ifdef FNAME_ILLEGAL
10791 /* if the file/dir contains illegal characters, don't add it */
10792 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10793 return;
10794#endif
10795
10796 isdir = mch_isdir(f);
10797 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10798 return;
10799
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010800 /* If the file isn't executable, may not add it. Do accept directories. */
10801 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f))
10802 return;
10803
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 /* Make room for another item in the file list. */
10805 if (ga_grow(gap, 1) == FAIL)
10806 return;
10807
10808 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10809 if (p == NULL)
10810 return;
10811
10812 STRCPY(p, f);
10813#ifdef BACKSLASH_IN_FILENAME
10814 slash_adjust(p);
10815#endif
10816 /*
10817 * Append a slash or backslash after directory names if none is present.
10818 */
10819#ifndef DONT_ADD_PATHSEP_TO_DIR
10820 if (isdir && (flags & EW_ADDSLASH))
10821 add_pathsep(p);
10822#endif
10823 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824}
10825#endif /* !NO_EXPANDPATH */
10826
10827#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10828
10829#ifndef SEEK_SET
10830# define SEEK_SET 0
10831#endif
10832#ifndef SEEK_END
10833# define SEEK_END 2
10834#endif
10835
10836/*
10837 * Get the stdout of an external command.
10838 * Returns an allocated string, or NULL for error.
10839 */
10840 char_u *
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010841get_cmd_output(cmd, infile, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010843 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 int flags; /* can be SHELL_SILENT */
10845{
10846 char_u *tempname;
10847 char_u *command;
10848 char_u *buffer = NULL;
10849 int len;
10850 int i = 0;
10851 FILE *fd;
10852
10853 if (check_restricted() || check_secure())
10854 return NULL;
10855
10856 /* get a name for the temp file */
10857 if ((tempname = vim_tempname('o')) == NULL)
10858 {
10859 EMSG(_(e_notmp));
10860 return NULL;
10861 }
10862
10863 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010864 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 if (command == NULL)
10866 goto done;
10867
10868 /*
10869 * Call the shell to execute the command (errors are ignored).
10870 * Don't check timestamps here.
10871 */
10872 ++no_check_timestamps;
10873 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10874 --no_check_timestamps;
10875
10876 vim_free(command);
10877
10878 /*
10879 * read the names from the file into memory
10880 */
10881# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010882 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 fd = mch_fopen((char *)tempname, "r");
10884# else
10885 fd = mch_fopen((char *)tempname, READBIN);
10886# endif
10887
10888 if (fd == NULL)
10889 {
10890 EMSG2(_(e_notopen), tempname);
10891 goto done;
10892 }
10893
10894 fseek(fd, 0L, SEEK_END);
10895 len = ftell(fd); /* get size of temp file */
10896 fseek(fd, 0L, SEEK_SET);
10897
10898 buffer = alloc(len + 1);
10899 if (buffer != NULL)
10900 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10901 fclose(fd);
10902 mch_remove(tempname);
10903 if (buffer == NULL)
10904 goto done;
10905#ifdef VMS
10906 len = i; /* VMS doesn't give us what we asked for... */
10907#endif
10908 if (i != len)
10909 {
10910 EMSG2(_(e_notread), tempname);
10911 vim_free(buffer);
10912 buffer = NULL;
10913 }
10914 else
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010915 {
10916 /* Change NUL into SOH, otherwise the string is truncated. */
10917 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020010918 if (buffer[i] == NUL)
10919 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010920
Bram Moolenaar162bd912010-07-28 22:29:10 +020010921 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923
10924done:
10925 vim_free(tempname);
10926 return buffer;
10927}
10928#endif
10929
10930/*
10931 * Free the list of files returned by expand_wildcards() or other expansion
10932 * functions.
10933 */
10934 void
10935FreeWild(count, files)
10936 int count;
10937 char_u **files;
10938{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010939 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 return;
10941#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10942 /*
10943 * Is this still OK for when other functions than expand_wildcards() have
10944 * been used???
10945 */
10946 _fnexplodefree((char **)files);
10947#else
10948 while (count--)
10949 vim_free(files[count]);
10950 vim_free(files);
10951#endif
10952}
10953
10954/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010955 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 * Don't do this when still processing a command or a mapping.
10957 * Don't do this when inside a ":normal" command.
10958 */
10959 int
10960goto_im()
10961{
10962 return (p_im && stuff_empty() && typebuf_typed());
10963}