blob: be0d7d0255cda7920aa71e586417908d4bb83a07 [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 Moolenaar01b626c2013-06-16 22:49:14 +020019static void init_users __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020static int copy_indent __ARGS((int size, char_u *src));
21
Bram Moolenaar24305862012-08-15 14:05:05 +020022/* All user names (for ~user completion as done by shell). */
23#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24static garray_T ga_users;
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027/*
28 * Count the size (in window cells) of the indent in the current line.
29 */
30 int
31get_indent()
32{
33 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts);
34}
35
36/*
37 * Count the size (in window cells) of the indent in line "lnum".
38 */
39 int
40get_indent_lnum(lnum)
41 linenr_T lnum;
42{
43 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts);
44}
45
46#if defined(FEAT_FOLDING) || defined(PROTO)
47/*
48 * Count the size (in window cells) of the indent in line "lnum" of buffer
49 * "buf".
50 */
51 int
52get_indent_buf(buf, lnum)
53 buf_T *buf;
54 linenr_T lnum;
55{
56 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts);
57}
58#endif
59
60/*
61 * count the size (in window cells) of the indent in line "ptr", with
62 * 'tabstop' at "ts"
63 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000064 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000065get_indent_str(ptr, ts)
66 char_u *ptr;
67 int ts;
68{
69 int count = 0;
70
71 for ( ; *ptr; ++ptr)
72 {
73 if (*ptr == TAB) /* count a tab for what it is worth */
74 count += ts - (count % ts);
75 else if (*ptr == ' ')
76 ++count; /* count a space for one */
77 else
78 break;
79 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000080 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000081}
82
83/*
84 * Set the indent of the current line.
85 * Leaves the cursor on the first non-blank in the line.
86 * Caller must take care of undo.
87 * "flags":
88 * SIN_CHANGED: call changed_bytes() if the line was changed.
89 * SIN_INSERT: insert the indent in front of the line.
90 * SIN_UNDO: save line for undo before changing it.
91 * Returns TRUE if the line was changed.
92 */
93 int
94set_indent(size, flags)
Bram Moolenaar5002c292007-07-24 13:26:15 +000095 int size; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 int flags;
97{
98 char_u *p;
99 char_u *newline;
100 char_u *oldline;
101 char_u *s;
102 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000103 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 int line_len;
105 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000106 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000108 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000109 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000110 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
112 /*
113 * First check if there is anything to do and compute the number of
114 * characters needed for the indent.
115 */
116 todo = size;
117 ind_len = 0;
118 p = oldline = ml_get_curline();
119
120 /* Calculate the buffer size for the new indent, and check to see if it
121 * isn't already set */
122
Bram Moolenaar5002c292007-07-24 13:26:15 +0000123 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
124 * 'preserveindent' are set count the number of characters at the
125 * beginning of the line to be copied */
126 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 {
128 /* If 'preserveindent' is set then reuse as much as possible of
129 * the existing indent structure for the new indent */
130 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
131 {
132 ind_done = 0;
133
134 /* count as many characters as we can use */
135 while (todo > 0 && vim_iswhite(*p))
136 {
137 if (*p == TAB)
138 {
139 tab_pad = (int)curbuf->b_p_ts
140 - (ind_done % (int)curbuf->b_p_ts);
141 /* stop if this tab will overshoot the target */
142 if (todo < tab_pad)
143 break;
144 todo -= tab_pad;
145 ++ind_len;
146 ind_done += tab_pad;
147 }
148 else
149 {
150 --todo;
151 ++ind_len;
152 ++ind_done;
153 }
154 ++p;
155 }
156
Bram Moolenaar5002c292007-07-24 13:26:15 +0000157 /* Set initial number of whitespace chars to copy if we are
158 * preserving indent but expandtab is set */
159 if (curbuf->b_p_et)
160 orig_char_len = ind_len;
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 /* Fill to next tabstop with a tab, if possible */
163 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000164 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 {
166 doit = TRUE;
167 todo -= tab_pad;
168 ++ind_len;
169 /* ind_done += tab_pad; */
170 }
171 }
172
173 /* count tabs required for indent */
174 while (todo >= (int)curbuf->b_p_ts)
175 {
176 if (*p != TAB)
177 doit = TRUE;
178 else
179 ++p;
180 todo -= (int)curbuf->b_p_ts;
181 ++ind_len;
182 /* ind_done += (int)curbuf->b_p_ts; */
183 }
184 }
185 /* count spaces required for indent */
186 while (todo > 0)
187 {
188 if (*p != ' ')
189 doit = TRUE;
190 else
191 ++p;
192 --todo;
193 ++ind_len;
194 /* ++ind_done; */
195 }
196
197 /* Return if the indent is OK already. */
198 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
199 return FALSE;
200
201 /* Allocate memory for the new line. */
202 if (flags & SIN_INSERT)
203 p = oldline;
204 else
205 p = skipwhite(p);
206 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000207
208 /* If 'preserveindent' and 'expandtab' are both set keep the original
209 * characters and allocate accordingly. We will fill the rest with spaces
210 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000211 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000212 {
213 newline = alloc(orig_char_len + size - ind_done + line_len);
214 if (newline == NULL)
215 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000216 todo = size - ind_done;
217 ind_len = orig_char_len + todo; /* Set total length of indent in
218 * characters, which may have been
219 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000220 p = oldline;
221 s = newline;
222 while (orig_char_len > 0)
223 {
224 *s++ = *p++;
225 orig_char_len--;
226 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000227
Bram Moolenaar5002c292007-07-24 13:26:15 +0000228 /* Skip over any additional white space (useful when newindent is less
229 * than old) */
230 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000231 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000232
Bram Moolenaar5002c292007-07-24 13:26:15 +0000233 }
234 else
235 {
236 todo = size;
237 newline = alloc(ind_len + line_len);
238 if (newline == NULL)
239 return FALSE;
240 s = newline;
241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242
243 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 /* if 'expandtab' isn't set: use TABs */
245 if (!curbuf->b_p_et)
246 {
247 /* If 'preserveindent' is set then reuse as much as possible of
248 * the existing indent structure for the new indent */
249 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
250 {
251 p = oldline;
252 ind_done = 0;
253
254 while (todo > 0 && vim_iswhite(*p))
255 {
256 if (*p == TAB)
257 {
258 tab_pad = (int)curbuf->b_p_ts
259 - (ind_done % (int)curbuf->b_p_ts);
260 /* stop if this tab will overshoot the target */
261 if (todo < tab_pad)
262 break;
263 todo -= tab_pad;
264 ind_done += tab_pad;
265 }
266 else
267 {
268 --todo;
269 ++ind_done;
270 }
271 *s++ = *p++;
272 }
273
274 /* Fill to next tabstop with a tab, if possible */
275 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
276 if (todo >= tab_pad)
277 {
278 *s++ = TAB;
279 todo -= tab_pad;
280 }
281
282 p = skipwhite(p);
283 }
284
285 while (todo >= (int)curbuf->b_p_ts)
286 {
287 *s++ = TAB;
288 todo -= (int)curbuf->b_p_ts;
289 }
290 }
291 while (todo > 0)
292 {
293 *s++ = ' ';
294 --todo;
295 }
296 mch_memmove(s, p, (size_t)line_len);
297
298 /* Replace the line (unless undo fails). */
299 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
300 {
301 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
302 if (flags & SIN_CHANGED)
303 changed_bytes(curwin->w_cursor.lnum, 0);
304 /* Correct saved cursor position if it's after the indent. */
305 if (saved_cursor.lnum == curwin->w_cursor.lnum
306 && saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000307 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
Bram Moolenaar5409c052005-03-18 20:27:04 +0000308 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 }
310 else
311 vim_free(newline);
312
313 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000314 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315}
316
317/*
318 * Copy the indent from ptr to the current line (and fill to size)
319 * Leaves the cursor on the first non-blank in the line.
320 * Returns TRUE if the line was changed.
321 */
322 static int
323copy_indent(size, src)
324 int size;
325 char_u *src;
326{
327 char_u *p = NULL;
328 char_u *line = NULL;
329 char_u *s;
330 int todo;
331 int ind_len;
332 int line_len = 0;
333 int tab_pad;
334 int ind_done;
335 int round;
336
337 /* Round 1: compute the number of characters needed for the indent
338 * Round 2: copy the characters. */
339 for (round = 1; round <= 2; ++round)
340 {
341 todo = size;
342 ind_len = 0;
343 ind_done = 0;
344 s = src;
345
346 /* Count/copy the usable portion of the source line */
347 while (todo > 0 && vim_iswhite(*s))
348 {
349 if (*s == TAB)
350 {
351 tab_pad = (int)curbuf->b_p_ts
352 - (ind_done % (int)curbuf->b_p_ts);
353 /* Stop if this tab will overshoot the target */
354 if (todo < tab_pad)
355 break;
356 todo -= tab_pad;
357 ind_done += tab_pad;
358 }
359 else
360 {
361 --todo;
362 ++ind_done;
363 }
364 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000365 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 *p++ = *s;
367 ++s;
368 }
369
370 /* Fill to next tabstop with a tab, if possible */
371 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200372 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 {
374 todo -= tab_pad;
375 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000376 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 *p++ = TAB;
378 }
379
380 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200381 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
383 todo -= (int)curbuf->b_p_ts;
384 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000385 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 *p++ = TAB;
387 }
388
389 /* Count/add spaces required for indent */
390 while (todo > 0)
391 {
392 --todo;
393 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000394 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 *p++ = ' ';
396 }
397
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000398 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {
400 /* Allocate memory for the result: the copied indent, new indent
401 * and the rest of the line. */
402 line_len = (int)STRLEN(ml_get_curline()) + 1;
403 line = alloc(ind_len + line_len);
404 if (line == NULL)
405 return FALSE;
406 p = line;
407 }
408 }
409
410 /* Append the original line */
411 mch_memmove(p, ml_get_curline(), (size_t)line_len);
412
413 /* Replace the line */
414 ml_replace(curwin->w_cursor.lnum, line, FALSE);
415
416 /* Put the cursor after the indent. */
417 curwin->w_cursor.col = ind_len;
418 return TRUE;
419}
420
421/*
422 * Return the indent of the current line after a number. Return -1 if no
423 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000424 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 */
426 int
427get_number_indent(lnum)
428 linenr_T lnum;
429{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 colnr_T col;
431 pos_T pos;
432
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200433 regmatch_T regmatch;
434 int lead_len = 0; /* length of comment leader */
435
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 if (lnum > curbuf->b_ml.ml_line_count)
437 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000438 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200439
440#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200441 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
442 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200443 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000444#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200445 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
446 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200447 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200448 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200449
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200450 /* vim_regexec() expects a pointer to a line. This lets us
451 * start matching for the flp beyond any comment leader... */
452 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200453 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200454 pos.lnum = lnum;
455 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200456#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200458#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200460 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000462
463 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 getvcol(curwin, &pos, &col, NULL, NULL);
466 return (int)col;
467}
468
469#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
470
471static int cin_is_cinword __ARGS((char_u *line));
472
473/*
474 * Return TRUE if the string "line" starts with a word from 'cinwords'.
475 */
476 static int
477cin_is_cinword(line)
478 char_u *line;
479{
480 char_u *cinw;
481 char_u *cinw_buf;
482 int cinw_len;
483 int retval = FALSE;
484 int len;
485
486 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
487 cinw_buf = alloc((unsigned)cinw_len);
488 if (cinw_buf != NULL)
489 {
490 line = skipwhite(line);
491 for (cinw = curbuf->b_p_cinw; *cinw; )
492 {
493 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
494 if (STRNCMP(line, cinw_buf, len) == 0
495 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
496 {
497 retval = TRUE;
498 break;
499 }
500 }
501 vim_free(cinw_buf);
502 }
503 return retval;
504}
505#endif
506
507/*
508 * open_line: Add a new line below or above the current line.
509 *
510 * For VREPLACE mode, we only add a new line when we get to the end of the
511 * file, otherwise we just start replacing the next line.
512 *
513 * Caller must take care of undo. Since VREPLACE may affect any number of
514 * lines however, it may call u_save_cursor() again when starting to change a
515 * new line.
516 * "flags": OPENLINE_DELSPACES delete spaces after cursor
517 * OPENLINE_DO_COM format comments
518 * OPENLINE_KEEPTRAIL keep trailing spaces
519 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200520 * OPENLINE_COM_LIST format comments with list or 2nd line indent
521 *
522 * "second_line_indent": indent for after ^^D in Insert mode or if flag
523 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 *
525 * Return TRUE for success, FALSE for failure
526 */
527 int
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200528open_line(dir, flags, second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 int dir; /* FORWARD or BACKWARD */
530 int flags;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200531 int second_line_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
533 char_u *saved_line; /* copy of the original line */
534 char_u *next_line = NULL; /* copy of the next line */
535 char_u *p_extra = NULL; /* what goes to next line */
536 int less_cols = 0; /* less columns for mark in new line */
537 int less_cols_off = 0; /* columns to skip for mark adjust */
538 pos_T old_cursor; /* old cursor position */
539 int newcol = 0; /* new cursor column */
540 int newindent = 0; /* auto-indent of the new line */
541 int n;
542 int trunc_line = FALSE; /* truncate current line afterwards */
543 int retval = FALSE; /* return value, default is FAIL */
544#ifdef FEAT_COMMENTS
545 int extra_len = 0; /* length of p_extra string */
546 int lead_len; /* length of comment leader */
547 char_u *lead_flags; /* position in 'comments' for comment leader */
548 char_u *leader = NULL; /* copy of comment leader */
549#endif
550 char_u *allocated = NULL; /* allocated memory */
551#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
552 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
553 char_u *p;
554#endif
555 int saved_char = NUL; /* init for GCC */
556#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
557 pos_T *pos;
558#endif
559#ifdef FEAT_SMARTINDENT
560 int do_si = (!p_paste && curbuf->b_p_si
561# ifdef FEAT_CINDENT
562 && !curbuf->b_p_cin
563# endif
564 );
565 int no_si = FALSE; /* reset did_si afterwards */
566 int first_char = NUL; /* init for GCC */
567#endif
568#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
569 int vreplace_mode;
570#endif
571 int did_append; /* appended a new line */
572 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
573
574 /*
575 * make a copy of the current line so we can mess with it
576 */
577 saved_line = vim_strsave(ml_get_curline());
578 if (saved_line == NULL) /* out of memory! */
579 return FALSE;
580
581#ifdef FEAT_VREPLACE
582 if (State & VREPLACE_FLAG)
583 {
584 /*
585 * With VREPLACE we make a copy of the next line, which we will be
586 * starting to replace. First make the new line empty and let vim play
587 * with the indenting and comment leader to its heart's content. Then
588 * we grab what it ended up putting on the new line, put back the
589 * original line, and call ins_char() to put each new character onto
590 * the line, replacing what was there before and pushing the right
591 * stuff onto the replace stack. -- webb.
592 */
593 if (curwin->w_cursor.lnum < orig_line_count)
594 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
595 else
596 next_line = vim_strsave((char_u *)"");
597 if (next_line == NULL) /* out of memory! */
598 goto theend;
599
600 /*
601 * In VREPLACE mode, a NL replaces the rest of the line, and starts
602 * replacing the next line, so push all of the characters left on the
603 * line onto the replace stack. We'll push any other characters that
604 * might be replaced at the start of the next line (due to autoindent
605 * etc) a bit later.
606 */
607 replace_push(NUL); /* Call twice because BS over NL expects it */
608 replace_push(NUL);
609 p = saved_line + curwin->w_cursor.col;
610 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000611 {
612#ifdef FEAT_MBYTE
613 if (has_mbyte)
614 p += replace_push_mb(p);
615 else
616#endif
617 replace_push(*p++);
618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 saved_line[curwin->w_cursor.col] = NUL;
620 }
621#endif
622
623 if ((State & INSERT)
624#ifdef FEAT_VREPLACE
625 && !(State & VREPLACE_FLAG)
626#endif
627 )
628 {
629 p_extra = saved_line + curwin->w_cursor.col;
630#ifdef FEAT_SMARTINDENT
631 if (do_si) /* need first char after new line break */
632 {
633 p = skipwhite(p_extra);
634 first_char = *p;
635 }
636#endif
637#ifdef FEAT_COMMENTS
638 extra_len = (int)STRLEN(p_extra);
639#endif
640 saved_char = *p_extra;
641 *p_extra = NUL;
642 }
643
644 u_clearline(); /* cannot do "U" command when adding lines */
645#ifdef FEAT_SMARTINDENT
646 did_si = FALSE;
647#endif
648 ai_col = 0;
649
650 /*
651 * If we just did an auto-indent, then we didn't type anything on
652 * the prior line, and it should be truncated. Do this even if 'ai' is not
653 * set because automatically inserting a comment leader also sets did_ai.
654 */
655 if (dir == FORWARD && did_ai)
656 trunc_line = TRUE;
657
658 /*
659 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
660 * indent to use for the new line.
661 */
662 if (curbuf->b_p_ai
663#ifdef FEAT_SMARTINDENT
664 || do_si
665#endif
666 )
667 {
668 /*
669 * count white space on current line
670 */
671 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200672 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
673 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675#ifdef FEAT_SMARTINDENT
676 /*
677 * Do smart indenting.
678 * In insert/replace mode (only when dir == FORWARD)
679 * we may move some text to the next line. If it starts with '{'
680 * don't add an indent. Fixes inserting a NL before '{' in line
681 * "if (condition) {"
682 */
683 if (!trunc_line && do_si && *saved_line != NUL
684 && (p_extra == NULL || first_char != '{'))
685 {
686 char_u *ptr;
687 char_u last_char;
688
689 old_cursor = curwin->w_cursor;
690 ptr = saved_line;
691# ifdef FEAT_COMMENTS
692 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200693 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 else
695 lead_len = 0;
696# endif
697 if (dir == FORWARD)
698 {
699 /*
700 * Skip preprocessor directives, unless they are
701 * recognised as comments.
702 */
703 if (
704# ifdef FEAT_COMMENTS
705 lead_len == 0 &&
706# endif
707 ptr[0] == '#')
708 {
709 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
710 ptr = ml_get(--curwin->w_cursor.lnum);
711 newindent = get_indent();
712 }
713# ifdef FEAT_COMMENTS
714 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200715 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 else
717 lead_len = 0;
718 if (lead_len > 0)
719 {
720 /*
721 * This case gets the following right:
722 * \*
723 * * A comment (read '\' as '/').
724 * *\
725 * #define IN_THE_WAY
726 * This should line up here;
727 */
728 p = skipwhite(ptr);
729 if (p[0] == '/' && p[1] == '*')
730 p++;
731 if (p[0] == '*')
732 {
733 for (p++; *p; p++)
734 {
735 if (p[0] == '/' && p[-1] == '*')
736 {
737 /*
738 * End of C comment, indent should line up
739 * with the line containing the start of
740 * the comment
741 */
742 curwin->w_cursor.col = (colnr_T)(p - ptr);
743 if ((pos = findmatch(NULL, NUL)) != NULL)
744 {
745 curwin->w_cursor.lnum = pos->lnum;
746 newindent = get_indent();
747 }
748 }
749 }
750 }
751 }
752 else /* Not a comment line */
753# endif
754 {
755 /* Find last non-blank in line */
756 p = ptr + STRLEN(ptr) - 1;
757 while (p > ptr && vim_iswhite(*p))
758 --p;
759 last_char = *p;
760
761 /*
762 * find the character just before the '{' or ';'
763 */
764 if (last_char == '{' || last_char == ';')
765 {
766 if (p > ptr)
767 --p;
768 while (p > ptr && vim_iswhite(*p))
769 --p;
770 }
771 /*
772 * Try to catch lines that are split over multiple
773 * lines. eg:
774 * if (condition &&
775 * condition) {
776 * Should line up here!
777 * }
778 */
779 if (*p == ')')
780 {
781 curwin->w_cursor.col = (colnr_T)(p - ptr);
782 if ((pos = findmatch(NULL, '(')) != NULL)
783 {
784 curwin->w_cursor.lnum = pos->lnum;
785 newindent = get_indent();
786 ptr = ml_get_curline();
787 }
788 }
789 /*
790 * If last character is '{' do indent, without
791 * checking for "if" and the like.
792 */
793 if (last_char == '{')
794 {
795 did_si = TRUE; /* do indent */
796 no_si = TRUE; /* don't delete it when '{' typed */
797 }
798 /*
799 * Look for "if" and the like, use 'cinwords'.
800 * Don't do this if the previous line ended in ';' or
801 * '}'.
802 */
803 else if (last_char != ';' && last_char != '}'
804 && cin_is_cinword(ptr))
805 did_si = TRUE;
806 }
807 }
808 else /* dir == BACKWARD */
809 {
810 /*
811 * Skip preprocessor directives, unless they are
812 * recognised as comments.
813 */
814 if (
815# ifdef FEAT_COMMENTS
816 lead_len == 0 &&
817# endif
818 ptr[0] == '#')
819 {
820 int was_backslashed = FALSE;
821
822 while ((ptr[0] == '#' || was_backslashed) &&
823 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
824 {
825 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
826 was_backslashed = TRUE;
827 else
828 was_backslashed = FALSE;
829 ptr = ml_get(++curwin->w_cursor.lnum);
830 }
831 if (was_backslashed)
832 newindent = 0; /* Got to end of file */
833 else
834 newindent = get_indent();
835 }
836 p = skipwhite(ptr);
837 if (*p == '}') /* if line starts with '}': do indent */
838 did_si = TRUE;
839 else /* can delete indent when '{' typed */
840 can_si_back = TRUE;
841 }
842 curwin->w_cursor = old_cursor;
843 }
844 if (do_si)
845 can_si = TRUE;
846#endif /* FEAT_SMARTINDENT */
847
848 did_ai = TRUE;
849 }
850
851#ifdef FEAT_COMMENTS
852 /*
853 * Find out if the current line starts with a comment leader.
854 * This may then be inserted in front of the new line.
855 */
856 end_comment_pending = NUL;
857 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200858 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 else
860 lead_len = 0;
861 if (lead_len > 0)
862 {
863 char_u *lead_repl = NULL; /* replaces comment leader */
864 int lead_repl_len = 0; /* length of *lead_repl */
865 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
866 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
867 char_u *comment_end = NULL; /* where lead_end has been found */
868 int extra_space = FALSE; /* append extra space */
869 int current_flag;
870 int require_blank = FALSE; /* requires blank after middle */
871 char_u *p2;
872
873 /*
874 * If the comment leader has the start, middle or end flag, it may not
875 * be used or may be replaced with the middle leader.
876 */
877 for (p = lead_flags; *p && *p != ':'; ++p)
878 {
879 if (*p == COM_BLANK)
880 {
881 require_blank = TRUE;
882 continue;
883 }
884 if (*p == COM_START || *p == COM_MIDDLE)
885 {
886 current_flag = *p;
887 if (*p == COM_START)
888 {
889 /*
890 * Doing "O" on a start of comment does not insert leader.
891 */
892 if (dir == BACKWARD)
893 {
894 lead_len = 0;
895 break;
896 }
897
898 /* find start of middle part */
899 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
900 require_blank = FALSE;
901 }
902
903 /*
904 * Isolate the strings of the middle and end leader.
905 */
906 while (*p && p[-1] != ':') /* find end of middle flags */
907 {
908 if (*p == COM_BLANK)
909 require_blank = TRUE;
910 ++p;
911 }
912 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
913
914 while (*p && p[-1] != ':') /* find end of end flags */
915 {
916 /* Check whether we allow automatic ending of comments */
917 if (*p == COM_AUTO_END)
918 end_comment_pending = -1; /* means we want to set it */
919 ++p;
920 }
921 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
922
923 if (end_comment_pending == -1) /* we can set it now */
924 end_comment_pending = lead_end[n - 1];
925
926 /*
927 * If the end of the comment is in the same line, don't use
928 * the comment leader.
929 */
930 if (dir == FORWARD)
931 {
932 for (p = saved_line + lead_len; *p; ++p)
933 if (STRNCMP(p, lead_end, n) == 0)
934 {
935 comment_end = p;
936 lead_len = 0;
937 break;
938 }
939 }
940
941 /*
942 * Doing "o" on a start of comment inserts the middle leader.
943 */
944 if (lead_len > 0)
945 {
946 if (current_flag == COM_START)
947 {
948 lead_repl = lead_middle;
949 lead_repl_len = (int)STRLEN(lead_middle);
950 }
951
952 /*
953 * If we have hit RETURN immediately after the start
954 * comment leader, then put a space after the middle
955 * comment leader on the next line.
956 */
957 if (!vim_iswhite(saved_line[lead_len - 1])
958 && ((p_extra != NULL
959 && (int)curwin->w_cursor.col == lead_len)
960 || (p_extra == NULL
961 && saved_line[lead_len] == NUL)
962 || require_blank))
963 extra_space = TRUE;
964 }
965 break;
966 }
967 if (*p == COM_END)
968 {
969 /*
970 * Doing "o" on the end of a comment does not insert leader.
971 * Remember where the end is, might want to use it to find the
972 * start (for C-comments).
973 */
974 if (dir == FORWARD)
975 {
976 comment_end = skipwhite(saved_line);
977 lead_len = 0;
978 break;
979 }
980
981 /*
982 * Doing "O" on the end of a comment inserts the middle leader.
983 * Find the string for the middle leader, searching backwards.
984 */
985 while (p > curbuf->b_p_com && *p != ',')
986 --p;
987 for (lead_repl = p; lead_repl > curbuf->b_p_com
988 && lead_repl[-1] != ':'; --lead_repl)
989 ;
990 lead_repl_len = (int)(p - lead_repl);
991
992 /* We can probably always add an extra space when doing "O" on
993 * the comment-end */
994 extra_space = TRUE;
995
996 /* Check whether we allow automatic ending of comments */
997 for (p2 = p; *p2 && *p2 != ':'; p2++)
998 {
999 if (*p2 == COM_AUTO_END)
1000 end_comment_pending = -1; /* means we want to set it */
1001 }
1002 if (end_comment_pending == -1)
1003 {
1004 /* Find last character in end-comment string */
1005 while (*p2 && *p2 != ',')
1006 p2++;
1007 end_comment_pending = p2[-1];
1008 }
1009 break;
1010 }
1011 if (*p == COM_FIRST)
1012 {
1013 /*
1014 * Comment leader for first line only: Don't repeat leader
1015 * when using "O", blank out leader when using "o".
1016 */
1017 if (dir == BACKWARD)
1018 lead_len = 0;
1019 else
1020 {
1021 lead_repl = (char_u *)"";
1022 lead_repl_len = 0;
1023 }
1024 break;
1025 }
1026 }
1027 if (lead_len)
1028 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001029 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001030 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001031 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 allocated = leader; /* remember to free it later */
1033
1034 if (leader == NULL)
1035 lead_len = 0;
1036 else
1037 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001038 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040 /*
1041 * Replace leader with lead_repl, right or left adjusted
1042 */
1043 if (lead_repl != NULL)
1044 {
1045 int c = 0;
1046 int off = 0;
1047
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001048 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
1050 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001051 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 else if (VIM_ISDIGIT(*p) || *p == '-')
1053 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001054 else
1055 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057 if (c == COM_RIGHT) /* right adjusted leader */
1058 {
1059 /* find last non-white in the leader to line up with */
1060 for (p = leader + lead_len - 1; p > leader
1061 && vim_iswhite(*p); --p)
1062 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001064
1065#ifdef FEAT_MBYTE
1066 /* Compute the length of the replaced characters in
1067 * screen characters, not bytes. */
1068 {
1069 int repl_size = vim_strnsize(lead_repl,
1070 lead_repl_len);
1071 int old_size = 0;
1072 char_u *endp = p;
1073 int l;
1074
1075 while (old_size < repl_size && p > leader)
1076 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001077 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001078 old_size += ptr2cells(p);
1079 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001080 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001081 if (l != 0)
1082 mch_memmove(endp + l, endp,
1083 (size_t)((leader + lead_len) - endp));
1084 lead_len += l;
1085 }
1086#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (p < leader + lead_repl_len)
1088 p = leader;
1089 else
1090 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1093 if (p + lead_repl_len > leader + lead_len)
1094 p[lead_repl_len] = NUL;
1095
1096 /* blank-out any other chars from the old leader. */
1097 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001098 {
1099#ifdef FEAT_MBYTE
1100 int l = mb_head_off(leader, p);
1101
1102 if (l > 1)
1103 {
1104 p -= l;
1105 if (ptr2cells(p) > 1)
1106 {
1107 p[1] = ' ';
1108 --l;
1109 }
1110 mch_memmove(p + 1, p + l + 1,
1111 (size_t)((leader + lead_len) - (p + l + 1)));
1112 lead_len -= l;
1113 *p = ' ';
1114 }
1115 else
1116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 if (!vim_iswhite(*p))
1118 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
1121 else /* left adjusted leader */
1122 {
1123 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001124#ifdef FEAT_MBYTE
1125 /* Compute the length of the replaced characters in
1126 * screen characters, not bytes. Move the part that is
1127 * not to be overwritten. */
1128 {
1129 int repl_size = vim_strnsize(lead_repl,
1130 lead_repl_len);
1131 int i;
1132 int l;
1133
1134 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1135 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001136 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001137 if (vim_strnsize(p, i + l) > repl_size)
1138 break;
1139 }
1140 if (i != lead_repl_len)
1141 {
1142 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001143 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001144 lead_len += lead_repl_len - i;
1145 }
1146 }
1147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1149
1150 /* Replace any remaining non-white chars in the old
1151 * leader by spaces. Keep Tabs, the indent must
1152 * remain the same. */
1153 for (p += lead_repl_len; p < leader + lead_len; ++p)
1154 if (!vim_iswhite(*p))
1155 {
1156 /* Don't put a space before a TAB. */
1157 if (p + 1 < leader + lead_len && p[1] == TAB)
1158 {
1159 --lead_len;
1160 mch_memmove(p, p + 1,
1161 (leader + lead_len) - p);
1162 }
1163 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001164 {
1165#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001166 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001167
1168 if (l > 1)
1169 {
1170 if (ptr2cells(p) > 1)
1171 {
1172 /* Replace a double-wide char with
1173 * two spaces */
1174 --l;
1175 *p++ = ' ';
1176 }
1177 mch_memmove(p + 1, p + l,
1178 (leader + lead_len) - p);
1179 lead_len -= l - 1;
1180 }
1181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 *p = NUL;
1186 }
1187
1188 /* Recompute the indent, it may have changed. */
1189 if (curbuf->b_p_ai
1190#ifdef FEAT_SMARTINDENT
1191 || do_si
1192#endif
1193 )
1194 newindent = get_indent_str(leader, (int)curbuf->b_p_ts);
1195
1196 /* Add the indent offset */
1197 if (newindent + off < 0)
1198 {
1199 off = -newindent;
1200 newindent = 0;
1201 }
1202 else
1203 newindent += off;
1204
1205 /* Correct trailing spaces for the shift, so that
1206 * alignment remains equal. */
1207 while (off > 0 && lead_len > 0
1208 && leader[lead_len - 1] == ' ')
1209 {
1210 /* Don't do it when there is a tab before the space */
1211 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1212 break;
1213 --lead_len;
1214 --off;
1215 }
1216
1217 /* If the leader ends in white space, don't add an
1218 * extra space */
1219 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1220 extra_space = FALSE;
1221 leader[lead_len] = NUL;
1222 }
1223
1224 if (extra_space)
1225 {
1226 leader[lead_len++] = ' ';
1227 leader[lead_len] = NUL;
1228 }
1229
1230 newcol = lead_len;
1231
1232 /*
1233 * if a new indent will be set below, remove the indent that
1234 * is in the comment leader
1235 */
1236 if (newindent
1237#ifdef FEAT_SMARTINDENT
1238 || did_si
1239#endif
1240 )
1241 {
1242 while (lead_len && vim_iswhite(*leader))
1243 {
1244 --lead_len;
1245 --newcol;
1246 ++leader;
1247 }
1248 }
1249
1250 }
1251#ifdef FEAT_SMARTINDENT
1252 did_si = can_si = FALSE;
1253#endif
1254 }
1255 else if (comment_end != NULL)
1256 {
1257 /*
1258 * We have finished a comment, so we don't use the leader.
1259 * If this was a C-comment and 'ai' or 'si' is set do a normal
1260 * indent to align with the line containing the start of the
1261 * comment.
1262 */
1263 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1264 (curbuf->b_p_ai
1265#ifdef FEAT_SMARTINDENT
1266 || do_si
1267#endif
1268 ))
1269 {
1270 old_cursor = curwin->w_cursor;
1271 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1272 if ((pos = findmatch(NULL, NUL)) != NULL)
1273 {
1274 curwin->w_cursor.lnum = pos->lnum;
1275 newindent = get_indent();
1276 }
1277 curwin->w_cursor = old_cursor;
1278 }
1279 }
1280 }
1281#endif
1282
1283 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1284 if (p_extra != NULL)
1285 {
1286 *p_extra = saved_char; /* restore char that NUL replaced */
1287
1288 /*
1289 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1290 * non-blank.
1291 *
1292 * When in REPLACE mode, put the deleted blanks on the replace stack,
1293 * preceded by a NUL, so they can be put back when a BS is entered.
1294 */
1295 if (REPLACE_NORMAL(State))
1296 replace_push(NUL); /* end of extra blanks */
1297 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1298 {
1299 while ((*p_extra == ' ' || *p_extra == '\t')
1300#ifdef FEAT_MBYTE
1301 && (!enc_utf8
1302 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1303#endif
1304 )
1305 {
1306 if (REPLACE_NORMAL(State))
1307 replace_push(*p_extra);
1308 ++p_extra;
1309 ++less_cols_off;
1310 }
1311 }
1312 if (*p_extra != NUL)
1313 did_ai = FALSE; /* append some text, don't truncate now */
1314
1315 /* columns for marks adjusted for removed columns */
1316 less_cols = (int)(p_extra - saved_line);
1317 }
1318
1319 if (p_extra == NULL)
1320 p_extra = (char_u *)""; /* append empty line */
1321
1322#ifdef FEAT_COMMENTS
1323 /* concatenate leader and p_extra, if there is a leader */
1324 if (lead_len)
1325 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001326 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1327 {
1328 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001329 int padding = second_line_indent
1330 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001331
1332 /* Here whitespace is inserted after the comment char.
1333 * Below, set_indent(newindent, SIN_INSERT) will insert the
1334 * whitespace needed before the comment char. */
1335 for (i = 0; i < padding; i++)
1336 {
1337 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001338 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001339 newcol++;
1340 }
1341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 STRCAT(leader, p_extra);
1343 p_extra = leader;
1344 did_ai = TRUE; /* So truncating blanks works with comments */
1345 less_cols -= lead_len;
1346 }
1347 else
1348 end_comment_pending = NUL; /* turns out there was no leader */
1349#endif
1350
1351 old_cursor = curwin->w_cursor;
1352 if (dir == BACKWARD)
1353 --curwin->w_cursor.lnum;
1354#ifdef FEAT_VREPLACE
1355 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1356#endif
1357 {
1358 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1359 == FAIL)
1360 goto theend;
1361 /* Postpone calling changed_lines(), because it would mess up folding
1362 * with markers. */
1363 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1364 did_append = TRUE;
1365 }
1366#ifdef FEAT_VREPLACE
1367 else
1368 {
1369 /*
1370 * In VREPLACE mode we are starting to replace the next line.
1371 */
1372 curwin->w_cursor.lnum++;
1373 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1374 {
1375 /* In case we NL to a new line, BS to the previous one, and NL
1376 * again, we don't want to save the new line for undo twice.
1377 */
1378 (void)u_save_cursor(); /* errors are ignored! */
1379 vr_lines_changed++;
1380 }
1381 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1382 changed_bytes(curwin->w_cursor.lnum, 0);
1383 curwin->w_cursor.lnum--;
1384 did_append = FALSE;
1385 }
1386#endif
1387
1388 if (newindent
1389#ifdef FEAT_SMARTINDENT
1390 || did_si
1391#endif
1392 )
1393 {
1394 ++curwin->w_cursor.lnum;
1395#ifdef FEAT_SMARTINDENT
1396 if (did_si)
1397 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02001398 int sw = (int)get_sw_value();
1399
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001401 newindent -= newindent % sw;
1402 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001405 /* Copy the indent */
1406 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
1408 (void)copy_indent(newindent, saved_line);
1409
1410 /*
1411 * Set the 'preserveindent' option so that any further screwing
1412 * with the line doesn't entirely destroy our efforts to preserve
1413 * it. It gets restored at the function end.
1414 */
1415 curbuf->b_p_pi = TRUE;
1416 }
1417 else
1418 (void)set_indent(newindent, SIN_INSERT);
1419 less_cols -= curwin->w_cursor.col;
1420
1421 ai_col = curwin->w_cursor.col;
1422
1423 /*
1424 * In REPLACE mode, for each character in the new indent, there must
1425 * be a NUL on the replace stack, for when it is deleted with BS
1426 */
1427 if (REPLACE_NORMAL(State))
1428 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1429 replace_push(NUL);
1430 newcol += curwin->w_cursor.col;
1431#ifdef FEAT_SMARTINDENT
1432 if (no_si)
1433 did_si = FALSE;
1434#endif
1435 }
1436
1437#ifdef FEAT_COMMENTS
1438 /*
1439 * In REPLACE mode, for each character in the extra leader, there must be
1440 * a NUL on the replace stack, for when it is deleted with BS.
1441 */
1442 if (REPLACE_NORMAL(State))
1443 while (lead_len-- > 0)
1444 replace_push(NUL);
1445#endif
1446
1447 curwin->w_cursor = old_cursor;
1448
1449 if (dir == FORWARD)
1450 {
1451 if (trunc_line || (State & INSERT))
1452 {
1453 /* truncate current line at cursor */
1454 saved_line[curwin->w_cursor.col] = NUL;
1455 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1456 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1457 truncate_spaces(saved_line);
1458 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1459 saved_line = NULL;
1460 if (did_append)
1461 {
1462 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1463 curwin->w_cursor.lnum + 1, 1L);
1464 did_append = FALSE;
1465
1466 /* Move marks after the line break to the new line. */
1467 if (flags & OPENLINE_MARKFIX)
1468 mark_col_adjust(curwin->w_cursor.lnum,
1469 curwin->w_cursor.col + less_cols_off,
1470 1L, (long)-less_cols);
1471 }
1472 else
1473 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1474 }
1475
1476 /*
1477 * Put the cursor on the new line. Careful: the scrollup() above may
1478 * have moved w_cursor, we must use old_cursor.
1479 */
1480 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1481 }
1482 if (did_append)
1483 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1484
1485 curwin->w_cursor.col = newcol;
1486#ifdef FEAT_VIRTUALEDIT
1487 curwin->w_cursor.coladd = 0;
1488#endif
1489
1490#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1491 /*
1492 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1493 * fixthisline() from doing it (via change_indent()) by telling it we're in
1494 * normal INSERT mode.
1495 */
1496 if (State & VREPLACE_FLAG)
1497 {
1498 vreplace_mode = State; /* So we know to put things right later */
1499 State = INSERT;
1500 }
1501 else
1502 vreplace_mode = 0;
1503#endif
1504#ifdef FEAT_LISP
1505 /*
1506 * May do lisp indenting.
1507 */
1508 if (!p_paste
1509# ifdef FEAT_COMMENTS
1510 && leader == NULL
1511# endif
1512 && curbuf->b_p_lisp
1513 && curbuf->b_p_ai)
1514 {
1515 fixthisline(get_lisp_indent);
1516 p = ml_get_curline();
1517 ai_col = (colnr_T)(skipwhite(p) - p);
1518 }
1519#endif
1520#ifdef FEAT_CINDENT
1521 /*
1522 * May do indenting after opening a new line.
1523 */
1524 if (!p_paste
1525 && (curbuf->b_p_cin
1526# ifdef FEAT_EVAL
1527 || *curbuf->b_p_inde != NUL
1528# endif
1529 )
1530 && in_cinkeys(dir == FORWARD
1531 ? KEY_OPEN_FORW
1532 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1533 {
1534 do_c_expr_indent();
1535 p = ml_get_curline();
1536 ai_col = (colnr_T)(skipwhite(p) - p);
1537 }
1538#endif
1539#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1540 if (vreplace_mode != 0)
1541 State = vreplace_mode;
1542#endif
1543
1544#ifdef FEAT_VREPLACE
1545 /*
1546 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1547 * original line, and inserts the new stuff char by char, pushing old stuff
1548 * onto the replace stack (via ins_char()).
1549 */
1550 if (State & VREPLACE_FLAG)
1551 {
1552 /* Put new line in p_extra */
1553 p_extra = vim_strsave(ml_get_curline());
1554 if (p_extra == NULL)
1555 goto theend;
1556
1557 /* Put back original line */
1558 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1559
1560 /* Insert new stuff into line again */
1561 curwin->w_cursor.col = 0;
1562#ifdef FEAT_VIRTUALEDIT
1563 curwin->w_cursor.coladd = 0;
1564#endif
1565 ins_bytes(p_extra); /* will call changed_bytes() */
1566 vim_free(p_extra);
1567 next_line = NULL;
1568 }
1569#endif
1570
1571 retval = TRUE; /* success! */
1572theend:
1573 curbuf->b_p_pi = saved_pi;
1574 vim_free(saved_line);
1575 vim_free(next_line);
1576 vim_free(allocated);
1577 return retval;
1578}
1579
1580#if defined(FEAT_COMMENTS) || defined(PROTO)
1581/*
1582 * get_leader_len() returns the length of the prefix of the given string
1583 * which introduces a comment. If this string is not a comment then 0 is
1584 * returned.
1585 * When "flags" is not NULL, it is set to point to the flags of the recognized
1586 * comment leader.
1587 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001588 * If "include_space" is set, include trailing whitespace while calculating the
1589 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001592get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 char_u *line;
1594 char_u **flags;
1595 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001596 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001599 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 int got_com = FALSE;
1601 int found_one;
1602 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1603 char_u *string; /* pointer to comment string */
1604 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001605 int middle_match_len = 0;
1606 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001607 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar81340392012-06-06 16:12:59 +02001609 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 while (vim_iswhite(line[i])) /* leading white space is ignored */
1611 ++i;
1612
1613 /*
1614 * Repeat to match several nested comment strings.
1615 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001616 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {
1618 /*
1619 * scan through the 'comments' option for a match
1620 */
1621 found_one = FALSE;
1622 for (list = curbuf->b_p_com; *list; )
1623 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001624 /* Get one option part into part_buf[]. Advance "list" to next
1625 * one. Put "string" at start of string. */
1626 if (!got_com && flags != NULL)
1627 *flags = list; /* remember where flags started */
1628 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1630 string = vim_strchr(part_buf, ':');
1631 if (string == NULL) /* missing ':', ignore this part */
1632 continue;
1633 *string++ = NUL; /* isolate flags from string */
1634
Bram Moolenaara4271d52011-05-10 13:38:27 +02001635 /* If we found a middle match previously, use that match when this
1636 * is not a middle or end. */
1637 if (middle_match_len != 0
1638 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1639 && vim_strchr(part_buf, COM_END) == NULL)
1640 break;
1641
1642 /* When we already found a nested comment, only accept further
1643 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1645 continue;
1646
Bram Moolenaara4271d52011-05-10 13:38:27 +02001647 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1649 continue;
1650
Bram Moolenaara4271d52011-05-10 13:38:27 +02001651 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 * When string starts with white space, must have some white space
1653 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001654 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (vim_iswhite(string[0]))
1656 {
1657 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001658 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 while (vim_iswhite(string[0]))
1660 ++string;
1661 }
1662 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1663 ;
1664 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001665 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaara4271d52011-05-10 13:38:27 +02001667 /* When 'b' flag used, there must be white space or an
1668 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 if (vim_strchr(part_buf, COM_BLANK) != NULL
1670 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1671 continue;
1672
Bram Moolenaara4271d52011-05-10 13:38:27 +02001673 /* We have found a match, stop searching unless this is a middle
1674 * comment. The middle comment can be a substring of the end
1675 * comment in which case it's better to return the length of the
1676 * end comment and its flags. Thus we keep searching with middle
1677 * and end matches and use an end match if it matches better. */
1678 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1679 {
1680 if (middle_match_len == 0)
1681 {
1682 middle_match_len = j;
1683 saved_flags = prev_list;
1684 }
1685 continue;
1686 }
1687 if (middle_match_len != 0 && j > middle_match_len)
1688 /* Use this match instead of the middle match, since it's a
1689 * longer thus better match. */
1690 middle_match_len = 0;
1691
1692 if (middle_match_len == 0)
1693 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 found_one = TRUE;
1695 break;
1696 }
1697
Bram Moolenaara4271d52011-05-10 13:38:27 +02001698 if (middle_match_len != 0)
1699 {
1700 /* Use the previously found middle match after failing to find a
1701 * match with an end. */
1702 if (!got_com && flags != NULL)
1703 *flags = saved_flags;
1704 i += middle_match_len;
1705 found_one = TRUE;
1706 }
1707
1708 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if (!found_one)
1710 break;
1711
Bram Moolenaar81340392012-06-06 16:12:59 +02001712 result = i;
1713
Bram Moolenaara4271d52011-05-10 13:38:27 +02001714 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 while (vim_iswhite(line[i]))
1716 ++i;
1717
Bram Moolenaar81340392012-06-06 16:12:59 +02001718 if (include_space)
1719 result = i;
1720
Bram Moolenaara4271d52011-05-10 13:38:27 +02001721 /* If this comment doesn't nest, stop here. */
1722 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (vim_strchr(part_buf, COM_NEST) == NULL)
1724 break;
1725 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001726 return result;
1727}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001728
Bram Moolenaar81340392012-06-06 16:12:59 +02001729/*
1730 * Return the offset at which the last comment in line starts. If there is no
1731 * comment in the whole line, -1 is returned.
1732 *
1733 * When "flags" is not null, it is set to point to the flags describing the
1734 * recognized comment leader.
1735 */
1736 int
1737get_last_leader_offset(line, flags)
1738 char_u *line;
1739 char_u **flags;
1740{
1741 int result = -1;
1742 int i, j;
1743 int lower_check_bound = 0;
1744 char_u *string;
1745 char_u *com_leader;
1746 char_u *com_flags;
1747 char_u *list;
1748 int found_one;
1749 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1750
1751 /*
1752 * Repeat to match several nested comment strings.
1753 */
1754 i = (int)STRLEN(line);
1755 while (--i >= lower_check_bound)
1756 {
1757 /*
1758 * scan through the 'comments' option for a match
1759 */
1760 found_one = FALSE;
1761 for (list = curbuf->b_p_com; *list; )
1762 {
1763 char_u *flags_save = list;
1764
1765 /*
1766 * Get one option part into part_buf[]. Advance list to next one.
1767 * put string at start of string.
1768 */
1769 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1770 string = vim_strchr(part_buf, ':');
1771 if (string == NULL) /* If everything is fine, this cannot actually
1772 * happen. */
1773 {
1774 continue;
1775 }
1776 *string++ = NUL; /* Isolate flags from string. */
1777 com_leader = string;
1778
1779 /*
1780 * Line contents and string must match.
1781 * When string starts with white space, must have some white space
1782 * (but the amount does not need to match, there might be a mix of
1783 * TABs and spaces).
1784 */
1785 if (vim_iswhite(string[0]))
1786 {
1787 if (i == 0 || !vim_iswhite(line[i - 1]))
1788 continue;
1789 while (vim_iswhite(string[0]))
1790 ++string;
1791 }
1792 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1793 /* do nothing */;
1794 if (string[j] != NUL)
1795 continue;
1796
1797 /*
1798 * When 'b' flag used, there must be white space or an
1799 * end-of-line after the string in the line.
1800 */
1801 if (vim_strchr(part_buf, COM_BLANK) != NULL
1802 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1803 {
1804 continue;
1805 }
1806
1807 /*
1808 * We have found a match, stop searching.
1809 */
1810 found_one = TRUE;
1811
1812 if (flags)
1813 *flags = flags_save;
1814 com_flags = flags_save;
1815
1816 break;
1817 }
1818
1819 if (found_one)
1820 {
1821 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1822 int len1, len2, off;
1823
1824 result = i;
1825 /*
1826 * If this comment nests, continue searching.
1827 */
1828 if (vim_strchr(part_buf, COM_NEST) != NULL)
1829 continue;
1830
1831 lower_check_bound = i;
1832
1833 /* Let's verify whether the comment leader found is a substring
1834 * of other comment leaders. If it is, let's adjust the
1835 * lower_check_bound so that we make sure that we have determined
1836 * the comment leader correctly.
1837 */
1838
1839 while (vim_iswhite(*com_leader))
1840 ++com_leader;
1841 len1 = (int)STRLEN(com_leader);
1842
1843 for (list = curbuf->b_p_com; *list; )
1844 {
1845 char_u *flags_save = list;
1846
1847 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1848 if (flags_save == com_flags)
1849 continue;
1850 string = vim_strchr(part_buf2, ':');
1851 ++string;
1852 while (vim_iswhite(*string))
1853 ++string;
1854 len2 = (int)STRLEN(string);
1855 if (len2 == 0)
1856 continue;
1857
1858 /* Now we have to verify whether string ends with a substring
1859 * beginning the com_leader. */
1860 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1861 {
1862 --off;
1863 if (!STRNCMP(string + off, com_leader, len2 - off))
1864 {
1865 if (i - off < lower_check_bound)
1866 lower_check_bound = i - off;
1867 }
1868 }
1869 }
1870 }
1871 }
1872 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873}
1874#endif
1875
1876/*
1877 * Return the number of window lines occupied by buffer line "lnum".
1878 */
1879 int
1880plines(lnum)
1881 linenr_T lnum;
1882{
1883 return plines_win(curwin, lnum, TRUE);
1884}
1885
1886 int
1887plines_win(wp, lnum, winheight)
1888 win_T *wp;
1889 linenr_T lnum;
1890 int winheight; /* when TRUE limit to window height */
1891{
1892#if defined(FEAT_DIFF) || defined(PROTO)
1893 /* Check for filler lines above this buffer line. When folded the result
1894 * is one line anyway. */
1895 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1896}
1897
1898 int
1899plines_nofill(lnum)
1900 linenr_T lnum;
1901{
1902 return plines_win_nofill(curwin, lnum, TRUE);
1903}
1904
1905 int
1906plines_win_nofill(wp, lnum, winheight)
1907 win_T *wp;
1908 linenr_T lnum;
1909 int winheight; /* when TRUE limit to window height */
1910{
1911#endif
1912 int lines;
1913
1914 if (!wp->w_p_wrap)
1915 return 1;
1916
1917#ifdef FEAT_VERTSPLIT
1918 if (wp->w_width == 0)
1919 return 1;
1920#endif
1921
1922#ifdef FEAT_FOLDING
1923 /* A folded lines is handled just like an empty line. */
1924 /* NOTE: Caller must handle lines that are MAYBE folded. */
1925 if (lineFolded(wp, lnum) == TRUE)
1926 return 1;
1927#endif
1928
1929 lines = plines_win_nofold(wp, lnum);
1930 if (winheight > 0 && lines > wp->w_height)
1931 return (int)wp->w_height;
1932 return lines;
1933}
1934
1935/*
1936 * Return number of window lines physical line "lnum" will occupy in window
1937 * "wp". Does not care about folding, 'wrap' or 'diff'.
1938 */
1939 int
1940plines_win_nofold(wp, lnum)
1941 win_T *wp;
1942 linenr_T lnum;
1943{
1944 char_u *s;
1945 long col;
1946 int width;
1947
1948 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
1949 if (*s == NUL) /* empty line */
1950 return 1;
1951 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
1952
1953 /*
1954 * If list mode is on, then the '$' at the end of the line may take up one
1955 * extra column.
1956 */
1957 if (wp->w_p_list && lcs_eol != NUL)
1958 col += 1;
1959
1960 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001961 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 */
1963 width = W_WIDTH(wp) - win_col_off(wp);
1964 if (width <= 0)
1965 return 32000;
1966 if (col <= width)
1967 return 1;
1968 col -= width;
1969 width += win_col_off2(wp);
1970 return (col + (width - 1)) / width + 1;
1971}
1972
1973/*
1974 * Like plines_win(), but only reports the number of physical screen lines
1975 * used from the start of the line to the given column number.
1976 */
1977 int
1978plines_win_col(wp, lnum, column)
1979 win_T *wp;
1980 linenr_T lnum;
1981 long column;
1982{
1983 long col;
1984 char_u *s;
1985 int lines = 0;
1986 int width;
1987
1988#ifdef FEAT_DIFF
1989 /* Check for filler lines above this buffer line. When folded the result
1990 * is one line anyway. */
1991 lines = diff_check_fill(wp, lnum);
1992#endif
1993
1994 if (!wp->w_p_wrap)
1995 return lines + 1;
1996
1997#ifdef FEAT_VERTSPLIT
1998 if (wp->w_width == 0)
1999 return lines + 1;
2000#endif
2001
2002 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2003
2004 col = 0;
2005 while (*s != NUL && --column >= 0)
2006 {
2007 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002008 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010
2011 /*
2012 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2013 * INSERT mode, then col must be adjusted so that it represents the last
2014 * screen position of the TAB. This only fixes an error when the TAB wraps
2015 * from one screen line to the next (when 'columns' is not a multiple of
2016 * 'ts') -- webb.
2017 */
2018 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
2019 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1;
2020
2021 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002022 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
2024 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002025 if (width <= 0)
2026 return 9999;
2027
2028 lines += 1;
2029 if (col > width)
2030 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2031 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032}
2033
2034 int
2035plines_m_win(wp, first, last)
2036 win_T *wp;
2037 linenr_T first, last;
2038{
2039 int count = 0;
2040
2041 while (first <= last)
2042 {
2043#ifdef FEAT_FOLDING
2044 int x;
2045
2046 /* Check if there are any really folded lines, but also included lines
2047 * that are maybe folded. */
2048 x = foldedCount(wp, first, NULL);
2049 if (x > 0)
2050 {
2051 ++count; /* count 1 for "+-- folded" line */
2052 first += x;
2053 }
2054 else
2055#endif
2056 {
2057#ifdef FEAT_DIFF
2058 if (first == wp->w_topline)
2059 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2060 else
2061#endif
2062 count += plines_win(wp, first, TRUE);
2063 ++first;
2064 }
2065 }
2066 return (count);
2067}
2068
2069#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2070/*
2071 * Insert string "p" at the cursor position. Stops at a NUL byte.
2072 * Handles Replace mode and multi-byte characters.
2073 */
2074 void
2075ins_bytes(p)
2076 char_u *p;
2077{
2078 ins_bytes_len(p, (int)STRLEN(p));
2079}
2080#endif
2081
2082#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2083 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2084/*
2085 * Insert string "p" with length "len" at the cursor position.
2086 * Handles Replace mode and multi-byte characters.
2087 */
2088 void
2089ins_bytes_len(p, len)
2090 char_u *p;
2091 int len;
2092{
2093 int i;
2094# ifdef FEAT_MBYTE
2095 int n;
2096
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002097 if (has_mbyte)
2098 for (i = 0; i < len; i += n)
2099 {
2100 if (enc_utf8)
2101 /* avoid reading past p[len] */
2102 n = utfc_ptr2len_len(p + i, len - i);
2103 else
2104 n = (*mb_ptr2len)(p + i);
2105 ins_char_bytes(p + i, n);
2106 }
2107 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002109 for (i = 0; i < len; ++i)
2110 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112#endif
2113
2114/*
2115 * Insert or replace a single character at the cursor position.
2116 * When in REPLACE or VREPLACE mode, replace any existing character.
2117 * Caller must have prepared for undo.
2118 * For multi-byte characters we get the whole character, the caller must
2119 * convert bytes to a character.
2120 */
2121 void
2122ins_char(c)
2123 int c;
2124{
2125#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002126 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 int n;
2128
2129 n = (*mb_char2bytes)(c, buf);
2130
2131 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2132 * Happens for CTRL-Vu9900. */
2133 if (buf[0] == 0)
2134 buf[0] = '\n';
2135
2136 ins_char_bytes(buf, n);
2137}
2138
2139 void
2140ins_char_bytes(buf, charlen)
2141 char_u *buf;
2142 int charlen;
2143{
2144 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145#endif
2146 int newlen; /* nr of bytes inserted */
2147 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2148 char_u *p;
2149 char_u *newp;
2150 char_u *oldp;
2151 int linelen; /* length of old line including NUL */
2152 colnr_T col;
2153 linenr_T lnum = curwin->w_cursor.lnum;
2154 int i;
2155
2156#ifdef FEAT_VIRTUALEDIT
2157 /* Break tabs if needed. */
2158 if (virtual_active() && curwin->w_cursor.coladd > 0)
2159 coladvance_force(getviscol());
2160#endif
2161
2162 col = curwin->w_cursor.col;
2163 oldp = ml_get(lnum);
2164 linelen = (int)STRLEN(oldp) + 1;
2165
2166 /* The lengths default to the values for when not replacing. */
2167 oldlen = 0;
2168#ifdef FEAT_MBYTE
2169 newlen = charlen;
2170#else
2171 newlen = 1;
2172#endif
2173
2174 if (State & REPLACE_FLAG)
2175 {
2176#ifdef FEAT_VREPLACE
2177 if (State & VREPLACE_FLAG)
2178 {
2179 colnr_T new_vcol = 0; /* init for GCC */
2180 colnr_T vcol;
2181 int old_list;
2182#ifndef FEAT_MBYTE
2183 char_u buf[2];
2184#endif
2185
2186 /*
2187 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2188 * Returns the old value of list, so when finished,
2189 * curwin->w_p_list should be set back to this.
2190 */
2191 old_list = curwin->w_p_list;
2192 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2193 curwin->w_p_list = FALSE;
2194
2195 /*
2196 * In virtual replace mode each character may replace one or more
2197 * characters (zero if it's a TAB). Count the number of bytes to
2198 * be deleted to make room for the new character, counting screen
2199 * cells. May result in adding spaces to fill a gap.
2200 */
2201 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2202#ifndef FEAT_MBYTE
2203 buf[0] = c;
2204 buf[1] = NUL;
2205#endif
2206 new_vcol = vcol + chartabsize(buf, vcol);
2207 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2208 {
2209 vcol += chartabsize(oldp + col + oldlen, vcol);
2210 /* Don't need to remove a TAB that takes us to the right
2211 * position. */
2212 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2213 break;
2214#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002215 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216#else
2217 ++oldlen;
2218#endif
2219 /* Deleted a bit too much, insert spaces. */
2220 if (vcol > new_vcol)
2221 newlen += vcol - new_vcol;
2222 }
2223 curwin->w_p_list = old_list;
2224 }
2225 else
2226#endif
2227 if (oldp[col] != NUL)
2228 {
2229 /* normal replace */
2230#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002231 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#else
2233 oldlen = 1;
2234#endif
2235 }
2236
2237
2238 /* Push the replaced bytes onto the replace stack, so that they can be
2239 * put back when BS is used. The bytes of a multi-byte character are
2240 * done the other way around, so that the first byte is popped off
2241 * first (it tells the byte length of the character). */
2242 replace_push(NUL);
2243 for (i = 0; i < oldlen; ++i)
2244 {
2245#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002246 if (has_mbyte)
2247 i += replace_push_mb(oldp + col + i) - 1;
2248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002250 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
2252 }
2253
2254 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2255 if (newp == NULL)
2256 return;
2257
2258 /* Copy bytes before the cursor. */
2259 if (col > 0)
2260 mch_memmove(newp, oldp, (size_t)col);
2261
2262 /* Copy bytes after the changed character(s). */
2263 p = newp + col;
2264 mch_memmove(p + newlen, oldp + col + oldlen,
2265 (size_t)(linelen - col - oldlen));
2266
2267 /* Insert or overwrite the new character. */
2268#ifdef FEAT_MBYTE
2269 mch_memmove(p, buf, charlen);
2270 i = charlen;
2271#else
2272 *p = c;
2273 i = 1;
2274#endif
2275
2276 /* Fill with spaces when necessary. */
2277 while (i < newlen)
2278 p[i++] = ' ';
2279
2280 /* Replace the line in the buffer. */
2281 ml_replace(lnum, newp, FALSE);
2282
2283 /* mark the buffer as changed and prepare for displaying */
2284 changed_bytes(lnum, col);
2285
2286 /*
2287 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2288 * show the match for right parens and braces.
2289 */
2290 if (p_sm && (State & INSERT)
2291 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002292#ifdef FEAT_INS_EXPAND
2293 && !ins_compl_active()
2294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002296 {
2297#ifdef FEAT_MBYTE
2298 if (has_mbyte)
2299 showmatch(mb_ptr2char(buf));
2300 else
2301#endif
2302 showmatch(c);
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305#ifdef FEAT_RIGHTLEFT
2306 if (!p_ri || (State & REPLACE_FLAG))
2307#endif
2308 {
2309 /* Normal insert: move cursor right */
2310#ifdef FEAT_MBYTE
2311 curwin->w_cursor.col += charlen;
2312#else
2313 ++curwin->w_cursor.col;
2314#endif
2315 }
2316 /*
2317 * TODO: should try to update w_row here, to avoid recomputing it later.
2318 */
2319}
2320
2321/*
2322 * Insert a string at the cursor position.
2323 * Note: Does NOT handle Replace mode.
2324 * Caller must have prepared for undo.
2325 */
2326 void
2327ins_str(s)
2328 char_u *s;
2329{
2330 char_u *oldp, *newp;
2331 int newlen = (int)STRLEN(s);
2332 int oldlen;
2333 colnr_T col;
2334 linenr_T lnum = curwin->w_cursor.lnum;
2335
2336#ifdef FEAT_VIRTUALEDIT
2337 if (virtual_active() && curwin->w_cursor.coladd > 0)
2338 coladvance_force(getviscol());
2339#endif
2340
2341 col = curwin->w_cursor.col;
2342 oldp = ml_get(lnum);
2343 oldlen = (int)STRLEN(oldp);
2344
2345 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2346 if (newp == NULL)
2347 return;
2348 if (col > 0)
2349 mch_memmove(newp, oldp, (size_t)col);
2350 mch_memmove(newp + col, s, (size_t)newlen);
2351 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2352 ml_replace(lnum, newp, FALSE);
2353 changed_bytes(lnum, col);
2354 curwin->w_cursor.col += newlen;
2355}
2356
2357/*
2358 * Delete one character under the cursor.
2359 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2360 * Caller must have prepared for undo.
2361 *
2362 * return FAIL for failure, OK otherwise
2363 */
2364 int
2365del_char(fixpos)
2366 int fixpos;
2367{
2368#ifdef FEAT_MBYTE
2369 if (has_mbyte)
2370 {
2371 /* Make sure the cursor is at the start of a character. */
2372 mb_adjust_cursor();
2373 if (*ml_get_cursor() == NUL)
2374 return FAIL;
2375 return del_chars(1L, fixpos);
2376 }
2377#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002378 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379}
2380
2381#if defined(FEAT_MBYTE) || defined(PROTO)
2382/*
2383 * Like del_bytes(), but delete characters instead of bytes.
2384 */
2385 int
2386del_chars(count, fixpos)
2387 long count;
2388 int fixpos;
2389{
2390 long bytes = 0;
2391 long i;
2392 char_u *p;
2393 int l;
2394
2395 p = ml_get_cursor();
2396 for (i = 0; i < count && *p != NUL; ++i)
2397 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002398 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 bytes += l;
2400 p += l;
2401 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002402 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403}
2404#endif
2405
2406/*
2407 * Delete "count" bytes under the cursor.
2408 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2409 * Caller must have prepared for undo.
2410 *
2411 * return FAIL for failure, OK otherwise
2412 */
2413 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002414del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002416 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002417 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419 char_u *oldp, *newp;
2420 colnr_T oldlen;
2421 linenr_T lnum = curwin->w_cursor.lnum;
2422 colnr_T col = curwin->w_cursor.col;
2423 int was_alloced;
2424 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002425 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427 oldp = ml_get(lnum);
2428 oldlen = (int)STRLEN(oldp);
2429
2430 /*
2431 * Can't do anything when the cursor is on the NUL after the line.
2432 */
2433 if (col >= oldlen)
2434 return FAIL;
2435
2436#ifdef FEAT_MBYTE
2437 /* If 'delcombine' is set and deleting (less than) one character, only
2438 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002439 if (p_deco && use_delcombine && enc_utf8
2440 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002442 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 int n;
2444
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002445 (void)utfc_ptr2char(oldp + col, cc);
2446 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 /* Find the last composing char, there can be several. */
2449 n = col;
2450 do
2451 {
2452 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002453 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 n += count;
2455 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2456 fixpos = 0;
2457 }
2458 }
2459#endif
2460
2461 /*
2462 * When count is too big, reduce it.
2463 */
2464 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2465 if (movelen <= 1)
2466 {
2467 /*
2468 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002469 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2470 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002472 if (col > 0 && fixpos && restart_edit == 0
2473#ifdef FEAT_VIRTUALEDIT
2474 && (ve_flags & VE_ONEMORE) == 0
2475#endif
2476 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 --curwin->w_cursor.col;
2479#ifdef FEAT_VIRTUALEDIT
2480 curwin->w_cursor.coladd = 0;
2481#endif
2482#ifdef FEAT_MBYTE
2483 if (has_mbyte)
2484 curwin->w_cursor.col -=
2485 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2486#endif
2487 }
2488 count = oldlen - col;
2489 movelen = 1;
2490 }
2491
2492 /*
2493 * If the old line has been allocated the deletion can be done in the
2494 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002495 * Can't do this when using Netbeans, because we would need to invoke
2496 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002497 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002500 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002501 was_alloced = FALSE;
2502 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002504 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 if (was_alloced)
2506 newp = oldp; /* use same allocated memory */
2507 else
2508 { /* need to allocate a new line */
2509 newp = alloc((unsigned)(oldlen + 1 - count));
2510 if (newp == NULL)
2511 return FAIL;
2512 mch_memmove(newp, oldp, (size_t)col);
2513 }
2514 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2515 if (!was_alloced)
2516 ml_replace(lnum, newp, FALSE);
2517
2518 /* mark the buffer as changed and prepare for displaying */
2519 changed_bytes(lnum, curwin->w_cursor.col);
2520
2521 return OK;
2522}
2523
2524/*
2525 * Delete from cursor to end of line.
2526 * Caller must have prepared for undo.
2527 *
2528 * return FAIL for failure, OK otherwise
2529 */
2530 int
2531truncate_line(fixpos)
2532 int fixpos; /* if TRUE fix the cursor position when done */
2533{
2534 char_u *newp;
2535 linenr_T lnum = curwin->w_cursor.lnum;
2536 colnr_T col = curwin->w_cursor.col;
2537
2538 if (col == 0)
2539 newp = vim_strsave((char_u *)"");
2540 else
2541 newp = vim_strnsave(ml_get(lnum), col);
2542
2543 if (newp == NULL)
2544 return FAIL;
2545
2546 ml_replace(lnum, newp, FALSE);
2547
2548 /* mark the buffer as changed and prepare for displaying */
2549 changed_bytes(lnum, curwin->w_cursor.col);
2550
2551 /*
2552 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2553 */
2554 if (fixpos && curwin->w_cursor.col > 0)
2555 --curwin->w_cursor.col;
2556
2557 return OK;
2558}
2559
2560/*
2561 * Delete "nlines" lines at the cursor.
2562 * Saves the lines for undo first if "undo" is TRUE.
2563 */
2564 void
2565del_lines(nlines, undo)
2566 long nlines; /* number of lines to delete */
2567 int undo; /* if TRUE, prepare for undo */
2568{
2569 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002570 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
2572 if (nlines <= 0)
2573 return;
2574
2575 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002576 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 return;
2578
2579 for (n = 0; n < nlines; )
2580 {
2581 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2582 break;
2583
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002584 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 ++n;
2586
2587 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002588 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 break;
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002592 /* Correct the cursor position before calling deleted_lines_mark(), it may
2593 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 curwin->w_cursor.col = 0;
2595 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002596
2597 /* adjust marks, mark the buffer as changed and prepare for displaying */
2598 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599}
2600
2601 int
2602gchar_pos(pos)
2603 pos_T *pos;
2604{
2605 char_u *ptr = ml_get_pos(pos);
2606
2607#ifdef FEAT_MBYTE
2608 if (has_mbyte)
2609 return (*mb_ptr2char)(ptr);
2610#endif
2611 return (int)*ptr;
2612}
2613
2614 int
2615gchar_cursor()
2616{
2617#ifdef FEAT_MBYTE
2618 if (has_mbyte)
2619 return (*mb_ptr2char)(ml_get_cursor());
2620#endif
2621 return (int)*ml_get_cursor();
2622}
2623
2624/*
2625 * Write a character at the current cursor position.
2626 * It is directly written into the block.
2627 */
2628 void
2629pchar_cursor(c)
2630 int c;
2631{
2632 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2633 + curwin->w_cursor.col) = c;
2634}
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636/*
2637 * When extra == 0: Return TRUE if the cursor is before or on the first
2638 * non-blank in the line.
2639 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2640 * the line.
2641 */
2642 int
2643inindent(extra)
2644 int extra;
2645{
2646 char_u *ptr;
2647 colnr_T col;
2648
2649 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2650 ++ptr;
2651 if (col >= curwin->w_cursor.col + extra)
2652 return TRUE;
2653 else
2654 return FALSE;
2655}
2656
2657/*
2658 * Skip to next part of an option argument: Skip space and comma.
2659 */
2660 char_u *
2661skip_to_option_part(p)
2662 char_u *p;
2663{
2664 if (*p == ',')
2665 ++p;
2666 while (*p == ' ')
2667 ++p;
2668 return p;
2669}
2670
2671/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002672 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 *
2674 * Most often called through changed_bytes() and changed_lines(), which also
2675 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002676 *
2677 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
2679 void
2680changed()
2681{
2682#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2683 /* The text of the preediting area is inserted, but this doesn't
2684 * mean a change of the buffer yet. That is delayed until the
2685 * text is committed. (this means preedit becomes empty) */
2686 if (im_is_preediting() && !xim_changed_while_preediting)
2687 return;
2688 xim_changed_while_preediting = FALSE;
2689#endif
2690
2691 if (!curbuf->b_changed)
2692 {
2693 int save_msg_scroll = msg_scroll;
2694
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002695 /* Give a warning about changing a read-only file. This may also
2696 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 /* Create a swap file if that is wanted.
2700 * Don't do this for "nofile" and "nowrite" buffer types. */
2701 if (curbuf->b_may_swap
2702#ifdef FEAT_QUICKFIX
2703 && !bt_dontwrite(curbuf)
2704#endif
2705 )
2706 {
2707 ml_open_file(curbuf);
2708
2709 /* The ml_open_file() can cause an ATTENTION message.
2710 * Wait two seconds, to make sure the user reads this unexpected
2711 * message. Since we could be anywhere, call wait_return() now,
2712 * and don't let the emsg() set msg_scroll. */
2713 if (need_wait_return && emsg_silent == 0)
2714 {
2715 out_flush();
2716 ui_delay(2000L, TRUE);
2717 wait_return(TRUE);
2718 msg_scroll = save_msg_scroll;
2719 }
2720 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002721 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724}
2725
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002726/*
2727 * Internal part of changed(), no user interaction.
2728 */
2729 void
2730changed_int()
2731{
2732 curbuf->b_changed = TRUE;
2733 ml_setflags(curbuf);
2734#ifdef FEAT_WINDOWS
2735 check_status(curbuf);
2736 redraw_tabline = TRUE;
2737#endif
2738#ifdef FEAT_TITLE
2739 need_maketitle = TRUE; /* set window title later */
2740#endif
2741}
2742
Bram Moolenaardba8a912005-04-24 22:08:39 +00002743static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2744static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2746
2747/*
2748 * Changed bytes within a single line for the current buffer.
2749 * - marks the windows on this buffer to be redisplayed
2750 * - marks the buffer changed by calling changed()
2751 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002752 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 */
2754 void
2755changed_bytes(lnum, col)
2756 linenr_T lnum;
2757 colnr_T col;
2758{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002759 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002761
2762#ifdef FEAT_DIFF
2763 /* Diff highlighting in other diff windows may need to be updated too. */
2764 if (curwin->w_p_diff)
2765 {
2766 win_T *wp;
2767 linenr_T wlnum;
2768
2769 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2770 if (wp->w_p_diff && wp != curwin)
2771 {
2772 redraw_win_later(wp, VALID);
2773 wlnum = diff_lnum_win(lnum, wp);
2774 if (wlnum > 0)
2775 changedOneline(wp->w_buffer, wlnum);
2776 }
2777 }
2778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779}
2780
2781 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002782changedOneline(buf, lnum)
2783 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 linenr_T lnum;
2785{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002786 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
2788 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002789 if (lnum < buf->b_mod_top)
2790 buf->b_mod_top = lnum;
2791 else if (lnum >= buf->b_mod_bot)
2792 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794 else
2795 {
2796 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002797 buf->b_mod_set = TRUE;
2798 buf->b_mod_top = lnum;
2799 buf->b_mod_bot = lnum + 1;
2800 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802}
2803
2804/*
2805 * Appended "count" lines below line "lnum" in the current buffer.
2806 * Must be called AFTER the change and after mark_adjust().
2807 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2808 */
2809 void
2810appended_lines(lnum, count)
2811 linenr_T lnum;
2812 long count;
2813{
2814 changed_lines(lnum + 1, 0, lnum + 1, count);
2815}
2816
2817/*
2818 * Like appended_lines(), but adjust marks first.
2819 */
2820 void
2821appended_lines_mark(lnum, count)
2822 linenr_T lnum;
2823 long count;
2824{
2825 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2826 changed_lines(lnum + 1, 0, lnum + 1, count);
2827}
2828
2829/*
2830 * Deleted "count" lines at line "lnum" in the current buffer.
2831 * Must be called AFTER the change and after mark_adjust().
2832 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2833 */
2834 void
2835deleted_lines(lnum, count)
2836 linenr_T lnum;
2837 long count;
2838{
2839 changed_lines(lnum, 0, lnum + count, -count);
2840}
2841
2842/*
2843 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002844 * Make sure the cursor is on a valid line before calling, a GUI callback may
2845 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 */
2847 void
2848deleted_lines_mark(lnum, count)
2849 linenr_T lnum;
2850 long count;
2851{
2852 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2853 changed_lines(lnum, 0, lnum + count, -count);
2854}
2855
2856/*
2857 * Changed lines for the current buffer.
2858 * Must be called AFTER the change and after mark_adjust().
2859 * - mark the buffer changed by calling changed()
2860 * - mark the windows on this buffer to be redisplayed
2861 * - invalidate cached values
2862 * "lnum" is the first line that needs displaying, "lnume" the first line
2863 * below the changed lines (BEFORE the change).
2864 * When only inserting lines, "lnum" and "lnume" are equal.
2865 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002866 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 */
2868 void
2869changed_lines(lnum, col, lnume, xtra)
2870 linenr_T lnum; /* first line with change */
2871 colnr_T col; /* column in first line with change */
2872 linenr_T lnume; /* line below last changed line */
2873 long xtra; /* number of extra lines (negative when deleting) */
2874{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002875 changed_lines_buf(curbuf, lnum, lnume, xtra);
2876
2877#ifdef FEAT_DIFF
2878 if (xtra == 0 && curwin->w_p_diff)
2879 {
2880 /* When the number of lines doesn't change then mark_adjust() isn't
2881 * called and other diff buffers still need to be marked for
2882 * displaying. */
2883 win_T *wp;
2884 linenr_T wlnum;
2885
2886 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2887 if (wp->w_p_diff && wp != curwin)
2888 {
2889 redraw_win_later(wp, VALID);
2890 wlnum = diff_lnum_win(lnum, wp);
2891 if (wlnum > 0)
2892 changed_lines_buf(wp->w_buffer, wlnum,
2893 lnume - lnum + wlnum, 0L);
2894 }
2895 }
2896#endif
2897
2898 changed_common(lnum, col, lnume, xtra);
2899}
2900
2901 static void
2902changed_lines_buf(buf, lnum, lnume, xtra)
2903 buf_T *buf;
2904 linenr_T lnum; /* first line with change */
2905 linenr_T lnume; /* line below last changed line */
2906 long xtra; /* number of extra lines (negative when deleting) */
2907{
2908 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
2910 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002911 if (lnum < buf->b_mod_top)
2912 buf->b_mod_top = lnum;
2913 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 {
2915 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002916 buf->b_mod_bot += xtra;
2917 if (buf->b_mod_bot < lnum)
2918 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002920 if (lnume + xtra > buf->b_mod_bot)
2921 buf->b_mod_bot = lnume + xtra;
2922 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924 else
2925 {
2926 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002927 buf->b_mod_set = TRUE;
2928 buf->b_mod_top = lnum;
2929 buf->b_mod_bot = lnume + xtra;
2930 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932}
2933
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002934/*
2935 * Common code for when a change is was made.
2936 * See changed_lines() for the arguments.
2937 * Careful: may trigger autocommands that reload the buffer.
2938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 static void
2940changed_common(lnum, col, lnume, xtra)
2941 linenr_T lnum;
2942 colnr_T col;
2943 linenr_T lnume;
2944 long xtra;
2945{
2946 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002947#ifdef FEAT_WINDOWS
2948 tabpage_T *tp;
2949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 int i;
2951#ifdef FEAT_JUMPLIST
2952 int cols;
2953 pos_T *p;
2954 int add;
2955#endif
2956
2957 /* mark the buffer as modified */
2958 changed();
2959
2960 /* set the '. mark */
2961 if (!cmdmod.keepjumps)
2962 {
2963 curbuf->b_last_change.lnum = lnum;
2964 curbuf->b_last_change.col = col;
2965
2966#ifdef FEAT_JUMPLIST
2967 /* Create a new entry if a new undo-able change was started or we
2968 * don't have an entry yet. */
2969 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
2970 {
2971 if (curbuf->b_changelistlen == 0)
2972 add = TRUE;
2973 else
2974 {
2975 /* Don't create a new entry when the line number is the same
2976 * as the last one and the column is not too far away. Avoids
2977 * creating many entries for typing "xxxxx". */
2978 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
2979 if (p->lnum != lnum)
2980 add = TRUE;
2981 else
2982 {
2983 cols = comp_textwidth(FALSE);
2984 if (cols == 0)
2985 cols = 79;
2986 add = (p->col + cols < col || col + cols < p->col);
2987 }
2988 }
2989 if (add)
2990 {
2991 /* This is the first of a new sequence of undo-able changes
2992 * and it's at some distance of the last change. Use a new
2993 * position in the changelist. */
2994 curbuf->b_new_change = FALSE;
2995
2996 if (curbuf->b_changelistlen == JUMPLISTSIZE)
2997 {
2998 /* changelist is full: remove oldest entry */
2999 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3000 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3001 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003002 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 {
3004 /* Correct position in changelist for other windows on
3005 * this buffer. */
3006 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3007 --wp->w_changelistidx;
3008 }
3009 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003010 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 {
3012 /* For other windows, if the position in the changelist is
3013 * at the end it stays at the end. */
3014 if (wp->w_buffer == curbuf
3015 && wp->w_changelistidx == curbuf->b_changelistlen)
3016 ++wp->w_changelistidx;
3017 }
3018 ++curbuf->b_changelistlen;
3019 }
3020 }
3021 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3022 curbuf->b_last_change;
3023 /* The current window is always after the last change, so that "g,"
3024 * takes you back to it. */
3025 curwin->w_changelistidx = curbuf->b_changelistlen;
3026#endif
3027 }
3028
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003029 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 {
3031 if (wp->w_buffer == curbuf)
3032 {
3033 /* Mark this window to be redrawn later. */
3034 if (wp->w_redr_type < VALID)
3035 wp->w_redr_type = VALID;
3036
3037 /* Check if a change in the buffer has invalidated the cached
3038 * values for the cursor. */
3039#ifdef FEAT_FOLDING
3040 /*
3041 * Update the folds for this window. Can't postpone this, because
3042 * a following operator might work on the whole fold: ">>dd".
3043 */
3044 foldUpdate(wp, lnum, lnume + xtra - 1);
3045
3046 /* The change may cause lines above or below the change to become
3047 * included in a fold. Set lnum/lnume to the first/last line that
3048 * might be displayed differently.
3049 * Set w_cline_folded here as an efficient way to update it when
3050 * inserting lines just above a closed fold. */
3051 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3052 if (wp->w_cursor.lnum == lnum)
3053 wp->w_cline_folded = i;
3054 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3055 if (wp->w_cursor.lnum == lnume)
3056 wp->w_cline_folded = i;
3057
3058 /* If the changed line is in a range of previously folded lines,
3059 * compare with the first line in that range. */
3060 if (wp->w_cursor.lnum <= lnum)
3061 {
3062 i = find_wl_entry(wp, lnum);
3063 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3064 changed_line_abv_curs_win(wp);
3065 }
3066#endif
3067
3068 if (wp->w_cursor.lnum > lnum)
3069 changed_line_abv_curs_win(wp);
3070 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3071 changed_cline_bef_curs_win(wp);
3072 if (wp->w_botline >= lnum)
3073 {
3074 /* Assume that botline doesn't change (inserted lines make
3075 * other lines scroll down below botline). */
3076 approximate_botline_win(wp);
3077 }
3078
3079 /* Check if any w_lines[] entries have become invalid.
3080 * For entries below the change: Correct the lnums for
3081 * inserted/deleted lines. Makes it possible to stop displaying
3082 * after the change. */
3083 for (i = 0; i < wp->w_lines_valid; ++i)
3084 if (wp->w_lines[i].wl_valid)
3085 {
3086 if (wp->w_lines[i].wl_lnum >= lnum)
3087 {
3088 if (wp->w_lines[i].wl_lnum < lnume)
3089 {
3090 /* line included in change */
3091 wp->w_lines[i].wl_valid = FALSE;
3092 }
3093 else if (xtra != 0)
3094 {
3095 /* line below change */
3096 wp->w_lines[i].wl_lnum += xtra;
3097#ifdef FEAT_FOLDING
3098 wp->w_lines[i].wl_lastlnum += xtra;
3099#endif
3100 }
3101 }
3102#ifdef FEAT_FOLDING
3103 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3104 {
3105 /* change somewhere inside this range of folded lines,
3106 * may need to be redrawn */
3107 wp->w_lines[i].wl_valid = FALSE;
3108 }
3109#endif
3110 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003111
3112#ifdef FEAT_FOLDING
3113 /* Take care of side effects for setting w_topline when folds have
3114 * changed. Esp. when the buffer was changed in another window. */
3115 if (hasAnyFolding(wp))
3116 set_topline(wp, wp->w_topline);
3117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119 }
3120
3121 /* Call update_screen() later, which checks out what needs to be redrawn,
3122 * since it notices b_mod_set and then uses b_mod_*. */
3123 if (must_redraw < VALID)
3124 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003125
3126#ifdef FEAT_AUTOCMD
3127 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003128 if (lnum <= curwin->w_cursor.lnum
3129 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003130 last_cursormoved.lnum = 0;
3131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132}
3133
3134/*
3135 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3136 */
3137 void
3138unchanged(buf, ff)
3139 buf_T *buf;
3140 int ff; /* also reset 'fileformat' */
3141{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003142 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
3144 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003145 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (ff)
3147 save_file_ff(buf);
3148#ifdef FEAT_WINDOWS
3149 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003150 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#endif
3152#ifdef FEAT_TITLE
3153 need_maketitle = TRUE; /* set window title later */
3154#endif
3155 }
3156 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#ifdef FEAT_NETBEANS_INTG
3158 netbeans_unmodified(buf);
3159#endif
3160}
3161
3162#if defined(FEAT_WINDOWS) || defined(PROTO)
3163/*
3164 * check_status: called when the status bars for the buffer 'buf'
3165 * need to be updated
3166 */
3167 void
3168check_status(buf)
3169 buf_T *buf;
3170{
3171 win_T *wp;
3172
3173 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3174 if (wp->w_buffer == buf && wp->w_status_height)
3175 {
3176 wp->w_redr_status = TRUE;
3177 if (must_redraw < VALID)
3178 must_redraw = VALID;
3179 }
3180}
3181#endif
3182
3183/*
3184 * If the file is readonly, give a warning message with the first change.
3185 * Don't do this for autocommands.
3186 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003187 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003189 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 */
3191 void
3192change_warning(col)
3193 int col; /* column for message; non-zero when in insert
3194 mode and 'showmode' is on */
3195{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003196 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (curbuf->b_did_warn == FALSE
3199 && curbufIsChanged() == 0
3200#ifdef FEAT_AUTOCMD
3201 && !autocmd_busy
3202#endif
3203 && curbuf->b_p_ro)
3204 {
3205#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003206 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003208 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 if (!curbuf->b_p_ro)
3210 return;
3211#endif
3212 /*
3213 * Do what msg() does, but with a column offset if the warning should
3214 * be after the mode message.
3215 */
3216 msg_start();
3217 if (msg_row == Rows - 1)
3218 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003219 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003220 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3221#ifdef FEAT_EVAL
3222 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 msg_clr_eos();
3225 (void)msg_end();
3226 if (msg_silent == 0 && !silent_mode)
3227 {
3228 out_flush();
3229 ui_delay(1000L, TRUE); /* give the user time to think about it */
3230 }
3231 curbuf->b_did_warn = TRUE;
3232 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3233 if (msg_row < Rows - 1)
3234 showmode();
3235 }
3236}
3237
3238/*
3239 * Ask for a reply from the user, a 'y' or a 'n'.
3240 * No other characters are accepted, the message is repeated until a valid
3241 * reply is entered or CTRL-C is hit.
3242 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3243 * from any buffers but directly from the user.
3244 *
3245 * return the 'y' or 'n'
3246 */
3247 int
3248ask_yesno(str, direct)
3249 char_u *str;
3250 int direct;
3251{
3252 int r = ' ';
3253 int save_State = State;
3254
3255 if (exiting) /* put terminal in raw mode for this question */
3256 settmode(TMODE_RAW);
3257 ++no_wait_return;
3258#ifdef USE_ON_FLY_SCROLL
3259 dont_scroll = TRUE; /* disallow scrolling here */
3260#endif
3261 State = CONFIRM; /* mouse behaves like with :confirm */
3262#ifdef FEAT_MOUSE
3263 setmouse(); /* disables mouse for xterm */
3264#endif
3265 ++no_mapping;
3266 ++allow_keys; /* no mapping here, but recognize keys */
3267
3268 while (r != 'y' && r != 'n')
3269 {
3270 /* same highlighting as for wait_return */
3271 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3272 if (direct)
3273 r = get_keystroke();
3274 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003275 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (r == Ctrl_C || r == ESC)
3277 r = 'n';
3278 msg_putchar(r); /* show what you typed */
3279 out_flush();
3280 }
3281 --no_wait_return;
3282 State = save_State;
3283#ifdef FEAT_MOUSE
3284 setmouse();
3285#endif
3286 --no_mapping;
3287 --allow_keys;
3288
3289 return r;
3290}
3291
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003292#if defined(FEAT_MOUSE) || defined(PROTO)
3293/*
3294 * Return TRUE if "c" is a mouse key.
3295 */
3296 int
3297is_mouse_key(c)
3298 int c;
3299{
3300 return c == K_LEFTMOUSE
3301 || c == K_LEFTMOUSE_NM
3302 || c == K_LEFTDRAG
3303 || c == K_LEFTRELEASE
3304 || c == K_LEFTRELEASE_NM
3305 || c == K_MIDDLEMOUSE
3306 || c == K_MIDDLEDRAG
3307 || c == K_MIDDLERELEASE
3308 || c == K_RIGHTMOUSE
3309 || c == K_RIGHTDRAG
3310 || c == K_RIGHTRELEASE
3311 || c == K_MOUSEDOWN
3312 || c == K_MOUSEUP
3313 || c == K_MOUSELEFT
3314 || c == K_MOUSERIGHT
3315 || c == K_X1MOUSE
3316 || c == K_X1DRAG
3317 || c == K_X1RELEASE
3318 || c == K_X2MOUSE
3319 || c == K_X2DRAG
3320 || c == K_X2RELEASE;
3321}
3322#endif
3323
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324/*
3325 * Get a key stroke directly from the user.
3326 * Ignores mouse clicks and scrollbar events, except a click for the left
3327 * button (used at the more prompt).
3328 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3329 * Disadvantage: typeahead is ignored.
3330 * Translates the interrupt character for unix to ESC.
3331 */
3332 int
3333get_keystroke()
3334{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003335 char_u *buf = NULL;
3336 int buflen = 150;
3337 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 int len = 0;
3339 int n;
3340 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003341 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
3343 mapped_ctrl_c = FALSE; /* mappings are not used here */
3344 for (;;)
3345 {
3346 cursor_on();
3347 out_flush();
3348
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003349 /* Leave some room for check_termcode() to insert a key code into (max
3350 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3351 * bytes. */
3352 maxlen = (buflen - 6 - len) / 3;
3353 if (buf == NULL)
3354 buf = alloc(buflen);
3355 else if (maxlen < 10)
3356 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003357 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003358 * escape sequence. */
3359 buflen += 100;
3360 buf = vim_realloc(buf, buflen);
3361 maxlen = (buflen - 6 - len) / 3;
3362 }
3363 if (buf == NULL)
3364 {
3365 do_outofmem_msg((long_u)buflen);
3366 return ESC; /* panic! */
3367 }
3368
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003370 * terminal code to complete. */
3371 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (n > 0)
3373 {
3374 /* Replace zero and CSI by a special key code. */
3375 n = fix_input_buffer(buf + len, n, FALSE);
3376 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003377 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003379 else if (len > 0)
3380 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
Bram Moolenaar4395a712006-09-05 18:57:57 +00003382 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003383 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003384 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003386
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003387 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003388 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003389 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003390 {
3391 /* Redrawing was postponed, do it now. */
3392 update_screen(0);
3393 setcursor(); /* put cursor back where it belongs */
3394 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003395 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003396 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003397 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003399 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 continue;
3401
3402 /* Handle modifier and/or special key code. */
3403 n = buf[0];
3404 if (n == K_SPECIAL)
3405 {
3406 n = TO_SPECIAL(buf[1], buf[2]);
3407 if (buf[1] == KS_MODIFIER
3408 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003409#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003410 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003411#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003412#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 || n == K_VER_SCROLLBAR
3414 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#endif
3416 )
3417 {
3418 if (buf[1] == KS_MODIFIER)
3419 mod_mask = buf[2];
3420 len -= 3;
3421 if (len > 0)
3422 mch_memmove(buf, buf + 3, (size_t)len);
3423 continue;
3424 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
3427#ifdef FEAT_MBYTE
3428 if (has_mbyte)
3429 {
3430 if (MB_BYTE2LEN(n) > len)
3431 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003432 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 n = (*mb_ptr2char)(buf);
3434 }
3435#endif
3436#ifdef UNIX
3437 if (n == intr_char)
3438 n = ESC;
3439#endif
3440 break;
3441 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003442 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
3444 mapped_ctrl_c = save_mapped_ctrl_c;
3445 return n;
3446}
3447
3448/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003449 * Get a number from the user.
3450 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 */
3452 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003453get_number(colon, mouse_used)
3454 int colon; /* allow colon to abort */
3455 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 int n = 0;
3458 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003459 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003461 if (mouse_used != NULL)
3462 *mouse_used = FALSE;
3463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 /* When not printing messages, the user won't know what to type, return a
3465 * zero (as if CR was hit). */
3466 if (msg_silent != 0)
3467 return 0;
3468
3469#ifdef USE_ON_FLY_SCROLL
3470 dont_scroll = TRUE; /* disallow scrolling here */
3471#endif
3472 ++no_mapping;
3473 ++allow_keys; /* no mapping here, but recognize keys */
3474 for (;;)
3475 {
3476 windgoto(msg_row, msg_col);
3477 c = safe_vgetc();
3478 if (VIM_ISDIGIT(c))
3479 {
3480 n = n * 10 + c - '0';
3481 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003482 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3485 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003486 if (typed > 0)
3487 {
3488 MSG_PUTS("\b \b");
3489 --typed;
3490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003493#ifdef FEAT_MOUSE
3494 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3495 {
3496 *mouse_used = TRUE;
3497 n = mouse_row + 1;
3498 break;
3499 }
3500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 else if (n == 0 && c == ':' && colon)
3502 {
3503 stuffcharReadbuff(':');
3504 if (!exmode_active)
3505 cmdline_row = msg_row;
3506 skip_redraw = TRUE; /* skip redraw once */
3507 do_redraw = FALSE;
3508 break;
3509 }
3510 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3511 break;
3512 }
3513 --no_mapping;
3514 --allow_keys;
3515 return n;
3516}
3517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003518/*
3519 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003520 * When "mouse_used" is not NULL allow using the mouse and in that case return
3521 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003522 */
3523 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003524prompt_for_number(mouse_used)
3525 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003526{
3527 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003528 int save_cmdline_row;
3529 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003530
3531 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003532 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003533 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003534 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003535 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003536
Bram Moolenaar203335e2006-09-03 14:35:42 +00003537 /* Set the state such that text can be selected/copied/pasted and we still
3538 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003539 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003540 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003541 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003542 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003543
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003544 i = get_number(TRUE, mouse_used);
3545 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003546 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003547 /* don't call wait_return() now */
3548 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003549 cmdline_row = msg_row - 1;
3550 need_wait_return = FALSE;
3551 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003552 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003553 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003554 else
3555 cmdline_row = save_cmdline_row;
3556 State = save_State;
3557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003558 return i;
3559}
3560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 void
3562msgmore(n)
3563 long n;
3564{
3565 long pn;
3566
3567 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3569 return;
3570
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003571 /* We don't want to overwrite another important message, but do overwrite
3572 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3573 * then "put" reports the last action. */
3574 if (keep_msg != NULL && !keep_msg_more)
3575 return;
3576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (n > 0)
3578 pn = n;
3579 else
3580 pn = -n;
3581
3582 if (pn > p_report)
3583 {
3584 if (pn == 1)
3585 {
3586 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003587 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3588 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003590 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3591 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
3593 else
3594 {
3595 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003596 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3597 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003599 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3600 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
3602 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003603 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (msg(msg_buf))
3605 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003606 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003607 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
3609 }
3610}
3611
3612/*
3613 * flush map and typeahead buffers and give a warning for an error
3614 */
3615 void
3616beep_flush()
3617{
3618 if (emsg_silent == 0)
3619 {
3620 flush_buffers(FALSE);
3621 vim_beep();
3622 }
3623}
3624
3625/*
3626 * give a warning for an error
3627 */
3628 void
3629vim_beep()
3630{
3631 if (emsg_silent == 0)
3632 {
3633 if (p_vb
3634#ifdef FEAT_GUI
3635 /* While the GUI is starting up the termcap is set for the GUI
3636 * but the output still goes to a terminal. */
3637 && !(gui.in_use && gui.starting)
3638#endif
3639 )
3640 {
3641 out_str(T_VB);
3642 }
3643 else
3644 {
3645#ifdef MSDOS
3646 /*
3647 * The number of beeps outputted is reduced to avoid having to wait
3648 * for all the beeps to finish. This is only a problem on systems
3649 * where the beeps don't overlap.
3650 */
3651 if (beep_count == 0 || beep_count == 10)
3652 {
3653 out_char(BELL);
3654 beep_count = 1;
3655 }
3656 else
3657 ++beep_count;
3658#else
3659 out_char(BELL);
3660#endif
3661 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003662
3663 /* When 'verbose' is set and we are sourcing a script or executing a
3664 * function give the user a hint where the beep comes from. */
3665 if (vim_strchr(p_debug, 'e') != NULL)
3666 {
3667 msg_source(hl_attr(HLF_W));
3668 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671}
3672
3673/*
3674 * To get the "real" home directory:
3675 * - get value of $HOME
3676 * For Unix:
3677 * - go to that directory
3678 * - do mch_dirname() to get the real name of that directory.
3679 * This also works with mounts and links.
3680 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3681 */
3682static char_u *homedir = NULL;
3683
3684 void
3685init_homedir()
3686{
3687 char_u *var;
3688
Bram Moolenaar05159a02005-02-26 23:04:13 +00003689 /* In case we are called a second time (when 'encoding' changes). */
3690 vim_free(homedir);
3691 homedir = NULL;
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693#ifdef VMS
3694 var = mch_getenv((char_u *)"SYS$LOGIN");
3695#else
3696 var = mch_getenv((char_u *)"HOME");
3697#endif
3698
3699 if (var != NULL && *var == NUL) /* empty is same as not set */
3700 var = NULL;
3701
3702#ifdef WIN3264
3703 /*
3704 * Weird but true: $HOME may contain an indirect reference to another
3705 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3706 * when $HOME is being set.
3707 */
3708 if (var != NULL && *var == '%')
3709 {
3710 char_u *p;
3711 char_u *exp;
3712
3713 p = vim_strchr(var + 1, '%');
3714 if (p != NULL)
3715 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003716 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 exp = mch_getenv(NameBuff);
3718 if (exp != NULL && *exp != NUL
3719 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3720 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003721 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 var = NameBuff;
3723 /* Also set $HOME, it's needed for _viminfo. */
3724 vim_setenv((char_u *)"HOME", NameBuff);
3725 }
3726 }
3727 }
3728
3729 /*
3730 * Typically, $HOME is not defined on Windows, unless the user has
3731 * specifically defined it for Vim's sake. However, on Windows NT
3732 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3733 * each user. Try constructing $HOME from these.
3734 */
3735 if (var == NULL)
3736 {
3737 char_u *homedrive, *homepath;
3738
3739 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3740 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003741 if (homepath == NULL || *homepath == NUL)
3742 homepath = "\\";
3743 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3745 {
3746 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3747 if (NameBuff[0] != NUL)
3748 {
3749 var = NameBuff;
3750 /* Also set $HOME, it's needed for _viminfo. */
3751 vim_setenv((char_u *)"HOME", NameBuff);
3752 }
3753 }
3754 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003755
3756# if defined(FEAT_MBYTE)
3757 if (enc_utf8 && var != NULL)
3758 {
3759 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003760 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003761
3762 /* Convert from active codepage to UTF-8. Other conversions are
3763 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003764 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003765 if (pp != NULL)
3766 {
3767 homedir = pp;
3768 return;
3769 }
3770 }
3771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772#endif
3773
3774#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3775 /*
3776 * Default home dir is C:/
3777 * Best assumption we can make in such a situation.
3778 */
3779 if (var == NULL)
3780 var = "C:/";
3781#endif
3782 if (var != NULL)
3783 {
3784#ifdef UNIX
3785 /*
3786 * Change to the directory and get the actual path. This resolves
3787 * links. Don't do it when we can't return.
3788 */
3789 if (mch_dirname(NameBuff, MAXPATHL) == OK
3790 && mch_chdir((char *)NameBuff) == 0)
3791 {
3792 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3793 var = IObuff;
3794 if (mch_chdir((char *)NameBuff) != 0)
3795 EMSG(_(e_prev_dir));
3796 }
3797#endif
3798 homedir = vim_strsave(var);
3799 }
3800}
3801
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003802#if defined(EXITFREE) || defined(PROTO)
3803 void
3804free_homedir()
3805{
3806 vim_free(homedir);
3807}
Bram Moolenaar24305862012-08-15 14:05:05 +02003808
3809# ifdef FEAT_CMDL_COMPL
3810 void
3811free_users()
3812{
3813 ga_clear_strings(&ga_users);
3814}
3815# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003816#endif
3817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003819 * Call expand_env() and store the result in an allocated string.
3820 * This is not very memory efficient, this expects the result to be freed
3821 * again soon.
3822 */
3823 char_u *
3824expand_env_save(src)
3825 char_u *src;
3826{
3827 return expand_env_save_opt(src, FALSE);
3828}
3829
3830/*
3831 * Idem, but when "one" is TRUE handle the string as one file name, only
3832 * expand "~" at the start.
3833 */
3834 char_u *
3835expand_env_save_opt(src, one)
3836 char_u *src;
3837 int one;
3838{
3839 char_u *p;
3840
3841 p = alloc(MAXPATHL);
3842 if (p != NULL)
3843 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3844 return p;
3845}
3846
3847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 * Expand environment variable with path name.
3849 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003850 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 * If anything fails no expansion is done and dst equals src.
3852 */
3853 void
3854expand_env(src, dst, dstlen)
3855 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3856 char_u *dst; /* where to put the result */
3857 int dstlen; /* maximum length of the result */
3858{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003859 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860}
3861
3862 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003863expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003864 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 char_u *dst; /* where to put the result */
3866 int dstlen; /* maximum length of the result */
3867 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003868 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003869 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003871 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 char_u *tail;
3873 int c;
3874 char_u *var;
3875 int copy_char;
3876 int mustfree; /* var was allocated, need to free it later */
3877 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003878 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003880 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003881 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003882
3883 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 --dstlen; /* leave one char space for "\," */
3885 while (*src && dstlen > 0)
3886 {
3887 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003888 if ((*src == '$'
3889#ifdef VMS
3890 && at_start
3891#endif
3892 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3894 || *src == '%'
3895#endif
3896 || (*src == '~' && at_start))
3897 {
3898 mustfree = FALSE;
3899
3900 /*
3901 * The variable name is copied into dst temporarily, because it may
3902 * be a string in read-only memory and a NUL needs to be appended.
3903 */
3904 if (*src != '~') /* environment var */
3905 {
3906 tail = src + 1;
3907 var = dst;
3908 c = dstlen - 1;
3909
3910#ifdef UNIX
3911 /* Unix has ${var-name} type environment vars */
3912 if (*tail == '{' && !vim_isIDc('{'))
3913 {
3914 tail++; /* ignore '{' */
3915 while (c-- > 0 && *tail && *tail != '}')
3916 *var++ = *tail++;
3917 }
3918 else
3919#endif
3920 {
3921 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3922#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3923 || (*src == '%' && *tail != '%')
3924#endif
3925 ))
3926 {
3927#ifdef OS2 /* env vars only in uppercase */
3928 *var++ = TOUPPER_LOC(*tail);
3929 tail++; /* toupper() may be a macro! */
3930#else
3931 *var++ = *tail++;
3932#endif
3933 }
3934 }
3935
3936#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3937# ifdef UNIX
3938 if (src[1] == '{' && *tail != '}')
3939# else
3940 if (*src == '%' && *tail != '%')
3941# endif
3942 var = NULL;
3943 else
3944 {
3945# ifdef UNIX
3946 if (src[1] == '{')
3947# else
3948 if (*src == '%')
3949#endif
3950 ++tail;
3951#endif
3952 *var = NUL;
3953 var = vim_getenv(dst, &mustfree);
3954#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3955 }
3956#endif
3957 }
3958 /* home directory */
3959 else if ( src[1] == NUL
3960 || vim_ispathsep(src[1])
3961 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3962 {
3963 var = homedir;
3964 tail = src + 1;
3965 }
3966 else /* user directory */
3967 {
3968#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
3969 /*
3970 * Copy ~user to dst[], so we can put a NUL after it.
3971 */
3972 tail = src;
3973 var = dst;
3974 c = dstlen - 1;
3975 while ( c-- > 0
3976 && *tail
3977 && vim_isfilec(*tail)
3978 && !vim_ispathsep(*tail))
3979 *var++ = *tail++;
3980 *var = NUL;
3981# ifdef UNIX
3982 /*
3983 * If the system supports getpwnam(), use it.
3984 * Otherwise, or if getpwnam() fails, the shell is used to
3985 * expand ~user. This is slower and may fail if the shell
3986 * does not support ~user (old versions of /bin/sh).
3987 */
3988# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
3989 {
3990 struct passwd *pw;
3991
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003992 /* Note: memory allocated by getpwnam() is never freed.
3993 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 pw = getpwnam((char *)dst + 1);
3995 if (pw != NULL)
3996 var = (char_u *)pw->pw_dir;
3997 else
3998 var = NULL;
3999 }
4000 if (var == NULL)
4001# endif
4002 {
4003 expand_T xpc;
4004
4005 ExpandInit(&xpc);
4006 xpc.xp_context = EXPAND_FILES;
4007 var = ExpandOne(&xpc, dst, NULL,
4008 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 mustfree = TRUE;
4010 }
4011
4012# else /* !UNIX, thus VMS */
4013 /*
4014 * USER_HOME is a comma-separated list of
4015 * directories to search for the user account in.
4016 */
4017 {
4018 char_u test[MAXPATHL], paths[MAXPATHL];
4019 char_u *path, *next_path, *ptr;
4020 struct stat st;
4021
4022 STRCPY(paths, USER_HOME);
4023 next_path = paths;
4024 while (*next_path)
4025 {
4026 for (path = next_path; *next_path && *next_path != ',';
4027 next_path++);
4028 if (*next_path)
4029 *next_path++ = NUL;
4030 STRCPY(test, path);
4031 STRCAT(test, "/");
4032 STRCAT(test, dst + 1);
4033 if (mch_stat(test, &st) == 0)
4034 {
4035 var = alloc(STRLEN(test) + 1);
4036 STRCPY(var, test);
4037 mustfree = TRUE;
4038 break;
4039 }
4040 }
4041 }
4042# endif /* UNIX */
4043#else
4044 /* cannot expand user's home directory, so don't try */
4045 var = NULL;
4046 tail = (char_u *)""; /* for gcc */
4047#endif /* UNIX || VMS */
4048 }
4049
4050#ifdef BACKSLASH_IN_FILENAME
4051 /* If 'shellslash' is set change backslashes to forward slashes.
4052 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4053 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4054 {
4055 char_u *p = vim_strsave(var);
4056
4057 if (p != NULL)
4058 {
4059 if (mustfree)
4060 vim_free(var);
4061 var = p;
4062 mustfree = TRUE;
4063 forward_slash(var);
4064 }
4065 }
4066#endif
4067
4068 /* If "var" contains white space, escape it with a backslash.
4069 * Required for ":e ~/tt" when $HOME includes a space. */
4070 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4071 {
4072 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4073
4074 if (p != NULL)
4075 {
4076 if (mustfree)
4077 vim_free(var);
4078 var = p;
4079 mustfree = TRUE;
4080 }
4081 }
4082
4083 if (var != NULL && *var != NUL
4084 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4085 {
4086 STRCPY(dst, var);
4087 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004088 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 /* if var[] ends in a path separator and tail[] starts
4090 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004091 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4093 && dst[-1] != ':'
4094#endif
4095 && vim_ispathsep(*tail))
4096 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004097 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 src = tail;
4099 copy_char = FALSE;
4100 }
4101 if (mustfree)
4102 vim_free(var);
4103 }
4104
4105 if (copy_char) /* copy at least one char */
4106 {
4107 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004108 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004109 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4110 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 */
4112 at_start = FALSE;
4113 if (src[0] == '\\' && src[1] != NUL)
4114 {
4115 *dst++ = *src++;
4116 --dstlen;
4117 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004118 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 at_start = TRUE;
4120 *dst++ = *src++;
4121 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004122
4123 if (startstr != NULL && src - startstr_len >= srcp
4124 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4125 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127 }
4128 *dst = NUL;
4129}
4130
4131/*
4132 * Vim's version of getenv().
4133 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004134 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004135 * "mustfree" is set to TRUE when returned is allocated, it must be
4136 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 */
4138 char_u *
4139vim_getenv(name, mustfree)
4140 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004141 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 char_u *p;
4144 char_u *pend;
4145 int vimruntime;
4146
4147#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4148 /* use "C:/" when $HOME is not set */
4149 if (STRCMP(name, "HOME") == 0)
4150 return homedir;
4151#endif
4152
4153 p = mch_getenv(name);
4154 if (p != NULL && *p == NUL) /* empty is the same as not set */
4155 p = NULL;
4156
4157 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004158 {
4159#if defined(FEAT_MBYTE) && defined(WIN3264)
4160 if (enc_utf8)
4161 {
4162 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004163 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004164
4165 /* Convert from active codepage to UTF-8. Other conversions are
4166 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004167 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004168 if (pp != NULL)
4169 {
4170 p = pp;
4171 *mustfree = TRUE;
4172 }
4173 }
4174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4179 if (!vimruntime && STRCMP(name, "VIM") != 0)
4180 return NULL;
4181
4182 /*
4183 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4184 * Don't do this when default_vimruntime_dir is non-empty.
4185 */
4186 if (vimruntime
4187#ifdef HAVE_PATHDEF
4188 && *default_vimruntime_dir == NUL
4189#endif
4190 )
4191 {
4192 p = mch_getenv((char_u *)"VIM");
4193 if (p != NULL && *p == NUL) /* empty is the same as not set */
4194 p = NULL;
4195 if (p != NULL)
4196 {
4197 p = vim_version_dir(p);
4198 if (p != NULL)
4199 *mustfree = TRUE;
4200 else
4201 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004202
4203#if defined(FEAT_MBYTE) && defined(WIN3264)
4204 if (enc_utf8)
4205 {
4206 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004207 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004208
4209 /* Convert from active codepage to UTF-8. Other conversions
4210 * are not done, because they would fail for non-ASCII
4211 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004212 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004213 if (pp != NULL)
4214 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004215 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004216 vim_free(p);
4217 p = pp;
4218 *mustfree = TRUE;
4219 }
4220 }
4221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223 }
4224
4225 /*
4226 * When expanding $VIM or $VIMRUNTIME fails, try using:
4227 * - the directory name from 'helpfile' (unless it contains '$')
4228 * - the executable name from argv[0]
4229 */
4230 if (p == NULL)
4231 {
4232 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4233 p = p_hf;
4234#ifdef USE_EXE_NAME
4235 /*
4236 * Use the name of the executable, obtained from argv[0].
4237 */
4238 else
4239 p = exe_name;
4240#endif
4241 if (p != NULL)
4242 {
4243 /* remove the file name */
4244 pend = gettail(p);
4245
4246 /* remove "doc/" from 'helpfile', if present */
4247 if (p == p_hf)
4248 pend = remove_tail(p, pend, (char_u *)"doc");
4249
4250#ifdef USE_EXE_NAME
4251# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004252 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (p == exe_name)
4254 {
4255 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004256 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004258 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4259 if (pend1 != pend)
4260 {
4261 pnew = alloc((unsigned)(pend1 - p) + 15);
4262 if (pnew != NULL)
4263 {
4264 STRNCPY(pnew, p, (pend1 - p));
4265 STRCPY(pnew + (pend1 - p), "Resources/vim");
4266 p = pnew;
4267 pend = p + STRLEN(p);
4268 }
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271# endif
4272 /* remove "src/" from exe_name, if present */
4273 if (p == exe_name)
4274 pend = remove_tail(p, pend, (char_u *)"src");
4275#endif
4276
4277 /* for $VIM, remove "runtime/" or "vim54/", if present */
4278 if (!vimruntime)
4279 {
4280 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4281 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4282 }
4283
4284 /* remove trailing path separator */
4285#ifndef MACOS_CLASSIC
4286 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004287 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004288 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 --pend;
4290#endif
4291
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004292#ifdef MACOS_X
4293 if (p == exe_name || p == p_hf)
4294#endif
4295 /* check that the result is a directory name */
4296 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
4298 if (p != NULL && !mch_isdir(p))
4299 {
4300 vim_free(p);
4301 p = NULL;
4302 }
4303 else
4304 {
4305#ifdef USE_EXE_NAME
4306 /* may add "/vim54" or "/runtime" if it exists */
4307 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4308 {
4309 vim_free(p);
4310 p = pend;
4311 }
4312#endif
4313 *mustfree = TRUE;
4314 }
4315 }
4316 }
4317
4318#ifdef HAVE_PATHDEF
4319 /* When there is a pathdef.c file we can use default_vim_dir and
4320 * default_vimruntime_dir */
4321 if (p == NULL)
4322 {
4323 /* Only use default_vimruntime_dir when it is not empty */
4324 if (vimruntime && *default_vimruntime_dir != NUL)
4325 {
4326 p = default_vimruntime_dir;
4327 *mustfree = FALSE;
4328 }
4329 else if (*default_vim_dir != NUL)
4330 {
4331 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4332 *mustfree = TRUE;
4333 else
4334 {
4335 p = default_vim_dir;
4336 *mustfree = FALSE;
4337 }
4338 }
4339 }
4340#endif
4341
4342 /*
4343 * Set the environment variable, so that the new value can be found fast
4344 * next time, and others can also use it (e.g. Perl).
4345 */
4346 if (p != NULL)
4347 {
4348 if (vimruntime)
4349 {
4350 vim_setenv((char_u *)"VIMRUNTIME", p);
4351 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 else
4354 {
4355 vim_setenv((char_u *)"VIM", p);
4356 didset_vim = TRUE;
4357 }
4358 }
4359 return p;
4360}
4361
4362/*
4363 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4364 * Return NULL if not, return its name in allocated memory otherwise.
4365 */
4366 static char_u *
4367vim_version_dir(vimdir)
4368 char_u *vimdir;
4369{
4370 char_u *p;
4371
4372 if (vimdir == NULL || *vimdir == NUL)
4373 return NULL;
4374 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4375 if (p != NULL && mch_isdir(p))
4376 return p;
4377 vim_free(p);
4378 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4379 if (p != NULL && mch_isdir(p))
4380 return p;
4381 vim_free(p);
4382 return NULL;
4383}
4384
4385/*
4386 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4387 * the length of "name/". Otherwise return "pend".
4388 */
4389 static char_u *
4390remove_tail(p, pend, name)
4391 char_u *p;
4392 char_u *pend;
4393 char_u *name;
4394{
4395 int len = (int)STRLEN(name) + 1;
4396 char_u *newend = pend - len;
4397
4398 if (newend >= p
4399 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004400 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 return newend;
4402 return pend;
4403}
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 * Our portable version of setenv.
4407 */
4408 void
4409vim_setenv(name, val)
4410 char_u *name;
4411 char_u *val;
4412{
4413#ifdef HAVE_SETENV
4414 mch_setenv((char *)name, (char *)val, 1);
4415#else
4416 char_u *envbuf;
4417
4418 /*
4419 * Putenv does not copy the string, it has to remain
4420 * valid. The allocated memory will never be freed.
4421 */
4422 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4423 if (envbuf != NULL)
4424 {
4425 sprintf((char *)envbuf, "%s=%s", name, val);
4426 putenv((char *)envbuf);
4427 }
4428#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004429#ifdef FEAT_GETTEXT
4430 /*
4431 * When setting $VIMRUNTIME adjust the directory to find message
4432 * translations to $VIMRUNTIME/lang.
4433 */
4434 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4435 {
4436 char_u *buf = concat_str(val, (char_u *)"/lang");
4437
4438 if (buf != NULL)
4439 {
4440 bindtextdomain(VIMPACKAGE, (char *)buf);
4441 vim_free(buf);
4442 }
4443 }
4444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445}
4446
4447#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4448/*
4449 * Function given to ExpandGeneric() to obtain an environment variable name.
4450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 char_u *
4452get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004453 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 int idx;
4455{
4456# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4457 /*
4458 * No environ[] on the Amiga and on the Mac (using MPW).
4459 */
4460 return NULL;
4461# else
4462# ifndef __WIN32__
4463 /* Borland C++ 5.2 has this in a header file. */
4464 extern char **environ;
4465# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004466# define ENVNAMELEN 100
4467 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 char_u *str;
4469 int n;
4470
4471 str = (char_u *)environ[idx];
4472 if (str == NULL)
4473 return NULL;
4474
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004475 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 if (str[n] == '=' || str[n] == NUL)
4478 break;
4479 name[n] = str[n];
4480 }
4481 name[n] = NUL;
4482 return name;
4483# endif
4484}
Bram Moolenaar24305862012-08-15 14:05:05 +02004485
4486/*
4487 * Find all user names for user completion.
4488 * Done only once and then cached.
4489 */
4490 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004491init_users()
4492{
Bram Moolenaar24305862012-08-15 14:05:05 +02004493 static int lazy_init_done = FALSE;
4494
4495 if (lazy_init_done)
4496 return;
4497
4498 lazy_init_done = TRUE;
4499 ga_init2(&ga_users, sizeof(char_u *), 20);
4500
4501# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4502 {
4503 char_u* user;
4504 struct passwd* pw;
4505
4506 setpwent();
4507 while ((pw = getpwent()) != NULL)
4508 /* pw->pw_name shouldn't be NULL but just in case... */
4509 if (pw->pw_name != NULL)
4510 {
4511 if (ga_grow(&ga_users, 1) == FAIL)
4512 break;
4513 user = vim_strsave((char_u*)pw->pw_name);
4514 if (user == NULL)
4515 break;
4516 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4517 }
4518 endpwent();
4519 }
4520# endif
4521}
4522
4523/*
4524 * Function given to ExpandGeneric() to obtain an user names.
4525 */
4526 char_u*
4527get_users(xp, idx)
4528 expand_T *xp UNUSED;
4529 int idx;
4530{
4531 init_users();
4532 if (idx < ga_users.ga_len)
4533 return ((char_u **)ga_users.ga_data)[idx];
4534 return NULL;
4535}
4536
4537/*
4538 * Check whether name matches a user name. Return:
4539 * 0 if name does not match any user name.
4540 * 1 if name partially matches the beginning of a user name.
4541 * 2 is name fully matches a user name.
4542 */
4543int match_user(name)
4544 char_u* name;
4545{
4546 int i;
4547 int n = (int)STRLEN(name);
4548 int result = 0;
4549
4550 init_users();
4551 for (i = 0; i < ga_users.ga_len; i++)
4552 {
4553 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4554 return 2; /* full match */
4555 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4556 result = 1; /* partial match */
4557 }
4558 return result;
4559}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#endif
4561
4562/*
4563 * Replace home directory by "~" in each space or comma separated file name in
4564 * 'src'.
4565 * If anything fails (except when out of space) dst equals src.
4566 */
4567 void
4568home_replace(buf, src, dst, dstlen, one)
4569 buf_T *buf; /* when not NULL, check for help files */
4570 char_u *src; /* input file name */
4571 char_u *dst; /* where to put the result */
4572 int dstlen; /* maximum length of the result */
4573 int one; /* if TRUE, only replace one file name, include
4574 spaces and commas in the file name. */
4575{
4576 size_t dirlen = 0, envlen = 0;
4577 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004578 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 char_u *p;
4580
4581 if (src == NULL)
4582 {
4583 *dst = NUL;
4584 return;
4585 }
4586
4587 /*
4588 * If the file is a help file, remove the path completely.
4589 */
4590 if (buf != NULL && buf->b_help)
4591 {
4592 STRCPY(dst, gettail(src));
4593 return;
4594 }
4595
4596 /*
4597 * We check both the value of the $HOME environment variable and the
4598 * "real" home directory.
4599 */
4600 if (homedir != NULL)
4601 dirlen = STRLEN(homedir);
4602
4603#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004604 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004606 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4607#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004608 /* Empty is the same as not set. */
4609 if (homedir_env != NULL && *homedir_env == NUL)
4610 homedir_env = NULL;
4611
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004612#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004613 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004614 {
4615 int usedlen = 0;
4616 int flen;
4617 char_u *fbuf = NULL;
4618
4619 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004620 (void)modify_fname((char_u *)":p", &usedlen,
4621 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004622 flen = (int)STRLEN(homedir_env);
4623 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4624 /* Remove the trailing / that is added to a directory. */
4625 homedir_env[flen - 1] = NUL;
4626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#endif
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (homedir_env != NULL)
4630 envlen = STRLEN(homedir_env);
4631
4632 if (!one)
4633 src = skipwhite(src);
4634 while (*src && dstlen > 0)
4635 {
4636 /*
4637 * Here we are at the beginning of a file name.
4638 * First, check to see if the beginning of the file name matches
4639 * $HOME or the "real" home directory. Check that there is a '/'
4640 * after the match (so that if e.g. the file is "/home/pieter/bla",
4641 * and the home directory is "/home/piet", the file does not end up
4642 * as "~er/bla" (which would seem to indicate the file "bla" in user
4643 * er's home directory)).
4644 */
4645 p = homedir;
4646 len = dirlen;
4647 for (;;)
4648 {
4649 if ( len
4650 && fnamencmp(src, p, len) == 0
4651 && (vim_ispathsep(src[len])
4652 || (!one && (src[len] == ',' || src[len] == ' '))
4653 || src[len] == NUL))
4654 {
4655 src += len;
4656 if (--dstlen > 0)
4657 *dst++ = '~';
4658
4659 /*
4660 * If it's just the home directory, add "/".
4661 */
4662 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4663 *dst++ = '/';
4664 break;
4665 }
4666 if (p == homedir_env)
4667 break;
4668 p = homedir_env;
4669 len = envlen;
4670 }
4671
4672 /* if (!one) skip to separator: space or comma */
4673 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4674 *dst++ = *src++;
4675 /* skip separator */
4676 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4677 *dst++ = *src++;
4678 }
4679 /* if (dstlen == 0) out of space, what to do??? */
4680
4681 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004682
4683 if (homedir_env != homedir_env_orig)
4684 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685}
4686
4687/*
4688 * Like home_replace, store the replaced string in allocated memory.
4689 * When something fails, NULL is returned.
4690 */
4691 char_u *
4692home_replace_save(buf, src)
4693 buf_T *buf; /* when not NULL, check for help files */
4694 char_u *src; /* input file name */
4695{
4696 char_u *dst;
4697 unsigned len;
4698
4699 len = 3; /* space for "~/" and trailing NUL */
4700 if (src != NULL) /* just in case */
4701 len += (unsigned)STRLEN(src);
4702 dst = alloc(len);
4703 if (dst != NULL)
4704 home_replace(buf, src, dst, len, TRUE);
4705 return dst;
4706}
4707
4708/*
4709 * Compare two file names and return:
4710 * FPC_SAME if they both exist and are the same file.
4711 * FPC_SAMEX if they both don't exist and have the same file name.
4712 * FPC_DIFF if they both exist and are different files.
4713 * FPC_NOTX if they both don't exist.
4714 * FPC_DIFFX if one of them doesn't exist.
4715 * For the first name environment variables are expanded
4716 */
4717 int
4718fullpathcmp(s1, s2, checkname)
4719 char_u *s1, *s2;
4720 int checkname; /* when both don't exist, check file names */
4721{
4722#ifdef UNIX
4723 char_u exp1[MAXPATHL];
4724 char_u full1[MAXPATHL];
4725 char_u full2[MAXPATHL];
4726 struct stat st1, st2;
4727 int r1, r2;
4728
4729 expand_env(s1, exp1, MAXPATHL);
4730 r1 = mch_stat((char *)exp1, &st1);
4731 r2 = mch_stat((char *)s2, &st2);
4732 if (r1 != 0 && r2 != 0)
4733 {
4734 /* if mch_stat() doesn't work, may compare the names */
4735 if (checkname)
4736 {
4737 if (fnamecmp(exp1, s2) == 0)
4738 return FPC_SAMEX;
4739 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4740 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4741 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4742 return FPC_SAMEX;
4743 }
4744 return FPC_NOTX;
4745 }
4746 if (r1 != 0 || r2 != 0)
4747 return FPC_DIFFX;
4748 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4749 return FPC_SAME;
4750 return FPC_DIFF;
4751#else
4752 char_u *exp1; /* expanded s1 */
4753 char_u *full1; /* full path of s1 */
4754 char_u *full2; /* full path of s2 */
4755 int retval = FPC_DIFF;
4756 int r1, r2;
4757
4758 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4759 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4760 {
4761 full1 = exp1 + MAXPATHL;
4762 full2 = full1 + MAXPATHL;
4763
4764 expand_env(s1, exp1, MAXPATHL);
4765 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4766 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4767
4768 /* If vim_FullName() fails, the file probably doesn't exist. */
4769 if (r1 != OK && r2 != OK)
4770 {
4771 if (checkname && fnamecmp(exp1, s2) == 0)
4772 retval = FPC_SAMEX;
4773 else
4774 retval = FPC_NOTX;
4775 }
4776 else if (r1 != OK || r2 != OK)
4777 retval = FPC_DIFFX;
4778 else if (fnamecmp(full1, full2))
4779 retval = FPC_DIFF;
4780 else
4781 retval = FPC_SAME;
4782 vim_free(exp1);
4783 }
4784 return retval;
4785#endif
4786}
4787
4788/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004789 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004790 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004791 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 */
4793 char_u *
4794gettail(fname)
4795 char_u *fname;
4796{
4797 char_u *p1, *p2;
4798
4799 if (fname == NULL)
4800 return (char_u *)"";
4801 for (p1 = p2 = fname; *p2; ) /* find last part of path */
4802 {
4803 if (vim_ispathsep(*p2))
4804 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004805 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 return p1;
4808}
4809
Bram Moolenaar31710262010-08-13 13:36:15 +02004810#if defined(FEAT_SEARCHPATH)
4811static char_u *gettail_dir __ARGS((char_u *fname));
4812
4813/*
4814 * Return the end of the directory name, on the first path
4815 * separator:
4816 * "/path/file", "/path/dir/", "/path//dir", "/file"
4817 * ^ ^ ^ ^
4818 */
4819 static char_u *
4820gettail_dir(fname)
4821 char_u *fname;
4822{
4823 char_u *dir_end = fname;
4824 char_u *next_dir_end = fname;
4825 int look_for_sep = TRUE;
4826 char_u *p;
4827
4828 for (p = fname; *p != NUL; )
4829 {
4830 if (vim_ispathsep(*p))
4831 {
4832 if (look_for_sep)
4833 {
4834 next_dir_end = p;
4835 look_for_sep = FALSE;
4836 }
4837 }
4838 else
4839 {
4840 if (!look_for_sep)
4841 dir_end = next_dir_end;
4842 look_for_sep = TRUE;
4843 }
4844 mb_ptr_adv(p);
4845 }
4846 return dir_end;
4847}
4848#endif
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004851 * Get pointer to tail of "fname", including path separators. Putting a NUL
4852 * here leaves the directory name. Takes care of "c:/" and "//".
4853 * Always returns a valid pointer.
4854 */
4855 char_u *
4856gettail_sep(fname)
4857 char_u *fname;
4858{
4859 char_u *p;
4860 char_u *t;
4861
4862 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4863 t = gettail(fname);
4864 while (t > p && after_pathsep(fname, t))
4865 --t;
4866#ifdef VMS
4867 /* path separator is part of the path */
4868 ++t;
4869#endif
4870 return t;
4871}
4872
4873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 * get the next path component (just after the next path separator).
4875 */
4876 char_u *
4877getnextcomp(fname)
4878 char_u *fname;
4879{
4880 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004881 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (*fname)
4883 ++fname;
4884 return fname;
4885}
4886
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887/*
4888 * Get a pointer to one character past the head of a path name.
4889 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4890 * If there is no head, path is returned.
4891 */
4892 char_u *
4893get_past_head(path)
4894 char_u *path;
4895{
4896 char_u *retval;
4897
4898#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4899 /* may skip "c:" */
4900 if (isalpha(path[0]) && path[1] == ':')
4901 retval = path + 2;
4902 else
4903 retval = path;
4904#else
4905# if defined(AMIGA)
4906 /* may skip "label:" */
4907 retval = vim_strchr(path, ':');
4908 if (retval == NULL)
4909 retval = path;
4910# else /* Unix */
4911 retval = path;
4912# endif
4913#endif
4914
4915 while (vim_ispathsep(*retval))
4916 ++retval;
4917
4918 return retval;
4919}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921/*
4922 * return TRUE if 'c' is a path separator.
4923 */
4924 int
4925vim_ispathsep(c)
4926 int c;
4927{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004928#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004930#else
4931# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004933# else
4934# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4936 return (c == ':' || c == '[' || c == ']' || c == '/'
4937 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004938# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004940# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943}
4944
4945#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4946/*
4947 * return TRUE if 'c' is a path list separator.
4948 */
4949 int
4950vim_ispathlistsep(c)
4951 int c;
4952{
4953#ifdef UNIX
4954 return (c == ':');
4955#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004956 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957#endif
4958}
4959#endif
4960
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004961#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4962 || defined(FEAT_EVAL) || defined(PROTO)
4963/*
4964 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4965 * It's done in-place.
4966 */
4967 void
4968shorten_dir(str)
4969 char_u *str;
4970{
4971 char_u *tail, *s, *d;
4972 int skip = FALSE;
4973
4974 tail = gettail(str);
4975 d = str;
4976 for (s = str; ; ++s)
4977 {
4978 if (s >= tail) /* copy the whole tail */
4979 {
4980 *d++ = *s;
4981 if (*s == NUL)
4982 break;
4983 }
4984 else if (vim_ispathsep(*s)) /* copy '/' and next char */
4985 {
4986 *d++ = *s;
4987 skip = FALSE;
4988 }
4989 else if (!skip)
4990 {
4991 *d++ = *s; /* copy next char */
4992 if (*s != '~' && *s != '.') /* and leading "~" and "." */
4993 skip = TRUE;
4994# ifdef FEAT_MBYTE
4995 if (has_mbyte)
4996 {
4997 int l = mb_ptr2len(s);
4998
4999 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005000 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005001 }
5002# endif
5003 }
5004 }
5005}
5006#endif
5007
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005008/*
5009 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5010 * Also returns TRUE if there is no directory name.
5011 * "fname" must be writable!.
5012 */
5013 int
5014dir_of_file_exists(fname)
5015 char_u *fname;
5016{
5017 char_u *p;
5018 int c;
5019 int retval;
5020
5021 p = gettail_sep(fname);
5022 if (p == fname)
5023 return TRUE;
5024 c = *p;
5025 *p = NUL;
5026 retval = mch_isdir(fname);
5027 *p = c;
5028 return retval;
5029}
5030
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005032 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5033 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 */
5035 int
5036vim_fnamecmp(x, y)
5037 char_u *x, *y;
5038{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005039#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005041#else
5042 if (p_fic)
5043 return MB_STRICMP(x, y);
5044 return STRCMP(x, y);
5045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046}
5047
5048 int
5049vim_fnamencmp(x, y, len)
5050 char_u *x, *y;
5051 size_t len;
5052{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005053#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005054 char_u *px = x;
5055 char_u *py = y;
5056 int cx = NUL;
5057 int cy = NUL;
5058
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005059 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005061 cx = PTR2CHAR(px);
5062 cy = PTR2CHAR(py);
5063 if (cx == NUL || cy == NUL
5064 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5065 && !(cx == '/' && cy == '\\')
5066 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005068 len -= MB_PTR2LEN(px);
5069 px += MB_PTR2LEN(px);
5070 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 if (len == 0)
5073 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005074 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005075#else
5076 if (p_fic)
5077 return MB_STRNICMP(x, y, len);
5078 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005080}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
5082/*
5083 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005084 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 */
5086 char_u *
5087concat_fnames(fname1, fname2, sep)
5088 char_u *fname1;
5089 char_u *fname2;
5090 int sep;
5091{
5092 char_u *dest;
5093
5094 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5095 if (dest != NULL)
5096 {
5097 STRCPY(dest, fname1);
5098 if (sep)
5099 add_pathsep(dest);
5100 STRCAT(dest, fname2);
5101 }
5102 return dest;
5103}
5104
Bram Moolenaard6754642005-01-17 22:18:45 +00005105/*
5106 * Concatenate two strings and return the result in allocated memory.
5107 * Returns NULL when out of memory.
5108 */
5109 char_u *
5110concat_str(str1, str2)
5111 char_u *str1;
5112 char_u *str2;
5113{
5114 char_u *dest;
5115 size_t l = STRLEN(str1);
5116
5117 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5118 if (dest != NULL)
5119 {
5120 STRCPY(dest, str1);
5121 STRCPY(dest + l, str2);
5122 }
5123 return dest;
5124}
Bram Moolenaard6754642005-01-17 22:18:45 +00005125
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126/*
5127 * Add a path separator to a file name, unless it already ends in a path
5128 * separator.
5129 */
5130 void
5131add_pathsep(p)
5132 char_u *p;
5133{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005134 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 STRCAT(p, PATHSEPSTR);
5136}
5137
5138/*
5139 * FullName_save - Make an allocated copy of a full file name.
5140 * Returns NULL when out of memory.
5141 */
5142 char_u *
5143FullName_save(fname, force)
5144 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005145 int force; /* force expansion, even when it already looks
5146 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147{
5148 char_u *buf;
5149 char_u *new_fname = NULL;
5150
5151 if (fname == NULL)
5152 return NULL;
5153
5154 buf = alloc((unsigned)MAXPATHL);
5155 if (buf != NULL)
5156 {
5157 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5158 new_fname = vim_strsave(buf);
5159 else
5160 new_fname = vim_strsave(fname);
5161 vim_free(buf);
5162 }
5163 return new_fname;
5164}
5165
5166#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5167
5168static char_u *skip_string __ARGS((char_u *p));
5169
5170/*
5171 * Find the start of a comment, not knowing if we are in a comment right now.
5172 * Search starts at w_cursor.lnum and goes backwards.
5173 */
5174 pos_T *
5175find_start_comment(ind_maxcomment) /* XXX */
5176 int ind_maxcomment;
5177{
5178 pos_T *pos;
5179 char_u *line;
5180 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005181 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005183 for (;;)
5184 {
5185 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5186 if (pos == NULL)
5187 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005189 /*
5190 * Check if the comment start we found is inside a string.
5191 * If it is then restrict the search to below this line and try again.
5192 */
5193 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005194 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005195 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005196 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005197 break;
5198 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5199 if (cur_maxcomment <= 0)
5200 {
5201 pos = NULL;
5202 break;
5203 }
5204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 return pos;
5206}
5207
5208/*
5209 * Skip to the end of a "string" and a 'c' character.
5210 * If there is no string or character, return argument unmodified.
5211 */
5212 static char_u *
5213skip_string(p)
5214 char_u *p;
5215{
5216 int i;
5217
5218 /*
5219 * We loop, because strings may be concatenated: "date""time".
5220 */
5221 for ( ; ; ++p)
5222 {
5223 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5224 {
5225 if (!p[1]) /* ' at end of line */
5226 break;
5227 i = 2;
5228 if (p[1] == '\\') /* '\n' or '\000' */
5229 {
5230 ++i;
5231 while (vim_isdigit(p[i - 1])) /* '\000' */
5232 ++i;
5233 }
5234 if (p[i] == '\'') /* check for trailing ' */
5235 {
5236 p += i;
5237 continue;
5238 }
5239 }
5240 else if (p[0] == '"') /* start of string */
5241 {
5242 for (++p; p[0]; ++p)
5243 {
5244 if (p[0] == '\\' && p[1] != NUL)
5245 ++p;
5246 else if (p[0] == '"') /* end of string */
5247 break;
5248 }
5249 if (p[0] == '"')
5250 continue;
5251 }
5252 break; /* no string found */
5253 }
5254 if (!*p)
5255 --p; /* backup from NUL */
5256 return p;
5257}
5258#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5259
5260#if defined(FEAT_CINDENT) || defined(PROTO)
5261
5262/*
5263 * Do C or expression indenting on the current line.
5264 */
5265 void
5266do_c_expr_indent()
5267{
5268# ifdef FEAT_EVAL
5269 if (*curbuf->b_p_inde != NUL)
5270 fixthisline(get_expr_indent);
5271 else
5272# endif
5273 fixthisline(get_c_indent);
5274}
5275
5276/*
5277 * Functions for C-indenting.
5278 * Most of this originally comes from Eric Fischer.
5279 */
5280/*
5281 * Below "XXX" means that this function may unlock the current line.
5282 */
5283
5284static char_u *cin_skipcomment __ARGS((char_u *));
5285static int cin_nocode __ARGS((char_u *));
5286static pos_T *find_line_comment __ARGS((void));
5287static int cin_islabel_skip __ARGS((char_u **));
5288static int cin_isdefault __ARGS((char_u *));
5289static char_u *after_label __ARGS((char_u *l));
5290static int get_indent_nolabel __ARGS((linenr_T lnum));
5291static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment));
5292static int cin_first_id_amount __ARGS((void));
5293static int cin_get_equal_amount __ARGS((linenr_T lnum));
5294static int cin_ispreproc __ARGS((char_u *));
5295static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5296static int cin_iscomment __ARGS((char_u *));
5297static int cin_islinecomment __ARGS((char_u *));
5298static int cin_isterminated __ARGS((char_u *, int, int));
5299static int cin_isinit __ARGS((void));
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005300static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301static int cin_isif __ARGS((char_u *));
5302static int cin_iselse __ARGS((char_u *));
5303static int cin_isdo __ARGS((char_u *));
5304static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005305static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005306static int cin_iswhileofdo_end __ARGS((int terminated, int ind_maxparen, int ind_maxcomment));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005308static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005309static int get_baseclass_amount __ARGS((int col, int ind_maxparen, int ind_maxcomment, int ind_cpp_baseclass));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005311static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312static int cin_skip2pos __ARGS((pos_T *trypos));
5313static pos_T *find_start_brace __ARGS((int));
5314static pos_T *find_match_paren __ARGS((int, int));
5315static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos));
5316static int find_last_paren __ARGS((char_u *l, int start, int end));
5317static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005318static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005320static int ind_hash_comment = 0; /* # starts a comment */
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322/*
5323 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005324 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 */
5326 static char_u *
5327cin_skipcomment(s)
5328 char_u *s;
5329{
5330 while (*s)
5331 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005332 char_u *prev_s = s;
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005335
5336 /* Perl/shell # comment comment continues until eol. Require a space
5337 * before # to avoid recognizing $#array. */
5338 if (ind_hash_comment != 0 && s != prev_s && *s == '#')
5339 {
5340 s += STRLEN(s);
5341 break;
5342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (*s != '/')
5344 break;
5345 ++s;
5346 if (*s == '/') /* slash-slash comment continues till eol */
5347 {
5348 s += STRLEN(s);
5349 break;
5350 }
5351 if (*s != '*')
5352 break;
5353 for (++s; *s; ++s) /* skip slash-star comment */
5354 if (s[0] == '*' && s[1] == '/')
5355 {
5356 s += 2;
5357 break;
5358 }
5359 }
5360 return s;
5361}
5362
5363/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005364 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 * not considered code.
5366 */
5367 static int
5368cin_nocode(s)
5369 char_u *s;
5370{
5371 return *cin_skipcomment(s) == NUL;
5372}
5373
5374/*
5375 * Check previous lines for a "//" line comment, skipping over blank lines.
5376 */
5377 static pos_T *
5378find_line_comment() /* XXX */
5379{
5380 static pos_T pos;
5381 char_u *line;
5382 char_u *p;
5383
5384 pos = curwin->w_cursor;
5385 while (--pos.lnum > 0)
5386 {
5387 line = ml_get(pos.lnum);
5388 p = skipwhite(line);
5389 if (cin_islinecomment(p))
5390 {
5391 pos.col = (int)(p - line);
5392 return &pos;
5393 }
5394 if (*p != NUL)
5395 break;
5396 }
5397 return NULL;
5398}
5399
5400/*
5401 * Check if string matches "label:"; move to character after ':' if true.
5402 */
5403 static int
5404cin_islabel_skip(s)
5405 char_u **s;
5406{
5407 if (!vim_isIDc(**s)) /* need at least one ID character */
5408 return FALSE;
5409
5410 while (vim_isIDc(**s))
5411 (*s)++;
5412
5413 *s = cin_skipcomment(*s);
5414
5415 /* "::" is not a label, it's C++ */
5416 return (**s == ':' && *++*s != ':');
5417}
5418
5419/*
5420 * Recognize a label: "label:".
5421 * Note: curwin->w_cursor must be where we are looking for the label.
5422 */
5423 int
5424cin_islabel(ind_maxcomment) /* XXX */
5425 int ind_maxcomment;
5426{
5427 char_u *s;
5428
5429 s = cin_skipcomment(ml_get_curline());
5430
5431 /*
5432 * Exclude "default" from labels, since it should be indented
5433 * like a switch label. Same for C++ scope declarations.
5434 */
5435 if (cin_isdefault(s))
5436 return FALSE;
5437 if (cin_isscopedecl(s))
5438 return FALSE;
5439
5440 if (cin_islabel_skip(&s))
5441 {
5442 /*
5443 * Only accept a label if the previous line is terminated or is a case
5444 * label.
5445 */
5446 pos_T cursor_save;
5447 pos_T *trypos;
5448 char_u *line;
5449
5450 cursor_save = curwin->w_cursor;
5451 while (curwin->w_cursor.lnum > 1)
5452 {
5453 --curwin->w_cursor.lnum;
5454
5455 /*
5456 * If we're in a comment now, skip to the start of the comment.
5457 */
5458 curwin->w_cursor.col = 0;
5459 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
5460 curwin->w_cursor = *trypos;
5461
5462 line = ml_get_curline();
5463 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5464 continue;
5465 if (*(line = cin_skipcomment(line)) == NUL)
5466 continue;
5467
5468 curwin->w_cursor = cursor_save;
5469 if (cin_isterminated(line, TRUE, FALSE)
5470 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005471 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 || (cin_islabel_skip(&line) && cin_nocode(line)))
5473 return TRUE;
5474 return FALSE;
5475 }
5476 curwin->w_cursor = cursor_save;
5477 return TRUE; /* label at start of file??? */
5478 }
5479 return FALSE;
5480}
5481
5482/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005483 * Recognize structure initialization and enumerations:
5484 * "[typedef] [static|public|protected|private] enum"
5485 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 */
5487 static int
5488cin_isinit(void)
5489{
5490 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005491 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 s = cin_skipcomment(ml_get_curline());
5494
Bram Moolenaar75342212013-03-07 13:13:52 +01005495 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 s = cin_skipcomment(s + 7);
5497
Bram Moolenaar75342212013-03-07 13:13:52 +01005498 for (;;)
5499 {
5500 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005501
Bram Moolenaar75342212013-03-07 13:13:52 +01005502 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5503 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005504 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005505 if (cin_starts_with(s, skip[i]))
5506 {
5507 s = cin_skipcomment(s + l);
5508 l = 0;
5509 break;
5510 }
5511 }
5512 if (l != 0)
5513 break;
5514 }
5515
5516 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 return TRUE;
5518
5519 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5520 return TRUE;
5521
5522 return FALSE;
5523}
5524
5525/*
5526 * Recognize a switch label: "case .*:" or "default:".
5527 */
5528 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005529cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005531 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005534 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 for (s += 4; *s; ++s)
5537 {
5538 s = cin_skipcomment(s);
5539 if (*s == ':')
5540 {
5541 if (s[1] == ':') /* skip over "::" for C++ */
5542 ++s;
5543 else
5544 return TRUE;
5545 }
5546 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005547 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5549 return FALSE; /* stop at comment */
5550 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005551 {
5552 /* JS etc. */
5553 if (strict)
5554 return FALSE; /* stop at string */
5555 else
5556 return TRUE;
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
5559 return FALSE;
5560 }
5561
5562 if (cin_isdefault(s))
5563 return TRUE;
5564 return FALSE;
5565}
5566
5567/*
5568 * Recognize a "default" switch label.
5569 */
5570 static int
5571cin_isdefault(s)
5572 char_u *s;
5573{
5574 return (STRNCMP(s, "default", 7) == 0
5575 && *(s = cin_skipcomment(s + 7)) == ':'
5576 && s[1] != ':');
5577}
5578
5579/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005580 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 */
5582 int
5583cin_isscopedecl(s)
5584 char_u *s;
5585{
5586 int i;
5587
5588 s = cin_skipcomment(s);
5589 if (STRNCMP(s, "public", 6) == 0)
5590 i = 6;
5591 else if (STRNCMP(s, "protected", 9) == 0)
5592 i = 9;
5593 else if (STRNCMP(s, "private", 7) == 0)
5594 i = 7;
5595 else
5596 return FALSE;
5597 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5598}
5599
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005600/* Maximum number of lines to search back for a "namespace" line. */
5601#define FIND_NAMESPACE_LIM 20
5602
5603/*
5604 * Recognize a "namespace" scope declaration.
5605 */
5606 static int
5607cin_is_cpp_namespace(s)
5608 char_u *s;
5609{
5610 char_u *p;
5611 int has_name = FALSE;
5612
5613 s = cin_skipcomment(s);
5614 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5615 {
5616 p = cin_skipcomment(skipwhite(s + 9));
5617 while (*p != NUL)
5618 {
5619 if (vim_iswhite(*p))
5620 {
5621 has_name = TRUE; /* found end of a name */
5622 p = cin_skipcomment(skipwhite(p));
5623 }
5624 else if (*p == '{')
5625 {
5626 break;
5627 }
5628 else if (vim_iswordc(*p))
5629 {
5630 if (has_name)
5631 return FALSE; /* word character after skipping past name */
5632 ++p;
5633 }
5634 else
5635 {
5636 return FALSE;
5637 }
5638 }
5639 return TRUE;
5640 }
5641 return FALSE;
5642}
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644/*
5645 * Return a pointer to the first non-empty non-comment character after a ':'.
5646 * Return NULL if not found.
5647 * case 234: a = b;
5648 * ^
5649 */
5650 static char_u *
5651after_label(l)
5652 char_u *l;
5653{
5654 for ( ; *l; ++l)
5655 {
5656 if (*l == ':')
5657 {
5658 if (l[1] == ':') /* skip over "::" for C++ */
5659 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005660 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 break;
5662 }
5663 else if (*l == '\'' && l[1] && l[2] == '\'')
5664 l += 2; /* skip over 'x' */
5665 }
5666 if (*l == NUL)
5667 return NULL;
5668 l = cin_skipcomment(l + 1);
5669 if (*l == NUL)
5670 return NULL;
5671 return l;
5672}
5673
5674/*
5675 * Get indent of line "lnum", skipping a label.
5676 * Return 0 if there is nothing after the label.
5677 */
5678 static int
5679get_indent_nolabel(lnum) /* XXX */
5680 linenr_T lnum;
5681{
5682 char_u *l;
5683 pos_T fp;
5684 colnr_T col;
5685 char_u *p;
5686
5687 l = ml_get(lnum);
5688 p = after_label(l);
5689 if (p == NULL)
5690 return 0;
5691
5692 fp.col = (colnr_T)(p - l);
5693 fp.lnum = lnum;
5694 getvcol(curwin, &fp, &col, NULL, NULL);
5695 return (int)col;
5696}
5697
5698/*
5699 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005700 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 * label: if (asdf && asdfasdf)
5702 * ^
5703 */
5704 static int
5705skip_label(lnum, pp, ind_maxcomment)
5706 linenr_T lnum;
5707 char_u **pp;
5708 int ind_maxcomment;
5709{
5710 char_u *l;
5711 int amount;
5712 pos_T cursor_save;
5713
5714 cursor_save = curwin->w_cursor;
5715 curwin->w_cursor.lnum = lnum;
5716 l = ml_get_curline();
5717 /* XXX */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005718 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)
5719 || cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
5721 amount = get_indent_nolabel(lnum);
5722 l = after_label(ml_get_curline());
5723 if (l == NULL) /* just in case */
5724 l = ml_get_curline();
5725 }
5726 else
5727 {
5728 amount = get_indent();
5729 l = ml_get_curline();
5730 }
5731 *pp = l;
5732
5733 curwin->w_cursor = cursor_save;
5734 return amount;
5735}
5736
5737/*
5738 * Return the indent of the first variable name after a type in a declaration.
5739 * int a, indent of "a"
5740 * static struct foo b, indent of "b"
5741 * enum bla c, indent of "c"
5742 * Returns zero when it doesn't look like a declaration.
5743 */
5744 static int
5745cin_first_id_amount()
5746{
5747 char_u *line, *p, *s;
5748 int len;
5749 pos_T fp;
5750 colnr_T col;
5751
5752 line = ml_get_curline();
5753 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005754 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5756 {
5757 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005758 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5761 p = skipwhite(p + 6);
5762 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5763 p = skipwhite(p + 4);
5764 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5765 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5766 {
5767 s = skipwhite(p + len);
5768 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5769 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5770 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5771 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5772 p = s;
5773 }
5774 for (len = 0; vim_isIDc(p[len]); ++len)
5775 ;
5776 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5777 return 0;
5778
5779 p = skipwhite(p + len);
5780 fp.lnum = curwin->w_cursor.lnum;
5781 fp.col = (colnr_T)(p - line);
5782 getvcol(curwin, &fp, &col, NULL, NULL);
5783 return (int)col;
5784}
5785
5786/*
5787 * Return the indent of the first non-blank after an equal sign.
5788 * char *foo = "here";
5789 * Return zero if no (useful) equal sign found.
5790 * Return -1 if the line above "lnum" ends in a backslash.
5791 * foo = "asdf\
5792 * asdf\
5793 * here";
5794 */
5795 static int
5796cin_get_equal_amount(lnum)
5797 linenr_T lnum;
5798{
5799 char_u *line;
5800 char_u *s;
5801 colnr_T col;
5802 pos_T fp;
5803
5804 if (lnum > 1)
5805 {
5806 line = ml_get(lnum - 1);
5807 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5808 return -1;
5809 }
5810
5811 line = s = ml_get(lnum);
5812 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5813 {
5814 if (cin_iscomment(s)) /* ignore comments */
5815 s = cin_skipcomment(s);
5816 else
5817 ++s;
5818 }
5819 if (*s != '=')
5820 return 0;
5821
5822 s = skipwhite(s + 1);
5823 if (cin_nocode(s))
5824 return 0;
5825
5826 if (*s == '"') /* nice alignment for continued strings */
5827 ++s;
5828
5829 fp.lnum = lnum;
5830 fp.col = (colnr_T)(s - line);
5831 getvcol(curwin, &fp, &col, NULL, NULL);
5832 return (int)col;
5833}
5834
5835/*
5836 * Recognize a preprocessor statement: Any line that starts with '#'.
5837 */
5838 static int
5839cin_ispreproc(s)
5840 char_u *s;
5841{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005842 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 return TRUE;
5844 return FALSE;
5845}
5846
5847/*
5848 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5849 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5850 * start and return the line in "*pp".
5851 */
5852 static int
5853cin_ispreproc_cont(pp, lnump)
5854 char_u **pp;
5855 linenr_T *lnump;
5856{
5857 char_u *line = *pp;
5858 linenr_T lnum = *lnump;
5859 int retval = FALSE;
5860
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005861 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
5863 if (cin_ispreproc(line))
5864 {
5865 retval = TRUE;
5866 *lnump = lnum;
5867 break;
5868 }
5869 if (lnum == 1)
5870 break;
5871 line = ml_get(--lnum);
5872 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5873 break;
5874 }
5875
5876 if (lnum != *lnump)
5877 *pp = ml_get(*lnump);
5878 return retval;
5879}
5880
5881/*
5882 * Recognize the start of a C or C++ comment.
5883 */
5884 static int
5885cin_iscomment(p)
5886 char_u *p;
5887{
5888 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5889}
5890
5891/*
5892 * Recognize the start of a "//" comment.
5893 */
5894 static int
5895cin_islinecomment(p)
5896 char_u *p;
5897{
5898 return (p[0] == '/' && p[1] == '/');
5899}
5900
5901/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005902 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5903 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005905 * If a line begins with an "else", only consider it terminated if no unmatched
5906 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 * Return the character terminating the line (ending char's have precedence if
5908 * both apply in order to determine initializations).
5909 */
5910 static int
5911cin_isterminated(s, incl_open, incl_comma)
5912 char_u *s;
5913 int incl_open; /* include '{' at the end as terminator */
5914 int incl_comma; /* recognize a trailing comma */
5915{
Bram Moolenaar496f9512011-05-19 16:35:09 +02005916 char_u found_start = 0;
5917 unsigned n_open = 0;
5918 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920 s = cin_skipcomment(s);
5921
5922 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
5923 found_start = *s;
5924
Bram Moolenaar496f9512011-05-19 16:35:09 +02005925 if (!found_start)
5926 is_else = cin_iselse(s);
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 while (*s)
5929 {
5930 /* skip over comments, "" strings and 'c'haracters */
5931 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005932 if (*s == '}' && n_open > 0)
5933 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02005934 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005935 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 && cin_nocode(s + 1))
5937 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005938 else if (*s == '{')
5939 {
5940 if (incl_open && cin_nocode(s + 1))
5941 return *s;
5942 else
5943 ++n_open;
5944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
5946 if (*s)
5947 s++;
5948 }
5949 return found_start;
5950}
5951
5952/*
5953 * Recognize the basic picture of a function declaration -- it needs to
5954 * have an open paren somewhere and a close paren at the end of the line and
5955 * no semicolons anywhere.
5956 * When a line ends in a comma we continue looking in the next line.
5957 * "sp" points to a string with the line. When looking at other lines it must
5958 * be restored to the line. When it's NULL fetch lines here.
5959 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005960 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 */
5962 static int
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005963cin_isfuncdecl(sp, first_lnum, min_lnum, ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 char_u **sp;
5965 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005966 linenr_T min_lnum;
5967 int ind_maxparen;
5968 int ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 char_u *s;
5971 linenr_T lnum = first_lnum;
5972 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005973 pos_T *trypos;
5974 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
5976 if (sp == NULL)
5977 s = ml_get(lnum);
5978 else
5979 s = *sp;
5980
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005981 if (find_last_paren(s, '(', ')')
5982 && (trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL)
5983 {
5984 lnum = trypos->lnum;
5985 if (lnum < min_lnum)
5986 return FALSE;
5987
5988 s = ml_get(lnum);
5989 }
5990
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005991 /* Ignore line starting with #. */
5992 if (cin_ispreproc(s))
5993 return FALSE;
5994
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
5996 {
5997 if (cin_iscomment(s)) /* ignore comments */
5998 s = cin_skipcomment(s);
5999 else
6000 ++s;
6001 }
6002 if (*s != '(')
6003 return FALSE; /* ';', ' or " before any () or no '(' */
6004
6005 while (*s && *s != ';' && *s != '\'' && *s != '"')
6006 {
6007 if (*s == ')' && cin_nocode(s + 1))
6008 {
6009 /* ')' at the end: may have found a match
6010 * Check for he previous line not to end in a backslash:
6011 * #if defined(x) && \
6012 * defined(y)
6013 */
6014 lnum = first_lnum - 1;
6015 s = ml_get(lnum);
6016 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6017 retval = TRUE;
6018 goto done;
6019 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006020 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006022 int comma = (*s == ',');
6023
6024 /* ',' at the end: continue looking in the next line.
6025 * At the end: check for ',' in the next line, for this style:
6026 * func(arg1
6027 * , arg2) */
6028 for (;;)
6029 {
6030 if (lnum >= curbuf->b_ml.ml_line_count)
6031 break;
6032 s = ml_get(++lnum);
6033 if (!cin_ispreproc(s))
6034 break;
6035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 if (lnum >= curbuf->b_ml.ml_line_count)
6037 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006038 /* Require a comma at end of the line or a comma or ')' at the
6039 * start of next line. */
6040 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006041 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006042 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006043 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
6045 else if (cin_iscomment(s)) /* ignore comments */
6046 s = cin_skipcomment(s);
6047 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006050 just_started = FALSE;
6051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 }
6053
6054done:
6055 if (lnum != first_lnum && sp != NULL)
6056 *sp = ml_get(first_lnum);
6057
6058 return retval;
6059}
6060
6061 static int
6062cin_isif(p)
6063 char_u *p;
6064{
6065 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6066}
6067
6068 static int
6069cin_iselse(p)
6070 char_u *p;
6071{
6072 if (*p == '}') /* accept "} else" */
6073 p = cin_skipcomment(p + 1);
6074 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6075}
6076
6077 static int
6078cin_isdo(p)
6079 char_u *p;
6080{
6081 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6082}
6083
6084/*
6085 * Check if this is a "while" that should have a matching "do".
6086 * We only accept a "while (condition) ;", with only white space between the
6087 * ')' and ';'. The condition may be spread over several lines.
6088 */
6089 static int
6090cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */
6091 char_u *p;
6092 linenr_T lnum;
6093 int ind_maxparen;
6094{
6095 pos_T cursor_save;
6096 pos_T *trypos;
6097 int retval = FALSE;
6098
6099 p = cin_skipcomment(p);
6100 if (*p == '}') /* accept "} while (cond);" */
6101 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006102 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 {
6104 cursor_save = curwin->w_cursor;
6105 curwin->w_cursor.lnum = lnum;
6106 curwin->w_cursor.col = 0;
6107 p = ml_get_curline();
6108 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6109 {
6110 ++p;
6111 ++curwin->w_cursor.col;
6112 }
6113 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL
6114 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6115 retval = TRUE;
6116 curwin->w_cursor = cursor_save;
6117 }
6118 return retval;
6119}
6120
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006121/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006122 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006123 * Return 0 if there is none.
6124 * Otherwise return !0 and update "*poffset" to point to the place where the
6125 * string was found.
6126 */
6127 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006128cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006129 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006130 int *poffset;
6131{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006132 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006133
6134 if (offset-- < 2)
6135 return 0;
6136 while (offset > 2 && vim_iswhite(line[offset]))
6137 --offset;
6138
6139 offset -= 1;
6140 if (!STRNCMP(line + offset, "if", 2))
6141 goto probablyFound;
6142
6143 if (offset >= 1)
6144 {
6145 offset -= 1;
6146 if (!STRNCMP(line + offset, "for", 3))
6147 goto probablyFound;
6148
6149 if (offset >= 2)
6150 {
6151 offset -= 2;
6152 if (!STRNCMP(line + offset, "while", 5))
6153 goto probablyFound;
6154 }
6155 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006156 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006157
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006158probablyFound:
6159 if (!offset || !vim_isIDc(line[offset - 1]))
6160 {
6161 *poffset = offset;
6162 return 1;
6163 }
6164 return 0;
6165}
6166
6167/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006168 * Return TRUE if we are at the end of a do-while.
6169 * do
6170 * nothing;
6171 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006172 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006173 * Adjust the cursor to the line with "while".
6174 */
6175 static int
6176cin_iswhileofdo_end(terminated, ind_maxparen, ind_maxcomment)
6177 int terminated;
6178 int ind_maxparen;
6179 int ind_maxcomment;
6180{
6181 char_u *line;
6182 char_u *p;
6183 char_u *s;
6184 pos_T *trypos;
6185 int i;
6186
6187 if (terminated != ';') /* there must be a ';' at the end */
6188 return FALSE;
6189
6190 p = line = ml_get_curline();
6191 while (*p != NUL)
6192 {
6193 p = cin_skipcomment(p);
6194 if (*p == ')')
6195 {
6196 s = skipwhite(p + 1);
6197 if (*s == ';' && cin_nocode(s + 1))
6198 {
6199 /* Found ");" at end of the line, now check there is "while"
6200 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006201 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006202 curwin->w_cursor.col = i;
6203 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
6204 if (trypos != NULL)
6205 {
6206 s = cin_skipcomment(ml_get(trypos->lnum));
6207 if (*s == '}') /* accept "} while (cond);" */
6208 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006209 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006210 {
6211 curwin->w_cursor.lnum = trypos->lnum;
6212 return TRUE;
6213 }
6214 }
6215
6216 /* Searching may have made "line" invalid, get it again. */
6217 line = ml_get_curline();
6218 p = line + i;
6219 }
6220 }
6221 if (*p != NUL)
6222 ++p;
6223 }
6224 return FALSE;
6225}
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 static int
6228cin_isbreak(p)
6229 char_u *p;
6230{
6231 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6232}
6233
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006234/*
6235 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 * constructor-initialization. eg:
6237 *
6238 * class MyClass :
6239 * baseClass <-- here
6240 * class MyClass : public baseClass,
6241 * anotherBaseClass <-- here (should probably lineup ??)
6242 * MyClass::MyClass(...) :
6243 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006244 *
6245 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 */
6247 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006248cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006249 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 char_u *s;
6252 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006253 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006254 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
6256 *col = 0;
6257
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006258 s = skipwhite(line);
6259 if (*s == '#') /* skip #define FOO x ? (x) : x */
6260 return FALSE;
6261 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (*s == NUL)
6263 return FALSE;
6264
6265 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6266
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006267 /* Search for a line starting with '#', empty, ending in ';' or containing
6268 * '{' or '}' and start below it. This handles the following situations:
6269 * a = cond ?
6270 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006271 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006272 * func::foo()
6273 * : something
6274 * {}
6275 * Foo::Foo (int one, int two)
6276 * : something(4),
6277 * somethingelse(3)
6278 * {}
6279 */
6280 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006282 line = ml_get(lnum - 1);
6283 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006284 if (*s == '#' || *s == NUL)
6285 break;
6286 while (*s != NUL)
6287 {
6288 s = cin_skipcomment(s);
6289 if (*s == '{' || *s == '}'
6290 || (*s == ';' && cin_nocode(s + 1)))
6291 break;
6292 if (*s != NUL)
6293 ++s;
6294 }
6295 if (*s != NUL)
6296 break;
6297 --lnum;
6298 }
6299
Bram Moolenaare7c56862007-08-04 10:14:52 +00006300 line = ml_get(lnum);
6301 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006302 for (;;)
6303 {
6304 if (*s == NUL)
6305 {
6306 if (lnum == curwin->w_cursor.lnum)
6307 break;
6308 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006309 line = ml_get(++lnum);
6310 s = cin_skipcomment(line);
6311 if (*s == NUL)
6312 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006313 }
6314
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006315 if (s[0] == '"')
6316 s = skip_string(s) + 1;
6317 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 {
6319 if (s[1] == ':')
6320 {
6321 /* skip double colon. It can't be a constructor
6322 * initialization any more */
6323 lookfor_ctor_init = FALSE;
6324 s = cin_skipcomment(s + 2);
6325 }
6326 else if (lookfor_ctor_init || class_or_struct)
6327 {
6328 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006329 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 cpp_base_class = TRUE;
6331 lookfor_ctor_init = class_or_struct = FALSE;
6332 *col = 0;
6333 s = cin_skipcomment(s + 1);
6334 }
6335 else
6336 s = cin_skipcomment(s + 1);
6337 }
6338 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6339 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6340 {
6341 class_or_struct = TRUE;
6342 lookfor_ctor_init = FALSE;
6343
6344 if (*s == 'c')
6345 s = cin_skipcomment(s + 5);
6346 else
6347 s = cin_skipcomment(s + 6);
6348 }
6349 else
6350 {
6351 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6352 {
6353 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6354 }
6355 else if (s[0] == ')')
6356 {
6357 /* Constructor-initialization is assumed if we come across
6358 * something like "):" */
6359 class_or_struct = FALSE;
6360 lookfor_ctor_init = TRUE;
6361 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006362 else if (s[0] == '?')
6363 {
6364 /* Avoid seeing '() :' after '?' as constructor init. */
6365 return FALSE;
6366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 else if (!vim_isIDc(s[0]))
6368 {
6369 /* if it is not an identifier, we are wrong */
6370 class_or_struct = FALSE;
6371 lookfor_ctor_init = FALSE;
6372 }
6373 else if (*col == 0)
6374 {
6375 /* it can't be a constructor-initialization any more */
6376 lookfor_ctor_init = FALSE;
6377
6378 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006379 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 *col = (colnr_T)(s - line);
6381 }
6382
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006383 /* When the line ends in a comma don't align with it. */
6384 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6385 *col = 0;
6386
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 s = cin_skipcomment(s + 1);
6388 }
6389 }
6390
6391 return cpp_base_class;
6392}
6393
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006394 static int
6395get_baseclass_amount(col, ind_maxparen, ind_maxcomment, ind_cpp_baseclass)
6396 int col;
6397 int ind_maxparen;
6398 int ind_maxcomment;
6399 int ind_cpp_baseclass;
6400{
6401 int amount;
6402 colnr_T vcol;
6403 pos_T *trypos;
6404
6405 if (col == 0)
6406 {
6407 amount = get_indent();
6408 if (find_last_paren(ml_get_curline(), '(', ')')
6409 && (trypos = find_match_paren(ind_maxparen,
6410 ind_maxcomment)) != NULL)
6411 amount = get_indent_lnum(trypos->lnum); /* XXX */
6412 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
6413 amount += ind_cpp_baseclass;
6414 }
6415 else
6416 {
6417 curwin->w_cursor.col = col;
6418 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6419 amount = (int)vcol;
6420 }
6421 if (amount < ind_cpp_baseclass)
6422 amount = ind_cpp_baseclass;
6423 return amount;
6424}
6425
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426/*
6427 * Return TRUE if string "s" ends with the string "find", possibly followed by
6428 * white space and comments. Skip strings and comments.
6429 * Ignore "ignore" after "find" if it's not NULL.
6430 */
6431 static int
6432cin_ends_in(s, find, ignore)
6433 char_u *s;
6434 char_u *find;
6435 char_u *ignore;
6436{
6437 char_u *p = s;
6438 char_u *r;
6439 int len = (int)STRLEN(find);
6440
6441 while (*p != NUL)
6442 {
6443 p = cin_skipcomment(p);
6444 if (STRNCMP(p, find, len) == 0)
6445 {
6446 r = skipwhite(p + len);
6447 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6448 r = skipwhite(r + STRLEN(ignore));
6449 if (cin_nocode(r))
6450 return TRUE;
6451 }
6452 if (*p != NUL)
6453 ++p;
6454 }
6455 return FALSE;
6456}
6457
6458/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006459 * Return TRUE when "s" starts with "word" and then a non-ID character.
6460 */
6461 static int
6462cin_starts_with(s, word)
6463 char_u *s;
6464 char *word;
6465{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006466 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006467
6468 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6469}
6470
6471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 * Skip strings, chars and comments until at or past "trypos".
6473 * Return the column found.
6474 */
6475 static int
6476cin_skip2pos(trypos)
6477 pos_T *trypos;
6478{
6479 char_u *line;
6480 char_u *p;
6481
6482 p = line = ml_get(trypos->lnum);
6483 while (*p && (colnr_T)(p - line) < trypos->col)
6484 {
6485 if (cin_iscomment(p))
6486 p = cin_skipcomment(p);
6487 else
6488 {
6489 p = skip_string(p);
6490 ++p;
6491 }
6492 }
6493 return (int)(p - line);
6494}
6495
6496/*
6497 * Find the '{' at the start of the block we are in.
6498 * Return NULL if no match found.
6499 * Ignore a '{' that is in a comment, makes indenting the next three lines
6500 * work. */
6501/* foo() */
6502/* { */
6503/* } */
6504
6505 static pos_T *
6506find_start_brace(ind_maxcomment) /* XXX */
6507 int ind_maxcomment;
6508{
6509 pos_T cursor_save;
6510 pos_T *trypos;
6511 pos_T *pos;
6512 static pos_T pos_copy;
6513
6514 cursor_save = curwin->w_cursor;
6515 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6516 {
6517 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6518 trypos = &pos_copy;
6519 curwin->w_cursor = *trypos;
6520 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006521 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
6523 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */
6524 break;
6525 if (pos != NULL)
6526 curwin->w_cursor.lnum = pos->lnum;
6527 }
6528 curwin->w_cursor = cursor_save;
6529 return trypos;
6530}
6531
6532/*
6533 * Find the matching '(', failing if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006534 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 */
6536 static pos_T *
6537find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */
6538 int ind_maxparen;
6539 int ind_maxcomment;
6540{
6541 pos_T cursor_save;
6542 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006543 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
6545 cursor_save = curwin->w_cursor;
6546 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6547 {
6548 /* check if the ( is in a // comment */
6549 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6550 trypos = NULL;
6551 else
6552 {
6553 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6554 trypos = &pos_copy;
6555 curwin->w_cursor = *trypos;
6556 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */
6557 trypos = NULL;
6558 }
6559 }
6560 curwin->w_cursor = cursor_save;
6561 return trypos;
6562}
6563
6564/*
6565 * Return ind_maxparen corrected for the difference in line number between the
6566 * cursor position and "startpos". This makes sure that searching for a
6567 * matching paren above the cursor line doesn't find a match because of
6568 * looking a few lines further.
6569 */
6570 static int
6571corr_ind_maxparen(ind_maxparen, startpos)
6572 int ind_maxparen;
6573 pos_T *startpos;
6574{
6575 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6576
6577 if (n > 0 && n < ind_maxparen / 2)
6578 return ind_maxparen - (int)n;
6579 return ind_maxparen;
6580}
6581
6582/*
6583 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006584 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 */
6586 static int
6587find_last_paren(l, start, end)
6588 char_u *l;
6589 int start, end;
6590{
6591 int i;
6592 int retval = FALSE;
6593 int open_count = 0;
6594
6595 curwin->w_cursor.col = 0; /* default is start of line */
6596
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006597 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
6599 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6600 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6601 if (l[i] == start)
6602 ++open_count;
6603 else if (l[i] == end)
6604 {
6605 if (open_count > 0)
6606 --open_count;
6607 else
6608 {
6609 curwin->w_cursor.col = i;
6610 retval = TRUE;
6611 }
6612 }
6613 }
6614 return retval;
6615}
6616
6617 int
6618get_c_indent()
6619{
Bram Moolenaar14f24742012-08-08 18:01:05 +02006620 int sw = (int)get_sw_value();
6621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 /*
6623 * spaces from a block's opening brace the prevailing indent for that
6624 * block should be
6625 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006626
6627 int ind_level = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 /*
6630 * spaces from the edge of the line an open brace that's at the end of a
6631 * line is imagined to be.
6632 */
6633 int ind_open_imag = 0;
6634
6635 /*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006636 * spaces from the prevailing indent for a line that is not preceded by
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 * an opening brace.
6638 */
6639 int ind_no_brace = 0;
6640
6641 /*
6642 * column where the first { of a function should be located }
6643 */
6644 int ind_first_open = 0;
6645
6646 /*
6647 * spaces from the prevailing indent a leftmost open brace should be
6648 * located
6649 */
6650 int ind_open_extra = 0;
6651
6652 /*
6653 * spaces from the matching open brace (real location for one at the left
6654 * edge; imaginary location from one that ends a line) the matching close
6655 * brace should be located
6656 */
6657 int ind_close_extra = 0;
6658
6659 /*
6660 * spaces from the edge of the line an open brace sitting in the leftmost
6661 * column is imagined to be
6662 */
6663 int ind_open_left_imag = 0;
6664
6665 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006666 * Spaces jump labels should be shifted to the left if N is non-negative,
6667 * otherwise the jump label will be put to column 1.
6668 */
6669 int ind_jump_label = -1;
6670
6671 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 * spaces from the switch() indent a "case xx" label should be located
6673 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006674 int ind_case = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
6676 /*
6677 * spaces from the "case xx:" code after a switch() should be located
6678 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006679 int ind_case_code = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681 /*
6682 * lineup break at end of case in switch() with case label
6683 */
6684 int ind_case_break = 0;
6685
6686 /*
6687 * spaces from the class declaration indent a scope declaration label
6688 * should be located
6689 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006690 int ind_scopedecl = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 /*
6693 * spaces from the scope declaration label code should be located
6694 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006695 int ind_scopedecl_code = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697 /*
6698 * amount K&R-style parameters should be indented
6699 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006700 int ind_param = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702 /*
6703 * amount a function type spec should be indented
6704 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006705 int ind_func_type = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
6707 /*
6708 * amount a cpp base class declaration or constructor initialization
6709 * should be indented
6710 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006711 int ind_cpp_baseclass = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713 /*
6714 * additional spaces beyond the prevailing indent a continuation line
6715 * should be located
6716 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006717 int ind_continuation = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 /*
6720 * spaces from the indent of the line with an unclosed parentheses
6721 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006722 int ind_unclosed = sw * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
6724 /*
6725 * spaces from the indent of the line with an unclosed parentheses, which
6726 * itself is also unclosed
6727 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006728 int ind_unclosed2 = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 /*
6731 * suppress ignoring spaces from the indent of a line starting with an
6732 * unclosed parentheses.
6733 */
6734 int ind_unclosed_noignore = 0;
6735
6736 /*
6737 * If the opening paren is the last nonwhite character on the line, and
6738 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6739 * context (for very long lines).
6740 */
6741 int ind_unclosed_wrapped = 0;
6742
6743 /*
6744 * suppress ignoring white space when lining up with the character after
6745 * an unclosed parentheses.
6746 */
6747 int ind_unclosed_whiteok = 0;
6748
6749 /*
6750 * indent a closing parentheses under the line start of the matching
6751 * opening parentheses.
6752 */
6753 int ind_matching_paren = 0;
6754
6755 /*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006756 * indent a closing parentheses under the previous line.
6757 */
6758 int ind_paren_prev = 0;
6759
6760 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 * Extra indent for comments.
6762 */
6763 int ind_comment = 0;
6764
6765 /*
6766 * spaces from the comment opener when there is nothing after it.
6767 */
6768 int ind_in_comment = 3;
6769
6770 /*
6771 * boolean: if non-zero, use ind_in_comment even if there is something
6772 * after the comment opener.
6773 */
6774 int ind_in_comment2 = 0;
6775
6776 /*
6777 * max lines to search for an open paren
6778 */
6779 int ind_maxparen = 20;
6780
6781 /*
6782 * max lines to search for an open comment
6783 */
6784 int ind_maxcomment = 70;
6785
6786 /*
6787 * handle braces for java code
6788 */
6789 int ind_java = 0;
6790
6791 /*
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006792 * not to confuse JS object properties with labels
6793 */
6794 int ind_js = 0;
6795
6796 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 * handle blocked cases correctly
6798 */
6799 int ind_keep_case_label = 0;
6800
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006801 /*
6802 * handle C++ namespace
6803 */
6804 int ind_cpp_namespace = 0;
6805
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006806 /*
6807 * handle continuation lines containing conditions of if(), for() and
6808 * while()
6809 */
6810 int ind_if_for_while = 0;
6811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 pos_T cur_curpos;
6813 int amount;
6814 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006815 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 colnr_T col;
6817 char_u *theline;
6818 char_u *linecopy;
6819 pos_T *trypos;
6820 pos_T *tryposBrace = NULL;
6821 pos_T our_paren_pos;
6822 char_u *start;
6823 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006824#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825#define BRACE_AT_START 2 /* '{' is at start of line */
6826#define BRACE_AT_END 3 /* '{' is at end of line */
6827 linenr_T ourscope;
6828 char_u *l;
6829 char_u *look;
6830 char_u terminated;
6831 int lookfor;
6832#define LOOKFOR_INITIAL 0
6833#define LOOKFOR_IF 1
6834#define LOOKFOR_DO 2
6835#define LOOKFOR_CASE 3
6836#define LOOKFOR_ANY 4
6837#define LOOKFOR_TERM 5
6838#define LOOKFOR_UNTERM 6
6839#define LOOKFOR_SCOPEDECL 7
6840#define LOOKFOR_NOBREAK 8
6841#define LOOKFOR_CPP_BASECLASS 9
6842#define LOOKFOR_ENUM_OR_INIT 10
6843
6844 int whilelevel;
6845 linenr_T lnum;
6846 char_u *options;
Bram Moolenaar48d27922012-06-13 13:40:48 +02006847 char_u *digits;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 int fraction = 0; /* init for GCC */
6849 int divider;
6850 int n;
6851 int iscase;
6852 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006853 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006855 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006856 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
6858 for (options = curbuf->b_p_cino; *options; )
6859 {
6860 l = options++;
6861 if (*options == '-')
6862 ++options;
Bram Moolenaar48d27922012-06-13 13:40:48 +02006863 digits = options; /* remember where the digits start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 n = getdigits(&options);
6865 divider = 0;
6866 if (*options == '.') /* ".5s" means a fraction */
6867 {
6868 fraction = atol((char *)++options);
6869 while (VIM_ISDIGIT(*options))
6870 {
6871 ++options;
6872 if (divider)
6873 divider *= 10;
6874 else
6875 divider = 10;
6876 }
6877 }
6878 if (*options == 's') /* "2s" means two times 'shiftwidth' */
6879 {
Bram Moolenaar48d27922012-06-13 13:40:48 +02006880 if (options == digits)
Bram Moolenaar14f24742012-08-08 18:01:05 +02006881 n = sw; /* just "s" is one 'shiftwidth' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 else
6883 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02006884 n *= sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (divider)
Bram Moolenaar14f24742012-08-08 18:01:05 +02006886 n += (sw * fraction + divider / 2) / divider;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888 ++options;
6889 }
6890 if (l[1] == '-')
6891 n = -n;
6892 /* When adding an entry here, also update the default 'cinoptions' in
Bram Moolenaar39353fd2007-03-27 09:02:11 +00006893 * doc/indent.txt, and add explanation for it! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 switch (*l)
6895 {
6896 case '>': ind_level = n; break;
6897 case 'e': ind_open_imag = n; break;
6898 case 'n': ind_no_brace = n; break;
6899 case 'f': ind_first_open = n; break;
6900 case '{': ind_open_extra = n; break;
6901 case '}': ind_close_extra = n; break;
6902 case '^': ind_open_left_imag = n; break;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006903 case 'L': ind_jump_label = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 case ':': ind_case = n; break;
6905 case '=': ind_case_code = n; break;
6906 case 'b': ind_case_break = n; break;
6907 case 'p': ind_param = n; break;
6908 case 't': ind_func_type = n; break;
6909 case '/': ind_comment = n; break;
6910 case 'c': ind_in_comment = n; break;
6911 case 'C': ind_in_comment2 = n; break;
6912 case 'i': ind_cpp_baseclass = n; break;
6913 case '+': ind_continuation = n; break;
6914 case '(': ind_unclosed = n; break;
6915 case 'u': ind_unclosed2 = n; break;
6916 case 'U': ind_unclosed_noignore = n; break;
6917 case 'W': ind_unclosed_wrapped = n; break;
6918 case 'w': ind_unclosed_whiteok = n; break;
6919 case 'm': ind_matching_paren = n; break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006920 case 'M': ind_paren_prev = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 case ')': ind_maxparen = n; break;
6922 case '*': ind_maxcomment = n; break;
6923 case 'g': ind_scopedecl = n; break;
6924 case 'h': ind_scopedecl_code = n; break;
6925 case 'j': ind_java = n; break;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006926 case 'J': ind_js = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 case 'l': ind_keep_case_label = n; break;
Bram Moolenaar39353fd2007-03-27 09:02:11 +00006928 case '#': ind_hash_comment = n; break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006929 case 'N': ind_cpp_namespace = n; break;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006930 case 'k': ind_if_for_while = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
Bram Moolenaardfdf3c42010-03-23 18:22:46 +01006932 if (*options == ',')
6933 ++options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 }
6935
6936 /* remember where the cursor was when we started */
6937 cur_curpos = curwin->w_cursor;
6938
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006939 /* if we are at line 1 0 is fine, right? */
6940 if (cur_curpos.lnum == 1)
6941 return 0;
6942
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 /* Get a copy of the current contents of the line.
6944 * This is required, because only the most recent line obtained with
6945 * ml_get is valid! */
6946 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6947 if (linecopy == NULL)
6948 return 0;
6949
6950 /*
6951 * In insert mode and the cursor is on a ')' truncate the line at the
6952 * cursor position. We don't want to line up with the matching '(' when
6953 * inserting new stuff.
6954 * For unknown reasons the cursor might be past the end of the line, thus
6955 * check for that.
6956 */
6957 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006958 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 && linecopy[curwin->w_cursor.col] == ')')
6960 linecopy[curwin->w_cursor.col] = NUL;
6961
6962 theline = skipwhite(linecopy);
6963
6964 /* move the cursor to the start of the line */
6965
6966 curwin->w_cursor.col = 0;
6967
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006968 original_line_islabel = cin_islabel(ind_maxcomment); /* XXX */
6969
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 /*
6971 * #defines and so on always go at the left when included in 'cinkeys'.
6972 */
6973 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
6974 {
6975 amount = 0;
6976 }
6977
6978 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006979 * Is it a non-case label? Then that goes at the left margin too unless:
6980 * - JS flag is set.
6981 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006983 else if (original_line_islabel && !ind_js && ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {
6985 amount = 0;
6986 }
6987
6988 /*
6989 * If we're inside a "//" comment and there is a "//" comment in a
6990 * previous line, lineup with that one.
6991 */
6992 else if (cin_islinecomment(theline)
6993 && (trypos = find_line_comment()) != NULL) /* XXX */
6994 {
6995 /* find how indented the line beginning the comment is */
6996 getvcol(curwin, trypos, &col, NULL, NULL);
6997 amount = col;
6998 }
6999
7000 /*
7001 * If we're inside a comment and not looking at the start of the
7002 * comment, try using the 'comments' option.
7003 */
7004 else if (!cin_iscomment(theline)
7005 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
7006 {
7007 int lead_start_len = 2;
7008 int lead_middle_len = 1;
7009 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7010 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7011 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7012 char_u *p;
7013 int start_align = 0;
7014 int start_off = 0;
7015 int done = FALSE;
7016
7017 /* find how indented the line beginning the comment is */
7018 getvcol(curwin, trypos, &col, NULL, NULL);
7019 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007020 *lead_start = NUL;
7021 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
7023 p = curbuf->b_p_com;
7024 while (*p != NUL)
7025 {
7026 int align = 0;
7027 int off = 0;
7028 int what = 0;
7029
7030 while (*p != NUL && *p != ':')
7031 {
7032 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7033 what = *p++;
7034 else if (*p == COM_LEFT || *p == COM_RIGHT)
7035 align = *p++;
7036 else if (VIM_ISDIGIT(*p) || *p == '-')
7037 off = getdigits(&p);
7038 else
7039 ++p;
7040 }
7041
7042 if (*p == ':')
7043 ++p;
7044 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7045 if (what == COM_START)
7046 {
7047 STRCPY(lead_start, lead_end);
7048 lead_start_len = (int)STRLEN(lead_start);
7049 start_off = off;
7050 start_align = align;
7051 }
7052 else if (what == COM_MIDDLE)
7053 {
7054 STRCPY(lead_middle, lead_end);
7055 lead_middle_len = (int)STRLEN(lead_middle);
7056 }
7057 else if (what == COM_END)
7058 {
7059 /* If our line starts with the middle comment string, line it
7060 * up with the comment opener per the 'comments' option. */
7061 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7062 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7063 {
7064 done = TRUE;
7065 if (curwin->w_cursor.lnum > 1)
7066 {
7067 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007068 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 * the middle comment string matches in the previous
7070 * line, use the indent of that line. XXX */
7071 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7072 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7073 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7074 else if (STRNCMP(look, lead_middle,
7075 lead_middle_len) == 0)
7076 {
7077 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7078 break;
7079 }
7080 /* If the start comment string doesn't match with the
7081 * start of the comment, skip this entry. XXX */
7082 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7083 lead_start, lead_start_len) != 0)
7084 continue;
7085 }
7086 if (start_off != 0)
7087 amount += start_off;
7088 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007089 amount += vim_strsize(lead_start)
7090 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 break;
7092 }
7093
7094 /* If our line starts with the end comment string, line it up
7095 * with the middle comment */
7096 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7097 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7098 {
7099 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7100 /* XXX */
7101 if (off != 0)
7102 amount += off;
7103 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007104 amount += vim_strsize(lead_start)
7105 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 done = TRUE;
7107 break;
7108 }
7109 }
7110 }
7111
7112 /* If our line starts with an asterisk, line up with the
7113 * asterisk in the comment opener; otherwise, line up
7114 * with the first character of the comment text.
7115 */
7116 if (done)
7117 ;
7118 else if (theline[0] == '*')
7119 amount += 1;
7120 else
7121 {
7122 /*
7123 * If we are more than one line away from the comment opener, take
7124 * the indent of the previous non-empty line. If 'cino' has "CO"
7125 * and we are just below the comment opener and there are any
7126 * white characters after it line up with the text after it;
7127 * otherwise, add the amount specified by "c" in 'cino'
7128 */
7129 amount = -1;
7130 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7131 {
7132 if (linewhite(lnum)) /* skip blank lines */
7133 continue;
7134 amount = get_indent_lnum(lnum); /* XXX */
7135 break;
7136 }
7137 if (amount == -1) /* use the comment opener */
7138 {
7139 if (!ind_in_comment2)
7140 {
7141 start = ml_get(trypos->lnum);
7142 look = start + trypos->col + 2; /* skip / and * */
7143 if (*look != NUL) /* if something after it */
7144 trypos->col = (colnr_T)(skipwhite(look) - start);
7145 }
7146 getvcol(curwin, trypos, &col, NULL, NULL);
7147 amount = col;
7148 if (ind_in_comment2 || *look == NUL)
7149 amount += ind_in_comment;
7150 }
7151 }
7152 }
7153
7154 /*
7155 * Are we inside parentheses or braces?
7156 */ /* XXX */
7157 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL
7158 && ind_java == 0)
7159 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL
7160 || trypos != NULL)
7161 {
7162 if (trypos != NULL && tryposBrace != NULL)
7163 {
7164 /* Both an unmatched '(' and '{' is found. Use the one which is
7165 * closer to the current cursor position, set the other to NULL. */
7166 if (trypos->lnum != tryposBrace->lnum
7167 ? trypos->lnum < tryposBrace->lnum
7168 : trypos->col < tryposBrace->col)
7169 trypos = NULL;
7170 else
7171 tryposBrace = NULL;
7172 }
7173
7174 if (trypos != NULL)
7175 {
7176 /*
7177 * If the matching paren is more than one line away, use the indent of
7178 * a previous non-empty line that matches the same paren.
7179 */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007180 if (theline[0] == ')' && ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007182 /* Line up with the start of the matching paren line. */
7183 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7184 }
7185 else
7186 {
7187 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007188 our_paren_pos = *trypos;
7189 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007191 l = skipwhite(ml_get(lnum));
7192 if (cin_nocode(l)) /* skip comment lines */
7193 continue;
7194 if (cin_ispreproc_cont(&l, &lnum))
7195 continue; /* ignore #define, #if, etc. */
7196 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007198 /* Skip a comment. XXX */
7199 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7200 {
7201 lnum = trypos->lnum + 1;
7202 continue;
7203 }
7204
7205 /* XXX */
7206 if ((trypos = find_match_paren(
7207 corr_ind_maxparen(ind_maxparen, &cur_curpos),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 ind_maxcomment)) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007209 && trypos->lnum == our_paren_pos.lnum
7210 && trypos->col == our_paren_pos.col)
7211 {
7212 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007214 if (theline[0] == ')')
7215 {
7216 if (our_paren_pos.lnum != lnum
7217 && cur_amount > amount)
7218 cur_amount = amount;
7219 amount = -1;
7220 }
7221 break;
7222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
7224 }
7225
7226 /*
7227 * Line up with line where the matching paren is. XXX
7228 * If the line starts with a '(' or the indent for unclosed
7229 * parentheses is zero, line up with the unclosed parentheses.
7230 */
7231 if (amount == -1)
7232 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007233 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007234 int is_if_for_while = 0;
7235
7236 if (ind_if_for_while)
7237 {
7238 /* Look for the outermost opening parenthesis on this line
7239 * and check whether it belongs to an "if", "for" or "while". */
7240
7241 pos_T cursor_save = curwin->w_cursor;
7242 pos_T outermost;
7243 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007244
7245 trypos = &our_paren_pos;
7246 do {
7247 outermost = *trypos;
7248 curwin->w_cursor.lnum = outermost.lnum;
7249 curwin->w_cursor.col = outermost.col;
7250
7251 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
7252 } while (trypos && trypos->lnum == outermost.lnum);
7253
7254 curwin->w_cursor = cursor_save;
7255
7256 line = ml_get(outermost.lnum);
7257
7258 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007259 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007260 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007261
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007263 look = skipwhite(look);
7264 if (*look == '(')
7265 {
7266 linenr_T save_lnum = curwin->w_cursor.lnum;
7267 char_u *line;
7268 int look_col;
7269
7270 /* Ignore a '(' in front of the line that has a match before
7271 * our matching '('. */
7272 curwin->w_cursor.lnum = our_paren_pos.lnum;
7273 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007274 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007275 curwin->w_cursor.col = look_col + 1;
7276 if ((trypos = findmatchlimit(NULL, ')', 0, ind_maxparen))
7277 != NULL
7278 && trypos->lnum == our_paren_pos.lnum
7279 && trypos->col < our_paren_pos.col)
7280 ignore_paren_col = trypos->col + 1;
7281
7282 curwin->w_cursor.lnum = save_lnum;
7283 look = ml_get(our_paren_pos.lnum) + look_col;
7284 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007285 if (theline[0] == ')' || (ind_unclosed == 0 && is_if_for_while == 0)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007286 || (!ind_unclosed_noignore && *look == '('
7287 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
7289 /*
7290 * If we're looking at a close paren, line up right there;
7291 * otherwise, line up with the next (non-white) character.
7292 * When ind_unclosed_wrapped is set and the matching paren is
7293 * the last nonwhite character of the line, use either the
7294 * indent of the current line or the indentation of the next
7295 * outer paren and add ind_unclosed_wrapped (for very long
7296 * lines).
7297 */
7298 if (theline[0] != ')')
7299 {
7300 cur_amount = MAXCOL;
7301 l = ml_get(our_paren_pos.lnum);
7302 if (ind_unclosed_wrapped
7303 && cin_ends_in(l, (char_u *)"(", NULL))
7304 {
7305 /* look for opening unmatched paren, indent one level
7306 * for each additional level */
7307 n = 1;
7308 for (col = 0; col < our_paren_pos.col; ++col)
7309 {
7310 switch (l[col])
7311 {
7312 case '(':
7313 case '{': ++n;
7314 break;
7315
7316 case ')':
7317 case '}': if (n > 1)
7318 --n;
7319 break;
7320 }
7321 }
7322
7323 our_paren_pos.col = 0;
7324 amount += n * ind_unclosed_wrapped;
7325 }
7326 else if (ind_unclosed_whiteok)
7327 our_paren_pos.col++;
7328 else
7329 {
7330 col = our_paren_pos.col + 1;
7331 while (vim_iswhite(l[col]))
7332 col++;
7333 if (l[col] != NUL) /* In case of trailing space */
7334 our_paren_pos.col = col;
7335 else
7336 our_paren_pos.col++;
7337 }
7338 }
7339
7340 /*
7341 * Find how indented the paren is, or the character after it
7342 * if we did the above "if".
7343 */
7344 if (our_paren_pos.col > 0)
7345 {
7346 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7347 if (cur_amount > (int)col)
7348 cur_amount = col;
7349 }
7350 }
7351
7352 if (theline[0] == ')' && ind_matching_paren)
7353 {
7354 /* Line up with the start of the matching paren line. */
7355 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007356 else if ((ind_unclosed == 0 && is_if_for_while == 0)
7357 || (!ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007358 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
7360 if (cur_amount != MAXCOL)
7361 amount = cur_amount;
7362 }
7363 else
7364 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007365 /* Add ind_unclosed2 for each '(' before our matching one, but
7366 * ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007368 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 --our_paren_pos.col;
7371 switch (*ml_get_pos(&our_paren_pos))
7372 {
7373 case '(': amount += ind_unclosed2;
7374 col = our_paren_pos.col;
7375 break;
7376 case ')': amount -= ind_unclosed2;
7377 col = MAXCOL;
7378 break;
7379 }
7380 }
7381
7382 /* Use ind_unclosed once, when the first '(' is not inside
7383 * braces */
7384 if (col == MAXCOL)
7385 amount += ind_unclosed;
7386 else
7387 {
7388 curwin->w_cursor.lnum = our_paren_pos.lnum;
7389 curwin->w_cursor.col = col;
Bram Moolenaar367bec82011-04-11 14:26:19 +02007390 if (find_match_paren(ind_maxparen, ind_maxcomment) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 amount += ind_unclosed2;
7392 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007393 {
7394 if (is_if_for_while)
7395 amount += ind_if_for_while;
7396 else
7397 amount += ind_unclosed;
7398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
7400 /*
7401 * For a line starting with ')' use the minimum of the two
7402 * positions, to avoid giving it more indent than the previous
7403 * lines:
7404 * func_long_name( if (x
7405 * arg && yy
7406 * ) ^ not here ) ^ not here
7407 */
7408 if (cur_amount < amount)
7409 amount = cur_amount;
7410 }
7411 }
7412
7413 /* add extra indent for a comment */
7414 if (cin_iscomment(theline))
7415 amount += ind_comment;
7416 }
7417
7418 /*
7419 * Are we at least inside braces, then?
7420 */
7421 else
7422 {
7423 trypos = tryposBrace;
7424
7425 ourscope = trypos->lnum;
7426 start = ml_get(ourscope);
7427
7428 /*
7429 * Now figure out how indented the line is in general.
7430 * If the brace was at the start of the line, we use that;
7431 * otherwise, check out the indentation of the line as
7432 * a whole and then add the "imaginary indent" to that.
7433 */
7434 look = skipwhite(start);
7435 if (*look == '{')
7436 {
7437 getvcol(curwin, trypos, &col, NULL, NULL);
7438 amount = col;
7439 if (*start == '{')
7440 start_brace = BRACE_IN_COL0;
7441 else
7442 start_brace = BRACE_AT_START;
7443 }
7444 else
7445 {
7446 /*
7447 * that opening brace might have been on a continuation
7448 * line. if so, find the start of the line.
7449 */
7450 curwin->w_cursor.lnum = ourscope;
7451
7452 /*
7453 * position the cursor over the rightmost paren, so that
7454 * matching it will take us back to the start of the line.
7455 */
7456 lnum = ourscope;
7457 if (find_last_paren(start, '(', ')')
7458 && (trypos = find_match_paren(ind_maxparen,
7459 ind_maxcomment)) != NULL)
7460 lnum = trypos->lnum;
7461
7462 /*
7463 * It could have been something like
7464 * case 1: if (asdf &&
7465 * ldfd) {
7466 * }
7467 */
Bram Moolenaar6ec154b2011-06-12 21:51:08 +02007468 if (ind_js || (ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007469 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 amount = get_indent();
7471 else
7472 amount = skip_label(lnum, &l, ind_maxcomment);
7473
7474 start_brace = BRACE_AT_END;
7475 }
7476
7477 /*
7478 * if we're looking at a closing brace, that's where
7479 * we want to be. otherwise, add the amount of room
7480 * that an indent is supposed to be.
7481 */
7482 if (theline[0] == '}')
7483 {
7484 /*
7485 * they may want closing braces to line up with something
7486 * other than the open brace. indulge them, if so.
7487 */
7488 amount += ind_close_extra;
7489 }
7490 else
7491 {
7492 /*
7493 * If we're looking at an "else", try to find an "if"
7494 * to match it with.
7495 * If we're looking at a "while", try to find a "do"
7496 * to match it with.
7497 */
7498 lookfor = LOOKFOR_INITIAL;
7499 if (cin_iselse(theline))
7500 lookfor = LOOKFOR_IF;
7501 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen))
7502 /* XXX */
7503 lookfor = LOOKFOR_DO;
7504 if (lookfor != LOOKFOR_INITIAL)
7505 {
7506 curwin->w_cursor.lnum = cur_curpos.lnum;
7507 if (find_match(lookfor, ourscope, ind_maxparen,
7508 ind_maxcomment) == OK)
7509 {
7510 amount = get_indent(); /* XXX */
7511 goto theend;
7512 }
7513 }
7514
7515 /*
7516 * We get here if we are not on an "while-of-do" or "else" (or
7517 * failed to find a matching "if").
7518 * Search backwards for something to line up with.
7519 * First set amount for when we don't find anything.
7520 */
7521
7522 /*
7523 * if the '{' is _really_ at the left margin, use the imaginary
7524 * location of a left-margin brace. Otherwise, correct the
7525 * location for ind_open_extra.
7526 */
7527
7528 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7529 {
7530 amount = ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007531 lookfor_cpp_namespace = TRUE;
7532 }
7533 else if (start_brace == BRACE_AT_START &&
7534 lookfor_cpp_namespace) /* '{' is at start */
7535 {
7536
7537 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 }
7539 else
7540 {
7541 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 amount += ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007544
7545 l = skipwhite(ml_get_curline());
7546 if (cin_is_cpp_namespace(l))
7547 amount += ind_cpp_namespace;
7548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 else
7550 {
7551 /* Compensate for adding ind_open_extra later. */
7552 amount -= ind_open_extra;
7553 if (amount < 0)
7554 amount = 0;
7555 }
7556 }
7557
7558 lookfor_break = FALSE;
7559
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007560 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
7562 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
7563 amount += ind_case;
7564 }
7565 else if (cin_isscopedecl(theline)) /* private:, ... */
7566 {
7567 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
7568 amount += ind_scopedecl;
7569 }
7570 else
7571 {
7572 if (ind_case_break && cin_isbreak(theline)) /* break; ... */
7573 lookfor_break = TRUE;
7574
7575 lookfor = LOOKFOR_INITIAL;
7576 amount += ind_level; /* ind_level from start of block */
7577 }
7578 scope_amount = amount;
7579 whilelevel = 0;
7580
7581 /*
7582 * Search backwards. If we find something we recognize, line up
7583 * with that.
7584 *
7585 * if we're looking at an open brace, indent
7586 * the usual amount relative to the conditional
7587 * that opens the block.
7588 */
7589 curwin->w_cursor = cur_curpos;
7590 for (;;)
7591 {
7592 curwin->w_cursor.lnum--;
7593 curwin->w_cursor.col = 0;
7594
7595 /*
7596 * If we went all the way back to the start of our scope, line
7597 * up with it.
7598 */
7599 if (curwin->w_cursor.lnum <= ourscope)
7600 {
7601 /* we reached end of scope:
7602 * if looking for a enum or structure initialization
7603 * go further back:
7604 * if it is an initializer (enum xxx or xxx =), then
7605 * don't add ind_continuation, otherwise it is a variable
7606 * declaration:
7607 * int x,
7608 * here; <-- add ind_continuation
7609 */
7610 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7611 {
7612 if (curwin->w_cursor.lnum == 0
7613 || curwin->w_cursor.lnum
7614 < ourscope - ind_maxparen)
7615 {
7616 /* nothing found (abuse ind_maxparen as limit)
7617 * assume terminated line (i.e. a variable
7618 * initialization) */
7619 if (cont_amount > 0)
7620 amount = cont_amount;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007621 else if (!ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 amount += ind_continuation;
7623 break;
7624 }
7625
7626 l = ml_get_curline();
7627
7628 /*
7629 * If we're in a comment now, skip to the start of the
7630 * comment.
7631 */
7632 trypos = find_start_comment(ind_maxcomment);
7633 if (trypos != NULL)
7634 {
7635 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007636 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 continue;
7638 }
7639
7640 /*
7641 * Skip preprocessor directives and blank lines.
7642 */
7643 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7644 continue;
7645
7646 if (cin_nocode(l))
7647 continue;
7648
7649 terminated = cin_isterminated(l, FALSE, TRUE);
7650
7651 /*
7652 * If we are at top level and the line looks like a
7653 * function declaration, we are done
7654 * (it's a variable declaration).
7655 */
7656 if (start_brace != BRACE_IN_COL0
Bram Moolenaarc367faa2011-12-14 20:21:35 +01007657 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum,
7658 0, ind_maxparen, ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {
7660 /* if the line is terminated with another ','
7661 * it is a continued variable initialization.
7662 * don't add extra indent.
7663 * TODO: does not work, if a function
7664 * declaration is split over multiple lines:
7665 * cin_isfuncdecl returns FALSE then.
7666 */
7667 if (terminated == ',')
7668 break;
7669
7670 /* if it es a enum declaration or an assignment,
7671 * we are done.
7672 */
7673 if (terminated != ';' && cin_isinit())
7674 break;
7675
7676 /* nothing useful found */
7677 if (terminated == 0 || terminated == '{')
7678 continue;
7679 }
7680
7681 if (terminated != ';')
7682 {
7683 /* Skip parens and braces. Position the cursor
7684 * over the rightmost paren, so that matching it
7685 * will take us back to the start of the line.
7686 */ /* XXX */
7687 trypos = NULL;
7688 if (find_last_paren(l, '(', ')'))
7689 trypos = find_match_paren(ind_maxparen,
7690 ind_maxcomment);
7691
7692 if (trypos == NULL && find_last_paren(l, '{', '}'))
7693 trypos = find_start_brace(ind_maxcomment);
7694
7695 if (trypos != NULL)
7696 {
7697 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007698 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 continue;
7700 }
7701 }
7702
7703 /* it's a variable declaration, add indentation
7704 * like in
7705 * int a,
7706 * b;
7707 */
7708 if (cont_amount > 0)
7709 amount = cont_amount;
7710 else
7711 amount += ind_continuation;
7712 }
7713 else if (lookfor == LOOKFOR_UNTERM)
7714 {
7715 if (cont_amount > 0)
7716 amount = cont_amount;
7717 else
7718 amount += ind_continuation;
7719 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007720 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007721 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007722 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007724 {
7725 amount = scope_amount;
7726 if (theline[0] == '{')
7727 {
7728 amount += ind_open_extra;
7729 added_to_amount = ind_open_extra;
7730 }
7731 }
7732
7733 if (lookfor_cpp_namespace)
7734 {
7735 /*
7736 * Looking for C++ namespace, need to look further
7737 * back.
7738 */
7739 if (curwin->w_cursor.lnum == ourscope)
7740 continue;
7741
7742 if (curwin->w_cursor.lnum == 0
7743 || curwin->w_cursor.lnum
7744 < ourscope - FIND_NAMESPACE_LIM)
7745 break;
7746
7747 l = ml_get_curline();
7748
7749 /* If we're in a comment now, skip to the start of
7750 * the comment. */
7751 trypos = find_start_comment(ind_maxcomment);
7752 if (trypos != NULL)
7753 {
7754 curwin->w_cursor.lnum = trypos->lnum + 1;
7755 curwin->w_cursor.col = 0;
7756 continue;
7757 }
7758
7759 /* Skip preprocessor directives and blank lines. */
7760 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7761 continue;
7762
7763 /* Finally the actual check for "namespace". */
7764 if (cin_is_cpp_namespace(l))
7765 {
7766 amount += ind_cpp_namespace - added_to_amount;
7767 break;
7768 }
7769
7770 if (cin_nocode(l))
7771 continue;
7772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 }
7774 break;
7775 }
7776
7777 /*
7778 * If we're in a comment now, skip to the start of the comment.
7779 */ /* XXX */
7780 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7781 {
7782 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007783 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 continue;
7785 }
7786
7787 l = ml_get_curline();
7788
7789 /*
7790 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007791 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007793 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 if (iscase || cin_isscopedecl(l))
7795 {
7796 /* we are only looking for cpp base class
7797 * declaration/initialization any longer */
7798 if (lookfor == LOOKFOR_CPP_BASECLASS)
7799 break;
7800
7801 /* When looking for a "do" we are not interested in
7802 * labels. */
7803 if (whilelevel > 0)
7804 continue;
7805
7806 /*
7807 * case xx:
7808 * c = 99 + <- this indent plus continuation
7809 *-> here;
7810 */
7811 if (lookfor == LOOKFOR_UNTERM
7812 || lookfor == LOOKFOR_ENUM_OR_INIT)
7813 {
7814 if (cont_amount > 0)
7815 amount = cont_amount;
7816 else
7817 amount += ind_continuation;
7818 break;
7819 }
7820
7821 /*
7822 * case xx: <- line up with this case
7823 * x = 333;
7824 * case yy:
7825 */
7826 if ( (iscase && lookfor == LOOKFOR_CASE)
7827 || (iscase && lookfor_break)
7828 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7829 {
7830 /*
7831 * Check that this case label is not for another
7832 * switch()
7833 */ /* XXX */
7834 if ((trypos = find_start_brace(ind_maxcomment)) ==
7835 NULL || trypos->lnum == ourscope)
7836 {
7837 amount = get_indent(); /* XXX */
7838 break;
7839 }
7840 continue;
7841 }
7842
7843 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7844
7845 /*
7846 * case xx: if (cond) <- line up with this if
7847 * y = y + 1;
7848 * -> s = 99;
7849 *
7850 * case xx:
7851 * if (cond) <- line up with this line
7852 * y = y + 1;
7853 * -> s = 99;
7854 */
7855 if (lookfor == LOOKFOR_TERM)
7856 {
7857 if (n)
7858 amount = n;
7859
7860 if (!lookfor_break)
7861 break;
7862 }
7863
7864 /*
7865 * case xx: x = x + 1; <- line up with this x
7866 * -> y = y + 1;
7867 *
7868 * case xx: if (cond) <- line up with this if
7869 * -> y = y + 1;
7870 */
7871 if (n)
7872 {
7873 amount = n;
7874 l = after_label(ml_get_curline());
7875 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007876 {
7877 if (theline[0] == '{')
7878 amount += ind_open_extra;
7879 else
7880 amount += ind_level + ind_no_brace;
7881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 break;
7883 }
7884
7885 /*
7886 * Try to get the indent of a statement before the switch
7887 * label. If nothing is found, line up relative to the
7888 * switch label.
7889 * break; <- may line up with this line
7890 * case xx:
7891 * -> y = 1;
7892 */
7893 scope_amount = get_indent() + (iscase /* XXX */
7894 ? ind_case_code : ind_scopedecl_code);
7895 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
7896 continue;
7897 }
7898
7899 /*
7900 * Looking for a switch() label or C++ scope declaration,
7901 * ignore other lines, skip {}-blocks.
7902 */
7903 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7904 {
7905 if (find_last_paren(l, '{', '}') && (trypos =
7906 find_start_brace(ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007909 curwin->w_cursor.col = 0;
7910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 continue;
7912 }
7913
7914 /*
7915 * Ignore jump labels with nothing after them.
7916 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007917 if (!ind_js && cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
7919 l = after_label(ml_get_curline());
7920 if (l == NULL || cin_nocode(l))
7921 continue;
7922 }
7923
7924 /*
7925 * Ignore #defines, #if, etc.
7926 * Ignore comment and empty lines.
7927 * (need to get the line again, cin_islabel() may have
7928 * unlocked it)
7929 */
7930 l = ml_get_curline();
7931 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7932 || cin_nocode(l))
7933 continue;
7934
7935 /*
7936 * Are we at the start of a cpp base class declaration or
7937 * constructor initialization?
7938 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007939 n = FALSE;
7940 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0)
7941 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007942 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007943 l = ml_get_curline();
7944 }
7945 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {
7947 if (lookfor == LOOKFOR_UNTERM)
7948 {
7949 if (cont_amount > 0)
7950 amount = cont_amount;
7951 else
7952 amount += ind_continuation;
7953 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007954 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007956 /* Need to find start of the declaration. */
7957 lookfor = LOOKFOR_UNTERM;
7958 ind_continuation = 0;
7959 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 }
7961 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007962 /* XXX */
7963 amount = get_baseclass_amount(col, ind_maxparen,
7964 ind_maxcomment, ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 break;
7966 }
7967 else if (lookfor == LOOKFOR_CPP_BASECLASS)
7968 {
7969 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00007970 * declaration or initialization before the opening brace.
7971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 if (cin_isterminated(l, TRUE, FALSE))
7973 break;
7974 else
7975 continue;
7976 }
7977
7978 /*
7979 * What happens next depends on the line being terminated.
7980 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00007981 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 * 123,
7983 * sizeof
7984 * here
7985 * Otherwise check whether it is a enumeration or structure
7986 * initialisation (not indented) or a variable declaration
7987 * (indented).
7988 */
7989 terminated = cin_isterminated(l, FALSE, TRUE);
7990
7991 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
7992 && terminated == ','))
7993 {
7994 /*
7995 * if we're in the middle of a paren thing,
7996 * go back to the line that starts it so
7997 * we can get the right prevailing indent
7998 * if ( foo &&
7999 * bar )
8000 */
8001 /*
8002 * position the cursor over the rightmost paren, so that
8003 * matching it will take us back to the start of the line.
8004 */
8005 (void)find_last_paren(l, '(', ')');
8006 trypos = find_match_paren(
8007 corr_ind_maxparen(ind_maxparen, &cur_curpos),
8008 ind_maxcomment);
8009
8010 /*
8011 * If we are looking for ',', we also look for matching
8012 * braces.
8013 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008014 if (trypos == NULL && terminated == ','
8015 && find_last_paren(l, '{', '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 trypos = find_start_brace(ind_maxcomment);
8017
8018 if (trypos != NULL)
8019 {
8020 /*
8021 * Check if we are on a case label now. This is
8022 * handled above.
8023 * case xx: if ( asdf &&
8024 * asdf)
8025 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008026 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008028 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {
8030 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008031 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 continue;
8033 }
8034 }
8035
8036 /*
8037 * Skip over continuation lines to find the one to get the
8038 * indent from
8039 * char *usethis = "bla\
8040 * bla",
8041 * here;
8042 */
8043 if (terminated == ',')
8044 {
8045 while (curwin->w_cursor.lnum > 1)
8046 {
8047 l = ml_get(curwin->w_cursor.lnum - 1);
8048 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8049 break;
8050 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008051 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 }
8054
8055 /*
8056 * Get indent and pointer to text for current line,
8057 * ignoring any jump label. XXX
8058 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008059 if (!ind_js)
8060 cur_amount = skip_label(curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 &l, ind_maxcomment);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008062 else
8063 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 /*
8065 * If this is just above the line we are indenting, and it
8066 * starts with a '{', line it up with this line.
8067 * while (not)
8068 * -> {
8069 * }
8070 */
8071 if (terminated != ',' && lookfor != LOOKFOR_TERM
8072 && theline[0] == '{')
8073 {
8074 amount = cur_amount;
8075 /*
8076 * Only add ind_open_extra when the current line
8077 * doesn't start with a '{', which must have a match
8078 * in the same line (scope is the same). Probably:
8079 * { 1, 2 },
8080 * -> { 3, 4 }
8081 */
8082 if (*skipwhite(l) != '{')
8083 amount += ind_open_extra;
8084
8085 if (ind_cpp_baseclass)
8086 {
8087 /* have to look back, whether it is a cpp base
8088 * class declaration or initialization */
8089 lookfor = LOOKFOR_CPP_BASECLASS;
8090 continue;
8091 }
8092 break;
8093 }
8094
8095 /*
8096 * Check if we are after an "if", "while", etc.
8097 * Also allow " } else".
8098 */
8099 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8100 {
8101 /*
8102 * Found an unterminated line after an if (), line up
8103 * with the last one.
8104 * if (cond)
8105 * 100 +
8106 * -> here;
8107 */
8108 if (lookfor == LOOKFOR_UNTERM
8109 || lookfor == LOOKFOR_ENUM_OR_INIT)
8110 {
8111 if (cont_amount > 0)
8112 amount = cont_amount;
8113 else
8114 amount += ind_continuation;
8115 break;
8116 }
8117
8118 /*
8119 * If this is just above the line we are indenting, we
8120 * are finished.
8121 * while (not)
8122 * -> here;
8123 * Otherwise this indent can be used when the line
8124 * before this is terminated.
8125 * yyy;
8126 * if (stat)
8127 * while (not)
8128 * xxx;
8129 * -> here;
8130 */
8131 amount = cur_amount;
8132 if (theline[0] == '{')
8133 amount += ind_open_extra;
8134 if (lookfor != LOOKFOR_TERM)
8135 {
8136 amount += ind_level + ind_no_brace;
8137 break;
8138 }
8139
8140 /*
8141 * Special trick: when expecting the while () after a
8142 * do, line up with the while()
8143 * do
8144 * x = 1;
8145 * -> here
8146 */
8147 l = skipwhite(ml_get_curline());
8148 if (cin_isdo(l))
8149 {
8150 if (whilelevel == 0)
8151 break;
8152 --whilelevel;
8153 }
8154
8155 /*
8156 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008157 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 * Need to use the scope of this "else". XXX
8159 * If whilelevel != 0 continue looking for a "do {".
8160 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008161 if (cin_iselse(l) && whilelevel == 0)
8162 {
8163 /* If we're looking at "} else", let's make sure we
8164 * find the opening brace of the enclosing scope,
8165 * not the one from "if () {". */
8166 if (*l == '}')
8167 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008168 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008169
8170 if ((trypos = find_start_brace(ind_maxcomment))
8171 == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 || find_match(LOOKFOR_IF, trypos->lnum,
Bram Moolenaar334adf02011-05-25 13:34:04 +02008173 ind_maxparen, ind_maxcomment) == FAIL)
8174 break;
8175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
8177
8178 /*
8179 * If we're below an unterminated line that is not an
8180 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008181 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 * the line before this one.
8183 */
8184 else
8185 {
8186 /*
8187 * Found two unterminated lines on a row, line up with
8188 * the last one.
8189 * c = 99 +
8190 * 100 +
8191 * -> here;
8192 */
8193 if (lookfor == LOOKFOR_UNTERM)
8194 {
8195 /* When line ends in a comma add extra indent */
8196 if (terminated == ',')
8197 amount += ind_continuation;
8198 break;
8199 }
8200
8201 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8202 {
8203 /* Found two lines ending in ',', lineup with the
8204 * lowest one, but check for cpp base class
8205 * declaration/initialization, if it is an
8206 * opening brace or we are looking just for
8207 * enumerations/initializations. */
8208 if (terminated == ',')
8209 {
8210 if (ind_cpp_baseclass == 0)
8211 break;
8212
8213 lookfor = LOOKFOR_CPP_BASECLASS;
8214 continue;
8215 }
8216
8217 /* Ignore unterminated lines in between, but
8218 * reduce indent. */
8219 if (amount > cur_amount)
8220 amount = cur_amount;
8221 }
8222 else
8223 {
8224 /*
8225 * Found first unterminated line on a row, may
8226 * line up with this line, remember its indent
8227 * 100 +
8228 * -> here;
8229 */
8230 amount = cur_amount;
8231
8232 /*
8233 * If previous line ends in ',', check whether we
8234 * are in an initialization or enum
8235 * struct xxx =
8236 * {
8237 * sizeof a,
8238 * 124 };
8239 * or a normal possible continuation line.
8240 * but only, of no other statement has been found
8241 * yet.
8242 */
8243 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8244 {
8245 lookfor = LOOKFOR_ENUM_OR_INIT;
8246 cont_amount = cin_first_id_amount();
8247 }
8248 else
8249 {
8250 if (lookfor == LOOKFOR_INITIAL
8251 && *l != NUL
8252 && l[STRLEN(l) - 1] == '\\')
8253 /* XXX */
8254 cont_amount = cin_get_equal_amount(
8255 curwin->w_cursor.lnum);
8256 if (lookfor != LOOKFOR_TERM)
8257 lookfor = LOOKFOR_UNTERM;
8258 }
8259 }
8260 }
8261 }
8262
8263 /*
8264 * Check if we are after a while (cond);
8265 * If so: Ignore until the matching "do".
8266 */
8267 /* XXX */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008268 else if (cin_iswhileofdo_end(terminated, ind_maxparen,
8269 ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {
8271 /*
8272 * Found an unterminated line after a while ();, line up
8273 * with the last one.
8274 * while (cond);
8275 * 100 + <- line up with this one
8276 * -> here;
8277 */
8278 if (lookfor == LOOKFOR_UNTERM
8279 || lookfor == LOOKFOR_ENUM_OR_INIT)
8280 {
8281 if (cont_amount > 0)
8282 amount = cont_amount;
8283 else
8284 amount += ind_continuation;
8285 break;
8286 }
8287
8288 if (whilelevel == 0)
8289 {
8290 lookfor = LOOKFOR_TERM;
8291 amount = get_indent(); /* XXX */
8292 if (theline[0] == '{')
8293 amount += ind_open_extra;
8294 }
8295 ++whilelevel;
8296 }
8297
8298 /*
8299 * We are after a "normal" statement.
8300 * If we had another statement we can stop now and use the
8301 * indent of that other statement.
8302 * Otherwise the indent of the current statement may be used,
8303 * search backwards for the next "normal" statement.
8304 */
8305 else
8306 {
8307 /*
8308 * Skip single break line, if before a switch label. It
8309 * may be lined up with the case label.
8310 */
8311 if (lookfor == LOOKFOR_NOBREAK
8312 && cin_isbreak(skipwhite(ml_get_curline())))
8313 {
8314 lookfor = LOOKFOR_ANY;
8315 continue;
8316 }
8317
8318 /*
8319 * Handle "do {" line.
8320 */
8321 if (whilelevel > 0)
8322 {
8323 l = cin_skipcomment(ml_get_curline());
8324 if (cin_isdo(l))
8325 {
8326 amount = get_indent(); /* XXX */
8327 --whilelevel;
8328 continue;
8329 }
8330 }
8331
8332 /*
8333 * Found a terminated line above an unterminated line. Add
8334 * the amount for a continuation line.
8335 * x = 1;
8336 * y = foo +
8337 * -> here;
8338 * or
8339 * int x = 1;
8340 * int foo,
8341 * -> here;
8342 */
8343 if (lookfor == LOOKFOR_UNTERM
8344 || lookfor == LOOKFOR_ENUM_OR_INIT)
8345 {
8346 if (cont_amount > 0)
8347 amount = cont_amount;
8348 else
8349 amount += ind_continuation;
8350 break;
8351 }
8352
8353 /*
8354 * Found a terminated line above a terminated line or "if"
8355 * etc. line. Use the amount of the line below us.
8356 * x = 1; x = 1;
8357 * if (asdf) y = 2;
8358 * while (asdf) ->here;
8359 * here;
8360 * ->foo;
8361 */
8362 if (lookfor == LOOKFOR_TERM)
8363 {
8364 if (!lookfor_break && whilelevel == 0)
8365 break;
8366 }
8367
8368 /*
8369 * First line above the one we're indenting is terminated.
8370 * To know what needs to be done look further backward for
8371 * a terminated line.
8372 */
8373 else
8374 {
8375 /*
8376 * position the cursor over the rightmost paren, so
8377 * that matching it will take us back to the start of
8378 * the line. Helps for:
8379 * func(asdr,
8380 * asdfasdf);
8381 * here;
8382 */
8383term_again:
8384 l = ml_get_curline();
8385 if (find_last_paren(l, '(', ')')
8386 && (trypos = find_match_paren(ind_maxparen,
8387 ind_maxcomment)) != NULL)
8388 {
8389 /*
8390 * Check if we are on a case label now. This is
8391 * handled above.
8392 * case xx: if ( asdf &&
8393 * asdf)
8394 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008395 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008397 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {
8399 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008400 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 continue;
8402 }
8403 }
8404
8405 /* When aligning with the case statement, don't align
8406 * with a statement after it.
8407 * case 1: { <-- don't use this { position
8408 * stat;
8409 * }
8410 * case 2:
8411 * stat;
8412 * }
8413 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008414 iscase = (ind_keep_case_label && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415
8416 /*
8417 * Get indent and pointer to text for current line,
8418 * ignoring any jump label.
8419 */
8420 amount = skip_label(curwin->w_cursor.lnum,
8421 &l, ind_maxcomment);
8422
8423 if (theline[0] == '{')
8424 amount += ind_open_extra;
8425 /* See remark above: "Only add ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008426 l = skipwhite(l);
8427 if (*l == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 amount -= ind_open_extra;
8429 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8430
8431 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008432 * When a terminated line starts with "else" skip to
8433 * the matching "if":
8434 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008435 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008436 * Need to use the scope of this "else". XXX
8437 * If whilelevel != 0 continue looking for a "do {".
8438 */
8439 if (lookfor == LOOKFOR_TERM
8440 && *l != '}'
8441 && cin_iselse(l)
8442 && whilelevel == 0)
8443 {
8444 if ((trypos = find_start_brace(ind_maxcomment))
8445 == NULL
8446 || find_match(LOOKFOR_IF, trypos->lnum,
8447 ind_maxparen, ind_maxcomment) == FAIL)
8448 break;
8449 continue;
8450 }
8451
8452 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 * If we're at the end of a block, skip to the start of
8454 * that block.
8455 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008456 l = ml_get_curline();
Bram Moolenaar50f42ca2011-07-15 14:12:30 +02008457 if (find_last_paren(l, '{', '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 && (trypos = find_start_brace(ind_maxcomment))
8459 != NULL) /* XXX */
8460 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008461 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 /* if not "else {" check for terminated again */
8463 /* but skip block for "} else {" */
8464 l = cin_skipcomment(ml_get_curline());
8465 if (*l == '}' || !cin_iselse(l))
8466 goto term_again;
8467 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008468 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 }
8470 }
8471 }
8472 }
8473 }
8474 }
8475
8476 /* add extra indent for a comment */
8477 if (cin_iscomment(theline))
8478 amount += ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008479
8480 /* subtract extra left-shift for jump labels */
8481 if (ind_jump_label > 0 && original_line_islabel)
8482 amount -= ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
8484
8485 /*
8486 * ok -- we're not inside any sort of structure at all!
8487 *
8488 * this means we're at the top level, and everything should
8489 * basically just match where the previous line is, except
8490 * for the lines immediately following a function declaration,
8491 * which are K&R-style parameters and need to be indented.
8492 */
8493 else
8494 {
8495 /*
8496 * if our line starts with an open brace, forget about any
8497 * prevailing indent and make sure it looks like the start
8498 * of a function
8499 */
8500
8501 if (theline[0] == '{')
8502 {
8503 amount = ind_first_open;
8504 }
8505
8506 /*
8507 * If the NEXT line is a function declaration, the current
8508 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008509 * Don't do this if the current line looks like a comment or if the
8510 * current line is terminated, ie. ends in ';', or if the current line
8511 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 */
8513 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8514 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008515 && vim_strchr(theline, '{') == NULL
8516 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 && !cin_ends_in(theline, (char_u *)":", NULL)
8518 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008519 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8520 cur_curpos.lnum + 1,
8521 ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 && !cin_isterminated(theline, FALSE, TRUE))
8523 {
8524 amount = ind_func_type;
8525 }
8526 else
8527 {
8528 amount = 0;
8529 curwin->w_cursor = cur_curpos;
8530
8531 /* search backwards until we find something we recognize */
8532
8533 while (curwin->w_cursor.lnum > 1)
8534 {
8535 curwin->w_cursor.lnum--;
8536 curwin->w_cursor.col = 0;
8537
8538 l = ml_get_curline();
8539
8540 /*
8541 * If we're in a comment now, skip to the start of the comment.
8542 */ /* XXX */
8543 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
8544 {
8545 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008546 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 continue;
8548 }
8549
8550 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008551 * Are we at the start of a cpp base class declaration or
8552 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008554 n = FALSE;
8555 if (ind_cpp_baseclass != 0 && theline[0] != '{')
8556 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008557 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008558 l = ml_get_curline();
8559 }
8560 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008562 /* XXX */
8563 amount = get_baseclass_amount(col, ind_maxparen,
8564 ind_maxcomment, ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 break;
8566 }
8567
8568 /*
8569 * Skip preprocessor directives and blank lines.
8570 */
8571 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8572 continue;
8573
8574 if (cin_nocode(l))
8575 continue;
8576
8577 /*
8578 * If the previous line ends in ',', use one level of
8579 * indentation:
8580 * int foo,
8581 * bar;
8582 * do this before checking for '}' in case of eg.
8583 * enum foobar
8584 * {
8585 * ...
8586 * } foo,
8587 * bar;
8588 */
8589 n = 0;
8590 if (cin_ends_in(l, (char_u *)",", NULL)
8591 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8592 {
8593 /* take us back to opening paren */
8594 if (find_last_paren(l, '(', ')')
8595 && (trypos = find_match_paren(ind_maxparen,
8596 ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008597 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
8599 /* For a line ending in ',' that is a continuation line go
8600 * back to the first line with a backslash:
8601 * char *foo = "bla\
8602 * bla",
8603 * here;
8604 */
8605 while (n == 0 && curwin->w_cursor.lnum > 1)
8606 {
8607 l = ml_get(curwin->w_cursor.lnum - 1);
8608 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8609 break;
8610 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008611 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613
8614 amount = get_indent(); /* XXX */
8615
8616 if (amount == 0)
8617 amount = cin_first_id_amount();
8618 if (amount == 0)
8619 amount = ind_continuation;
8620 break;
8621 }
8622
8623 /*
8624 * If the line looks like a function declaration, and we're
8625 * not in a comment, put it the left margin.
8626 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008627 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0,
8628 ind_maxparen, ind_maxcomment)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 break;
8630 l = ml_get_curline();
8631
8632 /*
8633 * Finding the closing '}' of a previous function. Put
8634 * current line at the left margin. For when 'cino' has "fs".
8635 */
8636 if (*skipwhite(l) == '}')
8637 break;
8638
8639 /* (matching {)
8640 * If the previous line ends on '};' (maybe followed by
8641 * comments) align at column 0. For example:
8642 * char *string_array[] = { "foo",
8643 * / * x * / "b};ar" }; / * foobar * /
8644 */
8645 if (cin_ends_in(l, (char_u *)"};", NULL))
8646 break;
8647
8648 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008649 * Find a line only has a semicolon that belongs to a previous
8650 * line ending in '}', e.g. before an #endif. Don't increase
8651 * indent then.
8652 */
8653 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8654 {
8655 pos_T curpos_save = curwin->w_cursor;
8656
8657 while (curwin->w_cursor.lnum > 1)
8658 {
8659 look = ml_get(--curwin->w_cursor.lnum);
8660 if (!(cin_nocode(look) || cin_ispreproc_cont(
8661 &look, &curwin->w_cursor.lnum)))
8662 break;
8663 }
8664 if (curwin->w_cursor.lnum > 0
8665 && cin_ends_in(look, (char_u *)"}", NULL))
8666 break;
8667
8668 curwin->w_cursor = curpos_save;
8669 }
8670
8671 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 * If the PREVIOUS line is a function declaration, the current
8673 * line (and the ones that follow) needs to be indented as
8674 * parameters.
8675 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008676 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0,
8677 ind_maxparen, ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {
8679 amount = ind_param;
8680 break;
8681 }
8682
8683 /*
8684 * If the previous line ends in ';' and the line before the
8685 * previous line ends in ',' or '\', ident to column zero:
8686 * int foo,
8687 * bar;
8688 * indent_to_0 here;
8689 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008690 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
8692 l = ml_get(curwin->w_cursor.lnum - 1);
8693 if (cin_ends_in(l, (char_u *)",", NULL)
8694 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8695 break;
8696 l = ml_get_curline();
8697 }
8698
8699 /*
8700 * Doesn't look like anything interesting -- so just
8701 * use the indent of this line.
8702 *
8703 * Position the cursor over the rightmost paren, so that
8704 * matching it will take us back to the start of the line.
8705 */
8706 find_last_paren(l, '(', ')');
8707
8708 if ((trypos = find_match_paren(ind_maxparen,
8709 ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008710 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 amount = get_indent(); /* XXX */
8712 break;
8713 }
8714
8715 /* add extra indent for a comment */
8716 if (cin_iscomment(theline))
8717 amount += ind_comment;
8718
8719 /* add extra indent if the previous line ended in a backslash:
8720 * "asdfasdf\
8721 * here";
8722 * char *foo = "asdf\
8723 * here";
8724 */
8725 if (cur_curpos.lnum > 1)
8726 {
8727 l = ml_get(cur_curpos.lnum - 1);
8728 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8729 {
8730 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8731 if (cur_amount > 0)
8732 amount = cur_amount;
8733 else if (cur_amount == 0)
8734 amount += ind_continuation;
8735 }
8736 }
8737 }
8738 }
8739
8740theend:
8741 /* put the cursor back where it belongs */
8742 curwin->w_cursor = cur_curpos;
8743
8744 vim_free(linecopy);
8745
8746 if (amount < 0)
8747 return 0;
8748 return amount;
8749}
8750
8751 static int
8752find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment)
8753 int lookfor;
8754 linenr_T ourscope;
8755 int ind_maxparen;
8756 int ind_maxcomment;
8757{
8758 char_u *look;
8759 pos_T *theirscope;
8760 char_u *mightbeif;
8761 int elselevel;
8762 int whilelevel;
8763
8764 if (lookfor == LOOKFOR_IF)
8765 {
8766 elselevel = 1;
8767 whilelevel = 0;
8768 }
8769 else
8770 {
8771 elselevel = 0;
8772 whilelevel = 1;
8773 }
8774
8775 curwin->w_cursor.col = 0;
8776
8777 while (curwin->w_cursor.lnum > ourscope + 1)
8778 {
8779 curwin->w_cursor.lnum--;
8780 curwin->w_cursor.col = 0;
8781
8782 look = cin_skipcomment(ml_get_curline());
8783 if (cin_iselse(look)
8784 || cin_isif(look)
8785 || cin_isdo(look) /* XXX */
8786 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8787 {
8788 /*
8789 * if we've gone outside the braces entirely,
8790 * we must be out of scope...
8791 */
8792 theirscope = find_start_brace(ind_maxcomment); /* XXX */
8793 if (theirscope == NULL)
8794 break;
8795
8796 /*
8797 * and if the brace enclosing this is further
8798 * back than the one enclosing the else, we're
8799 * out of luck too.
8800 */
8801 if (theirscope->lnum < ourscope)
8802 break;
8803
8804 /*
8805 * and if they're enclosed in a *deeper* brace,
8806 * then we can ignore it because it's in a
8807 * different scope...
8808 */
8809 if (theirscope->lnum > ourscope)
8810 continue;
8811
8812 /*
8813 * if it was an "else" (that's not an "else if")
8814 * then we need to go back to another if, so
8815 * increment elselevel
8816 */
8817 look = cin_skipcomment(ml_get_curline());
8818 if (cin_iselse(look))
8819 {
8820 mightbeif = cin_skipcomment(look + 4);
8821 if (!cin_isif(mightbeif))
8822 ++elselevel;
8823 continue;
8824 }
8825
8826 /*
8827 * if it was a "while" then we need to go back to
8828 * another "do", so increment whilelevel. XXX
8829 */
8830 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8831 {
8832 ++whilelevel;
8833 continue;
8834 }
8835
8836 /* If it's an "if" decrement elselevel */
8837 look = cin_skipcomment(ml_get_curline());
8838 if (cin_isif(look))
8839 {
8840 elselevel--;
8841 /*
8842 * When looking for an "if" ignore "while"s that
8843 * get in the way.
8844 */
8845 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8846 whilelevel = 0;
8847 }
8848
8849 /* If it's a "do" decrement whilelevel */
8850 if (cin_isdo(look))
8851 whilelevel--;
8852
8853 /*
8854 * if we've used up all the elses, then
8855 * this must be the if that we want!
8856 * match the indent level of that if.
8857 */
8858 if (elselevel <= 0 && whilelevel <= 0)
8859 {
8860 return OK;
8861 }
8862 }
8863 }
8864 return FAIL;
8865}
8866
8867# if defined(FEAT_EVAL) || defined(PROTO)
8868/*
8869 * Get indent level from 'indentexpr'.
8870 */
8871 int
8872get_expr_indent()
8873{
8874 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008875 pos_T save_pos;
8876 colnr_T save_curswant;
8877 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008879 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8880 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008882 /* Save and restore cursor position and curswant, in case it was changed
8883 * via :normal commands */
8884 save_pos = curwin->w_cursor;
8885 save_curswant = curwin->w_curswant;
8886 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008888 if (use_sandbox)
8889 ++sandbox;
8890 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008892 if (use_sandbox)
8893 --sandbox;
8894 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
8896 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8897 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8898 * command. */
8899 save_State = State;
8900 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008901 curwin->w_cursor = save_pos;
8902 curwin->w_curswant = save_curswant;
8903 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 check_cursor();
8905 State = save_State;
8906
8907 /* If there is an error, just keep the current indent. */
8908 if (indent < 0)
8909 indent = get_indent();
8910
8911 return indent;
8912}
8913# endif
8914
8915#endif /* FEAT_CINDENT */
8916
8917#if defined(FEAT_LISP) || defined(PROTO)
8918
8919static int lisp_match __ARGS((char_u *p));
8920
8921 static int
8922lisp_match(p)
8923 char_u *p;
8924{
8925 char_u buf[LSIZE];
8926 int len;
8927 char_u *word = p_lispwords;
8928
8929 while (*word != NUL)
8930 {
8931 (void)copy_option_part(&word, buf, LSIZE, ",");
8932 len = (int)STRLEN(buf);
8933 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8934 return TRUE;
8935 }
8936 return FALSE;
8937}
8938
8939/*
8940 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8941 * The incompatible newer method is quite a bit better at indenting
8942 * code in lisp-like languages than the traditional one; it's still
8943 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8944 *
8945 * TODO:
8946 * Findmatch() should be adapted for lisp, also to make showmatch
8947 * work correctly: now (v5.3) it seems all C/C++ oriented:
8948 * - it does not recognize the #\( and #\) notations as character literals
8949 * - it doesn't know about comments starting with a semicolon
8950 * - it incorrectly interprets '(' as a character literal
8951 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008952 * Update from Sergey Khorev:
8953 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 */
8955 int
8956get_lisp_indent()
8957{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008958 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 int amount;
8960 char_u *that;
8961 colnr_T col;
8962 colnr_T firsttry;
8963 int parencount, quotecount;
8964 int vi_lisp;
8965
8966 /* Set vi_lisp to use the vi-compatible method */
8967 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8968
8969 realpos = curwin->w_cursor;
8970 curwin->w_cursor.col = 0;
8971
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008972 if ((pos = findmatch(NULL, '(')) == NULL)
8973 pos = findmatch(NULL, '[');
8974 else
8975 {
8976 paren = *pos;
8977 pos = findmatch(NULL, '[');
8978 if (pos == NULL || ltp(pos, &paren))
8979 pos = &paren;
8980 }
8981 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 {
8983 /* Extra trick: Take the indent of the first previous non-white
8984 * line that is at the same () level. */
8985 amount = -1;
8986 parencount = 0;
8987
8988 while (--curwin->w_cursor.lnum >= pos->lnum)
8989 {
8990 if (linewhite(curwin->w_cursor.lnum))
8991 continue;
8992 for (that = ml_get_curline(); *that != NUL; ++that)
8993 {
8994 if (*that == ';')
8995 {
8996 while (*(that + 1) != NUL)
8997 ++that;
8998 continue;
8999 }
9000 if (*that == '\\')
9001 {
9002 if (*(that + 1) != NUL)
9003 ++that;
9004 continue;
9005 }
9006 if (*that == '"' && *(that + 1) != NUL)
9007 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009008 while (*++that && *that != '"')
9009 {
9010 /* skipping escaped characters in the string */
9011 if (*that == '\\')
9012 {
9013 if (*++that == NUL)
9014 break;
9015 if (that[1] == NUL)
9016 {
9017 ++that;
9018 break;
9019 }
9020 }
9021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009023 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009025 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 --parencount;
9027 }
9028 if (parencount == 0)
9029 {
9030 amount = get_indent();
9031 break;
9032 }
9033 }
9034
9035 if (amount == -1)
9036 {
9037 curwin->w_cursor.lnum = pos->lnum;
9038 curwin->w_cursor.col = pos->col;
9039 col = pos->col;
9040
9041 that = ml_get_curline();
9042
9043 if (vi_lisp && get_indent() == 0)
9044 amount = 2;
9045 else
9046 {
9047 amount = 0;
9048 while (*that && col)
9049 {
9050 amount += lbr_chartabsize_adv(&that, (colnr_T)amount);
9051 col--;
9052 }
9053
9054 /*
9055 * Some keywords require "body" indenting rules (the
9056 * non-standard-lisp ones are Scheme special forms):
9057 *
9058 * (let ((a 1)) instead (let ((a 1))
9059 * (...)) of (...))
9060 */
9061
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009062 if (!vi_lisp && (*that == '(' || *that == '[')
9063 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 amount += 2;
9065 else
9066 {
9067 that++;
9068 amount++;
9069 firsttry = amount;
9070
9071 while (vim_iswhite(*that))
9072 {
9073 amount += lbr_chartabsize(that, (colnr_T)amount);
9074 ++that;
9075 }
9076
9077 if (*that && *that != ';') /* not a comment line */
9078 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009079 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009081 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 firsttry++;
9083
9084 parencount = 0;
9085 quotecount = 0;
9086
9087 if (vi_lisp
9088 || (*that != '"'
9089 && *that != '\''
9090 && *that != '#'
9091 && (*that < '0' || *that > '9')))
9092 {
9093 while (*that
9094 && (!vim_iswhite(*that)
9095 || quotecount
9096 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009097 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 && !quotecount
9099 && !parencount
9100 && vi_lisp)))
9101 {
9102 if (*that == '"')
9103 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009104 if ((*that == '(' || *that == '[')
9105 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009107 if ((*that == ')' || *that == ']')
9108 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 --parencount;
9110 if (*that == '\\' && *(that+1) != NUL)
9111 amount += lbr_chartabsize_adv(&that,
9112 (colnr_T)amount);
9113 amount += lbr_chartabsize_adv(&that,
9114 (colnr_T)amount);
9115 }
9116 }
9117 while (vim_iswhite(*that))
9118 {
9119 amount += lbr_chartabsize(that, (colnr_T)amount);
9120 that++;
9121 }
9122 if (!*that || *that == ';')
9123 amount = firsttry;
9124 }
9125 }
9126 }
9127 }
9128 }
9129 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009130 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131
9132 curwin->w_cursor = realpos;
9133
9134 return amount;
9135}
9136#endif /* FEAT_LISP */
9137
9138 void
9139prepare_to_exit()
9140{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009141#if defined(SIGHUP) && defined(SIG_IGN)
9142 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9143 * makes Vim exit and then handling SIGHUP causes various reentrance
9144 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009145 signal(SIGHUP, SIG_IGN);
9146#endif
9147
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148#ifdef FEAT_GUI
9149 if (gui.in_use)
9150 {
9151 gui.dying = TRUE;
9152 out_trash(); /* trash any pending output */
9153 }
9154 else
9155#endif
9156 {
9157 windgoto((int)Rows - 1, 0);
9158
9159 /*
9160 * Switch terminal mode back now, so messages end up on the "normal"
9161 * screen (if there are two screens).
9162 */
9163 settmode(TMODE_COOK);
9164#ifdef WIN3264
9165 if (can_end_termcap_mode(FALSE) == TRUE)
9166#endif
9167 stoptermcap();
9168 out_flush();
9169 }
9170}
9171
9172/*
9173 * Preserve files and exit.
9174 * When called IObuff must contain a message.
9175 */
9176 void
9177preserve_exit()
9178{
9179 buf_T *buf;
9180
9181 prepare_to_exit();
9182
Bram Moolenaar4770d092006-01-12 23:22:24 +00009183 /* Setting this will prevent free() calls. That avoids calling free()
9184 * recursively when free() was invoked with a bad pointer. */
9185 really_exiting = TRUE;
9186
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 out_str(IObuff);
9188 screen_start(); /* don't know where cursor is now */
9189 out_flush();
9190
9191 ml_close_notmod(); /* close all not-modified buffers */
9192
9193 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9194 {
9195 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9196 {
9197 OUT_STR(_("Vim: preserving files...\n"));
9198 screen_start(); /* don't know where cursor is now */
9199 out_flush();
9200 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9201 break;
9202 }
9203 }
9204
9205 ml_close_all(FALSE); /* close all memfiles, without deleting */
9206
9207 OUT_STR(_("Vim: Finished.\n"));
9208
9209 getout(1);
9210}
9211
9212/*
9213 * return TRUE if "fname" exists.
9214 */
9215 int
9216vim_fexists(fname)
9217 char_u *fname;
9218{
9219 struct stat st;
9220
9221 if (mch_stat((char *)fname, &st))
9222 return FALSE;
9223 return TRUE;
9224}
9225
9226/*
9227 * Check for CTRL-C pressed, but only once in a while.
9228 * Should be used instead of ui_breakcheck() for functions that check for
9229 * each line in the file. Calling ui_breakcheck() each time takes too much
9230 * time, because it can be a system call.
9231 */
9232
9233#ifndef BREAKCHECK_SKIP
9234# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9235# define BREAKCHECK_SKIP 200
9236# else
9237# define BREAKCHECK_SKIP 32
9238# endif
9239#endif
9240
9241static int breakcheck_count = 0;
9242
9243 void
9244line_breakcheck()
9245{
9246 if (++breakcheck_count >= BREAKCHECK_SKIP)
9247 {
9248 breakcheck_count = 0;
9249 ui_breakcheck();
9250 }
9251}
9252
9253/*
9254 * Like line_breakcheck() but check 10 times less often.
9255 */
9256 void
9257fast_breakcheck()
9258{
9259 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9260 {
9261 breakcheck_count = 0;
9262 ui_breakcheck();
9263 }
9264}
9265
9266/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009267 * Invoke expand_wildcards() for one pattern.
9268 * Expand items like "%:h" before the expansion.
9269 * Returns OK or FAIL.
9270 */
9271 int
9272expand_wildcards_eval(pat, num_file, file, flags)
9273 char_u **pat; /* pointer to input pattern */
9274 int *num_file; /* resulting number of files */
9275 char_u ***file; /* array of resulting files */
9276 int flags; /* EW_DIR, etc. */
9277{
9278 int ret = FAIL;
9279 char_u *eval_pat = NULL;
9280 char_u *exp_pat = *pat;
9281 char_u *ignored_msg;
9282 int usedlen;
9283
9284 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9285 {
9286 ++emsg_off;
9287 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9288 NULL, &ignored_msg, NULL);
9289 --emsg_off;
9290 if (eval_pat != NULL)
9291 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9292 }
9293
9294 if (exp_pat != NULL)
9295 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9296
9297 if (eval_pat != NULL)
9298 {
9299 vim_free(exp_pat);
9300 vim_free(eval_pat);
9301 }
9302
9303 return ret;
9304}
9305
9306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9308 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009309 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 */
9311 int
9312expand_wildcards(num_pat, pat, num_file, file, flags)
9313 int num_pat; /* number of input patterns */
9314 char_u **pat; /* array of input patterns */
9315 int *num_file; /* resulting number of files */
9316 char_u ***file; /* array of resulting files */
9317 int flags; /* EW_DIR, etc. */
9318{
9319 int retval;
9320 int i, j;
9321 char_u *p;
9322 int non_suf_match; /* number without matching suffix */
9323
9324 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9325
9326 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009327 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 return retval;
9329
9330#ifdef FEAT_WILDIGN
9331 /*
9332 * Remove names that match 'wildignore'.
9333 */
9334 if (*p_wig)
9335 {
9336 char_u *ffname;
9337
9338 /* check all files in (*file)[] */
9339 for (i = 0; i < *num_file; ++i)
9340 {
9341 ffname = FullName_save((*file)[i], FALSE);
9342 if (ffname == NULL) /* out of memory */
9343 break;
9344# ifdef VMS
9345 vms_remove_version(ffname);
9346# endif
9347 if (match_file_list(p_wig, (*file)[i], ffname))
9348 {
9349 /* remove this matching file from the list */
9350 vim_free((*file)[i]);
9351 for (j = i; j + 1 < *num_file; ++j)
9352 (*file)[j] = (*file)[j + 1];
9353 --*num_file;
9354 --i;
9355 }
9356 vim_free(ffname);
9357 }
9358 }
9359#endif
9360
9361 /*
9362 * Move the names where 'suffixes' match to the end.
9363 */
9364 if (*num_file > 1)
9365 {
9366 non_suf_match = 0;
9367 for (i = 0; i < *num_file; ++i)
9368 {
9369 if (!match_suffix((*file)[i]))
9370 {
9371 /*
9372 * Move the name without matching suffix to the front
9373 * of the list.
9374 */
9375 p = (*file)[i];
9376 for (j = i; j > non_suf_match; --j)
9377 (*file)[j] = (*file)[j - 1];
9378 (*file)[non_suf_match++] = p;
9379 }
9380 }
9381 }
9382
9383 return retval;
9384}
9385
9386/*
9387 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9388 */
9389 int
9390match_suffix(fname)
9391 char_u *fname;
9392{
9393 int fnamelen, setsuflen;
9394 char_u *setsuf;
9395#define MAXSUFLEN 30 /* maximum length of a file suffix */
9396 char_u suf_buf[MAXSUFLEN];
9397
9398 fnamelen = (int)STRLEN(fname);
9399 setsuflen = 0;
9400 for (setsuf = p_su; *setsuf; )
9401 {
9402 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009403 if (setsuflen == 0)
9404 {
9405 char_u *tail = gettail(fname);
9406
9407 /* empty entry: match name without a '.' */
9408 if (vim_strchr(tail, '.') == NULL)
9409 {
9410 setsuflen = 1;
9411 break;
9412 }
9413 }
9414 else
9415 {
9416 if (fnamelen >= setsuflen
9417 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9418 (size_t)setsuflen) == 0)
9419 break;
9420 setsuflen = 0;
9421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 }
9423 return (setsuflen != 0);
9424}
9425
9426#if !defined(NO_EXPANDPATH) || defined(PROTO)
9427
9428# ifdef VIM_BACKTICK
9429static int vim_backtick __ARGS((char_u *p));
9430static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9431# endif
9432
9433# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9434/*
9435 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9436 * it's shared between these systems.
9437 */
9438# if defined(DJGPP) || defined(PROTO)
9439# define _cdecl /* DJGPP doesn't have this */
9440# else
9441# ifdef __BORLANDC__
9442# define _cdecl _RTLENTRYF
9443# endif
9444# endif
9445
9446/*
9447 * comparison function for qsort in dos_expandpath()
9448 */
9449 static int _cdecl
9450pstrcmp(const void *a, const void *b)
9451{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009452 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453}
9454
9455# ifndef WIN3264
9456 static void
9457namelowcpy(
9458 char_u *d,
9459 char_u *s)
9460{
9461# ifdef DJGPP
9462 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9463 while (*s)
9464 *d++ = *s++;
9465 else
9466# endif
9467 while (*s)
9468 *d++ = TOLOWER_LOC(*s++);
9469 *d = NUL;
9470}
9471# endif
9472
9473/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009474 * Recursively expand one path component into all matching files and/or
9475 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 * Return the number of matches found.
9477 * "path" has backslashes before chars that are not to be expanded, starting
9478 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009479 * Return the number of matches found.
9480 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 */
9482 static int
9483dos_expandpath(
9484 garray_T *gap,
9485 char_u *path,
9486 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009487 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009488 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009490 char_u *buf;
9491 char_u *path_end;
9492 char_u *p, *s, *e;
9493 int start_len = gap->ga_len;
9494 char_u *pat;
9495 regmatch_T regmatch;
9496 int starts_with_dot;
9497 int matches;
9498 int len;
9499 int starstar = FALSE;
9500 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#ifdef WIN3264
9502 WIN32_FIND_DATA fb;
9503 HANDLE hFind = (HANDLE)0;
9504# ifdef FEAT_MBYTE
9505 WIN32_FIND_DATAW wfb;
9506 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9507# endif
9508#else
9509 struct ffblk fb;
9510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009512 int ok;
9513
9514 /* Expanding "**" may take a long time, check for CTRL-C. */
9515 if (stardepth > 0)
9516 {
9517 ui_breakcheck();
9518 if (got_int)
9519 return 0;
9520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521
9522 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009523 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 if (buf == NULL)
9525 return 0;
9526
9527 /*
9528 * Find the first part in the path name that contains a wildcard or a ~1.
9529 * Copy it into buf, including the preceding characters.
9530 */
9531 p = buf;
9532 s = buf;
9533 e = NULL;
9534 path_end = path;
9535 while (*path_end != NUL)
9536 {
9537 /* May ignore a wildcard that has a backslash before it; it will
9538 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9539 if (path_end >= path + wildoff && rem_backslash(path_end))
9540 *p++ = *path_end++;
9541 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9542 {
9543 if (e != NULL)
9544 break;
9545 s = p + 1;
9546 }
9547 else if (path_end >= path + wildoff
9548 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9549 e = p;
9550#ifdef FEAT_MBYTE
9551 if (has_mbyte)
9552 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009553 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 STRNCPY(p, path_end, len);
9555 p += len;
9556 path_end += len;
9557 }
9558 else
9559#endif
9560 *p++ = *path_end++;
9561 }
9562 e = p;
9563 *e = NUL;
9564
9565 /* now we have one wildcard component between s and e */
9566 /* Remove backslashes between "wildoff" and the start of the wildcard
9567 * component. */
9568 for (p = buf + wildoff; p < s; ++p)
9569 if (rem_backslash(p))
9570 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009571 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 --e;
9573 --s;
9574 }
9575
Bram Moolenaar231334e2005-07-25 20:46:57 +00009576 /* Check for "**" between "s" and "e". */
9577 for (p = s; p < e; ++p)
9578 if (p[0] == '*' && p[1] == '*')
9579 starstar = TRUE;
9580
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 starts_with_dot = (*s == '.');
9582 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9583 if (pat == NULL)
9584 {
9585 vim_free(buf);
9586 return 0;
9587 }
9588
9589 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009590 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009591 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 regmatch.rm_ic = TRUE; /* Always ignore case */
9593 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009594 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009595 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 vim_free(pat);
9597
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009598 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 {
9600 vim_free(buf);
9601 return 0;
9602 }
9603
9604 /* remember the pattern or file name being looked for */
9605 matchname = vim_strsave(s);
9606
Bram Moolenaar231334e2005-07-25 20:46:57 +00009607 /* If "**" is by itself, this is the first time we encounter it and more
9608 * is following then find matches without any directory. */
9609 if (!didstar && stardepth < 100 && starstar && e - s == 2
9610 && *path_end == '/')
9611 {
9612 STRCPY(s, path_end + 1);
9613 ++stardepth;
9614 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9615 --stardepth;
9616 }
9617
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 /* Scan all files in the directory with "dir/ *.*" */
9619 STRCPY(s, "*.*");
9620#ifdef WIN3264
9621# ifdef FEAT_MBYTE
9622 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9623 {
9624 /* The active codepage differs from 'encoding'. Attempt using the
9625 * wide function. If it fails because it is not implemented fall back
9626 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009627 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 if (wn != NULL)
9629 {
9630 hFind = FindFirstFileW(wn, &wfb);
9631 if (hFind == INVALID_HANDLE_VALUE
9632 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9633 {
9634 vim_free(wn);
9635 wn = NULL;
9636 }
9637 }
9638 }
9639
9640 if (wn == NULL)
9641# endif
9642 hFind = FindFirstFile(buf, &fb);
9643 ok = (hFind != INVALID_HANDLE_VALUE);
9644#else
9645 /* If we are expanding wildcards we try both files and directories */
9646 ok = (findfirst((char *)buf, &fb,
9647 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9648#endif
9649
9650 while (ok)
9651 {
9652#ifdef WIN3264
9653# ifdef FEAT_MBYTE
9654 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009655 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 else
9657# endif
9658 p = (char_u *)fb.cFileName;
9659#else
9660 p = (char_u *)fb.ff_name;
9661#endif
9662 /* Ignore entries starting with a dot, unless when asked for. Accept
9663 * all entries found with "matchname". */
9664 if ((p[0] != '.' || starts_with_dot)
9665 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009666 || (regmatch.regprog != NULL
9667 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009668 || ((flags & EW_NOTWILD)
9669 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 {
9671#ifdef WIN3264
9672 STRCPY(s, p);
9673#else
9674 namelowcpy(s, p);
9675#endif
9676 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009677
9678 if (starstar && stardepth < 100)
9679 {
9680 /* For "**" in the pattern first go deeper in the tree to
9681 * find matches. */
9682 STRCPY(buf + len, "/**");
9683 STRCPY(buf + len + 3, path_end);
9684 ++stardepth;
9685 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9686 --stardepth;
9687 }
9688
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 STRCPY(buf + len, path_end);
9690 if (mch_has_exp_wildcard(path_end))
9691 {
9692 /* need to expand another component of the path */
9693 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009694 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 }
9696 else
9697 {
9698 /* no more wildcards, check if there is a match */
9699 /* remove backslashes for the remaining components only */
9700 if (*path_end != 0)
9701 backslash_halve(buf + len + 1);
9702 if (mch_getperm(buf) >= 0) /* add existing file */
9703 addfile(gap, buf, flags);
9704 }
9705 }
9706
9707#ifdef WIN3264
9708# ifdef FEAT_MBYTE
9709 if (wn != NULL)
9710 {
9711 vim_free(p);
9712 ok = FindNextFileW(hFind, &wfb);
9713 }
9714 else
9715# endif
9716 ok = FindNextFile(hFind, &fb);
9717#else
9718 ok = (findnext(&fb) == 0);
9719#endif
9720
9721 /* If no more matches and no match was used, try expanding the name
9722 * itself. Finds the long name of a short filename. */
9723 if (!ok && matchname != NULL && gap->ga_len == start_len)
9724 {
9725 STRCPY(s, matchname);
9726#ifdef WIN3264
9727 FindClose(hFind);
9728# ifdef FEAT_MBYTE
9729 if (wn != NULL)
9730 {
9731 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009732 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (wn != NULL)
9734 hFind = FindFirstFileW(wn, &wfb);
9735 }
9736 if (wn == NULL)
9737# endif
9738 hFind = FindFirstFile(buf, &fb);
9739 ok = (hFind != INVALID_HANDLE_VALUE);
9740#else
9741 ok = (findfirst((char *)buf, &fb,
9742 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9743#endif
9744 vim_free(matchname);
9745 matchname = NULL;
9746 }
9747 }
9748
9749#ifdef WIN3264
9750 FindClose(hFind);
9751# ifdef FEAT_MBYTE
9752 vim_free(wn);
9753# endif
9754#endif
9755 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009756 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 vim_free(matchname);
9758
9759 matches = gap->ga_len - start_len;
9760 if (matches > 0)
9761 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9762 sizeof(char_u *), pstrcmp);
9763 return matches;
9764}
9765
9766 int
9767mch_expandpath(
9768 garray_T *gap,
9769 char_u *path,
9770 int flags) /* EW_* flags */
9771{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009772 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773}
9774# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9775
Bram Moolenaar231334e2005-07-25 20:46:57 +00009776#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9777 || defined(PROTO)
9778/*
9779 * Unix style wildcard expansion code.
9780 * It's here because it's used both for Unix and Mac.
9781 */
9782static int pstrcmp __ARGS((const void *, const void *));
9783
9784 static int
9785pstrcmp(a, b)
9786 const void *a, *b;
9787{
9788 return (pathcmp(*(char **)a, *(char **)b, -1));
9789}
9790
9791/*
9792 * Recursively expand one path component into all matching files and/or
9793 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9794 * "path" has backslashes before chars that are not to be expanded, starting
9795 * at "path + wildoff".
9796 * Return the number of matches found.
9797 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9798 */
9799 int
9800unix_expandpath(gap, path, wildoff, flags, didstar)
9801 garray_T *gap;
9802 char_u *path;
9803 int wildoff;
9804 int flags; /* EW_* flags */
9805 int didstar; /* expanded "**" once already */
9806{
9807 char_u *buf;
9808 char_u *path_end;
9809 char_u *p, *s, *e;
9810 int start_len = gap->ga_len;
9811 char_u *pat;
9812 regmatch_T regmatch;
9813 int starts_with_dot;
9814 int matches;
9815 int len;
9816 int starstar = FALSE;
9817 static int stardepth = 0; /* depth for "**" expansion */
9818
9819 DIR *dirp;
9820 struct dirent *dp;
9821
9822 /* Expanding "**" may take a long time, check for CTRL-C. */
9823 if (stardepth > 0)
9824 {
9825 ui_breakcheck();
9826 if (got_int)
9827 return 0;
9828 }
9829
9830 /* make room for file name */
9831 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9832 if (buf == NULL)
9833 return 0;
9834
9835 /*
9836 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009837 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009838 * Copy it into "buf", including the preceding characters.
9839 */
9840 p = buf;
9841 s = buf;
9842 e = NULL;
9843 path_end = path;
9844 while (*path_end != NUL)
9845 {
9846 /* May ignore a wildcard that has a backslash before it; it will
9847 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9848 if (path_end >= path + wildoff && rem_backslash(path_end))
9849 *p++ = *path_end++;
9850 else if (*path_end == '/')
9851 {
9852 if (e != NULL)
9853 break;
9854 s = p + 1;
9855 }
9856 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009857 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009858 || (!p_fic && (flags & EW_ICASE)
9859 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009860 e = p;
9861#ifdef FEAT_MBYTE
9862 if (has_mbyte)
9863 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009864 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009865 STRNCPY(p, path_end, len);
9866 p += len;
9867 path_end += len;
9868 }
9869 else
9870#endif
9871 *p++ = *path_end++;
9872 }
9873 e = p;
9874 *e = NUL;
9875
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009876 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009877 /* Remove backslashes between "wildoff" and the start of the wildcard
9878 * component. */
9879 for (p = buf + wildoff; p < s; ++p)
9880 if (rem_backslash(p))
9881 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009882 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009883 --e;
9884 --s;
9885 }
9886
9887 /* Check for "**" between "s" and "e". */
9888 for (p = s; p < e; ++p)
9889 if (p[0] == '*' && p[1] == '*')
9890 starstar = TRUE;
9891
9892 /* convert the file pattern to a regexp pattern */
9893 starts_with_dot = (*s == '.');
9894 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9895 if (pat == NULL)
9896 {
9897 vim_free(buf);
9898 return 0;
9899 }
9900
9901 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009902 if (flags & EW_ICASE)
9903 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9904 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009905 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009906 if (flags & (EW_NOERROR | EW_NOTWILD))
9907 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009908 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009909 if (flags & (EW_NOERROR | EW_NOTWILD))
9910 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009911 vim_free(pat);
9912
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009913 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009914 {
9915 vim_free(buf);
9916 return 0;
9917 }
9918
9919 /* If "**" is by itself, this is the first time we encounter it and more
9920 * is following then find matches without any directory. */
9921 if (!didstar && stardepth < 100 && starstar && e - s == 2
9922 && *path_end == '/')
9923 {
9924 STRCPY(s, path_end + 1);
9925 ++stardepth;
9926 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9927 --stardepth;
9928 }
9929
9930 /* open the directory for scanning */
9931 *s = NUL;
9932 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9933
9934 /* Find all matching entries */
9935 if (dirp != NULL)
9936 {
9937 for (;;)
9938 {
9939 dp = readdir(dirp);
9940 if (dp == NULL)
9941 break;
9942 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009943 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9944 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009945 || ((flags & EW_NOTWILD)
9946 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009947 {
9948 STRCPY(s, dp->d_name);
9949 len = STRLEN(buf);
9950
9951 if (starstar && stardepth < 100)
9952 {
9953 /* For "**" in the pattern first go deeper in the tree to
9954 * find matches. */
9955 STRCPY(buf + len, "/**");
9956 STRCPY(buf + len + 3, path_end);
9957 ++stardepth;
9958 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9959 --stardepth;
9960 }
9961
9962 STRCPY(buf + len, path_end);
9963 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9964 {
9965 /* need to expand another component of the path */
9966 /* remove backslashes for the remaining components only */
9967 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9968 }
9969 else
9970 {
9971 /* no more wildcards, check if there is a match */
9972 /* remove backslashes for the remaining components only */
9973 if (*path_end != NUL)
9974 backslash_halve(buf + len + 1);
9975 if (mch_getperm(buf) >= 0) /* add existing file */
9976 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009977#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00009978 size_t precomp_len = STRLEN(buf)+1;
9979 char_u *precomp_buf =
9980 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009981
Bram Moolenaar231334e2005-07-25 20:46:57 +00009982 if (precomp_buf)
9983 {
9984 mch_memmove(buf, precomp_buf, precomp_len);
9985 vim_free(precomp_buf);
9986 }
9987#endif
9988 addfile(gap, buf, flags);
9989 }
9990 }
9991 }
9992 }
9993
9994 closedir(dirp);
9995 }
9996
9997 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009998 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009999
10000 matches = gap->ga_len - start_len;
10001 if (matches > 0)
10002 qsort(((char_u **)gap->ga_data) + start_len, matches,
10003 sizeof(char_u *), pstrcmp);
10004 return matches;
10005}
10006#endif
10007
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010008#if defined(FEAT_SEARCHPATH)
10009static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10010static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010011static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10012static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010013static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10014static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10015
10016/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010017 * Moves "*psep" back to the previous path separator in "path".
10018 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010019 */
10020 static int
10021find_previous_pathsep(path, psep)
10022 char_u *path;
10023 char_u **psep;
10024{
10025 /* skip the current separator */
10026 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010027 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010028
10029 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010030 while (*psep > path)
10031 {
10032 if (vim_ispathsep(**psep))
10033 return OK;
10034 mb_ptr_back(path, *psep);
10035 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010036
10037 return FAIL;
10038}
10039
10040/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010041 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10042 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010043 */
10044 static int
10045is_unique(maybe_unique, gap, i)
10046 char_u *maybe_unique;
10047 garray_T *gap;
10048 int i;
10049{
10050 int j;
10051 int candidate_len;
10052 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010053 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010054 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010055
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010056 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010057 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010058 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010059 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010060
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010061 candidate_len = (int)STRLEN(maybe_unique);
10062 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010063 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010064 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010065
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010066 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010067 if (fnamecmp(maybe_unique, rival) == 0
10068 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010069 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010070 }
10071
Bram Moolenaar162bd912010-07-28 22:29:10 +020010072 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010073}
10074
10075/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010076 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010077 * paths are expanded to their equivalent fullpath. This includes the "."
10078 * (relative to current buffer directory) and empty path (relative to current
10079 * directory) notations.
10080 *
10081 * TODO: handle upward search (;) and path limiter (**N) notations by
10082 * expanding each into their equivalent path(s).
10083 */
10084 static void
10085expand_path_option(curdir, gap)
10086 char_u *curdir;
10087 garray_T *gap;
10088{
10089 char_u *path_option = *curbuf->b_p_path == NUL
10090 ? p_path : curbuf->b_p_path;
10091 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010092 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010093 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010094
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010095 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010096 return;
10097
10098 while (*path_option != NUL)
10099 {
10100 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10101
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010102 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010103 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010104 /* Relative to current buffer:
10105 * "/path/file" + "." -> "/path/"
10106 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010107 if (curbuf->b_ffname == NULL)
10108 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010109 p = gettail(curbuf->b_ffname);
10110 len = (int)(p - curbuf->b_ffname);
10111 if (len + (int)STRLEN(buf) >= MAXPATHL)
10112 continue;
10113 if (buf[1] == NUL)
10114 buf[len] = NUL;
10115 else
10116 STRMOVE(buf + len, buf + 2);
10117 mch_memmove(buf, curbuf->b_ffname, len);
10118 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010119 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010120 else if (buf[0] == NUL)
10121 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010122 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010123 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010124 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010125 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010126 else if (!mch_isFullName(buf))
10127 {
10128 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010129 len = (int)STRLEN(curdir);
10130 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010131 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010132 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010133 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010134 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010135 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010136 }
10137
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010138 if (ga_grow(gap, 1) == FAIL)
10139 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010140
10141# if defined(MSWIN) || defined(MSDOS)
10142 /* Avoid the path ending in a backslash, it fails when a comma is
10143 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010144 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010145 if (buf[len - 1] == '\\')
10146 buf[len - 1] = '/';
10147# endif
10148
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010149 p = vim_strsave(buf);
10150 if (p == NULL)
10151 break;
10152 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010153 }
10154
10155 vim_free(buf);
10156}
10157
10158/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010159 * Returns a pointer to the file or directory name in "fname" that matches the
10160 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010161 *
10162 * path: /foo/bar/baz
10163 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010164 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010165 */
10166 static char_u *
10167get_path_cutoff(fname, gap)
10168 char_u *fname;
10169 garray_T *gap;
10170{
10171 int i;
10172 int maxlen = 0;
10173 char_u **path_part = (char_u **)gap->ga_data;
10174 char_u *cutoff = NULL;
10175
10176 for (i = 0; i < gap->ga_len; i++)
10177 {
10178 int j = 0;
10179
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010180 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010181# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010182 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10183#endif
10184 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010185 j++;
10186 if (j > maxlen)
10187 {
10188 maxlen = j;
10189 cutoff = &fname[j];
10190 }
10191 }
10192
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010193 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010194 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010195 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010196 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010197
10198 return cutoff;
10199}
10200
10201/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010202 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10203 * that they are unique with respect to each other while conserving the part
10204 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010205 */
10206 static void
10207uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010208 garray_T *gap;
10209 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010210{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010211 int i;
10212 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010213 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010214 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010215 char_u *pat;
10216 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010217 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010218 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010219 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010220 char_u **in_curdir = NULL;
10221 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010222
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010223 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010224 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010225
10226 /*
10227 * We need to prepend a '*' at the beginning of file_pattern so that the
10228 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010229 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010230 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010231 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010232 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010233 if (file_pattern == NULL)
10234 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010235 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010236 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010237 STRCAT(file_pattern, pattern);
10238 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10239 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010240 if (pat == NULL)
10241 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010242
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010243 regmatch.rm_ic = TRUE; /* always ignore case */
10244 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10245 vim_free(pat);
10246 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010247 return;
10248
Bram Moolenaar162bd912010-07-28 22:29:10 +020010249 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010250 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010251 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010252 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010253
10254 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010255 if (in_curdir == NULL)
10256 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010257
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010258 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010259 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010260 char_u *path = fnames[i];
10261 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010262 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010263 char_u *pathsep_p;
10264 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010265
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010266 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010267 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010268 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010269 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010270 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010271
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010272 /* Shorten the filename while maintaining its uniqueness */
10273 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010274
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010275 /* we start at the end of the path */
10276 pathsep_p = path + len - 1;
10277
10278 while (find_previous_pathsep(path, &pathsep_p))
10279 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10280 && is_unique(pathsep_p + 1, gap, i)
10281 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10282 {
10283 sort_again = TRUE;
10284 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10285 break;
10286 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010287
10288 if (mch_isFullName(path))
10289 {
10290 /*
10291 * Last resort: shorten relative to curdir if possible.
10292 * 'possible' means:
10293 * 1. It is under the current directory.
10294 * 2. The result is actually shorter than the original.
10295 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010296 * Before curdir After
10297 * /foo/bar/file.txt /foo/bar ./file.txt
10298 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10299 * /file.txt / /file.txt
10300 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010301 */
10302 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010303 if (short_name != NULL && short_name > path + 1
10304#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010305 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010306 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010307 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010308 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010309 && !vim_ispathsep(*short_name)
10310#endif
10311 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010312 {
10313 STRCPY(path, ".");
10314 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010315 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010316 }
10317 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010318 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010319 }
10320
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010321 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010322 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010323 {
10324 char_u *rel_path;
10325 char_u *path = in_curdir[i];
10326
10327 if (path == NULL)
10328 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010329
10330 /* If the {filename} is not unique, change it to ./{filename}.
10331 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010332 short_name = shorten_fname(path, curdir);
10333 if (short_name == NULL)
10334 short_name = path;
10335 if (is_unique(short_name, gap, i))
10336 {
10337 STRCPY(fnames[i], short_name);
10338 continue;
10339 }
10340
10341 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10342 if (rel_path == NULL)
10343 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010344 STRCPY(rel_path, ".");
10345 add_pathsep(rel_path);
10346 STRCAT(rel_path, short_name);
10347
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010348 vim_free(fnames[i]);
10349 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010350 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010351 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010352 }
10353
Bram Moolenaar162bd912010-07-28 22:29:10 +020010354theend:
10355 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010356 if (in_curdir != NULL)
10357 {
10358 for (i = 0; i < gap->ga_len; i++)
10359 vim_free(in_curdir[i]);
10360 vim_free(in_curdir);
10361 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010362 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010363 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010364
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010365 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010366 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010367}
10368
10369/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010370 * Calls globpath() with 'path' values for the given pattern and stores the
10371 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010372 * Returns the total number of matches.
10373 */
10374 static int
10375expand_in_path(gap, pattern, flags)
10376 garray_T *gap;
10377 char_u *pattern;
10378 int flags; /* EW_* flags */
10379{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010380 char_u *curdir;
10381 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010382 char_u *files = NULL;
10383 char_u *s; /* start */
10384 char_u *e; /* end */
10385 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010386
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010387 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010388 return 0;
10389 mch_dirname(curdir, MAXPATHL);
10390
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010391 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010392 expand_path_option(curdir, &path_ga);
10393 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010394 if (path_ga.ga_len == 0)
10395 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010396
10397 paths = ga_concat_strings(&path_ga);
10398 ga_clear_strings(&path_ga);
10399 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010400 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010401
Bram Moolenaar94950a92010-12-02 16:01:29 +010010402 files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010403 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010404 if (files == NULL)
10405 return 0;
10406
10407 /* Copy each path in files into gap */
10408 s = e = files;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010409 while (*s != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010410 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010411 while (*e != '\n' && *e != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010412 e++;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010413 if (*e == NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010414 {
10415 addfile(gap, s, flags);
10416 break;
10417 }
10418 else
10419 {
10420 /* *e is '\n' */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010421 *e = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010422 addfile(gap, s, flags);
10423 e++;
10424 s = e;
10425 }
10426 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010427 vim_free(files);
10428
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010429 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010430}
10431#endif
10432
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010433#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10434/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010435 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10436 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010437 */
10438 void
10439remove_duplicates(gap)
10440 garray_T *gap;
10441{
10442 int i;
10443 int j;
10444 char_u **fnames = (char_u **)gap->ga_data;
10445
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010446 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010447 for (i = gap->ga_len - 1; i > 0; --i)
10448 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10449 {
10450 vim_free(fnames[i]);
10451 for (j = i + 1; j < gap->ga_len; ++j)
10452 fnames[j - 1] = fnames[j];
10453 --gap->ga_len;
10454 }
10455}
10456#endif
10457
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458/*
10459 * Generic wildcard expansion code.
10460 *
10461 * Characters in "pat" that should not be expanded must be preceded with a
10462 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10463 *
10464 * Return FAIL when no single file was found. In this case "num_file" is not
10465 * set, and "file" may contain an error message.
10466 * Return OK when some files found. "num_file" is set to the number of
10467 * matches, "file" to the array of matches. Call FreeWild() later.
10468 */
10469 int
10470gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10471 int num_pat; /* number of input patterns */
10472 char_u **pat; /* array of input patterns */
10473 int *num_file; /* resulting number of files */
10474 char_u ***file; /* array of resulting files */
10475 int flags; /* EW_* flags */
10476{
10477 int i;
10478 garray_T ga;
10479 char_u *p;
10480 static int recursive = FALSE;
10481 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010482#if defined(FEAT_SEARCHPATH)
10483 int did_expand_in_path = FALSE;
10484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
10486 /*
10487 * expand_env() is called to expand things like "~user". If this fails,
10488 * it calls ExpandOne(), which brings us back here. In this case, always
10489 * call the machine specific expansion function, if possible. Otherwise,
10490 * return FAIL.
10491 */
10492 if (recursive)
10493#ifdef SPECIAL_WILDCHAR
10494 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10495#else
10496 return FAIL;
10497#endif
10498
10499#ifdef SPECIAL_WILDCHAR
10500 /*
10501 * If there are any special wildcard characters which we cannot handle
10502 * here, call machine specific function for all the expansion. This
10503 * avoids starting the shell for each argument separately.
10504 * For `=expr` do use the internal function.
10505 */
10506 for (i = 0; i < num_pat; i++)
10507 {
10508 if (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL
10509# ifdef VIM_BACKTICK
10510 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10511# endif
10512 )
10513 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10514 }
10515#endif
10516
10517 recursive = TRUE;
10518
10519 /*
10520 * The matching file names are stored in a growarray. Init it empty.
10521 */
10522 ga_init2(&ga, (int)sizeof(char_u *), 30);
10523
10524 for (i = 0; i < num_pat; ++i)
10525 {
10526 add_pat = -1;
10527 p = pat[i];
10528
10529#ifdef VIM_BACKTICK
10530 if (vim_backtick(p))
10531 add_pat = expand_backtick(&ga, p, flags);
10532 else
10533#endif
10534 {
10535 /*
10536 * First expand environment variables, "~/" and "~user/".
10537 */
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010538 if (vim_strchr(p, '$') != NULL || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010540 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 if (p == NULL)
10542 p = pat[i];
10543#ifdef UNIX
10544 /*
10545 * On Unix, if expand_env() can't expand an environment
10546 * variable, use the shell to do that. Discard previously
10547 * found file names and start all over again.
10548 */
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010549 else if (vim_strchr(p, '$') != NULL || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 {
10551 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010552 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10554 flags);
10555 recursive = FALSE;
10556 return i;
10557 }
10558#endif
10559 }
10560
10561 /*
10562 * If there are wildcards: Expand file names and add each match to
10563 * the list. If there is no match, and EW_NOTFOUND is given, add
10564 * the pattern.
10565 * If there are no wildcards: Add the file name if it exists or
10566 * when EW_NOTFOUND is given.
10567 */
10568 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010569 {
10570#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010571 if ((flags & EW_PATH)
10572 && !mch_isFullName(p)
10573 && !(p[0] == '.'
10574 && (vim_ispathsep(p[1])
10575 || (p[1] == '.' && vim_ispathsep(p[2]))))
10576 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010577 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010578 /* :find completion where 'path' is used.
10579 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010580 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010581 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010582 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010583 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010584 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010585 else
10586#endif
10587 add_pat = mch_expandpath(&ga, p, flags);
10588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 }
10590
10591 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10592 {
10593 char_u *t = backslash_halve_save(p);
10594
10595#if defined(MACOS_CLASSIC)
10596 slash_to_colon(t);
10597#endif
10598 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10599 * "vim c:/" work. */
10600 if (flags & EW_NOTFOUND)
10601 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10602 else if (mch_getperm(t) >= 0)
10603 addfile(&ga, t, flags);
10604 vim_free(t);
10605 }
10606
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010607#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010608 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010609 uniquefy_paths(&ga, p);
10610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 if (p != pat[i])
10612 vim_free(p);
10613 }
10614
10615 *num_file = ga.ga_len;
10616 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10617
10618 recursive = FALSE;
10619
10620 return (ga.ga_data != NULL) ? OK : FAIL;
10621}
10622
10623# ifdef VIM_BACKTICK
10624
10625/*
10626 * Return TRUE if we can expand this backtick thing here.
10627 */
10628 static int
10629vim_backtick(p)
10630 char_u *p;
10631{
10632 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10633}
10634
10635/*
10636 * Expand an item in `backticks` by executing it as a command.
10637 * Currently only works when pat[] starts and ends with a `.
10638 * Returns number of file names found.
10639 */
10640 static int
10641expand_backtick(gap, pat, flags)
10642 garray_T *gap;
10643 char_u *pat;
10644 int flags; /* EW_* flags */
10645{
10646 char_u *p;
10647 char_u *cmd;
10648 char_u *buffer;
10649 int cnt = 0;
10650 int i;
10651
10652 /* Create the command: lop off the backticks. */
10653 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10654 if (cmd == NULL)
10655 return 0;
10656
10657#ifdef FEAT_EVAL
10658 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010659 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 else
10661#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010662 buffer = get_cmd_output(cmd, NULL,
10663 (flags & EW_SILENT) ? SHELL_SILENT : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 vim_free(cmd);
10665 if (buffer == NULL)
10666 return 0;
10667
10668 cmd = buffer;
10669 while (*cmd != NUL)
10670 {
10671 cmd = skipwhite(cmd); /* skip over white space */
10672 p = cmd;
10673 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10674 ++p;
10675 /* add an entry if it is not empty */
10676 if (p > cmd)
10677 {
10678 i = *p;
10679 *p = NUL;
10680 addfile(gap, cmd, flags);
10681 *p = i;
10682 ++cnt;
10683 }
10684 cmd = p;
10685 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10686 ++cmd;
10687 }
10688
10689 vim_free(buffer);
10690 return cnt;
10691}
10692# endif /* VIM_BACKTICK */
10693
10694/*
10695 * Add a file to a file list. Accepted flags:
10696 * EW_DIR add directories
10697 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010698 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 * EW_NOTFOUND add even when it doesn't exist
10700 * EW_ADDSLASH add slash after directory name
10701 */
10702 void
10703addfile(gap, f, flags)
10704 garray_T *gap;
10705 char_u *f; /* filename */
10706 int flags;
10707{
10708 char_u *p;
10709 int isdir;
10710
10711 /* if the file/dir doesn't exist, may not add it */
10712 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10713 return;
10714
10715#ifdef FNAME_ILLEGAL
10716 /* if the file/dir contains illegal characters, don't add it */
10717 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10718 return;
10719#endif
10720
10721 isdir = mch_isdir(f);
10722 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10723 return;
10724
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010725 /* If the file isn't executable, may not add it. Do accept directories. */
10726 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f))
10727 return;
10728
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 /* Make room for another item in the file list. */
10730 if (ga_grow(gap, 1) == FAIL)
10731 return;
10732
10733 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10734 if (p == NULL)
10735 return;
10736
10737 STRCPY(p, f);
10738#ifdef BACKSLASH_IN_FILENAME
10739 slash_adjust(p);
10740#endif
10741 /*
10742 * Append a slash or backslash after directory names if none is present.
10743 */
10744#ifndef DONT_ADD_PATHSEP_TO_DIR
10745 if (isdir && (flags & EW_ADDSLASH))
10746 add_pathsep(p);
10747#endif
10748 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749}
10750#endif /* !NO_EXPANDPATH */
10751
10752#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10753
10754#ifndef SEEK_SET
10755# define SEEK_SET 0
10756#endif
10757#ifndef SEEK_END
10758# define SEEK_END 2
10759#endif
10760
10761/*
10762 * Get the stdout of an external command.
10763 * Returns an allocated string, or NULL for error.
10764 */
10765 char_u *
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010766get_cmd_output(cmd, infile, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010768 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 int flags; /* can be SHELL_SILENT */
10770{
10771 char_u *tempname;
10772 char_u *command;
10773 char_u *buffer = NULL;
10774 int len;
10775 int i = 0;
10776 FILE *fd;
10777
10778 if (check_restricted() || check_secure())
10779 return NULL;
10780
10781 /* get a name for the temp file */
10782 if ((tempname = vim_tempname('o')) == NULL)
10783 {
10784 EMSG(_(e_notmp));
10785 return NULL;
10786 }
10787
10788 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010789 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 if (command == NULL)
10791 goto done;
10792
10793 /*
10794 * Call the shell to execute the command (errors are ignored).
10795 * Don't check timestamps here.
10796 */
10797 ++no_check_timestamps;
10798 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10799 --no_check_timestamps;
10800
10801 vim_free(command);
10802
10803 /*
10804 * read the names from the file into memory
10805 */
10806# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010807 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 fd = mch_fopen((char *)tempname, "r");
10809# else
10810 fd = mch_fopen((char *)tempname, READBIN);
10811# endif
10812
10813 if (fd == NULL)
10814 {
10815 EMSG2(_(e_notopen), tempname);
10816 goto done;
10817 }
10818
10819 fseek(fd, 0L, SEEK_END);
10820 len = ftell(fd); /* get size of temp file */
10821 fseek(fd, 0L, SEEK_SET);
10822
10823 buffer = alloc(len + 1);
10824 if (buffer != NULL)
10825 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10826 fclose(fd);
10827 mch_remove(tempname);
10828 if (buffer == NULL)
10829 goto done;
10830#ifdef VMS
10831 len = i; /* VMS doesn't give us what we asked for... */
10832#endif
10833 if (i != len)
10834 {
10835 EMSG2(_(e_notread), tempname);
10836 vim_free(buffer);
10837 buffer = NULL;
10838 }
10839 else
Bram Moolenaar162bd912010-07-28 22:29:10 +020010840 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841
10842done:
10843 vim_free(tempname);
10844 return buffer;
10845}
10846#endif
10847
10848/*
10849 * Free the list of files returned by expand_wildcards() or other expansion
10850 * functions.
10851 */
10852 void
10853FreeWild(count, files)
10854 int count;
10855 char_u **files;
10856{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010857 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 return;
10859#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10860 /*
10861 * Is this still OK for when other functions than expand_wildcards() have
10862 * been used???
10863 */
10864 _fnexplodefree((char **)files);
10865#else
10866 while (count--)
10867 vim_free(files[count]);
10868 vim_free(files);
10869#endif
10870}
10871
10872/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010873 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 * Don't do this when still processing a command or a mapping.
10875 * Don't do this when inside a ":normal" command.
10876 */
10877 int
10878goto_im()
10879{
10880 return (p_im && stuff_empty() && typebuf_typed());
10881}