blob: a9e6c8a595f49e3783c4bfd9ca9fa946ded55c18 [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);
306 /* Correct saved cursor position if it's after the indent. */
307 if (saved_cursor.lnum == curwin->w_cursor.lnum
308 && saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000309 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
Bram Moolenaar5409c052005-03-18 20:27:04 +0000310 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 }
312 else
313 vim_free(newline);
314
315 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000316 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317}
318
319/*
320 * Copy the indent from ptr to the current line (and fill to size)
321 * Leaves the cursor on the first non-blank in the line.
322 * Returns TRUE if the line was changed.
323 */
324 static int
325copy_indent(size, src)
326 int size;
327 char_u *src;
328{
329 char_u *p = NULL;
330 char_u *line = NULL;
331 char_u *s;
332 int todo;
333 int ind_len;
334 int line_len = 0;
335 int tab_pad;
336 int ind_done;
337 int round;
338
339 /* Round 1: compute the number of characters needed for the indent
340 * Round 2: copy the characters. */
341 for (round = 1; round <= 2; ++round)
342 {
343 todo = size;
344 ind_len = 0;
345 ind_done = 0;
346 s = src;
347
348 /* Count/copy the usable portion of the source line */
349 while (todo > 0 && vim_iswhite(*s))
350 {
351 if (*s == TAB)
352 {
353 tab_pad = (int)curbuf->b_p_ts
354 - (ind_done % (int)curbuf->b_p_ts);
355 /* Stop if this tab will overshoot the target */
356 if (todo < tab_pad)
357 break;
358 todo -= tab_pad;
359 ind_done += tab_pad;
360 }
361 else
362 {
363 --todo;
364 ++ind_done;
365 }
366 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000367 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 *p++ = *s;
369 ++s;
370 }
371
372 /* Fill to next tabstop with a tab, if possible */
373 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200374 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {
376 todo -= tab_pad;
377 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000378 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 *p++ = TAB;
380 }
381
382 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200383 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 {
385 todo -= (int)curbuf->b_p_ts;
386 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000387 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 *p++ = TAB;
389 }
390
391 /* Count/add spaces required for indent */
392 while (todo > 0)
393 {
394 --todo;
395 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000396 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 *p++ = ' ';
398 }
399
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000400 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {
402 /* Allocate memory for the result: the copied indent, new indent
403 * and the rest of the line. */
404 line_len = (int)STRLEN(ml_get_curline()) + 1;
405 line = alloc(ind_len + line_len);
406 if (line == NULL)
407 return FALSE;
408 p = line;
409 }
410 }
411
412 /* Append the original line */
413 mch_memmove(p, ml_get_curline(), (size_t)line_len);
414
415 /* Replace the line */
416 ml_replace(curwin->w_cursor.lnum, line, FALSE);
417
418 /* Put the cursor after the indent. */
419 curwin->w_cursor.col = ind_len;
420 return TRUE;
421}
422
423/*
424 * Return the indent of the current line after a number. Return -1 if no
425 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000426 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 */
428 int
429get_number_indent(lnum)
430 linenr_T lnum;
431{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 colnr_T col;
433 pos_T pos;
434
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200435 regmatch_T regmatch;
436 int lead_len = 0; /* length of comment leader */
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 if (lnum > curbuf->b_ml.ml_line_count)
439 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000440 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200441
442#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200443 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
444 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200445 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000446#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200447 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
448 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200449 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200450 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200451
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200452 /* vim_regexec() expects a pointer to a line. This lets us
453 * start matching for the flp beyond any comment leader... */
454 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200456 pos.lnum = lnum;
457 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200458#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200459 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200460#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200462 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200463 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000464
465 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 getvcol(curwin, &pos, &col, NULL, NULL);
468 return (int)col;
469}
470
471#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
472
473static int cin_is_cinword __ARGS((char_u *line));
474
475/*
476 * Return TRUE if the string "line" starts with a word from 'cinwords'.
477 */
478 static int
479cin_is_cinword(line)
480 char_u *line;
481{
482 char_u *cinw;
483 char_u *cinw_buf;
484 int cinw_len;
485 int retval = FALSE;
486 int len;
487
488 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
489 cinw_buf = alloc((unsigned)cinw_len);
490 if (cinw_buf != NULL)
491 {
492 line = skipwhite(line);
493 for (cinw = curbuf->b_p_cinw; *cinw; )
494 {
495 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
496 if (STRNCMP(line, cinw_buf, len) == 0
497 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
498 {
499 retval = TRUE;
500 break;
501 }
502 }
503 vim_free(cinw_buf);
504 }
505 return retval;
506}
507#endif
508
509/*
510 * open_line: Add a new line below or above the current line.
511 *
512 * For VREPLACE mode, we only add a new line when we get to the end of the
513 * file, otherwise we just start replacing the next line.
514 *
515 * Caller must take care of undo. Since VREPLACE may affect any number of
516 * lines however, it may call u_save_cursor() again when starting to change a
517 * new line.
518 * "flags": OPENLINE_DELSPACES delete spaces after cursor
519 * OPENLINE_DO_COM format comments
520 * OPENLINE_KEEPTRAIL keep trailing spaces
521 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200522 * OPENLINE_COM_LIST format comments with list or 2nd line indent
523 *
524 * "second_line_indent": indent for after ^^D in Insert mode or if flag
525 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 *
527 * Return TRUE for success, FALSE for failure
528 */
529 int
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200530open_line(dir, flags, second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 int dir; /* FORWARD or BACKWARD */
532 int flags;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200533 int second_line_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535 char_u *saved_line; /* copy of the original line */
536 char_u *next_line = NULL; /* copy of the next line */
537 char_u *p_extra = NULL; /* what goes to next line */
538 int less_cols = 0; /* less columns for mark in new line */
539 int less_cols_off = 0; /* columns to skip for mark adjust */
540 pos_T old_cursor; /* old cursor position */
541 int newcol = 0; /* new cursor column */
542 int newindent = 0; /* auto-indent of the new line */
543 int n;
544 int trunc_line = FALSE; /* truncate current line afterwards */
545 int retval = FALSE; /* return value, default is FAIL */
546#ifdef FEAT_COMMENTS
547 int extra_len = 0; /* length of p_extra string */
548 int lead_len; /* length of comment leader */
549 char_u *lead_flags; /* position in 'comments' for comment leader */
550 char_u *leader = NULL; /* copy of comment leader */
551#endif
552 char_u *allocated = NULL; /* allocated memory */
553#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
554 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
555 char_u *p;
556#endif
557 int saved_char = NUL; /* init for GCC */
558#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
559 pos_T *pos;
560#endif
561#ifdef FEAT_SMARTINDENT
562 int do_si = (!p_paste && curbuf->b_p_si
563# ifdef FEAT_CINDENT
564 && !curbuf->b_p_cin
565# endif
566 );
567 int no_si = FALSE; /* reset did_si afterwards */
568 int first_char = NUL; /* init for GCC */
569#endif
570#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
571 int vreplace_mode;
572#endif
573 int did_append; /* appended a new line */
574 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
575
576 /*
577 * make a copy of the current line so we can mess with it
578 */
579 saved_line = vim_strsave(ml_get_curline());
580 if (saved_line == NULL) /* out of memory! */
581 return FALSE;
582
583#ifdef FEAT_VREPLACE
584 if (State & VREPLACE_FLAG)
585 {
586 /*
587 * With VREPLACE we make a copy of the next line, which we will be
588 * starting to replace. First make the new line empty and let vim play
589 * with the indenting and comment leader to its heart's content. Then
590 * we grab what it ended up putting on the new line, put back the
591 * original line, and call ins_char() to put each new character onto
592 * the line, replacing what was there before and pushing the right
593 * stuff onto the replace stack. -- webb.
594 */
595 if (curwin->w_cursor.lnum < orig_line_count)
596 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
597 else
598 next_line = vim_strsave((char_u *)"");
599 if (next_line == NULL) /* out of memory! */
600 goto theend;
601
602 /*
603 * In VREPLACE mode, a NL replaces the rest of the line, and starts
604 * replacing the next line, so push all of the characters left on the
605 * line onto the replace stack. We'll push any other characters that
606 * might be replaced at the start of the next line (due to autoindent
607 * etc) a bit later.
608 */
609 replace_push(NUL); /* Call twice because BS over NL expects it */
610 replace_push(NUL);
611 p = saved_line + curwin->w_cursor.col;
612 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000613 {
614#ifdef FEAT_MBYTE
615 if (has_mbyte)
616 p += replace_push_mb(p);
617 else
618#endif
619 replace_push(*p++);
620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 saved_line[curwin->w_cursor.col] = NUL;
622 }
623#endif
624
625 if ((State & INSERT)
626#ifdef FEAT_VREPLACE
627 && !(State & VREPLACE_FLAG)
628#endif
629 )
630 {
631 p_extra = saved_line + curwin->w_cursor.col;
632#ifdef FEAT_SMARTINDENT
633 if (do_si) /* need first char after new line break */
634 {
635 p = skipwhite(p_extra);
636 first_char = *p;
637 }
638#endif
639#ifdef FEAT_COMMENTS
640 extra_len = (int)STRLEN(p_extra);
641#endif
642 saved_char = *p_extra;
643 *p_extra = NUL;
644 }
645
646 u_clearline(); /* cannot do "U" command when adding lines */
647#ifdef FEAT_SMARTINDENT
648 did_si = FALSE;
649#endif
650 ai_col = 0;
651
652 /*
653 * If we just did an auto-indent, then we didn't type anything on
654 * the prior line, and it should be truncated. Do this even if 'ai' is not
655 * set because automatically inserting a comment leader also sets did_ai.
656 */
657 if (dir == FORWARD && did_ai)
658 trunc_line = TRUE;
659
660 /*
661 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
662 * indent to use for the new line.
663 */
664 if (curbuf->b_p_ai
665#ifdef FEAT_SMARTINDENT
666 || do_si
667#endif
668 )
669 {
670 /*
671 * count white space on current line
672 */
673 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200674 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
675 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
677#ifdef FEAT_SMARTINDENT
678 /*
679 * Do smart indenting.
680 * In insert/replace mode (only when dir == FORWARD)
681 * we may move some text to the next line. If it starts with '{'
682 * don't add an indent. Fixes inserting a NL before '{' in line
683 * "if (condition) {"
684 */
685 if (!trunc_line && do_si && *saved_line != NUL
686 && (p_extra == NULL || first_char != '{'))
687 {
688 char_u *ptr;
689 char_u last_char;
690
691 old_cursor = curwin->w_cursor;
692 ptr = saved_line;
693# ifdef FEAT_COMMENTS
694 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200695 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 else
697 lead_len = 0;
698# endif
699 if (dir == FORWARD)
700 {
701 /*
702 * Skip preprocessor directives, unless they are
703 * recognised as comments.
704 */
705 if (
706# ifdef FEAT_COMMENTS
707 lead_len == 0 &&
708# endif
709 ptr[0] == '#')
710 {
711 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
712 ptr = ml_get(--curwin->w_cursor.lnum);
713 newindent = get_indent();
714 }
715# ifdef FEAT_COMMENTS
716 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200717 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 else
719 lead_len = 0;
720 if (lead_len > 0)
721 {
722 /*
723 * This case gets the following right:
724 * \*
725 * * A comment (read '\' as '/').
726 * *\
727 * #define IN_THE_WAY
728 * This should line up here;
729 */
730 p = skipwhite(ptr);
731 if (p[0] == '/' && p[1] == '*')
732 p++;
733 if (p[0] == '*')
734 {
735 for (p++; *p; p++)
736 {
737 if (p[0] == '/' && p[-1] == '*')
738 {
739 /*
740 * End of C comment, indent should line up
741 * with the line containing the start of
742 * the comment
743 */
744 curwin->w_cursor.col = (colnr_T)(p - ptr);
745 if ((pos = findmatch(NULL, NUL)) != NULL)
746 {
747 curwin->w_cursor.lnum = pos->lnum;
748 newindent = get_indent();
749 }
750 }
751 }
752 }
753 }
754 else /* Not a comment line */
755# endif
756 {
757 /* Find last non-blank in line */
758 p = ptr + STRLEN(ptr) - 1;
759 while (p > ptr && vim_iswhite(*p))
760 --p;
761 last_char = *p;
762
763 /*
764 * find the character just before the '{' or ';'
765 */
766 if (last_char == '{' || last_char == ';')
767 {
768 if (p > ptr)
769 --p;
770 while (p > ptr && vim_iswhite(*p))
771 --p;
772 }
773 /*
774 * Try to catch lines that are split over multiple
775 * lines. eg:
776 * if (condition &&
777 * condition) {
778 * Should line up here!
779 * }
780 */
781 if (*p == ')')
782 {
783 curwin->w_cursor.col = (colnr_T)(p - ptr);
784 if ((pos = findmatch(NULL, '(')) != NULL)
785 {
786 curwin->w_cursor.lnum = pos->lnum;
787 newindent = get_indent();
788 ptr = ml_get_curline();
789 }
790 }
791 /*
792 * If last character is '{' do indent, without
793 * checking for "if" and the like.
794 */
795 if (last_char == '{')
796 {
797 did_si = TRUE; /* do indent */
798 no_si = TRUE; /* don't delete it when '{' typed */
799 }
800 /*
801 * Look for "if" and the like, use 'cinwords'.
802 * Don't do this if the previous line ended in ';' or
803 * '}'.
804 */
805 else if (last_char != ';' && last_char != '}'
806 && cin_is_cinword(ptr))
807 did_si = TRUE;
808 }
809 }
810 else /* dir == BACKWARD */
811 {
812 /*
813 * Skip preprocessor directives, unless they are
814 * recognised as comments.
815 */
816 if (
817# ifdef FEAT_COMMENTS
818 lead_len == 0 &&
819# endif
820 ptr[0] == '#')
821 {
822 int was_backslashed = FALSE;
823
824 while ((ptr[0] == '#' || was_backslashed) &&
825 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
826 {
827 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
828 was_backslashed = TRUE;
829 else
830 was_backslashed = FALSE;
831 ptr = ml_get(++curwin->w_cursor.lnum);
832 }
833 if (was_backslashed)
834 newindent = 0; /* Got to end of file */
835 else
836 newindent = get_indent();
837 }
838 p = skipwhite(ptr);
839 if (*p == '}') /* if line starts with '}': do indent */
840 did_si = TRUE;
841 else /* can delete indent when '{' typed */
842 can_si_back = TRUE;
843 }
844 curwin->w_cursor = old_cursor;
845 }
846 if (do_si)
847 can_si = TRUE;
848#endif /* FEAT_SMARTINDENT */
849
850 did_ai = TRUE;
851 }
852
853#ifdef FEAT_COMMENTS
854 /*
855 * Find out if the current line starts with a comment leader.
856 * This may then be inserted in front of the new line.
857 */
858 end_comment_pending = NUL;
859 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200860 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 else
862 lead_len = 0;
863 if (lead_len > 0)
864 {
865 char_u *lead_repl = NULL; /* replaces comment leader */
866 int lead_repl_len = 0; /* length of *lead_repl */
867 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
868 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
869 char_u *comment_end = NULL; /* where lead_end has been found */
870 int extra_space = FALSE; /* append extra space */
871 int current_flag;
872 int require_blank = FALSE; /* requires blank after middle */
873 char_u *p2;
874
875 /*
876 * If the comment leader has the start, middle or end flag, it may not
877 * be used or may be replaced with the middle leader.
878 */
879 for (p = lead_flags; *p && *p != ':'; ++p)
880 {
881 if (*p == COM_BLANK)
882 {
883 require_blank = TRUE;
884 continue;
885 }
886 if (*p == COM_START || *p == COM_MIDDLE)
887 {
888 current_flag = *p;
889 if (*p == COM_START)
890 {
891 /*
892 * Doing "O" on a start of comment does not insert leader.
893 */
894 if (dir == BACKWARD)
895 {
896 lead_len = 0;
897 break;
898 }
899
900 /* find start of middle part */
901 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
902 require_blank = FALSE;
903 }
904
905 /*
906 * Isolate the strings of the middle and end leader.
907 */
908 while (*p && p[-1] != ':') /* find end of middle flags */
909 {
910 if (*p == COM_BLANK)
911 require_blank = TRUE;
912 ++p;
913 }
914 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
915
916 while (*p && p[-1] != ':') /* find end of end flags */
917 {
918 /* Check whether we allow automatic ending of comments */
919 if (*p == COM_AUTO_END)
920 end_comment_pending = -1; /* means we want to set it */
921 ++p;
922 }
923 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
924
925 if (end_comment_pending == -1) /* we can set it now */
926 end_comment_pending = lead_end[n - 1];
927
928 /*
929 * If the end of the comment is in the same line, don't use
930 * the comment leader.
931 */
932 if (dir == FORWARD)
933 {
934 for (p = saved_line + lead_len; *p; ++p)
935 if (STRNCMP(p, lead_end, n) == 0)
936 {
937 comment_end = p;
938 lead_len = 0;
939 break;
940 }
941 }
942
943 /*
944 * Doing "o" on a start of comment inserts the middle leader.
945 */
946 if (lead_len > 0)
947 {
948 if (current_flag == COM_START)
949 {
950 lead_repl = lead_middle;
951 lead_repl_len = (int)STRLEN(lead_middle);
952 }
953
954 /*
955 * If we have hit RETURN immediately after the start
956 * comment leader, then put a space after the middle
957 * comment leader on the next line.
958 */
959 if (!vim_iswhite(saved_line[lead_len - 1])
960 && ((p_extra != NULL
961 && (int)curwin->w_cursor.col == lead_len)
962 || (p_extra == NULL
963 && saved_line[lead_len] == NUL)
964 || require_blank))
965 extra_space = TRUE;
966 }
967 break;
968 }
969 if (*p == COM_END)
970 {
971 /*
972 * Doing "o" on the end of a comment does not insert leader.
973 * Remember where the end is, might want to use it to find the
974 * start (for C-comments).
975 */
976 if (dir == FORWARD)
977 {
978 comment_end = skipwhite(saved_line);
979 lead_len = 0;
980 break;
981 }
982
983 /*
984 * Doing "O" on the end of a comment inserts the middle leader.
985 * Find the string for the middle leader, searching backwards.
986 */
987 while (p > curbuf->b_p_com && *p != ',')
988 --p;
989 for (lead_repl = p; lead_repl > curbuf->b_p_com
990 && lead_repl[-1] != ':'; --lead_repl)
991 ;
992 lead_repl_len = (int)(p - lead_repl);
993
994 /* We can probably always add an extra space when doing "O" on
995 * the comment-end */
996 extra_space = TRUE;
997
998 /* Check whether we allow automatic ending of comments */
999 for (p2 = p; *p2 && *p2 != ':'; p2++)
1000 {
1001 if (*p2 == COM_AUTO_END)
1002 end_comment_pending = -1; /* means we want to set it */
1003 }
1004 if (end_comment_pending == -1)
1005 {
1006 /* Find last character in end-comment string */
1007 while (*p2 && *p2 != ',')
1008 p2++;
1009 end_comment_pending = p2[-1];
1010 }
1011 break;
1012 }
1013 if (*p == COM_FIRST)
1014 {
1015 /*
1016 * Comment leader for first line only: Don't repeat leader
1017 * when using "O", blank out leader when using "o".
1018 */
1019 if (dir == BACKWARD)
1020 lead_len = 0;
1021 else
1022 {
1023 lead_repl = (char_u *)"";
1024 lead_repl_len = 0;
1025 }
1026 break;
1027 }
1028 }
1029 if (lead_len)
1030 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001031 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001032 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001033 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 allocated = leader; /* remember to free it later */
1035
1036 if (leader == NULL)
1037 lead_len = 0;
1038 else
1039 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001040 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
1042 /*
1043 * Replace leader with lead_repl, right or left adjusted
1044 */
1045 if (lead_repl != NULL)
1046 {
1047 int c = 0;
1048 int off = 0;
1049
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001050 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
1052 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001053 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 else if (VIM_ISDIGIT(*p) || *p == '-')
1055 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001056 else
1057 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 if (c == COM_RIGHT) /* right adjusted leader */
1060 {
1061 /* find last non-white in the leader to line up with */
1062 for (p = leader + lead_len - 1; p > leader
1063 && vim_iswhite(*p); --p)
1064 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001066
1067#ifdef FEAT_MBYTE
1068 /* Compute the length of the replaced characters in
1069 * screen characters, not bytes. */
1070 {
1071 int repl_size = vim_strnsize(lead_repl,
1072 lead_repl_len);
1073 int old_size = 0;
1074 char_u *endp = p;
1075 int l;
1076
1077 while (old_size < repl_size && p > leader)
1078 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001079 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001080 old_size += ptr2cells(p);
1081 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001082 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001083 if (l != 0)
1084 mch_memmove(endp + l, endp,
1085 (size_t)((leader + lead_len) - endp));
1086 lead_len += l;
1087 }
1088#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (p < leader + lead_repl_len)
1090 p = leader;
1091 else
1092 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1095 if (p + lead_repl_len > leader + lead_len)
1096 p[lead_repl_len] = NUL;
1097
1098 /* blank-out any other chars from the old leader. */
1099 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001100 {
1101#ifdef FEAT_MBYTE
1102 int l = mb_head_off(leader, p);
1103
1104 if (l > 1)
1105 {
1106 p -= l;
1107 if (ptr2cells(p) > 1)
1108 {
1109 p[1] = ' ';
1110 --l;
1111 }
1112 mch_memmove(p + 1, p + l + 1,
1113 (size_t)((leader + lead_len) - (p + l + 1)));
1114 lead_len -= l;
1115 *p = ' ';
1116 }
1117 else
1118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (!vim_iswhite(*p))
1120 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 else /* left adjusted leader */
1124 {
1125 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001126#ifdef FEAT_MBYTE
1127 /* Compute the length of the replaced characters in
1128 * screen characters, not bytes. Move the part that is
1129 * not to be overwritten. */
1130 {
1131 int repl_size = vim_strnsize(lead_repl,
1132 lead_repl_len);
1133 int i;
1134 int l;
1135
1136 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1137 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001138 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001139 if (vim_strnsize(p, i + l) > repl_size)
1140 break;
1141 }
1142 if (i != lead_repl_len)
1143 {
1144 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001145 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001146 lead_len += lead_repl_len - i;
1147 }
1148 }
1149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1151
1152 /* Replace any remaining non-white chars in the old
1153 * leader by spaces. Keep Tabs, the indent must
1154 * remain the same. */
1155 for (p += lead_repl_len; p < leader + lead_len; ++p)
1156 if (!vim_iswhite(*p))
1157 {
1158 /* Don't put a space before a TAB. */
1159 if (p + 1 < leader + lead_len && p[1] == TAB)
1160 {
1161 --lead_len;
1162 mch_memmove(p, p + 1,
1163 (leader + lead_len) - p);
1164 }
1165 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001166 {
1167#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001168 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001169
1170 if (l > 1)
1171 {
1172 if (ptr2cells(p) > 1)
1173 {
1174 /* Replace a double-wide char with
1175 * two spaces */
1176 --l;
1177 *p++ = ' ';
1178 }
1179 mch_memmove(p + 1, p + l,
1180 (leader + lead_len) - p);
1181 lead_len -= l - 1;
1182 }
1183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 *p = NUL;
1188 }
1189
1190 /* Recompute the indent, it may have changed. */
1191 if (curbuf->b_p_ai
1192#ifdef FEAT_SMARTINDENT
1193 || do_si
1194#endif
1195 )
1196 newindent = get_indent_str(leader, (int)curbuf->b_p_ts);
1197
1198 /* Add the indent offset */
1199 if (newindent + off < 0)
1200 {
1201 off = -newindent;
1202 newindent = 0;
1203 }
1204 else
1205 newindent += off;
1206
1207 /* Correct trailing spaces for the shift, so that
1208 * alignment remains equal. */
1209 while (off > 0 && lead_len > 0
1210 && leader[lead_len - 1] == ' ')
1211 {
1212 /* Don't do it when there is a tab before the space */
1213 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1214 break;
1215 --lead_len;
1216 --off;
1217 }
1218
1219 /* If the leader ends in white space, don't add an
1220 * extra space */
1221 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1222 extra_space = FALSE;
1223 leader[lead_len] = NUL;
1224 }
1225
1226 if (extra_space)
1227 {
1228 leader[lead_len++] = ' ';
1229 leader[lead_len] = NUL;
1230 }
1231
1232 newcol = lead_len;
1233
1234 /*
1235 * if a new indent will be set below, remove the indent that
1236 * is in the comment leader
1237 */
1238 if (newindent
1239#ifdef FEAT_SMARTINDENT
1240 || did_si
1241#endif
1242 )
1243 {
1244 while (lead_len && vim_iswhite(*leader))
1245 {
1246 --lead_len;
1247 --newcol;
1248 ++leader;
1249 }
1250 }
1251
1252 }
1253#ifdef FEAT_SMARTINDENT
1254 did_si = can_si = FALSE;
1255#endif
1256 }
1257 else if (comment_end != NULL)
1258 {
1259 /*
1260 * We have finished a comment, so we don't use the leader.
1261 * If this was a C-comment and 'ai' or 'si' is set do a normal
1262 * indent to align with the line containing the start of the
1263 * comment.
1264 */
1265 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1266 (curbuf->b_p_ai
1267#ifdef FEAT_SMARTINDENT
1268 || do_si
1269#endif
1270 ))
1271 {
1272 old_cursor = curwin->w_cursor;
1273 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1274 if ((pos = findmatch(NULL, NUL)) != NULL)
1275 {
1276 curwin->w_cursor.lnum = pos->lnum;
1277 newindent = get_indent();
1278 }
1279 curwin->w_cursor = old_cursor;
1280 }
1281 }
1282 }
1283#endif
1284
1285 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1286 if (p_extra != NULL)
1287 {
1288 *p_extra = saved_char; /* restore char that NUL replaced */
1289
1290 /*
1291 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1292 * non-blank.
1293 *
1294 * When in REPLACE mode, put the deleted blanks on the replace stack,
1295 * preceded by a NUL, so they can be put back when a BS is entered.
1296 */
1297 if (REPLACE_NORMAL(State))
1298 replace_push(NUL); /* end of extra blanks */
1299 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1300 {
1301 while ((*p_extra == ' ' || *p_extra == '\t')
1302#ifdef FEAT_MBYTE
1303 && (!enc_utf8
1304 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1305#endif
1306 )
1307 {
1308 if (REPLACE_NORMAL(State))
1309 replace_push(*p_extra);
1310 ++p_extra;
1311 ++less_cols_off;
1312 }
1313 }
1314 if (*p_extra != NUL)
1315 did_ai = FALSE; /* append some text, don't truncate now */
1316
1317 /* columns for marks adjusted for removed columns */
1318 less_cols = (int)(p_extra - saved_line);
1319 }
1320
1321 if (p_extra == NULL)
1322 p_extra = (char_u *)""; /* append empty line */
1323
1324#ifdef FEAT_COMMENTS
1325 /* concatenate leader and p_extra, if there is a leader */
1326 if (lead_len)
1327 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001328 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1329 {
1330 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001331 int padding = second_line_indent
1332 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001333
1334 /* Here whitespace is inserted after the comment char.
1335 * Below, set_indent(newindent, SIN_INSERT) will insert the
1336 * whitespace needed before the comment char. */
1337 for (i = 0; i < padding; i++)
1338 {
1339 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001340 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001341 newcol++;
1342 }
1343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 STRCAT(leader, p_extra);
1345 p_extra = leader;
1346 did_ai = TRUE; /* So truncating blanks works with comments */
1347 less_cols -= lead_len;
1348 }
1349 else
1350 end_comment_pending = NUL; /* turns out there was no leader */
1351#endif
1352
1353 old_cursor = curwin->w_cursor;
1354 if (dir == BACKWARD)
1355 --curwin->w_cursor.lnum;
1356#ifdef FEAT_VREPLACE
1357 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1358#endif
1359 {
1360 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1361 == FAIL)
1362 goto theend;
1363 /* Postpone calling changed_lines(), because it would mess up folding
1364 * with markers. */
1365 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1366 did_append = TRUE;
1367 }
1368#ifdef FEAT_VREPLACE
1369 else
1370 {
1371 /*
1372 * In VREPLACE mode we are starting to replace the next line.
1373 */
1374 curwin->w_cursor.lnum++;
1375 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1376 {
1377 /* In case we NL to a new line, BS to the previous one, and NL
1378 * again, we don't want to save the new line for undo twice.
1379 */
1380 (void)u_save_cursor(); /* errors are ignored! */
1381 vr_lines_changed++;
1382 }
1383 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1384 changed_bytes(curwin->w_cursor.lnum, 0);
1385 curwin->w_cursor.lnum--;
1386 did_append = FALSE;
1387 }
1388#endif
1389
1390 if (newindent
1391#ifdef FEAT_SMARTINDENT
1392 || did_si
1393#endif
1394 )
1395 {
1396 ++curwin->w_cursor.lnum;
1397#ifdef FEAT_SMARTINDENT
1398 if (did_si)
1399 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02001400 int sw = (int)get_sw_value();
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001403 newindent -= newindent % sw;
1404 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
1406#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001407 /* Copy the indent */
1408 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 {
1410 (void)copy_indent(newindent, saved_line);
1411
1412 /*
1413 * Set the 'preserveindent' option so that any further screwing
1414 * with the line doesn't entirely destroy our efforts to preserve
1415 * it. It gets restored at the function end.
1416 */
1417 curbuf->b_p_pi = TRUE;
1418 }
1419 else
1420 (void)set_indent(newindent, SIN_INSERT);
1421 less_cols -= curwin->w_cursor.col;
1422
1423 ai_col = curwin->w_cursor.col;
1424
1425 /*
1426 * In REPLACE mode, for each character in the new indent, there must
1427 * be a NUL on the replace stack, for when it is deleted with BS
1428 */
1429 if (REPLACE_NORMAL(State))
1430 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1431 replace_push(NUL);
1432 newcol += curwin->w_cursor.col;
1433#ifdef FEAT_SMARTINDENT
1434 if (no_si)
1435 did_si = FALSE;
1436#endif
1437 }
1438
1439#ifdef FEAT_COMMENTS
1440 /*
1441 * In REPLACE mode, for each character in the extra leader, there must be
1442 * a NUL on the replace stack, for when it is deleted with BS.
1443 */
1444 if (REPLACE_NORMAL(State))
1445 while (lead_len-- > 0)
1446 replace_push(NUL);
1447#endif
1448
1449 curwin->w_cursor = old_cursor;
1450
1451 if (dir == FORWARD)
1452 {
1453 if (trunc_line || (State & INSERT))
1454 {
1455 /* truncate current line at cursor */
1456 saved_line[curwin->w_cursor.col] = NUL;
1457 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1458 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1459 truncate_spaces(saved_line);
1460 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1461 saved_line = NULL;
1462 if (did_append)
1463 {
1464 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1465 curwin->w_cursor.lnum + 1, 1L);
1466 did_append = FALSE;
1467
1468 /* Move marks after the line break to the new line. */
1469 if (flags & OPENLINE_MARKFIX)
1470 mark_col_adjust(curwin->w_cursor.lnum,
1471 curwin->w_cursor.col + less_cols_off,
1472 1L, (long)-less_cols);
1473 }
1474 else
1475 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1476 }
1477
1478 /*
1479 * Put the cursor on the new line. Careful: the scrollup() above may
1480 * have moved w_cursor, we must use old_cursor.
1481 */
1482 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1483 }
1484 if (did_append)
1485 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1486
1487 curwin->w_cursor.col = newcol;
1488#ifdef FEAT_VIRTUALEDIT
1489 curwin->w_cursor.coladd = 0;
1490#endif
1491
1492#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1493 /*
1494 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1495 * fixthisline() from doing it (via change_indent()) by telling it we're in
1496 * normal INSERT mode.
1497 */
1498 if (State & VREPLACE_FLAG)
1499 {
1500 vreplace_mode = State; /* So we know to put things right later */
1501 State = INSERT;
1502 }
1503 else
1504 vreplace_mode = 0;
1505#endif
1506#ifdef FEAT_LISP
1507 /*
1508 * May do lisp indenting.
1509 */
1510 if (!p_paste
1511# ifdef FEAT_COMMENTS
1512 && leader == NULL
1513# endif
1514 && curbuf->b_p_lisp
1515 && curbuf->b_p_ai)
1516 {
1517 fixthisline(get_lisp_indent);
1518 p = ml_get_curline();
1519 ai_col = (colnr_T)(skipwhite(p) - p);
1520 }
1521#endif
1522#ifdef FEAT_CINDENT
1523 /*
1524 * May do indenting after opening a new line.
1525 */
1526 if (!p_paste
1527 && (curbuf->b_p_cin
1528# ifdef FEAT_EVAL
1529 || *curbuf->b_p_inde != NUL
1530# endif
1531 )
1532 && in_cinkeys(dir == FORWARD
1533 ? KEY_OPEN_FORW
1534 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1535 {
1536 do_c_expr_indent();
1537 p = ml_get_curline();
1538 ai_col = (colnr_T)(skipwhite(p) - p);
1539 }
1540#endif
1541#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1542 if (vreplace_mode != 0)
1543 State = vreplace_mode;
1544#endif
1545
1546#ifdef FEAT_VREPLACE
1547 /*
1548 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1549 * original line, and inserts the new stuff char by char, pushing old stuff
1550 * onto the replace stack (via ins_char()).
1551 */
1552 if (State & VREPLACE_FLAG)
1553 {
1554 /* Put new line in p_extra */
1555 p_extra = vim_strsave(ml_get_curline());
1556 if (p_extra == NULL)
1557 goto theend;
1558
1559 /* Put back original line */
1560 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1561
1562 /* Insert new stuff into line again */
1563 curwin->w_cursor.col = 0;
1564#ifdef FEAT_VIRTUALEDIT
1565 curwin->w_cursor.coladd = 0;
1566#endif
1567 ins_bytes(p_extra); /* will call changed_bytes() */
1568 vim_free(p_extra);
1569 next_line = NULL;
1570 }
1571#endif
1572
1573 retval = TRUE; /* success! */
1574theend:
1575 curbuf->b_p_pi = saved_pi;
1576 vim_free(saved_line);
1577 vim_free(next_line);
1578 vim_free(allocated);
1579 return retval;
1580}
1581
1582#if defined(FEAT_COMMENTS) || defined(PROTO)
1583/*
1584 * get_leader_len() returns the length of the prefix of the given string
1585 * which introduces a comment. If this string is not a comment then 0 is
1586 * returned.
1587 * When "flags" is not NULL, it is set to point to the flags of the recognized
1588 * comment leader.
1589 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001590 * If "include_space" is set, include trailing whitespace while calculating the
1591 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 */
1593 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001594get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 char_u *line;
1596 char_u **flags;
1597 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001598 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
1600 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001601 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 int got_com = FALSE;
1603 int found_one;
1604 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1605 char_u *string; /* pointer to comment string */
1606 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001607 int middle_match_len = 0;
1608 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001609 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaar81340392012-06-06 16:12:59 +02001611 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 while (vim_iswhite(line[i])) /* leading white space is ignored */
1613 ++i;
1614
1615 /*
1616 * Repeat to match several nested comment strings.
1617 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001618 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 {
1620 /*
1621 * scan through the 'comments' option for a match
1622 */
1623 found_one = FALSE;
1624 for (list = curbuf->b_p_com; *list; )
1625 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001626 /* Get one option part into part_buf[]. Advance "list" to next
1627 * one. Put "string" at start of string. */
1628 if (!got_com && flags != NULL)
1629 *flags = list; /* remember where flags started */
1630 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1632 string = vim_strchr(part_buf, ':');
1633 if (string == NULL) /* missing ':', ignore this part */
1634 continue;
1635 *string++ = NUL; /* isolate flags from string */
1636
Bram Moolenaara4271d52011-05-10 13:38:27 +02001637 /* If we found a middle match previously, use that match when this
1638 * is not a middle or end. */
1639 if (middle_match_len != 0
1640 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1641 && vim_strchr(part_buf, COM_END) == NULL)
1642 break;
1643
1644 /* When we already found a nested comment, only accept further
1645 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1647 continue;
1648
Bram Moolenaara4271d52011-05-10 13:38:27 +02001649 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1651 continue;
1652
Bram Moolenaara4271d52011-05-10 13:38:27 +02001653 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * When string starts with white space, must have some white space
1655 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001656 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (vim_iswhite(string[0]))
1658 {
1659 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001660 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 while (vim_iswhite(string[0]))
1662 ++string;
1663 }
1664 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1665 ;
1666 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001667 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
Bram Moolenaara4271d52011-05-10 13:38:27 +02001669 /* When 'b' flag used, there must be white space or an
1670 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (vim_strchr(part_buf, COM_BLANK) != NULL
1672 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1673 continue;
1674
Bram Moolenaara4271d52011-05-10 13:38:27 +02001675 /* We have found a match, stop searching unless this is a middle
1676 * comment. The middle comment can be a substring of the end
1677 * comment in which case it's better to return the length of the
1678 * end comment and its flags. Thus we keep searching with middle
1679 * and end matches and use an end match if it matches better. */
1680 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1681 {
1682 if (middle_match_len == 0)
1683 {
1684 middle_match_len = j;
1685 saved_flags = prev_list;
1686 }
1687 continue;
1688 }
1689 if (middle_match_len != 0 && j > middle_match_len)
1690 /* Use this match instead of the middle match, since it's a
1691 * longer thus better match. */
1692 middle_match_len = 0;
1693
1694 if (middle_match_len == 0)
1695 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 found_one = TRUE;
1697 break;
1698 }
1699
Bram Moolenaara4271d52011-05-10 13:38:27 +02001700 if (middle_match_len != 0)
1701 {
1702 /* Use the previously found middle match after failing to find a
1703 * match with an end. */
1704 if (!got_com && flags != NULL)
1705 *flags = saved_flags;
1706 i += middle_match_len;
1707 found_one = TRUE;
1708 }
1709
1710 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if (!found_one)
1712 break;
1713
Bram Moolenaar81340392012-06-06 16:12:59 +02001714 result = i;
1715
Bram Moolenaara4271d52011-05-10 13:38:27 +02001716 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 while (vim_iswhite(line[i]))
1718 ++i;
1719
Bram Moolenaar81340392012-06-06 16:12:59 +02001720 if (include_space)
1721 result = i;
1722
Bram Moolenaara4271d52011-05-10 13:38:27 +02001723 /* If this comment doesn't nest, stop here. */
1724 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (vim_strchr(part_buf, COM_NEST) == NULL)
1726 break;
1727 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001728 return result;
1729}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001730
Bram Moolenaar81340392012-06-06 16:12:59 +02001731/*
1732 * Return the offset at which the last comment in line starts. If there is no
1733 * comment in the whole line, -1 is returned.
1734 *
1735 * When "flags" is not null, it is set to point to the flags describing the
1736 * recognized comment leader.
1737 */
1738 int
1739get_last_leader_offset(line, flags)
1740 char_u *line;
1741 char_u **flags;
1742{
1743 int result = -1;
1744 int i, j;
1745 int lower_check_bound = 0;
1746 char_u *string;
1747 char_u *com_leader;
1748 char_u *com_flags;
1749 char_u *list;
1750 int found_one;
1751 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1752
1753 /*
1754 * Repeat to match several nested comment strings.
1755 */
1756 i = (int)STRLEN(line);
1757 while (--i >= lower_check_bound)
1758 {
1759 /*
1760 * scan through the 'comments' option for a match
1761 */
1762 found_one = FALSE;
1763 for (list = curbuf->b_p_com; *list; )
1764 {
1765 char_u *flags_save = list;
1766
1767 /*
1768 * Get one option part into part_buf[]. Advance list to next one.
1769 * put string at start of string.
1770 */
1771 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1772 string = vim_strchr(part_buf, ':');
1773 if (string == NULL) /* If everything is fine, this cannot actually
1774 * happen. */
1775 {
1776 continue;
1777 }
1778 *string++ = NUL; /* Isolate flags from string. */
1779 com_leader = string;
1780
1781 /*
1782 * Line contents and string must match.
1783 * When string starts with white space, must have some white space
1784 * (but the amount does not need to match, there might be a mix of
1785 * TABs and spaces).
1786 */
1787 if (vim_iswhite(string[0]))
1788 {
1789 if (i == 0 || !vim_iswhite(line[i - 1]))
1790 continue;
1791 while (vim_iswhite(string[0]))
1792 ++string;
1793 }
1794 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1795 /* do nothing */;
1796 if (string[j] != NUL)
1797 continue;
1798
1799 /*
1800 * When 'b' flag used, there must be white space or an
1801 * end-of-line after the string in the line.
1802 */
1803 if (vim_strchr(part_buf, COM_BLANK) != NULL
1804 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1805 {
1806 continue;
1807 }
1808
1809 /*
1810 * We have found a match, stop searching.
1811 */
1812 found_one = TRUE;
1813
1814 if (flags)
1815 *flags = flags_save;
1816 com_flags = flags_save;
1817
1818 break;
1819 }
1820
1821 if (found_one)
1822 {
1823 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1824 int len1, len2, off;
1825
1826 result = i;
1827 /*
1828 * If this comment nests, continue searching.
1829 */
1830 if (vim_strchr(part_buf, COM_NEST) != NULL)
1831 continue;
1832
1833 lower_check_bound = i;
1834
1835 /* Let's verify whether the comment leader found is a substring
1836 * of other comment leaders. If it is, let's adjust the
1837 * lower_check_bound so that we make sure that we have determined
1838 * the comment leader correctly.
1839 */
1840
1841 while (vim_iswhite(*com_leader))
1842 ++com_leader;
1843 len1 = (int)STRLEN(com_leader);
1844
1845 for (list = curbuf->b_p_com; *list; )
1846 {
1847 char_u *flags_save = list;
1848
1849 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1850 if (flags_save == com_flags)
1851 continue;
1852 string = vim_strchr(part_buf2, ':');
1853 ++string;
1854 while (vim_iswhite(*string))
1855 ++string;
1856 len2 = (int)STRLEN(string);
1857 if (len2 == 0)
1858 continue;
1859
1860 /* Now we have to verify whether string ends with a substring
1861 * beginning the com_leader. */
1862 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1863 {
1864 --off;
1865 if (!STRNCMP(string + off, com_leader, len2 - off))
1866 {
1867 if (i - off < lower_check_bound)
1868 lower_check_bound = i - off;
1869 }
1870 }
1871 }
1872 }
1873 }
1874 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875}
1876#endif
1877
1878/*
1879 * Return the number of window lines occupied by buffer line "lnum".
1880 */
1881 int
1882plines(lnum)
1883 linenr_T lnum;
1884{
1885 return plines_win(curwin, lnum, TRUE);
1886}
1887
1888 int
1889plines_win(wp, lnum, winheight)
1890 win_T *wp;
1891 linenr_T lnum;
1892 int winheight; /* when TRUE limit to window height */
1893{
1894#if defined(FEAT_DIFF) || defined(PROTO)
1895 /* Check for filler lines above this buffer line. When folded the result
1896 * is one line anyway. */
1897 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1898}
1899
1900 int
1901plines_nofill(lnum)
1902 linenr_T lnum;
1903{
1904 return plines_win_nofill(curwin, lnum, TRUE);
1905}
1906
1907 int
1908plines_win_nofill(wp, lnum, winheight)
1909 win_T *wp;
1910 linenr_T lnum;
1911 int winheight; /* when TRUE limit to window height */
1912{
1913#endif
1914 int lines;
1915
1916 if (!wp->w_p_wrap)
1917 return 1;
1918
1919#ifdef FEAT_VERTSPLIT
1920 if (wp->w_width == 0)
1921 return 1;
1922#endif
1923
1924#ifdef FEAT_FOLDING
1925 /* A folded lines is handled just like an empty line. */
1926 /* NOTE: Caller must handle lines that are MAYBE folded. */
1927 if (lineFolded(wp, lnum) == TRUE)
1928 return 1;
1929#endif
1930
1931 lines = plines_win_nofold(wp, lnum);
1932 if (winheight > 0 && lines > wp->w_height)
1933 return (int)wp->w_height;
1934 return lines;
1935}
1936
1937/*
1938 * Return number of window lines physical line "lnum" will occupy in window
1939 * "wp". Does not care about folding, 'wrap' or 'diff'.
1940 */
1941 int
1942plines_win_nofold(wp, lnum)
1943 win_T *wp;
1944 linenr_T lnum;
1945{
1946 char_u *s;
1947 long col;
1948 int width;
1949
1950 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
1951 if (*s == NUL) /* empty line */
1952 return 1;
1953 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
1954
1955 /*
1956 * If list mode is on, then the '$' at the end of the line may take up one
1957 * extra column.
1958 */
1959 if (wp->w_p_list && lcs_eol != NUL)
1960 col += 1;
1961
1962 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001963 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 */
1965 width = W_WIDTH(wp) - win_col_off(wp);
1966 if (width <= 0)
1967 return 32000;
1968 if (col <= width)
1969 return 1;
1970 col -= width;
1971 width += win_col_off2(wp);
1972 return (col + (width - 1)) / width + 1;
1973}
1974
1975/*
1976 * Like plines_win(), but only reports the number of physical screen lines
1977 * used from the start of the line to the given column number.
1978 */
1979 int
1980plines_win_col(wp, lnum, column)
1981 win_T *wp;
1982 linenr_T lnum;
1983 long column;
1984{
1985 long col;
1986 char_u *s;
1987 int lines = 0;
1988 int width;
1989
1990#ifdef FEAT_DIFF
1991 /* Check for filler lines above this buffer line. When folded the result
1992 * is one line anyway. */
1993 lines = diff_check_fill(wp, lnum);
1994#endif
1995
1996 if (!wp->w_p_wrap)
1997 return lines + 1;
1998
1999#ifdef FEAT_VERTSPLIT
2000 if (wp->w_width == 0)
2001 return lines + 1;
2002#endif
2003
2004 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2005
2006 col = 0;
2007 while (*s != NUL && --column >= 0)
2008 {
2009 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002010 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012
2013 /*
2014 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2015 * INSERT mode, then col must be adjusted so that it represents the last
2016 * screen position of the TAB. This only fixes an error when the TAB wraps
2017 * from one screen line to the next (when 'columns' is not a multiple of
2018 * 'ts') -- webb.
2019 */
2020 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
2021 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1;
2022
2023 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002024 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 */
2026 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002027 if (width <= 0)
2028 return 9999;
2029
2030 lines += 1;
2031 if (col > width)
2032 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2033 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034}
2035
2036 int
2037plines_m_win(wp, first, last)
2038 win_T *wp;
2039 linenr_T first, last;
2040{
2041 int count = 0;
2042
2043 while (first <= last)
2044 {
2045#ifdef FEAT_FOLDING
2046 int x;
2047
2048 /* Check if there are any really folded lines, but also included lines
2049 * that are maybe folded. */
2050 x = foldedCount(wp, first, NULL);
2051 if (x > 0)
2052 {
2053 ++count; /* count 1 for "+-- folded" line */
2054 first += x;
2055 }
2056 else
2057#endif
2058 {
2059#ifdef FEAT_DIFF
2060 if (first == wp->w_topline)
2061 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2062 else
2063#endif
2064 count += plines_win(wp, first, TRUE);
2065 ++first;
2066 }
2067 }
2068 return (count);
2069}
2070
2071#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2072/*
2073 * Insert string "p" at the cursor position. Stops at a NUL byte.
2074 * Handles Replace mode and multi-byte characters.
2075 */
2076 void
2077ins_bytes(p)
2078 char_u *p;
2079{
2080 ins_bytes_len(p, (int)STRLEN(p));
2081}
2082#endif
2083
2084#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2085 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2086/*
2087 * Insert string "p" with length "len" at the cursor position.
2088 * Handles Replace mode and multi-byte characters.
2089 */
2090 void
2091ins_bytes_len(p, len)
2092 char_u *p;
2093 int len;
2094{
2095 int i;
2096# ifdef FEAT_MBYTE
2097 int n;
2098
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002099 if (has_mbyte)
2100 for (i = 0; i < len; i += n)
2101 {
2102 if (enc_utf8)
2103 /* avoid reading past p[len] */
2104 n = utfc_ptr2len_len(p + i, len - i);
2105 else
2106 n = (*mb_ptr2len)(p + i);
2107 ins_char_bytes(p + i, n);
2108 }
2109 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002111 for (i = 0; i < len; ++i)
2112 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113}
2114#endif
2115
2116/*
2117 * Insert or replace a single character at the cursor position.
2118 * When in REPLACE or VREPLACE mode, replace any existing character.
2119 * Caller must have prepared for undo.
2120 * For multi-byte characters we get the whole character, the caller must
2121 * convert bytes to a character.
2122 */
2123 void
2124ins_char(c)
2125 int c;
2126{
2127#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002128 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 int n;
2130
2131 n = (*mb_char2bytes)(c, buf);
2132
2133 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2134 * Happens for CTRL-Vu9900. */
2135 if (buf[0] == 0)
2136 buf[0] = '\n';
2137
2138 ins_char_bytes(buf, n);
2139}
2140
2141 void
2142ins_char_bytes(buf, charlen)
2143 char_u *buf;
2144 int charlen;
2145{
2146 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147#endif
2148 int newlen; /* nr of bytes inserted */
2149 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2150 char_u *p;
2151 char_u *newp;
2152 char_u *oldp;
2153 int linelen; /* length of old line including NUL */
2154 colnr_T col;
2155 linenr_T lnum = curwin->w_cursor.lnum;
2156 int i;
2157
2158#ifdef FEAT_VIRTUALEDIT
2159 /* Break tabs if needed. */
2160 if (virtual_active() && curwin->w_cursor.coladd > 0)
2161 coladvance_force(getviscol());
2162#endif
2163
2164 col = curwin->w_cursor.col;
2165 oldp = ml_get(lnum);
2166 linelen = (int)STRLEN(oldp) + 1;
2167
2168 /* The lengths default to the values for when not replacing. */
2169 oldlen = 0;
2170#ifdef FEAT_MBYTE
2171 newlen = charlen;
2172#else
2173 newlen = 1;
2174#endif
2175
2176 if (State & REPLACE_FLAG)
2177 {
2178#ifdef FEAT_VREPLACE
2179 if (State & VREPLACE_FLAG)
2180 {
2181 colnr_T new_vcol = 0; /* init for GCC */
2182 colnr_T vcol;
2183 int old_list;
2184#ifndef FEAT_MBYTE
2185 char_u buf[2];
2186#endif
2187
2188 /*
2189 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2190 * Returns the old value of list, so when finished,
2191 * curwin->w_p_list should be set back to this.
2192 */
2193 old_list = curwin->w_p_list;
2194 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2195 curwin->w_p_list = FALSE;
2196
2197 /*
2198 * In virtual replace mode each character may replace one or more
2199 * characters (zero if it's a TAB). Count the number of bytes to
2200 * be deleted to make room for the new character, counting screen
2201 * cells. May result in adding spaces to fill a gap.
2202 */
2203 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2204#ifndef FEAT_MBYTE
2205 buf[0] = c;
2206 buf[1] = NUL;
2207#endif
2208 new_vcol = vcol + chartabsize(buf, vcol);
2209 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2210 {
2211 vcol += chartabsize(oldp + col + oldlen, vcol);
2212 /* Don't need to remove a TAB that takes us to the right
2213 * position. */
2214 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2215 break;
2216#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002217 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218#else
2219 ++oldlen;
2220#endif
2221 /* Deleted a bit too much, insert spaces. */
2222 if (vcol > new_vcol)
2223 newlen += vcol - new_vcol;
2224 }
2225 curwin->w_p_list = old_list;
2226 }
2227 else
2228#endif
2229 if (oldp[col] != NUL)
2230 {
2231 /* normal replace */
2232#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002233 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#else
2235 oldlen = 1;
2236#endif
2237 }
2238
2239
2240 /* Push the replaced bytes onto the replace stack, so that they can be
2241 * put back when BS is used. The bytes of a multi-byte character are
2242 * done the other way around, so that the first byte is popped off
2243 * first (it tells the byte length of the character). */
2244 replace_push(NUL);
2245 for (i = 0; i < oldlen; ++i)
2246 {
2247#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002248 if (has_mbyte)
2249 i += replace_push_mb(oldp + col + i) - 1;
2250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002252 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 }
2255
2256 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2257 if (newp == NULL)
2258 return;
2259
2260 /* Copy bytes before the cursor. */
2261 if (col > 0)
2262 mch_memmove(newp, oldp, (size_t)col);
2263
2264 /* Copy bytes after the changed character(s). */
2265 p = newp + col;
2266 mch_memmove(p + newlen, oldp + col + oldlen,
2267 (size_t)(linelen - col - oldlen));
2268
2269 /* Insert or overwrite the new character. */
2270#ifdef FEAT_MBYTE
2271 mch_memmove(p, buf, charlen);
2272 i = charlen;
2273#else
2274 *p = c;
2275 i = 1;
2276#endif
2277
2278 /* Fill with spaces when necessary. */
2279 while (i < newlen)
2280 p[i++] = ' ';
2281
2282 /* Replace the line in the buffer. */
2283 ml_replace(lnum, newp, FALSE);
2284
2285 /* mark the buffer as changed and prepare for displaying */
2286 changed_bytes(lnum, col);
2287
2288 /*
2289 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2290 * show the match for right parens and braces.
2291 */
2292 if (p_sm && (State & INSERT)
2293 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002294#ifdef FEAT_INS_EXPAND
2295 && !ins_compl_active()
2296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002298 {
2299#ifdef FEAT_MBYTE
2300 if (has_mbyte)
2301 showmatch(mb_ptr2char(buf));
2302 else
2303#endif
2304 showmatch(c);
2305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
2307#ifdef FEAT_RIGHTLEFT
2308 if (!p_ri || (State & REPLACE_FLAG))
2309#endif
2310 {
2311 /* Normal insert: move cursor right */
2312#ifdef FEAT_MBYTE
2313 curwin->w_cursor.col += charlen;
2314#else
2315 ++curwin->w_cursor.col;
2316#endif
2317 }
2318 /*
2319 * TODO: should try to update w_row here, to avoid recomputing it later.
2320 */
2321}
2322
2323/*
2324 * Insert a string at the cursor position.
2325 * Note: Does NOT handle Replace mode.
2326 * Caller must have prepared for undo.
2327 */
2328 void
2329ins_str(s)
2330 char_u *s;
2331{
2332 char_u *oldp, *newp;
2333 int newlen = (int)STRLEN(s);
2334 int oldlen;
2335 colnr_T col;
2336 linenr_T lnum = curwin->w_cursor.lnum;
2337
2338#ifdef FEAT_VIRTUALEDIT
2339 if (virtual_active() && curwin->w_cursor.coladd > 0)
2340 coladvance_force(getviscol());
2341#endif
2342
2343 col = curwin->w_cursor.col;
2344 oldp = ml_get(lnum);
2345 oldlen = (int)STRLEN(oldp);
2346
2347 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2348 if (newp == NULL)
2349 return;
2350 if (col > 0)
2351 mch_memmove(newp, oldp, (size_t)col);
2352 mch_memmove(newp + col, s, (size_t)newlen);
2353 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2354 ml_replace(lnum, newp, FALSE);
2355 changed_bytes(lnum, col);
2356 curwin->w_cursor.col += newlen;
2357}
2358
2359/*
2360 * Delete one character under the cursor.
2361 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2362 * Caller must have prepared for undo.
2363 *
2364 * return FAIL for failure, OK otherwise
2365 */
2366 int
2367del_char(fixpos)
2368 int fixpos;
2369{
2370#ifdef FEAT_MBYTE
2371 if (has_mbyte)
2372 {
2373 /* Make sure the cursor is at the start of a character. */
2374 mb_adjust_cursor();
2375 if (*ml_get_cursor() == NUL)
2376 return FAIL;
2377 return del_chars(1L, fixpos);
2378 }
2379#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002380 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381}
2382
2383#if defined(FEAT_MBYTE) || defined(PROTO)
2384/*
2385 * Like del_bytes(), but delete characters instead of bytes.
2386 */
2387 int
2388del_chars(count, fixpos)
2389 long count;
2390 int fixpos;
2391{
2392 long bytes = 0;
2393 long i;
2394 char_u *p;
2395 int l;
2396
2397 p = ml_get_cursor();
2398 for (i = 0; i < count && *p != NUL; ++i)
2399 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002400 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 bytes += l;
2402 p += l;
2403 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002404 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405}
2406#endif
2407
2408/*
2409 * Delete "count" bytes under the cursor.
2410 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2411 * Caller must have prepared for undo.
2412 *
2413 * return FAIL for failure, OK otherwise
2414 */
2415 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002416del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002418 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002419 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 char_u *oldp, *newp;
2422 colnr_T oldlen;
2423 linenr_T lnum = curwin->w_cursor.lnum;
2424 colnr_T col = curwin->w_cursor.col;
2425 int was_alloced;
2426 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002427 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 oldp = ml_get(lnum);
2430 oldlen = (int)STRLEN(oldp);
2431
2432 /*
2433 * Can't do anything when the cursor is on the NUL after the line.
2434 */
2435 if (col >= oldlen)
2436 return FAIL;
2437
2438#ifdef FEAT_MBYTE
2439 /* If 'delcombine' is set and deleting (less than) one character, only
2440 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002441 if (p_deco && use_delcombine && enc_utf8
2442 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002444 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 int n;
2446
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002447 (void)utfc_ptr2char(oldp + col, cc);
2448 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 /* Find the last composing char, there can be several. */
2451 n = col;
2452 do
2453 {
2454 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002455 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 n += count;
2457 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2458 fixpos = 0;
2459 }
2460 }
2461#endif
2462
2463 /*
2464 * When count is too big, reduce it.
2465 */
2466 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2467 if (movelen <= 1)
2468 {
2469 /*
2470 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002471 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2472 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002474 if (col > 0 && fixpos && restart_edit == 0
2475#ifdef FEAT_VIRTUALEDIT
2476 && (ve_flags & VE_ONEMORE) == 0
2477#endif
2478 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 --curwin->w_cursor.col;
2481#ifdef FEAT_VIRTUALEDIT
2482 curwin->w_cursor.coladd = 0;
2483#endif
2484#ifdef FEAT_MBYTE
2485 if (has_mbyte)
2486 curwin->w_cursor.col -=
2487 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2488#endif
2489 }
2490 count = oldlen - col;
2491 movelen = 1;
2492 }
2493
2494 /*
2495 * If the old line has been allocated the deletion can be done in the
2496 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002497 * Can't do this when using Netbeans, because we would need to invoke
2498 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002499 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002502 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002503 was_alloced = FALSE;
2504 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002506 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (was_alloced)
2508 newp = oldp; /* use same allocated memory */
2509 else
2510 { /* need to allocate a new line */
2511 newp = alloc((unsigned)(oldlen + 1 - count));
2512 if (newp == NULL)
2513 return FAIL;
2514 mch_memmove(newp, oldp, (size_t)col);
2515 }
2516 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2517 if (!was_alloced)
2518 ml_replace(lnum, newp, FALSE);
2519
2520 /* mark the buffer as changed and prepare for displaying */
2521 changed_bytes(lnum, curwin->w_cursor.col);
2522
2523 return OK;
2524}
2525
2526/*
2527 * Delete from cursor to end of line.
2528 * Caller must have prepared for undo.
2529 *
2530 * return FAIL for failure, OK otherwise
2531 */
2532 int
2533truncate_line(fixpos)
2534 int fixpos; /* if TRUE fix the cursor position when done */
2535{
2536 char_u *newp;
2537 linenr_T lnum = curwin->w_cursor.lnum;
2538 colnr_T col = curwin->w_cursor.col;
2539
2540 if (col == 0)
2541 newp = vim_strsave((char_u *)"");
2542 else
2543 newp = vim_strnsave(ml_get(lnum), col);
2544
2545 if (newp == NULL)
2546 return FAIL;
2547
2548 ml_replace(lnum, newp, FALSE);
2549
2550 /* mark the buffer as changed and prepare for displaying */
2551 changed_bytes(lnum, curwin->w_cursor.col);
2552
2553 /*
2554 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2555 */
2556 if (fixpos && curwin->w_cursor.col > 0)
2557 --curwin->w_cursor.col;
2558
2559 return OK;
2560}
2561
2562/*
2563 * Delete "nlines" lines at the cursor.
2564 * Saves the lines for undo first if "undo" is TRUE.
2565 */
2566 void
2567del_lines(nlines, undo)
2568 long nlines; /* number of lines to delete */
2569 int undo; /* if TRUE, prepare for undo */
2570{
2571 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002572 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574 if (nlines <= 0)
2575 return;
2576
2577 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002578 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 return;
2580
2581 for (n = 0; n < nlines; )
2582 {
2583 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2584 break;
2585
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002586 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 ++n;
2588
2589 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002590 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 break;
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002594 /* Correct the cursor position before calling deleted_lines_mark(), it may
2595 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 curwin->w_cursor.col = 0;
2597 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002598
2599 /* adjust marks, mark the buffer as changed and prepare for displaying */
2600 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601}
2602
2603 int
2604gchar_pos(pos)
2605 pos_T *pos;
2606{
2607 char_u *ptr = ml_get_pos(pos);
2608
2609#ifdef FEAT_MBYTE
2610 if (has_mbyte)
2611 return (*mb_ptr2char)(ptr);
2612#endif
2613 return (int)*ptr;
2614}
2615
2616 int
2617gchar_cursor()
2618{
2619#ifdef FEAT_MBYTE
2620 if (has_mbyte)
2621 return (*mb_ptr2char)(ml_get_cursor());
2622#endif
2623 return (int)*ml_get_cursor();
2624}
2625
2626/*
2627 * Write a character at the current cursor position.
2628 * It is directly written into the block.
2629 */
2630 void
2631pchar_cursor(c)
2632 int c;
2633{
2634 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2635 + curwin->w_cursor.col) = c;
2636}
2637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638/*
2639 * When extra == 0: Return TRUE if the cursor is before or on the first
2640 * non-blank in the line.
2641 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2642 * the line.
2643 */
2644 int
2645inindent(extra)
2646 int extra;
2647{
2648 char_u *ptr;
2649 colnr_T col;
2650
2651 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2652 ++ptr;
2653 if (col >= curwin->w_cursor.col + extra)
2654 return TRUE;
2655 else
2656 return FALSE;
2657}
2658
2659/*
2660 * Skip to next part of an option argument: Skip space and comma.
2661 */
2662 char_u *
2663skip_to_option_part(p)
2664 char_u *p;
2665{
2666 if (*p == ',')
2667 ++p;
2668 while (*p == ' ')
2669 ++p;
2670 return p;
2671}
2672
2673/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002674 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 *
2676 * Most often called through changed_bytes() and changed_lines(), which also
2677 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002678 *
2679 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
2681 void
2682changed()
2683{
2684#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2685 /* The text of the preediting area is inserted, but this doesn't
2686 * mean a change of the buffer yet. That is delayed until the
2687 * text is committed. (this means preedit becomes empty) */
2688 if (im_is_preediting() && !xim_changed_while_preediting)
2689 return;
2690 xim_changed_while_preediting = FALSE;
2691#endif
2692
2693 if (!curbuf->b_changed)
2694 {
2695 int save_msg_scroll = msg_scroll;
2696
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002697 /* Give a warning about changing a read-only file. This may also
2698 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 /* Create a swap file if that is wanted.
2702 * Don't do this for "nofile" and "nowrite" buffer types. */
2703 if (curbuf->b_may_swap
2704#ifdef FEAT_QUICKFIX
2705 && !bt_dontwrite(curbuf)
2706#endif
2707 )
2708 {
2709 ml_open_file(curbuf);
2710
2711 /* The ml_open_file() can cause an ATTENTION message.
2712 * Wait two seconds, to make sure the user reads this unexpected
2713 * message. Since we could be anywhere, call wait_return() now,
2714 * and don't let the emsg() set msg_scroll. */
2715 if (need_wait_return && emsg_silent == 0)
2716 {
2717 out_flush();
2718 ui_delay(2000L, TRUE);
2719 wait_return(TRUE);
2720 msg_scroll = save_msg_scroll;
2721 }
2722 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002723 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726}
2727
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002728/*
2729 * Internal part of changed(), no user interaction.
2730 */
2731 void
2732changed_int()
2733{
2734 curbuf->b_changed = TRUE;
2735 ml_setflags(curbuf);
2736#ifdef FEAT_WINDOWS
2737 check_status(curbuf);
2738 redraw_tabline = TRUE;
2739#endif
2740#ifdef FEAT_TITLE
2741 need_maketitle = TRUE; /* set window title later */
2742#endif
2743}
2744
Bram Moolenaardba8a912005-04-24 22:08:39 +00002745static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2746static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2748
2749/*
2750 * Changed bytes within a single line for the current buffer.
2751 * - marks the windows on this buffer to be redisplayed
2752 * - marks the buffer changed by calling changed()
2753 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002754 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 */
2756 void
2757changed_bytes(lnum, col)
2758 linenr_T lnum;
2759 colnr_T col;
2760{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002761 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002763
2764#ifdef FEAT_DIFF
2765 /* Diff highlighting in other diff windows may need to be updated too. */
2766 if (curwin->w_p_diff)
2767 {
2768 win_T *wp;
2769 linenr_T wlnum;
2770
2771 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2772 if (wp->w_p_diff && wp != curwin)
2773 {
2774 redraw_win_later(wp, VALID);
2775 wlnum = diff_lnum_win(lnum, wp);
2776 if (wlnum > 0)
2777 changedOneline(wp->w_buffer, wlnum);
2778 }
2779 }
2780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781}
2782
2783 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002784changedOneline(buf, lnum)
2785 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 linenr_T lnum;
2787{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002788 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
2790 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002791 if (lnum < buf->b_mod_top)
2792 buf->b_mod_top = lnum;
2793 else if (lnum >= buf->b_mod_bot)
2794 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796 else
2797 {
2798 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002799 buf->b_mod_set = TRUE;
2800 buf->b_mod_top = lnum;
2801 buf->b_mod_bot = lnum + 1;
2802 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804}
2805
2806/*
2807 * Appended "count" lines below line "lnum" in the current buffer.
2808 * Must be called AFTER the change and after mark_adjust().
2809 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2810 */
2811 void
2812appended_lines(lnum, count)
2813 linenr_T lnum;
2814 long count;
2815{
2816 changed_lines(lnum + 1, 0, lnum + 1, count);
2817}
2818
2819/*
2820 * Like appended_lines(), but adjust marks first.
2821 */
2822 void
2823appended_lines_mark(lnum, count)
2824 linenr_T lnum;
2825 long count;
2826{
2827 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2828 changed_lines(lnum + 1, 0, lnum + 1, count);
2829}
2830
2831/*
2832 * Deleted "count" lines at line "lnum" in the current buffer.
2833 * Must be called AFTER the change and after mark_adjust().
2834 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2835 */
2836 void
2837deleted_lines(lnum, count)
2838 linenr_T lnum;
2839 long count;
2840{
2841 changed_lines(lnum, 0, lnum + count, -count);
2842}
2843
2844/*
2845 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002846 * Make sure the cursor is on a valid line before calling, a GUI callback may
2847 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 */
2849 void
2850deleted_lines_mark(lnum, count)
2851 linenr_T lnum;
2852 long count;
2853{
2854 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2855 changed_lines(lnum, 0, lnum + count, -count);
2856}
2857
2858/*
2859 * Changed lines for the current buffer.
2860 * Must be called AFTER the change and after mark_adjust().
2861 * - mark the buffer changed by calling changed()
2862 * - mark the windows on this buffer to be redisplayed
2863 * - invalidate cached values
2864 * "lnum" is the first line that needs displaying, "lnume" the first line
2865 * below the changed lines (BEFORE the change).
2866 * When only inserting lines, "lnum" and "lnume" are equal.
2867 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002868 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
2870 void
2871changed_lines(lnum, col, lnume, xtra)
2872 linenr_T lnum; /* first line with change */
2873 colnr_T col; /* column in first line with change */
2874 linenr_T lnume; /* line below last changed line */
2875 long xtra; /* number of extra lines (negative when deleting) */
2876{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002877 changed_lines_buf(curbuf, lnum, lnume, xtra);
2878
2879#ifdef FEAT_DIFF
2880 if (xtra == 0 && curwin->w_p_diff)
2881 {
2882 /* When the number of lines doesn't change then mark_adjust() isn't
2883 * called and other diff buffers still need to be marked for
2884 * displaying. */
2885 win_T *wp;
2886 linenr_T wlnum;
2887
2888 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2889 if (wp->w_p_diff && wp != curwin)
2890 {
2891 redraw_win_later(wp, VALID);
2892 wlnum = diff_lnum_win(lnum, wp);
2893 if (wlnum > 0)
2894 changed_lines_buf(wp->w_buffer, wlnum,
2895 lnume - lnum + wlnum, 0L);
2896 }
2897 }
2898#endif
2899
2900 changed_common(lnum, col, lnume, xtra);
2901}
2902
2903 static void
2904changed_lines_buf(buf, lnum, lnume, xtra)
2905 buf_T *buf;
2906 linenr_T lnum; /* first line with change */
2907 linenr_T lnume; /* line below last changed line */
2908 long xtra; /* number of extra lines (negative when deleting) */
2909{
2910 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002913 if (lnum < buf->b_mod_top)
2914 buf->b_mod_top = lnum;
2915 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
2917 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002918 buf->b_mod_bot += xtra;
2919 if (buf->b_mod_bot < lnum)
2920 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002922 if (lnume + xtra > buf->b_mod_bot)
2923 buf->b_mod_bot = lnume + xtra;
2924 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926 else
2927 {
2928 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002929 buf->b_mod_set = TRUE;
2930 buf->b_mod_top = lnum;
2931 buf->b_mod_bot = lnume + xtra;
2932 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934}
2935
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002936/*
2937 * Common code for when a change is was made.
2938 * See changed_lines() for the arguments.
2939 * Careful: may trigger autocommands that reload the buffer.
2940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 static void
2942changed_common(lnum, col, lnume, xtra)
2943 linenr_T lnum;
2944 colnr_T col;
2945 linenr_T lnume;
2946 long xtra;
2947{
2948 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002949#ifdef FEAT_WINDOWS
2950 tabpage_T *tp;
2951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 int i;
2953#ifdef FEAT_JUMPLIST
2954 int cols;
2955 pos_T *p;
2956 int add;
2957#endif
2958
2959 /* mark the buffer as modified */
2960 changed();
2961
2962 /* set the '. mark */
2963 if (!cmdmod.keepjumps)
2964 {
2965 curbuf->b_last_change.lnum = lnum;
2966 curbuf->b_last_change.col = col;
2967
2968#ifdef FEAT_JUMPLIST
2969 /* Create a new entry if a new undo-able change was started or we
2970 * don't have an entry yet. */
2971 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
2972 {
2973 if (curbuf->b_changelistlen == 0)
2974 add = TRUE;
2975 else
2976 {
2977 /* Don't create a new entry when the line number is the same
2978 * as the last one and the column is not too far away. Avoids
2979 * creating many entries for typing "xxxxx". */
2980 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
2981 if (p->lnum != lnum)
2982 add = TRUE;
2983 else
2984 {
2985 cols = comp_textwidth(FALSE);
2986 if (cols == 0)
2987 cols = 79;
2988 add = (p->col + cols < col || col + cols < p->col);
2989 }
2990 }
2991 if (add)
2992 {
2993 /* This is the first of a new sequence of undo-able changes
2994 * and it's at some distance of the last change. Use a new
2995 * position in the changelist. */
2996 curbuf->b_new_change = FALSE;
2997
2998 if (curbuf->b_changelistlen == JUMPLISTSIZE)
2999 {
3000 /* changelist is full: remove oldest entry */
3001 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3002 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3003 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003004 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
3006 /* Correct position in changelist for other windows on
3007 * this buffer. */
3008 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3009 --wp->w_changelistidx;
3010 }
3011 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003012 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
3014 /* For other windows, if the position in the changelist is
3015 * at the end it stays at the end. */
3016 if (wp->w_buffer == curbuf
3017 && wp->w_changelistidx == curbuf->b_changelistlen)
3018 ++wp->w_changelistidx;
3019 }
3020 ++curbuf->b_changelistlen;
3021 }
3022 }
3023 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3024 curbuf->b_last_change;
3025 /* The current window is always after the last change, so that "g,"
3026 * takes you back to it. */
3027 curwin->w_changelistidx = curbuf->b_changelistlen;
3028#endif
3029 }
3030
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003031 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 {
3033 if (wp->w_buffer == curbuf)
3034 {
3035 /* Mark this window to be redrawn later. */
3036 if (wp->w_redr_type < VALID)
3037 wp->w_redr_type = VALID;
3038
3039 /* Check if a change in the buffer has invalidated the cached
3040 * values for the cursor. */
3041#ifdef FEAT_FOLDING
3042 /*
3043 * Update the folds for this window. Can't postpone this, because
3044 * a following operator might work on the whole fold: ">>dd".
3045 */
3046 foldUpdate(wp, lnum, lnume + xtra - 1);
3047
3048 /* The change may cause lines above or below the change to become
3049 * included in a fold. Set lnum/lnume to the first/last line that
3050 * might be displayed differently.
3051 * Set w_cline_folded here as an efficient way to update it when
3052 * inserting lines just above a closed fold. */
3053 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3054 if (wp->w_cursor.lnum == lnum)
3055 wp->w_cline_folded = i;
3056 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3057 if (wp->w_cursor.lnum == lnume)
3058 wp->w_cline_folded = i;
3059
3060 /* If the changed line is in a range of previously folded lines,
3061 * compare with the first line in that range. */
3062 if (wp->w_cursor.lnum <= lnum)
3063 {
3064 i = find_wl_entry(wp, lnum);
3065 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3066 changed_line_abv_curs_win(wp);
3067 }
3068#endif
3069
3070 if (wp->w_cursor.lnum > lnum)
3071 changed_line_abv_curs_win(wp);
3072 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3073 changed_cline_bef_curs_win(wp);
3074 if (wp->w_botline >= lnum)
3075 {
3076 /* Assume that botline doesn't change (inserted lines make
3077 * other lines scroll down below botline). */
3078 approximate_botline_win(wp);
3079 }
3080
3081 /* Check if any w_lines[] entries have become invalid.
3082 * For entries below the change: Correct the lnums for
3083 * inserted/deleted lines. Makes it possible to stop displaying
3084 * after the change. */
3085 for (i = 0; i < wp->w_lines_valid; ++i)
3086 if (wp->w_lines[i].wl_valid)
3087 {
3088 if (wp->w_lines[i].wl_lnum >= lnum)
3089 {
3090 if (wp->w_lines[i].wl_lnum < lnume)
3091 {
3092 /* line included in change */
3093 wp->w_lines[i].wl_valid = FALSE;
3094 }
3095 else if (xtra != 0)
3096 {
3097 /* line below change */
3098 wp->w_lines[i].wl_lnum += xtra;
3099#ifdef FEAT_FOLDING
3100 wp->w_lines[i].wl_lastlnum += xtra;
3101#endif
3102 }
3103 }
3104#ifdef FEAT_FOLDING
3105 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3106 {
3107 /* change somewhere inside this range of folded lines,
3108 * may need to be redrawn */
3109 wp->w_lines[i].wl_valid = FALSE;
3110 }
3111#endif
3112 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003113
3114#ifdef FEAT_FOLDING
3115 /* Take care of side effects for setting w_topline when folds have
3116 * changed. Esp. when the buffer was changed in another window. */
3117 if (hasAnyFolding(wp))
3118 set_topline(wp, wp->w_topline);
3119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121 }
3122
3123 /* Call update_screen() later, which checks out what needs to be redrawn,
3124 * since it notices b_mod_set and then uses b_mod_*. */
3125 if (must_redraw < VALID)
3126 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003127
3128#ifdef FEAT_AUTOCMD
3129 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003130 if (lnum <= curwin->w_cursor.lnum
3131 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003132 last_cursormoved.lnum = 0;
3133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134}
3135
3136/*
3137 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3138 */
3139 void
3140unchanged(buf, ff)
3141 buf_T *buf;
3142 int ff; /* also reset 'fileformat' */
3143{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003144 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003147 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (ff)
3149 save_file_ff(buf);
3150#ifdef FEAT_WINDOWS
3151 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003152 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#endif
3154#ifdef FEAT_TITLE
3155 need_maketitle = TRUE; /* set window title later */
3156#endif
3157 }
3158 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#ifdef FEAT_NETBEANS_INTG
3160 netbeans_unmodified(buf);
3161#endif
3162}
3163
3164#if defined(FEAT_WINDOWS) || defined(PROTO)
3165/*
3166 * check_status: called when the status bars for the buffer 'buf'
3167 * need to be updated
3168 */
3169 void
3170check_status(buf)
3171 buf_T *buf;
3172{
3173 win_T *wp;
3174
3175 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3176 if (wp->w_buffer == buf && wp->w_status_height)
3177 {
3178 wp->w_redr_status = TRUE;
3179 if (must_redraw < VALID)
3180 must_redraw = VALID;
3181 }
3182}
3183#endif
3184
3185/*
3186 * If the file is readonly, give a warning message with the first change.
3187 * Don't do this for autocommands.
3188 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003189 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003191 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 */
3193 void
3194change_warning(col)
3195 int col; /* column for message; non-zero when in insert
3196 mode and 'showmode' is on */
3197{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003198 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (curbuf->b_did_warn == FALSE
3201 && curbufIsChanged() == 0
3202#ifdef FEAT_AUTOCMD
3203 && !autocmd_busy
3204#endif
3205 && curbuf->b_p_ro)
3206 {
3207#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003208 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003210 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (!curbuf->b_p_ro)
3212 return;
3213#endif
3214 /*
3215 * Do what msg() does, but with a column offset if the warning should
3216 * be after the mode message.
3217 */
3218 msg_start();
3219 if (msg_row == Rows - 1)
3220 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003221 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003222 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3223#ifdef FEAT_EVAL
3224 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 msg_clr_eos();
3227 (void)msg_end();
3228 if (msg_silent == 0 && !silent_mode)
3229 {
3230 out_flush();
3231 ui_delay(1000L, TRUE); /* give the user time to think about it */
3232 }
3233 curbuf->b_did_warn = TRUE;
3234 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3235 if (msg_row < Rows - 1)
3236 showmode();
3237 }
3238}
3239
3240/*
3241 * Ask for a reply from the user, a 'y' or a 'n'.
3242 * No other characters are accepted, the message is repeated until a valid
3243 * reply is entered or CTRL-C is hit.
3244 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3245 * from any buffers but directly from the user.
3246 *
3247 * return the 'y' or 'n'
3248 */
3249 int
3250ask_yesno(str, direct)
3251 char_u *str;
3252 int direct;
3253{
3254 int r = ' ';
3255 int save_State = State;
3256
3257 if (exiting) /* put terminal in raw mode for this question */
3258 settmode(TMODE_RAW);
3259 ++no_wait_return;
3260#ifdef USE_ON_FLY_SCROLL
3261 dont_scroll = TRUE; /* disallow scrolling here */
3262#endif
3263 State = CONFIRM; /* mouse behaves like with :confirm */
3264#ifdef FEAT_MOUSE
3265 setmouse(); /* disables mouse for xterm */
3266#endif
3267 ++no_mapping;
3268 ++allow_keys; /* no mapping here, but recognize keys */
3269
3270 while (r != 'y' && r != 'n')
3271 {
3272 /* same highlighting as for wait_return */
3273 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3274 if (direct)
3275 r = get_keystroke();
3276 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003277 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (r == Ctrl_C || r == ESC)
3279 r = 'n';
3280 msg_putchar(r); /* show what you typed */
3281 out_flush();
3282 }
3283 --no_wait_return;
3284 State = save_State;
3285#ifdef FEAT_MOUSE
3286 setmouse();
3287#endif
3288 --no_mapping;
3289 --allow_keys;
3290
3291 return r;
3292}
3293
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003294#if defined(FEAT_MOUSE) || defined(PROTO)
3295/*
3296 * Return TRUE if "c" is a mouse key.
3297 */
3298 int
3299is_mouse_key(c)
3300 int c;
3301{
3302 return c == K_LEFTMOUSE
3303 || c == K_LEFTMOUSE_NM
3304 || c == K_LEFTDRAG
3305 || c == K_LEFTRELEASE
3306 || c == K_LEFTRELEASE_NM
3307 || c == K_MIDDLEMOUSE
3308 || c == K_MIDDLEDRAG
3309 || c == K_MIDDLERELEASE
3310 || c == K_RIGHTMOUSE
3311 || c == K_RIGHTDRAG
3312 || c == K_RIGHTRELEASE
3313 || c == K_MOUSEDOWN
3314 || c == K_MOUSEUP
3315 || c == K_MOUSELEFT
3316 || c == K_MOUSERIGHT
3317 || c == K_X1MOUSE
3318 || c == K_X1DRAG
3319 || c == K_X1RELEASE
3320 || c == K_X2MOUSE
3321 || c == K_X2DRAG
3322 || c == K_X2RELEASE;
3323}
3324#endif
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326/*
3327 * Get a key stroke directly from the user.
3328 * Ignores mouse clicks and scrollbar events, except a click for the left
3329 * button (used at the more prompt).
3330 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3331 * Disadvantage: typeahead is ignored.
3332 * Translates the interrupt character for unix to ESC.
3333 */
3334 int
3335get_keystroke()
3336{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003337 char_u *buf = NULL;
3338 int buflen = 150;
3339 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 int len = 0;
3341 int n;
3342 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003343 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 mapped_ctrl_c = FALSE; /* mappings are not used here */
3346 for (;;)
3347 {
3348 cursor_on();
3349 out_flush();
3350
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003351 /* Leave some room for check_termcode() to insert a key code into (max
3352 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3353 * bytes. */
3354 maxlen = (buflen - 6 - len) / 3;
3355 if (buf == NULL)
3356 buf = alloc(buflen);
3357 else if (maxlen < 10)
3358 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003359 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003360 * escape sequence. */
3361 buflen += 100;
3362 buf = vim_realloc(buf, buflen);
3363 maxlen = (buflen - 6 - len) / 3;
3364 }
3365 if (buf == NULL)
3366 {
3367 do_outofmem_msg((long_u)buflen);
3368 return ESC; /* panic! */
3369 }
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003372 * terminal code to complete. */
3373 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (n > 0)
3375 {
3376 /* Replace zero and CSI by a special key code. */
3377 n = fix_input_buffer(buf + len, n, FALSE);
3378 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003379 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003381 else if (len > 0)
3382 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaar4395a712006-09-05 18:57:57 +00003384 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003385 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003386 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003388
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003389 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003390 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003391 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003392 {
3393 /* Redrawing was postponed, do it now. */
3394 update_screen(0);
3395 setcursor(); /* put cursor back where it belongs */
3396 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003397 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003398 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003399 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003401 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 continue;
3403
3404 /* Handle modifier and/or special key code. */
3405 n = buf[0];
3406 if (n == K_SPECIAL)
3407 {
3408 n = TO_SPECIAL(buf[1], buf[2]);
3409 if (buf[1] == KS_MODIFIER
3410 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003411#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003412 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003413#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003414#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 || n == K_VER_SCROLLBAR
3416 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417#endif
3418 )
3419 {
3420 if (buf[1] == KS_MODIFIER)
3421 mod_mask = buf[2];
3422 len -= 3;
3423 if (len > 0)
3424 mch_memmove(buf, buf + 3, (size_t)len);
3425 continue;
3426 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429#ifdef FEAT_MBYTE
3430 if (has_mbyte)
3431 {
3432 if (MB_BYTE2LEN(n) > len)
3433 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003434 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 n = (*mb_ptr2char)(buf);
3436 }
3437#endif
3438#ifdef UNIX
3439 if (n == intr_char)
3440 n = ESC;
3441#endif
3442 break;
3443 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003444 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
3446 mapped_ctrl_c = save_mapped_ctrl_c;
3447 return n;
3448}
3449
3450/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003451 * Get a number from the user.
3452 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 */
3454 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003455get_number(colon, mouse_used)
3456 int colon; /* allow colon to abort */
3457 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 int n = 0;
3460 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003461 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003463 if (mouse_used != NULL)
3464 *mouse_used = FALSE;
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 /* When not printing messages, the user won't know what to type, return a
3467 * zero (as if CR was hit). */
3468 if (msg_silent != 0)
3469 return 0;
3470
3471#ifdef USE_ON_FLY_SCROLL
3472 dont_scroll = TRUE; /* disallow scrolling here */
3473#endif
3474 ++no_mapping;
3475 ++allow_keys; /* no mapping here, but recognize keys */
3476 for (;;)
3477 {
3478 windgoto(msg_row, msg_col);
3479 c = safe_vgetc();
3480 if (VIM_ISDIGIT(c))
3481 {
3482 n = n * 10 + c - '0';
3483 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003484 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3487 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003488 if (typed > 0)
3489 {
3490 MSG_PUTS("\b \b");
3491 --typed;
3492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003495#ifdef FEAT_MOUSE
3496 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3497 {
3498 *mouse_used = TRUE;
3499 n = mouse_row + 1;
3500 break;
3501 }
3502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 else if (n == 0 && c == ':' && colon)
3504 {
3505 stuffcharReadbuff(':');
3506 if (!exmode_active)
3507 cmdline_row = msg_row;
3508 skip_redraw = TRUE; /* skip redraw once */
3509 do_redraw = FALSE;
3510 break;
3511 }
3512 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3513 break;
3514 }
3515 --no_mapping;
3516 --allow_keys;
3517 return n;
3518}
3519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003520/*
3521 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003522 * When "mouse_used" is not NULL allow using the mouse and in that case return
3523 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003524 */
3525 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003526prompt_for_number(mouse_used)
3527 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003528{
3529 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003530 int save_cmdline_row;
3531 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532
3533 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003534 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003535 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003536 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003537 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003538
Bram Moolenaar203335e2006-09-03 14:35:42 +00003539 /* Set the state such that text can be selected/copied/pasted and we still
3540 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003541 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003542 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003543 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003544 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003545
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003546 i = get_number(TRUE, mouse_used);
3547 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003548 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003549 /* don't call wait_return() now */
3550 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003551 cmdline_row = msg_row - 1;
3552 need_wait_return = FALSE;
3553 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003554 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003555 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003556 else
3557 cmdline_row = save_cmdline_row;
3558 State = save_State;
3559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560 return i;
3561}
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 void
3564msgmore(n)
3565 long n;
3566{
3567 long pn;
3568
3569 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3571 return;
3572
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003573 /* We don't want to overwrite another important message, but do overwrite
3574 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3575 * then "put" reports the last action. */
3576 if (keep_msg != NULL && !keep_msg_more)
3577 return;
3578
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (n > 0)
3580 pn = n;
3581 else
3582 pn = -n;
3583
3584 if (pn > p_report)
3585 {
3586 if (pn == 1)
3587 {
3588 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003589 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3590 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003592 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3593 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595 else
3596 {
3597 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003598 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3599 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003601 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3602 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
3604 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003605 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 if (msg(msg_buf))
3607 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003608 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003609 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611 }
3612}
3613
3614/*
3615 * flush map and typeahead buffers and give a warning for an error
3616 */
3617 void
3618beep_flush()
3619{
3620 if (emsg_silent == 0)
3621 {
3622 flush_buffers(FALSE);
3623 vim_beep();
3624 }
3625}
3626
3627/*
3628 * give a warning for an error
3629 */
3630 void
3631vim_beep()
3632{
3633 if (emsg_silent == 0)
3634 {
3635 if (p_vb
3636#ifdef FEAT_GUI
3637 /* While the GUI is starting up the termcap is set for the GUI
3638 * but the output still goes to a terminal. */
3639 && !(gui.in_use && gui.starting)
3640#endif
3641 )
3642 {
3643 out_str(T_VB);
3644 }
3645 else
3646 {
3647#ifdef MSDOS
3648 /*
3649 * The number of beeps outputted is reduced to avoid having to wait
3650 * for all the beeps to finish. This is only a problem on systems
3651 * where the beeps don't overlap.
3652 */
3653 if (beep_count == 0 || beep_count == 10)
3654 {
3655 out_char(BELL);
3656 beep_count = 1;
3657 }
3658 else
3659 ++beep_count;
3660#else
3661 out_char(BELL);
3662#endif
3663 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003664
3665 /* When 'verbose' is set and we are sourcing a script or executing a
3666 * function give the user a hint where the beep comes from. */
3667 if (vim_strchr(p_debug, 'e') != NULL)
3668 {
3669 msg_source(hl_attr(HLF_W));
3670 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673}
3674
3675/*
3676 * To get the "real" home directory:
3677 * - get value of $HOME
3678 * For Unix:
3679 * - go to that directory
3680 * - do mch_dirname() to get the real name of that directory.
3681 * This also works with mounts and links.
3682 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3683 */
3684static char_u *homedir = NULL;
3685
3686 void
3687init_homedir()
3688{
3689 char_u *var;
3690
Bram Moolenaar05159a02005-02-26 23:04:13 +00003691 /* In case we are called a second time (when 'encoding' changes). */
3692 vim_free(homedir);
3693 homedir = NULL;
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695#ifdef VMS
3696 var = mch_getenv((char_u *)"SYS$LOGIN");
3697#else
3698 var = mch_getenv((char_u *)"HOME");
3699#endif
3700
3701 if (var != NULL && *var == NUL) /* empty is same as not set */
3702 var = NULL;
3703
3704#ifdef WIN3264
3705 /*
3706 * Weird but true: $HOME may contain an indirect reference to another
3707 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3708 * when $HOME is being set.
3709 */
3710 if (var != NULL && *var == '%')
3711 {
3712 char_u *p;
3713 char_u *exp;
3714
3715 p = vim_strchr(var + 1, '%');
3716 if (p != NULL)
3717 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003718 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 exp = mch_getenv(NameBuff);
3720 if (exp != NULL && *exp != NUL
3721 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3722 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003723 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 var = NameBuff;
3725 /* Also set $HOME, it's needed for _viminfo. */
3726 vim_setenv((char_u *)"HOME", NameBuff);
3727 }
3728 }
3729 }
3730
3731 /*
3732 * Typically, $HOME is not defined on Windows, unless the user has
3733 * specifically defined it for Vim's sake. However, on Windows NT
3734 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3735 * each user. Try constructing $HOME from these.
3736 */
3737 if (var == NULL)
3738 {
3739 char_u *homedrive, *homepath;
3740
3741 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3742 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003743 if (homepath == NULL || *homepath == NUL)
3744 homepath = "\\";
3745 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3747 {
3748 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3749 if (NameBuff[0] != NUL)
3750 {
3751 var = NameBuff;
3752 /* Also set $HOME, it's needed for _viminfo. */
3753 vim_setenv((char_u *)"HOME", NameBuff);
3754 }
3755 }
3756 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003757
3758# if defined(FEAT_MBYTE)
3759 if (enc_utf8 && var != NULL)
3760 {
3761 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003762 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003763
3764 /* Convert from active codepage to UTF-8. Other conversions are
3765 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003766 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003767 if (pp != NULL)
3768 {
3769 homedir = pp;
3770 return;
3771 }
3772 }
3773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774#endif
3775
3776#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3777 /*
3778 * Default home dir is C:/
3779 * Best assumption we can make in such a situation.
3780 */
3781 if (var == NULL)
3782 var = "C:/";
3783#endif
3784 if (var != NULL)
3785 {
3786#ifdef UNIX
3787 /*
3788 * Change to the directory and get the actual path. This resolves
3789 * links. Don't do it when we can't return.
3790 */
3791 if (mch_dirname(NameBuff, MAXPATHL) == OK
3792 && mch_chdir((char *)NameBuff) == 0)
3793 {
3794 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3795 var = IObuff;
3796 if (mch_chdir((char *)NameBuff) != 0)
3797 EMSG(_(e_prev_dir));
3798 }
3799#endif
3800 homedir = vim_strsave(var);
3801 }
3802}
3803
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003804#if defined(EXITFREE) || defined(PROTO)
3805 void
3806free_homedir()
3807{
3808 vim_free(homedir);
3809}
Bram Moolenaar24305862012-08-15 14:05:05 +02003810
3811# ifdef FEAT_CMDL_COMPL
3812 void
3813free_users()
3814{
3815 ga_clear_strings(&ga_users);
3816}
3817# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003818#endif
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003821 * Call expand_env() and store the result in an allocated string.
3822 * This is not very memory efficient, this expects the result to be freed
3823 * again soon.
3824 */
3825 char_u *
3826expand_env_save(src)
3827 char_u *src;
3828{
3829 return expand_env_save_opt(src, FALSE);
3830}
3831
3832/*
3833 * Idem, but when "one" is TRUE handle the string as one file name, only
3834 * expand "~" at the start.
3835 */
3836 char_u *
3837expand_env_save_opt(src, one)
3838 char_u *src;
3839 int one;
3840{
3841 char_u *p;
3842
3843 p = alloc(MAXPATHL);
3844 if (p != NULL)
3845 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3846 return p;
3847}
3848
3849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 * Expand environment variable with path name.
3851 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003852 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 * If anything fails no expansion is done and dst equals src.
3854 */
3855 void
3856expand_env(src, dst, dstlen)
3857 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3858 char_u *dst; /* where to put the result */
3859 int dstlen; /* maximum length of the result */
3860{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003861 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862}
3863
3864 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003865expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003866 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 char_u *dst; /* where to put the result */
3868 int dstlen; /* maximum length of the result */
3869 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003870 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003871 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003873 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 char_u *tail;
3875 int c;
3876 char_u *var;
3877 int copy_char;
3878 int mustfree; /* var was allocated, need to free it later */
3879 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003880 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003882 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003883 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003884
3885 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 --dstlen; /* leave one char space for "\," */
3887 while (*src && dstlen > 0)
3888 {
3889 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003890 if ((*src == '$'
3891#ifdef VMS
3892 && at_start
3893#endif
3894 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3896 || *src == '%'
3897#endif
3898 || (*src == '~' && at_start))
3899 {
3900 mustfree = FALSE;
3901
3902 /*
3903 * The variable name is copied into dst temporarily, because it may
3904 * be a string in read-only memory and a NUL needs to be appended.
3905 */
3906 if (*src != '~') /* environment var */
3907 {
3908 tail = src + 1;
3909 var = dst;
3910 c = dstlen - 1;
3911
3912#ifdef UNIX
3913 /* Unix has ${var-name} type environment vars */
3914 if (*tail == '{' && !vim_isIDc('{'))
3915 {
3916 tail++; /* ignore '{' */
3917 while (c-- > 0 && *tail && *tail != '}')
3918 *var++ = *tail++;
3919 }
3920 else
3921#endif
3922 {
3923 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3924#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3925 || (*src == '%' && *tail != '%')
3926#endif
3927 ))
3928 {
3929#ifdef OS2 /* env vars only in uppercase */
3930 *var++ = TOUPPER_LOC(*tail);
3931 tail++; /* toupper() may be a macro! */
3932#else
3933 *var++ = *tail++;
3934#endif
3935 }
3936 }
3937
3938#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3939# ifdef UNIX
3940 if (src[1] == '{' && *tail != '}')
3941# else
3942 if (*src == '%' && *tail != '%')
3943# endif
3944 var = NULL;
3945 else
3946 {
3947# ifdef UNIX
3948 if (src[1] == '{')
3949# else
3950 if (*src == '%')
3951#endif
3952 ++tail;
3953#endif
3954 *var = NUL;
3955 var = vim_getenv(dst, &mustfree);
3956#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3957 }
3958#endif
3959 }
3960 /* home directory */
3961 else if ( src[1] == NUL
3962 || vim_ispathsep(src[1])
3963 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3964 {
3965 var = homedir;
3966 tail = src + 1;
3967 }
3968 else /* user directory */
3969 {
3970#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
3971 /*
3972 * Copy ~user to dst[], so we can put a NUL after it.
3973 */
3974 tail = src;
3975 var = dst;
3976 c = dstlen - 1;
3977 while ( c-- > 0
3978 && *tail
3979 && vim_isfilec(*tail)
3980 && !vim_ispathsep(*tail))
3981 *var++ = *tail++;
3982 *var = NUL;
3983# ifdef UNIX
3984 /*
3985 * If the system supports getpwnam(), use it.
3986 * Otherwise, or if getpwnam() fails, the shell is used to
3987 * expand ~user. This is slower and may fail if the shell
3988 * does not support ~user (old versions of /bin/sh).
3989 */
3990# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
3991 {
3992 struct passwd *pw;
3993
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003994 /* Note: memory allocated by getpwnam() is never freed.
3995 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 pw = getpwnam((char *)dst + 1);
3997 if (pw != NULL)
3998 var = (char_u *)pw->pw_dir;
3999 else
4000 var = NULL;
4001 }
4002 if (var == NULL)
4003# endif
4004 {
4005 expand_T xpc;
4006
4007 ExpandInit(&xpc);
4008 xpc.xp_context = EXPAND_FILES;
4009 var = ExpandOne(&xpc, dst, NULL,
4010 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 mustfree = TRUE;
4012 }
4013
4014# else /* !UNIX, thus VMS */
4015 /*
4016 * USER_HOME is a comma-separated list of
4017 * directories to search for the user account in.
4018 */
4019 {
4020 char_u test[MAXPATHL], paths[MAXPATHL];
4021 char_u *path, *next_path, *ptr;
4022 struct stat st;
4023
4024 STRCPY(paths, USER_HOME);
4025 next_path = paths;
4026 while (*next_path)
4027 {
4028 for (path = next_path; *next_path && *next_path != ',';
4029 next_path++);
4030 if (*next_path)
4031 *next_path++ = NUL;
4032 STRCPY(test, path);
4033 STRCAT(test, "/");
4034 STRCAT(test, dst + 1);
4035 if (mch_stat(test, &st) == 0)
4036 {
4037 var = alloc(STRLEN(test) + 1);
4038 STRCPY(var, test);
4039 mustfree = TRUE;
4040 break;
4041 }
4042 }
4043 }
4044# endif /* UNIX */
4045#else
4046 /* cannot expand user's home directory, so don't try */
4047 var = NULL;
4048 tail = (char_u *)""; /* for gcc */
4049#endif /* UNIX || VMS */
4050 }
4051
4052#ifdef BACKSLASH_IN_FILENAME
4053 /* If 'shellslash' is set change backslashes to forward slashes.
4054 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4055 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4056 {
4057 char_u *p = vim_strsave(var);
4058
4059 if (p != NULL)
4060 {
4061 if (mustfree)
4062 vim_free(var);
4063 var = p;
4064 mustfree = TRUE;
4065 forward_slash(var);
4066 }
4067 }
4068#endif
4069
4070 /* If "var" contains white space, escape it with a backslash.
4071 * Required for ":e ~/tt" when $HOME includes a space. */
4072 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4073 {
4074 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4075
4076 if (p != NULL)
4077 {
4078 if (mustfree)
4079 vim_free(var);
4080 var = p;
4081 mustfree = TRUE;
4082 }
4083 }
4084
4085 if (var != NULL && *var != NUL
4086 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4087 {
4088 STRCPY(dst, var);
4089 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004090 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 /* if var[] ends in a path separator and tail[] starts
4092 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004093 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4095 && dst[-1] != ':'
4096#endif
4097 && vim_ispathsep(*tail))
4098 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004099 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 src = tail;
4101 copy_char = FALSE;
4102 }
4103 if (mustfree)
4104 vim_free(var);
4105 }
4106
4107 if (copy_char) /* copy at least one char */
4108 {
4109 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004110 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004111 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4112 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 */
4114 at_start = FALSE;
4115 if (src[0] == '\\' && src[1] != NUL)
4116 {
4117 *dst++ = *src++;
4118 --dstlen;
4119 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004120 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 at_start = TRUE;
4122 *dst++ = *src++;
4123 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004124
4125 if (startstr != NULL && src - startstr_len >= srcp
4126 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4127 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 }
4130 *dst = NUL;
4131}
4132
4133/*
4134 * Vim's version of getenv().
4135 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004136 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004137 * "mustfree" is set to TRUE when returned is allocated, it must be
4138 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 */
4140 char_u *
4141vim_getenv(name, mustfree)
4142 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004143 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
4145 char_u *p;
4146 char_u *pend;
4147 int vimruntime;
4148
4149#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4150 /* use "C:/" when $HOME is not set */
4151 if (STRCMP(name, "HOME") == 0)
4152 return homedir;
4153#endif
4154
4155 p = mch_getenv(name);
4156 if (p != NULL && *p == NUL) /* empty is the same as not set */
4157 p = NULL;
4158
4159 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004160 {
4161#if defined(FEAT_MBYTE) && defined(WIN3264)
4162 if (enc_utf8)
4163 {
4164 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004165 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004166
4167 /* Convert from active codepage to UTF-8. Other conversions are
4168 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004169 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004170 if (pp != NULL)
4171 {
4172 p = pp;
4173 *mustfree = TRUE;
4174 }
4175 }
4176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
4180 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4181 if (!vimruntime && STRCMP(name, "VIM") != 0)
4182 return NULL;
4183
4184 /*
4185 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4186 * Don't do this when default_vimruntime_dir is non-empty.
4187 */
4188 if (vimruntime
4189#ifdef HAVE_PATHDEF
4190 && *default_vimruntime_dir == NUL
4191#endif
4192 )
4193 {
4194 p = mch_getenv((char_u *)"VIM");
4195 if (p != NULL && *p == NUL) /* empty is the same as not set */
4196 p = NULL;
4197 if (p != NULL)
4198 {
4199 p = vim_version_dir(p);
4200 if (p != NULL)
4201 *mustfree = TRUE;
4202 else
4203 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004204
4205#if defined(FEAT_MBYTE) && defined(WIN3264)
4206 if (enc_utf8)
4207 {
4208 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004209 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004210
4211 /* Convert from active codepage to UTF-8. Other conversions
4212 * are not done, because they would fail for non-ASCII
4213 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004214 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215 if (pp != NULL)
4216 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004217 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004218 vim_free(p);
4219 p = pp;
4220 *mustfree = TRUE;
4221 }
4222 }
4223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 }
4226
4227 /*
4228 * When expanding $VIM or $VIMRUNTIME fails, try using:
4229 * - the directory name from 'helpfile' (unless it contains '$')
4230 * - the executable name from argv[0]
4231 */
4232 if (p == NULL)
4233 {
4234 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4235 p = p_hf;
4236#ifdef USE_EXE_NAME
4237 /*
4238 * Use the name of the executable, obtained from argv[0].
4239 */
4240 else
4241 p = exe_name;
4242#endif
4243 if (p != NULL)
4244 {
4245 /* remove the file name */
4246 pend = gettail(p);
4247
4248 /* remove "doc/" from 'helpfile', if present */
4249 if (p == p_hf)
4250 pend = remove_tail(p, pend, (char_u *)"doc");
4251
4252#ifdef USE_EXE_NAME
4253# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004254 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 if (p == exe_name)
4256 {
4257 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004258 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004260 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4261 if (pend1 != pend)
4262 {
4263 pnew = alloc((unsigned)(pend1 - p) + 15);
4264 if (pnew != NULL)
4265 {
4266 STRNCPY(pnew, p, (pend1 - p));
4267 STRCPY(pnew + (pend1 - p), "Resources/vim");
4268 p = pnew;
4269 pend = p + STRLEN(p);
4270 }
4271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273# endif
4274 /* remove "src/" from exe_name, if present */
4275 if (p == exe_name)
4276 pend = remove_tail(p, pend, (char_u *)"src");
4277#endif
4278
4279 /* for $VIM, remove "runtime/" or "vim54/", if present */
4280 if (!vimruntime)
4281 {
4282 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4283 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4284 }
4285
4286 /* remove trailing path separator */
4287#ifndef MACOS_CLASSIC
4288 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004289 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004290 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 --pend;
4292#endif
4293
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004294#ifdef MACOS_X
4295 if (p == exe_name || p == p_hf)
4296#endif
4297 /* check that the result is a directory name */
4298 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
4300 if (p != NULL && !mch_isdir(p))
4301 {
4302 vim_free(p);
4303 p = NULL;
4304 }
4305 else
4306 {
4307#ifdef USE_EXE_NAME
4308 /* may add "/vim54" or "/runtime" if it exists */
4309 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4310 {
4311 vim_free(p);
4312 p = pend;
4313 }
4314#endif
4315 *mustfree = TRUE;
4316 }
4317 }
4318 }
4319
4320#ifdef HAVE_PATHDEF
4321 /* When there is a pathdef.c file we can use default_vim_dir and
4322 * default_vimruntime_dir */
4323 if (p == NULL)
4324 {
4325 /* Only use default_vimruntime_dir when it is not empty */
4326 if (vimruntime && *default_vimruntime_dir != NUL)
4327 {
4328 p = default_vimruntime_dir;
4329 *mustfree = FALSE;
4330 }
4331 else if (*default_vim_dir != NUL)
4332 {
4333 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4334 *mustfree = TRUE;
4335 else
4336 {
4337 p = default_vim_dir;
4338 *mustfree = FALSE;
4339 }
4340 }
4341 }
4342#endif
4343
4344 /*
4345 * Set the environment variable, so that the new value can be found fast
4346 * next time, and others can also use it (e.g. Perl).
4347 */
4348 if (p != NULL)
4349 {
4350 if (vimruntime)
4351 {
4352 vim_setenv((char_u *)"VIMRUNTIME", p);
4353 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355 else
4356 {
4357 vim_setenv((char_u *)"VIM", p);
4358 didset_vim = TRUE;
4359 }
4360 }
4361 return p;
4362}
4363
4364/*
4365 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4366 * Return NULL if not, return its name in allocated memory otherwise.
4367 */
4368 static char_u *
4369vim_version_dir(vimdir)
4370 char_u *vimdir;
4371{
4372 char_u *p;
4373
4374 if (vimdir == NULL || *vimdir == NUL)
4375 return NULL;
4376 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4377 if (p != NULL && mch_isdir(p))
4378 return p;
4379 vim_free(p);
4380 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4381 if (p != NULL && mch_isdir(p))
4382 return p;
4383 vim_free(p);
4384 return NULL;
4385}
4386
4387/*
4388 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4389 * the length of "name/". Otherwise return "pend".
4390 */
4391 static char_u *
4392remove_tail(p, pend, name)
4393 char_u *p;
4394 char_u *pend;
4395 char_u *name;
4396{
4397 int len = (int)STRLEN(name) + 1;
4398 char_u *newend = pend - len;
4399
4400 if (newend >= p
4401 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004402 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 return newend;
4404 return pend;
4405}
4406
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 * Our portable version of setenv.
4409 */
4410 void
4411vim_setenv(name, val)
4412 char_u *name;
4413 char_u *val;
4414{
4415#ifdef HAVE_SETENV
4416 mch_setenv((char *)name, (char *)val, 1);
4417#else
4418 char_u *envbuf;
4419
4420 /*
4421 * Putenv does not copy the string, it has to remain
4422 * valid. The allocated memory will never be freed.
4423 */
4424 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4425 if (envbuf != NULL)
4426 {
4427 sprintf((char *)envbuf, "%s=%s", name, val);
4428 putenv((char *)envbuf);
4429 }
4430#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004431#ifdef FEAT_GETTEXT
4432 /*
4433 * When setting $VIMRUNTIME adjust the directory to find message
4434 * translations to $VIMRUNTIME/lang.
4435 */
4436 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4437 {
4438 char_u *buf = concat_str(val, (char_u *)"/lang");
4439
4440 if (buf != NULL)
4441 {
4442 bindtextdomain(VIMPACKAGE, (char *)buf);
4443 vim_free(buf);
4444 }
4445 }
4446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447}
4448
4449#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4450/*
4451 * Function given to ExpandGeneric() to obtain an environment variable name.
4452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 char_u *
4454get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004455 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 int idx;
4457{
4458# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4459 /*
4460 * No environ[] on the Amiga and on the Mac (using MPW).
4461 */
4462 return NULL;
4463# else
4464# ifndef __WIN32__
4465 /* Borland C++ 5.2 has this in a header file. */
4466 extern char **environ;
4467# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004468# define ENVNAMELEN 100
4469 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 char_u *str;
4471 int n;
4472
4473 str = (char_u *)environ[idx];
4474 if (str == NULL)
4475 return NULL;
4476
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004477 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
4479 if (str[n] == '=' || str[n] == NUL)
4480 break;
4481 name[n] = str[n];
4482 }
4483 name[n] = NUL;
4484 return name;
4485# endif
4486}
Bram Moolenaar24305862012-08-15 14:05:05 +02004487
4488/*
4489 * Find all user names for user completion.
4490 * Done only once and then cached.
4491 */
4492 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004493init_users()
4494{
Bram Moolenaar24305862012-08-15 14:05:05 +02004495 static int lazy_init_done = FALSE;
4496
4497 if (lazy_init_done)
4498 return;
4499
4500 lazy_init_done = TRUE;
4501 ga_init2(&ga_users, sizeof(char_u *), 20);
4502
4503# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4504 {
4505 char_u* user;
4506 struct passwd* pw;
4507
4508 setpwent();
4509 while ((pw = getpwent()) != NULL)
4510 /* pw->pw_name shouldn't be NULL but just in case... */
4511 if (pw->pw_name != NULL)
4512 {
4513 if (ga_grow(&ga_users, 1) == FAIL)
4514 break;
4515 user = vim_strsave((char_u*)pw->pw_name);
4516 if (user == NULL)
4517 break;
4518 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4519 }
4520 endpwent();
4521 }
4522# endif
4523}
4524
4525/*
4526 * Function given to ExpandGeneric() to obtain an user names.
4527 */
4528 char_u*
4529get_users(xp, idx)
4530 expand_T *xp UNUSED;
4531 int idx;
4532{
4533 init_users();
4534 if (idx < ga_users.ga_len)
4535 return ((char_u **)ga_users.ga_data)[idx];
4536 return NULL;
4537}
4538
4539/*
4540 * Check whether name matches a user name. Return:
4541 * 0 if name does not match any user name.
4542 * 1 if name partially matches the beginning of a user name.
4543 * 2 is name fully matches a user name.
4544 */
4545int match_user(name)
4546 char_u* name;
4547{
4548 int i;
4549 int n = (int)STRLEN(name);
4550 int result = 0;
4551
4552 init_users();
4553 for (i = 0; i < ga_users.ga_len; i++)
4554 {
4555 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4556 return 2; /* full match */
4557 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4558 result = 1; /* partial match */
4559 }
4560 return result;
4561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562#endif
4563
4564/*
4565 * Replace home directory by "~" in each space or comma separated file name in
4566 * 'src'.
4567 * If anything fails (except when out of space) dst equals src.
4568 */
4569 void
4570home_replace(buf, src, dst, dstlen, one)
4571 buf_T *buf; /* when not NULL, check for help files */
4572 char_u *src; /* input file name */
4573 char_u *dst; /* where to put the result */
4574 int dstlen; /* maximum length of the result */
4575 int one; /* if TRUE, only replace one file name, include
4576 spaces and commas in the file name. */
4577{
4578 size_t dirlen = 0, envlen = 0;
4579 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004580 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 char_u *p;
4582
4583 if (src == NULL)
4584 {
4585 *dst = NUL;
4586 return;
4587 }
4588
4589 /*
4590 * If the file is a help file, remove the path completely.
4591 */
4592 if (buf != NULL && buf->b_help)
4593 {
4594 STRCPY(dst, gettail(src));
4595 return;
4596 }
4597
4598 /*
4599 * We check both the value of the $HOME environment variable and the
4600 * "real" home directory.
4601 */
4602 if (homedir != NULL)
4603 dirlen = STRLEN(homedir);
4604
4605#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004606 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004608 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4609#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004610 /* Empty is the same as not set. */
4611 if (homedir_env != NULL && *homedir_env == NUL)
4612 homedir_env = NULL;
4613
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004614#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004615 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004616 {
4617 int usedlen = 0;
4618 int flen;
4619 char_u *fbuf = NULL;
4620
4621 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004622 (void)modify_fname((char_u *)":p", &usedlen,
4623 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004624 flen = (int)STRLEN(homedir_env);
4625 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4626 /* Remove the trailing / that is added to a directory. */
4627 homedir_env[flen - 1] = NUL;
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629#endif
4630
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (homedir_env != NULL)
4632 envlen = STRLEN(homedir_env);
4633
4634 if (!one)
4635 src = skipwhite(src);
4636 while (*src && dstlen > 0)
4637 {
4638 /*
4639 * Here we are at the beginning of a file name.
4640 * First, check to see if the beginning of the file name matches
4641 * $HOME or the "real" home directory. Check that there is a '/'
4642 * after the match (so that if e.g. the file is "/home/pieter/bla",
4643 * and the home directory is "/home/piet", the file does not end up
4644 * as "~er/bla" (which would seem to indicate the file "bla" in user
4645 * er's home directory)).
4646 */
4647 p = homedir;
4648 len = dirlen;
4649 for (;;)
4650 {
4651 if ( len
4652 && fnamencmp(src, p, len) == 0
4653 && (vim_ispathsep(src[len])
4654 || (!one && (src[len] == ',' || src[len] == ' '))
4655 || src[len] == NUL))
4656 {
4657 src += len;
4658 if (--dstlen > 0)
4659 *dst++ = '~';
4660
4661 /*
4662 * If it's just the home directory, add "/".
4663 */
4664 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4665 *dst++ = '/';
4666 break;
4667 }
4668 if (p == homedir_env)
4669 break;
4670 p = homedir_env;
4671 len = envlen;
4672 }
4673
4674 /* if (!one) skip to separator: space or comma */
4675 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4676 *dst++ = *src++;
4677 /* skip separator */
4678 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4679 *dst++ = *src++;
4680 }
4681 /* if (dstlen == 0) out of space, what to do??? */
4682
4683 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004684
4685 if (homedir_env != homedir_env_orig)
4686 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687}
4688
4689/*
4690 * Like home_replace, store the replaced string in allocated memory.
4691 * When something fails, NULL is returned.
4692 */
4693 char_u *
4694home_replace_save(buf, src)
4695 buf_T *buf; /* when not NULL, check for help files */
4696 char_u *src; /* input file name */
4697{
4698 char_u *dst;
4699 unsigned len;
4700
4701 len = 3; /* space for "~/" and trailing NUL */
4702 if (src != NULL) /* just in case */
4703 len += (unsigned)STRLEN(src);
4704 dst = alloc(len);
4705 if (dst != NULL)
4706 home_replace(buf, src, dst, len, TRUE);
4707 return dst;
4708}
4709
4710/*
4711 * Compare two file names and return:
4712 * FPC_SAME if they both exist and are the same file.
4713 * FPC_SAMEX if they both don't exist and have the same file name.
4714 * FPC_DIFF if they both exist and are different files.
4715 * FPC_NOTX if they both don't exist.
4716 * FPC_DIFFX if one of them doesn't exist.
4717 * For the first name environment variables are expanded
4718 */
4719 int
4720fullpathcmp(s1, s2, checkname)
4721 char_u *s1, *s2;
4722 int checkname; /* when both don't exist, check file names */
4723{
4724#ifdef UNIX
4725 char_u exp1[MAXPATHL];
4726 char_u full1[MAXPATHL];
4727 char_u full2[MAXPATHL];
4728 struct stat st1, st2;
4729 int r1, r2;
4730
4731 expand_env(s1, exp1, MAXPATHL);
4732 r1 = mch_stat((char *)exp1, &st1);
4733 r2 = mch_stat((char *)s2, &st2);
4734 if (r1 != 0 && r2 != 0)
4735 {
4736 /* if mch_stat() doesn't work, may compare the names */
4737 if (checkname)
4738 {
4739 if (fnamecmp(exp1, s2) == 0)
4740 return FPC_SAMEX;
4741 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4742 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4743 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4744 return FPC_SAMEX;
4745 }
4746 return FPC_NOTX;
4747 }
4748 if (r1 != 0 || r2 != 0)
4749 return FPC_DIFFX;
4750 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4751 return FPC_SAME;
4752 return FPC_DIFF;
4753#else
4754 char_u *exp1; /* expanded s1 */
4755 char_u *full1; /* full path of s1 */
4756 char_u *full2; /* full path of s2 */
4757 int retval = FPC_DIFF;
4758 int r1, r2;
4759
4760 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4761 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4762 {
4763 full1 = exp1 + MAXPATHL;
4764 full2 = full1 + MAXPATHL;
4765
4766 expand_env(s1, exp1, MAXPATHL);
4767 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4768 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4769
4770 /* If vim_FullName() fails, the file probably doesn't exist. */
4771 if (r1 != OK && r2 != OK)
4772 {
4773 if (checkname && fnamecmp(exp1, s2) == 0)
4774 retval = FPC_SAMEX;
4775 else
4776 retval = FPC_NOTX;
4777 }
4778 else if (r1 != OK || r2 != OK)
4779 retval = FPC_DIFFX;
4780 else if (fnamecmp(full1, full2))
4781 retval = FPC_DIFF;
4782 else
4783 retval = FPC_SAME;
4784 vim_free(exp1);
4785 }
4786 return retval;
4787#endif
4788}
4789
4790/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004791 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004792 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004793 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 */
4795 char_u *
4796gettail(fname)
4797 char_u *fname;
4798{
4799 char_u *p1, *p2;
4800
4801 if (fname == NULL)
4802 return (char_u *)"";
4803 for (p1 = p2 = fname; *p2; ) /* find last part of path */
4804 {
4805 if (vim_ispathsep(*p2))
4806 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004807 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 return p1;
4810}
4811
Bram Moolenaar31710262010-08-13 13:36:15 +02004812#if defined(FEAT_SEARCHPATH)
4813static char_u *gettail_dir __ARGS((char_u *fname));
4814
4815/*
4816 * Return the end of the directory name, on the first path
4817 * separator:
4818 * "/path/file", "/path/dir/", "/path//dir", "/file"
4819 * ^ ^ ^ ^
4820 */
4821 static char_u *
4822gettail_dir(fname)
4823 char_u *fname;
4824{
4825 char_u *dir_end = fname;
4826 char_u *next_dir_end = fname;
4827 int look_for_sep = TRUE;
4828 char_u *p;
4829
4830 for (p = fname; *p != NUL; )
4831 {
4832 if (vim_ispathsep(*p))
4833 {
4834 if (look_for_sep)
4835 {
4836 next_dir_end = p;
4837 look_for_sep = FALSE;
4838 }
4839 }
4840 else
4841 {
4842 if (!look_for_sep)
4843 dir_end = next_dir_end;
4844 look_for_sep = TRUE;
4845 }
4846 mb_ptr_adv(p);
4847 }
4848 return dir_end;
4849}
4850#endif
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004853 * Get pointer to tail of "fname", including path separators. Putting a NUL
4854 * here leaves the directory name. Takes care of "c:/" and "//".
4855 * Always returns a valid pointer.
4856 */
4857 char_u *
4858gettail_sep(fname)
4859 char_u *fname;
4860{
4861 char_u *p;
4862 char_u *t;
4863
4864 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4865 t = gettail(fname);
4866 while (t > p && after_pathsep(fname, t))
4867 --t;
4868#ifdef VMS
4869 /* path separator is part of the path */
4870 ++t;
4871#endif
4872 return t;
4873}
4874
4875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 * get the next path component (just after the next path separator).
4877 */
4878 char_u *
4879getnextcomp(fname)
4880 char_u *fname;
4881{
4882 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004883 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 if (*fname)
4885 ++fname;
4886 return fname;
4887}
4888
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889/*
4890 * Get a pointer to one character past the head of a path name.
4891 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4892 * If there is no head, path is returned.
4893 */
4894 char_u *
4895get_past_head(path)
4896 char_u *path;
4897{
4898 char_u *retval;
4899
4900#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4901 /* may skip "c:" */
4902 if (isalpha(path[0]) && path[1] == ':')
4903 retval = path + 2;
4904 else
4905 retval = path;
4906#else
4907# if defined(AMIGA)
4908 /* may skip "label:" */
4909 retval = vim_strchr(path, ':');
4910 if (retval == NULL)
4911 retval = path;
4912# else /* Unix */
4913 retval = path;
4914# endif
4915#endif
4916
4917 while (vim_ispathsep(*retval))
4918 ++retval;
4919
4920 return retval;
4921}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923/*
4924 * return TRUE if 'c' is a path separator.
4925 */
4926 int
4927vim_ispathsep(c)
4928 int c;
4929{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004930#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004932#else
4933# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004935# else
4936# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4938 return (c == ':' || c == '[' || c == ']' || c == '/'
4939 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004940# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004942# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945}
4946
4947#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4948/*
4949 * return TRUE if 'c' is a path list separator.
4950 */
4951 int
4952vim_ispathlistsep(c)
4953 int c;
4954{
4955#ifdef UNIX
4956 return (c == ':');
4957#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004958 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959#endif
4960}
4961#endif
4962
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004963#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4964 || defined(FEAT_EVAL) || defined(PROTO)
4965/*
4966 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4967 * It's done in-place.
4968 */
4969 void
4970shorten_dir(str)
4971 char_u *str;
4972{
4973 char_u *tail, *s, *d;
4974 int skip = FALSE;
4975
4976 tail = gettail(str);
4977 d = str;
4978 for (s = str; ; ++s)
4979 {
4980 if (s >= tail) /* copy the whole tail */
4981 {
4982 *d++ = *s;
4983 if (*s == NUL)
4984 break;
4985 }
4986 else if (vim_ispathsep(*s)) /* copy '/' and next char */
4987 {
4988 *d++ = *s;
4989 skip = FALSE;
4990 }
4991 else if (!skip)
4992 {
4993 *d++ = *s; /* copy next char */
4994 if (*s != '~' && *s != '.') /* and leading "~" and "." */
4995 skip = TRUE;
4996# ifdef FEAT_MBYTE
4997 if (has_mbyte)
4998 {
4999 int l = mb_ptr2len(s);
5000
5001 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005002 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005003 }
5004# endif
5005 }
5006 }
5007}
5008#endif
5009
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005010/*
5011 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5012 * Also returns TRUE if there is no directory name.
5013 * "fname" must be writable!.
5014 */
5015 int
5016dir_of_file_exists(fname)
5017 char_u *fname;
5018{
5019 char_u *p;
5020 int c;
5021 int retval;
5022
5023 p = gettail_sep(fname);
5024 if (p == fname)
5025 return TRUE;
5026 c = *p;
5027 *p = NUL;
5028 retval = mch_isdir(fname);
5029 *p = c;
5030 return retval;
5031}
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005034 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5035 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 */
5037 int
5038vim_fnamecmp(x, y)
5039 char_u *x, *y;
5040{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005041#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005043#else
5044 if (p_fic)
5045 return MB_STRICMP(x, y);
5046 return STRCMP(x, y);
5047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048}
5049
5050 int
5051vim_fnamencmp(x, y, len)
5052 char_u *x, *y;
5053 size_t len;
5054{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005055#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005056 char_u *px = x;
5057 char_u *py = y;
5058 int cx = NUL;
5059 int cy = NUL;
5060
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005061 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005063 cx = PTR2CHAR(px);
5064 cy = PTR2CHAR(py);
5065 if (cx == NUL || cy == NUL
5066 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5067 && !(cx == '/' && cy == '\\')
5068 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005070 len -= MB_PTR2LEN(px);
5071 px += MB_PTR2LEN(px);
5072 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 if (len == 0)
5075 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005076 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005077#else
5078 if (p_fic)
5079 return MB_STRNICMP(x, y, len);
5080 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084/*
5085 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005086 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 */
5088 char_u *
5089concat_fnames(fname1, fname2, sep)
5090 char_u *fname1;
5091 char_u *fname2;
5092 int sep;
5093{
5094 char_u *dest;
5095
5096 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5097 if (dest != NULL)
5098 {
5099 STRCPY(dest, fname1);
5100 if (sep)
5101 add_pathsep(dest);
5102 STRCAT(dest, fname2);
5103 }
5104 return dest;
5105}
5106
Bram Moolenaard6754642005-01-17 22:18:45 +00005107/*
5108 * Concatenate two strings and return the result in allocated memory.
5109 * Returns NULL when out of memory.
5110 */
5111 char_u *
5112concat_str(str1, str2)
5113 char_u *str1;
5114 char_u *str2;
5115{
5116 char_u *dest;
5117 size_t l = STRLEN(str1);
5118
5119 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5120 if (dest != NULL)
5121 {
5122 STRCPY(dest, str1);
5123 STRCPY(dest + l, str2);
5124 }
5125 return dest;
5126}
Bram Moolenaard6754642005-01-17 22:18:45 +00005127
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128/*
5129 * Add a path separator to a file name, unless it already ends in a path
5130 * separator.
5131 */
5132 void
5133add_pathsep(p)
5134 char_u *p;
5135{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005136 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 STRCAT(p, PATHSEPSTR);
5138}
5139
5140/*
5141 * FullName_save - Make an allocated copy of a full file name.
5142 * Returns NULL when out of memory.
5143 */
5144 char_u *
5145FullName_save(fname, force)
5146 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005147 int force; /* force expansion, even when it already looks
5148 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149{
5150 char_u *buf;
5151 char_u *new_fname = NULL;
5152
5153 if (fname == NULL)
5154 return NULL;
5155
5156 buf = alloc((unsigned)MAXPATHL);
5157 if (buf != NULL)
5158 {
5159 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5160 new_fname = vim_strsave(buf);
5161 else
5162 new_fname = vim_strsave(fname);
5163 vim_free(buf);
5164 }
5165 return new_fname;
5166}
5167
5168#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5169
5170static char_u *skip_string __ARGS((char_u *p));
5171
5172/*
5173 * Find the start of a comment, not knowing if we are in a comment right now.
5174 * Search starts at w_cursor.lnum and goes backwards.
5175 */
5176 pos_T *
5177find_start_comment(ind_maxcomment) /* XXX */
5178 int ind_maxcomment;
5179{
5180 pos_T *pos;
5181 char_u *line;
5182 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005183 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005185 for (;;)
5186 {
5187 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5188 if (pos == NULL)
5189 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005191 /*
5192 * Check if the comment start we found is inside a string.
5193 * If it is then restrict the search to below this line and try again.
5194 */
5195 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005196 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005197 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005198 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005199 break;
5200 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5201 if (cur_maxcomment <= 0)
5202 {
5203 pos = NULL;
5204 break;
5205 }
5206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 return pos;
5208}
5209
5210/*
5211 * Skip to the end of a "string" and a 'c' character.
5212 * If there is no string or character, return argument unmodified.
5213 */
5214 static char_u *
5215skip_string(p)
5216 char_u *p;
5217{
5218 int i;
5219
5220 /*
5221 * We loop, because strings may be concatenated: "date""time".
5222 */
5223 for ( ; ; ++p)
5224 {
5225 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5226 {
5227 if (!p[1]) /* ' at end of line */
5228 break;
5229 i = 2;
5230 if (p[1] == '\\') /* '\n' or '\000' */
5231 {
5232 ++i;
5233 while (vim_isdigit(p[i - 1])) /* '\000' */
5234 ++i;
5235 }
5236 if (p[i] == '\'') /* check for trailing ' */
5237 {
5238 p += i;
5239 continue;
5240 }
5241 }
5242 else if (p[0] == '"') /* start of string */
5243 {
5244 for (++p; p[0]; ++p)
5245 {
5246 if (p[0] == '\\' && p[1] != NUL)
5247 ++p;
5248 else if (p[0] == '"') /* end of string */
5249 break;
5250 }
5251 if (p[0] == '"')
5252 continue;
5253 }
5254 break; /* no string found */
5255 }
5256 if (!*p)
5257 --p; /* backup from NUL */
5258 return p;
5259}
5260#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5261
5262#if defined(FEAT_CINDENT) || defined(PROTO)
5263
5264/*
5265 * Do C or expression indenting on the current line.
5266 */
5267 void
5268do_c_expr_indent()
5269{
5270# ifdef FEAT_EVAL
5271 if (*curbuf->b_p_inde != NUL)
5272 fixthisline(get_expr_indent);
5273 else
5274# endif
5275 fixthisline(get_c_indent);
5276}
5277
5278/*
5279 * Functions for C-indenting.
5280 * Most of this originally comes from Eric Fischer.
5281 */
5282/*
5283 * Below "XXX" means that this function may unlock the current line.
5284 */
5285
5286static char_u *cin_skipcomment __ARGS((char_u *));
5287static int cin_nocode __ARGS((char_u *));
5288static pos_T *find_line_comment __ARGS((void));
5289static int cin_islabel_skip __ARGS((char_u **));
5290static int cin_isdefault __ARGS((char_u *));
5291static char_u *after_label __ARGS((char_u *l));
5292static int get_indent_nolabel __ARGS((linenr_T lnum));
5293static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment));
5294static int cin_first_id_amount __ARGS((void));
5295static int cin_get_equal_amount __ARGS((linenr_T lnum));
5296static int cin_ispreproc __ARGS((char_u *));
5297static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5298static int cin_iscomment __ARGS((char_u *));
5299static int cin_islinecomment __ARGS((char_u *));
5300static int cin_isterminated __ARGS((char_u *, int, int));
5301static int cin_isinit __ARGS((void));
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005302static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303static int cin_isif __ARGS((char_u *));
5304static int cin_iselse __ARGS((char_u *));
5305static int cin_isdo __ARGS((char_u *));
5306static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005307static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005308static int cin_iswhileofdo_end __ARGS((int terminated, int ind_maxparen, int ind_maxcomment));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005310static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005311static int get_baseclass_amount __ARGS((int col, int ind_maxparen, int ind_maxcomment, int ind_cpp_baseclass));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005313static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314static int cin_skip2pos __ARGS((pos_T *trypos));
5315static pos_T *find_start_brace __ARGS((int));
5316static pos_T *find_match_paren __ARGS((int, int));
5317static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos));
5318static int find_last_paren __ARGS((char_u *l, int start, int end));
5319static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005320static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005322static int ind_hash_comment = 0; /* # starts a comment */
5323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324/*
5325 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005326 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 */
5328 static char_u *
5329cin_skipcomment(s)
5330 char_u *s;
5331{
5332 while (*s)
5333 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005334 char_u *prev_s = s;
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005337
5338 /* Perl/shell # comment comment continues until eol. Require a space
5339 * before # to avoid recognizing $#array. */
5340 if (ind_hash_comment != 0 && s != prev_s && *s == '#')
5341 {
5342 s += STRLEN(s);
5343 break;
5344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 if (*s != '/')
5346 break;
5347 ++s;
5348 if (*s == '/') /* slash-slash comment continues till eol */
5349 {
5350 s += STRLEN(s);
5351 break;
5352 }
5353 if (*s != '*')
5354 break;
5355 for (++s; *s; ++s) /* skip slash-star comment */
5356 if (s[0] == '*' && s[1] == '/')
5357 {
5358 s += 2;
5359 break;
5360 }
5361 }
5362 return s;
5363}
5364
5365/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005366 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 * not considered code.
5368 */
5369 static int
5370cin_nocode(s)
5371 char_u *s;
5372{
5373 return *cin_skipcomment(s) == NUL;
5374}
5375
5376/*
5377 * Check previous lines for a "//" line comment, skipping over blank lines.
5378 */
5379 static pos_T *
5380find_line_comment() /* XXX */
5381{
5382 static pos_T pos;
5383 char_u *line;
5384 char_u *p;
5385
5386 pos = curwin->w_cursor;
5387 while (--pos.lnum > 0)
5388 {
5389 line = ml_get(pos.lnum);
5390 p = skipwhite(line);
5391 if (cin_islinecomment(p))
5392 {
5393 pos.col = (int)(p - line);
5394 return &pos;
5395 }
5396 if (*p != NUL)
5397 break;
5398 }
5399 return NULL;
5400}
5401
5402/*
5403 * Check if string matches "label:"; move to character after ':' if true.
5404 */
5405 static int
5406cin_islabel_skip(s)
5407 char_u **s;
5408{
5409 if (!vim_isIDc(**s)) /* need at least one ID character */
5410 return FALSE;
5411
5412 while (vim_isIDc(**s))
5413 (*s)++;
5414
5415 *s = cin_skipcomment(*s);
5416
5417 /* "::" is not a label, it's C++ */
5418 return (**s == ':' && *++*s != ':');
5419}
5420
5421/*
5422 * Recognize a label: "label:".
5423 * Note: curwin->w_cursor must be where we are looking for the label.
5424 */
5425 int
5426cin_islabel(ind_maxcomment) /* XXX */
5427 int ind_maxcomment;
5428{
5429 char_u *s;
5430
5431 s = cin_skipcomment(ml_get_curline());
5432
5433 /*
5434 * Exclude "default" from labels, since it should be indented
5435 * like a switch label. Same for C++ scope declarations.
5436 */
5437 if (cin_isdefault(s))
5438 return FALSE;
5439 if (cin_isscopedecl(s))
5440 return FALSE;
5441
5442 if (cin_islabel_skip(&s))
5443 {
5444 /*
5445 * Only accept a label if the previous line is terminated or is a case
5446 * label.
5447 */
5448 pos_T cursor_save;
5449 pos_T *trypos;
5450 char_u *line;
5451
5452 cursor_save = curwin->w_cursor;
5453 while (curwin->w_cursor.lnum > 1)
5454 {
5455 --curwin->w_cursor.lnum;
5456
5457 /*
5458 * If we're in a comment now, skip to the start of the comment.
5459 */
5460 curwin->w_cursor.col = 0;
5461 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
5462 curwin->w_cursor = *trypos;
5463
5464 line = ml_get_curline();
5465 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5466 continue;
5467 if (*(line = cin_skipcomment(line)) == NUL)
5468 continue;
5469
5470 curwin->w_cursor = cursor_save;
5471 if (cin_isterminated(line, TRUE, FALSE)
5472 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005473 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 || (cin_islabel_skip(&line) && cin_nocode(line)))
5475 return TRUE;
5476 return FALSE;
5477 }
5478 curwin->w_cursor = cursor_save;
5479 return TRUE; /* label at start of file??? */
5480 }
5481 return FALSE;
5482}
5483
5484/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005485 * Recognize structure initialization and enumerations:
5486 * "[typedef] [static|public|protected|private] enum"
5487 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 */
5489 static int
5490cin_isinit(void)
5491{
5492 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005493 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 s = cin_skipcomment(ml_get_curline());
5496
Bram Moolenaar75342212013-03-07 13:13:52 +01005497 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 s = cin_skipcomment(s + 7);
5499
Bram Moolenaar75342212013-03-07 13:13:52 +01005500 for (;;)
5501 {
5502 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005503
Bram Moolenaar75342212013-03-07 13:13:52 +01005504 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5505 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005506 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005507 if (cin_starts_with(s, skip[i]))
5508 {
5509 s = cin_skipcomment(s + l);
5510 l = 0;
5511 break;
5512 }
5513 }
5514 if (l != 0)
5515 break;
5516 }
5517
5518 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 return TRUE;
5520
5521 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5522 return TRUE;
5523
5524 return FALSE;
5525}
5526
5527/*
5528 * Recognize a switch label: "case .*:" or "default:".
5529 */
5530 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005531cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005533 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005536 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 for (s += 4; *s; ++s)
5539 {
5540 s = cin_skipcomment(s);
5541 if (*s == ':')
5542 {
5543 if (s[1] == ':') /* skip over "::" for C++ */
5544 ++s;
5545 else
5546 return TRUE;
5547 }
5548 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005549 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5551 return FALSE; /* stop at comment */
5552 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005553 {
5554 /* JS etc. */
5555 if (strict)
5556 return FALSE; /* stop at string */
5557 else
5558 return TRUE;
5559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561 return FALSE;
5562 }
5563
5564 if (cin_isdefault(s))
5565 return TRUE;
5566 return FALSE;
5567}
5568
5569/*
5570 * Recognize a "default" switch label.
5571 */
5572 static int
5573cin_isdefault(s)
5574 char_u *s;
5575{
5576 return (STRNCMP(s, "default", 7) == 0
5577 && *(s = cin_skipcomment(s + 7)) == ':'
5578 && s[1] != ':');
5579}
5580
5581/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005582 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 */
5584 int
5585cin_isscopedecl(s)
5586 char_u *s;
5587{
5588 int i;
5589
5590 s = cin_skipcomment(s);
5591 if (STRNCMP(s, "public", 6) == 0)
5592 i = 6;
5593 else if (STRNCMP(s, "protected", 9) == 0)
5594 i = 9;
5595 else if (STRNCMP(s, "private", 7) == 0)
5596 i = 7;
5597 else
5598 return FALSE;
5599 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5600}
5601
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005602/* Maximum number of lines to search back for a "namespace" line. */
5603#define FIND_NAMESPACE_LIM 20
5604
5605/*
5606 * Recognize a "namespace" scope declaration.
5607 */
5608 static int
5609cin_is_cpp_namespace(s)
5610 char_u *s;
5611{
5612 char_u *p;
5613 int has_name = FALSE;
5614
5615 s = cin_skipcomment(s);
5616 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5617 {
5618 p = cin_skipcomment(skipwhite(s + 9));
5619 while (*p != NUL)
5620 {
5621 if (vim_iswhite(*p))
5622 {
5623 has_name = TRUE; /* found end of a name */
5624 p = cin_skipcomment(skipwhite(p));
5625 }
5626 else if (*p == '{')
5627 {
5628 break;
5629 }
5630 else if (vim_iswordc(*p))
5631 {
5632 if (has_name)
5633 return FALSE; /* word character after skipping past name */
5634 ++p;
5635 }
5636 else
5637 {
5638 return FALSE;
5639 }
5640 }
5641 return TRUE;
5642 }
5643 return FALSE;
5644}
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646/*
5647 * Return a pointer to the first non-empty non-comment character after a ':'.
5648 * Return NULL if not found.
5649 * case 234: a = b;
5650 * ^
5651 */
5652 static char_u *
5653after_label(l)
5654 char_u *l;
5655{
5656 for ( ; *l; ++l)
5657 {
5658 if (*l == ':')
5659 {
5660 if (l[1] == ':') /* skip over "::" for C++ */
5661 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005662 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 break;
5664 }
5665 else if (*l == '\'' && l[1] && l[2] == '\'')
5666 l += 2; /* skip over 'x' */
5667 }
5668 if (*l == NUL)
5669 return NULL;
5670 l = cin_skipcomment(l + 1);
5671 if (*l == NUL)
5672 return NULL;
5673 return l;
5674}
5675
5676/*
5677 * Get indent of line "lnum", skipping a label.
5678 * Return 0 if there is nothing after the label.
5679 */
5680 static int
5681get_indent_nolabel(lnum) /* XXX */
5682 linenr_T lnum;
5683{
5684 char_u *l;
5685 pos_T fp;
5686 colnr_T col;
5687 char_u *p;
5688
5689 l = ml_get(lnum);
5690 p = after_label(l);
5691 if (p == NULL)
5692 return 0;
5693
5694 fp.col = (colnr_T)(p - l);
5695 fp.lnum = lnum;
5696 getvcol(curwin, &fp, &col, NULL, NULL);
5697 return (int)col;
5698}
5699
5700/*
5701 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005702 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 * label: if (asdf && asdfasdf)
5704 * ^
5705 */
5706 static int
5707skip_label(lnum, pp, ind_maxcomment)
5708 linenr_T lnum;
5709 char_u **pp;
5710 int ind_maxcomment;
5711{
5712 char_u *l;
5713 int amount;
5714 pos_T cursor_save;
5715
5716 cursor_save = curwin->w_cursor;
5717 curwin->w_cursor.lnum = lnum;
5718 l = ml_get_curline();
5719 /* XXX */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005720 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)
5721 || cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 amount = get_indent_nolabel(lnum);
5724 l = after_label(ml_get_curline());
5725 if (l == NULL) /* just in case */
5726 l = ml_get_curline();
5727 }
5728 else
5729 {
5730 amount = get_indent();
5731 l = ml_get_curline();
5732 }
5733 *pp = l;
5734
5735 curwin->w_cursor = cursor_save;
5736 return amount;
5737}
5738
5739/*
5740 * Return the indent of the first variable name after a type in a declaration.
5741 * int a, indent of "a"
5742 * static struct foo b, indent of "b"
5743 * enum bla c, indent of "c"
5744 * Returns zero when it doesn't look like a declaration.
5745 */
5746 static int
5747cin_first_id_amount()
5748{
5749 char_u *line, *p, *s;
5750 int len;
5751 pos_T fp;
5752 colnr_T col;
5753
5754 line = ml_get_curline();
5755 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005756 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5758 {
5759 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005760 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5763 p = skipwhite(p + 6);
5764 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5765 p = skipwhite(p + 4);
5766 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5767 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5768 {
5769 s = skipwhite(p + len);
5770 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5771 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5772 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5773 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5774 p = s;
5775 }
5776 for (len = 0; vim_isIDc(p[len]); ++len)
5777 ;
5778 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5779 return 0;
5780
5781 p = skipwhite(p + len);
5782 fp.lnum = curwin->w_cursor.lnum;
5783 fp.col = (colnr_T)(p - line);
5784 getvcol(curwin, &fp, &col, NULL, NULL);
5785 return (int)col;
5786}
5787
5788/*
5789 * Return the indent of the first non-blank after an equal sign.
5790 * char *foo = "here";
5791 * Return zero if no (useful) equal sign found.
5792 * Return -1 if the line above "lnum" ends in a backslash.
5793 * foo = "asdf\
5794 * asdf\
5795 * here";
5796 */
5797 static int
5798cin_get_equal_amount(lnum)
5799 linenr_T lnum;
5800{
5801 char_u *line;
5802 char_u *s;
5803 colnr_T col;
5804 pos_T fp;
5805
5806 if (lnum > 1)
5807 {
5808 line = ml_get(lnum - 1);
5809 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5810 return -1;
5811 }
5812
5813 line = s = ml_get(lnum);
5814 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5815 {
5816 if (cin_iscomment(s)) /* ignore comments */
5817 s = cin_skipcomment(s);
5818 else
5819 ++s;
5820 }
5821 if (*s != '=')
5822 return 0;
5823
5824 s = skipwhite(s + 1);
5825 if (cin_nocode(s))
5826 return 0;
5827
5828 if (*s == '"') /* nice alignment for continued strings */
5829 ++s;
5830
5831 fp.lnum = lnum;
5832 fp.col = (colnr_T)(s - line);
5833 getvcol(curwin, &fp, &col, NULL, NULL);
5834 return (int)col;
5835}
5836
5837/*
5838 * Recognize a preprocessor statement: Any line that starts with '#'.
5839 */
5840 static int
5841cin_ispreproc(s)
5842 char_u *s;
5843{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005844 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 return TRUE;
5846 return FALSE;
5847}
5848
5849/*
5850 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5851 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5852 * start and return the line in "*pp".
5853 */
5854 static int
5855cin_ispreproc_cont(pp, lnump)
5856 char_u **pp;
5857 linenr_T *lnump;
5858{
5859 char_u *line = *pp;
5860 linenr_T lnum = *lnump;
5861 int retval = FALSE;
5862
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005863 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {
5865 if (cin_ispreproc(line))
5866 {
5867 retval = TRUE;
5868 *lnump = lnum;
5869 break;
5870 }
5871 if (lnum == 1)
5872 break;
5873 line = ml_get(--lnum);
5874 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5875 break;
5876 }
5877
5878 if (lnum != *lnump)
5879 *pp = ml_get(*lnump);
5880 return retval;
5881}
5882
5883/*
5884 * Recognize the start of a C or C++ comment.
5885 */
5886 static int
5887cin_iscomment(p)
5888 char_u *p;
5889{
5890 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5891}
5892
5893/*
5894 * Recognize the start of a "//" comment.
5895 */
5896 static int
5897cin_islinecomment(p)
5898 char_u *p;
5899{
5900 return (p[0] == '/' && p[1] == '/');
5901}
5902
5903/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005904 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5905 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005907 * If a line begins with an "else", only consider it terminated if no unmatched
5908 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 * Return the character terminating the line (ending char's have precedence if
5910 * both apply in order to determine initializations).
5911 */
5912 static int
5913cin_isterminated(s, incl_open, incl_comma)
5914 char_u *s;
5915 int incl_open; /* include '{' at the end as terminator */
5916 int incl_comma; /* recognize a trailing comma */
5917{
Bram Moolenaar496f9512011-05-19 16:35:09 +02005918 char_u found_start = 0;
5919 unsigned n_open = 0;
5920 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
5922 s = cin_skipcomment(s);
5923
5924 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
5925 found_start = *s;
5926
Bram Moolenaar496f9512011-05-19 16:35:09 +02005927 if (!found_start)
5928 is_else = cin_iselse(s);
5929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 while (*s)
5931 {
5932 /* skip over comments, "" strings and 'c'haracters */
5933 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005934 if (*s == '}' && n_open > 0)
5935 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02005936 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005937 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 && cin_nocode(s + 1))
5939 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005940 else if (*s == '{')
5941 {
5942 if (incl_open && cin_nocode(s + 1))
5943 return *s;
5944 else
5945 ++n_open;
5946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948 if (*s)
5949 s++;
5950 }
5951 return found_start;
5952}
5953
5954/*
5955 * Recognize the basic picture of a function declaration -- it needs to
5956 * have an open paren somewhere and a close paren at the end of the line and
5957 * no semicolons anywhere.
5958 * When a line ends in a comma we continue looking in the next line.
5959 * "sp" points to a string with the line. When looking at other lines it must
5960 * be restored to the line. When it's NULL fetch lines here.
5961 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005962 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 */
5964 static int
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005965cin_isfuncdecl(sp, first_lnum, min_lnum, ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 char_u **sp;
5967 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005968 linenr_T min_lnum;
5969 int ind_maxparen;
5970 int ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
5972 char_u *s;
5973 linenr_T lnum = first_lnum;
5974 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005975 pos_T *trypos;
5976 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 if (sp == NULL)
5979 s = ml_get(lnum);
5980 else
5981 s = *sp;
5982
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005983 if (find_last_paren(s, '(', ')')
5984 && (trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL)
5985 {
5986 lnum = trypos->lnum;
5987 if (lnum < min_lnum)
5988 return FALSE;
5989
5990 s = ml_get(lnum);
5991 }
5992
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005993 /* Ignore line starting with #. */
5994 if (cin_ispreproc(s))
5995 return FALSE;
5996
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
5998 {
5999 if (cin_iscomment(s)) /* ignore comments */
6000 s = cin_skipcomment(s);
6001 else
6002 ++s;
6003 }
6004 if (*s != '(')
6005 return FALSE; /* ';', ' or " before any () or no '(' */
6006
6007 while (*s && *s != ';' && *s != '\'' && *s != '"')
6008 {
6009 if (*s == ')' && cin_nocode(s + 1))
6010 {
6011 /* ')' at the end: may have found a match
6012 * Check for he previous line not to end in a backslash:
6013 * #if defined(x) && \
6014 * defined(y)
6015 */
6016 lnum = first_lnum - 1;
6017 s = ml_get(lnum);
6018 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6019 retval = TRUE;
6020 goto done;
6021 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006022 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006024 int comma = (*s == ',');
6025
6026 /* ',' at the end: continue looking in the next line.
6027 * At the end: check for ',' in the next line, for this style:
6028 * func(arg1
6029 * , arg2) */
6030 for (;;)
6031 {
6032 if (lnum >= curbuf->b_ml.ml_line_count)
6033 break;
6034 s = ml_get(++lnum);
6035 if (!cin_ispreproc(s))
6036 break;
6037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 if (lnum >= curbuf->b_ml.ml_line_count)
6039 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006040 /* Require a comma at end of the line or a comma or ')' at the
6041 * start of next line. */
6042 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006043 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006044 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006045 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 }
6047 else if (cin_iscomment(s)) /* ignore comments */
6048 s = cin_skipcomment(s);
6049 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006052 just_started = FALSE;
6053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 }
6055
6056done:
6057 if (lnum != first_lnum && sp != NULL)
6058 *sp = ml_get(first_lnum);
6059
6060 return retval;
6061}
6062
6063 static int
6064cin_isif(p)
6065 char_u *p;
6066{
6067 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6068}
6069
6070 static int
6071cin_iselse(p)
6072 char_u *p;
6073{
6074 if (*p == '}') /* accept "} else" */
6075 p = cin_skipcomment(p + 1);
6076 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6077}
6078
6079 static int
6080cin_isdo(p)
6081 char_u *p;
6082{
6083 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6084}
6085
6086/*
6087 * Check if this is a "while" that should have a matching "do".
6088 * We only accept a "while (condition) ;", with only white space between the
6089 * ')' and ';'. The condition may be spread over several lines.
6090 */
6091 static int
6092cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */
6093 char_u *p;
6094 linenr_T lnum;
6095 int ind_maxparen;
6096{
6097 pos_T cursor_save;
6098 pos_T *trypos;
6099 int retval = FALSE;
6100
6101 p = cin_skipcomment(p);
6102 if (*p == '}') /* accept "} while (cond);" */
6103 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006104 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
6106 cursor_save = curwin->w_cursor;
6107 curwin->w_cursor.lnum = lnum;
6108 curwin->w_cursor.col = 0;
6109 p = ml_get_curline();
6110 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6111 {
6112 ++p;
6113 ++curwin->w_cursor.col;
6114 }
6115 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL
6116 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6117 retval = TRUE;
6118 curwin->w_cursor = cursor_save;
6119 }
6120 return retval;
6121}
6122
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006123/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006124 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006125 * Return 0 if there is none.
6126 * Otherwise return !0 and update "*poffset" to point to the place where the
6127 * string was found.
6128 */
6129 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006130cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006131 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006132 int *poffset;
6133{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006134 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006135
6136 if (offset-- < 2)
6137 return 0;
6138 while (offset > 2 && vim_iswhite(line[offset]))
6139 --offset;
6140
6141 offset -= 1;
6142 if (!STRNCMP(line + offset, "if", 2))
6143 goto probablyFound;
6144
6145 if (offset >= 1)
6146 {
6147 offset -= 1;
6148 if (!STRNCMP(line + offset, "for", 3))
6149 goto probablyFound;
6150
6151 if (offset >= 2)
6152 {
6153 offset -= 2;
6154 if (!STRNCMP(line + offset, "while", 5))
6155 goto probablyFound;
6156 }
6157 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006158 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006159
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006160probablyFound:
6161 if (!offset || !vim_isIDc(line[offset - 1]))
6162 {
6163 *poffset = offset;
6164 return 1;
6165 }
6166 return 0;
6167}
6168
6169/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006170 * Return TRUE if we are at the end of a do-while.
6171 * do
6172 * nothing;
6173 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006174 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006175 * Adjust the cursor to the line with "while".
6176 */
6177 static int
6178cin_iswhileofdo_end(terminated, ind_maxparen, ind_maxcomment)
6179 int terminated;
6180 int ind_maxparen;
6181 int ind_maxcomment;
6182{
6183 char_u *line;
6184 char_u *p;
6185 char_u *s;
6186 pos_T *trypos;
6187 int i;
6188
6189 if (terminated != ';') /* there must be a ';' at the end */
6190 return FALSE;
6191
6192 p = line = ml_get_curline();
6193 while (*p != NUL)
6194 {
6195 p = cin_skipcomment(p);
6196 if (*p == ')')
6197 {
6198 s = skipwhite(p + 1);
6199 if (*s == ';' && cin_nocode(s + 1))
6200 {
6201 /* Found ");" at end of the line, now check there is "while"
6202 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006203 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006204 curwin->w_cursor.col = i;
6205 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
6206 if (trypos != NULL)
6207 {
6208 s = cin_skipcomment(ml_get(trypos->lnum));
6209 if (*s == '}') /* accept "} while (cond);" */
6210 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006211 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006212 {
6213 curwin->w_cursor.lnum = trypos->lnum;
6214 return TRUE;
6215 }
6216 }
6217
6218 /* Searching may have made "line" invalid, get it again. */
6219 line = ml_get_curline();
6220 p = line + i;
6221 }
6222 }
6223 if (*p != NUL)
6224 ++p;
6225 }
6226 return FALSE;
6227}
6228
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 static int
6230cin_isbreak(p)
6231 char_u *p;
6232{
6233 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6234}
6235
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006236/*
6237 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 * constructor-initialization. eg:
6239 *
6240 * class MyClass :
6241 * baseClass <-- here
6242 * class MyClass : public baseClass,
6243 * anotherBaseClass <-- here (should probably lineup ??)
6244 * MyClass::MyClass(...) :
6245 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006246 *
6247 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 */
6249 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006250cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006251 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 char_u *s;
6254 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006255 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006256 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 *col = 0;
6259
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006260 s = skipwhite(line);
6261 if (*s == '#') /* skip #define FOO x ? (x) : x */
6262 return FALSE;
6263 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (*s == NUL)
6265 return FALSE;
6266
6267 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6268
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006269 /* Search for a line starting with '#', empty, ending in ';' or containing
6270 * '{' or '}' and start below it. This handles the following situations:
6271 * a = cond ?
6272 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006273 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006274 * func::foo()
6275 * : something
6276 * {}
6277 * Foo::Foo (int one, int two)
6278 * : something(4),
6279 * somethingelse(3)
6280 * {}
6281 */
6282 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006284 line = ml_get(lnum - 1);
6285 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006286 if (*s == '#' || *s == NUL)
6287 break;
6288 while (*s != NUL)
6289 {
6290 s = cin_skipcomment(s);
6291 if (*s == '{' || *s == '}'
6292 || (*s == ';' && cin_nocode(s + 1)))
6293 break;
6294 if (*s != NUL)
6295 ++s;
6296 }
6297 if (*s != NUL)
6298 break;
6299 --lnum;
6300 }
6301
Bram Moolenaare7c56862007-08-04 10:14:52 +00006302 line = ml_get(lnum);
6303 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006304 for (;;)
6305 {
6306 if (*s == NUL)
6307 {
6308 if (lnum == curwin->w_cursor.lnum)
6309 break;
6310 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006311 line = ml_get(++lnum);
6312 s = cin_skipcomment(line);
6313 if (*s == NUL)
6314 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006315 }
6316
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006317 if (s[0] == '"')
6318 s = skip_string(s) + 1;
6319 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
6321 if (s[1] == ':')
6322 {
6323 /* skip double colon. It can't be a constructor
6324 * initialization any more */
6325 lookfor_ctor_init = FALSE;
6326 s = cin_skipcomment(s + 2);
6327 }
6328 else if (lookfor_ctor_init || class_or_struct)
6329 {
6330 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006331 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 cpp_base_class = TRUE;
6333 lookfor_ctor_init = class_or_struct = FALSE;
6334 *col = 0;
6335 s = cin_skipcomment(s + 1);
6336 }
6337 else
6338 s = cin_skipcomment(s + 1);
6339 }
6340 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6341 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6342 {
6343 class_or_struct = TRUE;
6344 lookfor_ctor_init = FALSE;
6345
6346 if (*s == 'c')
6347 s = cin_skipcomment(s + 5);
6348 else
6349 s = cin_skipcomment(s + 6);
6350 }
6351 else
6352 {
6353 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6354 {
6355 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6356 }
6357 else if (s[0] == ')')
6358 {
6359 /* Constructor-initialization is assumed if we come across
6360 * something like "):" */
6361 class_or_struct = FALSE;
6362 lookfor_ctor_init = TRUE;
6363 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006364 else if (s[0] == '?')
6365 {
6366 /* Avoid seeing '() :' after '?' as constructor init. */
6367 return FALSE;
6368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 else if (!vim_isIDc(s[0]))
6370 {
6371 /* if it is not an identifier, we are wrong */
6372 class_or_struct = FALSE;
6373 lookfor_ctor_init = FALSE;
6374 }
6375 else if (*col == 0)
6376 {
6377 /* it can't be a constructor-initialization any more */
6378 lookfor_ctor_init = FALSE;
6379
6380 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006381 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 *col = (colnr_T)(s - line);
6383 }
6384
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006385 /* When the line ends in a comma don't align with it. */
6386 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6387 *col = 0;
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 s = cin_skipcomment(s + 1);
6390 }
6391 }
6392
6393 return cpp_base_class;
6394}
6395
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006396 static int
6397get_baseclass_amount(col, ind_maxparen, ind_maxcomment, ind_cpp_baseclass)
6398 int col;
6399 int ind_maxparen;
6400 int ind_maxcomment;
6401 int ind_cpp_baseclass;
6402{
6403 int amount;
6404 colnr_T vcol;
6405 pos_T *trypos;
6406
6407 if (col == 0)
6408 {
6409 amount = get_indent();
6410 if (find_last_paren(ml_get_curline(), '(', ')')
6411 && (trypos = find_match_paren(ind_maxparen,
6412 ind_maxcomment)) != NULL)
6413 amount = get_indent_lnum(trypos->lnum); /* XXX */
6414 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
6415 amount += ind_cpp_baseclass;
6416 }
6417 else
6418 {
6419 curwin->w_cursor.col = col;
6420 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6421 amount = (int)vcol;
6422 }
6423 if (amount < ind_cpp_baseclass)
6424 amount = ind_cpp_baseclass;
6425 return amount;
6426}
6427
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428/*
6429 * Return TRUE if string "s" ends with the string "find", possibly followed by
6430 * white space and comments. Skip strings and comments.
6431 * Ignore "ignore" after "find" if it's not NULL.
6432 */
6433 static int
6434cin_ends_in(s, find, ignore)
6435 char_u *s;
6436 char_u *find;
6437 char_u *ignore;
6438{
6439 char_u *p = s;
6440 char_u *r;
6441 int len = (int)STRLEN(find);
6442
6443 while (*p != NUL)
6444 {
6445 p = cin_skipcomment(p);
6446 if (STRNCMP(p, find, len) == 0)
6447 {
6448 r = skipwhite(p + len);
6449 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6450 r = skipwhite(r + STRLEN(ignore));
6451 if (cin_nocode(r))
6452 return TRUE;
6453 }
6454 if (*p != NUL)
6455 ++p;
6456 }
6457 return FALSE;
6458}
6459
6460/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006461 * Return TRUE when "s" starts with "word" and then a non-ID character.
6462 */
6463 static int
6464cin_starts_with(s, word)
6465 char_u *s;
6466 char *word;
6467{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006468 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006469
6470 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6471}
6472
6473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 * Skip strings, chars and comments until at or past "trypos".
6475 * Return the column found.
6476 */
6477 static int
6478cin_skip2pos(trypos)
6479 pos_T *trypos;
6480{
6481 char_u *line;
6482 char_u *p;
6483
6484 p = line = ml_get(trypos->lnum);
6485 while (*p && (colnr_T)(p - line) < trypos->col)
6486 {
6487 if (cin_iscomment(p))
6488 p = cin_skipcomment(p);
6489 else
6490 {
6491 p = skip_string(p);
6492 ++p;
6493 }
6494 }
6495 return (int)(p - line);
6496}
6497
6498/*
6499 * Find the '{' at the start of the block we are in.
6500 * Return NULL if no match found.
6501 * Ignore a '{' that is in a comment, makes indenting the next three lines
6502 * work. */
6503/* foo() */
6504/* { */
6505/* } */
6506
6507 static pos_T *
6508find_start_brace(ind_maxcomment) /* XXX */
6509 int ind_maxcomment;
6510{
6511 pos_T cursor_save;
6512 pos_T *trypos;
6513 pos_T *pos;
6514 static pos_T pos_copy;
6515
6516 cursor_save = curwin->w_cursor;
6517 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6518 {
6519 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6520 trypos = &pos_copy;
6521 curwin->w_cursor = *trypos;
6522 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006523 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
6525 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */
6526 break;
6527 if (pos != NULL)
6528 curwin->w_cursor.lnum = pos->lnum;
6529 }
6530 curwin->w_cursor = cursor_save;
6531 return trypos;
6532}
6533
6534/*
6535 * Find the matching '(', failing if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006536 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 */
6538 static pos_T *
6539find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */
6540 int ind_maxparen;
6541 int ind_maxcomment;
6542{
6543 pos_T cursor_save;
6544 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006545 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547 cursor_save = curwin->w_cursor;
6548 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6549 {
6550 /* check if the ( is in a // comment */
6551 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6552 trypos = NULL;
6553 else
6554 {
6555 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6556 trypos = &pos_copy;
6557 curwin->w_cursor = *trypos;
6558 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */
6559 trypos = NULL;
6560 }
6561 }
6562 curwin->w_cursor = cursor_save;
6563 return trypos;
6564}
6565
6566/*
6567 * Return ind_maxparen corrected for the difference in line number between the
6568 * cursor position and "startpos". This makes sure that searching for a
6569 * matching paren above the cursor line doesn't find a match because of
6570 * looking a few lines further.
6571 */
6572 static int
6573corr_ind_maxparen(ind_maxparen, startpos)
6574 int ind_maxparen;
6575 pos_T *startpos;
6576{
6577 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6578
6579 if (n > 0 && n < ind_maxparen / 2)
6580 return ind_maxparen - (int)n;
6581 return ind_maxparen;
6582}
6583
6584/*
6585 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006586 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 */
6588 static int
6589find_last_paren(l, start, end)
6590 char_u *l;
6591 int start, end;
6592{
6593 int i;
6594 int retval = FALSE;
6595 int open_count = 0;
6596
6597 curwin->w_cursor.col = 0; /* default is start of line */
6598
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006599 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
6601 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6602 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6603 if (l[i] == start)
6604 ++open_count;
6605 else if (l[i] == end)
6606 {
6607 if (open_count > 0)
6608 --open_count;
6609 else
6610 {
6611 curwin->w_cursor.col = i;
6612 retval = TRUE;
6613 }
6614 }
6615 }
6616 return retval;
6617}
6618
6619 int
6620get_c_indent()
6621{
Bram Moolenaar14f24742012-08-08 18:01:05 +02006622 int sw = (int)get_sw_value();
6623
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 /*
6625 * spaces from a block's opening brace the prevailing indent for that
6626 * block should be
6627 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006628
6629 int ind_level = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630
6631 /*
6632 * spaces from the edge of the line an open brace that's at the end of a
6633 * line is imagined to be.
6634 */
6635 int ind_open_imag = 0;
6636
6637 /*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006638 * spaces from the prevailing indent for a line that is not preceded by
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 * an opening brace.
6640 */
6641 int ind_no_brace = 0;
6642
6643 /*
6644 * column where the first { of a function should be located }
6645 */
6646 int ind_first_open = 0;
6647
6648 /*
6649 * spaces from the prevailing indent a leftmost open brace should be
6650 * located
6651 */
6652 int ind_open_extra = 0;
6653
6654 /*
6655 * spaces from the matching open brace (real location for one at the left
6656 * edge; imaginary location from one that ends a line) the matching close
6657 * brace should be located
6658 */
6659 int ind_close_extra = 0;
6660
6661 /*
6662 * spaces from the edge of the line an open brace sitting in the leftmost
6663 * column is imagined to be
6664 */
6665 int ind_open_left_imag = 0;
6666
6667 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006668 * Spaces jump labels should be shifted to the left if N is non-negative,
6669 * otherwise the jump label will be put to column 1.
6670 */
6671 int ind_jump_label = -1;
6672
6673 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 * spaces from the switch() indent a "case xx" label should be located
6675 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006676 int ind_case = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
6678 /*
6679 * spaces from the "case xx:" code after a switch() should be located
6680 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006681 int ind_case_code = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683 /*
6684 * lineup break at end of case in switch() with case label
6685 */
6686 int ind_case_break = 0;
6687
6688 /*
6689 * spaces from the class declaration indent a scope declaration label
6690 * should be located
6691 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006692 int ind_scopedecl = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694 /*
6695 * spaces from the scope declaration label code should be located
6696 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006697 int ind_scopedecl_code = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
6699 /*
6700 * amount K&R-style parameters should be indented
6701 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006702 int ind_param = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704 /*
6705 * amount a function type spec should be indented
6706 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006707 int ind_func_type = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709 /*
6710 * amount a cpp base class declaration or constructor initialization
6711 * should be indented
6712 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006713 int ind_cpp_baseclass = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
6715 /*
6716 * additional spaces beyond the prevailing indent a continuation line
6717 * should be located
6718 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006719 int ind_continuation = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 /*
6722 * spaces from the indent of the line with an unclosed parentheses
6723 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006724 int ind_unclosed = sw * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726 /*
6727 * spaces from the indent of the line with an unclosed parentheses, which
6728 * itself is also unclosed
6729 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006730 int ind_unclosed2 = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731
6732 /*
6733 * suppress ignoring spaces from the indent of a line starting with an
6734 * unclosed parentheses.
6735 */
6736 int ind_unclosed_noignore = 0;
6737
6738 /*
6739 * If the opening paren is the last nonwhite character on the line, and
6740 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6741 * context (for very long lines).
6742 */
6743 int ind_unclosed_wrapped = 0;
6744
6745 /*
6746 * suppress ignoring white space when lining up with the character after
6747 * an unclosed parentheses.
6748 */
6749 int ind_unclosed_whiteok = 0;
6750
6751 /*
6752 * indent a closing parentheses under the line start of the matching
6753 * opening parentheses.
6754 */
6755 int ind_matching_paren = 0;
6756
6757 /*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006758 * indent a closing parentheses under the previous line.
6759 */
6760 int ind_paren_prev = 0;
6761
6762 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 * Extra indent for comments.
6764 */
6765 int ind_comment = 0;
6766
6767 /*
6768 * spaces from the comment opener when there is nothing after it.
6769 */
6770 int ind_in_comment = 3;
6771
6772 /*
6773 * boolean: if non-zero, use ind_in_comment even if there is something
6774 * after the comment opener.
6775 */
6776 int ind_in_comment2 = 0;
6777
6778 /*
6779 * max lines to search for an open paren
6780 */
6781 int ind_maxparen = 20;
6782
6783 /*
6784 * max lines to search for an open comment
6785 */
6786 int ind_maxcomment = 70;
6787
6788 /*
6789 * handle braces for java code
6790 */
6791 int ind_java = 0;
6792
6793 /*
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006794 * not to confuse JS object properties with labels
6795 */
6796 int ind_js = 0;
6797
6798 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 * handle blocked cases correctly
6800 */
6801 int ind_keep_case_label = 0;
6802
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006803 /*
6804 * handle C++ namespace
6805 */
6806 int ind_cpp_namespace = 0;
6807
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006808 /*
6809 * handle continuation lines containing conditions of if(), for() and
6810 * while()
6811 */
6812 int ind_if_for_while = 0;
6813
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 pos_T cur_curpos;
6815 int amount;
6816 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006817 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 colnr_T col;
6819 char_u *theline;
6820 char_u *linecopy;
6821 pos_T *trypos;
6822 pos_T *tryposBrace = NULL;
6823 pos_T our_paren_pos;
6824 char_u *start;
6825 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006826#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827#define BRACE_AT_START 2 /* '{' is at start of line */
6828#define BRACE_AT_END 3 /* '{' is at end of line */
6829 linenr_T ourscope;
6830 char_u *l;
6831 char_u *look;
6832 char_u terminated;
6833 int lookfor;
6834#define LOOKFOR_INITIAL 0
6835#define LOOKFOR_IF 1
6836#define LOOKFOR_DO 2
6837#define LOOKFOR_CASE 3
6838#define LOOKFOR_ANY 4
6839#define LOOKFOR_TERM 5
6840#define LOOKFOR_UNTERM 6
6841#define LOOKFOR_SCOPEDECL 7
6842#define LOOKFOR_NOBREAK 8
6843#define LOOKFOR_CPP_BASECLASS 9
6844#define LOOKFOR_ENUM_OR_INIT 10
6845
6846 int whilelevel;
6847 linenr_T lnum;
6848 char_u *options;
Bram Moolenaar48d27922012-06-13 13:40:48 +02006849 char_u *digits;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 int fraction = 0; /* init for GCC */
6851 int divider;
6852 int n;
6853 int iscase;
6854 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006855 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006857 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006858 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859
6860 for (options = curbuf->b_p_cino; *options; )
6861 {
6862 l = options++;
6863 if (*options == '-')
6864 ++options;
Bram Moolenaar48d27922012-06-13 13:40:48 +02006865 digits = options; /* remember where the digits start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 n = getdigits(&options);
6867 divider = 0;
6868 if (*options == '.') /* ".5s" means a fraction */
6869 {
6870 fraction = atol((char *)++options);
6871 while (VIM_ISDIGIT(*options))
6872 {
6873 ++options;
6874 if (divider)
6875 divider *= 10;
6876 else
6877 divider = 10;
6878 }
6879 }
6880 if (*options == 's') /* "2s" means two times 'shiftwidth' */
6881 {
Bram Moolenaar48d27922012-06-13 13:40:48 +02006882 if (options == digits)
Bram Moolenaar14f24742012-08-08 18:01:05 +02006883 n = sw; /* just "s" is one 'shiftwidth' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 else
6885 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02006886 n *= sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 if (divider)
Bram Moolenaar14f24742012-08-08 18:01:05 +02006888 n += (sw * fraction + divider / 2) / divider;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890 ++options;
6891 }
6892 if (l[1] == '-')
6893 n = -n;
6894 /* When adding an entry here, also update the default 'cinoptions' in
Bram Moolenaar39353fd2007-03-27 09:02:11 +00006895 * doc/indent.txt, and add explanation for it! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 switch (*l)
6897 {
6898 case '>': ind_level = n; break;
6899 case 'e': ind_open_imag = n; break;
6900 case 'n': ind_no_brace = n; break;
6901 case 'f': ind_first_open = n; break;
6902 case '{': ind_open_extra = n; break;
6903 case '}': ind_close_extra = n; break;
6904 case '^': ind_open_left_imag = n; break;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006905 case 'L': ind_jump_label = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 case ':': ind_case = n; break;
6907 case '=': ind_case_code = n; break;
6908 case 'b': ind_case_break = n; break;
6909 case 'p': ind_param = n; break;
6910 case 't': ind_func_type = n; break;
6911 case '/': ind_comment = n; break;
6912 case 'c': ind_in_comment = n; break;
6913 case 'C': ind_in_comment2 = n; break;
6914 case 'i': ind_cpp_baseclass = n; break;
6915 case '+': ind_continuation = n; break;
6916 case '(': ind_unclosed = n; break;
6917 case 'u': ind_unclosed2 = n; break;
6918 case 'U': ind_unclosed_noignore = n; break;
6919 case 'W': ind_unclosed_wrapped = n; break;
6920 case 'w': ind_unclosed_whiteok = n; break;
6921 case 'm': ind_matching_paren = n; break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006922 case 'M': ind_paren_prev = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 case ')': ind_maxparen = n; break;
6924 case '*': ind_maxcomment = n; break;
6925 case 'g': ind_scopedecl = n; break;
6926 case 'h': ind_scopedecl_code = n; break;
6927 case 'j': ind_java = n; break;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006928 case 'J': ind_js = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 case 'l': ind_keep_case_label = n; break;
Bram Moolenaar39353fd2007-03-27 09:02:11 +00006930 case '#': ind_hash_comment = n; break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006931 case 'N': ind_cpp_namespace = n; break;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006932 case 'k': ind_if_for_while = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 }
Bram Moolenaardfdf3c42010-03-23 18:22:46 +01006934 if (*options == ',')
6935 ++options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
6937
6938 /* remember where the cursor was when we started */
6939 cur_curpos = curwin->w_cursor;
6940
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006941 /* if we are at line 1 0 is fine, right? */
6942 if (cur_curpos.lnum == 1)
6943 return 0;
6944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 /* Get a copy of the current contents of the line.
6946 * This is required, because only the most recent line obtained with
6947 * ml_get is valid! */
6948 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6949 if (linecopy == NULL)
6950 return 0;
6951
6952 /*
6953 * In insert mode and the cursor is on a ')' truncate the line at the
6954 * cursor position. We don't want to line up with the matching '(' when
6955 * inserting new stuff.
6956 * For unknown reasons the cursor might be past the end of the line, thus
6957 * check for that.
6958 */
6959 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006960 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 && linecopy[curwin->w_cursor.col] == ')')
6962 linecopy[curwin->w_cursor.col] = NUL;
6963
6964 theline = skipwhite(linecopy);
6965
6966 /* move the cursor to the start of the line */
6967
6968 curwin->w_cursor.col = 0;
6969
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006970 original_line_islabel = cin_islabel(ind_maxcomment); /* XXX */
6971
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 /*
6973 * #defines and so on always go at the left when included in 'cinkeys'.
6974 */
6975 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
6976 {
6977 amount = 0;
6978 }
6979
6980 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006981 * Is it a non-case label? Then that goes at the left margin too unless:
6982 * - JS flag is set.
6983 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006985 else if (original_line_islabel && !ind_js && ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {
6987 amount = 0;
6988 }
6989
6990 /*
6991 * If we're inside a "//" comment and there is a "//" comment in a
6992 * previous line, lineup with that one.
6993 */
6994 else if (cin_islinecomment(theline)
6995 && (trypos = find_line_comment()) != NULL) /* XXX */
6996 {
6997 /* find how indented the line beginning the comment is */
6998 getvcol(curwin, trypos, &col, NULL, NULL);
6999 amount = col;
7000 }
7001
7002 /*
7003 * If we're inside a comment and not looking at the start of the
7004 * comment, try using the 'comments' option.
7005 */
7006 else if (!cin_iscomment(theline)
7007 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
7008 {
7009 int lead_start_len = 2;
7010 int lead_middle_len = 1;
7011 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7012 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7013 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7014 char_u *p;
7015 int start_align = 0;
7016 int start_off = 0;
7017 int done = FALSE;
7018
7019 /* find how indented the line beginning the comment is */
7020 getvcol(curwin, trypos, &col, NULL, NULL);
7021 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007022 *lead_start = NUL;
7023 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 p = curbuf->b_p_com;
7026 while (*p != NUL)
7027 {
7028 int align = 0;
7029 int off = 0;
7030 int what = 0;
7031
7032 while (*p != NUL && *p != ':')
7033 {
7034 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7035 what = *p++;
7036 else if (*p == COM_LEFT || *p == COM_RIGHT)
7037 align = *p++;
7038 else if (VIM_ISDIGIT(*p) || *p == '-')
7039 off = getdigits(&p);
7040 else
7041 ++p;
7042 }
7043
7044 if (*p == ':')
7045 ++p;
7046 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7047 if (what == COM_START)
7048 {
7049 STRCPY(lead_start, lead_end);
7050 lead_start_len = (int)STRLEN(lead_start);
7051 start_off = off;
7052 start_align = align;
7053 }
7054 else if (what == COM_MIDDLE)
7055 {
7056 STRCPY(lead_middle, lead_end);
7057 lead_middle_len = (int)STRLEN(lead_middle);
7058 }
7059 else if (what == COM_END)
7060 {
7061 /* If our line starts with the middle comment string, line it
7062 * up with the comment opener per the 'comments' option. */
7063 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7064 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7065 {
7066 done = TRUE;
7067 if (curwin->w_cursor.lnum > 1)
7068 {
7069 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007070 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 * the middle comment string matches in the previous
7072 * line, use the indent of that line. XXX */
7073 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7074 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7075 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7076 else if (STRNCMP(look, lead_middle,
7077 lead_middle_len) == 0)
7078 {
7079 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7080 break;
7081 }
7082 /* If the start comment string doesn't match with the
7083 * start of the comment, skip this entry. XXX */
7084 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7085 lead_start, lead_start_len) != 0)
7086 continue;
7087 }
7088 if (start_off != 0)
7089 amount += start_off;
7090 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007091 amount += vim_strsize(lead_start)
7092 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 break;
7094 }
7095
7096 /* If our line starts with the end comment string, line it up
7097 * with the middle comment */
7098 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7099 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7100 {
7101 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7102 /* XXX */
7103 if (off != 0)
7104 amount += off;
7105 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007106 amount += vim_strsize(lead_start)
7107 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 done = TRUE;
7109 break;
7110 }
7111 }
7112 }
7113
7114 /* If our line starts with an asterisk, line up with the
7115 * asterisk in the comment opener; otherwise, line up
7116 * with the first character of the comment text.
7117 */
7118 if (done)
7119 ;
7120 else if (theline[0] == '*')
7121 amount += 1;
7122 else
7123 {
7124 /*
7125 * If we are more than one line away from the comment opener, take
7126 * the indent of the previous non-empty line. If 'cino' has "CO"
7127 * and we are just below the comment opener and there are any
7128 * white characters after it line up with the text after it;
7129 * otherwise, add the amount specified by "c" in 'cino'
7130 */
7131 amount = -1;
7132 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7133 {
7134 if (linewhite(lnum)) /* skip blank lines */
7135 continue;
7136 amount = get_indent_lnum(lnum); /* XXX */
7137 break;
7138 }
7139 if (amount == -1) /* use the comment opener */
7140 {
7141 if (!ind_in_comment2)
7142 {
7143 start = ml_get(trypos->lnum);
7144 look = start + trypos->col + 2; /* skip / and * */
7145 if (*look != NUL) /* if something after it */
7146 trypos->col = (colnr_T)(skipwhite(look) - start);
7147 }
7148 getvcol(curwin, trypos, &col, NULL, NULL);
7149 amount = col;
7150 if (ind_in_comment2 || *look == NUL)
7151 amount += ind_in_comment;
7152 }
7153 }
7154 }
7155
7156 /*
7157 * Are we inside parentheses or braces?
7158 */ /* XXX */
7159 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL
7160 && ind_java == 0)
7161 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL
7162 || trypos != NULL)
7163 {
7164 if (trypos != NULL && tryposBrace != NULL)
7165 {
7166 /* Both an unmatched '(' and '{' is found. Use the one which is
7167 * closer to the current cursor position, set the other to NULL. */
7168 if (trypos->lnum != tryposBrace->lnum
7169 ? trypos->lnum < tryposBrace->lnum
7170 : trypos->col < tryposBrace->col)
7171 trypos = NULL;
7172 else
7173 tryposBrace = NULL;
7174 }
7175
7176 if (trypos != NULL)
7177 {
7178 /*
7179 * If the matching paren is more than one line away, use the indent of
7180 * a previous non-empty line that matches the same paren.
7181 */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007182 if (theline[0] == ')' && ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007184 /* Line up with the start of the matching paren line. */
7185 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7186 }
7187 else
7188 {
7189 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007190 our_paren_pos = *trypos;
7191 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007193 l = skipwhite(ml_get(lnum));
7194 if (cin_nocode(l)) /* skip comment lines */
7195 continue;
7196 if (cin_ispreproc_cont(&l, &lnum))
7197 continue; /* ignore #define, #if, etc. */
7198 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007200 /* Skip a comment. XXX */
7201 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7202 {
7203 lnum = trypos->lnum + 1;
7204 continue;
7205 }
7206
7207 /* XXX */
7208 if ((trypos = find_match_paren(
7209 corr_ind_maxparen(ind_maxparen, &cur_curpos),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 ind_maxcomment)) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007211 && trypos->lnum == our_paren_pos.lnum
7212 && trypos->col == our_paren_pos.col)
7213 {
7214 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007216 if (theline[0] == ')')
7217 {
7218 if (our_paren_pos.lnum != lnum
7219 && cur_amount > amount)
7220 cur_amount = amount;
7221 amount = -1;
7222 }
7223 break;
7224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
7226 }
7227
7228 /*
7229 * Line up with line where the matching paren is. XXX
7230 * If the line starts with a '(' or the indent for unclosed
7231 * parentheses is zero, line up with the unclosed parentheses.
7232 */
7233 if (amount == -1)
7234 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007235 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007236 int is_if_for_while = 0;
7237
7238 if (ind_if_for_while)
7239 {
7240 /* Look for the outermost opening parenthesis on this line
7241 * and check whether it belongs to an "if", "for" or "while". */
7242
7243 pos_T cursor_save = curwin->w_cursor;
7244 pos_T outermost;
7245 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007246
7247 trypos = &our_paren_pos;
7248 do {
7249 outermost = *trypos;
7250 curwin->w_cursor.lnum = outermost.lnum;
7251 curwin->w_cursor.col = outermost.col;
7252
7253 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
7254 } while (trypos && trypos->lnum == outermost.lnum);
7255
7256 curwin->w_cursor = cursor_save;
7257
7258 line = ml_get(outermost.lnum);
7259
7260 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007261 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007262 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007263
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007265 look = skipwhite(look);
7266 if (*look == '(')
7267 {
7268 linenr_T save_lnum = curwin->w_cursor.lnum;
7269 char_u *line;
7270 int look_col;
7271
7272 /* Ignore a '(' in front of the line that has a match before
7273 * our matching '('. */
7274 curwin->w_cursor.lnum = our_paren_pos.lnum;
7275 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007276 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007277 curwin->w_cursor.col = look_col + 1;
7278 if ((trypos = findmatchlimit(NULL, ')', 0, ind_maxparen))
7279 != NULL
7280 && trypos->lnum == our_paren_pos.lnum
7281 && trypos->col < our_paren_pos.col)
7282 ignore_paren_col = trypos->col + 1;
7283
7284 curwin->w_cursor.lnum = save_lnum;
7285 look = ml_get(our_paren_pos.lnum) + look_col;
7286 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007287 if (theline[0] == ')' || (ind_unclosed == 0 && is_if_for_while == 0)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007288 || (!ind_unclosed_noignore && *look == '('
7289 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
7291 /*
7292 * If we're looking at a close paren, line up right there;
7293 * otherwise, line up with the next (non-white) character.
7294 * When ind_unclosed_wrapped is set and the matching paren is
7295 * the last nonwhite character of the line, use either the
7296 * indent of the current line or the indentation of the next
7297 * outer paren and add ind_unclosed_wrapped (for very long
7298 * lines).
7299 */
7300 if (theline[0] != ')')
7301 {
7302 cur_amount = MAXCOL;
7303 l = ml_get(our_paren_pos.lnum);
7304 if (ind_unclosed_wrapped
7305 && cin_ends_in(l, (char_u *)"(", NULL))
7306 {
7307 /* look for opening unmatched paren, indent one level
7308 * for each additional level */
7309 n = 1;
7310 for (col = 0; col < our_paren_pos.col; ++col)
7311 {
7312 switch (l[col])
7313 {
7314 case '(':
7315 case '{': ++n;
7316 break;
7317
7318 case ')':
7319 case '}': if (n > 1)
7320 --n;
7321 break;
7322 }
7323 }
7324
7325 our_paren_pos.col = 0;
7326 amount += n * ind_unclosed_wrapped;
7327 }
7328 else if (ind_unclosed_whiteok)
7329 our_paren_pos.col++;
7330 else
7331 {
7332 col = our_paren_pos.col + 1;
7333 while (vim_iswhite(l[col]))
7334 col++;
7335 if (l[col] != NUL) /* In case of trailing space */
7336 our_paren_pos.col = col;
7337 else
7338 our_paren_pos.col++;
7339 }
7340 }
7341
7342 /*
7343 * Find how indented the paren is, or the character after it
7344 * if we did the above "if".
7345 */
7346 if (our_paren_pos.col > 0)
7347 {
7348 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7349 if (cur_amount > (int)col)
7350 cur_amount = col;
7351 }
7352 }
7353
7354 if (theline[0] == ')' && ind_matching_paren)
7355 {
7356 /* Line up with the start of the matching paren line. */
7357 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007358 else if ((ind_unclosed == 0 && is_if_for_while == 0)
7359 || (!ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007360 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
7362 if (cur_amount != MAXCOL)
7363 amount = cur_amount;
7364 }
7365 else
7366 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007367 /* Add ind_unclosed2 for each '(' before our matching one, but
7368 * ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007370 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
7372 --our_paren_pos.col;
7373 switch (*ml_get_pos(&our_paren_pos))
7374 {
7375 case '(': amount += ind_unclosed2;
7376 col = our_paren_pos.col;
7377 break;
7378 case ')': amount -= ind_unclosed2;
7379 col = MAXCOL;
7380 break;
7381 }
7382 }
7383
7384 /* Use ind_unclosed once, when the first '(' is not inside
7385 * braces */
7386 if (col == MAXCOL)
7387 amount += ind_unclosed;
7388 else
7389 {
7390 curwin->w_cursor.lnum = our_paren_pos.lnum;
7391 curwin->w_cursor.col = col;
Bram Moolenaar367bec82011-04-11 14:26:19 +02007392 if (find_match_paren(ind_maxparen, ind_maxcomment) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 amount += ind_unclosed2;
7394 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007395 {
7396 if (is_if_for_while)
7397 amount += ind_if_for_while;
7398 else
7399 amount += ind_unclosed;
7400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 }
7402 /*
7403 * For a line starting with ')' use the minimum of the two
7404 * positions, to avoid giving it more indent than the previous
7405 * lines:
7406 * func_long_name( if (x
7407 * arg && yy
7408 * ) ^ not here ) ^ not here
7409 */
7410 if (cur_amount < amount)
7411 amount = cur_amount;
7412 }
7413 }
7414
7415 /* add extra indent for a comment */
7416 if (cin_iscomment(theline))
7417 amount += ind_comment;
7418 }
7419
7420 /*
7421 * Are we at least inside braces, then?
7422 */
7423 else
7424 {
7425 trypos = tryposBrace;
7426
7427 ourscope = trypos->lnum;
7428 start = ml_get(ourscope);
7429
7430 /*
7431 * Now figure out how indented the line is in general.
7432 * If the brace was at the start of the line, we use that;
7433 * otherwise, check out the indentation of the line as
7434 * a whole and then add the "imaginary indent" to that.
7435 */
7436 look = skipwhite(start);
7437 if (*look == '{')
7438 {
7439 getvcol(curwin, trypos, &col, NULL, NULL);
7440 amount = col;
7441 if (*start == '{')
7442 start_brace = BRACE_IN_COL0;
7443 else
7444 start_brace = BRACE_AT_START;
7445 }
7446 else
7447 {
7448 /*
7449 * that opening brace might have been on a continuation
7450 * line. if so, find the start of the line.
7451 */
7452 curwin->w_cursor.lnum = ourscope;
7453
7454 /*
7455 * position the cursor over the rightmost paren, so that
7456 * matching it will take us back to the start of the line.
7457 */
7458 lnum = ourscope;
7459 if (find_last_paren(start, '(', ')')
7460 && (trypos = find_match_paren(ind_maxparen,
7461 ind_maxcomment)) != NULL)
7462 lnum = trypos->lnum;
7463
7464 /*
7465 * It could have been something like
7466 * case 1: if (asdf &&
7467 * ldfd) {
7468 * }
7469 */
Bram Moolenaar6ec154b2011-06-12 21:51:08 +02007470 if (ind_js || (ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007471 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 amount = get_indent();
7473 else
7474 amount = skip_label(lnum, &l, ind_maxcomment);
7475
7476 start_brace = BRACE_AT_END;
7477 }
7478
7479 /*
7480 * if we're looking at a closing brace, that's where
7481 * we want to be. otherwise, add the amount of room
7482 * that an indent is supposed to be.
7483 */
7484 if (theline[0] == '}')
7485 {
7486 /*
7487 * they may want closing braces to line up with something
7488 * other than the open brace. indulge them, if so.
7489 */
7490 amount += ind_close_extra;
7491 }
7492 else
7493 {
7494 /*
7495 * If we're looking at an "else", try to find an "if"
7496 * to match it with.
7497 * If we're looking at a "while", try to find a "do"
7498 * to match it with.
7499 */
7500 lookfor = LOOKFOR_INITIAL;
7501 if (cin_iselse(theline))
7502 lookfor = LOOKFOR_IF;
7503 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen))
7504 /* XXX */
7505 lookfor = LOOKFOR_DO;
7506 if (lookfor != LOOKFOR_INITIAL)
7507 {
7508 curwin->w_cursor.lnum = cur_curpos.lnum;
7509 if (find_match(lookfor, ourscope, ind_maxparen,
7510 ind_maxcomment) == OK)
7511 {
7512 amount = get_indent(); /* XXX */
7513 goto theend;
7514 }
7515 }
7516
7517 /*
7518 * We get here if we are not on an "while-of-do" or "else" (or
7519 * failed to find a matching "if").
7520 * Search backwards for something to line up with.
7521 * First set amount for when we don't find anything.
7522 */
7523
7524 /*
7525 * if the '{' is _really_ at the left margin, use the imaginary
7526 * location of a left-margin brace. Otherwise, correct the
7527 * location for ind_open_extra.
7528 */
7529
7530 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7531 {
7532 amount = ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007533 lookfor_cpp_namespace = TRUE;
7534 }
7535 else if (start_brace == BRACE_AT_START &&
7536 lookfor_cpp_namespace) /* '{' is at start */
7537 {
7538
7539 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 }
7541 else
7542 {
7543 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 amount += ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007546
7547 l = skipwhite(ml_get_curline());
7548 if (cin_is_cpp_namespace(l))
7549 amount += ind_cpp_namespace;
7550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 else
7552 {
7553 /* Compensate for adding ind_open_extra later. */
7554 amount -= ind_open_extra;
7555 if (amount < 0)
7556 amount = 0;
7557 }
7558 }
7559
7560 lookfor_break = FALSE;
7561
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007562 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {
7564 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
7565 amount += ind_case;
7566 }
7567 else if (cin_isscopedecl(theline)) /* private:, ... */
7568 {
7569 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
7570 amount += ind_scopedecl;
7571 }
7572 else
7573 {
7574 if (ind_case_break && cin_isbreak(theline)) /* break; ... */
7575 lookfor_break = TRUE;
7576
7577 lookfor = LOOKFOR_INITIAL;
7578 amount += ind_level; /* ind_level from start of block */
7579 }
7580 scope_amount = amount;
7581 whilelevel = 0;
7582
7583 /*
7584 * Search backwards. If we find something we recognize, line up
7585 * with that.
7586 *
7587 * if we're looking at an open brace, indent
7588 * the usual amount relative to the conditional
7589 * that opens the block.
7590 */
7591 curwin->w_cursor = cur_curpos;
7592 for (;;)
7593 {
7594 curwin->w_cursor.lnum--;
7595 curwin->w_cursor.col = 0;
7596
7597 /*
7598 * If we went all the way back to the start of our scope, line
7599 * up with it.
7600 */
7601 if (curwin->w_cursor.lnum <= ourscope)
7602 {
7603 /* we reached end of scope:
7604 * if looking for a enum or structure initialization
7605 * go further back:
7606 * if it is an initializer (enum xxx or xxx =), then
7607 * don't add ind_continuation, otherwise it is a variable
7608 * declaration:
7609 * int x,
7610 * here; <-- add ind_continuation
7611 */
7612 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7613 {
7614 if (curwin->w_cursor.lnum == 0
7615 || curwin->w_cursor.lnum
7616 < ourscope - ind_maxparen)
7617 {
7618 /* nothing found (abuse ind_maxparen as limit)
7619 * assume terminated line (i.e. a variable
7620 * initialization) */
7621 if (cont_amount > 0)
7622 amount = cont_amount;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007623 else if (!ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 amount += ind_continuation;
7625 break;
7626 }
7627
7628 l = ml_get_curline();
7629
7630 /*
7631 * If we're in a comment now, skip to the start of the
7632 * comment.
7633 */
7634 trypos = find_start_comment(ind_maxcomment);
7635 if (trypos != NULL)
7636 {
7637 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007638 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 continue;
7640 }
7641
7642 /*
7643 * Skip preprocessor directives and blank lines.
7644 */
7645 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7646 continue;
7647
7648 if (cin_nocode(l))
7649 continue;
7650
7651 terminated = cin_isterminated(l, FALSE, TRUE);
7652
7653 /*
7654 * If we are at top level and the line looks like a
7655 * function declaration, we are done
7656 * (it's a variable declaration).
7657 */
7658 if (start_brace != BRACE_IN_COL0
Bram Moolenaarc367faa2011-12-14 20:21:35 +01007659 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum,
7660 0, ind_maxparen, ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {
7662 /* if the line is terminated with another ','
7663 * it is a continued variable initialization.
7664 * don't add extra indent.
7665 * TODO: does not work, if a function
7666 * declaration is split over multiple lines:
7667 * cin_isfuncdecl returns FALSE then.
7668 */
7669 if (terminated == ',')
7670 break;
7671
7672 /* if it es a enum declaration or an assignment,
7673 * we are done.
7674 */
7675 if (terminated != ';' && cin_isinit())
7676 break;
7677
7678 /* nothing useful found */
7679 if (terminated == 0 || terminated == '{')
7680 continue;
7681 }
7682
7683 if (terminated != ';')
7684 {
7685 /* Skip parens and braces. Position the cursor
7686 * over the rightmost paren, so that matching it
7687 * will take us back to the start of the line.
7688 */ /* XXX */
7689 trypos = NULL;
7690 if (find_last_paren(l, '(', ')'))
7691 trypos = find_match_paren(ind_maxparen,
7692 ind_maxcomment);
7693
7694 if (trypos == NULL && find_last_paren(l, '{', '}'))
7695 trypos = find_start_brace(ind_maxcomment);
7696
7697 if (trypos != NULL)
7698 {
7699 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007700 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 continue;
7702 }
7703 }
7704
7705 /* it's a variable declaration, add indentation
7706 * like in
7707 * int a,
7708 * b;
7709 */
7710 if (cont_amount > 0)
7711 amount = cont_amount;
7712 else
7713 amount += ind_continuation;
7714 }
7715 else if (lookfor == LOOKFOR_UNTERM)
7716 {
7717 if (cont_amount > 0)
7718 amount = cont_amount;
7719 else
7720 amount += ind_continuation;
7721 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007722 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007723 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007724 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007726 {
7727 amount = scope_amount;
7728 if (theline[0] == '{')
7729 {
7730 amount += ind_open_extra;
7731 added_to_amount = ind_open_extra;
7732 }
7733 }
7734
7735 if (lookfor_cpp_namespace)
7736 {
7737 /*
7738 * Looking for C++ namespace, need to look further
7739 * back.
7740 */
7741 if (curwin->w_cursor.lnum == ourscope)
7742 continue;
7743
7744 if (curwin->w_cursor.lnum == 0
7745 || curwin->w_cursor.lnum
7746 < ourscope - FIND_NAMESPACE_LIM)
7747 break;
7748
7749 l = ml_get_curline();
7750
7751 /* If we're in a comment now, skip to the start of
7752 * the comment. */
7753 trypos = find_start_comment(ind_maxcomment);
7754 if (trypos != NULL)
7755 {
7756 curwin->w_cursor.lnum = trypos->lnum + 1;
7757 curwin->w_cursor.col = 0;
7758 continue;
7759 }
7760
7761 /* Skip preprocessor directives and blank lines. */
7762 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7763 continue;
7764
7765 /* Finally the actual check for "namespace". */
7766 if (cin_is_cpp_namespace(l))
7767 {
7768 amount += ind_cpp_namespace - added_to_amount;
7769 break;
7770 }
7771
7772 if (cin_nocode(l))
7773 continue;
7774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
7776 break;
7777 }
7778
7779 /*
7780 * If we're in a comment now, skip to the start of the comment.
7781 */ /* XXX */
7782 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7783 {
7784 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007785 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 continue;
7787 }
7788
7789 l = ml_get_curline();
7790
7791 /*
7792 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007793 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007795 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 if (iscase || cin_isscopedecl(l))
7797 {
7798 /* we are only looking for cpp base class
7799 * declaration/initialization any longer */
7800 if (lookfor == LOOKFOR_CPP_BASECLASS)
7801 break;
7802
7803 /* When looking for a "do" we are not interested in
7804 * labels. */
7805 if (whilelevel > 0)
7806 continue;
7807
7808 /*
7809 * case xx:
7810 * c = 99 + <- this indent plus continuation
7811 *-> here;
7812 */
7813 if (lookfor == LOOKFOR_UNTERM
7814 || lookfor == LOOKFOR_ENUM_OR_INIT)
7815 {
7816 if (cont_amount > 0)
7817 amount = cont_amount;
7818 else
7819 amount += ind_continuation;
7820 break;
7821 }
7822
7823 /*
7824 * case xx: <- line up with this case
7825 * x = 333;
7826 * case yy:
7827 */
7828 if ( (iscase && lookfor == LOOKFOR_CASE)
7829 || (iscase && lookfor_break)
7830 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7831 {
7832 /*
7833 * Check that this case label is not for another
7834 * switch()
7835 */ /* XXX */
7836 if ((trypos = find_start_brace(ind_maxcomment)) ==
7837 NULL || trypos->lnum == ourscope)
7838 {
7839 amount = get_indent(); /* XXX */
7840 break;
7841 }
7842 continue;
7843 }
7844
7845 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7846
7847 /*
7848 * case xx: if (cond) <- line up with this if
7849 * y = y + 1;
7850 * -> s = 99;
7851 *
7852 * case xx:
7853 * if (cond) <- line up with this line
7854 * y = y + 1;
7855 * -> s = 99;
7856 */
7857 if (lookfor == LOOKFOR_TERM)
7858 {
7859 if (n)
7860 amount = n;
7861
7862 if (!lookfor_break)
7863 break;
7864 }
7865
7866 /*
7867 * case xx: x = x + 1; <- line up with this x
7868 * -> y = y + 1;
7869 *
7870 * case xx: if (cond) <- line up with this if
7871 * -> y = y + 1;
7872 */
7873 if (n)
7874 {
7875 amount = n;
7876 l = after_label(ml_get_curline());
7877 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007878 {
7879 if (theline[0] == '{')
7880 amount += ind_open_extra;
7881 else
7882 amount += ind_level + ind_no_brace;
7883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 break;
7885 }
7886
7887 /*
7888 * Try to get the indent of a statement before the switch
7889 * label. If nothing is found, line up relative to the
7890 * switch label.
7891 * break; <- may line up with this line
7892 * case xx:
7893 * -> y = 1;
7894 */
7895 scope_amount = get_indent() + (iscase /* XXX */
7896 ? ind_case_code : ind_scopedecl_code);
7897 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
7898 continue;
7899 }
7900
7901 /*
7902 * Looking for a switch() label or C++ scope declaration,
7903 * ignore other lines, skip {}-blocks.
7904 */
7905 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7906 {
7907 if (find_last_paren(l, '{', '}') && (trypos =
7908 find_start_brace(ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007911 curwin->w_cursor.col = 0;
7912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 continue;
7914 }
7915
7916 /*
7917 * Ignore jump labels with nothing after them.
7918 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007919 if (!ind_js && cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {
7921 l = after_label(ml_get_curline());
7922 if (l == NULL || cin_nocode(l))
7923 continue;
7924 }
7925
7926 /*
7927 * Ignore #defines, #if, etc.
7928 * Ignore comment and empty lines.
7929 * (need to get the line again, cin_islabel() may have
7930 * unlocked it)
7931 */
7932 l = ml_get_curline();
7933 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7934 || cin_nocode(l))
7935 continue;
7936
7937 /*
7938 * Are we at the start of a cpp base class declaration or
7939 * constructor initialization?
7940 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007941 n = FALSE;
7942 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0)
7943 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007944 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007945 l = ml_get_curline();
7946 }
7947 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
7949 if (lookfor == LOOKFOR_UNTERM)
7950 {
7951 if (cont_amount > 0)
7952 amount = cont_amount;
7953 else
7954 amount += ind_continuation;
7955 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007956 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007958 /* Need to find start of the declaration. */
7959 lookfor = LOOKFOR_UNTERM;
7960 ind_continuation = 0;
7961 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
7963 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007964 /* XXX */
7965 amount = get_baseclass_amount(col, ind_maxparen,
7966 ind_maxcomment, ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 break;
7968 }
7969 else if (lookfor == LOOKFOR_CPP_BASECLASS)
7970 {
7971 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00007972 * declaration or initialization before the opening brace.
7973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (cin_isterminated(l, TRUE, FALSE))
7975 break;
7976 else
7977 continue;
7978 }
7979
7980 /*
7981 * What happens next depends on the line being terminated.
7982 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00007983 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 * 123,
7985 * sizeof
7986 * here
7987 * Otherwise check whether it is a enumeration or structure
7988 * initialisation (not indented) or a variable declaration
7989 * (indented).
7990 */
7991 terminated = cin_isterminated(l, FALSE, TRUE);
7992
7993 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
7994 && terminated == ','))
7995 {
7996 /*
7997 * if we're in the middle of a paren thing,
7998 * go back to the line that starts it so
7999 * we can get the right prevailing indent
8000 * if ( foo &&
8001 * bar )
8002 */
8003 /*
8004 * position the cursor over the rightmost paren, so that
8005 * matching it will take us back to the start of the line.
8006 */
8007 (void)find_last_paren(l, '(', ')');
8008 trypos = find_match_paren(
8009 corr_ind_maxparen(ind_maxparen, &cur_curpos),
8010 ind_maxcomment);
8011
8012 /*
8013 * If we are looking for ',', we also look for matching
8014 * braces.
8015 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008016 if (trypos == NULL && terminated == ','
8017 && find_last_paren(l, '{', '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 trypos = find_start_brace(ind_maxcomment);
8019
8020 if (trypos != NULL)
8021 {
8022 /*
8023 * Check if we are on a case label now. This is
8024 * handled above.
8025 * case xx: if ( asdf &&
8026 * asdf)
8027 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008028 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008030 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {
8032 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008033 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 continue;
8035 }
8036 }
8037
8038 /*
8039 * Skip over continuation lines to find the one to get the
8040 * indent from
8041 * char *usethis = "bla\
8042 * bla",
8043 * here;
8044 */
8045 if (terminated == ',')
8046 {
8047 while (curwin->w_cursor.lnum > 1)
8048 {
8049 l = ml_get(curwin->w_cursor.lnum - 1);
8050 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8051 break;
8052 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008053 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 }
8055 }
8056
8057 /*
8058 * Get indent and pointer to text for current line,
8059 * ignoring any jump label. XXX
8060 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008061 if (!ind_js)
8062 cur_amount = skip_label(curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 &l, ind_maxcomment);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008064 else
8065 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 /*
8067 * If this is just above the line we are indenting, and it
8068 * starts with a '{', line it up with this line.
8069 * while (not)
8070 * -> {
8071 * }
8072 */
8073 if (terminated != ',' && lookfor != LOOKFOR_TERM
8074 && theline[0] == '{')
8075 {
8076 amount = cur_amount;
8077 /*
8078 * Only add ind_open_extra when the current line
8079 * doesn't start with a '{', which must have a match
8080 * in the same line (scope is the same). Probably:
8081 * { 1, 2 },
8082 * -> { 3, 4 }
8083 */
8084 if (*skipwhite(l) != '{')
8085 amount += ind_open_extra;
8086
8087 if (ind_cpp_baseclass)
8088 {
8089 /* have to look back, whether it is a cpp base
8090 * class declaration or initialization */
8091 lookfor = LOOKFOR_CPP_BASECLASS;
8092 continue;
8093 }
8094 break;
8095 }
8096
8097 /*
8098 * Check if we are after an "if", "while", etc.
8099 * Also allow " } else".
8100 */
8101 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8102 {
8103 /*
8104 * Found an unterminated line after an if (), line up
8105 * with the last one.
8106 * if (cond)
8107 * 100 +
8108 * -> here;
8109 */
8110 if (lookfor == LOOKFOR_UNTERM
8111 || lookfor == LOOKFOR_ENUM_OR_INIT)
8112 {
8113 if (cont_amount > 0)
8114 amount = cont_amount;
8115 else
8116 amount += ind_continuation;
8117 break;
8118 }
8119
8120 /*
8121 * If this is just above the line we are indenting, we
8122 * are finished.
8123 * while (not)
8124 * -> here;
8125 * Otherwise this indent can be used when the line
8126 * before this is terminated.
8127 * yyy;
8128 * if (stat)
8129 * while (not)
8130 * xxx;
8131 * -> here;
8132 */
8133 amount = cur_amount;
8134 if (theline[0] == '{')
8135 amount += ind_open_extra;
8136 if (lookfor != LOOKFOR_TERM)
8137 {
8138 amount += ind_level + ind_no_brace;
8139 break;
8140 }
8141
8142 /*
8143 * Special trick: when expecting the while () after a
8144 * do, line up with the while()
8145 * do
8146 * x = 1;
8147 * -> here
8148 */
8149 l = skipwhite(ml_get_curline());
8150 if (cin_isdo(l))
8151 {
8152 if (whilelevel == 0)
8153 break;
8154 --whilelevel;
8155 }
8156
8157 /*
8158 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008159 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 * Need to use the scope of this "else". XXX
8161 * If whilelevel != 0 continue looking for a "do {".
8162 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008163 if (cin_iselse(l) && whilelevel == 0)
8164 {
8165 /* If we're looking at "} else", let's make sure we
8166 * find the opening brace of the enclosing scope,
8167 * not the one from "if () {". */
8168 if (*l == '}')
8169 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008170 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008171
8172 if ((trypos = find_start_brace(ind_maxcomment))
8173 == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 || find_match(LOOKFOR_IF, trypos->lnum,
Bram Moolenaar334adf02011-05-25 13:34:04 +02008175 ind_maxparen, ind_maxcomment) == FAIL)
8176 break;
8177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179
8180 /*
8181 * If we're below an unterminated line that is not an
8182 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008183 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 * the line before this one.
8185 */
8186 else
8187 {
8188 /*
8189 * Found two unterminated lines on a row, line up with
8190 * the last one.
8191 * c = 99 +
8192 * 100 +
8193 * -> here;
8194 */
8195 if (lookfor == LOOKFOR_UNTERM)
8196 {
8197 /* When line ends in a comma add extra indent */
8198 if (terminated == ',')
8199 amount += ind_continuation;
8200 break;
8201 }
8202
8203 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8204 {
8205 /* Found two lines ending in ',', lineup with the
8206 * lowest one, but check for cpp base class
8207 * declaration/initialization, if it is an
8208 * opening brace or we are looking just for
8209 * enumerations/initializations. */
8210 if (terminated == ',')
8211 {
8212 if (ind_cpp_baseclass == 0)
8213 break;
8214
8215 lookfor = LOOKFOR_CPP_BASECLASS;
8216 continue;
8217 }
8218
8219 /* Ignore unterminated lines in between, but
8220 * reduce indent. */
8221 if (amount > cur_amount)
8222 amount = cur_amount;
8223 }
8224 else
8225 {
8226 /*
8227 * Found first unterminated line on a row, may
8228 * line up with this line, remember its indent
8229 * 100 +
8230 * -> here;
8231 */
8232 amount = cur_amount;
8233
8234 /*
8235 * If previous line ends in ',', check whether we
8236 * are in an initialization or enum
8237 * struct xxx =
8238 * {
8239 * sizeof a,
8240 * 124 };
8241 * or a normal possible continuation line.
8242 * but only, of no other statement has been found
8243 * yet.
8244 */
8245 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8246 {
8247 lookfor = LOOKFOR_ENUM_OR_INIT;
8248 cont_amount = cin_first_id_amount();
8249 }
8250 else
8251 {
8252 if (lookfor == LOOKFOR_INITIAL
8253 && *l != NUL
8254 && l[STRLEN(l) - 1] == '\\')
8255 /* XXX */
8256 cont_amount = cin_get_equal_amount(
8257 curwin->w_cursor.lnum);
8258 if (lookfor != LOOKFOR_TERM)
8259 lookfor = LOOKFOR_UNTERM;
8260 }
8261 }
8262 }
8263 }
8264
8265 /*
8266 * Check if we are after a while (cond);
8267 * If so: Ignore until the matching "do".
8268 */
8269 /* XXX */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008270 else if (cin_iswhileofdo_end(terminated, ind_maxparen,
8271 ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
8273 /*
8274 * Found an unterminated line after a while ();, line up
8275 * with the last one.
8276 * while (cond);
8277 * 100 + <- line up with this one
8278 * -> here;
8279 */
8280 if (lookfor == LOOKFOR_UNTERM
8281 || lookfor == LOOKFOR_ENUM_OR_INIT)
8282 {
8283 if (cont_amount > 0)
8284 amount = cont_amount;
8285 else
8286 amount += ind_continuation;
8287 break;
8288 }
8289
8290 if (whilelevel == 0)
8291 {
8292 lookfor = LOOKFOR_TERM;
8293 amount = get_indent(); /* XXX */
8294 if (theline[0] == '{')
8295 amount += ind_open_extra;
8296 }
8297 ++whilelevel;
8298 }
8299
8300 /*
8301 * We are after a "normal" statement.
8302 * If we had another statement we can stop now and use the
8303 * indent of that other statement.
8304 * Otherwise the indent of the current statement may be used,
8305 * search backwards for the next "normal" statement.
8306 */
8307 else
8308 {
8309 /*
8310 * Skip single break line, if before a switch label. It
8311 * may be lined up with the case label.
8312 */
8313 if (lookfor == LOOKFOR_NOBREAK
8314 && cin_isbreak(skipwhite(ml_get_curline())))
8315 {
8316 lookfor = LOOKFOR_ANY;
8317 continue;
8318 }
8319
8320 /*
8321 * Handle "do {" line.
8322 */
8323 if (whilelevel > 0)
8324 {
8325 l = cin_skipcomment(ml_get_curline());
8326 if (cin_isdo(l))
8327 {
8328 amount = get_indent(); /* XXX */
8329 --whilelevel;
8330 continue;
8331 }
8332 }
8333
8334 /*
8335 * Found a terminated line above an unterminated line. Add
8336 * the amount for a continuation line.
8337 * x = 1;
8338 * y = foo +
8339 * -> here;
8340 * or
8341 * int x = 1;
8342 * int foo,
8343 * -> here;
8344 */
8345 if (lookfor == LOOKFOR_UNTERM
8346 || lookfor == LOOKFOR_ENUM_OR_INIT)
8347 {
8348 if (cont_amount > 0)
8349 amount = cont_amount;
8350 else
8351 amount += ind_continuation;
8352 break;
8353 }
8354
8355 /*
8356 * Found a terminated line above a terminated line or "if"
8357 * etc. line. Use the amount of the line below us.
8358 * x = 1; x = 1;
8359 * if (asdf) y = 2;
8360 * while (asdf) ->here;
8361 * here;
8362 * ->foo;
8363 */
8364 if (lookfor == LOOKFOR_TERM)
8365 {
8366 if (!lookfor_break && whilelevel == 0)
8367 break;
8368 }
8369
8370 /*
8371 * First line above the one we're indenting is terminated.
8372 * To know what needs to be done look further backward for
8373 * a terminated line.
8374 */
8375 else
8376 {
8377 /*
8378 * position the cursor over the rightmost paren, so
8379 * that matching it will take us back to the start of
8380 * the line. Helps for:
8381 * func(asdr,
8382 * asdfasdf);
8383 * here;
8384 */
8385term_again:
8386 l = ml_get_curline();
8387 if (find_last_paren(l, '(', ')')
8388 && (trypos = find_match_paren(ind_maxparen,
8389 ind_maxcomment)) != NULL)
8390 {
8391 /*
8392 * Check if we are on a case label now. This is
8393 * handled above.
8394 * case xx: if ( asdf &&
8395 * asdf)
8396 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008397 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008399 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {
8401 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008402 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 continue;
8404 }
8405 }
8406
8407 /* When aligning with the case statement, don't align
8408 * with a statement after it.
8409 * case 1: { <-- don't use this { position
8410 * stat;
8411 * }
8412 * case 2:
8413 * stat;
8414 * }
8415 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008416 iscase = (ind_keep_case_label && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417
8418 /*
8419 * Get indent and pointer to text for current line,
8420 * ignoring any jump label.
8421 */
8422 amount = skip_label(curwin->w_cursor.lnum,
8423 &l, ind_maxcomment);
8424
8425 if (theline[0] == '{')
8426 amount += ind_open_extra;
8427 /* See remark above: "Only add ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008428 l = skipwhite(l);
8429 if (*l == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 amount -= ind_open_extra;
8431 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8432
8433 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008434 * When a terminated line starts with "else" skip to
8435 * the matching "if":
8436 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008437 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008438 * Need to use the scope of this "else". XXX
8439 * If whilelevel != 0 continue looking for a "do {".
8440 */
8441 if (lookfor == LOOKFOR_TERM
8442 && *l != '}'
8443 && cin_iselse(l)
8444 && whilelevel == 0)
8445 {
8446 if ((trypos = find_start_brace(ind_maxcomment))
8447 == NULL
8448 || find_match(LOOKFOR_IF, trypos->lnum,
8449 ind_maxparen, ind_maxcomment) == FAIL)
8450 break;
8451 continue;
8452 }
8453
8454 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 * If we're at the end of a block, skip to the start of
8456 * that block.
8457 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008458 l = ml_get_curline();
Bram Moolenaar50f42ca2011-07-15 14:12:30 +02008459 if (find_last_paren(l, '{', '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 && (trypos = find_start_brace(ind_maxcomment))
8461 != NULL) /* XXX */
8462 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008463 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 /* if not "else {" check for terminated again */
8465 /* but skip block for "} else {" */
8466 l = cin_skipcomment(ml_get_curline());
8467 if (*l == '}' || !cin_iselse(l))
8468 goto term_again;
8469 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008470 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 }
8472 }
8473 }
8474 }
8475 }
8476 }
8477
8478 /* add extra indent for a comment */
8479 if (cin_iscomment(theline))
8480 amount += ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008481
8482 /* subtract extra left-shift for jump labels */
8483 if (ind_jump_label > 0 && original_line_islabel)
8484 amount -= ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 }
8486
8487 /*
8488 * ok -- we're not inside any sort of structure at all!
8489 *
8490 * this means we're at the top level, and everything should
8491 * basically just match where the previous line is, except
8492 * for the lines immediately following a function declaration,
8493 * which are K&R-style parameters and need to be indented.
8494 */
8495 else
8496 {
8497 /*
8498 * if our line starts with an open brace, forget about any
8499 * prevailing indent and make sure it looks like the start
8500 * of a function
8501 */
8502
8503 if (theline[0] == '{')
8504 {
8505 amount = ind_first_open;
8506 }
8507
8508 /*
8509 * If the NEXT line is a function declaration, the current
8510 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008511 * Don't do this if the current line looks like a comment or if the
8512 * current line is terminated, ie. ends in ';', or if the current line
8513 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 */
8515 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8516 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008517 && vim_strchr(theline, '{') == NULL
8518 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 && !cin_ends_in(theline, (char_u *)":", NULL)
8520 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008521 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8522 cur_curpos.lnum + 1,
8523 ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 && !cin_isterminated(theline, FALSE, TRUE))
8525 {
8526 amount = ind_func_type;
8527 }
8528 else
8529 {
8530 amount = 0;
8531 curwin->w_cursor = cur_curpos;
8532
8533 /* search backwards until we find something we recognize */
8534
8535 while (curwin->w_cursor.lnum > 1)
8536 {
8537 curwin->w_cursor.lnum--;
8538 curwin->w_cursor.col = 0;
8539
8540 l = ml_get_curline();
8541
8542 /*
8543 * If we're in a comment now, skip to the start of the comment.
8544 */ /* XXX */
8545 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
8546 {
8547 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008548 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 continue;
8550 }
8551
8552 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008553 * Are we at the start of a cpp base class declaration or
8554 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008556 n = FALSE;
8557 if (ind_cpp_baseclass != 0 && theline[0] != '{')
8558 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008559 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008560 l = ml_get_curline();
8561 }
8562 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008564 /* XXX */
8565 amount = get_baseclass_amount(col, ind_maxparen,
8566 ind_maxcomment, ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 break;
8568 }
8569
8570 /*
8571 * Skip preprocessor directives and blank lines.
8572 */
8573 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8574 continue;
8575
8576 if (cin_nocode(l))
8577 continue;
8578
8579 /*
8580 * If the previous line ends in ',', use one level of
8581 * indentation:
8582 * int foo,
8583 * bar;
8584 * do this before checking for '}' in case of eg.
8585 * enum foobar
8586 * {
8587 * ...
8588 * } foo,
8589 * bar;
8590 */
8591 n = 0;
8592 if (cin_ends_in(l, (char_u *)",", NULL)
8593 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8594 {
8595 /* take us back to opening paren */
8596 if (find_last_paren(l, '(', ')')
8597 && (trypos = find_match_paren(ind_maxparen,
8598 ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008599 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
8601 /* For a line ending in ',' that is a continuation line go
8602 * back to the first line with a backslash:
8603 * char *foo = "bla\
8604 * bla",
8605 * here;
8606 */
8607 while (n == 0 && curwin->w_cursor.lnum > 1)
8608 {
8609 l = ml_get(curwin->w_cursor.lnum - 1);
8610 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8611 break;
8612 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008613 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 }
8615
8616 amount = get_indent(); /* XXX */
8617
8618 if (amount == 0)
8619 amount = cin_first_id_amount();
8620 if (amount == 0)
8621 amount = ind_continuation;
8622 break;
8623 }
8624
8625 /*
8626 * If the line looks like a function declaration, and we're
8627 * not in a comment, put it the left margin.
8628 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008629 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0,
8630 ind_maxparen, ind_maxcomment)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 break;
8632 l = ml_get_curline();
8633
8634 /*
8635 * Finding the closing '}' of a previous function. Put
8636 * current line at the left margin. For when 'cino' has "fs".
8637 */
8638 if (*skipwhite(l) == '}')
8639 break;
8640
8641 /* (matching {)
8642 * If the previous line ends on '};' (maybe followed by
8643 * comments) align at column 0. For example:
8644 * char *string_array[] = { "foo",
8645 * / * x * / "b};ar" }; / * foobar * /
8646 */
8647 if (cin_ends_in(l, (char_u *)"};", NULL))
8648 break;
8649
8650 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008651 * Find a line only has a semicolon that belongs to a previous
8652 * line ending in '}', e.g. before an #endif. Don't increase
8653 * indent then.
8654 */
8655 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8656 {
8657 pos_T curpos_save = curwin->w_cursor;
8658
8659 while (curwin->w_cursor.lnum > 1)
8660 {
8661 look = ml_get(--curwin->w_cursor.lnum);
8662 if (!(cin_nocode(look) || cin_ispreproc_cont(
8663 &look, &curwin->w_cursor.lnum)))
8664 break;
8665 }
8666 if (curwin->w_cursor.lnum > 0
8667 && cin_ends_in(look, (char_u *)"}", NULL))
8668 break;
8669
8670 curwin->w_cursor = curpos_save;
8671 }
8672
8673 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 * If the PREVIOUS line is a function declaration, the current
8675 * line (and the ones that follow) needs to be indented as
8676 * parameters.
8677 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008678 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0,
8679 ind_maxparen, ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {
8681 amount = ind_param;
8682 break;
8683 }
8684
8685 /*
8686 * If the previous line ends in ';' and the line before the
8687 * previous line ends in ',' or '\', ident to column zero:
8688 * int foo,
8689 * bar;
8690 * indent_to_0 here;
8691 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008692 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
8694 l = ml_get(curwin->w_cursor.lnum - 1);
8695 if (cin_ends_in(l, (char_u *)",", NULL)
8696 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8697 break;
8698 l = ml_get_curline();
8699 }
8700
8701 /*
8702 * Doesn't look like anything interesting -- so just
8703 * use the indent of this line.
8704 *
8705 * Position the cursor over the rightmost paren, so that
8706 * matching it will take us back to the start of the line.
8707 */
8708 find_last_paren(l, '(', ')');
8709
8710 if ((trypos = find_match_paren(ind_maxparen,
8711 ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008712 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 amount = get_indent(); /* XXX */
8714 break;
8715 }
8716
8717 /* add extra indent for a comment */
8718 if (cin_iscomment(theline))
8719 amount += ind_comment;
8720
8721 /* add extra indent if the previous line ended in a backslash:
8722 * "asdfasdf\
8723 * here";
8724 * char *foo = "asdf\
8725 * here";
8726 */
8727 if (cur_curpos.lnum > 1)
8728 {
8729 l = ml_get(cur_curpos.lnum - 1);
8730 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8731 {
8732 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8733 if (cur_amount > 0)
8734 amount = cur_amount;
8735 else if (cur_amount == 0)
8736 amount += ind_continuation;
8737 }
8738 }
8739 }
8740 }
8741
8742theend:
8743 /* put the cursor back where it belongs */
8744 curwin->w_cursor = cur_curpos;
8745
8746 vim_free(linecopy);
8747
8748 if (amount < 0)
8749 return 0;
8750 return amount;
8751}
8752
8753 static int
8754find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment)
8755 int lookfor;
8756 linenr_T ourscope;
8757 int ind_maxparen;
8758 int ind_maxcomment;
8759{
8760 char_u *look;
8761 pos_T *theirscope;
8762 char_u *mightbeif;
8763 int elselevel;
8764 int whilelevel;
8765
8766 if (lookfor == LOOKFOR_IF)
8767 {
8768 elselevel = 1;
8769 whilelevel = 0;
8770 }
8771 else
8772 {
8773 elselevel = 0;
8774 whilelevel = 1;
8775 }
8776
8777 curwin->w_cursor.col = 0;
8778
8779 while (curwin->w_cursor.lnum > ourscope + 1)
8780 {
8781 curwin->w_cursor.lnum--;
8782 curwin->w_cursor.col = 0;
8783
8784 look = cin_skipcomment(ml_get_curline());
8785 if (cin_iselse(look)
8786 || cin_isif(look)
8787 || cin_isdo(look) /* XXX */
8788 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8789 {
8790 /*
8791 * if we've gone outside the braces entirely,
8792 * we must be out of scope...
8793 */
8794 theirscope = find_start_brace(ind_maxcomment); /* XXX */
8795 if (theirscope == NULL)
8796 break;
8797
8798 /*
8799 * and if the brace enclosing this is further
8800 * back than the one enclosing the else, we're
8801 * out of luck too.
8802 */
8803 if (theirscope->lnum < ourscope)
8804 break;
8805
8806 /*
8807 * and if they're enclosed in a *deeper* brace,
8808 * then we can ignore it because it's in a
8809 * different scope...
8810 */
8811 if (theirscope->lnum > ourscope)
8812 continue;
8813
8814 /*
8815 * if it was an "else" (that's not an "else if")
8816 * then we need to go back to another if, so
8817 * increment elselevel
8818 */
8819 look = cin_skipcomment(ml_get_curline());
8820 if (cin_iselse(look))
8821 {
8822 mightbeif = cin_skipcomment(look + 4);
8823 if (!cin_isif(mightbeif))
8824 ++elselevel;
8825 continue;
8826 }
8827
8828 /*
8829 * if it was a "while" then we need to go back to
8830 * another "do", so increment whilelevel. XXX
8831 */
8832 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8833 {
8834 ++whilelevel;
8835 continue;
8836 }
8837
8838 /* If it's an "if" decrement elselevel */
8839 look = cin_skipcomment(ml_get_curline());
8840 if (cin_isif(look))
8841 {
8842 elselevel--;
8843 /*
8844 * When looking for an "if" ignore "while"s that
8845 * get in the way.
8846 */
8847 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8848 whilelevel = 0;
8849 }
8850
8851 /* If it's a "do" decrement whilelevel */
8852 if (cin_isdo(look))
8853 whilelevel--;
8854
8855 /*
8856 * if we've used up all the elses, then
8857 * this must be the if that we want!
8858 * match the indent level of that if.
8859 */
8860 if (elselevel <= 0 && whilelevel <= 0)
8861 {
8862 return OK;
8863 }
8864 }
8865 }
8866 return FAIL;
8867}
8868
8869# if defined(FEAT_EVAL) || defined(PROTO)
8870/*
8871 * Get indent level from 'indentexpr'.
8872 */
8873 int
8874get_expr_indent()
8875{
8876 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008877 pos_T save_pos;
8878 colnr_T save_curswant;
8879 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008881 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8882 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008884 /* Save and restore cursor position and curswant, in case it was changed
8885 * via :normal commands */
8886 save_pos = curwin->w_cursor;
8887 save_curswant = curwin->w_curswant;
8888 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008890 if (use_sandbox)
8891 ++sandbox;
8892 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008894 if (use_sandbox)
8895 --sandbox;
8896 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897
8898 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8899 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8900 * command. */
8901 save_State = State;
8902 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008903 curwin->w_cursor = save_pos;
8904 curwin->w_curswant = save_curswant;
8905 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 check_cursor();
8907 State = save_State;
8908
8909 /* If there is an error, just keep the current indent. */
8910 if (indent < 0)
8911 indent = get_indent();
8912
8913 return indent;
8914}
8915# endif
8916
8917#endif /* FEAT_CINDENT */
8918
8919#if defined(FEAT_LISP) || defined(PROTO)
8920
8921static int lisp_match __ARGS((char_u *p));
8922
8923 static int
8924lisp_match(p)
8925 char_u *p;
8926{
8927 char_u buf[LSIZE];
8928 int len;
8929 char_u *word = p_lispwords;
8930
8931 while (*word != NUL)
8932 {
8933 (void)copy_option_part(&word, buf, LSIZE, ",");
8934 len = (int)STRLEN(buf);
8935 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8936 return TRUE;
8937 }
8938 return FALSE;
8939}
8940
8941/*
8942 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8943 * The incompatible newer method is quite a bit better at indenting
8944 * code in lisp-like languages than the traditional one; it's still
8945 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8946 *
8947 * TODO:
8948 * Findmatch() should be adapted for lisp, also to make showmatch
8949 * work correctly: now (v5.3) it seems all C/C++ oriented:
8950 * - it does not recognize the #\( and #\) notations as character literals
8951 * - it doesn't know about comments starting with a semicolon
8952 * - it incorrectly interprets '(' as a character literal
8953 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008954 * Update from Sergey Khorev:
8955 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 */
8957 int
8958get_lisp_indent()
8959{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008960 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 int amount;
8962 char_u *that;
8963 colnr_T col;
8964 colnr_T firsttry;
8965 int parencount, quotecount;
8966 int vi_lisp;
8967
8968 /* Set vi_lisp to use the vi-compatible method */
8969 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8970
8971 realpos = curwin->w_cursor;
8972 curwin->w_cursor.col = 0;
8973
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008974 if ((pos = findmatch(NULL, '(')) == NULL)
8975 pos = findmatch(NULL, '[');
8976 else
8977 {
8978 paren = *pos;
8979 pos = findmatch(NULL, '[');
8980 if (pos == NULL || ltp(pos, &paren))
8981 pos = &paren;
8982 }
8983 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
8985 /* Extra trick: Take the indent of the first previous non-white
8986 * line that is at the same () level. */
8987 amount = -1;
8988 parencount = 0;
8989
8990 while (--curwin->w_cursor.lnum >= pos->lnum)
8991 {
8992 if (linewhite(curwin->w_cursor.lnum))
8993 continue;
8994 for (that = ml_get_curline(); *that != NUL; ++that)
8995 {
8996 if (*that == ';')
8997 {
8998 while (*(that + 1) != NUL)
8999 ++that;
9000 continue;
9001 }
9002 if (*that == '\\')
9003 {
9004 if (*(that + 1) != NUL)
9005 ++that;
9006 continue;
9007 }
9008 if (*that == '"' && *(that + 1) != NUL)
9009 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009010 while (*++that && *that != '"')
9011 {
9012 /* skipping escaped characters in the string */
9013 if (*that == '\\')
9014 {
9015 if (*++that == NUL)
9016 break;
9017 if (that[1] == NUL)
9018 {
9019 ++that;
9020 break;
9021 }
9022 }
9023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009025 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009027 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 --parencount;
9029 }
9030 if (parencount == 0)
9031 {
9032 amount = get_indent();
9033 break;
9034 }
9035 }
9036
9037 if (amount == -1)
9038 {
9039 curwin->w_cursor.lnum = pos->lnum;
9040 curwin->w_cursor.col = pos->col;
9041 col = pos->col;
9042
9043 that = ml_get_curline();
9044
9045 if (vi_lisp && get_indent() == 0)
9046 amount = 2;
9047 else
9048 {
9049 amount = 0;
9050 while (*that && col)
9051 {
9052 amount += lbr_chartabsize_adv(&that, (colnr_T)amount);
9053 col--;
9054 }
9055
9056 /*
9057 * Some keywords require "body" indenting rules (the
9058 * non-standard-lisp ones are Scheme special forms):
9059 *
9060 * (let ((a 1)) instead (let ((a 1))
9061 * (...)) of (...))
9062 */
9063
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009064 if (!vi_lisp && (*that == '(' || *that == '[')
9065 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 amount += 2;
9067 else
9068 {
9069 that++;
9070 amount++;
9071 firsttry = amount;
9072
9073 while (vim_iswhite(*that))
9074 {
9075 amount += lbr_chartabsize(that, (colnr_T)amount);
9076 ++that;
9077 }
9078
9079 if (*that && *that != ';') /* not a comment line */
9080 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009081 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009083 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 firsttry++;
9085
9086 parencount = 0;
9087 quotecount = 0;
9088
9089 if (vi_lisp
9090 || (*that != '"'
9091 && *that != '\''
9092 && *that != '#'
9093 && (*that < '0' || *that > '9')))
9094 {
9095 while (*that
9096 && (!vim_iswhite(*that)
9097 || quotecount
9098 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009099 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 && !quotecount
9101 && !parencount
9102 && vi_lisp)))
9103 {
9104 if (*that == '"')
9105 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009106 if ((*that == '(' || *that == '[')
9107 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009109 if ((*that == ')' || *that == ']')
9110 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 --parencount;
9112 if (*that == '\\' && *(that+1) != NUL)
9113 amount += lbr_chartabsize_adv(&that,
9114 (colnr_T)amount);
9115 amount += lbr_chartabsize_adv(&that,
9116 (colnr_T)amount);
9117 }
9118 }
9119 while (vim_iswhite(*that))
9120 {
9121 amount += lbr_chartabsize(that, (colnr_T)amount);
9122 that++;
9123 }
9124 if (!*that || *that == ';')
9125 amount = firsttry;
9126 }
9127 }
9128 }
9129 }
9130 }
9131 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009132 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133
9134 curwin->w_cursor = realpos;
9135
9136 return amount;
9137}
9138#endif /* FEAT_LISP */
9139
9140 void
9141prepare_to_exit()
9142{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009143#if defined(SIGHUP) && defined(SIG_IGN)
9144 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9145 * makes Vim exit and then handling SIGHUP causes various reentrance
9146 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009147 signal(SIGHUP, SIG_IGN);
9148#endif
9149
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#ifdef FEAT_GUI
9151 if (gui.in_use)
9152 {
9153 gui.dying = TRUE;
9154 out_trash(); /* trash any pending output */
9155 }
9156 else
9157#endif
9158 {
9159 windgoto((int)Rows - 1, 0);
9160
9161 /*
9162 * Switch terminal mode back now, so messages end up on the "normal"
9163 * screen (if there are two screens).
9164 */
9165 settmode(TMODE_COOK);
9166#ifdef WIN3264
9167 if (can_end_termcap_mode(FALSE) == TRUE)
9168#endif
9169 stoptermcap();
9170 out_flush();
9171 }
9172}
9173
9174/*
9175 * Preserve files and exit.
9176 * When called IObuff must contain a message.
9177 */
9178 void
9179preserve_exit()
9180{
9181 buf_T *buf;
9182
9183 prepare_to_exit();
9184
Bram Moolenaar4770d092006-01-12 23:22:24 +00009185 /* Setting this will prevent free() calls. That avoids calling free()
9186 * recursively when free() was invoked with a bad pointer. */
9187 really_exiting = TRUE;
9188
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 out_str(IObuff);
9190 screen_start(); /* don't know where cursor is now */
9191 out_flush();
9192
9193 ml_close_notmod(); /* close all not-modified buffers */
9194
9195 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9196 {
9197 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9198 {
9199 OUT_STR(_("Vim: preserving files...\n"));
9200 screen_start(); /* don't know where cursor is now */
9201 out_flush();
9202 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9203 break;
9204 }
9205 }
9206
9207 ml_close_all(FALSE); /* close all memfiles, without deleting */
9208
9209 OUT_STR(_("Vim: Finished.\n"));
9210
9211 getout(1);
9212}
9213
9214/*
9215 * return TRUE if "fname" exists.
9216 */
9217 int
9218vim_fexists(fname)
9219 char_u *fname;
9220{
9221 struct stat st;
9222
9223 if (mch_stat((char *)fname, &st))
9224 return FALSE;
9225 return TRUE;
9226}
9227
9228/*
9229 * Check for CTRL-C pressed, but only once in a while.
9230 * Should be used instead of ui_breakcheck() for functions that check for
9231 * each line in the file. Calling ui_breakcheck() each time takes too much
9232 * time, because it can be a system call.
9233 */
9234
9235#ifndef BREAKCHECK_SKIP
9236# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9237# define BREAKCHECK_SKIP 200
9238# else
9239# define BREAKCHECK_SKIP 32
9240# endif
9241#endif
9242
9243static int breakcheck_count = 0;
9244
9245 void
9246line_breakcheck()
9247{
9248 if (++breakcheck_count >= BREAKCHECK_SKIP)
9249 {
9250 breakcheck_count = 0;
9251 ui_breakcheck();
9252 }
9253}
9254
9255/*
9256 * Like line_breakcheck() but check 10 times less often.
9257 */
9258 void
9259fast_breakcheck()
9260{
9261 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9262 {
9263 breakcheck_count = 0;
9264 ui_breakcheck();
9265 }
9266}
9267
9268/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009269 * Invoke expand_wildcards() for one pattern.
9270 * Expand items like "%:h" before the expansion.
9271 * Returns OK or FAIL.
9272 */
9273 int
9274expand_wildcards_eval(pat, num_file, file, flags)
9275 char_u **pat; /* pointer to input pattern */
9276 int *num_file; /* resulting number of files */
9277 char_u ***file; /* array of resulting files */
9278 int flags; /* EW_DIR, etc. */
9279{
9280 int ret = FAIL;
9281 char_u *eval_pat = NULL;
9282 char_u *exp_pat = *pat;
9283 char_u *ignored_msg;
9284 int usedlen;
9285
9286 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9287 {
9288 ++emsg_off;
9289 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9290 NULL, &ignored_msg, NULL);
9291 --emsg_off;
9292 if (eval_pat != NULL)
9293 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9294 }
9295
9296 if (exp_pat != NULL)
9297 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9298
9299 if (eval_pat != NULL)
9300 {
9301 vim_free(exp_pat);
9302 vim_free(eval_pat);
9303 }
9304
9305 return ret;
9306}
9307
9308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9310 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009311 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 */
9313 int
9314expand_wildcards(num_pat, pat, num_file, file, flags)
9315 int num_pat; /* number of input patterns */
9316 char_u **pat; /* array of input patterns */
9317 int *num_file; /* resulting number of files */
9318 char_u ***file; /* array of resulting files */
9319 int flags; /* EW_DIR, etc. */
9320{
9321 int retval;
9322 int i, j;
9323 char_u *p;
9324 int non_suf_match; /* number without matching suffix */
9325
9326 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9327
9328 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009329 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 return retval;
9331
9332#ifdef FEAT_WILDIGN
9333 /*
9334 * Remove names that match 'wildignore'.
9335 */
9336 if (*p_wig)
9337 {
9338 char_u *ffname;
9339
9340 /* check all files in (*file)[] */
9341 for (i = 0; i < *num_file; ++i)
9342 {
9343 ffname = FullName_save((*file)[i], FALSE);
9344 if (ffname == NULL) /* out of memory */
9345 break;
9346# ifdef VMS
9347 vms_remove_version(ffname);
9348# endif
9349 if (match_file_list(p_wig, (*file)[i], ffname))
9350 {
9351 /* remove this matching file from the list */
9352 vim_free((*file)[i]);
9353 for (j = i; j + 1 < *num_file; ++j)
9354 (*file)[j] = (*file)[j + 1];
9355 --*num_file;
9356 --i;
9357 }
9358 vim_free(ffname);
9359 }
9360 }
9361#endif
9362
9363 /*
9364 * Move the names where 'suffixes' match to the end.
9365 */
9366 if (*num_file > 1)
9367 {
9368 non_suf_match = 0;
9369 for (i = 0; i < *num_file; ++i)
9370 {
9371 if (!match_suffix((*file)[i]))
9372 {
9373 /*
9374 * Move the name without matching suffix to the front
9375 * of the list.
9376 */
9377 p = (*file)[i];
9378 for (j = i; j > non_suf_match; --j)
9379 (*file)[j] = (*file)[j - 1];
9380 (*file)[non_suf_match++] = p;
9381 }
9382 }
9383 }
9384
9385 return retval;
9386}
9387
9388/*
9389 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9390 */
9391 int
9392match_suffix(fname)
9393 char_u *fname;
9394{
9395 int fnamelen, setsuflen;
9396 char_u *setsuf;
9397#define MAXSUFLEN 30 /* maximum length of a file suffix */
9398 char_u suf_buf[MAXSUFLEN];
9399
9400 fnamelen = (int)STRLEN(fname);
9401 setsuflen = 0;
9402 for (setsuf = p_su; *setsuf; )
9403 {
9404 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009405 if (setsuflen == 0)
9406 {
9407 char_u *tail = gettail(fname);
9408
9409 /* empty entry: match name without a '.' */
9410 if (vim_strchr(tail, '.') == NULL)
9411 {
9412 setsuflen = 1;
9413 break;
9414 }
9415 }
9416 else
9417 {
9418 if (fnamelen >= setsuflen
9419 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9420 (size_t)setsuflen) == 0)
9421 break;
9422 setsuflen = 0;
9423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 }
9425 return (setsuflen != 0);
9426}
9427
9428#if !defined(NO_EXPANDPATH) || defined(PROTO)
9429
9430# ifdef VIM_BACKTICK
9431static int vim_backtick __ARGS((char_u *p));
9432static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9433# endif
9434
9435# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9436/*
9437 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9438 * it's shared between these systems.
9439 */
9440# if defined(DJGPP) || defined(PROTO)
9441# define _cdecl /* DJGPP doesn't have this */
9442# else
9443# ifdef __BORLANDC__
9444# define _cdecl _RTLENTRYF
9445# endif
9446# endif
9447
9448/*
9449 * comparison function for qsort in dos_expandpath()
9450 */
9451 static int _cdecl
9452pstrcmp(const void *a, const void *b)
9453{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009454 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455}
9456
9457# ifndef WIN3264
9458 static void
9459namelowcpy(
9460 char_u *d,
9461 char_u *s)
9462{
9463# ifdef DJGPP
9464 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9465 while (*s)
9466 *d++ = *s++;
9467 else
9468# endif
9469 while (*s)
9470 *d++ = TOLOWER_LOC(*s++);
9471 *d = NUL;
9472}
9473# endif
9474
9475/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009476 * Recursively expand one path component into all matching files and/or
9477 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 * Return the number of matches found.
9479 * "path" has backslashes before chars that are not to be expanded, starting
9480 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009481 * Return the number of matches found.
9482 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 */
9484 static int
9485dos_expandpath(
9486 garray_T *gap,
9487 char_u *path,
9488 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009489 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009490 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009492 char_u *buf;
9493 char_u *path_end;
9494 char_u *p, *s, *e;
9495 int start_len = gap->ga_len;
9496 char_u *pat;
9497 regmatch_T regmatch;
9498 int starts_with_dot;
9499 int matches;
9500 int len;
9501 int starstar = FALSE;
9502 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#ifdef WIN3264
9504 WIN32_FIND_DATA fb;
9505 HANDLE hFind = (HANDLE)0;
9506# ifdef FEAT_MBYTE
9507 WIN32_FIND_DATAW wfb;
9508 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9509# endif
9510#else
9511 struct ffblk fb;
9512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009514 int ok;
9515
9516 /* Expanding "**" may take a long time, check for CTRL-C. */
9517 if (stardepth > 0)
9518 {
9519 ui_breakcheck();
9520 if (got_int)
9521 return 0;
9522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
9524 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009525 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 if (buf == NULL)
9527 return 0;
9528
9529 /*
9530 * Find the first part in the path name that contains a wildcard or a ~1.
9531 * Copy it into buf, including the preceding characters.
9532 */
9533 p = buf;
9534 s = buf;
9535 e = NULL;
9536 path_end = path;
9537 while (*path_end != NUL)
9538 {
9539 /* May ignore a wildcard that has a backslash before it; it will
9540 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9541 if (path_end >= path + wildoff && rem_backslash(path_end))
9542 *p++ = *path_end++;
9543 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9544 {
9545 if (e != NULL)
9546 break;
9547 s = p + 1;
9548 }
9549 else if (path_end >= path + wildoff
9550 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9551 e = p;
9552#ifdef FEAT_MBYTE
9553 if (has_mbyte)
9554 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009555 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 STRNCPY(p, path_end, len);
9557 p += len;
9558 path_end += len;
9559 }
9560 else
9561#endif
9562 *p++ = *path_end++;
9563 }
9564 e = p;
9565 *e = NUL;
9566
9567 /* now we have one wildcard component between s and e */
9568 /* Remove backslashes between "wildoff" and the start of the wildcard
9569 * component. */
9570 for (p = buf + wildoff; p < s; ++p)
9571 if (rem_backslash(p))
9572 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009573 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 --e;
9575 --s;
9576 }
9577
Bram Moolenaar231334e2005-07-25 20:46:57 +00009578 /* Check for "**" between "s" and "e". */
9579 for (p = s; p < e; ++p)
9580 if (p[0] == '*' && p[1] == '*')
9581 starstar = TRUE;
9582
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 starts_with_dot = (*s == '.');
9584 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9585 if (pat == NULL)
9586 {
9587 vim_free(buf);
9588 return 0;
9589 }
9590
9591 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009592 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009593 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 regmatch.rm_ic = TRUE; /* Always ignore case */
9595 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009596 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009597 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 vim_free(pat);
9599
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009600 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 {
9602 vim_free(buf);
9603 return 0;
9604 }
9605
9606 /* remember the pattern or file name being looked for */
9607 matchname = vim_strsave(s);
9608
Bram Moolenaar231334e2005-07-25 20:46:57 +00009609 /* If "**" is by itself, this is the first time we encounter it and more
9610 * is following then find matches without any directory. */
9611 if (!didstar && stardepth < 100 && starstar && e - s == 2
9612 && *path_end == '/')
9613 {
9614 STRCPY(s, path_end + 1);
9615 ++stardepth;
9616 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9617 --stardepth;
9618 }
9619
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 /* Scan all files in the directory with "dir/ *.*" */
9621 STRCPY(s, "*.*");
9622#ifdef WIN3264
9623# ifdef FEAT_MBYTE
9624 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9625 {
9626 /* The active codepage differs from 'encoding'. Attempt using the
9627 * wide function. If it fails because it is not implemented fall back
9628 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009629 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 if (wn != NULL)
9631 {
9632 hFind = FindFirstFileW(wn, &wfb);
9633 if (hFind == INVALID_HANDLE_VALUE
9634 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9635 {
9636 vim_free(wn);
9637 wn = NULL;
9638 }
9639 }
9640 }
9641
9642 if (wn == NULL)
9643# endif
9644 hFind = FindFirstFile(buf, &fb);
9645 ok = (hFind != INVALID_HANDLE_VALUE);
9646#else
9647 /* If we are expanding wildcards we try both files and directories */
9648 ok = (findfirst((char *)buf, &fb,
9649 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9650#endif
9651
9652 while (ok)
9653 {
9654#ifdef WIN3264
9655# ifdef FEAT_MBYTE
9656 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009657 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 else
9659# endif
9660 p = (char_u *)fb.cFileName;
9661#else
9662 p = (char_u *)fb.ff_name;
9663#endif
9664 /* Ignore entries starting with a dot, unless when asked for. Accept
9665 * all entries found with "matchname". */
9666 if ((p[0] != '.' || starts_with_dot)
9667 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009668 || (regmatch.regprog != NULL
9669 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009670 || ((flags & EW_NOTWILD)
9671 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 {
9673#ifdef WIN3264
9674 STRCPY(s, p);
9675#else
9676 namelowcpy(s, p);
9677#endif
9678 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009679
9680 if (starstar && stardepth < 100)
9681 {
9682 /* For "**" in the pattern first go deeper in the tree to
9683 * find matches. */
9684 STRCPY(buf + len, "/**");
9685 STRCPY(buf + len + 3, path_end);
9686 ++stardepth;
9687 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9688 --stardepth;
9689 }
9690
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 STRCPY(buf + len, path_end);
9692 if (mch_has_exp_wildcard(path_end))
9693 {
9694 /* need to expand another component of the path */
9695 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009696 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 }
9698 else
9699 {
9700 /* no more wildcards, check if there is a match */
9701 /* remove backslashes for the remaining components only */
9702 if (*path_end != 0)
9703 backslash_halve(buf + len + 1);
9704 if (mch_getperm(buf) >= 0) /* add existing file */
9705 addfile(gap, buf, flags);
9706 }
9707 }
9708
9709#ifdef WIN3264
9710# ifdef FEAT_MBYTE
9711 if (wn != NULL)
9712 {
9713 vim_free(p);
9714 ok = FindNextFileW(hFind, &wfb);
9715 }
9716 else
9717# endif
9718 ok = FindNextFile(hFind, &fb);
9719#else
9720 ok = (findnext(&fb) == 0);
9721#endif
9722
9723 /* If no more matches and no match was used, try expanding the name
9724 * itself. Finds the long name of a short filename. */
9725 if (!ok && matchname != NULL && gap->ga_len == start_len)
9726 {
9727 STRCPY(s, matchname);
9728#ifdef WIN3264
9729 FindClose(hFind);
9730# ifdef FEAT_MBYTE
9731 if (wn != NULL)
9732 {
9733 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009734 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 if (wn != NULL)
9736 hFind = FindFirstFileW(wn, &wfb);
9737 }
9738 if (wn == NULL)
9739# endif
9740 hFind = FindFirstFile(buf, &fb);
9741 ok = (hFind != INVALID_HANDLE_VALUE);
9742#else
9743 ok = (findfirst((char *)buf, &fb,
9744 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9745#endif
9746 vim_free(matchname);
9747 matchname = NULL;
9748 }
9749 }
9750
9751#ifdef WIN3264
9752 FindClose(hFind);
9753# ifdef FEAT_MBYTE
9754 vim_free(wn);
9755# endif
9756#endif
9757 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009758 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 vim_free(matchname);
9760
9761 matches = gap->ga_len - start_len;
9762 if (matches > 0)
9763 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9764 sizeof(char_u *), pstrcmp);
9765 return matches;
9766}
9767
9768 int
9769mch_expandpath(
9770 garray_T *gap,
9771 char_u *path,
9772 int flags) /* EW_* flags */
9773{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009774 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775}
9776# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9777
Bram Moolenaar231334e2005-07-25 20:46:57 +00009778#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9779 || defined(PROTO)
9780/*
9781 * Unix style wildcard expansion code.
9782 * It's here because it's used both for Unix and Mac.
9783 */
9784static int pstrcmp __ARGS((const void *, const void *));
9785
9786 static int
9787pstrcmp(a, b)
9788 const void *a, *b;
9789{
9790 return (pathcmp(*(char **)a, *(char **)b, -1));
9791}
9792
9793/*
9794 * Recursively expand one path component into all matching files and/or
9795 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9796 * "path" has backslashes before chars that are not to be expanded, starting
9797 * at "path + wildoff".
9798 * Return the number of matches found.
9799 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9800 */
9801 int
9802unix_expandpath(gap, path, wildoff, flags, didstar)
9803 garray_T *gap;
9804 char_u *path;
9805 int wildoff;
9806 int flags; /* EW_* flags */
9807 int didstar; /* expanded "**" once already */
9808{
9809 char_u *buf;
9810 char_u *path_end;
9811 char_u *p, *s, *e;
9812 int start_len = gap->ga_len;
9813 char_u *pat;
9814 regmatch_T regmatch;
9815 int starts_with_dot;
9816 int matches;
9817 int len;
9818 int starstar = FALSE;
9819 static int stardepth = 0; /* depth for "**" expansion */
9820
9821 DIR *dirp;
9822 struct dirent *dp;
9823
9824 /* Expanding "**" may take a long time, check for CTRL-C. */
9825 if (stardepth > 0)
9826 {
9827 ui_breakcheck();
9828 if (got_int)
9829 return 0;
9830 }
9831
9832 /* make room for file name */
9833 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9834 if (buf == NULL)
9835 return 0;
9836
9837 /*
9838 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009839 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009840 * Copy it into "buf", including the preceding characters.
9841 */
9842 p = buf;
9843 s = buf;
9844 e = NULL;
9845 path_end = path;
9846 while (*path_end != NUL)
9847 {
9848 /* May ignore a wildcard that has a backslash before it; it will
9849 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9850 if (path_end >= path + wildoff && rem_backslash(path_end))
9851 *p++ = *path_end++;
9852 else if (*path_end == '/')
9853 {
9854 if (e != NULL)
9855 break;
9856 s = p + 1;
9857 }
9858 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009859 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009860 || (!p_fic && (flags & EW_ICASE)
9861 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009862 e = p;
9863#ifdef FEAT_MBYTE
9864 if (has_mbyte)
9865 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009866 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009867 STRNCPY(p, path_end, len);
9868 p += len;
9869 path_end += len;
9870 }
9871 else
9872#endif
9873 *p++ = *path_end++;
9874 }
9875 e = p;
9876 *e = NUL;
9877
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009878 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009879 /* Remove backslashes between "wildoff" and the start of the wildcard
9880 * component. */
9881 for (p = buf + wildoff; p < s; ++p)
9882 if (rem_backslash(p))
9883 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009884 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009885 --e;
9886 --s;
9887 }
9888
9889 /* Check for "**" between "s" and "e". */
9890 for (p = s; p < e; ++p)
9891 if (p[0] == '*' && p[1] == '*')
9892 starstar = TRUE;
9893
9894 /* convert the file pattern to a regexp pattern */
9895 starts_with_dot = (*s == '.');
9896 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9897 if (pat == NULL)
9898 {
9899 vim_free(buf);
9900 return 0;
9901 }
9902
9903 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009904 if (flags & EW_ICASE)
9905 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9906 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009907 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009908 if (flags & (EW_NOERROR | EW_NOTWILD))
9909 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009910 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009911 if (flags & (EW_NOERROR | EW_NOTWILD))
9912 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009913 vim_free(pat);
9914
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009915 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009916 {
9917 vim_free(buf);
9918 return 0;
9919 }
9920
9921 /* If "**" is by itself, this is the first time we encounter it and more
9922 * is following then find matches without any directory. */
9923 if (!didstar && stardepth < 100 && starstar && e - s == 2
9924 && *path_end == '/')
9925 {
9926 STRCPY(s, path_end + 1);
9927 ++stardepth;
9928 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9929 --stardepth;
9930 }
9931
9932 /* open the directory for scanning */
9933 *s = NUL;
9934 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9935
9936 /* Find all matching entries */
9937 if (dirp != NULL)
9938 {
9939 for (;;)
9940 {
9941 dp = readdir(dirp);
9942 if (dp == NULL)
9943 break;
9944 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009945 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9946 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009947 || ((flags & EW_NOTWILD)
9948 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009949 {
9950 STRCPY(s, dp->d_name);
9951 len = STRLEN(buf);
9952
9953 if (starstar && stardepth < 100)
9954 {
9955 /* For "**" in the pattern first go deeper in the tree to
9956 * find matches. */
9957 STRCPY(buf + len, "/**");
9958 STRCPY(buf + len + 3, path_end);
9959 ++stardepth;
9960 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9961 --stardepth;
9962 }
9963
9964 STRCPY(buf + len, path_end);
9965 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9966 {
9967 /* need to expand another component of the path */
9968 /* remove backslashes for the remaining components only */
9969 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9970 }
9971 else
9972 {
9973 /* no more wildcards, check if there is a match */
9974 /* remove backslashes for the remaining components only */
9975 if (*path_end != NUL)
9976 backslash_halve(buf + len + 1);
9977 if (mch_getperm(buf) >= 0) /* add existing file */
9978 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009979#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00009980 size_t precomp_len = STRLEN(buf)+1;
9981 char_u *precomp_buf =
9982 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009983
Bram Moolenaar231334e2005-07-25 20:46:57 +00009984 if (precomp_buf)
9985 {
9986 mch_memmove(buf, precomp_buf, precomp_len);
9987 vim_free(precomp_buf);
9988 }
9989#endif
9990 addfile(gap, buf, flags);
9991 }
9992 }
9993 }
9994 }
9995
9996 closedir(dirp);
9997 }
9998
9999 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010000 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010001
10002 matches = gap->ga_len - start_len;
10003 if (matches > 0)
10004 qsort(((char_u **)gap->ga_data) + start_len, matches,
10005 sizeof(char_u *), pstrcmp);
10006 return matches;
10007}
10008#endif
10009
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010010#if defined(FEAT_SEARCHPATH)
10011static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10012static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010013static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10014static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010015static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10016static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10017
10018/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010019 * Moves "*psep" back to the previous path separator in "path".
10020 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010021 */
10022 static int
10023find_previous_pathsep(path, psep)
10024 char_u *path;
10025 char_u **psep;
10026{
10027 /* skip the current separator */
10028 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010029 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010030
10031 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010032 while (*psep > path)
10033 {
10034 if (vim_ispathsep(**psep))
10035 return OK;
10036 mb_ptr_back(path, *psep);
10037 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010038
10039 return FAIL;
10040}
10041
10042/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010043 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10044 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010045 */
10046 static int
10047is_unique(maybe_unique, gap, i)
10048 char_u *maybe_unique;
10049 garray_T *gap;
10050 int i;
10051{
10052 int j;
10053 int candidate_len;
10054 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010055 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010056 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010057
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010058 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010059 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010060 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010061 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010062
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010063 candidate_len = (int)STRLEN(maybe_unique);
10064 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010065 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010066 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010067
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010068 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010069 if (fnamecmp(maybe_unique, rival) == 0
10070 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010071 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010072 }
10073
Bram Moolenaar162bd912010-07-28 22:29:10 +020010074 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010075}
10076
10077/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010078 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010079 * paths are expanded to their equivalent fullpath. This includes the "."
10080 * (relative to current buffer directory) and empty path (relative to current
10081 * directory) notations.
10082 *
10083 * TODO: handle upward search (;) and path limiter (**N) notations by
10084 * expanding each into their equivalent path(s).
10085 */
10086 static void
10087expand_path_option(curdir, gap)
10088 char_u *curdir;
10089 garray_T *gap;
10090{
10091 char_u *path_option = *curbuf->b_p_path == NUL
10092 ? p_path : curbuf->b_p_path;
10093 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010094 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010095 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010096
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010097 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010098 return;
10099
10100 while (*path_option != NUL)
10101 {
10102 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10103
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010104 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010105 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010106 /* Relative to current buffer:
10107 * "/path/file" + "." -> "/path/"
10108 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010109 if (curbuf->b_ffname == NULL)
10110 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010111 p = gettail(curbuf->b_ffname);
10112 len = (int)(p - curbuf->b_ffname);
10113 if (len + (int)STRLEN(buf) >= MAXPATHL)
10114 continue;
10115 if (buf[1] == NUL)
10116 buf[len] = NUL;
10117 else
10118 STRMOVE(buf + len, buf + 2);
10119 mch_memmove(buf, curbuf->b_ffname, len);
10120 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010121 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010122 else if (buf[0] == NUL)
10123 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010124 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010125 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010126 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010127 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010128 else if (!mch_isFullName(buf))
10129 {
10130 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010131 len = (int)STRLEN(curdir);
10132 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010133 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010134 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010135 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010136 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010137 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010138 }
10139
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010140 if (ga_grow(gap, 1) == FAIL)
10141 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010142
10143# if defined(MSWIN) || defined(MSDOS)
10144 /* Avoid the path ending in a backslash, it fails when a comma is
10145 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010146 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010147 if (buf[len - 1] == '\\')
10148 buf[len - 1] = '/';
10149# endif
10150
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010151 p = vim_strsave(buf);
10152 if (p == NULL)
10153 break;
10154 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010155 }
10156
10157 vim_free(buf);
10158}
10159
10160/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010161 * Returns a pointer to the file or directory name in "fname" that matches the
10162 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010163 *
10164 * path: /foo/bar/baz
10165 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010166 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010167 */
10168 static char_u *
10169get_path_cutoff(fname, gap)
10170 char_u *fname;
10171 garray_T *gap;
10172{
10173 int i;
10174 int maxlen = 0;
10175 char_u **path_part = (char_u **)gap->ga_data;
10176 char_u *cutoff = NULL;
10177
10178 for (i = 0; i < gap->ga_len; i++)
10179 {
10180 int j = 0;
10181
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010182 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010183# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010184 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10185#endif
10186 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010187 j++;
10188 if (j > maxlen)
10189 {
10190 maxlen = j;
10191 cutoff = &fname[j];
10192 }
10193 }
10194
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010195 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010196 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010197 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010198 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010199
10200 return cutoff;
10201}
10202
10203/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010204 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10205 * that they are unique with respect to each other while conserving the part
10206 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010207 */
10208 static void
10209uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010210 garray_T *gap;
10211 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010212{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010213 int i;
10214 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010215 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010216 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010217 char_u *pat;
10218 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010219 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010220 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010221 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010222 char_u **in_curdir = NULL;
10223 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010224
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010225 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010226 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010227
10228 /*
10229 * We need to prepend a '*' at the beginning of file_pattern so that the
10230 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010231 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010232 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010233 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010234 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010235 if (file_pattern == NULL)
10236 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010237 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010238 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010239 STRCAT(file_pattern, pattern);
10240 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10241 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010242 if (pat == NULL)
10243 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010244
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010245 regmatch.rm_ic = TRUE; /* always ignore case */
10246 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10247 vim_free(pat);
10248 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010249 return;
10250
Bram Moolenaar162bd912010-07-28 22:29:10 +020010251 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010252 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010253 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010254 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010255
10256 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010257 if (in_curdir == NULL)
10258 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010259
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010260 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010261 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010262 char_u *path = fnames[i];
10263 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010264 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010265 char_u *pathsep_p;
10266 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010267
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010268 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010269 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010270 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010271 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010272 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010273
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010274 /* Shorten the filename while maintaining its uniqueness */
10275 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010276
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010277 /* we start at the end of the path */
10278 pathsep_p = path + len - 1;
10279
10280 while (find_previous_pathsep(path, &pathsep_p))
10281 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10282 && is_unique(pathsep_p + 1, gap, i)
10283 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10284 {
10285 sort_again = TRUE;
10286 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10287 break;
10288 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010289
10290 if (mch_isFullName(path))
10291 {
10292 /*
10293 * Last resort: shorten relative to curdir if possible.
10294 * 'possible' means:
10295 * 1. It is under the current directory.
10296 * 2. The result is actually shorter than the original.
10297 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010298 * Before curdir After
10299 * /foo/bar/file.txt /foo/bar ./file.txt
10300 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10301 * /file.txt / /file.txt
10302 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010303 */
10304 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010305 if (short_name != NULL && short_name > path + 1
10306#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010307 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010308 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010309 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010310 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010311 && !vim_ispathsep(*short_name)
10312#endif
10313 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010314 {
10315 STRCPY(path, ".");
10316 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010317 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010318 }
10319 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010320 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010321 }
10322
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010323 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010324 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010325 {
10326 char_u *rel_path;
10327 char_u *path = in_curdir[i];
10328
10329 if (path == NULL)
10330 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010331
10332 /* If the {filename} is not unique, change it to ./{filename}.
10333 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010334 short_name = shorten_fname(path, curdir);
10335 if (short_name == NULL)
10336 short_name = path;
10337 if (is_unique(short_name, gap, i))
10338 {
10339 STRCPY(fnames[i], short_name);
10340 continue;
10341 }
10342
10343 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10344 if (rel_path == NULL)
10345 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010346 STRCPY(rel_path, ".");
10347 add_pathsep(rel_path);
10348 STRCAT(rel_path, short_name);
10349
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010350 vim_free(fnames[i]);
10351 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010352 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010353 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010354 }
10355
Bram Moolenaar162bd912010-07-28 22:29:10 +020010356theend:
10357 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010358 if (in_curdir != NULL)
10359 {
10360 for (i = 0; i < gap->ga_len; i++)
10361 vim_free(in_curdir[i]);
10362 vim_free(in_curdir);
10363 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010364 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010365 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010366
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010367 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010368 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010369}
10370
10371/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010372 * Calls globpath() with 'path' values for the given pattern and stores the
10373 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010374 * Returns the total number of matches.
10375 */
10376 static int
10377expand_in_path(gap, pattern, flags)
10378 garray_T *gap;
10379 char_u *pattern;
10380 int flags; /* EW_* flags */
10381{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010382 char_u *curdir;
10383 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010384 char_u *files = NULL;
10385 char_u *s; /* start */
10386 char_u *e; /* end */
10387 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010388
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010389 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010390 return 0;
10391 mch_dirname(curdir, MAXPATHL);
10392
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010393 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010394 expand_path_option(curdir, &path_ga);
10395 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010396 if (path_ga.ga_len == 0)
10397 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010398
10399 paths = ga_concat_strings(&path_ga);
10400 ga_clear_strings(&path_ga);
10401 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010402 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010403
Bram Moolenaar94950a92010-12-02 16:01:29 +010010404 files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010405 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010406 if (files == NULL)
10407 return 0;
10408
10409 /* Copy each path in files into gap */
10410 s = e = files;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010411 while (*s != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010412 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010413 while (*e != '\n' && *e != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010414 e++;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010415 if (*e == NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010416 {
10417 addfile(gap, s, flags);
10418 break;
10419 }
10420 else
10421 {
10422 /* *e is '\n' */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010423 *e = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010424 addfile(gap, s, flags);
10425 e++;
10426 s = e;
10427 }
10428 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429 vim_free(files);
10430
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010431 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010432}
10433#endif
10434
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010435#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10436/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010437 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10438 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010439 */
10440 void
10441remove_duplicates(gap)
10442 garray_T *gap;
10443{
10444 int i;
10445 int j;
10446 char_u **fnames = (char_u **)gap->ga_data;
10447
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010448 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010449 for (i = gap->ga_len - 1; i > 0; --i)
10450 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10451 {
10452 vim_free(fnames[i]);
10453 for (j = i + 1; j < gap->ga_len; ++j)
10454 fnames[j - 1] = fnames[j];
10455 --gap->ga_len;
10456 }
10457}
10458#endif
10459
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460/*
10461 * Generic wildcard expansion code.
10462 *
10463 * Characters in "pat" that should not be expanded must be preceded with a
10464 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10465 *
10466 * Return FAIL when no single file was found. In this case "num_file" is not
10467 * set, and "file" may contain an error message.
10468 * Return OK when some files found. "num_file" is set to the number of
10469 * matches, "file" to the array of matches. Call FreeWild() later.
10470 */
10471 int
10472gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10473 int num_pat; /* number of input patterns */
10474 char_u **pat; /* array of input patterns */
10475 int *num_file; /* resulting number of files */
10476 char_u ***file; /* array of resulting files */
10477 int flags; /* EW_* flags */
10478{
10479 int i;
10480 garray_T ga;
10481 char_u *p;
10482 static int recursive = FALSE;
10483 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010484#if defined(FEAT_SEARCHPATH)
10485 int did_expand_in_path = FALSE;
10486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487
10488 /*
10489 * expand_env() is called to expand things like "~user". If this fails,
10490 * it calls ExpandOne(), which brings us back here. In this case, always
10491 * call the machine specific expansion function, if possible. Otherwise,
10492 * return FAIL.
10493 */
10494 if (recursive)
10495#ifdef SPECIAL_WILDCHAR
10496 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10497#else
10498 return FAIL;
10499#endif
10500
10501#ifdef SPECIAL_WILDCHAR
10502 /*
10503 * If there are any special wildcard characters which we cannot handle
10504 * here, call machine specific function for all the expansion. This
10505 * avoids starting the shell for each argument separately.
10506 * For `=expr` do use the internal function.
10507 */
10508 for (i = 0; i < num_pat; i++)
10509 {
10510 if (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL
10511# ifdef VIM_BACKTICK
10512 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10513# endif
10514 )
10515 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10516 }
10517#endif
10518
10519 recursive = TRUE;
10520
10521 /*
10522 * The matching file names are stored in a growarray. Init it empty.
10523 */
10524 ga_init2(&ga, (int)sizeof(char_u *), 30);
10525
10526 for (i = 0; i < num_pat; ++i)
10527 {
10528 add_pat = -1;
10529 p = pat[i];
10530
10531#ifdef VIM_BACKTICK
10532 if (vim_backtick(p))
10533 add_pat = expand_backtick(&ga, p, flags);
10534 else
10535#endif
10536 {
10537 /*
10538 * First expand environment variables, "~/" and "~user/".
10539 */
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010540 if (vim_strchr(p, '$') != NULL || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010542 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 if (p == NULL)
10544 p = pat[i];
10545#ifdef UNIX
10546 /*
10547 * On Unix, if expand_env() can't expand an environment
10548 * variable, use the shell to do that. Discard previously
10549 * found file names and start all over again.
10550 */
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010551 else if (vim_strchr(p, '$') != NULL || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 {
10553 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010554 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10556 flags);
10557 recursive = FALSE;
10558 return i;
10559 }
10560#endif
10561 }
10562
10563 /*
10564 * If there are wildcards: Expand file names and add each match to
10565 * the list. If there is no match, and EW_NOTFOUND is given, add
10566 * the pattern.
10567 * If there are no wildcards: Add the file name if it exists or
10568 * when EW_NOTFOUND is given.
10569 */
10570 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010571 {
10572#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010573 if ((flags & EW_PATH)
10574 && !mch_isFullName(p)
10575 && !(p[0] == '.'
10576 && (vim_ispathsep(p[1])
10577 || (p[1] == '.' && vim_ispathsep(p[2]))))
10578 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010579 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010580 /* :find completion where 'path' is used.
10581 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010582 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010583 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010584 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010585 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010586 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010587 else
10588#endif
10589 add_pat = mch_expandpath(&ga, p, flags);
10590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 }
10592
10593 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10594 {
10595 char_u *t = backslash_halve_save(p);
10596
10597#if defined(MACOS_CLASSIC)
10598 slash_to_colon(t);
10599#endif
10600 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10601 * "vim c:/" work. */
10602 if (flags & EW_NOTFOUND)
10603 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10604 else if (mch_getperm(t) >= 0)
10605 addfile(&ga, t, flags);
10606 vim_free(t);
10607 }
10608
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010609#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010610 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010611 uniquefy_paths(&ga, p);
10612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 if (p != pat[i])
10614 vim_free(p);
10615 }
10616
10617 *num_file = ga.ga_len;
10618 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10619
10620 recursive = FALSE;
10621
10622 return (ga.ga_data != NULL) ? OK : FAIL;
10623}
10624
10625# ifdef VIM_BACKTICK
10626
10627/*
10628 * Return TRUE if we can expand this backtick thing here.
10629 */
10630 static int
10631vim_backtick(p)
10632 char_u *p;
10633{
10634 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10635}
10636
10637/*
10638 * Expand an item in `backticks` by executing it as a command.
10639 * Currently only works when pat[] starts and ends with a `.
10640 * Returns number of file names found.
10641 */
10642 static int
10643expand_backtick(gap, pat, flags)
10644 garray_T *gap;
10645 char_u *pat;
10646 int flags; /* EW_* flags */
10647{
10648 char_u *p;
10649 char_u *cmd;
10650 char_u *buffer;
10651 int cnt = 0;
10652 int i;
10653
10654 /* Create the command: lop off the backticks. */
10655 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10656 if (cmd == NULL)
10657 return 0;
10658
10659#ifdef FEAT_EVAL
10660 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010661 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 else
10663#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010664 buffer = get_cmd_output(cmd, NULL,
10665 (flags & EW_SILENT) ? SHELL_SILENT : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 vim_free(cmd);
10667 if (buffer == NULL)
10668 return 0;
10669
10670 cmd = buffer;
10671 while (*cmd != NUL)
10672 {
10673 cmd = skipwhite(cmd); /* skip over white space */
10674 p = cmd;
10675 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10676 ++p;
10677 /* add an entry if it is not empty */
10678 if (p > cmd)
10679 {
10680 i = *p;
10681 *p = NUL;
10682 addfile(gap, cmd, flags);
10683 *p = i;
10684 ++cnt;
10685 }
10686 cmd = p;
10687 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10688 ++cmd;
10689 }
10690
10691 vim_free(buffer);
10692 return cnt;
10693}
10694# endif /* VIM_BACKTICK */
10695
10696/*
10697 * Add a file to a file list. Accepted flags:
10698 * EW_DIR add directories
10699 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010700 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 * EW_NOTFOUND add even when it doesn't exist
10702 * EW_ADDSLASH add slash after directory name
10703 */
10704 void
10705addfile(gap, f, flags)
10706 garray_T *gap;
10707 char_u *f; /* filename */
10708 int flags;
10709{
10710 char_u *p;
10711 int isdir;
10712
10713 /* if the file/dir doesn't exist, may not add it */
10714 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10715 return;
10716
10717#ifdef FNAME_ILLEGAL
10718 /* if the file/dir contains illegal characters, don't add it */
10719 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10720 return;
10721#endif
10722
10723 isdir = mch_isdir(f);
10724 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10725 return;
10726
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010727 /* If the file isn't executable, may not add it. Do accept directories. */
10728 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f))
10729 return;
10730
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 /* Make room for another item in the file list. */
10732 if (ga_grow(gap, 1) == FAIL)
10733 return;
10734
10735 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10736 if (p == NULL)
10737 return;
10738
10739 STRCPY(p, f);
10740#ifdef BACKSLASH_IN_FILENAME
10741 slash_adjust(p);
10742#endif
10743 /*
10744 * Append a slash or backslash after directory names if none is present.
10745 */
10746#ifndef DONT_ADD_PATHSEP_TO_DIR
10747 if (isdir && (flags & EW_ADDSLASH))
10748 add_pathsep(p);
10749#endif
10750 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751}
10752#endif /* !NO_EXPANDPATH */
10753
10754#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10755
10756#ifndef SEEK_SET
10757# define SEEK_SET 0
10758#endif
10759#ifndef SEEK_END
10760# define SEEK_END 2
10761#endif
10762
10763/*
10764 * Get the stdout of an external command.
10765 * Returns an allocated string, or NULL for error.
10766 */
10767 char_u *
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010768get_cmd_output(cmd, infile, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010770 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 int flags; /* can be SHELL_SILENT */
10772{
10773 char_u *tempname;
10774 char_u *command;
10775 char_u *buffer = NULL;
10776 int len;
10777 int i = 0;
10778 FILE *fd;
10779
10780 if (check_restricted() || check_secure())
10781 return NULL;
10782
10783 /* get a name for the temp file */
10784 if ((tempname = vim_tempname('o')) == NULL)
10785 {
10786 EMSG(_(e_notmp));
10787 return NULL;
10788 }
10789
10790 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010791 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 if (command == NULL)
10793 goto done;
10794
10795 /*
10796 * Call the shell to execute the command (errors are ignored).
10797 * Don't check timestamps here.
10798 */
10799 ++no_check_timestamps;
10800 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10801 --no_check_timestamps;
10802
10803 vim_free(command);
10804
10805 /*
10806 * read the names from the file into memory
10807 */
10808# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010809 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 fd = mch_fopen((char *)tempname, "r");
10811# else
10812 fd = mch_fopen((char *)tempname, READBIN);
10813# endif
10814
10815 if (fd == NULL)
10816 {
10817 EMSG2(_(e_notopen), tempname);
10818 goto done;
10819 }
10820
10821 fseek(fd, 0L, SEEK_END);
10822 len = ftell(fd); /* get size of temp file */
10823 fseek(fd, 0L, SEEK_SET);
10824
10825 buffer = alloc(len + 1);
10826 if (buffer != NULL)
10827 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10828 fclose(fd);
10829 mch_remove(tempname);
10830 if (buffer == NULL)
10831 goto done;
10832#ifdef VMS
10833 len = i; /* VMS doesn't give us what we asked for... */
10834#endif
10835 if (i != len)
10836 {
10837 EMSG2(_(e_notread), tempname);
10838 vim_free(buffer);
10839 buffer = NULL;
10840 }
10841 else
Bram Moolenaar162bd912010-07-28 22:29:10 +020010842 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843
10844done:
10845 vim_free(tempname);
10846 return buffer;
10847}
10848#endif
10849
10850/*
10851 * Free the list of files returned by expand_wildcards() or other expansion
10852 * functions.
10853 */
10854 void
10855FreeWild(count, files)
10856 int count;
10857 char_u **files;
10858{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010859 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 return;
10861#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10862 /*
10863 * Is this still OK for when other functions than expand_wildcards() have
10864 * been used???
10865 */
10866 _fnexplodefree((char **)files);
10867#else
10868 while (count--)
10869 vim_free(files[count]);
10870 vim_free(files);
10871#endif
10872}
10873
10874/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010875 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 * Don't do this when still processing a command or a mapping.
10877 * Don't do this when inside a ":normal" command.
10878 */
10879 int
10880goto_im()
10881{
10882 return (p_im && stuff_empty() && typebuf_typed());
10883}