blob: eb685cdc887b472aee917be3a46fc05b3016d2e9 [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 Moolenaar071d4272004-06-13 20:20:40 +000019static int copy_indent __ARGS((int size, char_u *src));
20
Bram Moolenaar24305862012-08-15 14:05:05 +020021/* All user names (for ~user completion as done by shell). */
22#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23static garray_T ga_users;
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026/*
27 * Count the size (in window cells) of the indent in the current line.
28 */
29 int
30get_indent()
31{
32 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts);
33}
34
35/*
36 * Count the size (in window cells) of the indent in line "lnum".
37 */
38 int
39get_indent_lnum(lnum)
40 linenr_T lnum;
41{
42 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts);
43}
44
45#if defined(FEAT_FOLDING) || defined(PROTO)
46/*
47 * Count the size (in window cells) of the indent in line "lnum" of buffer
48 * "buf".
49 */
50 int
51get_indent_buf(buf, lnum)
52 buf_T *buf;
53 linenr_T lnum;
54{
55 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts);
56}
57#endif
58
59/*
60 * count the size (in window cells) of the indent in line "ptr", with
61 * 'tabstop' at "ts"
62 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000063 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000064get_indent_str(ptr, ts)
65 char_u *ptr;
66 int ts;
67{
68 int count = 0;
69
70 for ( ; *ptr; ++ptr)
71 {
72 if (*ptr == TAB) /* count a tab for what it is worth */
73 count += ts - (count % ts);
74 else if (*ptr == ' ')
75 ++count; /* count a space for one */
76 else
77 break;
78 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000079 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000080}
81
82/*
83 * Set the indent of the current line.
84 * Leaves the cursor on the first non-blank in the line.
85 * Caller must take care of undo.
86 * "flags":
87 * SIN_CHANGED: call changed_bytes() if the line was changed.
88 * SIN_INSERT: insert the indent in front of the line.
89 * SIN_UNDO: save line for undo before changing it.
90 * Returns TRUE if the line was changed.
91 */
92 int
93set_indent(size, flags)
Bram Moolenaar5002c292007-07-24 13:26:15 +000094 int size; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 int flags;
96{
97 char_u *p;
98 char_u *newline;
99 char_u *oldline;
100 char_u *s;
101 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000102 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 int line_len;
104 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000105 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000107 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000108 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000109 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111 /*
112 * First check if there is anything to do and compute the number of
113 * characters needed for the indent.
114 */
115 todo = size;
116 ind_len = 0;
117 p = oldline = ml_get_curline();
118
119 /* Calculate the buffer size for the new indent, and check to see if it
120 * isn't already set */
121
Bram Moolenaar5002c292007-07-24 13:26:15 +0000122 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
123 * 'preserveindent' are set count the number of characters at the
124 * beginning of the line to be copied */
125 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 {
127 /* If 'preserveindent' is set then reuse as much as possible of
128 * the existing indent structure for the new indent */
129 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
130 {
131 ind_done = 0;
132
133 /* count as many characters as we can use */
134 while (todo > 0 && vim_iswhite(*p))
135 {
136 if (*p == TAB)
137 {
138 tab_pad = (int)curbuf->b_p_ts
139 - (ind_done % (int)curbuf->b_p_ts);
140 /* stop if this tab will overshoot the target */
141 if (todo < tab_pad)
142 break;
143 todo -= tab_pad;
144 ++ind_len;
145 ind_done += tab_pad;
146 }
147 else
148 {
149 --todo;
150 ++ind_len;
151 ++ind_done;
152 }
153 ++p;
154 }
155
Bram Moolenaar5002c292007-07-24 13:26:15 +0000156 /* Set initial number of whitespace chars to copy if we are
157 * preserving indent but expandtab is set */
158 if (curbuf->b_p_et)
159 orig_char_len = ind_len;
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 /* Fill to next tabstop with a tab, if possible */
162 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000163 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 {
165 doit = TRUE;
166 todo -= tab_pad;
167 ++ind_len;
168 /* ind_done += tab_pad; */
169 }
170 }
171
172 /* count tabs required for indent */
173 while (todo >= (int)curbuf->b_p_ts)
174 {
175 if (*p != TAB)
176 doit = TRUE;
177 else
178 ++p;
179 todo -= (int)curbuf->b_p_ts;
180 ++ind_len;
181 /* ind_done += (int)curbuf->b_p_ts; */
182 }
183 }
184 /* count spaces required for indent */
185 while (todo > 0)
186 {
187 if (*p != ' ')
188 doit = TRUE;
189 else
190 ++p;
191 --todo;
192 ++ind_len;
193 /* ++ind_done; */
194 }
195
196 /* Return if the indent is OK already. */
197 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
198 return FALSE;
199
200 /* Allocate memory for the new line. */
201 if (flags & SIN_INSERT)
202 p = oldline;
203 else
204 p = skipwhite(p);
205 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000206
207 /* If 'preserveindent' and 'expandtab' are both set keep the original
208 * characters and allocate accordingly. We will fill the rest with spaces
209 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000210 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000211 {
212 newline = alloc(orig_char_len + size - ind_done + line_len);
213 if (newline == NULL)
214 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000215 todo = size - ind_done;
216 ind_len = orig_char_len + todo; /* Set total length of indent in
217 * characters, which may have been
218 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000219 p = oldline;
220 s = newline;
221 while (orig_char_len > 0)
222 {
223 *s++ = *p++;
224 orig_char_len--;
225 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000226
Bram Moolenaar5002c292007-07-24 13:26:15 +0000227 /* Skip over any additional white space (useful when newindent is less
228 * than old) */
229 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000230 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000231
Bram Moolenaar5002c292007-07-24 13:26:15 +0000232 }
233 else
234 {
235 todo = size;
236 newline = alloc(ind_len + line_len);
237 if (newline == NULL)
238 return FALSE;
239 s = newline;
240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
242 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 /* if 'expandtab' isn't set: use TABs */
244 if (!curbuf->b_p_et)
245 {
246 /* If 'preserveindent' is set then reuse as much as possible of
247 * the existing indent structure for the new indent */
248 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
249 {
250 p = oldline;
251 ind_done = 0;
252
253 while (todo > 0 && vim_iswhite(*p))
254 {
255 if (*p == TAB)
256 {
257 tab_pad = (int)curbuf->b_p_ts
258 - (ind_done % (int)curbuf->b_p_ts);
259 /* stop if this tab will overshoot the target */
260 if (todo < tab_pad)
261 break;
262 todo -= tab_pad;
263 ind_done += tab_pad;
264 }
265 else
266 {
267 --todo;
268 ++ind_done;
269 }
270 *s++ = *p++;
271 }
272
273 /* Fill to next tabstop with a tab, if possible */
274 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
275 if (todo >= tab_pad)
276 {
277 *s++ = TAB;
278 todo -= tab_pad;
279 }
280
281 p = skipwhite(p);
282 }
283
284 while (todo >= (int)curbuf->b_p_ts)
285 {
286 *s++ = TAB;
287 todo -= (int)curbuf->b_p_ts;
288 }
289 }
290 while (todo > 0)
291 {
292 *s++ = ' ';
293 --todo;
294 }
295 mch_memmove(s, p, (size_t)line_len);
296
297 /* Replace the line (unless undo fails). */
298 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
299 {
300 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
301 if (flags & SIN_CHANGED)
302 changed_bytes(curwin->w_cursor.lnum, 0);
303 /* Correct saved cursor position if it's after the indent. */
304 if (saved_cursor.lnum == curwin->w_cursor.lnum
305 && saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000306 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
Bram Moolenaar5409c052005-03-18 20:27:04 +0000307 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 }
309 else
310 vim_free(newline);
311
312 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000313 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314}
315
316/*
317 * Copy the indent from ptr to the current line (and fill to size)
318 * Leaves the cursor on the first non-blank in the line.
319 * Returns TRUE if the line was changed.
320 */
321 static int
322copy_indent(size, src)
323 int size;
324 char_u *src;
325{
326 char_u *p = NULL;
327 char_u *line = NULL;
328 char_u *s;
329 int todo;
330 int ind_len;
331 int line_len = 0;
332 int tab_pad;
333 int ind_done;
334 int round;
335
336 /* Round 1: compute the number of characters needed for the indent
337 * Round 2: copy the characters. */
338 for (round = 1; round <= 2; ++round)
339 {
340 todo = size;
341 ind_len = 0;
342 ind_done = 0;
343 s = src;
344
345 /* Count/copy the usable portion of the source line */
346 while (todo > 0 && vim_iswhite(*s))
347 {
348 if (*s == TAB)
349 {
350 tab_pad = (int)curbuf->b_p_ts
351 - (ind_done % (int)curbuf->b_p_ts);
352 /* Stop if this tab will overshoot the target */
353 if (todo < tab_pad)
354 break;
355 todo -= tab_pad;
356 ind_done += tab_pad;
357 }
358 else
359 {
360 --todo;
361 ++ind_done;
362 }
363 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000364 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 *p++ = *s;
366 ++s;
367 }
368
369 /* Fill to next tabstop with a tab, if possible */
370 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200371 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 {
373 todo -= tab_pad;
374 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000375 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 *p++ = TAB;
377 }
378
379 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200380 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 {
382 todo -= (int)curbuf->b_p_ts;
383 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000384 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 *p++ = TAB;
386 }
387
388 /* Count/add spaces required for indent */
389 while (todo > 0)
390 {
391 --todo;
392 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000393 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 *p++ = ' ';
395 }
396
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000397 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {
399 /* Allocate memory for the result: the copied indent, new indent
400 * and the rest of the line. */
401 line_len = (int)STRLEN(ml_get_curline()) + 1;
402 line = alloc(ind_len + line_len);
403 if (line == NULL)
404 return FALSE;
405 p = line;
406 }
407 }
408
409 /* Append the original line */
410 mch_memmove(p, ml_get_curline(), (size_t)line_len);
411
412 /* Replace the line */
413 ml_replace(curwin->w_cursor.lnum, line, FALSE);
414
415 /* Put the cursor after the indent. */
416 curwin->w_cursor.col = ind_len;
417 return TRUE;
418}
419
420/*
421 * Return the indent of the current line after a number. Return -1 if no
422 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000423 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 */
425 int
426get_number_indent(lnum)
427 linenr_T lnum;
428{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 colnr_T col;
430 pos_T pos;
431
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200432 regmatch_T regmatch;
433 int lead_len = 0; /* length of comment leader */
434
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 if (lnum > curbuf->b_ml.ml_line_count)
436 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000437 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200438
439#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200440 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
441 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200442 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000443#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200444 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
445 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200446 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200447 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200448
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200449 /* vim_regexec() expects a pointer to a line. This lets us
450 * start matching for the flp beyond any comment leader... */
451 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200452 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200453 pos.lnum = lnum;
454 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200456 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200457#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200458 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200459 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200460 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000461
462 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 getvcol(curwin, &pos, &col, NULL, NULL);
465 return (int)col;
466}
467
468#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
469
470static int cin_is_cinword __ARGS((char_u *line));
471
472/*
473 * Return TRUE if the string "line" starts with a word from 'cinwords'.
474 */
475 static int
476cin_is_cinword(line)
477 char_u *line;
478{
479 char_u *cinw;
480 char_u *cinw_buf;
481 int cinw_len;
482 int retval = FALSE;
483 int len;
484
485 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
486 cinw_buf = alloc((unsigned)cinw_len);
487 if (cinw_buf != NULL)
488 {
489 line = skipwhite(line);
490 for (cinw = curbuf->b_p_cinw; *cinw; )
491 {
492 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
493 if (STRNCMP(line, cinw_buf, len) == 0
494 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
495 {
496 retval = TRUE;
497 break;
498 }
499 }
500 vim_free(cinw_buf);
501 }
502 return retval;
503}
504#endif
505
506/*
507 * open_line: Add a new line below or above the current line.
508 *
509 * For VREPLACE mode, we only add a new line when we get to the end of the
510 * file, otherwise we just start replacing the next line.
511 *
512 * Caller must take care of undo. Since VREPLACE may affect any number of
513 * lines however, it may call u_save_cursor() again when starting to change a
514 * new line.
515 * "flags": OPENLINE_DELSPACES delete spaces after cursor
516 * OPENLINE_DO_COM format comments
517 * OPENLINE_KEEPTRAIL keep trailing spaces
518 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200519 * OPENLINE_COM_LIST format comments with list or 2nd line indent
520 *
521 * "second_line_indent": indent for after ^^D in Insert mode or if flag
522 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 *
524 * Return TRUE for success, FALSE for failure
525 */
526 int
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200527open_line(dir, flags, second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 int dir; /* FORWARD or BACKWARD */
529 int flags;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200530 int second_line_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 char_u *saved_line; /* copy of the original line */
533 char_u *next_line = NULL; /* copy of the next line */
534 char_u *p_extra = NULL; /* what goes to next line */
535 int less_cols = 0; /* less columns for mark in new line */
536 int less_cols_off = 0; /* columns to skip for mark adjust */
537 pos_T old_cursor; /* old cursor position */
538 int newcol = 0; /* new cursor column */
539 int newindent = 0; /* auto-indent of the new line */
540 int n;
541 int trunc_line = FALSE; /* truncate current line afterwards */
542 int retval = FALSE; /* return value, default is FAIL */
543#ifdef FEAT_COMMENTS
544 int extra_len = 0; /* length of p_extra string */
545 int lead_len; /* length of comment leader */
546 char_u *lead_flags; /* position in 'comments' for comment leader */
547 char_u *leader = NULL; /* copy of comment leader */
548#endif
549 char_u *allocated = NULL; /* allocated memory */
550#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
551 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
552 char_u *p;
553#endif
554 int saved_char = NUL; /* init for GCC */
555#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
556 pos_T *pos;
557#endif
558#ifdef FEAT_SMARTINDENT
559 int do_si = (!p_paste && curbuf->b_p_si
560# ifdef FEAT_CINDENT
561 && !curbuf->b_p_cin
562# endif
563 );
564 int no_si = FALSE; /* reset did_si afterwards */
565 int first_char = NUL; /* init for GCC */
566#endif
567#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
568 int vreplace_mode;
569#endif
570 int did_append; /* appended a new line */
571 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
572
573 /*
574 * make a copy of the current line so we can mess with it
575 */
576 saved_line = vim_strsave(ml_get_curline());
577 if (saved_line == NULL) /* out of memory! */
578 return FALSE;
579
580#ifdef FEAT_VREPLACE
581 if (State & VREPLACE_FLAG)
582 {
583 /*
584 * With VREPLACE we make a copy of the next line, which we will be
585 * starting to replace. First make the new line empty and let vim play
586 * with the indenting and comment leader to its heart's content. Then
587 * we grab what it ended up putting on the new line, put back the
588 * original line, and call ins_char() to put each new character onto
589 * the line, replacing what was there before and pushing the right
590 * stuff onto the replace stack. -- webb.
591 */
592 if (curwin->w_cursor.lnum < orig_line_count)
593 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
594 else
595 next_line = vim_strsave((char_u *)"");
596 if (next_line == NULL) /* out of memory! */
597 goto theend;
598
599 /*
600 * In VREPLACE mode, a NL replaces the rest of the line, and starts
601 * replacing the next line, so push all of the characters left on the
602 * line onto the replace stack. We'll push any other characters that
603 * might be replaced at the start of the next line (due to autoindent
604 * etc) a bit later.
605 */
606 replace_push(NUL); /* Call twice because BS over NL expects it */
607 replace_push(NUL);
608 p = saved_line + curwin->w_cursor.col;
609 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000610 {
611#ifdef FEAT_MBYTE
612 if (has_mbyte)
613 p += replace_push_mb(p);
614 else
615#endif
616 replace_push(*p++);
617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 saved_line[curwin->w_cursor.col] = NUL;
619 }
620#endif
621
622 if ((State & INSERT)
623#ifdef FEAT_VREPLACE
624 && !(State & VREPLACE_FLAG)
625#endif
626 )
627 {
628 p_extra = saved_line + curwin->w_cursor.col;
629#ifdef FEAT_SMARTINDENT
630 if (do_si) /* need first char after new line break */
631 {
632 p = skipwhite(p_extra);
633 first_char = *p;
634 }
635#endif
636#ifdef FEAT_COMMENTS
637 extra_len = (int)STRLEN(p_extra);
638#endif
639 saved_char = *p_extra;
640 *p_extra = NUL;
641 }
642
643 u_clearline(); /* cannot do "U" command when adding lines */
644#ifdef FEAT_SMARTINDENT
645 did_si = FALSE;
646#endif
647 ai_col = 0;
648
649 /*
650 * If we just did an auto-indent, then we didn't type anything on
651 * the prior line, and it should be truncated. Do this even if 'ai' is not
652 * set because automatically inserting a comment leader also sets did_ai.
653 */
654 if (dir == FORWARD && did_ai)
655 trunc_line = TRUE;
656
657 /*
658 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
659 * indent to use for the new line.
660 */
661 if (curbuf->b_p_ai
662#ifdef FEAT_SMARTINDENT
663 || do_si
664#endif
665 )
666 {
667 /*
668 * count white space on current line
669 */
670 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200671 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
672 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674#ifdef FEAT_SMARTINDENT
675 /*
676 * Do smart indenting.
677 * In insert/replace mode (only when dir == FORWARD)
678 * we may move some text to the next line. If it starts with '{'
679 * don't add an indent. Fixes inserting a NL before '{' in line
680 * "if (condition) {"
681 */
682 if (!trunc_line && do_si && *saved_line != NUL
683 && (p_extra == NULL || first_char != '{'))
684 {
685 char_u *ptr;
686 char_u last_char;
687
688 old_cursor = curwin->w_cursor;
689 ptr = saved_line;
690# ifdef FEAT_COMMENTS
691 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200692 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 else
694 lead_len = 0;
695# endif
696 if (dir == FORWARD)
697 {
698 /*
699 * Skip preprocessor directives, unless they are
700 * recognised as comments.
701 */
702 if (
703# ifdef FEAT_COMMENTS
704 lead_len == 0 &&
705# endif
706 ptr[0] == '#')
707 {
708 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
709 ptr = ml_get(--curwin->w_cursor.lnum);
710 newindent = get_indent();
711 }
712# ifdef FEAT_COMMENTS
713 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200714 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 else
716 lead_len = 0;
717 if (lead_len > 0)
718 {
719 /*
720 * This case gets the following right:
721 * \*
722 * * A comment (read '\' as '/').
723 * *\
724 * #define IN_THE_WAY
725 * This should line up here;
726 */
727 p = skipwhite(ptr);
728 if (p[0] == '/' && p[1] == '*')
729 p++;
730 if (p[0] == '*')
731 {
732 for (p++; *p; p++)
733 {
734 if (p[0] == '/' && p[-1] == '*')
735 {
736 /*
737 * End of C comment, indent should line up
738 * with the line containing the start of
739 * the comment
740 */
741 curwin->w_cursor.col = (colnr_T)(p - ptr);
742 if ((pos = findmatch(NULL, NUL)) != NULL)
743 {
744 curwin->w_cursor.lnum = pos->lnum;
745 newindent = get_indent();
746 }
747 }
748 }
749 }
750 }
751 else /* Not a comment line */
752# endif
753 {
754 /* Find last non-blank in line */
755 p = ptr + STRLEN(ptr) - 1;
756 while (p > ptr && vim_iswhite(*p))
757 --p;
758 last_char = *p;
759
760 /*
761 * find the character just before the '{' or ';'
762 */
763 if (last_char == '{' || last_char == ';')
764 {
765 if (p > ptr)
766 --p;
767 while (p > ptr && vim_iswhite(*p))
768 --p;
769 }
770 /*
771 * Try to catch lines that are split over multiple
772 * lines. eg:
773 * if (condition &&
774 * condition) {
775 * Should line up here!
776 * }
777 */
778 if (*p == ')')
779 {
780 curwin->w_cursor.col = (colnr_T)(p - ptr);
781 if ((pos = findmatch(NULL, '(')) != NULL)
782 {
783 curwin->w_cursor.lnum = pos->lnum;
784 newindent = get_indent();
785 ptr = ml_get_curline();
786 }
787 }
788 /*
789 * If last character is '{' do indent, without
790 * checking for "if" and the like.
791 */
792 if (last_char == '{')
793 {
794 did_si = TRUE; /* do indent */
795 no_si = TRUE; /* don't delete it when '{' typed */
796 }
797 /*
798 * Look for "if" and the like, use 'cinwords'.
799 * Don't do this if the previous line ended in ';' or
800 * '}'.
801 */
802 else if (last_char != ';' && last_char != '}'
803 && cin_is_cinword(ptr))
804 did_si = TRUE;
805 }
806 }
807 else /* dir == BACKWARD */
808 {
809 /*
810 * Skip preprocessor directives, unless they are
811 * recognised as comments.
812 */
813 if (
814# ifdef FEAT_COMMENTS
815 lead_len == 0 &&
816# endif
817 ptr[0] == '#')
818 {
819 int was_backslashed = FALSE;
820
821 while ((ptr[0] == '#' || was_backslashed) &&
822 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
823 {
824 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
825 was_backslashed = TRUE;
826 else
827 was_backslashed = FALSE;
828 ptr = ml_get(++curwin->w_cursor.lnum);
829 }
830 if (was_backslashed)
831 newindent = 0; /* Got to end of file */
832 else
833 newindent = get_indent();
834 }
835 p = skipwhite(ptr);
836 if (*p == '}') /* if line starts with '}': do indent */
837 did_si = TRUE;
838 else /* can delete indent when '{' typed */
839 can_si_back = TRUE;
840 }
841 curwin->w_cursor = old_cursor;
842 }
843 if (do_si)
844 can_si = TRUE;
845#endif /* FEAT_SMARTINDENT */
846
847 did_ai = TRUE;
848 }
849
850#ifdef FEAT_COMMENTS
851 /*
852 * Find out if the current line starts with a comment leader.
853 * This may then be inserted in front of the new line.
854 */
855 end_comment_pending = NUL;
856 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200857 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 else
859 lead_len = 0;
860 if (lead_len > 0)
861 {
862 char_u *lead_repl = NULL; /* replaces comment leader */
863 int lead_repl_len = 0; /* length of *lead_repl */
864 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
865 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
866 char_u *comment_end = NULL; /* where lead_end has been found */
867 int extra_space = FALSE; /* append extra space */
868 int current_flag;
869 int require_blank = FALSE; /* requires blank after middle */
870 char_u *p2;
871
872 /*
873 * If the comment leader has the start, middle or end flag, it may not
874 * be used or may be replaced with the middle leader.
875 */
876 for (p = lead_flags; *p && *p != ':'; ++p)
877 {
878 if (*p == COM_BLANK)
879 {
880 require_blank = TRUE;
881 continue;
882 }
883 if (*p == COM_START || *p == COM_MIDDLE)
884 {
885 current_flag = *p;
886 if (*p == COM_START)
887 {
888 /*
889 * Doing "O" on a start of comment does not insert leader.
890 */
891 if (dir == BACKWARD)
892 {
893 lead_len = 0;
894 break;
895 }
896
897 /* find start of middle part */
898 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
899 require_blank = FALSE;
900 }
901
902 /*
903 * Isolate the strings of the middle and end leader.
904 */
905 while (*p && p[-1] != ':') /* find end of middle flags */
906 {
907 if (*p == COM_BLANK)
908 require_blank = TRUE;
909 ++p;
910 }
911 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
912
913 while (*p && p[-1] != ':') /* find end of end flags */
914 {
915 /* Check whether we allow automatic ending of comments */
916 if (*p == COM_AUTO_END)
917 end_comment_pending = -1; /* means we want to set it */
918 ++p;
919 }
920 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
921
922 if (end_comment_pending == -1) /* we can set it now */
923 end_comment_pending = lead_end[n - 1];
924
925 /*
926 * If the end of the comment is in the same line, don't use
927 * the comment leader.
928 */
929 if (dir == FORWARD)
930 {
931 for (p = saved_line + lead_len; *p; ++p)
932 if (STRNCMP(p, lead_end, n) == 0)
933 {
934 comment_end = p;
935 lead_len = 0;
936 break;
937 }
938 }
939
940 /*
941 * Doing "o" on a start of comment inserts the middle leader.
942 */
943 if (lead_len > 0)
944 {
945 if (current_flag == COM_START)
946 {
947 lead_repl = lead_middle;
948 lead_repl_len = (int)STRLEN(lead_middle);
949 }
950
951 /*
952 * If we have hit RETURN immediately after the start
953 * comment leader, then put a space after the middle
954 * comment leader on the next line.
955 */
956 if (!vim_iswhite(saved_line[lead_len - 1])
957 && ((p_extra != NULL
958 && (int)curwin->w_cursor.col == lead_len)
959 || (p_extra == NULL
960 && saved_line[lead_len] == NUL)
961 || require_blank))
962 extra_space = TRUE;
963 }
964 break;
965 }
966 if (*p == COM_END)
967 {
968 /*
969 * Doing "o" on the end of a comment does not insert leader.
970 * Remember where the end is, might want to use it to find the
971 * start (for C-comments).
972 */
973 if (dir == FORWARD)
974 {
975 comment_end = skipwhite(saved_line);
976 lead_len = 0;
977 break;
978 }
979
980 /*
981 * Doing "O" on the end of a comment inserts the middle leader.
982 * Find the string for the middle leader, searching backwards.
983 */
984 while (p > curbuf->b_p_com && *p != ',')
985 --p;
986 for (lead_repl = p; lead_repl > curbuf->b_p_com
987 && lead_repl[-1] != ':'; --lead_repl)
988 ;
989 lead_repl_len = (int)(p - lead_repl);
990
991 /* We can probably always add an extra space when doing "O" on
992 * the comment-end */
993 extra_space = TRUE;
994
995 /* Check whether we allow automatic ending of comments */
996 for (p2 = p; *p2 && *p2 != ':'; p2++)
997 {
998 if (*p2 == COM_AUTO_END)
999 end_comment_pending = -1; /* means we want to set it */
1000 }
1001 if (end_comment_pending == -1)
1002 {
1003 /* Find last character in end-comment string */
1004 while (*p2 && *p2 != ',')
1005 p2++;
1006 end_comment_pending = p2[-1];
1007 }
1008 break;
1009 }
1010 if (*p == COM_FIRST)
1011 {
1012 /*
1013 * Comment leader for first line only: Don't repeat leader
1014 * when using "O", blank out leader when using "o".
1015 */
1016 if (dir == BACKWARD)
1017 lead_len = 0;
1018 else
1019 {
1020 lead_repl = (char_u *)"";
1021 lead_repl_len = 0;
1022 }
1023 break;
1024 }
1025 }
1026 if (lead_len)
1027 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001028 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001029 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001030 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 allocated = leader; /* remember to free it later */
1032
1033 if (leader == NULL)
1034 lead_len = 0;
1035 else
1036 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001037 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
1039 /*
1040 * Replace leader with lead_repl, right or left adjusted
1041 */
1042 if (lead_repl != NULL)
1043 {
1044 int c = 0;
1045 int off = 0;
1046
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001047 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001050 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 else if (VIM_ISDIGIT(*p) || *p == '-')
1052 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001053 else
1054 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 }
1056 if (c == COM_RIGHT) /* right adjusted leader */
1057 {
1058 /* find last non-white in the leader to line up with */
1059 for (p = leader + lead_len - 1; p > leader
1060 && vim_iswhite(*p); --p)
1061 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001063
1064#ifdef FEAT_MBYTE
1065 /* Compute the length of the replaced characters in
1066 * screen characters, not bytes. */
1067 {
1068 int repl_size = vim_strnsize(lead_repl,
1069 lead_repl_len);
1070 int old_size = 0;
1071 char_u *endp = p;
1072 int l;
1073
1074 while (old_size < repl_size && p > leader)
1075 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001076 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001077 old_size += ptr2cells(p);
1078 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001079 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001080 if (l != 0)
1081 mch_memmove(endp + l, endp,
1082 (size_t)((leader + lead_len) - endp));
1083 lead_len += l;
1084 }
1085#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (p < leader + lead_repl_len)
1087 p = leader;
1088 else
1089 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1092 if (p + lead_repl_len > leader + lead_len)
1093 p[lead_repl_len] = NUL;
1094
1095 /* blank-out any other chars from the old leader. */
1096 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001097 {
1098#ifdef FEAT_MBYTE
1099 int l = mb_head_off(leader, p);
1100
1101 if (l > 1)
1102 {
1103 p -= l;
1104 if (ptr2cells(p) > 1)
1105 {
1106 p[1] = ' ';
1107 --l;
1108 }
1109 mch_memmove(p + 1, p + l + 1,
1110 (size_t)((leader + lead_len) - (p + l + 1)));
1111 lead_len -= l;
1112 *p = ' ';
1113 }
1114 else
1115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (!vim_iswhite(*p))
1117 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120 else /* left adjusted leader */
1121 {
1122 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001123#ifdef FEAT_MBYTE
1124 /* Compute the length of the replaced characters in
1125 * screen characters, not bytes. Move the part that is
1126 * not to be overwritten. */
1127 {
1128 int repl_size = vim_strnsize(lead_repl,
1129 lead_repl_len);
1130 int i;
1131 int l;
1132
1133 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1134 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001135 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001136 if (vim_strnsize(p, i + l) > repl_size)
1137 break;
1138 }
1139 if (i != lead_repl_len)
1140 {
1141 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001142 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001143 lead_len += lead_repl_len - i;
1144 }
1145 }
1146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1148
1149 /* Replace any remaining non-white chars in the old
1150 * leader by spaces. Keep Tabs, the indent must
1151 * remain the same. */
1152 for (p += lead_repl_len; p < leader + lead_len; ++p)
1153 if (!vim_iswhite(*p))
1154 {
1155 /* Don't put a space before a TAB. */
1156 if (p + 1 < leader + lead_len && p[1] == TAB)
1157 {
1158 --lead_len;
1159 mch_memmove(p, p + 1,
1160 (leader + lead_len) - p);
1161 }
1162 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001163 {
1164#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001165 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001166
1167 if (l > 1)
1168 {
1169 if (ptr2cells(p) > 1)
1170 {
1171 /* Replace a double-wide char with
1172 * two spaces */
1173 --l;
1174 *p++ = ' ';
1175 }
1176 mch_memmove(p + 1, p + l,
1177 (leader + lead_len) - p);
1178 lead_len -= l - 1;
1179 }
1180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 *p = NUL;
1185 }
1186
1187 /* Recompute the indent, it may have changed. */
1188 if (curbuf->b_p_ai
1189#ifdef FEAT_SMARTINDENT
1190 || do_si
1191#endif
1192 )
1193 newindent = get_indent_str(leader, (int)curbuf->b_p_ts);
1194
1195 /* Add the indent offset */
1196 if (newindent + off < 0)
1197 {
1198 off = -newindent;
1199 newindent = 0;
1200 }
1201 else
1202 newindent += off;
1203
1204 /* Correct trailing spaces for the shift, so that
1205 * alignment remains equal. */
1206 while (off > 0 && lead_len > 0
1207 && leader[lead_len - 1] == ' ')
1208 {
1209 /* Don't do it when there is a tab before the space */
1210 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1211 break;
1212 --lead_len;
1213 --off;
1214 }
1215
1216 /* If the leader ends in white space, don't add an
1217 * extra space */
1218 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1219 extra_space = FALSE;
1220 leader[lead_len] = NUL;
1221 }
1222
1223 if (extra_space)
1224 {
1225 leader[lead_len++] = ' ';
1226 leader[lead_len] = NUL;
1227 }
1228
1229 newcol = lead_len;
1230
1231 /*
1232 * if a new indent will be set below, remove the indent that
1233 * is in the comment leader
1234 */
1235 if (newindent
1236#ifdef FEAT_SMARTINDENT
1237 || did_si
1238#endif
1239 )
1240 {
1241 while (lead_len && vim_iswhite(*leader))
1242 {
1243 --lead_len;
1244 --newcol;
1245 ++leader;
1246 }
1247 }
1248
1249 }
1250#ifdef FEAT_SMARTINDENT
1251 did_si = can_si = FALSE;
1252#endif
1253 }
1254 else if (comment_end != NULL)
1255 {
1256 /*
1257 * We have finished a comment, so we don't use the leader.
1258 * If this was a C-comment and 'ai' or 'si' is set do a normal
1259 * indent to align with the line containing the start of the
1260 * comment.
1261 */
1262 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1263 (curbuf->b_p_ai
1264#ifdef FEAT_SMARTINDENT
1265 || do_si
1266#endif
1267 ))
1268 {
1269 old_cursor = curwin->w_cursor;
1270 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1271 if ((pos = findmatch(NULL, NUL)) != NULL)
1272 {
1273 curwin->w_cursor.lnum = pos->lnum;
1274 newindent = get_indent();
1275 }
1276 curwin->w_cursor = old_cursor;
1277 }
1278 }
1279 }
1280#endif
1281
1282 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1283 if (p_extra != NULL)
1284 {
1285 *p_extra = saved_char; /* restore char that NUL replaced */
1286
1287 /*
1288 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1289 * non-blank.
1290 *
1291 * When in REPLACE mode, put the deleted blanks on the replace stack,
1292 * preceded by a NUL, so they can be put back when a BS is entered.
1293 */
1294 if (REPLACE_NORMAL(State))
1295 replace_push(NUL); /* end of extra blanks */
1296 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1297 {
1298 while ((*p_extra == ' ' || *p_extra == '\t')
1299#ifdef FEAT_MBYTE
1300 && (!enc_utf8
1301 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1302#endif
1303 )
1304 {
1305 if (REPLACE_NORMAL(State))
1306 replace_push(*p_extra);
1307 ++p_extra;
1308 ++less_cols_off;
1309 }
1310 }
1311 if (*p_extra != NUL)
1312 did_ai = FALSE; /* append some text, don't truncate now */
1313
1314 /* columns for marks adjusted for removed columns */
1315 less_cols = (int)(p_extra - saved_line);
1316 }
1317
1318 if (p_extra == NULL)
1319 p_extra = (char_u *)""; /* append empty line */
1320
1321#ifdef FEAT_COMMENTS
1322 /* concatenate leader and p_extra, if there is a leader */
1323 if (lead_len)
1324 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001325 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1326 {
1327 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001328 int padding = second_line_indent
1329 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001330
1331 /* Here whitespace is inserted after the comment char.
1332 * Below, set_indent(newindent, SIN_INSERT) will insert the
1333 * whitespace needed before the comment char. */
1334 for (i = 0; i < padding; i++)
1335 {
1336 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001337 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001338 newcol++;
1339 }
1340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 STRCAT(leader, p_extra);
1342 p_extra = leader;
1343 did_ai = TRUE; /* So truncating blanks works with comments */
1344 less_cols -= lead_len;
1345 }
1346 else
1347 end_comment_pending = NUL; /* turns out there was no leader */
1348#endif
1349
1350 old_cursor = curwin->w_cursor;
1351 if (dir == BACKWARD)
1352 --curwin->w_cursor.lnum;
1353#ifdef FEAT_VREPLACE
1354 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1355#endif
1356 {
1357 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1358 == FAIL)
1359 goto theend;
1360 /* Postpone calling changed_lines(), because it would mess up folding
1361 * with markers. */
1362 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1363 did_append = TRUE;
1364 }
1365#ifdef FEAT_VREPLACE
1366 else
1367 {
1368 /*
1369 * In VREPLACE mode we are starting to replace the next line.
1370 */
1371 curwin->w_cursor.lnum++;
1372 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1373 {
1374 /* In case we NL to a new line, BS to the previous one, and NL
1375 * again, we don't want to save the new line for undo twice.
1376 */
1377 (void)u_save_cursor(); /* errors are ignored! */
1378 vr_lines_changed++;
1379 }
1380 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1381 changed_bytes(curwin->w_cursor.lnum, 0);
1382 curwin->w_cursor.lnum--;
1383 did_append = FALSE;
1384 }
1385#endif
1386
1387 if (newindent
1388#ifdef FEAT_SMARTINDENT
1389 || did_si
1390#endif
1391 )
1392 {
1393 ++curwin->w_cursor.lnum;
1394#ifdef FEAT_SMARTINDENT
1395 if (did_si)
1396 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02001397 int sw = (int)get_sw_value();
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001400 newindent -= newindent % sw;
1401 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001404 /* Copy the indent */
1405 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 {
1407 (void)copy_indent(newindent, saved_line);
1408
1409 /*
1410 * Set the 'preserveindent' option so that any further screwing
1411 * with the line doesn't entirely destroy our efforts to preserve
1412 * it. It gets restored at the function end.
1413 */
1414 curbuf->b_p_pi = TRUE;
1415 }
1416 else
1417 (void)set_indent(newindent, SIN_INSERT);
1418 less_cols -= curwin->w_cursor.col;
1419
1420 ai_col = curwin->w_cursor.col;
1421
1422 /*
1423 * In REPLACE mode, for each character in the new indent, there must
1424 * be a NUL on the replace stack, for when it is deleted with BS
1425 */
1426 if (REPLACE_NORMAL(State))
1427 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1428 replace_push(NUL);
1429 newcol += curwin->w_cursor.col;
1430#ifdef FEAT_SMARTINDENT
1431 if (no_si)
1432 did_si = FALSE;
1433#endif
1434 }
1435
1436#ifdef FEAT_COMMENTS
1437 /*
1438 * In REPLACE mode, for each character in the extra leader, there must be
1439 * a NUL on the replace stack, for when it is deleted with BS.
1440 */
1441 if (REPLACE_NORMAL(State))
1442 while (lead_len-- > 0)
1443 replace_push(NUL);
1444#endif
1445
1446 curwin->w_cursor = old_cursor;
1447
1448 if (dir == FORWARD)
1449 {
1450 if (trunc_line || (State & INSERT))
1451 {
1452 /* truncate current line at cursor */
1453 saved_line[curwin->w_cursor.col] = NUL;
1454 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1455 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1456 truncate_spaces(saved_line);
1457 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1458 saved_line = NULL;
1459 if (did_append)
1460 {
1461 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1462 curwin->w_cursor.lnum + 1, 1L);
1463 did_append = FALSE;
1464
1465 /* Move marks after the line break to the new line. */
1466 if (flags & OPENLINE_MARKFIX)
1467 mark_col_adjust(curwin->w_cursor.lnum,
1468 curwin->w_cursor.col + less_cols_off,
1469 1L, (long)-less_cols);
1470 }
1471 else
1472 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1473 }
1474
1475 /*
1476 * Put the cursor on the new line. Careful: the scrollup() above may
1477 * have moved w_cursor, we must use old_cursor.
1478 */
1479 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1480 }
1481 if (did_append)
1482 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1483
1484 curwin->w_cursor.col = newcol;
1485#ifdef FEAT_VIRTUALEDIT
1486 curwin->w_cursor.coladd = 0;
1487#endif
1488
1489#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1490 /*
1491 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1492 * fixthisline() from doing it (via change_indent()) by telling it we're in
1493 * normal INSERT mode.
1494 */
1495 if (State & VREPLACE_FLAG)
1496 {
1497 vreplace_mode = State; /* So we know to put things right later */
1498 State = INSERT;
1499 }
1500 else
1501 vreplace_mode = 0;
1502#endif
1503#ifdef FEAT_LISP
1504 /*
1505 * May do lisp indenting.
1506 */
1507 if (!p_paste
1508# ifdef FEAT_COMMENTS
1509 && leader == NULL
1510# endif
1511 && curbuf->b_p_lisp
1512 && curbuf->b_p_ai)
1513 {
1514 fixthisline(get_lisp_indent);
1515 p = ml_get_curline();
1516 ai_col = (colnr_T)(skipwhite(p) - p);
1517 }
1518#endif
1519#ifdef FEAT_CINDENT
1520 /*
1521 * May do indenting after opening a new line.
1522 */
1523 if (!p_paste
1524 && (curbuf->b_p_cin
1525# ifdef FEAT_EVAL
1526 || *curbuf->b_p_inde != NUL
1527# endif
1528 )
1529 && in_cinkeys(dir == FORWARD
1530 ? KEY_OPEN_FORW
1531 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1532 {
1533 do_c_expr_indent();
1534 p = ml_get_curline();
1535 ai_col = (colnr_T)(skipwhite(p) - p);
1536 }
1537#endif
1538#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1539 if (vreplace_mode != 0)
1540 State = vreplace_mode;
1541#endif
1542
1543#ifdef FEAT_VREPLACE
1544 /*
1545 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1546 * original line, and inserts the new stuff char by char, pushing old stuff
1547 * onto the replace stack (via ins_char()).
1548 */
1549 if (State & VREPLACE_FLAG)
1550 {
1551 /* Put new line in p_extra */
1552 p_extra = vim_strsave(ml_get_curline());
1553 if (p_extra == NULL)
1554 goto theend;
1555
1556 /* Put back original line */
1557 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1558
1559 /* Insert new stuff into line again */
1560 curwin->w_cursor.col = 0;
1561#ifdef FEAT_VIRTUALEDIT
1562 curwin->w_cursor.coladd = 0;
1563#endif
1564 ins_bytes(p_extra); /* will call changed_bytes() */
1565 vim_free(p_extra);
1566 next_line = NULL;
1567 }
1568#endif
1569
1570 retval = TRUE; /* success! */
1571theend:
1572 curbuf->b_p_pi = saved_pi;
1573 vim_free(saved_line);
1574 vim_free(next_line);
1575 vim_free(allocated);
1576 return retval;
1577}
1578
1579#if defined(FEAT_COMMENTS) || defined(PROTO)
1580/*
1581 * get_leader_len() returns the length of the prefix of the given string
1582 * which introduces a comment. If this string is not a comment then 0 is
1583 * returned.
1584 * When "flags" is not NULL, it is set to point to the flags of the recognized
1585 * comment leader.
1586 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001587 * If "include_space" is set, include trailing whitespace while calculating the
1588 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 */
1590 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001591get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 char_u *line;
1593 char_u **flags;
1594 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001595 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
1597 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001598 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 int got_com = FALSE;
1600 int found_one;
1601 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1602 char_u *string; /* pointer to comment string */
1603 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001604 int middle_match_len = 0;
1605 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001606 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
Bram Moolenaar81340392012-06-06 16:12:59 +02001608 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 while (vim_iswhite(line[i])) /* leading white space is ignored */
1610 ++i;
1611
1612 /*
1613 * Repeat to match several nested comment strings.
1614 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001615 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 /*
1618 * scan through the 'comments' option for a match
1619 */
1620 found_one = FALSE;
1621 for (list = curbuf->b_p_com; *list; )
1622 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001623 /* Get one option part into part_buf[]. Advance "list" to next
1624 * one. Put "string" at start of string. */
1625 if (!got_com && flags != NULL)
1626 *flags = list; /* remember where flags started */
1627 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1629 string = vim_strchr(part_buf, ':');
1630 if (string == NULL) /* missing ':', ignore this part */
1631 continue;
1632 *string++ = NUL; /* isolate flags from string */
1633
Bram Moolenaara4271d52011-05-10 13:38:27 +02001634 /* If we found a middle match previously, use that match when this
1635 * is not a middle or end. */
1636 if (middle_match_len != 0
1637 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1638 && vim_strchr(part_buf, COM_END) == NULL)
1639 break;
1640
1641 /* When we already found a nested comment, only accept further
1642 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1644 continue;
1645
Bram Moolenaara4271d52011-05-10 13:38:27 +02001646 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1648 continue;
1649
Bram Moolenaara4271d52011-05-10 13:38:27 +02001650 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 * When string starts with white space, must have some white space
1652 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001653 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (vim_iswhite(string[0]))
1655 {
1656 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001657 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 while (vim_iswhite(string[0]))
1659 ++string;
1660 }
1661 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1662 ;
1663 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001664 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaara4271d52011-05-10 13:38:27 +02001666 /* When 'b' flag used, there must be white space or an
1667 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (vim_strchr(part_buf, COM_BLANK) != NULL
1669 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1670 continue;
1671
Bram Moolenaara4271d52011-05-10 13:38:27 +02001672 /* We have found a match, stop searching unless this is a middle
1673 * comment. The middle comment can be a substring of the end
1674 * comment in which case it's better to return the length of the
1675 * end comment and its flags. Thus we keep searching with middle
1676 * and end matches and use an end match if it matches better. */
1677 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1678 {
1679 if (middle_match_len == 0)
1680 {
1681 middle_match_len = j;
1682 saved_flags = prev_list;
1683 }
1684 continue;
1685 }
1686 if (middle_match_len != 0 && j > middle_match_len)
1687 /* Use this match instead of the middle match, since it's a
1688 * longer thus better match. */
1689 middle_match_len = 0;
1690
1691 if (middle_match_len == 0)
1692 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 found_one = TRUE;
1694 break;
1695 }
1696
Bram Moolenaara4271d52011-05-10 13:38:27 +02001697 if (middle_match_len != 0)
1698 {
1699 /* Use the previously found middle match after failing to find a
1700 * match with an end. */
1701 if (!got_com && flags != NULL)
1702 *flags = saved_flags;
1703 i += middle_match_len;
1704 found_one = TRUE;
1705 }
1706
1707 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (!found_one)
1709 break;
1710
Bram Moolenaar81340392012-06-06 16:12:59 +02001711 result = i;
1712
Bram Moolenaara4271d52011-05-10 13:38:27 +02001713 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 while (vim_iswhite(line[i]))
1715 ++i;
1716
Bram Moolenaar81340392012-06-06 16:12:59 +02001717 if (include_space)
1718 result = i;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* If this comment doesn't nest, stop here. */
1721 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (vim_strchr(part_buf, COM_NEST) == NULL)
1723 break;
1724 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001725 return result;
1726}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001727
Bram Moolenaar81340392012-06-06 16:12:59 +02001728/*
1729 * Return the offset at which the last comment in line starts. If there is no
1730 * comment in the whole line, -1 is returned.
1731 *
1732 * When "flags" is not null, it is set to point to the flags describing the
1733 * recognized comment leader.
1734 */
1735 int
1736get_last_leader_offset(line, flags)
1737 char_u *line;
1738 char_u **flags;
1739{
1740 int result = -1;
1741 int i, j;
1742 int lower_check_bound = 0;
1743 char_u *string;
1744 char_u *com_leader;
1745 char_u *com_flags;
1746 char_u *list;
1747 int found_one;
1748 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1749
1750 /*
1751 * Repeat to match several nested comment strings.
1752 */
1753 i = (int)STRLEN(line);
1754 while (--i >= lower_check_bound)
1755 {
1756 /*
1757 * scan through the 'comments' option for a match
1758 */
1759 found_one = FALSE;
1760 for (list = curbuf->b_p_com; *list; )
1761 {
1762 char_u *flags_save = list;
1763
1764 /*
1765 * Get one option part into part_buf[]. Advance list to next one.
1766 * put string at start of string.
1767 */
1768 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1769 string = vim_strchr(part_buf, ':');
1770 if (string == NULL) /* If everything is fine, this cannot actually
1771 * happen. */
1772 {
1773 continue;
1774 }
1775 *string++ = NUL; /* Isolate flags from string. */
1776 com_leader = string;
1777
1778 /*
1779 * Line contents and string must match.
1780 * When string starts with white space, must have some white space
1781 * (but the amount does not need to match, there might be a mix of
1782 * TABs and spaces).
1783 */
1784 if (vim_iswhite(string[0]))
1785 {
1786 if (i == 0 || !vim_iswhite(line[i - 1]))
1787 continue;
1788 while (vim_iswhite(string[0]))
1789 ++string;
1790 }
1791 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1792 /* do nothing */;
1793 if (string[j] != NUL)
1794 continue;
1795
1796 /*
1797 * When 'b' flag used, there must be white space or an
1798 * end-of-line after the string in the line.
1799 */
1800 if (vim_strchr(part_buf, COM_BLANK) != NULL
1801 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1802 {
1803 continue;
1804 }
1805
1806 /*
1807 * We have found a match, stop searching.
1808 */
1809 found_one = TRUE;
1810
1811 if (flags)
1812 *flags = flags_save;
1813 com_flags = flags_save;
1814
1815 break;
1816 }
1817
1818 if (found_one)
1819 {
1820 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1821 int len1, len2, off;
1822
1823 result = i;
1824 /*
1825 * If this comment nests, continue searching.
1826 */
1827 if (vim_strchr(part_buf, COM_NEST) != NULL)
1828 continue;
1829
1830 lower_check_bound = i;
1831
1832 /* Let's verify whether the comment leader found is a substring
1833 * of other comment leaders. If it is, let's adjust the
1834 * lower_check_bound so that we make sure that we have determined
1835 * the comment leader correctly.
1836 */
1837
1838 while (vim_iswhite(*com_leader))
1839 ++com_leader;
1840 len1 = (int)STRLEN(com_leader);
1841
1842 for (list = curbuf->b_p_com; *list; )
1843 {
1844 char_u *flags_save = list;
1845
1846 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1847 if (flags_save == com_flags)
1848 continue;
1849 string = vim_strchr(part_buf2, ':');
1850 ++string;
1851 while (vim_iswhite(*string))
1852 ++string;
1853 len2 = (int)STRLEN(string);
1854 if (len2 == 0)
1855 continue;
1856
1857 /* Now we have to verify whether string ends with a substring
1858 * beginning the com_leader. */
1859 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1860 {
1861 --off;
1862 if (!STRNCMP(string + off, com_leader, len2 - off))
1863 {
1864 if (i - off < lower_check_bound)
1865 lower_check_bound = i - off;
1866 }
1867 }
1868 }
1869 }
1870 }
1871 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872}
1873#endif
1874
1875/*
1876 * Return the number of window lines occupied by buffer line "lnum".
1877 */
1878 int
1879plines(lnum)
1880 linenr_T lnum;
1881{
1882 return plines_win(curwin, lnum, TRUE);
1883}
1884
1885 int
1886plines_win(wp, lnum, winheight)
1887 win_T *wp;
1888 linenr_T lnum;
1889 int winheight; /* when TRUE limit to window height */
1890{
1891#if defined(FEAT_DIFF) || defined(PROTO)
1892 /* Check for filler lines above this buffer line. When folded the result
1893 * is one line anyway. */
1894 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1895}
1896
1897 int
1898plines_nofill(lnum)
1899 linenr_T lnum;
1900{
1901 return plines_win_nofill(curwin, lnum, TRUE);
1902}
1903
1904 int
1905plines_win_nofill(wp, lnum, winheight)
1906 win_T *wp;
1907 linenr_T lnum;
1908 int winheight; /* when TRUE limit to window height */
1909{
1910#endif
1911 int lines;
1912
1913 if (!wp->w_p_wrap)
1914 return 1;
1915
1916#ifdef FEAT_VERTSPLIT
1917 if (wp->w_width == 0)
1918 return 1;
1919#endif
1920
1921#ifdef FEAT_FOLDING
1922 /* A folded lines is handled just like an empty line. */
1923 /* NOTE: Caller must handle lines that are MAYBE folded. */
1924 if (lineFolded(wp, lnum) == TRUE)
1925 return 1;
1926#endif
1927
1928 lines = plines_win_nofold(wp, lnum);
1929 if (winheight > 0 && lines > wp->w_height)
1930 return (int)wp->w_height;
1931 return lines;
1932}
1933
1934/*
1935 * Return number of window lines physical line "lnum" will occupy in window
1936 * "wp". Does not care about folding, 'wrap' or 'diff'.
1937 */
1938 int
1939plines_win_nofold(wp, lnum)
1940 win_T *wp;
1941 linenr_T lnum;
1942{
1943 char_u *s;
1944 long col;
1945 int width;
1946
1947 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
1948 if (*s == NUL) /* empty line */
1949 return 1;
1950 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
1951
1952 /*
1953 * If list mode is on, then the '$' at the end of the line may take up one
1954 * extra column.
1955 */
1956 if (wp->w_p_list && lcs_eol != NUL)
1957 col += 1;
1958
1959 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001960 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 */
1962 width = W_WIDTH(wp) - win_col_off(wp);
1963 if (width <= 0)
1964 return 32000;
1965 if (col <= width)
1966 return 1;
1967 col -= width;
1968 width += win_col_off2(wp);
1969 return (col + (width - 1)) / width + 1;
1970}
1971
1972/*
1973 * Like plines_win(), but only reports the number of physical screen lines
1974 * used from the start of the line to the given column number.
1975 */
1976 int
1977plines_win_col(wp, lnum, column)
1978 win_T *wp;
1979 linenr_T lnum;
1980 long column;
1981{
1982 long col;
1983 char_u *s;
1984 int lines = 0;
1985 int width;
1986
1987#ifdef FEAT_DIFF
1988 /* Check for filler lines above this buffer line. When folded the result
1989 * is one line anyway. */
1990 lines = diff_check_fill(wp, lnum);
1991#endif
1992
1993 if (!wp->w_p_wrap)
1994 return lines + 1;
1995
1996#ifdef FEAT_VERTSPLIT
1997 if (wp->w_width == 0)
1998 return lines + 1;
1999#endif
2000
2001 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2002
2003 col = 0;
2004 while (*s != NUL && --column >= 0)
2005 {
2006 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002007 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009
2010 /*
2011 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2012 * INSERT mode, then col must be adjusted so that it represents the last
2013 * screen position of the TAB. This only fixes an error when the TAB wraps
2014 * from one screen line to the next (when 'columns' is not a multiple of
2015 * 'ts') -- webb.
2016 */
2017 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
2018 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1;
2019
2020 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002021 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 */
2023 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002024 if (width <= 0)
2025 return 9999;
2026
2027 lines += 1;
2028 if (col > width)
2029 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2030 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031}
2032
2033 int
2034plines_m_win(wp, first, last)
2035 win_T *wp;
2036 linenr_T first, last;
2037{
2038 int count = 0;
2039
2040 while (first <= last)
2041 {
2042#ifdef FEAT_FOLDING
2043 int x;
2044
2045 /* Check if there are any really folded lines, but also included lines
2046 * that are maybe folded. */
2047 x = foldedCount(wp, first, NULL);
2048 if (x > 0)
2049 {
2050 ++count; /* count 1 for "+-- folded" line */
2051 first += x;
2052 }
2053 else
2054#endif
2055 {
2056#ifdef FEAT_DIFF
2057 if (first == wp->w_topline)
2058 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2059 else
2060#endif
2061 count += plines_win(wp, first, TRUE);
2062 ++first;
2063 }
2064 }
2065 return (count);
2066}
2067
2068#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2069/*
2070 * Insert string "p" at the cursor position. Stops at a NUL byte.
2071 * Handles Replace mode and multi-byte characters.
2072 */
2073 void
2074ins_bytes(p)
2075 char_u *p;
2076{
2077 ins_bytes_len(p, (int)STRLEN(p));
2078}
2079#endif
2080
2081#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2082 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2083/*
2084 * Insert string "p" with length "len" at the cursor position.
2085 * Handles Replace mode and multi-byte characters.
2086 */
2087 void
2088ins_bytes_len(p, len)
2089 char_u *p;
2090 int len;
2091{
2092 int i;
2093# ifdef FEAT_MBYTE
2094 int n;
2095
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002096 if (has_mbyte)
2097 for (i = 0; i < len; i += n)
2098 {
2099 if (enc_utf8)
2100 /* avoid reading past p[len] */
2101 n = utfc_ptr2len_len(p + i, len - i);
2102 else
2103 n = (*mb_ptr2len)(p + i);
2104 ins_char_bytes(p + i, n);
2105 }
2106 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002108 for (i = 0; i < len; ++i)
2109 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110}
2111#endif
2112
2113/*
2114 * Insert or replace a single character at the cursor position.
2115 * When in REPLACE or VREPLACE mode, replace any existing character.
2116 * Caller must have prepared for undo.
2117 * For multi-byte characters we get the whole character, the caller must
2118 * convert bytes to a character.
2119 */
2120 void
2121ins_char(c)
2122 int c;
2123{
2124#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002125 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 int n;
2127
2128 n = (*mb_char2bytes)(c, buf);
2129
2130 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2131 * Happens for CTRL-Vu9900. */
2132 if (buf[0] == 0)
2133 buf[0] = '\n';
2134
2135 ins_char_bytes(buf, n);
2136}
2137
2138 void
2139ins_char_bytes(buf, charlen)
2140 char_u *buf;
2141 int charlen;
2142{
2143 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144#endif
2145 int newlen; /* nr of bytes inserted */
2146 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2147 char_u *p;
2148 char_u *newp;
2149 char_u *oldp;
2150 int linelen; /* length of old line including NUL */
2151 colnr_T col;
2152 linenr_T lnum = curwin->w_cursor.lnum;
2153 int i;
2154
2155#ifdef FEAT_VIRTUALEDIT
2156 /* Break tabs if needed. */
2157 if (virtual_active() && curwin->w_cursor.coladd > 0)
2158 coladvance_force(getviscol());
2159#endif
2160
2161 col = curwin->w_cursor.col;
2162 oldp = ml_get(lnum);
2163 linelen = (int)STRLEN(oldp) + 1;
2164
2165 /* The lengths default to the values for when not replacing. */
2166 oldlen = 0;
2167#ifdef FEAT_MBYTE
2168 newlen = charlen;
2169#else
2170 newlen = 1;
2171#endif
2172
2173 if (State & REPLACE_FLAG)
2174 {
2175#ifdef FEAT_VREPLACE
2176 if (State & VREPLACE_FLAG)
2177 {
2178 colnr_T new_vcol = 0; /* init for GCC */
2179 colnr_T vcol;
2180 int old_list;
2181#ifndef FEAT_MBYTE
2182 char_u buf[2];
2183#endif
2184
2185 /*
2186 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2187 * Returns the old value of list, so when finished,
2188 * curwin->w_p_list should be set back to this.
2189 */
2190 old_list = curwin->w_p_list;
2191 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2192 curwin->w_p_list = FALSE;
2193
2194 /*
2195 * In virtual replace mode each character may replace one or more
2196 * characters (zero if it's a TAB). Count the number of bytes to
2197 * be deleted to make room for the new character, counting screen
2198 * cells. May result in adding spaces to fill a gap.
2199 */
2200 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2201#ifndef FEAT_MBYTE
2202 buf[0] = c;
2203 buf[1] = NUL;
2204#endif
2205 new_vcol = vcol + chartabsize(buf, vcol);
2206 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2207 {
2208 vcol += chartabsize(oldp + col + oldlen, vcol);
2209 /* Don't need to remove a TAB that takes us to the right
2210 * position. */
2211 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2212 break;
2213#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002214 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#else
2216 ++oldlen;
2217#endif
2218 /* Deleted a bit too much, insert spaces. */
2219 if (vcol > new_vcol)
2220 newlen += vcol - new_vcol;
2221 }
2222 curwin->w_p_list = old_list;
2223 }
2224 else
2225#endif
2226 if (oldp[col] != NUL)
2227 {
2228 /* normal replace */
2229#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002230 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#else
2232 oldlen = 1;
2233#endif
2234 }
2235
2236
2237 /* Push the replaced bytes onto the replace stack, so that they can be
2238 * put back when BS is used. The bytes of a multi-byte character are
2239 * done the other way around, so that the first byte is popped off
2240 * first (it tells the byte length of the character). */
2241 replace_push(NUL);
2242 for (i = 0; i < oldlen; ++i)
2243 {
2244#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002245 if (has_mbyte)
2246 i += replace_push_mb(oldp + col + i) - 1;
2247 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002249 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251 }
2252
2253 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2254 if (newp == NULL)
2255 return;
2256
2257 /* Copy bytes before the cursor. */
2258 if (col > 0)
2259 mch_memmove(newp, oldp, (size_t)col);
2260
2261 /* Copy bytes after the changed character(s). */
2262 p = newp + col;
2263 mch_memmove(p + newlen, oldp + col + oldlen,
2264 (size_t)(linelen - col - oldlen));
2265
2266 /* Insert or overwrite the new character. */
2267#ifdef FEAT_MBYTE
2268 mch_memmove(p, buf, charlen);
2269 i = charlen;
2270#else
2271 *p = c;
2272 i = 1;
2273#endif
2274
2275 /* Fill with spaces when necessary. */
2276 while (i < newlen)
2277 p[i++] = ' ';
2278
2279 /* Replace the line in the buffer. */
2280 ml_replace(lnum, newp, FALSE);
2281
2282 /* mark the buffer as changed and prepare for displaying */
2283 changed_bytes(lnum, col);
2284
2285 /*
2286 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2287 * show the match for right parens and braces.
2288 */
2289 if (p_sm && (State & INSERT)
2290 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002291#ifdef FEAT_INS_EXPAND
2292 && !ins_compl_active()
2293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002295 {
2296#ifdef FEAT_MBYTE
2297 if (has_mbyte)
2298 showmatch(mb_ptr2char(buf));
2299 else
2300#endif
2301 showmatch(c);
2302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304#ifdef FEAT_RIGHTLEFT
2305 if (!p_ri || (State & REPLACE_FLAG))
2306#endif
2307 {
2308 /* Normal insert: move cursor right */
2309#ifdef FEAT_MBYTE
2310 curwin->w_cursor.col += charlen;
2311#else
2312 ++curwin->w_cursor.col;
2313#endif
2314 }
2315 /*
2316 * TODO: should try to update w_row here, to avoid recomputing it later.
2317 */
2318}
2319
2320/*
2321 * Insert a string at the cursor position.
2322 * Note: Does NOT handle Replace mode.
2323 * Caller must have prepared for undo.
2324 */
2325 void
2326ins_str(s)
2327 char_u *s;
2328{
2329 char_u *oldp, *newp;
2330 int newlen = (int)STRLEN(s);
2331 int oldlen;
2332 colnr_T col;
2333 linenr_T lnum = curwin->w_cursor.lnum;
2334
2335#ifdef FEAT_VIRTUALEDIT
2336 if (virtual_active() && curwin->w_cursor.coladd > 0)
2337 coladvance_force(getviscol());
2338#endif
2339
2340 col = curwin->w_cursor.col;
2341 oldp = ml_get(lnum);
2342 oldlen = (int)STRLEN(oldp);
2343
2344 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2345 if (newp == NULL)
2346 return;
2347 if (col > 0)
2348 mch_memmove(newp, oldp, (size_t)col);
2349 mch_memmove(newp + col, s, (size_t)newlen);
2350 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2351 ml_replace(lnum, newp, FALSE);
2352 changed_bytes(lnum, col);
2353 curwin->w_cursor.col += newlen;
2354}
2355
2356/*
2357 * Delete one character under the cursor.
2358 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2359 * Caller must have prepared for undo.
2360 *
2361 * return FAIL for failure, OK otherwise
2362 */
2363 int
2364del_char(fixpos)
2365 int fixpos;
2366{
2367#ifdef FEAT_MBYTE
2368 if (has_mbyte)
2369 {
2370 /* Make sure the cursor is at the start of a character. */
2371 mb_adjust_cursor();
2372 if (*ml_get_cursor() == NUL)
2373 return FAIL;
2374 return del_chars(1L, fixpos);
2375 }
2376#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002377 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378}
2379
2380#if defined(FEAT_MBYTE) || defined(PROTO)
2381/*
2382 * Like del_bytes(), but delete characters instead of bytes.
2383 */
2384 int
2385del_chars(count, fixpos)
2386 long count;
2387 int fixpos;
2388{
2389 long bytes = 0;
2390 long i;
2391 char_u *p;
2392 int l;
2393
2394 p = ml_get_cursor();
2395 for (i = 0; i < count && *p != NUL; ++i)
2396 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002397 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 bytes += l;
2399 p += l;
2400 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002401 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402}
2403#endif
2404
2405/*
2406 * Delete "count" bytes under the cursor.
2407 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2408 * Caller must have prepared for undo.
2409 *
2410 * return FAIL for failure, OK otherwise
2411 */
2412 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002413del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002415 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002416 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418 char_u *oldp, *newp;
2419 colnr_T oldlen;
2420 linenr_T lnum = curwin->w_cursor.lnum;
2421 colnr_T col = curwin->w_cursor.col;
2422 int was_alloced;
2423 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002424 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 oldp = ml_get(lnum);
2427 oldlen = (int)STRLEN(oldp);
2428
2429 /*
2430 * Can't do anything when the cursor is on the NUL after the line.
2431 */
2432 if (col >= oldlen)
2433 return FAIL;
2434
2435#ifdef FEAT_MBYTE
2436 /* If 'delcombine' is set and deleting (less than) one character, only
2437 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002438 if (p_deco && use_delcombine && enc_utf8
2439 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002441 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 int n;
2443
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002444 (void)utfc_ptr2char(oldp + col, cc);
2445 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
2447 /* Find the last composing char, there can be several. */
2448 n = col;
2449 do
2450 {
2451 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002452 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 n += count;
2454 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2455 fixpos = 0;
2456 }
2457 }
2458#endif
2459
2460 /*
2461 * When count is too big, reduce it.
2462 */
2463 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2464 if (movelen <= 1)
2465 {
2466 /*
2467 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002468 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2469 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002471 if (col > 0 && fixpos && restart_edit == 0
2472#ifdef FEAT_VIRTUALEDIT
2473 && (ve_flags & VE_ONEMORE) == 0
2474#endif
2475 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
2477 --curwin->w_cursor.col;
2478#ifdef FEAT_VIRTUALEDIT
2479 curwin->w_cursor.coladd = 0;
2480#endif
2481#ifdef FEAT_MBYTE
2482 if (has_mbyte)
2483 curwin->w_cursor.col -=
2484 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2485#endif
2486 }
2487 count = oldlen - col;
2488 movelen = 1;
2489 }
2490
2491 /*
2492 * If the old line has been allocated the deletion can be done in the
2493 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002494 * Can't do this when using Netbeans, because we would need to invoke
2495 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002496 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002499 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002500 was_alloced = FALSE;
2501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002503 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (was_alloced)
2505 newp = oldp; /* use same allocated memory */
2506 else
2507 { /* need to allocate a new line */
2508 newp = alloc((unsigned)(oldlen + 1 - count));
2509 if (newp == NULL)
2510 return FAIL;
2511 mch_memmove(newp, oldp, (size_t)col);
2512 }
2513 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2514 if (!was_alloced)
2515 ml_replace(lnum, newp, FALSE);
2516
2517 /* mark the buffer as changed and prepare for displaying */
2518 changed_bytes(lnum, curwin->w_cursor.col);
2519
2520 return OK;
2521}
2522
2523/*
2524 * Delete from cursor to end of line.
2525 * Caller must have prepared for undo.
2526 *
2527 * return FAIL for failure, OK otherwise
2528 */
2529 int
2530truncate_line(fixpos)
2531 int fixpos; /* if TRUE fix the cursor position when done */
2532{
2533 char_u *newp;
2534 linenr_T lnum = curwin->w_cursor.lnum;
2535 colnr_T col = curwin->w_cursor.col;
2536
2537 if (col == 0)
2538 newp = vim_strsave((char_u *)"");
2539 else
2540 newp = vim_strnsave(ml_get(lnum), col);
2541
2542 if (newp == NULL)
2543 return FAIL;
2544
2545 ml_replace(lnum, newp, FALSE);
2546
2547 /* mark the buffer as changed and prepare for displaying */
2548 changed_bytes(lnum, curwin->w_cursor.col);
2549
2550 /*
2551 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2552 */
2553 if (fixpos && curwin->w_cursor.col > 0)
2554 --curwin->w_cursor.col;
2555
2556 return OK;
2557}
2558
2559/*
2560 * Delete "nlines" lines at the cursor.
2561 * Saves the lines for undo first if "undo" is TRUE.
2562 */
2563 void
2564del_lines(nlines, undo)
2565 long nlines; /* number of lines to delete */
2566 int undo; /* if TRUE, prepare for undo */
2567{
2568 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002569 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571 if (nlines <= 0)
2572 return;
2573
2574 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002575 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 return;
2577
2578 for (n = 0; n < nlines; )
2579 {
2580 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2581 break;
2582
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002583 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 ++n;
2585
2586 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002587 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 break;
2589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002591 /* Correct the cursor position before calling deleted_lines_mark(), it may
2592 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 curwin->w_cursor.col = 0;
2594 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002595
2596 /* adjust marks, mark the buffer as changed and prepare for displaying */
2597 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598}
2599
2600 int
2601gchar_pos(pos)
2602 pos_T *pos;
2603{
2604 char_u *ptr = ml_get_pos(pos);
2605
2606#ifdef FEAT_MBYTE
2607 if (has_mbyte)
2608 return (*mb_ptr2char)(ptr);
2609#endif
2610 return (int)*ptr;
2611}
2612
2613 int
2614gchar_cursor()
2615{
2616#ifdef FEAT_MBYTE
2617 if (has_mbyte)
2618 return (*mb_ptr2char)(ml_get_cursor());
2619#endif
2620 return (int)*ml_get_cursor();
2621}
2622
2623/*
2624 * Write a character at the current cursor position.
2625 * It is directly written into the block.
2626 */
2627 void
2628pchar_cursor(c)
2629 int c;
2630{
2631 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2632 + curwin->w_cursor.col) = c;
2633}
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635/*
2636 * When extra == 0: Return TRUE if the cursor is before or on the first
2637 * non-blank in the line.
2638 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2639 * the line.
2640 */
2641 int
2642inindent(extra)
2643 int extra;
2644{
2645 char_u *ptr;
2646 colnr_T col;
2647
2648 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2649 ++ptr;
2650 if (col >= curwin->w_cursor.col + extra)
2651 return TRUE;
2652 else
2653 return FALSE;
2654}
2655
2656/*
2657 * Skip to next part of an option argument: Skip space and comma.
2658 */
2659 char_u *
2660skip_to_option_part(p)
2661 char_u *p;
2662{
2663 if (*p == ',')
2664 ++p;
2665 while (*p == ' ')
2666 ++p;
2667 return p;
2668}
2669
2670/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002671 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 *
2673 * Most often called through changed_bytes() and changed_lines(), which also
2674 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002675 *
2676 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
2678 void
2679changed()
2680{
2681#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2682 /* The text of the preediting area is inserted, but this doesn't
2683 * mean a change of the buffer yet. That is delayed until the
2684 * text is committed. (this means preedit becomes empty) */
2685 if (im_is_preediting() && !xim_changed_while_preediting)
2686 return;
2687 xim_changed_while_preediting = FALSE;
2688#endif
2689
2690 if (!curbuf->b_changed)
2691 {
2692 int save_msg_scroll = msg_scroll;
2693
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002694 /* Give a warning about changing a read-only file. This may also
2695 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 /* Create a swap file if that is wanted.
2699 * Don't do this for "nofile" and "nowrite" buffer types. */
2700 if (curbuf->b_may_swap
2701#ifdef FEAT_QUICKFIX
2702 && !bt_dontwrite(curbuf)
2703#endif
2704 )
2705 {
2706 ml_open_file(curbuf);
2707
2708 /* The ml_open_file() can cause an ATTENTION message.
2709 * Wait two seconds, to make sure the user reads this unexpected
2710 * message. Since we could be anywhere, call wait_return() now,
2711 * and don't let the emsg() set msg_scroll. */
2712 if (need_wait_return && emsg_silent == 0)
2713 {
2714 out_flush();
2715 ui_delay(2000L, TRUE);
2716 wait_return(TRUE);
2717 msg_scroll = save_msg_scroll;
2718 }
2719 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002720 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723}
2724
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002725/*
2726 * Internal part of changed(), no user interaction.
2727 */
2728 void
2729changed_int()
2730{
2731 curbuf->b_changed = TRUE;
2732 ml_setflags(curbuf);
2733#ifdef FEAT_WINDOWS
2734 check_status(curbuf);
2735 redraw_tabline = TRUE;
2736#endif
2737#ifdef FEAT_TITLE
2738 need_maketitle = TRUE; /* set window title later */
2739#endif
2740}
2741
Bram Moolenaardba8a912005-04-24 22:08:39 +00002742static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2743static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2745
2746/*
2747 * Changed bytes within a single line for the current buffer.
2748 * - marks the windows on this buffer to be redisplayed
2749 * - marks the buffer changed by calling changed()
2750 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002751 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
2753 void
2754changed_bytes(lnum, col)
2755 linenr_T lnum;
2756 colnr_T col;
2757{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002758 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002760
2761#ifdef FEAT_DIFF
2762 /* Diff highlighting in other diff windows may need to be updated too. */
2763 if (curwin->w_p_diff)
2764 {
2765 win_T *wp;
2766 linenr_T wlnum;
2767
2768 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2769 if (wp->w_p_diff && wp != curwin)
2770 {
2771 redraw_win_later(wp, VALID);
2772 wlnum = diff_lnum_win(lnum, wp);
2773 if (wlnum > 0)
2774 changedOneline(wp->w_buffer, wlnum);
2775 }
2776 }
2777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778}
2779
2780 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002781changedOneline(buf, lnum)
2782 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 linenr_T lnum;
2784{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002785 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
2787 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002788 if (lnum < buf->b_mod_top)
2789 buf->b_mod_top = lnum;
2790 else if (lnum >= buf->b_mod_bot)
2791 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793 else
2794 {
2795 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002796 buf->b_mod_set = TRUE;
2797 buf->b_mod_top = lnum;
2798 buf->b_mod_bot = lnum + 1;
2799 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801}
2802
2803/*
2804 * Appended "count" lines below line "lnum" in the current buffer.
2805 * Must be called AFTER the change and after mark_adjust().
2806 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2807 */
2808 void
2809appended_lines(lnum, count)
2810 linenr_T lnum;
2811 long count;
2812{
2813 changed_lines(lnum + 1, 0, lnum + 1, count);
2814}
2815
2816/*
2817 * Like appended_lines(), but adjust marks first.
2818 */
2819 void
2820appended_lines_mark(lnum, count)
2821 linenr_T lnum;
2822 long count;
2823{
2824 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2825 changed_lines(lnum + 1, 0, lnum + 1, count);
2826}
2827
2828/*
2829 * Deleted "count" lines at line "lnum" in the current buffer.
2830 * Must be called AFTER the change and after mark_adjust().
2831 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2832 */
2833 void
2834deleted_lines(lnum, count)
2835 linenr_T lnum;
2836 long count;
2837{
2838 changed_lines(lnum, 0, lnum + count, -count);
2839}
2840
2841/*
2842 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002843 * Make sure the cursor is on a valid line before calling, a GUI callback may
2844 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 */
2846 void
2847deleted_lines_mark(lnum, count)
2848 linenr_T lnum;
2849 long count;
2850{
2851 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2852 changed_lines(lnum, 0, lnum + count, -count);
2853}
2854
2855/*
2856 * Changed lines for the current buffer.
2857 * Must be called AFTER the change and after mark_adjust().
2858 * - mark the buffer changed by calling changed()
2859 * - mark the windows on this buffer to be redisplayed
2860 * - invalidate cached values
2861 * "lnum" is the first line that needs displaying, "lnume" the first line
2862 * below the changed lines (BEFORE the change).
2863 * When only inserting lines, "lnum" and "lnume" are equal.
2864 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002865 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 */
2867 void
2868changed_lines(lnum, col, lnume, xtra)
2869 linenr_T lnum; /* first line with change */
2870 colnr_T col; /* column in first line with change */
2871 linenr_T lnume; /* line below last changed line */
2872 long xtra; /* number of extra lines (negative when deleting) */
2873{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002874 changed_lines_buf(curbuf, lnum, lnume, xtra);
2875
2876#ifdef FEAT_DIFF
2877 if (xtra == 0 && curwin->w_p_diff)
2878 {
2879 /* When the number of lines doesn't change then mark_adjust() isn't
2880 * called and other diff buffers still need to be marked for
2881 * displaying. */
2882 win_T *wp;
2883 linenr_T wlnum;
2884
2885 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2886 if (wp->w_p_diff && wp != curwin)
2887 {
2888 redraw_win_later(wp, VALID);
2889 wlnum = diff_lnum_win(lnum, wp);
2890 if (wlnum > 0)
2891 changed_lines_buf(wp->w_buffer, wlnum,
2892 lnume - lnum + wlnum, 0L);
2893 }
2894 }
2895#endif
2896
2897 changed_common(lnum, col, lnume, xtra);
2898}
2899
2900 static void
2901changed_lines_buf(buf, lnum, lnume, xtra)
2902 buf_T *buf;
2903 linenr_T lnum; /* first line with change */
2904 linenr_T lnume; /* line below last changed line */
2905 long xtra; /* number of extra lines (negative when deleting) */
2906{
2907 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
2909 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002910 if (lnum < buf->b_mod_top)
2911 buf->b_mod_top = lnum;
2912 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
2914 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002915 buf->b_mod_bot += xtra;
2916 if (buf->b_mod_bot < lnum)
2917 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002919 if (lnume + xtra > buf->b_mod_bot)
2920 buf->b_mod_bot = lnume + xtra;
2921 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 }
2923 else
2924 {
2925 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002926 buf->b_mod_set = TRUE;
2927 buf->b_mod_top = lnum;
2928 buf->b_mod_bot = lnume + xtra;
2929 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931}
2932
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002933/*
2934 * Common code for when a change is was made.
2935 * See changed_lines() for the arguments.
2936 * Careful: may trigger autocommands that reload the buffer.
2937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 static void
2939changed_common(lnum, col, lnume, xtra)
2940 linenr_T lnum;
2941 colnr_T col;
2942 linenr_T lnume;
2943 long xtra;
2944{
2945 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002946#ifdef FEAT_WINDOWS
2947 tabpage_T *tp;
2948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 int i;
2950#ifdef FEAT_JUMPLIST
2951 int cols;
2952 pos_T *p;
2953 int add;
2954#endif
2955
2956 /* mark the buffer as modified */
2957 changed();
2958
2959 /* set the '. mark */
2960 if (!cmdmod.keepjumps)
2961 {
2962 curbuf->b_last_change.lnum = lnum;
2963 curbuf->b_last_change.col = col;
2964
2965#ifdef FEAT_JUMPLIST
2966 /* Create a new entry if a new undo-able change was started or we
2967 * don't have an entry yet. */
2968 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
2969 {
2970 if (curbuf->b_changelistlen == 0)
2971 add = TRUE;
2972 else
2973 {
2974 /* Don't create a new entry when the line number is the same
2975 * as the last one and the column is not too far away. Avoids
2976 * creating many entries for typing "xxxxx". */
2977 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
2978 if (p->lnum != lnum)
2979 add = TRUE;
2980 else
2981 {
2982 cols = comp_textwidth(FALSE);
2983 if (cols == 0)
2984 cols = 79;
2985 add = (p->col + cols < col || col + cols < p->col);
2986 }
2987 }
2988 if (add)
2989 {
2990 /* This is the first of a new sequence of undo-able changes
2991 * and it's at some distance of the last change. Use a new
2992 * position in the changelist. */
2993 curbuf->b_new_change = FALSE;
2994
2995 if (curbuf->b_changelistlen == JUMPLISTSIZE)
2996 {
2997 /* changelist is full: remove oldest entry */
2998 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
2999 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3000 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003001 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 {
3003 /* Correct position in changelist for other windows on
3004 * this buffer. */
3005 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3006 --wp->w_changelistidx;
3007 }
3008 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003009 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 {
3011 /* For other windows, if the position in the changelist is
3012 * at the end it stays at the end. */
3013 if (wp->w_buffer == curbuf
3014 && wp->w_changelistidx == curbuf->b_changelistlen)
3015 ++wp->w_changelistidx;
3016 }
3017 ++curbuf->b_changelistlen;
3018 }
3019 }
3020 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3021 curbuf->b_last_change;
3022 /* The current window is always after the last change, so that "g,"
3023 * takes you back to it. */
3024 curwin->w_changelistidx = curbuf->b_changelistlen;
3025#endif
3026 }
3027
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003028 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 if (wp->w_buffer == curbuf)
3031 {
3032 /* Mark this window to be redrawn later. */
3033 if (wp->w_redr_type < VALID)
3034 wp->w_redr_type = VALID;
3035
3036 /* Check if a change in the buffer has invalidated the cached
3037 * values for the cursor. */
3038#ifdef FEAT_FOLDING
3039 /*
3040 * Update the folds for this window. Can't postpone this, because
3041 * a following operator might work on the whole fold: ">>dd".
3042 */
3043 foldUpdate(wp, lnum, lnume + xtra - 1);
3044
3045 /* The change may cause lines above or below the change to become
3046 * included in a fold. Set lnum/lnume to the first/last line that
3047 * might be displayed differently.
3048 * Set w_cline_folded here as an efficient way to update it when
3049 * inserting lines just above a closed fold. */
3050 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3051 if (wp->w_cursor.lnum == lnum)
3052 wp->w_cline_folded = i;
3053 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3054 if (wp->w_cursor.lnum == lnume)
3055 wp->w_cline_folded = i;
3056
3057 /* If the changed line is in a range of previously folded lines,
3058 * compare with the first line in that range. */
3059 if (wp->w_cursor.lnum <= lnum)
3060 {
3061 i = find_wl_entry(wp, lnum);
3062 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3063 changed_line_abv_curs_win(wp);
3064 }
3065#endif
3066
3067 if (wp->w_cursor.lnum > lnum)
3068 changed_line_abv_curs_win(wp);
3069 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3070 changed_cline_bef_curs_win(wp);
3071 if (wp->w_botline >= lnum)
3072 {
3073 /* Assume that botline doesn't change (inserted lines make
3074 * other lines scroll down below botline). */
3075 approximate_botline_win(wp);
3076 }
3077
3078 /* Check if any w_lines[] entries have become invalid.
3079 * For entries below the change: Correct the lnums for
3080 * inserted/deleted lines. Makes it possible to stop displaying
3081 * after the change. */
3082 for (i = 0; i < wp->w_lines_valid; ++i)
3083 if (wp->w_lines[i].wl_valid)
3084 {
3085 if (wp->w_lines[i].wl_lnum >= lnum)
3086 {
3087 if (wp->w_lines[i].wl_lnum < lnume)
3088 {
3089 /* line included in change */
3090 wp->w_lines[i].wl_valid = FALSE;
3091 }
3092 else if (xtra != 0)
3093 {
3094 /* line below change */
3095 wp->w_lines[i].wl_lnum += xtra;
3096#ifdef FEAT_FOLDING
3097 wp->w_lines[i].wl_lastlnum += xtra;
3098#endif
3099 }
3100 }
3101#ifdef FEAT_FOLDING
3102 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3103 {
3104 /* change somewhere inside this range of folded lines,
3105 * may need to be redrawn */
3106 wp->w_lines[i].wl_valid = FALSE;
3107 }
3108#endif
3109 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003110
3111#ifdef FEAT_FOLDING
3112 /* Take care of side effects for setting w_topline when folds have
3113 * changed. Esp. when the buffer was changed in another window. */
3114 if (hasAnyFolding(wp))
3115 set_topline(wp, wp->w_topline);
3116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
3118 }
3119
3120 /* Call update_screen() later, which checks out what needs to be redrawn,
3121 * since it notices b_mod_set and then uses b_mod_*. */
3122 if (must_redraw < VALID)
3123 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003124
3125#ifdef FEAT_AUTOCMD
3126 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003127 if (lnum <= curwin->w_cursor.lnum
3128 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003129 last_cursormoved.lnum = 0;
3130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131}
3132
3133/*
3134 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3135 */
3136 void
3137unchanged(buf, ff)
3138 buf_T *buf;
3139 int ff; /* also reset 'fileformat' */
3140{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003141 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
3143 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003144 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (ff)
3146 save_file_ff(buf);
3147#ifdef FEAT_WINDOWS
3148 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003149 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#endif
3151#ifdef FEAT_TITLE
3152 need_maketitle = TRUE; /* set window title later */
3153#endif
3154 }
3155 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156#ifdef FEAT_NETBEANS_INTG
3157 netbeans_unmodified(buf);
3158#endif
3159}
3160
3161#if defined(FEAT_WINDOWS) || defined(PROTO)
3162/*
3163 * check_status: called when the status bars for the buffer 'buf'
3164 * need to be updated
3165 */
3166 void
3167check_status(buf)
3168 buf_T *buf;
3169{
3170 win_T *wp;
3171
3172 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3173 if (wp->w_buffer == buf && wp->w_status_height)
3174 {
3175 wp->w_redr_status = TRUE;
3176 if (must_redraw < VALID)
3177 must_redraw = VALID;
3178 }
3179}
3180#endif
3181
3182/*
3183 * If the file is readonly, give a warning message with the first change.
3184 * Don't do this for autocommands.
3185 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003186 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003188 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 */
3190 void
3191change_warning(col)
3192 int col; /* column for message; non-zero when in insert
3193 mode and 'showmode' is on */
3194{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003195 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (curbuf->b_did_warn == FALSE
3198 && curbufIsChanged() == 0
3199#ifdef FEAT_AUTOCMD
3200 && !autocmd_busy
3201#endif
3202 && curbuf->b_p_ro)
3203 {
3204#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003205 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003207 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (!curbuf->b_p_ro)
3209 return;
3210#endif
3211 /*
3212 * Do what msg() does, but with a column offset if the warning should
3213 * be after the mode message.
3214 */
3215 msg_start();
3216 if (msg_row == Rows - 1)
3217 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003218 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003219 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3220#ifdef FEAT_EVAL
3221 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 msg_clr_eos();
3224 (void)msg_end();
3225 if (msg_silent == 0 && !silent_mode)
3226 {
3227 out_flush();
3228 ui_delay(1000L, TRUE); /* give the user time to think about it */
3229 }
3230 curbuf->b_did_warn = TRUE;
3231 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3232 if (msg_row < Rows - 1)
3233 showmode();
3234 }
3235}
3236
3237/*
3238 * Ask for a reply from the user, a 'y' or a 'n'.
3239 * No other characters are accepted, the message is repeated until a valid
3240 * reply is entered or CTRL-C is hit.
3241 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3242 * from any buffers but directly from the user.
3243 *
3244 * return the 'y' or 'n'
3245 */
3246 int
3247ask_yesno(str, direct)
3248 char_u *str;
3249 int direct;
3250{
3251 int r = ' ';
3252 int save_State = State;
3253
3254 if (exiting) /* put terminal in raw mode for this question */
3255 settmode(TMODE_RAW);
3256 ++no_wait_return;
3257#ifdef USE_ON_FLY_SCROLL
3258 dont_scroll = TRUE; /* disallow scrolling here */
3259#endif
3260 State = CONFIRM; /* mouse behaves like with :confirm */
3261#ifdef FEAT_MOUSE
3262 setmouse(); /* disables mouse for xterm */
3263#endif
3264 ++no_mapping;
3265 ++allow_keys; /* no mapping here, but recognize keys */
3266
3267 while (r != 'y' && r != 'n')
3268 {
3269 /* same highlighting as for wait_return */
3270 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3271 if (direct)
3272 r = get_keystroke();
3273 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003274 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 if (r == Ctrl_C || r == ESC)
3276 r = 'n';
3277 msg_putchar(r); /* show what you typed */
3278 out_flush();
3279 }
3280 --no_wait_return;
3281 State = save_State;
3282#ifdef FEAT_MOUSE
3283 setmouse();
3284#endif
3285 --no_mapping;
3286 --allow_keys;
3287
3288 return r;
3289}
3290
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003291#if defined(FEAT_MOUSE) || defined(PROTO)
3292/*
3293 * Return TRUE if "c" is a mouse key.
3294 */
3295 int
3296is_mouse_key(c)
3297 int c;
3298{
3299 return c == K_LEFTMOUSE
3300 || c == K_LEFTMOUSE_NM
3301 || c == K_LEFTDRAG
3302 || c == K_LEFTRELEASE
3303 || c == K_LEFTRELEASE_NM
3304 || c == K_MIDDLEMOUSE
3305 || c == K_MIDDLEDRAG
3306 || c == K_MIDDLERELEASE
3307 || c == K_RIGHTMOUSE
3308 || c == K_RIGHTDRAG
3309 || c == K_RIGHTRELEASE
3310 || c == K_MOUSEDOWN
3311 || c == K_MOUSEUP
3312 || c == K_MOUSELEFT
3313 || c == K_MOUSERIGHT
3314 || c == K_X1MOUSE
3315 || c == K_X1DRAG
3316 || c == K_X1RELEASE
3317 || c == K_X2MOUSE
3318 || c == K_X2DRAG
3319 || c == K_X2RELEASE;
3320}
3321#endif
3322
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323/*
3324 * Get a key stroke directly from the user.
3325 * Ignores mouse clicks and scrollbar events, except a click for the left
3326 * button (used at the more prompt).
3327 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3328 * Disadvantage: typeahead is ignored.
3329 * Translates the interrupt character for unix to ESC.
3330 */
3331 int
3332get_keystroke()
3333{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003334 char_u *buf = NULL;
3335 int buflen = 150;
3336 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 int len = 0;
3338 int n;
3339 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003340 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
3342 mapped_ctrl_c = FALSE; /* mappings are not used here */
3343 for (;;)
3344 {
3345 cursor_on();
3346 out_flush();
3347
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003348 /* Leave some room for check_termcode() to insert a key code into (max
3349 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3350 * bytes. */
3351 maxlen = (buflen - 6 - len) / 3;
3352 if (buf == NULL)
3353 buf = alloc(buflen);
3354 else if (maxlen < 10)
3355 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003356 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003357 * escape sequence. */
3358 buflen += 100;
3359 buf = vim_realloc(buf, buflen);
3360 maxlen = (buflen - 6 - len) / 3;
3361 }
3362 if (buf == NULL)
3363 {
3364 do_outofmem_msg((long_u)buflen);
3365 return ESC; /* panic! */
3366 }
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003369 * terminal code to complete. */
3370 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (n > 0)
3372 {
3373 /* Replace zero and CSI by a special key code. */
3374 n = fix_input_buffer(buf + len, n, FALSE);
3375 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003376 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003378 else if (len > 0)
3379 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar4395a712006-09-05 18:57:57 +00003381 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003382 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003383 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003385
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003386 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003387 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003388 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003389 {
3390 /* Redrawing was postponed, do it now. */
3391 update_screen(0);
3392 setcursor(); /* put cursor back where it belongs */
3393 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003394 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003395 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003396 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003398 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 continue;
3400
3401 /* Handle modifier and/or special key code. */
3402 n = buf[0];
3403 if (n == K_SPECIAL)
3404 {
3405 n = TO_SPECIAL(buf[1], buf[2]);
3406 if (buf[1] == KS_MODIFIER
3407 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003408#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003409 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003410#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003411#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 || n == K_VER_SCROLLBAR
3413 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#endif
3415 )
3416 {
3417 if (buf[1] == KS_MODIFIER)
3418 mod_mask = buf[2];
3419 len -= 3;
3420 if (len > 0)
3421 mch_memmove(buf, buf + 3, (size_t)len);
3422 continue;
3423 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003424 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426#ifdef FEAT_MBYTE
3427 if (has_mbyte)
3428 {
3429 if (MB_BYTE2LEN(n) > len)
3430 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003431 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 n = (*mb_ptr2char)(buf);
3433 }
3434#endif
3435#ifdef UNIX
3436 if (n == intr_char)
3437 n = ESC;
3438#endif
3439 break;
3440 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003441 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 mapped_ctrl_c = save_mapped_ctrl_c;
3444 return n;
3445}
3446
3447/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003448 * Get a number from the user.
3449 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
3451 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003452get_number(colon, mouse_used)
3453 int colon; /* allow colon to abort */
3454 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 int n = 0;
3457 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003458 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003460 if (mouse_used != NULL)
3461 *mouse_used = FALSE;
3462
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 /* When not printing messages, the user won't know what to type, return a
3464 * zero (as if CR was hit). */
3465 if (msg_silent != 0)
3466 return 0;
3467
3468#ifdef USE_ON_FLY_SCROLL
3469 dont_scroll = TRUE; /* disallow scrolling here */
3470#endif
3471 ++no_mapping;
3472 ++allow_keys; /* no mapping here, but recognize keys */
3473 for (;;)
3474 {
3475 windgoto(msg_row, msg_col);
3476 c = safe_vgetc();
3477 if (VIM_ISDIGIT(c))
3478 {
3479 n = n * 10 + c - '0';
3480 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003481 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
3483 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3484 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003485 if (typed > 0)
3486 {
3487 MSG_PUTS("\b \b");
3488 --typed;
3489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003492#ifdef FEAT_MOUSE
3493 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3494 {
3495 *mouse_used = TRUE;
3496 n = mouse_row + 1;
3497 break;
3498 }
3499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 else if (n == 0 && c == ':' && colon)
3501 {
3502 stuffcharReadbuff(':');
3503 if (!exmode_active)
3504 cmdline_row = msg_row;
3505 skip_redraw = TRUE; /* skip redraw once */
3506 do_redraw = FALSE;
3507 break;
3508 }
3509 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3510 break;
3511 }
3512 --no_mapping;
3513 --allow_keys;
3514 return n;
3515}
3516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517/*
3518 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003519 * When "mouse_used" is not NULL allow using the mouse and in that case return
3520 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003521 */
3522 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003523prompt_for_number(mouse_used)
3524 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003525{
3526 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003527 int save_cmdline_row;
3528 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003529
3530 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003531 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003532 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003533 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003534 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003535
Bram Moolenaar203335e2006-09-03 14:35:42 +00003536 /* Set the state such that text can be selected/copied/pasted and we still
3537 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003538 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003539 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003540 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003541 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003542
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003543 i = get_number(TRUE, mouse_used);
3544 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003545 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003546 /* don't call wait_return() now */
3547 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003548 cmdline_row = msg_row - 1;
3549 need_wait_return = FALSE;
3550 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003551 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003552 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003553 else
3554 cmdline_row = save_cmdline_row;
3555 State = save_State;
3556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003557 return i;
3558}
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 void
3561msgmore(n)
3562 long n;
3563{
3564 long pn;
3565
3566 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3568 return;
3569
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003570 /* We don't want to overwrite another important message, but do overwrite
3571 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3572 * then "put" reports the last action. */
3573 if (keep_msg != NULL && !keep_msg_more)
3574 return;
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (n > 0)
3577 pn = n;
3578 else
3579 pn = -n;
3580
3581 if (pn > p_report)
3582 {
3583 if (pn == 1)
3584 {
3585 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003586 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3587 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003589 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3590 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592 else
3593 {
3594 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003595 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3596 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003598 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3599 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003602 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (msg(msg_buf))
3604 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003605 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003606 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 }
3609}
3610
3611/*
3612 * flush map and typeahead buffers and give a warning for an error
3613 */
3614 void
3615beep_flush()
3616{
3617 if (emsg_silent == 0)
3618 {
3619 flush_buffers(FALSE);
3620 vim_beep();
3621 }
3622}
3623
3624/*
3625 * give a warning for an error
3626 */
3627 void
3628vim_beep()
3629{
3630 if (emsg_silent == 0)
3631 {
3632 if (p_vb
3633#ifdef FEAT_GUI
3634 /* While the GUI is starting up the termcap is set for the GUI
3635 * but the output still goes to a terminal. */
3636 && !(gui.in_use && gui.starting)
3637#endif
3638 )
3639 {
3640 out_str(T_VB);
3641 }
3642 else
3643 {
3644#ifdef MSDOS
3645 /*
3646 * The number of beeps outputted is reduced to avoid having to wait
3647 * for all the beeps to finish. This is only a problem on systems
3648 * where the beeps don't overlap.
3649 */
3650 if (beep_count == 0 || beep_count == 10)
3651 {
3652 out_char(BELL);
3653 beep_count = 1;
3654 }
3655 else
3656 ++beep_count;
3657#else
3658 out_char(BELL);
3659#endif
3660 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003661
3662 /* When 'verbose' is set and we are sourcing a script or executing a
3663 * function give the user a hint where the beep comes from. */
3664 if (vim_strchr(p_debug, 'e') != NULL)
3665 {
3666 msg_source(hl_attr(HLF_W));
3667 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670}
3671
3672/*
3673 * To get the "real" home directory:
3674 * - get value of $HOME
3675 * For Unix:
3676 * - go to that directory
3677 * - do mch_dirname() to get the real name of that directory.
3678 * This also works with mounts and links.
3679 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3680 */
3681static char_u *homedir = NULL;
3682
3683 void
3684init_homedir()
3685{
3686 char_u *var;
3687
Bram Moolenaar05159a02005-02-26 23:04:13 +00003688 /* In case we are called a second time (when 'encoding' changes). */
3689 vim_free(homedir);
3690 homedir = NULL;
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#ifdef VMS
3693 var = mch_getenv((char_u *)"SYS$LOGIN");
3694#else
3695 var = mch_getenv((char_u *)"HOME");
3696#endif
3697
3698 if (var != NULL && *var == NUL) /* empty is same as not set */
3699 var = NULL;
3700
3701#ifdef WIN3264
3702 /*
3703 * Weird but true: $HOME may contain an indirect reference to another
3704 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3705 * when $HOME is being set.
3706 */
3707 if (var != NULL && *var == '%')
3708 {
3709 char_u *p;
3710 char_u *exp;
3711
3712 p = vim_strchr(var + 1, '%');
3713 if (p != NULL)
3714 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003715 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 exp = mch_getenv(NameBuff);
3717 if (exp != NULL && *exp != NUL
3718 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3719 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003720 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 var = NameBuff;
3722 /* Also set $HOME, it's needed for _viminfo. */
3723 vim_setenv((char_u *)"HOME", NameBuff);
3724 }
3725 }
3726 }
3727
3728 /*
3729 * Typically, $HOME is not defined on Windows, unless the user has
3730 * specifically defined it for Vim's sake. However, on Windows NT
3731 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3732 * each user. Try constructing $HOME from these.
3733 */
3734 if (var == NULL)
3735 {
3736 char_u *homedrive, *homepath;
3737
3738 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3739 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003740 if (homepath == NULL || *homepath == NUL)
3741 homepath = "\\";
3742 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3744 {
3745 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3746 if (NameBuff[0] != NUL)
3747 {
3748 var = NameBuff;
3749 /* Also set $HOME, it's needed for _viminfo. */
3750 vim_setenv((char_u *)"HOME", NameBuff);
3751 }
3752 }
3753 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003754
3755# if defined(FEAT_MBYTE)
3756 if (enc_utf8 && var != NULL)
3757 {
3758 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003759 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003760
3761 /* Convert from active codepage to UTF-8. Other conversions are
3762 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003763 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003764 if (pp != NULL)
3765 {
3766 homedir = pp;
3767 return;
3768 }
3769 }
3770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771#endif
3772
3773#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3774 /*
3775 * Default home dir is C:/
3776 * Best assumption we can make in such a situation.
3777 */
3778 if (var == NULL)
3779 var = "C:/";
3780#endif
3781 if (var != NULL)
3782 {
3783#ifdef UNIX
3784 /*
3785 * Change to the directory and get the actual path. This resolves
3786 * links. Don't do it when we can't return.
3787 */
3788 if (mch_dirname(NameBuff, MAXPATHL) == OK
3789 && mch_chdir((char *)NameBuff) == 0)
3790 {
3791 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3792 var = IObuff;
3793 if (mch_chdir((char *)NameBuff) != 0)
3794 EMSG(_(e_prev_dir));
3795 }
3796#endif
3797 homedir = vim_strsave(var);
3798 }
3799}
3800
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003801#if defined(EXITFREE) || defined(PROTO)
3802 void
3803free_homedir()
3804{
3805 vim_free(homedir);
3806}
Bram Moolenaar24305862012-08-15 14:05:05 +02003807
3808# ifdef FEAT_CMDL_COMPL
3809 void
3810free_users()
3811{
3812 ga_clear_strings(&ga_users);
3813}
3814# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003815#endif
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003818 * Call expand_env() and store the result in an allocated string.
3819 * This is not very memory efficient, this expects the result to be freed
3820 * again soon.
3821 */
3822 char_u *
3823expand_env_save(src)
3824 char_u *src;
3825{
3826 return expand_env_save_opt(src, FALSE);
3827}
3828
3829/*
3830 * Idem, but when "one" is TRUE handle the string as one file name, only
3831 * expand "~" at the start.
3832 */
3833 char_u *
3834expand_env_save_opt(src, one)
3835 char_u *src;
3836 int one;
3837{
3838 char_u *p;
3839
3840 p = alloc(MAXPATHL);
3841 if (p != NULL)
3842 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3843 return p;
3844}
3845
3846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 * Expand environment variable with path name.
3848 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003849 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 * If anything fails no expansion is done and dst equals src.
3851 */
3852 void
3853expand_env(src, dst, dstlen)
3854 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3855 char_u *dst; /* where to put the result */
3856 int dstlen; /* maximum length of the result */
3857{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003858 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859}
3860
3861 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003862expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003863 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 char_u *dst; /* where to put the result */
3865 int dstlen; /* maximum length of the result */
3866 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003867 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003868 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003870 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 char_u *tail;
3872 int c;
3873 char_u *var;
3874 int copy_char;
3875 int mustfree; /* var was allocated, need to free it later */
3876 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003877 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003879 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003880 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003881
3882 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 --dstlen; /* leave one char space for "\," */
3884 while (*src && dstlen > 0)
3885 {
3886 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003887 if ((*src == '$'
3888#ifdef VMS
3889 && at_start
3890#endif
3891 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3893 || *src == '%'
3894#endif
3895 || (*src == '~' && at_start))
3896 {
3897 mustfree = FALSE;
3898
3899 /*
3900 * The variable name is copied into dst temporarily, because it may
3901 * be a string in read-only memory and a NUL needs to be appended.
3902 */
3903 if (*src != '~') /* environment var */
3904 {
3905 tail = src + 1;
3906 var = dst;
3907 c = dstlen - 1;
3908
3909#ifdef UNIX
3910 /* Unix has ${var-name} type environment vars */
3911 if (*tail == '{' && !vim_isIDc('{'))
3912 {
3913 tail++; /* ignore '{' */
3914 while (c-- > 0 && *tail && *tail != '}')
3915 *var++ = *tail++;
3916 }
3917 else
3918#endif
3919 {
3920 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3921#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3922 || (*src == '%' && *tail != '%')
3923#endif
3924 ))
3925 {
3926#ifdef OS2 /* env vars only in uppercase */
3927 *var++ = TOUPPER_LOC(*tail);
3928 tail++; /* toupper() may be a macro! */
3929#else
3930 *var++ = *tail++;
3931#endif
3932 }
3933 }
3934
3935#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3936# ifdef UNIX
3937 if (src[1] == '{' && *tail != '}')
3938# else
3939 if (*src == '%' && *tail != '%')
3940# endif
3941 var = NULL;
3942 else
3943 {
3944# ifdef UNIX
3945 if (src[1] == '{')
3946# else
3947 if (*src == '%')
3948#endif
3949 ++tail;
3950#endif
3951 *var = NUL;
3952 var = vim_getenv(dst, &mustfree);
3953#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3954 }
3955#endif
3956 }
3957 /* home directory */
3958 else if ( src[1] == NUL
3959 || vim_ispathsep(src[1])
3960 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3961 {
3962 var = homedir;
3963 tail = src + 1;
3964 }
3965 else /* user directory */
3966 {
3967#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
3968 /*
3969 * Copy ~user to dst[], so we can put a NUL after it.
3970 */
3971 tail = src;
3972 var = dst;
3973 c = dstlen - 1;
3974 while ( c-- > 0
3975 && *tail
3976 && vim_isfilec(*tail)
3977 && !vim_ispathsep(*tail))
3978 *var++ = *tail++;
3979 *var = NUL;
3980# ifdef UNIX
3981 /*
3982 * If the system supports getpwnam(), use it.
3983 * Otherwise, or if getpwnam() fails, the shell is used to
3984 * expand ~user. This is slower and may fail if the shell
3985 * does not support ~user (old versions of /bin/sh).
3986 */
3987# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
3988 {
3989 struct passwd *pw;
3990
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003991 /* Note: memory allocated by getpwnam() is never freed.
3992 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 pw = getpwnam((char *)dst + 1);
3994 if (pw != NULL)
3995 var = (char_u *)pw->pw_dir;
3996 else
3997 var = NULL;
3998 }
3999 if (var == NULL)
4000# endif
4001 {
4002 expand_T xpc;
4003
4004 ExpandInit(&xpc);
4005 xpc.xp_context = EXPAND_FILES;
4006 var = ExpandOne(&xpc, dst, NULL,
4007 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 mustfree = TRUE;
4009 }
4010
4011# else /* !UNIX, thus VMS */
4012 /*
4013 * USER_HOME is a comma-separated list of
4014 * directories to search for the user account in.
4015 */
4016 {
4017 char_u test[MAXPATHL], paths[MAXPATHL];
4018 char_u *path, *next_path, *ptr;
4019 struct stat st;
4020
4021 STRCPY(paths, USER_HOME);
4022 next_path = paths;
4023 while (*next_path)
4024 {
4025 for (path = next_path; *next_path && *next_path != ',';
4026 next_path++);
4027 if (*next_path)
4028 *next_path++ = NUL;
4029 STRCPY(test, path);
4030 STRCAT(test, "/");
4031 STRCAT(test, dst + 1);
4032 if (mch_stat(test, &st) == 0)
4033 {
4034 var = alloc(STRLEN(test) + 1);
4035 STRCPY(var, test);
4036 mustfree = TRUE;
4037 break;
4038 }
4039 }
4040 }
4041# endif /* UNIX */
4042#else
4043 /* cannot expand user's home directory, so don't try */
4044 var = NULL;
4045 tail = (char_u *)""; /* for gcc */
4046#endif /* UNIX || VMS */
4047 }
4048
4049#ifdef BACKSLASH_IN_FILENAME
4050 /* If 'shellslash' is set change backslashes to forward slashes.
4051 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4052 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4053 {
4054 char_u *p = vim_strsave(var);
4055
4056 if (p != NULL)
4057 {
4058 if (mustfree)
4059 vim_free(var);
4060 var = p;
4061 mustfree = TRUE;
4062 forward_slash(var);
4063 }
4064 }
4065#endif
4066
4067 /* If "var" contains white space, escape it with a backslash.
4068 * Required for ":e ~/tt" when $HOME includes a space. */
4069 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4070 {
4071 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4072
4073 if (p != NULL)
4074 {
4075 if (mustfree)
4076 vim_free(var);
4077 var = p;
4078 mustfree = TRUE;
4079 }
4080 }
4081
4082 if (var != NULL && *var != NUL
4083 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4084 {
4085 STRCPY(dst, var);
4086 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004087 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 /* if var[] ends in a path separator and tail[] starts
4089 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004090 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4092 && dst[-1] != ':'
4093#endif
4094 && vim_ispathsep(*tail))
4095 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004096 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 src = tail;
4098 copy_char = FALSE;
4099 }
4100 if (mustfree)
4101 vim_free(var);
4102 }
4103
4104 if (copy_char) /* copy at least one char */
4105 {
4106 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004107 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004108 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4109 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 */
4111 at_start = FALSE;
4112 if (src[0] == '\\' && src[1] != NUL)
4113 {
4114 *dst++ = *src++;
4115 --dstlen;
4116 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004117 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 at_start = TRUE;
4119 *dst++ = *src++;
4120 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004121
4122 if (startstr != NULL && src - startstr_len >= srcp
4123 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4124 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 }
4127 *dst = NUL;
4128}
4129
4130/*
4131 * Vim's version of getenv().
4132 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004133 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004134 * "mustfree" is set to TRUE when returned is allocated, it must be
4135 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 */
4137 char_u *
4138vim_getenv(name, mustfree)
4139 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004140 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
4142 char_u *p;
4143 char_u *pend;
4144 int vimruntime;
4145
4146#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4147 /* use "C:/" when $HOME is not set */
4148 if (STRCMP(name, "HOME") == 0)
4149 return homedir;
4150#endif
4151
4152 p = mch_getenv(name);
4153 if (p != NULL && *p == NUL) /* empty is the same as not set */
4154 p = NULL;
4155
4156 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004157 {
4158#if defined(FEAT_MBYTE) && defined(WIN3264)
4159 if (enc_utf8)
4160 {
4161 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004162 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004163
4164 /* Convert from active codepage to UTF-8. Other conversions are
4165 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004166 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004167 if (pp != NULL)
4168 {
4169 p = pp;
4170 *mustfree = TRUE;
4171 }
4172 }
4173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4178 if (!vimruntime && STRCMP(name, "VIM") != 0)
4179 return NULL;
4180
4181 /*
4182 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4183 * Don't do this when default_vimruntime_dir is non-empty.
4184 */
4185 if (vimruntime
4186#ifdef HAVE_PATHDEF
4187 && *default_vimruntime_dir == NUL
4188#endif
4189 )
4190 {
4191 p = mch_getenv((char_u *)"VIM");
4192 if (p != NULL && *p == NUL) /* empty is the same as not set */
4193 p = NULL;
4194 if (p != NULL)
4195 {
4196 p = vim_version_dir(p);
4197 if (p != NULL)
4198 *mustfree = TRUE;
4199 else
4200 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004201
4202#if defined(FEAT_MBYTE) && defined(WIN3264)
4203 if (enc_utf8)
4204 {
4205 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004206 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004207
4208 /* Convert from active codepage to UTF-8. Other conversions
4209 * are not done, because they would fail for non-ASCII
4210 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004211 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212 if (pp != NULL)
4213 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004214 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215 vim_free(p);
4216 p = pp;
4217 *mustfree = TRUE;
4218 }
4219 }
4220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 }
4223
4224 /*
4225 * When expanding $VIM or $VIMRUNTIME fails, try using:
4226 * - the directory name from 'helpfile' (unless it contains '$')
4227 * - the executable name from argv[0]
4228 */
4229 if (p == NULL)
4230 {
4231 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4232 p = p_hf;
4233#ifdef USE_EXE_NAME
4234 /*
4235 * Use the name of the executable, obtained from argv[0].
4236 */
4237 else
4238 p = exe_name;
4239#endif
4240 if (p != NULL)
4241 {
4242 /* remove the file name */
4243 pend = gettail(p);
4244
4245 /* remove "doc/" from 'helpfile', if present */
4246 if (p == p_hf)
4247 pend = remove_tail(p, pend, (char_u *)"doc");
4248
4249#ifdef USE_EXE_NAME
4250# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004251 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (p == exe_name)
4253 {
4254 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004255 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004257 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4258 if (pend1 != pend)
4259 {
4260 pnew = alloc((unsigned)(pend1 - p) + 15);
4261 if (pnew != NULL)
4262 {
4263 STRNCPY(pnew, p, (pend1 - p));
4264 STRCPY(pnew + (pend1 - p), "Resources/vim");
4265 p = pnew;
4266 pend = p + STRLEN(p);
4267 }
4268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270# endif
4271 /* remove "src/" from exe_name, if present */
4272 if (p == exe_name)
4273 pend = remove_tail(p, pend, (char_u *)"src");
4274#endif
4275
4276 /* for $VIM, remove "runtime/" or "vim54/", if present */
4277 if (!vimruntime)
4278 {
4279 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4280 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4281 }
4282
4283 /* remove trailing path separator */
4284#ifndef MACOS_CLASSIC
4285 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004286 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004287 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 --pend;
4289#endif
4290
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004291#ifdef MACOS_X
4292 if (p == exe_name || p == p_hf)
4293#endif
4294 /* check that the result is a directory name */
4295 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 if (p != NULL && !mch_isdir(p))
4298 {
4299 vim_free(p);
4300 p = NULL;
4301 }
4302 else
4303 {
4304#ifdef USE_EXE_NAME
4305 /* may add "/vim54" or "/runtime" if it exists */
4306 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4307 {
4308 vim_free(p);
4309 p = pend;
4310 }
4311#endif
4312 *mustfree = TRUE;
4313 }
4314 }
4315 }
4316
4317#ifdef HAVE_PATHDEF
4318 /* When there is a pathdef.c file we can use default_vim_dir and
4319 * default_vimruntime_dir */
4320 if (p == NULL)
4321 {
4322 /* Only use default_vimruntime_dir when it is not empty */
4323 if (vimruntime && *default_vimruntime_dir != NUL)
4324 {
4325 p = default_vimruntime_dir;
4326 *mustfree = FALSE;
4327 }
4328 else if (*default_vim_dir != NUL)
4329 {
4330 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4331 *mustfree = TRUE;
4332 else
4333 {
4334 p = default_vim_dir;
4335 *mustfree = FALSE;
4336 }
4337 }
4338 }
4339#endif
4340
4341 /*
4342 * Set the environment variable, so that the new value can be found fast
4343 * next time, and others can also use it (e.g. Perl).
4344 */
4345 if (p != NULL)
4346 {
4347 if (vimruntime)
4348 {
4349 vim_setenv((char_u *)"VIMRUNTIME", p);
4350 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 else
4353 {
4354 vim_setenv((char_u *)"VIM", p);
4355 didset_vim = TRUE;
4356 }
4357 }
4358 return p;
4359}
4360
4361/*
4362 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4363 * Return NULL if not, return its name in allocated memory otherwise.
4364 */
4365 static char_u *
4366vim_version_dir(vimdir)
4367 char_u *vimdir;
4368{
4369 char_u *p;
4370
4371 if (vimdir == NULL || *vimdir == NUL)
4372 return NULL;
4373 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4374 if (p != NULL && mch_isdir(p))
4375 return p;
4376 vim_free(p);
4377 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4378 if (p != NULL && mch_isdir(p))
4379 return p;
4380 vim_free(p);
4381 return NULL;
4382}
4383
4384/*
4385 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4386 * the length of "name/". Otherwise return "pend".
4387 */
4388 static char_u *
4389remove_tail(p, pend, name)
4390 char_u *p;
4391 char_u *pend;
4392 char_u *name;
4393{
4394 int len = (int)STRLEN(name) + 1;
4395 char_u *newend = pend - len;
4396
4397 if (newend >= p
4398 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004399 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return newend;
4401 return pend;
4402}
4403
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 * Our portable version of setenv.
4406 */
4407 void
4408vim_setenv(name, val)
4409 char_u *name;
4410 char_u *val;
4411{
4412#ifdef HAVE_SETENV
4413 mch_setenv((char *)name, (char *)val, 1);
4414#else
4415 char_u *envbuf;
4416
4417 /*
4418 * Putenv does not copy the string, it has to remain
4419 * valid. The allocated memory will never be freed.
4420 */
4421 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4422 if (envbuf != NULL)
4423 {
4424 sprintf((char *)envbuf, "%s=%s", name, val);
4425 putenv((char *)envbuf);
4426 }
4427#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004428#ifdef FEAT_GETTEXT
4429 /*
4430 * When setting $VIMRUNTIME adjust the directory to find message
4431 * translations to $VIMRUNTIME/lang.
4432 */
4433 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4434 {
4435 char_u *buf = concat_str(val, (char_u *)"/lang");
4436
4437 if (buf != NULL)
4438 {
4439 bindtextdomain(VIMPACKAGE, (char *)buf);
4440 vim_free(buf);
4441 }
4442 }
4443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444}
4445
4446#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4447/*
4448 * Function given to ExpandGeneric() to obtain an environment variable name.
4449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 char_u *
4451get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004452 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 int idx;
4454{
4455# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4456 /*
4457 * No environ[] on the Amiga and on the Mac (using MPW).
4458 */
4459 return NULL;
4460# else
4461# ifndef __WIN32__
4462 /* Borland C++ 5.2 has this in a header file. */
4463 extern char **environ;
4464# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004465# define ENVNAMELEN 100
4466 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 char_u *str;
4468 int n;
4469
4470 str = (char_u *)environ[idx];
4471 if (str == NULL)
4472 return NULL;
4473
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004474 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
4476 if (str[n] == '=' || str[n] == NUL)
4477 break;
4478 name[n] = str[n];
4479 }
4480 name[n] = NUL;
4481 return name;
4482# endif
4483}
Bram Moolenaar24305862012-08-15 14:05:05 +02004484
4485/*
4486 * Find all user names for user completion.
4487 * Done only once and then cached.
4488 */
4489 static void
4490init_users() {
4491 static int lazy_init_done = FALSE;
4492
4493 if (lazy_init_done)
4494 return;
4495
4496 lazy_init_done = TRUE;
4497 ga_init2(&ga_users, sizeof(char_u *), 20);
4498
4499# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4500 {
4501 char_u* user;
4502 struct passwd* pw;
4503
4504 setpwent();
4505 while ((pw = getpwent()) != NULL)
4506 /* pw->pw_name shouldn't be NULL but just in case... */
4507 if (pw->pw_name != NULL)
4508 {
4509 if (ga_grow(&ga_users, 1) == FAIL)
4510 break;
4511 user = vim_strsave((char_u*)pw->pw_name);
4512 if (user == NULL)
4513 break;
4514 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4515 }
4516 endpwent();
4517 }
4518# endif
4519}
4520
4521/*
4522 * Function given to ExpandGeneric() to obtain an user names.
4523 */
4524 char_u*
4525get_users(xp, idx)
4526 expand_T *xp UNUSED;
4527 int idx;
4528{
4529 init_users();
4530 if (idx < ga_users.ga_len)
4531 return ((char_u **)ga_users.ga_data)[idx];
4532 return NULL;
4533}
4534
4535/*
4536 * Check whether name matches a user name. Return:
4537 * 0 if name does not match any user name.
4538 * 1 if name partially matches the beginning of a user name.
4539 * 2 is name fully matches a user name.
4540 */
4541int match_user(name)
4542 char_u* name;
4543{
4544 int i;
4545 int n = (int)STRLEN(name);
4546 int result = 0;
4547
4548 init_users();
4549 for (i = 0; i < ga_users.ga_len; i++)
4550 {
4551 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4552 return 2; /* full match */
4553 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4554 result = 1; /* partial match */
4555 }
4556 return result;
4557}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#endif
4559
4560/*
4561 * Replace home directory by "~" in each space or comma separated file name in
4562 * 'src'.
4563 * If anything fails (except when out of space) dst equals src.
4564 */
4565 void
4566home_replace(buf, src, dst, dstlen, one)
4567 buf_T *buf; /* when not NULL, check for help files */
4568 char_u *src; /* input file name */
4569 char_u *dst; /* where to put the result */
4570 int dstlen; /* maximum length of the result */
4571 int one; /* if TRUE, only replace one file name, include
4572 spaces and commas in the file name. */
4573{
4574 size_t dirlen = 0, envlen = 0;
4575 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004576 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 char_u *p;
4578
4579 if (src == NULL)
4580 {
4581 *dst = NUL;
4582 return;
4583 }
4584
4585 /*
4586 * If the file is a help file, remove the path completely.
4587 */
4588 if (buf != NULL && buf->b_help)
4589 {
4590 STRCPY(dst, gettail(src));
4591 return;
4592 }
4593
4594 /*
4595 * We check both the value of the $HOME environment variable and the
4596 * "real" home directory.
4597 */
4598 if (homedir != NULL)
4599 dirlen = STRLEN(homedir);
4600
4601#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004602 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004604 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4605#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004606 /* Empty is the same as not set. */
4607 if (homedir_env != NULL && *homedir_env == NUL)
4608 homedir_env = NULL;
4609
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004610#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004611 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004612 {
4613 int usedlen = 0;
4614 int flen;
4615 char_u *fbuf = NULL;
4616
4617 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004618 (void)modify_fname((char_u *)":p", &usedlen,
4619 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004620 flen = (int)STRLEN(homedir_env);
4621 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4622 /* Remove the trailing / that is added to a directory. */
4623 homedir_env[flen - 1] = NUL;
4624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625#endif
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (homedir_env != NULL)
4628 envlen = STRLEN(homedir_env);
4629
4630 if (!one)
4631 src = skipwhite(src);
4632 while (*src && dstlen > 0)
4633 {
4634 /*
4635 * Here we are at the beginning of a file name.
4636 * First, check to see if the beginning of the file name matches
4637 * $HOME or the "real" home directory. Check that there is a '/'
4638 * after the match (so that if e.g. the file is "/home/pieter/bla",
4639 * and the home directory is "/home/piet", the file does not end up
4640 * as "~er/bla" (which would seem to indicate the file "bla" in user
4641 * er's home directory)).
4642 */
4643 p = homedir;
4644 len = dirlen;
4645 for (;;)
4646 {
4647 if ( len
4648 && fnamencmp(src, p, len) == 0
4649 && (vim_ispathsep(src[len])
4650 || (!one && (src[len] == ',' || src[len] == ' '))
4651 || src[len] == NUL))
4652 {
4653 src += len;
4654 if (--dstlen > 0)
4655 *dst++ = '~';
4656
4657 /*
4658 * If it's just the home directory, add "/".
4659 */
4660 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4661 *dst++ = '/';
4662 break;
4663 }
4664 if (p == homedir_env)
4665 break;
4666 p = homedir_env;
4667 len = envlen;
4668 }
4669
4670 /* if (!one) skip to separator: space or comma */
4671 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4672 *dst++ = *src++;
4673 /* skip separator */
4674 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4675 *dst++ = *src++;
4676 }
4677 /* if (dstlen == 0) out of space, what to do??? */
4678
4679 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004680
4681 if (homedir_env != homedir_env_orig)
4682 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683}
4684
4685/*
4686 * Like home_replace, store the replaced string in allocated memory.
4687 * When something fails, NULL is returned.
4688 */
4689 char_u *
4690home_replace_save(buf, src)
4691 buf_T *buf; /* when not NULL, check for help files */
4692 char_u *src; /* input file name */
4693{
4694 char_u *dst;
4695 unsigned len;
4696
4697 len = 3; /* space for "~/" and trailing NUL */
4698 if (src != NULL) /* just in case */
4699 len += (unsigned)STRLEN(src);
4700 dst = alloc(len);
4701 if (dst != NULL)
4702 home_replace(buf, src, dst, len, TRUE);
4703 return dst;
4704}
4705
4706/*
4707 * Compare two file names and return:
4708 * FPC_SAME if they both exist and are the same file.
4709 * FPC_SAMEX if they both don't exist and have the same file name.
4710 * FPC_DIFF if they both exist and are different files.
4711 * FPC_NOTX if they both don't exist.
4712 * FPC_DIFFX if one of them doesn't exist.
4713 * For the first name environment variables are expanded
4714 */
4715 int
4716fullpathcmp(s1, s2, checkname)
4717 char_u *s1, *s2;
4718 int checkname; /* when both don't exist, check file names */
4719{
4720#ifdef UNIX
4721 char_u exp1[MAXPATHL];
4722 char_u full1[MAXPATHL];
4723 char_u full2[MAXPATHL];
4724 struct stat st1, st2;
4725 int r1, r2;
4726
4727 expand_env(s1, exp1, MAXPATHL);
4728 r1 = mch_stat((char *)exp1, &st1);
4729 r2 = mch_stat((char *)s2, &st2);
4730 if (r1 != 0 && r2 != 0)
4731 {
4732 /* if mch_stat() doesn't work, may compare the names */
4733 if (checkname)
4734 {
4735 if (fnamecmp(exp1, s2) == 0)
4736 return FPC_SAMEX;
4737 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4738 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4739 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4740 return FPC_SAMEX;
4741 }
4742 return FPC_NOTX;
4743 }
4744 if (r1 != 0 || r2 != 0)
4745 return FPC_DIFFX;
4746 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4747 return FPC_SAME;
4748 return FPC_DIFF;
4749#else
4750 char_u *exp1; /* expanded s1 */
4751 char_u *full1; /* full path of s1 */
4752 char_u *full2; /* full path of s2 */
4753 int retval = FPC_DIFF;
4754 int r1, r2;
4755
4756 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4757 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4758 {
4759 full1 = exp1 + MAXPATHL;
4760 full2 = full1 + MAXPATHL;
4761
4762 expand_env(s1, exp1, MAXPATHL);
4763 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4764 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4765
4766 /* If vim_FullName() fails, the file probably doesn't exist. */
4767 if (r1 != OK && r2 != OK)
4768 {
4769 if (checkname && fnamecmp(exp1, s2) == 0)
4770 retval = FPC_SAMEX;
4771 else
4772 retval = FPC_NOTX;
4773 }
4774 else if (r1 != OK || r2 != OK)
4775 retval = FPC_DIFFX;
4776 else if (fnamecmp(full1, full2))
4777 retval = FPC_DIFF;
4778 else
4779 retval = FPC_SAME;
4780 vim_free(exp1);
4781 }
4782 return retval;
4783#endif
4784}
4785
4786/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004787 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004788 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004789 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 */
4791 char_u *
4792gettail(fname)
4793 char_u *fname;
4794{
4795 char_u *p1, *p2;
4796
4797 if (fname == NULL)
4798 return (char_u *)"";
4799 for (p1 = p2 = fname; *p2; ) /* find last part of path */
4800 {
4801 if (vim_ispathsep(*p2))
4802 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004803 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 return p1;
4806}
4807
Bram Moolenaar31710262010-08-13 13:36:15 +02004808#if defined(FEAT_SEARCHPATH)
4809static char_u *gettail_dir __ARGS((char_u *fname));
4810
4811/*
4812 * Return the end of the directory name, on the first path
4813 * separator:
4814 * "/path/file", "/path/dir/", "/path//dir", "/file"
4815 * ^ ^ ^ ^
4816 */
4817 static char_u *
4818gettail_dir(fname)
4819 char_u *fname;
4820{
4821 char_u *dir_end = fname;
4822 char_u *next_dir_end = fname;
4823 int look_for_sep = TRUE;
4824 char_u *p;
4825
4826 for (p = fname; *p != NUL; )
4827 {
4828 if (vim_ispathsep(*p))
4829 {
4830 if (look_for_sep)
4831 {
4832 next_dir_end = p;
4833 look_for_sep = FALSE;
4834 }
4835 }
4836 else
4837 {
4838 if (!look_for_sep)
4839 dir_end = next_dir_end;
4840 look_for_sep = TRUE;
4841 }
4842 mb_ptr_adv(p);
4843 }
4844 return dir_end;
4845}
4846#endif
4847
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004849 * Get pointer to tail of "fname", including path separators. Putting a NUL
4850 * here leaves the directory name. Takes care of "c:/" and "//".
4851 * Always returns a valid pointer.
4852 */
4853 char_u *
4854gettail_sep(fname)
4855 char_u *fname;
4856{
4857 char_u *p;
4858 char_u *t;
4859
4860 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4861 t = gettail(fname);
4862 while (t > p && after_pathsep(fname, t))
4863 --t;
4864#ifdef VMS
4865 /* path separator is part of the path */
4866 ++t;
4867#endif
4868 return t;
4869}
4870
4871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 * get the next path component (just after the next path separator).
4873 */
4874 char_u *
4875getnextcomp(fname)
4876 char_u *fname;
4877{
4878 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004879 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (*fname)
4881 ++fname;
4882 return fname;
4883}
4884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885/*
4886 * Get a pointer to one character past the head of a path name.
4887 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4888 * If there is no head, path is returned.
4889 */
4890 char_u *
4891get_past_head(path)
4892 char_u *path;
4893{
4894 char_u *retval;
4895
4896#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4897 /* may skip "c:" */
4898 if (isalpha(path[0]) && path[1] == ':')
4899 retval = path + 2;
4900 else
4901 retval = path;
4902#else
4903# if defined(AMIGA)
4904 /* may skip "label:" */
4905 retval = vim_strchr(path, ':');
4906 if (retval == NULL)
4907 retval = path;
4908# else /* Unix */
4909 retval = path;
4910# endif
4911#endif
4912
4913 while (vim_ispathsep(*retval))
4914 ++retval;
4915
4916 return retval;
4917}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919/*
4920 * return TRUE if 'c' is a path separator.
4921 */
4922 int
4923vim_ispathsep(c)
4924 int c;
4925{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004926#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004928#else
4929# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004931# else
4932# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4934 return (c == ':' || c == '[' || c == ']' || c == '/'
4935 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004936# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004938# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941}
4942
4943#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4944/*
4945 * return TRUE if 'c' is a path list separator.
4946 */
4947 int
4948vim_ispathlistsep(c)
4949 int c;
4950{
4951#ifdef UNIX
4952 return (c == ':');
4953#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004954 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#endif
4956}
4957#endif
4958
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004959#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4960 || defined(FEAT_EVAL) || defined(PROTO)
4961/*
4962 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4963 * It's done in-place.
4964 */
4965 void
4966shorten_dir(str)
4967 char_u *str;
4968{
4969 char_u *tail, *s, *d;
4970 int skip = FALSE;
4971
4972 tail = gettail(str);
4973 d = str;
4974 for (s = str; ; ++s)
4975 {
4976 if (s >= tail) /* copy the whole tail */
4977 {
4978 *d++ = *s;
4979 if (*s == NUL)
4980 break;
4981 }
4982 else if (vim_ispathsep(*s)) /* copy '/' and next char */
4983 {
4984 *d++ = *s;
4985 skip = FALSE;
4986 }
4987 else if (!skip)
4988 {
4989 *d++ = *s; /* copy next char */
4990 if (*s != '~' && *s != '.') /* and leading "~" and "." */
4991 skip = TRUE;
4992# ifdef FEAT_MBYTE
4993 if (has_mbyte)
4994 {
4995 int l = mb_ptr2len(s);
4996
4997 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00004998 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004999 }
5000# endif
5001 }
5002 }
5003}
5004#endif
5005
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005006/*
5007 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5008 * Also returns TRUE if there is no directory name.
5009 * "fname" must be writable!.
5010 */
5011 int
5012dir_of_file_exists(fname)
5013 char_u *fname;
5014{
5015 char_u *p;
5016 int c;
5017 int retval;
5018
5019 p = gettail_sep(fname);
5020 if (p == fname)
5021 return TRUE;
5022 c = *p;
5023 *p = NUL;
5024 retval = mch_isdir(fname);
5025 *p = c;
5026 return retval;
5027}
5028
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005030 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5031 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 */
5033 int
5034vim_fnamecmp(x, y)
5035 char_u *x, *y;
5036{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005037#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005039#else
5040 if (p_fic)
5041 return MB_STRICMP(x, y);
5042 return STRCMP(x, y);
5043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044}
5045
5046 int
5047vim_fnamencmp(x, y, len)
5048 char_u *x, *y;
5049 size_t len;
5050{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005051#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005052 char_u *px = x;
5053 char_u *py = y;
5054 int cx = NUL;
5055 int cy = NUL;
5056
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005057 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005059 cx = PTR2CHAR(px);
5060 cy = PTR2CHAR(py);
5061 if (cx == NUL || cy == NUL
5062 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5063 && !(cx == '/' && cy == '\\')
5064 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005066 len -= MB_PTR2LEN(px);
5067 px += MB_PTR2LEN(px);
5068 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070 if (len == 0)
5071 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005072 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005073#else
5074 if (p_fic)
5075 return MB_STRNICMP(x, y, len);
5076 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005078}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
5080/*
5081 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005082 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
5084 char_u *
5085concat_fnames(fname1, fname2, sep)
5086 char_u *fname1;
5087 char_u *fname2;
5088 int sep;
5089{
5090 char_u *dest;
5091
5092 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5093 if (dest != NULL)
5094 {
5095 STRCPY(dest, fname1);
5096 if (sep)
5097 add_pathsep(dest);
5098 STRCAT(dest, fname2);
5099 }
5100 return dest;
5101}
5102
Bram Moolenaard6754642005-01-17 22:18:45 +00005103/*
5104 * Concatenate two strings and return the result in allocated memory.
5105 * Returns NULL when out of memory.
5106 */
5107 char_u *
5108concat_str(str1, str2)
5109 char_u *str1;
5110 char_u *str2;
5111{
5112 char_u *dest;
5113 size_t l = STRLEN(str1);
5114
5115 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5116 if (dest != NULL)
5117 {
5118 STRCPY(dest, str1);
5119 STRCPY(dest + l, str2);
5120 }
5121 return dest;
5122}
Bram Moolenaard6754642005-01-17 22:18:45 +00005123
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124/*
5125 * Add a path separator to a file name, unless it already ends in a path
5126 * separator.
5127 */
5128 void
5129add_pathsep(p)
5130 char_u *p;
5131{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005132 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 STRCAT(p, PATHSEPSTR);
5134}
5135
5136/*
5137 * FullName_save - Make an allocated copy of a full file name.
5138 * Returns NULL when out of memory.
5139 */
5140 char_u *
5141FullName_save(fname, force)
5142 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005143 int force; /* force expansion, even when it already looks
5144 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 char_u *buf;
5147 char_u *new_fname = NULL;
5148
5149 if (fname == NULL)
5150 return NULL;
5151
5152 buf = alloc((unsigned)MAXPATHL);
5153 if (buf != NULL)
5154 {
5155 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5156 new_fname = vim_strsave(buf);
5157 else
5158 new_fname = vim_strsave(fname);
5159 vim_free(buf);
5160 }
5161 return new_fname;
5162}
5163
5164#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5165
5166static char_u *skip_string __ARGS((char_u *p));
5167
5168/*
5169 * Find the start of a comment, not knowing if we are in a comment right now.
5170 * Search starts at w_cursor.lnum and goes backwards.
5171 */
5172 pos_T *
5173find_start_comment(ind_maxcomment) /* XXX */
5174 int ind_maxcomment;
5175{
5176 pos_T *pos;
5177 char_u *line;
5178 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005179 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005181 for (;;)
5182 {
5183 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5184 if (pos == NULL)
5185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005187 /*
5188 * Check if the comment start we found is inside a string.
5189 * If it is then restrict the search to below this line and try again.
5190 */
5191 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005192 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005193 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005194 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005195 break;
5196 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5197 if (cur_maxcomment <= 0)
5198 {
5199 pos = NULL;
5200 break;
5201 }
5202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 return pos;
5204}
5205
5206/*
5207 * Skip to the end of a "string" and a 'c' character.
5208 * If there is no string or character, return argument unmodified.
5209 */
5210 static char_u *
5211skip_string(p)
5212 char_u *p;
5213{
5214 int i;
5215
5216 /*
5217 * We loop, because strings may be concatenated: "date""time".
5218 */
5219 for ( ; ; ++p)
5220 {
5221 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5222 {
5223 if (!p[1]) /* ' at end of line */
5224 break;
5225 i = 2;
5226 if (p[1] == '\\') /* '\n' or '\000' */
5227 {
5228 ++i;
5229 while (vim_isdigit(p[i - 1])) /* '\000' */
5230 ++i;
5231 }
5232 if (p[i] == '\'') /* check for trailing ' */
5233 {
5234 p += i;
5235 continue;
5236 }
5237 }
5238 else if (p[0] == '"') /* start of string */
5239 {
5240 for (++p; p[0]; ++p)
5241 {
5242 if (p[0] == '\\' && p[1] != NUL)
5243 ++p;
5244 else if (p[0] == '"') /* end of string */
5245 break;
5246 }
5247 if (p[0] == '"')
5248 continue;
5249 }
5250 break; /* no string found */
5251 }
5252 if (!*p)
5253 --p; /* backup from NUL */
5254 return p;
5255}
5256#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5257
5258#if defined(FEAT_CINDENT) || defined(PROTO)
5259
5260/*
5261 * Do C or expression indenting on the current line.
5262 */
5263 void
5264do_c_expr_indent()
5265{
5266# ifdef FEAT_EVAL
5267 if (*curbuf->b_p_inde != NUL)
5268 fixthisline(get_expr_indent);
5269 else
5270# endif
5271 fixthisline(get_c_indent);
5272}
5273
5274/*
5275 * Functions for C-indenting.
5276 * Most of this originally comes from Eric Fischer.
5277 */
5278/*
5279 * Below "XXX" means that this function may unlock the current line.
5280 */
5281
5282static char_u *cin_skipcomment __ARGS((char_u *));
5283static int cin_nocode __ARGS((char_u *));
5284static pos_T *find_line_comment __ARGS((void));
5285static int cin_islabel_skip __ARGS((char_u **));
5286static int cin_isdefault __ARGS((char_u *));
5287static char_u *after_label __ARGS((char_u *l));
5288static int get_indent_nolabel __ARGS((linenr_T lnum));
5289static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment));
5290static int cin_first_id_amount __ARGS((void));
5291static int cin_get_equal_amount __ARGS((linenr_T lnum));
5292static int cin_ispreproc __ARGS((char_u *));
5293static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5294static int cin_iscomment __ARGS((char_u *));
5295static int cin_islinecomment __ARGS((char_u *));
5296static int cin_isterminated __ARGS((char_u *, int, int));
5297static int cin_isinit __ARGS((void));
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005298static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299static int cin_isif __ARGS((char_u *));
5300static int cin_iselse __ARGS((char_u *));
5301static int cin_isdo __ARGS((char_u *));
5302static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005303static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005304static int cin_iswhileofdo_end __ARGS((int terminated, int ind_maxparen, int ind_maxcomment));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005306static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005307static int get_baseclass_amount __ARGS((int col, int ind_maxparen, int ind_maxcomment, int ind_cpp_baseclass));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005309static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310static int cin_skip2pos __ARGS((pos_T *trypos));
5311static pos_T *find_start_brace __ARGS((int));
5312static pos_T *find_match_paren __ARGS((int, int));
5313static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos));
5314static int find_last_paren __ARGS((char_u *l, int start, int end));
5315static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005316static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005318static int ind_hash_comment = 0; /* # starts a comment */
5319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320/*
5321 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005322 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 */
5324 static char_u *
5325cin_skipcomment(s)
5326 char_u *s;
5327{
5328 while (*s)
5329 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005330 char_u *prev_s = s;
5331
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005333
5334 /* Perl/shell # comment comment continues until eol. Require a space
5335 * before # to avoid recognizing $#array. */
5336 if (ind_hash_comment != 0 && s != prev_s && *s == '#')
5337 {
5338 s += STRLEN(s);
5339 break;
5340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 if (*s != '/')
5342 break;
5343 ++s;
5344 if (*s == '/') /* slash-slash comment continues till eol */
5345 {
5346 s += STRLEN(s);
5347 break;
5348 }
5349 if (*s != '*')
5350 break;
5351 for (++s; *s; ++s) /* skip slash-star comment */
5352 if (s[0] == '*' && s[1] == '/')
5353 {
5354 s += 2;
5355 break;
5356 }
5357 }
5358 return s;
5359}
5360
5361/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005362 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 * not considered code.
5364 */
5365 static int
5366cin_nocode(s)
5367 char_u *s;
5368{
5369 return *cin_skipcomment(s) == NUL;
5370}
5371
5372/*
5373 * Check previous lines for a "//" line comment, skipping over blank lines.
5374 */
5375 static pos_T *
5376find_line_comment() /* XXX */
5377{
5378 static pos_T pos;
5379 char_u *line;
5380 char_u *p;
5381
5382 pos = curwin->w_cursor;
5383 while (--pos.lnum > 0)
5384 {
5385 line = ml_get(pos.lnum);
5386 p = skipwhite(line);
5387 if (cin_islinecomment(p))
5388 {
5389 pos.col = (int)(p - line);
5390 return &pos;
5391 }
5392 if (*p != NUL)
5393 break;
5394 }
5395 return NULL;
5396}
5397
5398/*
5399 * Check if string matches "label:"; move to character after ':' if true.
5400 */
5401 static int
5402cin_islabel_skip(s)
5403 char_u **s;
5404{
5405 if (!vim_isIDc(**s)) /* need at least one ID character */
5406 return FALSE;
5407
5408 while (vim_isIDc(**s))
5409 (*s)++;
5410
5411 *s = cin_skipcomment(*s);
5412
5413 /* "::" is not a label, it's C++ */
5414 return (**s == ':' && *++*s != ':');
5415}
5416
5417/*
5418 * Recognize a label: "label:".
5419 * Note: curwin->w_cursor must be where we are looking for the label.
5420 */
5421 int
5422cin_islabel(ind_maxcomment) /* XXX */
5423 int ind_maxcomment;
5424{
5425 char_u *s;
5426
5427 s = cin_skipcomment(ml_get_curline());
5428
5429 /*
5430 * Exclude "default" from labels, since it should be indented
5431 * like a switch label. Same for C++ scope declarations.
5432 */
5433 if (cin_isdefault(s))
5434 return FALSE;
5435 if (cin_isscopedecl(s))
5436 return FALSE;
5437
5438 if (cin_islabel_skip(&s))
5439 {
5440 /*
5441 * Only accept a label if the previous line is terminated or is a case
5442 * label.
5443 */
5444 pos_T cursor_save;
5445 pos_T *trypos;
5446 char_u *line;
5447
5448 cursor_save = curwin->w_cursor;
5449 while (curwin->w_cursor.lnum > 1)
5450 {
5451 --curwin->w_cursor.lnum;
5452
5453 /*
5454 * If we're in a comment now, skip to the start of the comment.
5455 */
5456 curwin->w_cursor.col = 0;
5457 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
5458 curwin->w_cursor = *trypos;
5459
5460 line = ml_get_curline();
5461 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5462 continue;
5463 if (*(line = cin_skipcomment(line)) == NUL)
5464 continue;
5465
5466 curwin->w_cursor = cursor_save;
5467 if (cin_isterminated(line, TRUE, FALSE)
5468 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005469 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 || (cin_islabel_skip(&line) && cin_nocode(line)))
5471 return TRUE;
5472 return FALSE;
5473 }
5474 curwin->w_cursor = cursor_save;
5475 return TRUE; /* label at start of file??? */
5476 }
5477 return FALSE;
5478}
5479
5480/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005481 * Recognize structure initialization and enumerations:
5482 * "[typedef] [static|public|protected|private] enum"
5483 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 */
5485 static int
5486cin_isinit(void)
5487{
5488 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005489 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
5491 s = cin_skipcomment(ml_get_curline());
5492
Bram Moolenaar75342212013-03-07 13:13:52 +01005493 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 s = cin_skipcomment(s + 7);
5495
Bram Moolenaar75342212013-03-07 13:13:52 +01005496 for (;;)
5497 {
5498 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005499
Bram Moolenaar75342212013-03-07 13:13:52 +01005500 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5501 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005502 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005503 if (cin_starts_with(s, skip[i]))
5504 {
5505 s = cin_skipcomment(s + l);
5506 l = 0;
5507 break;
5508 }
5509 }
5510 if (l != 0)
5511 break;
5512 }
5513
5514 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 return TRUE;
5516
5517 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5518 return TRUE;
5519
5520 return FALSE;
5521}
5522
5523/*
5524 * Recognize a switch label: "case .*:" or "default:".
5525 */
5526 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005527cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005529 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005532 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
5534 for (s += 4; *s; ++s)
5535 {
5536 s = cin_skipcomment(s);
5537 if (*s == ':')
5538 {
5539 if (s[1] == ':') /* skip over "::" for C++ */
5540 ++s;
5541 else
5542 return TRUE;
5543 }
5544 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005545 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5547 return FALSE; /* stop at comment */
5548 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005549 {
5550 /* JS etc. */
5551 if (strict)
5552 return FALSE; /* stop at string */
5553 else
5554 return TRUE;
5555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557 return FALSE;
5558 }
5559
5560 if (cin_isdefault(s))
5561 return TRUE;
5562 return FALSE;
5563}
5564
5565/*
5566 * Recognize a "default" switch label.
5567 */
5568 static int
5569cin_isdefault(s)
5570 char_u *s;
5571{
5572 return (STRNCMP(s, "default", 7) == 0
5573 && *(s = cin_skipcomment(s + 7)) == ':'
5574 && s[1] != ':');
5575}
5576
5577/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005578 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 */
5580 int
5581cin_isscopedecl(s)
5582 char_u *s;
5583{
5584 int i;
5585
5586 s = cin_skipcomment(s);
5587 if (STRNCMP(s, "public", 6) == 0)
5588 i = 6;
5589 else if (STRNCMP(s, "protected", 9) == 0)
5590 i = 9;
5591 else if (STRNCMP(s, "private", 7) == 0)
5592 i = 7;
5593 else
5594 return FALSE;
5595 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5596}
5597
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005598/* Maximum number of lines to search back for a "namespace" line. */
5599#define FIND_NAMESPACE_LIM 20
5600
5601/*
5602 * Recognize a "namespace" scope declaration.
5603 */
5604 static int
5605cin_is_cpp_namespace(s)
5606 char_u *s;
5607{
5608 char_u *p;
5609 int has_name = FALSE;
5610
5611 s = cin_skipcomment(s);
5612 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5613 {
5614 p = cin_skipcomment(skipwhite(s + 9));
5615 while (*p != NUL)
5616 {
5617 if (vim_iswhite(*p))
5618 {
5619 has_name = TRUE; /* found end of a name */
5620 p = cin_skipcomment(skipwhite(p));
5621 }
5622 else if (*p == '{')
5623 {
5624 break;
5625 }
5626 else if (vim_iswordc(*p))
5627 {
5628 if (has_name)
5629 return FALSE; /* word character after skipping past name */
5630 ++p;
5631 }
5632 else
5633 {
5634 return FALSE;
5635 }
5636 }
5637 return TRUE;
5638 }
5639 return FALSE;
5640}
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642/*
5643 * Return a pointer to the first non-empty non-comment character after a ':'.
5644 * Return NULL if not found.
5645 * case 234: a = b;
5646 * ^
5647 */
5648 static char_u *
5649after_label(l)
5650 char_u *l;
5651{
5652 for ( ; *l; ++l)
5653 {
5654 if (*l == ':')
5655 {
5656 if (l[1] == ':') /* skip over "::" for C++ */
5657 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005658 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 break;
5660 }
5661 else if (*l == '\'' && l[1] && l[2] == '\'')
5662 l += 2; /* skip over 'x' */
5663 }
5664 if (*l == NUL)
5665 return NULL;
5666 l = cin_skipcomment(l + 1);
5667 if (*l == NUL)
5668 return NULL;
5669 return l;
5670}
5671
5672/*
5673 * Get indent of line "lnum", skipping a label.
5674 * Return 0 if there is nothing after the label.
5675 */
5676 static int
5677get_indent_nolabel(lnum) /* XXX */
5678 linenr_T lnum;
5679{
5680 char_u *l;
5681 pos_T fp;
5682 colnr_T col;
5683 char_u *p;
5684
5685 l = ml_get(lnum);
5686 p = after_label(l);
5687 if (p == NULL)
5688 return 0;
5689
5690 fp.col = (colnr_T)(p - l);
5691 fp.lnum = lnum;
5692 getvcol(curwin, &fp, &col, NULL, NULL);
5693 return (int)col;
5694}
5695
5696/*
5697 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005698 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 * label: if (asdf && asdfasdf)
5700 * ^
5701 */
5702 static int
5703skip_label(lnum, pp, ind_maxcomment)
5704 linenr_T lnum;
5705 char_u **pp;
5706 int ind_maxcomment;
5707{
5708 char_u *l;
5709 int amount;
5710 pos_T cursor_save;
5711
5712 cursor_save = curwin->w_cursor;
5713 curwin->w_cursor.lnum = lnum;
5714 l = ml_get_curline();
5715 /* XXX */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005716 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)
5717 || cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 amount = get_indent_nolabel(lnum);
5720 l = after_label(ml_get_curline());
5721 if (l == NULL) /* just in case */
5722 l = ml_get_curline();
5723 }
5724 else
5725 {
5726 amount = get_indent();
5727 l = ml_get_curline();
5728 }
5729 *pp = l;
5730
5731 curwin->w_cursor = cursor_save;
5732 return amount;
5733}
5734
5735/*
5736 * Return the indent of the first variable name after a type in a declaration.
5737 * int a, indent of "a"
5738 * static struct foo b, indent of "b"
5739 * enum bla c, indent of "c"
5740 * Returns zero when it doesn't look like a declaration.
5741 */
5742 static int
5743cin_first_id_amount()
5744{
5745 char_u *line, *p, *s;
5746 int len;
5747 pos_T fp;
5748 colnr_T col;
5749
5750 line = ml_get_curline();
5751 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005752 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5754 {
5755 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005756 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5759 p = skipwhite(p + 6);
5760 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5761 p = skipwhite(p + 4);
5762 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5763 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5764 {
5765 s = skipwhite(p + len);
5766 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5767 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5768 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5769 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5770 p = s;
5771 }
5772 for (len = 0; vim_isIDc(p[len]); ++len)
5773 ;
5774 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5775 return 0;
5776
5777 p = skipwhite(p + len);
5778 fp.lnum = curwin->w_cursor.lnum;
5779 fp.col = (colnr_T)(p - line);
5780 getvcol(curwin, &fp, &col, NULL, NULL);
5781 return (int)col;
5782}
5783
5784/*
5785 * Return the indent of the first non-blank after an equal sign.
5786 * char *foo = "here";
5787 * Return zero if no (useful) equal sign found.
5788 * Return -1 if the line above "lnum" ends in a backslash.
5789 * foo = "asdf\
5790 * asdf\
5791 * here";
5792 */
5793 static int
5794cin_get_equal_amount(lnum)
5795 linenr_T lnum;
5796{
5797 char_u *line;
5798 char_u *s;
5799 colnr_T col;
5800 pos_T fp;
5801
5802 if (lnum > 1)
5803 {
5804 line = ml_get(lnum - 1);
5805 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5806 return -1;
5807 }
5808
5809 line = s = ml_get(lnum);
5810 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5811 {
5812 if (cin_iscomment(s)) /* ignore comments */
5813 s = cin_skipcomment(s);
5814 else
5815 ++s;
5816 }
5817 if (*s != '=')
5818 return 0;
5819
5820 s = skipwhite(s + 1);
5821 if (cin_nocode(s))
5822 return 0;
5823
5824 if (*s == '"') /* nice alignment for continued strings */
5825 ++s;
5826
5827 fp.lnum = lnum;
5828 fp.col = (colnr_T)(s - line);
5829 getvcol(curwin, &fp, &col, NULL, NULL);
5830 return (int)col;
5831}
5832
5833/*
5834 * Recognize a preprocessor statement: Any line that starts with '#'.
5835 */
5836 static int
5837cin_ispreproc(s)
5838 char_u *s;
5839{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005840 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 return TRUE;
5842 return FALSE;
5843}
5844
5845/*
5846 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5847 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5848 * start and return the line in "*pp".
5849 */
5850 static int
5851cin_ispreproc_cont(pp, lnump)
5852 char_u **pp;
5853 linenr_T *lnump;
5854{
5855 char_u *line = *pp;
5856 linenr_T lnum = *lnump;
5857 int retval = FALSE;
5858
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005859 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
5861 if (cin_ispreproc(line))
5862 {
5863 retval = TRUE;
5864 *lnump = lnum;
5865 break;
5866 }
5867 if (lnum == 1)
5868 break;
5869 line = ml_get(--lnum);
5870 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5871 break;
5872 }
5873
5874 if (lnum != *lnump)
5875 *pp = ml_get(*lnump);
5876 return retval;
5877}
5878
5879/*
5880 * Recognize the start of a C or C++ comment.
5881 */
5882 static int
5883cin_iscomment(p)
5884 char_u *p;
5885{
5886 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5887}
5888
5889/*
5890 * Recognize the start of a "//" comment.
5891 */
5892 static int
5893cin_islinecomment(p)
5894 char_u *p;
5895{
5896 return (p[0] == '/' && p[1] == '/');
5897}
5898
5899/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005900 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5901 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005903 * If a line begins with an "else", only consider it terminated if no unmatched
5904 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 * Return the character terminating the line (ending char's have precedence if
5906 * both apply in order to determine initializations).
5907 */
5908 static int
5909cin_isterminated(s, incl_open, incl_comma)
5910 char_u *s;
5911 int incl_open; /* include '{' at the end as terminator */
5912 int incl_comma; /* recognize a trailing comma */
5913{
Bram Moolenaar496f9512011-05-19 16:35:09 +02005914 char_u found_start = 0;
5915 unsigned n_open = 0;
5916 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918 s = cin_skipcomment(s);
5919
5920 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
5921 found_start = *s;
5922
Bram Moolenaar496f9512011-05-19 16:35:09 +02005923 if (!found_start)
5924 is_else = cin_iselse(s);
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 while (*s)
5927 {
5928 /* skip over comments, "" strings and 'c'haracters */
5929 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005930 if (*s == '}' && n_open > 0)
5931 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02005932 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005933 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 && cin_nocode(s + 1))
5935 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005936 else if (*s == '{')
5937 {
5938 if (incl_open && cin_nocode(s + 1))
5939 return *s;
5940 else
5941 ++n_open;
5942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943
5944 if (*s)
5945 s++;
5946 }
5947 return found_start;
5948}
5949
5950/*
5951 * Recognize the basic picture of a function declaration -- it needs to
5952 * have an open paren somewhere and a close paren at the end of the line and
5953 * no semicolons anywhere.
5954 * When a line ends in a comma we continue looking in the next line.
5955 * "sp" points to a string with the line. When looking at other lines it must
5956 * be restored to the line. When it's NULL fetch lines here.
5957 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005958 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 */
5960 static int
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005961cin_isfuncdecl(sp, first_lnum, min_lnum, ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 char_u **sp;
5963 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005964 linenr_T min_lnum;
5965 int ind_maxparen;
5966 int ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967{
5968 char_u *s;
5969 linenr_T lnum = first_lnum;
5970 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005971 pos_T *trypos;
5972 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
5974 if (sp == NULL)
5975 s = ml_get(lnum);
5976 else
5977 s = *sp;
5978
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005979 if (find_last_paren(s, '(', ')')
5980 && (trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL)
5981 {
5982 lnum = trypos->lnum;
5983 if (lnum < min_lnum)
5984 return FALSE;
5985
5986 s = ml_get(lnum);
5987 }
5988
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005989 /* Ignore line starting with #. */
5990 if (cin_ispreproc(s))
5991 return FALSE;
5992
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
5994 {
5995 if (cin_iscomment(s)) /* ignore comments */
5996 s = cin_skipcomment(s);
5997 else
5998 ++s;
5999 }
6000 if (*s != '(')
6001 return FALSE; /* ';', ' or " before any () or no '(' */
6002
6003 while (*s && *s != ';' && *s != '\'' && *s != '"')
6004 {
6005 if (*s == ')' && cin_nocode(s + 1))
6006 {
6007 /* ')' at the end: may have found a match
6008 * Check for he previous line not to end in a backslash:
6009 * #if defined(x) && \
6010 * defined(y)
6011 */
6012 lnum = first_lnum - 1;
6013 s = ml_get(lnum);
6014 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6015 retval = TRUE;
6016 goto done;
6017 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006018 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006020 int comma = (*s == ',');
6021
6022 /* ',' at the end: continue looking in the next line.
6023 * At the end: check for ',' in the next line, for this style:
6024 * func(arg1
6025 * , arg2) */
6026 for (;;)
6027 {
6028 if (lnum >= curbuf->b_ml.ml_line_count)
6029 break;
6030 s = ml_get(++lnum);
6031 if (!cin_ispreproc(s))
6032 break;
6033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (lnum >= curbuf->b_ml.ml_line_count)
6035 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006036 /* Require a comma at end of the line or a comma or ')' at the
6037 * start of next line. */
6038 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006039 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006040 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006041 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 }
6043 else if (cin_iscomment(s)) /* ignore comments */
6044 s = cin_skipcomment(s);
6045 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006048 just_started = FALSE;
6049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 }
6051
6052done:
6053 if (lnum != first_lnum && sp != NULL)
6054 *sp = ml_get(first_lnum);
6055
6056 return retval;
6057}
6058
6059 static int
6060cin_isif(p)
6061 char_u *p;
6062{
6063 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6064}
6065
6066 static int
6067cin_iselse(p)
6068 char_u *p;
6069{
6070 if (*p == '}') /* accept "} else" */
6071 p = cin_skipcomment(p + 1);
6072 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6073}
6074
6075 static int
6076cin_isdo(p)
6077 char_u *p;
6078{
6079 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6080}
6081
6082/*
6083 * Check if this is a "while" that should have a matching "do".
6084 * We only accept a "while (condition) ;", with only white space between the
6085 * ')' and ';'. The condition may be spread over several lines.
6086 */
6087 static int
6088cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */
6089 char_u *p;
6090 linenr_T lnum;
6091 int ind_maxparen;
6092{
6093 pos_T cursor_save;
6094 pos_T *trypos;
6095 int retval = FALSE;
6096
6097 p = cin_skipcomment(p);
6098 if (*p == '}') /* accept "} while (cond);" */
6099 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006100 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 {
6102 cursor_save = curwin->w_cursor;
6103 curwin->w_cursor.lnum = lnum;
6104 curwin->w_cursor.col = 0;
6105 p = ml_get_curline();
6106 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6107 {
6108 ++p;
6109 ++curwin->w_cursor.col;
6110 }
6111 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL
6112 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6113 retval = TRUE;
6114 curwin->w_cursor = cursor_save;
6115 }
6116 return retval;
6117}
6118
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006119/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006120 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006121 * Return 0 if there is none.
6122 * Otherwise return !0 and update "*poffset" to point to the place where the
6123 * string was found.
6124 */
6125 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006126cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006127 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006128 int *poffset;
6129{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006130 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006131
6132 if (offset-- < 2)
6133 return 0;
6134 while (offset > 2 && vim_iswhite(line[offset]))
6135 --offset;
6136
6137 offset -= 1;
6138 if (!STRNCMP(line + offset, "if", 2))
6139 goto probablyFound;
6140
6141 if (offset >= 1)
6142 {
6143 offset -= 1;
6144 if (!STRNCMP(line + offset, "for", 3))
6145 goto probablyFound;
6146
6147 if (offset >= 2)
6148 {
6149 offset -= 2;
6150 if (!STRNCMP(line + offset, "while", 5))
6151 goto probablyFound;
6152 }
6153 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006154 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006155
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006156probablyFound:
6157 if (!offset || !vim_isIDc(line[offset - 1]))
6158 {
6159 *poffset = offset;
6160 return 1;
6161 }
6162 return 0;
6163}
6164
6165/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006166 * Return TRUE if we are at the end of a do-while.
6167 * do
6168 * nothing;
6169 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006170 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006171 * Adjust the cursor to the line with "while".
6172 */
6173 static int
6174cin_iswhileofdo_end(terminated, ind_maxparen, ind_maxcomment)
6175 int terminated;
6176 int ind_maxparen;
6177 int ind_maxcomment;
6178{
6179 char_u *line;
6180 char_u *p;
6181 char_u *s;
6182 pos_T *trypos;
6183 int i;
6184
6185 if (terminated != ';') /* there must be a ';' at the end */
6186 return FALSE;
6187
6188 p = line = ml_get_curline();
6189 while (*p != NUL)
6190 {
6191 p = cin_skipcomment(p);
6192 if (*p == ')')
6193 {
6194 s = skipwhite(p + 1);
6195 if (*s == ';' && cin_nocode(s + 1))
6196 {
6197 /* Found ");" at end of the line, now check there is "while"
6198 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006199 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006200 curwin->w_cursor.col = i;
6201 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
6202 if (trypos != NULL)
6203 {
6204 s = cin_skipcomment(ml_get(trypos->lnum));
6205 if (*s == '}') /* accept "} while (cond);" */
6206 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006207 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006208 {
6209 curwin->w_cursor.lnum = trypos->lnum;
6210 return TRUE;
6211 }
6212 }
6213
6214 /* Searching may have made "line" invalid, get it again. */
6215 line = ml_get_curline();
6216 p = line + i;
6217 }
6218 }
6219 if (*p != NUL)
6220 ++p;
6221 }
6222 return FALSE;
6223}
6224
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 static int
6226cin_isbreak(p)
6227 char_u *p;
6228{
6229 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6230}
6231
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006232/*
6233 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 * constructor-initialization. eg:
6235 *
6236 * class MyClass :
6237 * baseClass <-- here
6238 * class MyClass : public baseClass,
6239 * anotherBaseClass <-- here (should probably lineup ??)
6240 * MyClass::MyClass(...) :
6241 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006242 *
6243 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 */
6245 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006246cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006247 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 char_u *s;
6250 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006251 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006252 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
6254 *col = 0;
6255
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006256 s = skipwhite(line);
6257 if (*s == '#') /* skip #define FOO x ? (x) : x */
6258 return FALSE;
6259 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (*s == NUL)
6261 return FALSE;
6262
6263 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6264
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006265 /* Search for a line starting with '#', empty, ending in ';' or containing
6266 * '{' or '}' and start below it. This handles the following situations:
6267 * a = cond ?
6268 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006269 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006270 * func::foo()
6271 * : something
6272 * {}
6273 * Foo::Foo (int one, int two)
6274 * : something(4),
6275 * somethingelse(3)
6276 * {}
6277 */
6278 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006280 line = ml_get(lnum - 1);
6281 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006282 if (*s == '#' || *s == NUL)
6283 break;
6284 while (*s != NUL)
6285 {
6286 s = cin_skipcomment(s);
6287 if (*s == '{' || *s == '}'
6288 || (*s == ';' && cin_nocode(s + 1)))
6289 break;
6290 if (*s != NUL)
6291 ++s;
6292 }
6293 if (*s != NUL)
6294 break;
6295 --lnum;
6296 }
6297
Bram Moolenaare7c56862007-08-04 10:14:52 +00006298 line = ml_get(lnum);
6299 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006300 for (;;)
6301 {
6302 if (*s == NUL)
6303 {
6304 if (lnum == curwin->w_cursor.lnum)
6305 break;
6306 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006307 line = ml_get(++lnum);
6308 s = cin_skipcomment(line);
6309 if (*s == NUL)
6310 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006311 }
6312
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006313 if (s[0] == '"')
6314 s = skip_string(s) + 1;
6315 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 {
6317 if (s[1] == ':')
6318 {
6319 /* skip double colon. It can't be a constructor
6320 * initialization any more */
6321 lookfor_ctor_init = FALSE;
6322 s = cin_skipcomment(s + 2);
6323 }
6324 else if (lookfor_ctor_init || class_or_struct)
6325 {
6326 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006327 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 cpp_base_class = TRUE;
6329 lookfor_ctor_init = class_or_struct = FALSE;
6330 *col = 0;
6331 s = cin_skipcomment(s + 1);
6332 }
6333 else
6334 s = cin_skipcomment(s + 1);
6335 }
6336 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6337 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6338 {
6339 class_or_struct = TRUE;
6340 lookfor_ctor_init = FALSE;
6341
6342 if (*s == 'c')
6343 s = cin_skipcomment(s + 5);
6344 else
6345 s = cin_skipcomment(s + 6);
6346 }
6347 else
6348 {
6349 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6350 {
6351 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6352 }
6353 else if (s[0] == ')')
6354 {
6355 /* Constructor-initialization is assumed if we come across
6356 * something like "):" */
6357 class_or_struct = FALSE;
6358 lookfor_ctor_init = TRUE;
6359 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006360 else if (s[0] == '?')
6361 {
6362 /* Avoid seeing '() :' after '?' as constructor init. */
6363 return FALSE;
6364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 else if (!vim_isIDc(s[0]))
6366 {
6367 /* if it is not an identifier, we are wrong */
6368 class_or_struct = FALSE;
6369 lookfor_ctor_init = FALSE;
6370 }
6371 else if (*col == 0)
6372 {
6373 /* it can't be a constructor-initialization any more */
6374 lookfor_ctor_init = FALSE;
6375
6376 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006377 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 *col = (colnr_T)(s - line);
6379 }
6380
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006381 /* When the line ends in a comma don't align with it. */
6382 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6383 *col = 0;
6384
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 s = cin_skipcomment(s + 1);
6386 }
6387 }
6388
6389 return cpp_base_class;
6390}
6391
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006392 static int
6393get_baseclass_amount(col, ind_maxparen, ind_maxcomment, ind_cpp_baseclass)
6394 int col;
6395 int ind_maxparen;
6396 int ind_maxcomment;
6397 int ind_cpp_baseclass;
6398{
6399 int amount;
6400 colnr_T vcol;
6401 pos_T *trypos;
6402
6403 if (col == 0)
6404 {
6405 amount = get_indent();
6406 if (find_last_paren(ml_get_curline(), '(', ')')
6407 && (trypos = find_match_paren(ind_maxparen,
6408 ind_maxcomment)) != NULL)
6409 amount = get_indent_lnum(trypos->lnum); /* XXX */
6410 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
6411 amount += ind_cpp_baseclass;
6412 }
6413 else
6414 {
6415 curwin->w_cursor.col = col;
6416 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6417 amount = (int)vcol;
6418 }
6419 if (amount < ind_cpp_baseclass)
6420 amount = ind_cpp_baseclass;
6421 return amount;
6422}
6423
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424/*
6425 * Return TRUE if string "s" ends with the string "find", possibly followed by
6426 * white space and comments. Skip strings and comments.
6427 * Ignore "ignore" after "find" if it's not NULL.
6428 */
6429 static int
6430cin_ends_in(s, find, ignore)
6431 char_u *s;
6432 char_u *find;
6433 char_u *ignore;
6434{
6435 char_u *p = s;
6436 char_u *r;
6437 int len = (int)STRLEN(find);
6438
6439 while (*p != NUL)
6440 {
6441 p = cin_skipcomment(p);
6442 if (STRNCMP(p, find, len) == 0)
6443 {
6444 r = skipwhite(p + len);
6445 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6446 r = skipwhite(r + STRLEN(ignore));
6447 if (cin_nocode(r))
6448 return TRUE;
6449 }
6450 if (*p != NUL)
6451 ++p;
6452 }
6453 return FALSE;
6454}
6455
6456/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006457 * Return TRUE when "s" starts with "word" and then a non-ID character.
6458 */
6459 static int
6460cin_starts_with(s, word)
6461 char_u *s;
6462 char *word;
6463{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006464 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006465
6466 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6467}
6468
6469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 * Skip strings, chars and comments until at or past "trypos".
6471 * Return the column found.
6472 */
6473 static int
6474cin_skip2pos(trypos)
6475 pos_T *trypos;
6476{
6477 char_u *line;
6478 char_u *p;
6479
6480 p = line = ml_get(trypos->lnum);
6481 while (*p && (colnr_T)(p - line) < trypos->col)
6482 {
6483 if (cin_iscomment(p))
6484 p = cin_skipcomment(p);
6485 else
6486 {
6487 p = skip_string(p);
6488 ++p;
6489 }
6490 }
6491 return (int)(p - line);
6492}
6493
6494/*
6495 * Find the '{' at the start of the block we are in.
6496 * Return NULL if no match found.
6497 * Ignore a '{' that is in a comment, makes indenting the next three lines
6498 * work. */
6499/* foo() */
6500/* { */
6501/* } */
6502
6503 static pos_T *
6504find_start_brace(ind_maxcomment) /* XXX */
6505 int ind_maxcomment;
6506{
6507 pos_T cursor_save;
6508 pos_T *trypos;
6509 pos_T *pos;
6510 static pos_T pos_copy;
6511
6512 cursor_save = curwin->w_cursor;
6513 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6514 {
6515 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6516 trypos = &pos_copy;
6517 curwin->w_cursor = *trypos;
6518 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006519 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
6521 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */
6522 break;
6523 if (pos != NULL)
6524 curwin->w_cursor.lnum = pos->lnum;
6525 }
6526 curwin->w_cursor = cursor_save;
6527 return trypos;
6528}
6529
6530/*
6531 * Find the matching '(', failing if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006532 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 */
6534 static pos_T *
6535find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */
6536 int ind_maxparen;
6537 int ind_maxcomment;
6538{
6539 pos_T cursor_save;
6540 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006541 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
6543 cursor_save = curwin->w_cursor;
6544 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6545 {
6546 /* check if the ( is in a // comment */
6547 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6548 trypos = NULL;
6549 else
6550 {
6551 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6552 trypos = &pos_copy;
6553 curwin->w_cursor = *trypos;
6554 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */
6555 trypos = NULL;
6556 }
6557 }
6558 curwin->w_cursor = cursor_save;
6559 return trypos;
6560}
6561
6562/*
6563 * Return ind_maxparen corrected for the difference in line number between the
6564 * cursor position and "startpos". This makes sure that searching for a
6565 * matching paren above the cursor line doesn't find a match because of
6566 * looking a few lines further.
6567 */
6568 static int
6569corr_ind_maxparen(ind_maxparen, startpos)
6570 int ind_maxparen;
6571 pos_T *startpos;
6572{
6573 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6574
6575 if (n > 0 && n < ind_maxparen / 2)
6576 return ind_maxparen - (int)n;
6577 return ind_maxparen;
6578}
6579
6580/*
6581 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006582 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 */
6584 static int
6585find_last_paren(l, start, end)
6586 char_u *l;
6587 int start, end;
6588{
6589 int i;
6590 int retval = FALSE;
6591 int open_count = 0;
6592
6593 curwin->w_cursor.col = 0; /* default is start of line */
6594
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006595 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 {
6597 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6598 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6599 if (l[i] == start)
6600 ++open_count;
6601 else if (l[i] == end)
6602 {
6603 if (open_count > 0)
6604 --open_count;
6605 else
6606 {
6607 curwin->w_cursor.col = i;
6608 retval = TRUE;
6609 }
6610 }
6611 }
6612 return retval;
6613}
6614
6615 int
6616get_c_indent()
6617{
Bram Moolenaar14f24742012-08-08 18:01:05 +02006618 int sw = (int)get_sw_value();
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 /*
6621 * spaces from a block's opening brace the prevailing indent for that
6622 * block should be
6623 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006624
6625 int ind_level = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627 /*
6628 * spaces from the edge of the line an open brace that's at the end of a
6629 * line is imagined to be.
6630 */
6631 int ind_open_imag = 0;
6632
6633 /*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006634 * spaces from the prevailing indent for a line that is not preceded by
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 * an opening brace.
6636 */
6637 int ind_no_brace = 0;
6638
6639 /*
6640 * column where the first { of a function should be located }
6641 */
6642 int ind_first_open = 0;
6643
6644 /*
6645 * spaces from the prevailing indent a leftmost open brace should be
6646 * located
6647 */
6648 int ind_open_extra = 0;
6649
6650 /*
6651 * spaces from the matching open brace (real location for one at the left
6652 * edge; imaginary location from one that ends a line) the matching close
6653 * brace should be located
6654 */
6655 int ind_close_extra = 0;
6656
6657 /*
6658 * spaces from the edge of the line an open brace sitting in the leftmost
6659 * column is imagined to be
6660 */
6661 int ind_open_left_imag = 0;
6662
6663 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006664 * Spaces jump labels should be shifted to the left if N is non-negative,
6665 * otherwise the jump label will be put to column 1.
6666 */
6667 int ind_jump_label = -1;
6668
6669 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 * spaces from the switch() indent a "case xx" label should be located
6671 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006672 int ind_case = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674 /*
6675 * spaces from the "case xx:" code after a switch() should be located
6676 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006677 int ind_case_code = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 /*
6680 * lineup break at end of case in switch() with case label
6681 */
6682 int ind_case_break = 0;
6683
6684 /*
6685 * spaces from the class declaration indent a scope declaration label
6686 * should be located
6687 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006688 int ind_scopedecl = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
6690 /*
6691 * spaces from the scope declaration label code should be located
6692 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006693 int ind_scopedecl_code = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694
6695 /*
6696 * amount K&R-style parameters should be indented
6697 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006698 int ind_param = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
6700 /*
6701 * amount a function type spec should be indented
6702 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006703 int ind_func_type = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
6705 /*
6706 * amount a cpp base class declaration or constructor initialization
6707 * should be indented
6708 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006709 int ind_cpp_baseclass = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
6711 /*
6712 * additional spaces beyond the prevailing indent a continuation line
6713 * should be located
6714 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006715 int ind_continuation = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
6717 /*
6718 * spaces from the indent of the line with an unclosed parentheses
6719 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006720 int ind_unclosed = sw * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722 /*
6723 * spaces from the indent of the line with an unclosed parentheses, which
6724 * itself is also unclosed
6725 */
Bram Moolenaar14f24742012-08-08 18:01:05 +02006726 int ind_unclosed2 = sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727
6728 /*
6729 * suppress ignoring spaces from the indent of a line starting with an
6730 * unclosed parentheses.
6731 */
6732 int ind_unclosed_noignore = 0;
6733
6734 /*
6735 * If the opening paren is the last nonwhite character on the line, and
6736 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6737 * context (for very long lines).
6738 */
6739 int ind_unclosed_wrapped = 0;
6740
6741 /*
6742 * suppress ignoring white space when lining up with the character after
6743 * an unclosed parentheses.
6744 */
6745 int ind_unclosed_whiteok = 0;
6746
6747 /*
6748 * indent a closing parentheses under the line start of the matching
6749 * opening parentheses.
6750 */
6751 int ind_matching_paren = 0;
6752
6753 /*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006754 * indent a closing parentheses under the previous line.
6755 */
6756 int ind_paren_prev = 0;
6757
6758 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 * Extra indent for comments.
6760 */
6761 int ind_comment = 0;
6762
6763 /*
6764 * spaces from the comment opener when there is nothing after it.
6765 */
6766 int ind_in_comment = 3;
6767
6768 /*
6769 * boolean: if non-zero, use ind_in_comment even if there is something
6770 * after the comment opener.
6771 */
6772 int ind_in_comment2 = 0;
6773
6774 /*
6775 * max lines to search for an open paren
6776 */
6777 int ind_maxparen = 20;
6778
6779 /*
6780 * max lines to search for an open comment
6781 */
6782 int ind_maxcomment = 70;
6783
6784 /*
6785 * handle braces for java code
6786 */
6787 int ind_java = 0;
6788
6789 /*
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006790 * not to confuse JS object properties with labels
6791 */
6792 int ind_js = 0;
6793
6794 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 * handle blocked cases correctly
6796 */
6797 int ind_keep_case_label = 0;
6798
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006799 /*
6800 * handle C++ namespace
6801 */
6802 int ind_cpp_namespace = 0;
6803
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006804 /*
6805 * handle continuation lines containing conditions of if(), for() and
6806 * while()
6807 */
6808 int ind_if_for_while = 0;
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 pos_T cur_curpos;
6811 int amount;
6812 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006813 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 colnr_T col;
6815 char_u *theline;
6816 char_u *linecopy;
6817 pos_T *trypos;
6818 pos_T *tryposBrace = NULL;
6819 pos_T our_paren_pos;
6820 char_u *start;
6821 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006822#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823#define BRACE_AT_START 2 /* '{' is at start of line */
6824#define BRACE_AT_END 3 /* '{' is at end of line */
6825 linenr_T ourscope;
6826 char_u *l;
6827 char_u *look;
6828 char_u terminated;
6829 int lookfor;
6830#define LOOKFOR_INITIAL 0
6831#define LOOKFOR_IF 1
6832#define LOOKFOR_DO 2
6833#define LOOKFOR_CASE 3
6834#define LOOKFOR_ANY 4
6835#define LOOKFOR_TERM 5
6836#define LOOKFOR_UNTERM 6
6837#define LOOKFOR_SCOPEDECL 7
6838#define LOOKFOR_NOBREAK 8
6839#define LOOKFOR_CPP_BASECLASS 9
6840#define LOOKFOR_ENUM_OR_INIT 10
6841
6842 int whilelevel;
6843 linenr_T lnum;
6844 char_u *options;
Bram Moolenaar48d27922012-06-13 13:40:48 +02006845 char_u *digits;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 int fraction = 0; /* init for GCC */
6847 int divider;
6848 int n;
6849 int iscase;
6850 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006851 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006853 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006854 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 for (options = curbuf->b_p_cino; *options; )
6857 {
6858 l = options++;
6859 if (*options == '-')
6860 ++options;
Bram Moolenaar48d27922012-06-13 13:40:48 +02006861 digits = options; /* remember where the digits start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 n = getdigits(&options);
6863 divider = 0;
6864 if (*options == '.') /* ".5s" means a fraction */
6865 {
6866 fraction = atol((char *)++options);
6867 while (VIM_ISDIGIT(*options))
6868 {
6869 ++options;
6870 if (divider)
6871 divider *= 10;
6872 else
6873 divider = 10;
6874 }
6875 }
6876 if (*options == 's') /* "2s" means two times 'shiftwidth' */
6877 {
Bram Moolenaar48d27922012-06-13 13:40:48 +02006878 if (options == digits)
Bram Moolenaar14f24742012-08-08 18:01:05 +02006879 n = sw; /* just "s" is one 'shiftwidth' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 else
6881 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02006882 n *= sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (divider)
Bram Moolenaar14f24742012-08-08 18:01:05 +02006884 n += (sw * fraction + divider / 2) / divider;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886 ++options;
6887 }
6888 if (l[1] == '-')
6889 n = -n;
6890 /* When adding an entry here, also update the default 'cinoptions' in
Bram Moolenaar39353fd2007-03-27 09:02:11 +00006891 * doc/indent.txt, and add explanation for it! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 switch (*l)
6893 {
6894 case '>': ind_level = n; break;
6895 case 'e': ind_open_imag = n; break;
6896 case 'n': ind_no_brace = n; break;
6897 case 'f': ind_first_open = n; break;
6898 case '{': ind_open_extra = n; break;
6899 case '}': ind_close_extra = n; break;
6900 case '^': ind_open_left_imag = n; break;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006901 case 'L': ind_jump_label = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 case ':': ind_case = n; break;
6903 case '=': ind_case_code = n; break;
6904 case 'b': ind_case_break = n; break;
6905 case 'p': ind_param = n; break;
6906 case 't': ind_func_type = n; break;
6907 case '/': ind_comment = n; break;
6908 case 'c': ind_in_comment = n; break;
6909 case 'C': ind_in_comment2 = n; break;
6910 case 'i': ind_cpp_baseclass = n; break;
6911 case '+': ind_continuation = n; break;
6912 case '(': ind_unclosed = n; break;
6913 case 'u': ind_unclosed2 = n; break;
6914 case 'U': ind_unclosed_noignore = n; break;
6915 case 'W': ind_unclosed_wrapped = n; break;
6916 case 'w': ind_unclosed_whiteok = n; break;
6917 case 'm': ind_matching_paren = n; break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006918 case 'M': ind_paren_prev = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 case ')': ind_maxparen = n; break;
6920 case '*': ind_maxcomment = n; break;
6921 case 'g': ind_scopedecl = n; break;
6922 case 'h': ind_scopedecl_code = n; break;
6923 case 'j': ind_java = n; break;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006924 case 'J': ind_js = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 case 'l': ind_keep_case_label = n; break;
Bram Moolenaar39353fd2007-03-27 09:02:11 +00006926 case '#': ind_hash_comment = n; break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006927 case 'N': ind_cpp_namespace = n; break;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006928 case 'k': ind_if_for_while = n; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 }
Bram Moolenaardfdf3c42010-03-23 18:22:46 +01006930 if (*options == ',')
6931 ++options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
6933
6934 /* remember where the cursor was when we started */
6935 cur_curpos = curwin->w_cursor;
6936
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006937 /* if we are at line 1 0 is fine, right? */
6938 if (cur_curpos.lnum == 1)
6939 return 0;
6940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 /* Get a copy of the current contents of the line.
6942 * This is required, because only the most recent line obtained with
6943 * ml_get is valid! */
6944 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6945 if (linecopy == NULL)
6946 return 0;
6947
6948 /*
6949 * In insert mode and the cursor is on a ')' truncate the line at the
6950 * cursor position. We don't want to line up with the matching '(' when
6951 * inserting new stuff.
6952 * For unknown reasons the cursor might be past the end of the line, thus
6953 * check for that.
6954 */
6955 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006956 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 && linecopy[curwin->w_cursor.col] == ')')
6958 linecopy[curwin->w_cursor.col] = NUL;
6959
6960 theline = skipwhite(linecopy);
6961
6962 /* move the cursor to the start of the line */
6963
6964 curwin->w_cursor.col = 0;
6965
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006966 original_line_islabel = cin_islabel(ind_maxcomment); /* XXX */
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 /*
6969 * #defines and so on always go at the left when included in 'cinkeys'.
6970 */
6971 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
6972 {
6973 amount = 0;
6974 }
6975
6976 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006977 * Is it a non-case label? Then that goes at the left margin too unless:
6978 * - JS flag is set.
6979 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006981 else if (original_line_islabel && !ind_js && ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
6983 amount = 0;
6984 }
6985
6986 /*
6987 * If we're inside a "//" comment and there is a "//" comment in a
6988 * previous line, lineup with that one.
6989 */
6990 else if (cin_islinecomment(theline)
6991 && (trypos = find_line_comment()) != NULL) /* XXX */
6992 {
6993 /* find how indented the line beginning the comment is */
6994 getvcol(curwin, trypos, &col, NULL, NULL);
6995 amount = col;
6996 }
6997
6998 /*
6999 * If we're inside a comment and not looking at the start of the
7000 * comment, try using the 'comments' option.
7001 */
7002 else if (!cin_iscomment(theline)
7003 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
7004 {
7005 int lead_start_len = 2;
7006 int lead_middle_len = 1;
7007 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7008 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7009 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7010 char_u *p;
7011 int start_align = 0;
7012 int start_off = 0;
7013 int done = FALSE;
7014
7015 /* find how indented the line beginning the comment is */
7016 getvcol(curwin, trypos, &col, NULL, NULL);
7017 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007018 *lead_start = NUL;
7019 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
7021 p = curbuf->b_p_com;
7022 while (*p != NUL)
7023 {
7024 int align = 0;
7025 int off = 0;
7026 int what = 0;
7027
7028 while (*p != NUL && *p != ':')
7029 {
7030 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7031 what = *p++;
7032 else if (*p == COM_LEFT || *p == COM_RIGHT)
7033 align = *p++;
7034 else if (VIM_ISDIGIT(*p) || *p == '-')
7035 off = getdigits(&p);
7036 else
7037 ++p;
7038 }
7039
7040 if (*p == ':')
7041 ++p;
7042 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7043 if (what == COM_START)
7044 {
7045 STRCPY(lead_start, lead_end);
7046 lead_start_len = (int)STRLEN(lead_start);
7047 start_off = off;
7048 start_align = align;
7049 }
7050 else if (what == COM_MIDDLE)
7051 {
7052 STRCPY(lead_middle, lead_end);
7053 lead_middle_len = (int)STRLEN(lead_middle);
7054 }
7055 else if (what == COM_END)
7056 {
7057 /* If our line starts with the middle comment string, line it
7058 * up with the comment opener per the 'comments' option. */
7059 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7060 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7061 {
7062 done = TRUE;
7063 if (curwin->w_cursor.lnum > 1)
7064 {
7065 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007066 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 * the middle comment string matches in the previous
7068 * line, use the indent of that line. XXX */
7069 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7070 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7071 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7072 else if (STRNCMP(look, lead_middle,
7073 lead_middle_len) == 0)
7074 {
7075 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7076 break;
7077 }
7078 /* If the start comment string doesn't match with the
7079 * start of the comment, skip this entry. XXX */
7080 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7081 lead_start, lead_start_len) != 0)
7082 continue;
7083 }
7084 if (start_off != 0)
7085 amount += start_off;
7086 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007087 amount += vim_strsize(lead_start)
7088 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 break;
7090 }
7091
7092 /* If our line starts with the end comment string, line it up
7093 * with the middle comment */
7094 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7095 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7096 {
7097 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7098 /* XXX */
7099 if (off != 0)
7100 amount += off;
7101 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007102 amount += vim_strsize(lead_start)
7103 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 done = TRUE;
7105 break;
7106 }
7107 }
7108 }
7109
7110 /* If our line starts with an asterisk, line up with the
7111 * asterisk in the comment opener; otherwise, line up
7112 * with the first character of the comment text.
7113 */
7114 if (done)
7115 ;
7116 else if (theline[0] == '*')
7117 amount += 1;
7118 else
7119 {
7120 /*
7121 * If we are more than one line away from the comment opener, take
7122 * the indent of the previous non-empty line. If 'cino' has "CO"
7123 * and we are just below the comment opener and there are any
7124 * white characters after it line up with the text after it;
7125 * otherwise, add the amount specified by "c" in 'cino'
7126 */
7127 amount = -1;
7128 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7129 {
7130 if (linewhite(lnum)) /* skip blank lines */
7131 continue;
7132 amount = get_indent_lnum(lnum); /* XXX */
7133 break;
7134 }
7135 if (amount == -1) /* use the comment opener */
7136 {
7137 if (!ind_in_comment2)
7138 {
7139 start = ml_get(trypos->lnum);
7140 look = start + trypos->col + 2; /* skip / and * */
7141 if (*look != NUL) /* if something after it */
7142 trypos->col = (colnr_T)(skipwhite(look) - start);
7143 }
7144 getvcol(curwin, trypos, &col, NULL, NULL);
7145 amount = col;
7146 if (ind_in_comment2 || *look == NUL)
7147 amount += ind_in_comment;
7148 }
7149 }
7150 }
7151
7152 /*
7153 * Are we inside parentheses or braces?
7154 */ /* XXX */
7155 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL
7156 && ind_java == 0)
7157 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL
7158 || trypos != NULL)
7159 {
7160 if (trypos != NULL && tryposBrace != NULL)
7161 {
7162 /* Both an unmatched '(' and '{' is found. Use the one which is
7163 * closer to the current cursor position, set the other to NULL. */
7164 if (trypos->lnum != tryposBrace->lnum
7165 ? trypos->lnum < tryposBrace->lnum
7166 : trypos->col < tryposBrace->col)
7167 trypos = NULL;
7168 else
7169 tryposBrace = NULL;
7170 }
7171
7172 if (trypos != NULL)
7173 {
7174 /*
7175 * If the matching paren is more than one line away, use the indent of
7176 * a previous non-empty line that matches the same paren.
7177 */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007178 if (theline[0] == ')' && ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007180 /* Line up with the start of the matching paren line. */
7181 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7182 }
7183 else
7184 {
7185 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007186 our_paren_pos = *trypos;
7187 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007189 l = skipwhite(ml_get(lnum));
7190 if (cin_nocode(l)) /* skip comment lines */
7191 continue;
7192 if (cin_ispreproc_cont(&l, &lnum))
7193 continue; /* ignore #define, #if, etc. */
7194 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007196 /* Skip a comment. XXX */
7197 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7198 {
7199 lnum = trypos->lnum + 1;
7200 continue;
7201 }
7202
7203 /* XXX */
7204 if ((trypos = find_match_paren(
7205 corr_ind_maxparen(ind_maxparen, &cur_curpos),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 ind_maxcomment)) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007207 && trypos->lnum == our_paren_pos.lnum
7208 && trypos->col == our_paren_pos.col)
7209 {
7210 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007212 if (theline[0] == ')')
7213 {
7214 if (our_paren_pos.lnum != lnum
7215 && cur_amount > amount)
7216 cur_amount = amount;
7217 amount = -1;
7218 }
7219 break;
7220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222 }
7223
7224 /*
7225 * Line up with line where the matching paren is. XXX
7226 * If the line starts with a '(' or the indent for unclosed
7227 * parentheses is zero, line up with the unclosed parentheses.
7228 */
7229 if (amount == -1)
7230 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007231 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007232 int is_if_for_while = 0;
7233
7234 if (ind_if_for_while)
7235 {
7236 /* Look for the outermost opening parenthesis on this line
7237 * and check whether it belongs to an "if", "for" or "while". */
7238
7239 pos_T cursor_save = curwin->w_cursor;
7240 pos_T outermost;
7241 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007242
7243 trypos = &our_paren_pos;
7244 do {
7245 outermost = *trypos;
7246 curwin->w_cursor.lnum = outermost.lnum;
7247 curwin->w_cursor.col = outermost.col;
7248
7249 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
7250 } while (trypos && trypos->lnum == outermost.lnum);
7251
7252 curwin->w_cursor = cursor_save;
7253
7254 line = ml_get(outermost.lnum);
7255
7256 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007257 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007258 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007259
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007261 look = skipwhite(look);
7262 if (*look == '(')
7263 {
7264 linenr_T save_lnum = curwin->w_cursor.lnum;
7265 char_u *line;
7266 int look_col;
7267
7268 /* Ignore a '(' in front of the line that has a match before
7269 * our matching '('. */
7270 curwin->w_cursor.lnum = our_paren_pos.lnum;
7271 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007272 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007273 curwin->w_cursor.col = look_col + 1;
7274 if ((trypos = findmatchlimit(NULL, ')', 0, ind_maxparen))
7275 != NULL
7276 && trypos->lnum == our_paren_pos.lnum
7277 && trypos->col < our_paren_pos.col)
7278 ignore_paren_col = trypos->col + 1;
7279
7280 curwin->w_cursor.lnum = save_lnum;
7281 look = ml_get(our_paren_pos.lnum) + look_col;
7282 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007283 if (theline[0] == ')' || (ind_unclosed == 0 && is_if_for_while == 0)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007284 || (!ind_unclosed_noignore && *look == '('
7285 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 {
7287 /*
7288 * If we're looking at a close paren, line up right there;
7289 * otherwise, line up with the next (non-white) character.
7290 * When ind_unclosed_wrapped is set and the matching paren is
7291 * the last nonwhite character of the line, use either the
7292 * indent of the current line or the indentation of the next
7293 * outer paren and add ind_unclosed_wrapped (for very long
7294 * lines).
7295 */
7296 if (theline[0] != ')')
7297 {
7298 cur_amount = MAXCOL;
7299 l = ml_get(our_paren_pos.lnum);
7300 if (ind_unclosed_wrapped
7301 && cin_ends_in(l, (char_u *)"(", NULL))
7302 {
7303 /* look for opening unmatched paren, indent one level
7304 * for each additional level */
7305 n = 1;
7306 for (col = 0; col < our_paren_pos.col; ++col)
7307 {
7308 switch (l[col])
7309 {
7310 case '(':
7311 case '{': ++n;
7312 break;
7313
7314 case ')':
7315 case '}': if (n > 1)
7316 --n;
7317 break;
7318 }
7319 }
7320
7321 our_paren_pos.col = 0;
7322 amount += n * ind_unclosed_wrapped;
7323 }
7324 else if (ind_unclosed_whiteok)
7325 our_paren_pos.col++;
7326 else
7327 {
7328 col = our_paren_pos.col + 1;
7329 while (vim_iswhite(l[col]))
7330 col++;
7331 if (l[col] != NUL) /* In case of trailing space */
7332 our_paren_pos.col = col;
7333 else
7334 our_paren_pos.col++;
7335 }
7336 }
7337
7338 /*
7339 * Find how indented the paren is, or the character after it
7340 * if we did the above "if".
7341 */
7342 if (our_paren_pos.col > 0)
7343 {
7344 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7345 if (cur_amount > (int)col)
7346 cur_amount = col;
7347 }
7348 }
7349
7350 if (theline[0] == ')' && ind_matching_paren)
7351 {
7352 /* Line up with the start of the matching paren line. */
7353 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007354 else if ((ind_unclosed == 0 && is_if_for_while == 0)
7355 || (!ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007356 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 {
7358 if (cur_amount != MAXCOL)
7359 amount = cur_amount;
7360 }
7361 else
7362 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007363 /* Add ind_unclosed2 for each '(' before our matching one, but
7364 * ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007366 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
7368 --our_paren_pos.col;
7369 switch (*ml_get_pos(&our_paren_pos))
7370 {
7371 case '(': amount += ind_unclosed2;
7372 col = our_paren_pos.col;
7373 break;
7374 case ')': amount -= ind_unclosed2;
7375 col = MAXCOL;
7376 break;
7377 }
7378 }
7379
7380 /* Use ind_unclosed once, when the first '(' is not inside
7381 * braces */
7382 if (col == MAXCOL)
7383 amount += ind_unclosed;
7384 else
7385 {
7386 curwin->w_cursor.lnum = our_paren_pos.lnum;
7387 curwin->w_cursor.col = col;
Bram Moolenaar367bec82011-04-11 14:26:19 +02007388 if (find_match_paren(ind_maxparen, ind_maxcomment) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 amount += ind_unclosed2;
7390 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007391 {
7392 if (is_if_for_while)
7393 amount += ind_if_for_while;
7394 else
7395 amount += ind_unclosed;
7396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
7398 /*
7399 * For a line starting with ')' use the minimum of the two
7400 * positions, to avoid giving it more indent than the previous
7401 * lines:
7402 * func_long_name( if (x
7403 * arg && yy
7404 * ) ^ not here ) ^ not here
7405 */
7406 if (cur_amount < amount)
7407 amount = cur_amount;
7408 }
7409 }
7410
7411 /* add extra indent for a comment */
7412 if (cin_iscomment(theline))
7413 amount += ind_comment;
7414 }
7415
7416 /*
7417 * Are we at least inside braces, then?
7418 */
7419 else
7420 {
7421 trypos = tryposBrace;
7422
7423 ourscope = trypos->lnum;
7424 start = ml_get(ourscope);
7425
7426 /*
7427 * Now figure out how indented the line is in general.
7428 * If the brace was at the start of the line, we use that;
7429 * otherwise, check out the indentation of the line as
7430 * a whole and then add the "imaginary indent" to that.
7431 */
7432 look = skipwhite(start);
7433 if (*look == '{')
7434 {
7435 getvcol(curwin, trypos, &col, NULL, NULL);
7436 amount = col;
7437 if (*start == '{')
7438 start_brace = BRACE_IN_COL0;
7439 else
7440 start_brace = BRACE_AT_START;
7441 }
7442 else
7443 {
7444 /*
7445 * that opening brace might have been on a continuation
7446 * line. if so, find the start of the line.
7447 */
7448 curwin->w_cursor.lnum = ourscope;
7449
7450 /*
7451 * position the cursor over the rightmost paren, so that
7452 * matching it will take us back to the start of the line.
7453 */
7454 lnum = ourscope;
7455 if (find_last_paren(start, '(', ')')
7456 && (trypos = find_match_paren(ind_maxparen,
7457 ind_maxcomment)) != NULL)
7458 lnum = trypos->lnum;
7459
7460 /*
7461 * It could have been something like
7462 * case 1: if (asdf &&
7463 * ldfd) {
7464 * }
7465 */
Bram Moolenaar6ec154b2011-06-12 21:51:08 +02007466 if (ind_js || (ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007467 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 amount = get_indent();
7469 else
7470 amount = skip_label(lnum, &l, ind_maxcomment);
7471
7472 start_brace = BRACE_AT_END;
7473 }
7474
7475 /*
7476 * if we're looking at a closing brace, that's where
7477 * we want to be. otherwise, add the amount of room
7478 * that an indent is supposed to be.
7479 */
7480 if (theline[0] == '}')
7481 {
7482 /*
7483 * they may want closing braces to line up with something
7484 * other than the open brace. indulge them, if so.
7485 */
7486 amount += ind_close_extra;
7487 }
7488 else
7489 {
7490 /*
7491 * If we're looking at an "else", try to find an "if"
7492 * to match it with.
7493 * If we're looking at a "while", try to find a "do"
7494 * to match it with.
7495 */
7496 lookfor = LOOKFOR_INITIAL;
7497 if (cin_iselse(theline))
7498 lookfor = LOOKFOR_IF;
7499 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen))
7500 /* XXX */
7501 lookfor = LOOKFOR_DO;
7502 if (lookfor != LOOKFOR_INITIAL)
7503 {
7504 curwin->w_cursor.lnum = cur_curpos.lnum;
7505 if (find_match(lookfor, ourscope, ind_maxparen,
7506 ind_maxcomment) == OK)
7507 {
7508 amount = get_indent(); /* XXX */
7509 goto theend;
7510 }
7511 }
7512
7513 /*
7514 * We get here if we are not on an "while-of-do" or "else" (or
7515 * failed to find a matching "if").
7516 * Search backwards for something to line up with.
7517 * First set amount for when we don't find anything.
7518 */
7519
7520 /*
7521 * if the '{' is _really_ at the left margin, use the imaginary
7522 * location of a left-margin brace. Otherwise, correct the
7523 * location for ind_open_extra.
7524 */
7525
7526 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7527 {
7528 amount = ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007529 lookfor_cpp_namespace = TRUE;
7530 }
7531 else if (start_brace == BRACE_AT_START &&
7532 lookfor_cpp_namespace) /* '{' is at start */
7533 {
7534
7535 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 }
7537 else
7538 {
7539 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 amount += ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007542
7543 l = skipwhite(ml_get_curline());
7544 if (cin_is_cpp_namespace(l))
7545 amount += ind_cpp_namespace;
7546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 else
7548 {
7549 /* Compensate for adding ind_open_extra later. */
7550 amount -= ind_open_extra;
7551 if (amount < 0)
7552 amount = 0;
7553 }
7554 }
7555
7556 lookfor_break = FALSE;
7557
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007558 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
7560 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
7561 amount += ind_case;
7562 }
7563 else if (cin_isscopedecl(theline)) /* private:, ... */
7564 {
7565 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
7566 amount += ind_scopedecl;
7567 }
7568 else
7569 {
7570 if (ind_case_break && cin_isbreak(theline)) /* break; ... */
7571 lookfor_break = TRUE;
7572
7573 lookfor = LOOKFOR_INITIAL;
7574 amount += ind_level; /* ind_level from start of block */
7575 }
7576 scope_amount = amount;
7577 whilelevel = 0;
7578
7579 /*
7580 * Search backwards. If we find something we recognize, line up
7581 * with that.
7582 *
7583 * if we're looking at an open brace, indent
7584 * the usual amount relative to the conditional
7585 * that opens the block.
7586 */
7587 curwin->w_cursor = cur_curpos;
7588 for (;;)
7589 {
7590 curwin->w_cursor.lnum--;
7591 curwin->w_cursor.col = 0;
7592
7593 /*
7594 * If we went all the way back to the start of our scope, line
7595 * up with it.
7596 */
7597 if (curwin->w_cursor.lnum <= ourscope)
7598 {
7599 /* we reached end of scope:
7600 * if looking for a enum or structure initialization
7601 * go further back:
7602 * if it is an initializer (enum xxx or xxx =), then
7603 * don't add ind_continuation, otherwise it is a variable
7604 * declaration:
7605 * int x,
7606 * here; <-- add ind_continuation
7607 */
7608 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7609 {
7610 if (curwin->w_cursor.lnum == 0
7611 || curwin->w_cursor.lnum
7612 < ourscope - ind_maxparen)
7613 {
7614 /* nothing found (abuse ind_maxparen as limit)
7615 * assume terminated line (i.e. a variable
7616 * initialization) */
7617 if (cont_amount > 0)
7618 amount = cont_amount;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007619 else if (!ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 amount += ind_continuation;
7621 break;
7622 }
7623
7624 l = ml_get_curline();
7625
7626 /*
7627 * If we're in a comment now, skip to the start of the
7628 * comment.
7629 */
7630 trypos = find_start_comment(ind_maxcomment);
7631 if (trypos != NULL)
7632 {
7633 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007634 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 continue;
7636 }
7637
7638 /*
7639 * Skip preprocessor directives and blank lines.
7640 */
7641 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7642 continue;
7643
7644 if (cin_nocode(l))
7645 continue;
7646
7647 terminated = cin_isterminated(l, FALSE, TRUE);
7648
7649 /*
7650 * If we are at top level and the line looks like a
7651 * function declaration, we are done
7652 * (it's a variable declaration).
7653 */
7654 if (start_brace != BRACE_IN_COL0
Bram Moolenaarc367faa2011-12-14 20:21:35 +01007655 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum,
7656 0, ind_maxparen, ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
7658 /* if the line is terminated with another ','
7659 * it is a continued variable initialization.
7660 * don't add extra indent.
7661 * TODO: does not work, if a function
7662 * declaration is split over multiple lines:
7663 * cin_isfuncdecl returns FALSE then.
7664 */
7665 if (terminated == ',')
7666 break;
7667
7668 /* if it es a enum declaration or an assignment,
7669 * we are done.
7670 */
7671 if (terminated != ';' && cin_isinit())
7672 break;
7673
7674 /* nothing useful found */
7675 if (terminated == 0 || terminated == '{')
7676 continue;
7677 }
7678
7679 if (terminated != ';')
7680 {
7681 /* Skip parens and braces. Position the cursor
7682 * over the rightmost paren, so that matching it
7683 * will take us back to the start of the line.
7684 */ /* XXX */
7685 trypos = NULL;
7686 if (find_last_paren(l, '(', ')'))
7687 trypos = find_match_paren(ind_maxparen,
7688 ind_maxcomment);
7689
7690 if (trypos == NULL && find_last_paren(l, '{', '}'))
7691 trypos = find_start_brace(ind_maxcomment);
7692
7693 if (trypos != NULL)
7694 {
7695 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007696 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 continue;
7698 }
7699 }
7700
7701 /* it's a variable declaration, add indentation
7702 * like in
7703 * int a,
7704 * b;
7705 */
7706 if (cont_amount > 0)
7707 amount = cont_amount;
7708 else
7709 amount += ind_continuation;
7710 }
7711 else if (lookfor == LOOKFOR_UNTERM)
7712 {
7713 if (cont_amount > 0)
7714 amount = cont_amount;
7715 else
7716 amount += ind_continuation;
7717 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007718 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007719 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007720 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007722 {
7723 amount = scope_amount;
7724 if (theline[0] == '{')
7725 {
7726 amount += ind_open_extra;
7727 added_to_amount = ind_open_extra;
7728 }
7729 }
7730
7731 if (lookfor_cpp_namespace)
7732 {
7733 /*
7734 * Looking for C++ namespace, need to look further
7735 * back.
7736 */
7737 if (curwin->w_cursor.lnum == ourscope)
7738 continue;
7739
7740 if (curwin->w_cursor.lnum == 0
7741 || curwin->w_cursor.lnum
7742 < ourscope - FIND_NAMESPACE_LIM)
7743 break;
7744
7745 l = ml_get_curline();
7746
7747 /* If we're in a comment now, skip to the start of
7748 * the comment. */
7749 trypos = find_start_comment(ind_maxcomment);
7750 if (trypos != NULL)
7751 {
7752 curwin->w_cursor.lnum = trypos->lnum + 1;
7753 curwin->w_cursor.col = 0;
7754 continue;
7755 }
7756
7757 /* Skip preprocessor directives and blank lines. */
7758 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7759 continue;
7760
7761 /* Finally the actual check for "namespace". */
7762 if (cin_is_cpp_namespace(l))
7763 {
7764 amount += ind_cpp_namespace - added_to_amount;
7765 break;
7766 }
7767
7768 if (cin_nocode(l))
7769 continue;
7770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 }
7772 break;
7773 }
7774
7775 /*
7776 * If we're in a comment now, skip to the start of the comment.
7777 */ /* XXX */
7778 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7779 {
7780 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007781 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 continue;
7783 }
7784
7785 l = ml_get_curline();
7786
7787 /*
7788 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007789 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007791 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 if (iscase || cin_isscopedecl(l))
7793 {
7794 /* we are only looking for cpp base class
7795 * declaration/initialization any longer */
7796 if (lookfor == LOOKFOR_CPP_BASECLASS)
7797 break;
7798
7799 /* When looking for a "do" we are not interested in
7800 * labels. */
7801 if (whilelevel > 0)
7802 continue;
7803
7804 /*
7805 * case xx:
7806 * c = 99 + <- this indent plus continuation
7807 *-> here;
7808 */
7809 if (lookfor == LOOKFOR_UNTERM
7810 || lookfor == LOOKFOR_ENUM_OR_INIT)
7811 {
7812 if (cont_amount > 0)
7813 amount = cont_amount;
7814 else
7815 amount += ind_continuation;
7816 break;
7817 }
7818
7819 /*
7820 * case xx: <- line up with this case
7821 * x = 333;
7822 * case yy:
7823 */
7824 if ( (iscase && lookfor == LOOKFOR_CASE)
7825 || (iscase && lookfor_break)
7826 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7827 {
7828 /*
7829 * Check that this case label is not for another
7830 * switch()
7831 */ /* XXX */
7832 if ((trypos = find_start_brace(ind_maxcomment)) ==
7833 NULL || trypos->lnum == ourscope)
7834 {
7835 amount = get_indent(); /* XXX */
7836 break;
7837 }
7838 continue;
7839 }
7840
7841 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7842
7843 /*
7844 * case xx: if (cond) <- line up with this if
7845 * y = y + 1;
7846 * -> s = 99;
7847 *
7848 * case xx:
7849 * if (cond) <- line up with this line
7850 * y = y + 1;
7851 * -> s = 99;
7852 */
7853 if (lookfor == LOOKFOR_TERM)
7854 {
7855 if (n)
7856 amount = n;
7857
7858 if (!lookfor_break)
7859 break;
7860 }
7861
7862 /*
7863 * case xx: x = x + 1; <- line up with this x
7864 * -> y = y + 1;
7865 *
7866 * case xx: if (cond) <- line up with this if
7867 * -> y = y + 1;
7868 */
7869 if (n)
7870 {
7871 amount = n;
7872 l = after_label(ml_get_curline());
7873 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007874 {
7875 if (theline[0] == '{')
7876 amount += ind_open_extra;
7877 else
7878 amount += ind_level + ind_no_brace;
7879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 break;
7881 }
7882
7883 /*
7884 * Try to get the indent of a statement before the switch
7885 * label. If nothing is found, line up relative to the
7886 * switch label.
7887 * break; <- may line up with this line
7888 * case xx:
7889 * -> y = 1;
7890 */
7891 scope_amount = get_indent() + (iscase /* XXX */
7892 ? ind_case_code : ind_scopedecl_code);
7893 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
7894 continue;
7895 }
7896
7897 /*
7898 * Looking for a switch() label or C++ scope declaration,
7899 * ignore other lines, skip {}-blocks.
7900 */
7901 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7902 {
7903 if (find_last_paren(l, '{', '}') && (trypos =
7904 find_start_brace(ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007907 curwin->w_cursor.col = 0;
7908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 continue;
7910 }
7911
7912 /*
7913 * Ignore jump labels with nothing after them.
7914 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007915 if (!ind_js && cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
7917 l = after_label(ml_get_curline());
7918 if (l == NULL || cin_nocode(l))
7919 continue;
7920 }
7921
7922 /*
7923 * Ignore #defines, #if, etc.
7924 * Ignore comment and empty lines.
7925 * (need to get the line again, cin_islabel() may have
7926 * unlocked it)
7927 */
7928 l = ml_get_curline();
7929 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7930 || cin_nocode(l))
7931 continue;
7932
7933 /*
7934 * Are we at the start of a cpp base class declaration or
7935 * constructor initialization?
7936 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007937 n = FALSE;
7938 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0)
7939 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007940 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007941 l = ml_get_curline();
7942 }
7943 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {
7945 if (lookfor == LOOKFOR_UNTERM)
7946 {
7947 if (cont_amount > 0)
7948 amount = cont_amount;
7949 else
7950 amount += ind_continuation;
7951 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007952 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007954 /* Need to find start of the declaration. */
7955 lookfor = LOOKFOR_UNTERM;
7956 ind_continuation = 0;
7957 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007960 /* XXX */
7961 amount = get_baseclass_amount(col, ind_maxparen,
7962 ind_maxcomment, ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 break;
7964 }
7965 else if (lookfor == LOOKFOR_CPP_BASECLASS)
7966 {
7967 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00007968 * declaration or initialization before the opening brace.
7969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (cin_isterminated(l, TRUE, FALSE))
7971 break;
7972 else
7973 continue;
7974 }
7975
7976 /*
7977 * What happens next depends on the line being terminated.
7978 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00007979 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 * 123,
7981 * sizeof
7982 * here
7983 * Otherwise check whether it is a enumeration or structure
7984 * initialisation (not indented) or a variable declaration
7985 * (indented).
7986 */
7987 terminated = cin_isterminated(l, FALSE, TRUE);
7988
7989 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
7990 && terminated == ','))
7991 {
7992 /*
7993 * if we're in the middle of a paren thing,
7994 * go back to the line that starts it so
7995 * we can get the right prevailing indent
7996 * if ( foo &&
7997 * bar )
7998 */
7999 /*
8000 * position the cursor over the rightmost paren, so that
8001 * matching it will take us back to the start of the line.
8002 */
8003 (void)find_last_paren(l, '(', ')');
8004 trypos = find_match_paren(
8005 corr_ind_maxparen(ind_maxparen, &cur_curpos),
8006 ind_maxcomment);
8007
8008 /*
8009 * If we are looking for ',', we also look for matching
8010 * braces.
8011 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008012 if (trypos == NULL && terminated == ','
8013 && find_last_paren(l, '{', '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 trypos = find_start_brace(ind_maxcomment);
8015
8016 if (trypos != NULL)
8017 {
8018 /*
8019 * Check if we are on a case label now. This is
8020 * handled above.
8021 * case xx: if ( asdf &&
8022 * asdf)
8023 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008024 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008026 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 {
8028 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008029 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 continue;
8031 }
8032 }
8033
8034 /*
8035 * Skip over continuation lines to find the one to get the
8036 * indent from
8037 * char *usethis = "bla\
8038 * bla",
8039 * here;
8040 */
8041 if (terminated == ',')
8042 {
8043 while (curwin->w_cursor.lnum > 1)
8044 {
8045 l = ml_get(curwin->w_cursor.lnum - 1);
8046 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8047 break;
8048 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008049 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 }
8051 }
8052
8053 /*
8054 * Get indent and pointer to text for current line,
8055 * ignoring any jump label. XXX
8056 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008057 if (!ind_js)
8058 cur_amount = skip_label(curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 &l, ind_maxcomment);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008060 else
8061 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 /*
8063 * If this is just above the line we are indenting, and it
8064 * starts with a '{', line it up with this line.
8065 * while (not)
8066 * -> {
8067 * }
8068 */
8069 if (terminated != ',' && lookfor != LOOKFOR_TERM
8070 && theline[0] == '{')
8071 {
8072 amount = cur_amount;
8073 /*
8074 * Only add ind_open_extra when the current line
8075 * doesn't start with a '{', which must have a match
8076 * in the same line (scope is the same). Probably:
8077 * { 1, 2 },
8078 * -> { 3, 4 }
8079 */
8080 if (*skipwhite(l) != '{')
8081 amount += ind_open_extra;
8082
8083 if (ind_cpp_baseclass)
8084 {
8085 /* have to look back, whether it is a cpp base
8086 * class declaration or initialization */
8087 lookfor = LOOKFOR_CPP_BASECLASS;
8088 continue;
8089 }
8090 break;
8091 }
8092
8093 /*
8094 * Check if we are after an "if", "while", etc.
8095 * Also allow " } else".
8096 */
8097 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8098 {
8099 /*
8100 * Found an unterminated line after an if (), line up
8101 * with the last one.
8102 * if (cond)
8103 * 100 +
8104 * -> here;
8105 */
8106 if (lookfor == LOOKFOR_UNTERM
8107 || lookfor == LOOKFOR_ENUM_OR_INIT)
8108 {
8109 if (cont_amount > 0)
8110 amount = cont_amount;
8111 else
8112 amount += ind_continuation;
8113 break;
8114 }
8115
8116 /*
8117 * If this is just above the line we are indenting, we
8118 * are finished.
8119 * while (not)
8120 * -> here;
8121 * Otherwise this indent can be used when the line
8122 * before this is terminated.
8123 * yyy;
8124 * if (stat)
8125 * while (not)
8126 * xxx;
8127 * -> here;
8128 */
8129 amount = cur_amount;
8130 if (theline[0] == '{')
8131 amount += ind_open_extra;
8132 if (lookfor != LOOKFOR_TERM)
8133 {
8134 amount += ind_level + ind_no_brace;
8135 break;
8136 }
8137
8138 /*
8139 * Special trick: when expecting the while () after a
8140 * do, line up with the while()
8141 * do
8142 * x = 1;
8143 * -> here
8144 */
8145 l = skipwhite(ml_get_curline());
8146 if (cin_isdo(l))
8147 {
8148 if (whilelevel == 0)
8149 break;
8150 --whilelevel;
8151 }
8152
8153 /*
8154 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008155 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 * Need to use the scope of this "else". XXX
8157 * If whilelevel != 0 continue looking for a "do {".
8158 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008159 if (cin_iselse(l) && whilelevel == 0)
8160 {
8161 /* If we're looking at "} else", let's make sure we
8162 * find the opening brace of the enclosing scope,
8163 * not the one from "if () {". */
8164 if (*l == '}')
8165 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008166 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008167
8168 if ((trypos = find_start_brace(ind_maxcomment))
8169 == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 || find_match(LOOKFOR_IF, trypos->lnum,
Bram Moolenaar334adf02011-05-25 13:34:04 +02008171 ind_maxparen, ind_maxcomment) == FAIL)
8172 break;
8173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 }
8175
8176 /*
8177 * If we're below an unterminated line that is not an
8178 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008179 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 * the line before this one.
8181 */
8182 else
8183 {
8184 /*
8185 * Found two unterminated lines on a row, line up with
8186 * the last one.
8187 * c = 99 +
8188 * 100 +
8189 * -> here;
8190 */
8191 if (lookfor == LOOKFOR_UNTERM)
8192 {
8193 /* When line ends in a comma add extra indent */
8194 if (terminated == ',')
8195 amount += ind_continuation;
8196 break;
8197 }
8198
8199 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8200 {
8201 /* Found two lines ending in ',', lineup with the
8202 * lowest one, but check for cpp base class
8203 * declaration/initialization, if it is an
8204 * opening brace or we are looking just for
8205 * enumerations/initializations. */
8206 if (terminated == ',')
8207 {
8208 if (ind_cpp_baseclass == 0)
8209 break;
8210
8211 lookfor = LOOKFOR_CPP_BASECLASS;
8212 continue;
8213 }
8214
8215 /* Ignore unterminated lines in between, but
8216 * reduce indent. */
8217 if (amount > cur_amount)
8218 amount = cur_amount;
8219 }
8220 else
8221 {
8222 /*
8223 * Found first unterminated line on a row, may
8224 * line up with this line, remember its indent
8225 * 100 +
8226 * -> here;
8227 */
8228 amount = cur_amount;
8229
8230 /*
8231 * If previous line ends in ',', check whether we
8232 * are in an initialization or enum
8233 * struct xxx =
8234 * {
8235 * sizeof a,
8236 * 124 };
8237 * or a normal possible continuation line.
8238 * but only, of no other statement has been found
8239 * yet.
8240 */
8241 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8242 {
8243 lookfor = LOOKFOR_ENUM_OR_INIT;
8244 cont_amount = cin_first_id_amount();
8245 }
8246 else
8247 {
8248 if (lookfor == LOOKFOR_INITIAL
8249 && *l != NUL
8250 && l[STRLEN(l) - 1] == '\\')
8251 /* XXX */
8252 cont_amount = cin_get_equal_amount(
8253 curwin->w_cursor.lnum);
8254 if (lookfor != LOOKFOR_TERM)
8255 lookfor = LOOKFOR_UNTERM;
8256 }
8257 }
8258 }
8259 }
8260
8261 /*
8262 * Check if we are after a while (cond);
8263 * If so: Ignore until the matching "do".
8264 */
8265 /* XXX */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008266 else if (cin_iswhileofdo_end(terminated, ind_maxparen,
8267 ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 {
8269 /*
8270 * Found an unterminated line after a while ();, line up
8271 * with the last one.
8272 * while (cond);
8273 * 100 + <- line up with this one
8274 * -> here;
8275 */
8276 if (lookfor == LOOKFOR_UNTERM
8277 || lookfor == LOOKFOR_ENUM_OR_INIT)
8278 {
8279 if (cont_amount > 0)
8280 amount = cont_amount;
8281 else
8282 amount += ind_continuation;
8283 break;
8284 }
8285
8286 if (whilelevel == 0)
8287 {
8288 lookfor = LOOKFOR_TERM;
8289 amount = get_indent(); /* XXX */
8290 if (theline[0] == '{')
8291 amount += ind_open_extra;
8292 }
8293 ++whilelevel;
8294 }
8295
8296 /*
8297 * We are after a "normal" statement.
8298 * If we had another statement we can stop now and use the
8299 * indent of that other statement.
8300 * Otherwise the indent of the current statement may be used,
8301 * search backwards for the next "normal" statement.
8302 */
8303 else
8304 {
8305 /*
8306 * Skip single break line, if before a switch label. It
8307 * may be lined up with the case label.
8308 */
8309 if (lookfor == LOOKFOR_NOBREAK
8310 && cin_isbreak(skipwhite(ml_get_curline())))
8311 {
8312 lookfor = LOOKFOR_ANY;
8313 continue;
8314 }
8315
8316 /*
8317 * Handle "do {" line.
8318 */
8319 if (whilelevel > 0)
8320 {
8321 l = cin_skipcomment(ml_get_curline());
8322 if (cin_isdo(l))
8323 {
8324 amount = get_indent(); /* XXX */
8325 --whilelevel;
8326 continue;
8327 }
8328 }
8329
8330 /*
8331 * Found a terminated line above an unterminated line. Add
8332 * the amount for a continuation line.
8333 * x = 1;
8334 * y = foo +
8335 * -> here;
8336 * or
8337 * int x = 1;
8338 * int foo,
8339 * -> here;
8340 */
8341 if (lookfor == LOOKFOR_UNTERM
8342 || lookfor == LOOKFOR_ENUM_OR_INIT)
8343 {
8344 if (cont_amount > 0)
8345 amount = cont_amount;
8346 else
8347 amount += ind_continuation;
8348 break;
8349 }
8350
8351 /*
8352 * Found a terminated line above a terminated line or "if"
8353 * etc. line. Use the amount of the line below us.
8354 * x = 1; x = 1;
8355 * if (asdf) y = 2;
8356 * while (asdf) ->here;
8357 * here;
8358 * ->foo;
8359 */
8360 if (lookfor == LOOKFOR_TERM)
8361 {
8362 if (!lookfor_break && whilelevel == 0)
8363 break;
8364 }
8365
8366 /*
8367 * First line above the one we're indenting is terminated.
8368 * To know what needs to be done look further backward for
8369 * a terminated line.
8370 */
8371 else
8372 {
8373 /*
8374 * position the cursor over the rightmost paren, so
8375 * that matching it will take us back to the start of
8376 * the line. Helps for:
8377 * func(asdr,
8378 * asdfasdf);
8379 * here;
8380 */
8381term_again:
8382 l = ml_get_curline();
8383 if (find_last_paren(l, '(', ')')
8384 && (trypos = find_match_paren(ind_maxparen,
8385 ind_maxcomment)) != NULL)
8386 {
8387 /*
8388 * Check if we are on a case label now. This is
8389 * handled above.
8390 * case xx: if ( asdf &&
8391 * asdf)
8392 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008393 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008395 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {
8397 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008398 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 continue;
8400 }
8401 }
8402
8403 /* When aligning with the case statement, don't align
8404 * with a statement after it.
8405 * case 1: { <-- don't use this { position
8406 * stat;
8407 * }
8408 * case 2:
8409 * stat;
8410 * }
8411 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008412 iscase = (ind_keep_case_label && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
8414 /*
8415 * Get indent and pointer to text for current line,
8416 * ignoring any jump label.
8417 */
8418 amount = skip_label(curwin->w_cursor.lnum,
8419 &l, ind_maxcomment);
8420
8421 if (theline[0] == '{')
8422 amount += ind_open_extra;
8423 /* See remark above: "Only add ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008424 l = skipwhite(l);
8425 if (*l == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 amount -= ind_open_extra;
8427 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8428
8429 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008430 * When a terminated line starts with "else" skip to
8431 * the matching "if":
8432 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008433 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008434 * Need to use the scope of this "else". XXX
8435 * If whilelevel != 0 continue looking for a "do {".
8436 */
8437 if (lookfor == LOOKFOR_TERM
8438 && *l != '}'
8439 && cin_iselse(l)
8440 && whilelevel == 0)
8441 {
8442 if ((trypos = find_start_brace(ind_maxcomment))
8443 == NULL
8444 || find_match(LOOKFOR_IF, trypos->lnum,
8445 ind_maxparen, ind_maxcomment) == FAIL)
8446 break;
8447 continue;
8448 }
8449
8450 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 * If we're at the end of a block, skip to the start of
8452 * that block.
8453 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008454 l = ml_get_curline();
Bram Moolenaar50f42ca2011-07-15 14:12:30 +02008455 if (find_last_paren(l, '{', '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 && (trypos = find_start_brace(ind_maxcomment))
8457 != NULL) /* XXX */
8458 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008459 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 /* if not "else {" check for terminated again */
8461 /* but skip block for "} else {" */
8462 l = cin_skipcomment(ml_get_curline());
8463 if (*l == '}' || !cin_iselse(l))
8464 goto term_again;
8465 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008466 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 }
8468 }
8469 }
8470 }
8471 }
8472 }
8473
8474 /* add extra indent for a comment */
8475 if (cin_iscomment(theline))
8476 amount += ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008477
8478 /* subtract extra left-shift for jump labels */
8479 if (ind_jump_label > 0 && original_line_islabel)
8480 amount -= ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 }
8482
8483 /*
8484 * ok -- we're not inside any sort of structure at all!
8485 *
8486 * this means we're at the top level, and everything should
8487 * basically just match where the previous line is, except
8488 * for the lines immediately following a function declaration,
8489 * which are K&R-style parameters and need to be indented.
8490 */
8491 else
8492 {
8493 /*
8494 * if our line starts with an open brace, forget about any
8495 * prevailing indent and make sure it looks like the start
8496 * of a function
8497 */
8498
8499 if (theline[0] == '{')
8500 {
8501 amount = ind_first_open;
8502 }
8503
8504 /*
8505 * If the NEXT line is a function declaration, the current
8506 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008507 * Don't do this if the current line looks like a comment or if the
8508 * current line is terminated, ie. ends in ';', or if the current line
8509 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 */
8511 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8512 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008513 && vim_strchr(theline, '{') == NULL
8514 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 && !cin_ends_in(theline, (char_u *)":", NULL)
8516 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008517 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8518 cur_curpos.lnum + 1,
8519 ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 && !cin_isterminated(theline, FALSE, TRUE))
8521 {
8522 amount = ind_func_type;
8523 }
8524 else
8525 {
8526 amount = 0;
8527 curwin->w_cursor = cur_curpos;
8528
8529 /* search backwards until we find something we recognize */
8530
8531 while (curwin->w_cursor.lnum > 1)
8532 {
8533 curwin->w_cursor.lnum--;
8534 curwin->w_cursor.col = 0;
8535
8536 l = ml_get_curline();
8537
8538 /*
8539 * If we're in a comment now, skip to the start of the comment.
8540 */ /* XXX */
8541 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
8542 {
8543 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008544 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 continue;
8546 }
8547
8548 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008549 * Are we at the start of a cpp base class declaration or
8550 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008552 n = FALSE;
8553 if (ind_cpp_baseclass != 0 && theline[0] != '{')
8554 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008555 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008556 l = ml_get_curline();
8557 }
8558 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008560 /* XXX */
8561 amount = get_baseclass_amount(col, ind_maxparen,
8562 ind_maxcomment, ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 break;
8564 }
8565
8566 /*
8567 * Skip preprocessor directives and blank lines.
8568 */
8569 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8570 continue;
8571
8572 if (cin_nocode(l))
8573 continue;
8574
8575 /*
8576 * If the previous line ends in ',', use one level of
8577 * indentation:
8578 * int foo,
8579 * bar;
8580 * do this before checking for '}' in case of eg.
8581 * enum foobar
8582 * {
8583 * ...
8584 * } foo,
8585 * bar;
8586 */
8587 n = 0;
8588 if (cin_ends_in(l, (char_u *)",", NULL)
8589 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8590 {
8591 /* take us back to opening paren */
8592 if (find_last_paren(l, '(', ')')
8593 && (trypos = find_match_paren(ind_maxparen,
8594 ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008595 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596
8597 /* For a line ending in ',' that is a continuation line go
8598 * back to the first line with a backslash:
8599 * char *foo = "bla\
8600 * bla",
8601 * here;
8602 */
8603 while (n == 0 && curwin->w_cursor.lnum > 1)
8604 {
8605 l = ml_get(curwin->w_cursor.lnum - 1);
8606 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8607 break;
8608 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008609 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 }
8611
8612 amount = get_indent(); /* XXX */
8613
8614 if (amount == 0)
8615 amount = cin_first_id_amount();
8616 if (amount == 0)
8617 amount = ind_continuation;
8618 break;
8619 }
8620
8621 /*
8622 * If the line looks like a function declaration, and we're
8623 * not in a comment, put it the left margin.
8624 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008625 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0,
8626 ind_maxparen, ind_maxcomment)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 break;
8628 l = ml_get_curline();
8629
8630 /*
8631 * Finding the closing '}' of a previous function. Put
8632 * current line at the left margin. For when 'cino' has "fs".
8633 */
8634 if (*skipwhite(l) == '}')
8635 break;
8636
8637 /* (matching {)
8638 * If the previous line ends on '};' (maybe followed by
8639 * comments) align at column 0. For example:
8640 * char *string_array[] = { "foo",
8641 * / * x * / "b};ar" }; / * foobar * /
8642 */
8643 if (cin_ends_in(l, (char_u *)"};", NULL))
8644 break;
8645
8646 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008647 * Find a line only has a semicolon that belongs to a previous
8648 * line ending in '}', e.g. before an #endif. Don't increase
8649 * indent then.
8650 */
8651 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8652 {
8653 pos_T curpos_save = curwin->w_cursor;
8654
8655 while (curwin->w_cursor.lnum > 1)
8656 {
8657 look = ml_get(--curwin->w_cursor.lnum);
8658 if (!(cin_nocode(look) || cin_ispreproc_cont(
8659 &look, &curwin->w_cursor.lnum)))
8660 break;
8661 }
8662 if (curwin->w_cursor.lnum > 0
8663 && cin_ends_in(look, (char_u *)"}", NULL))
8664 break;
8665
8666 curwin->w_cursor = curpos_save;
8667 }
8668
8669 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 * If the PREVIOUS line is a function declaration, the current
8671 * line (and the ones that follow) needs to be indented as
8672 * parameters.
8673 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008674 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0,
8675 ind_maxparen, ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 {
8677 amount = ind_param;
8678 break;
8679 }
8680
8681 /*
8682 * If the previous line ends in ';' and the line before the
8683 * previous line ends in ',' or '\', ident to column zero:
8684 * int foo,
8685 * bar;
8686 * indent_to_0 here;
8687 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008688 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {
8690 l = ml_get(curwin->w_cursor.lnum - 1);
8691 if (cin_ends_in(l, (char_u *)",", NULL)
8692 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8693 break;
8694 l = ml_get_curline();
8695 }
8696
8697 /*
8698 * Doesn't look like anything interesting -- so just
8699 * use the indent of this line.
8700 *
8701 * Position the cursor over the rightmost paren, so that
8702 * matching it will take us back to the start of the line.
8703 */
8704 find_last_paren(l, '(', ')');
8705
8706 if ((trypos = find_match_paren(ind_maxparen,
8707 ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008708 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 amount = get_indent(); /* XXX */
8710 break;
8711 }
8712
8713 /* add extra indent for a comment */
8714 if (cin_iscomment(theline))
8715 amount += ind_comment;
8716
8717 /* add extra indent if the previous line ended in a backslash:
8718 * "asdfasdf\
8719 * here";
8720 * char *foo = "asdf\
8721 * here";
8722 */
8723 if (cur_curpos.lnum > 1)
8724 {
8725 l = ml_get(cur_curpos.lnum - 1);
8726 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8727 {
8728 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8729 if (cur_amount > 0)
8730 amount = cur_amount;
8731 else if (cur_amount == 0)
8732 amount += ind_continuation;
8733 }
8734 }
8735 }
8736 }
8737
8738theend:
8739 /* put the cursor back where it belongs */
8740 curwin->w_cursor = cur_curpos;
8741
8742 vim_free(linecopy);
8743
8744 if (amount < 0)
8745 return 0;
8746 return amount;
8747}
8748
8749 static int
8750find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment)
8751 int lookfor;
8752 linenr_T ourscope;
8753 int ind_maxparen;
8754 int ind_maxcomment;
8755{
8756 char_u *look;
8757 pos_T *theirscope;
8758 char_u *mightbeif;
8759 int elselevel;
8760 int whilelevel;
8761
8762 if (lookfor == LOOKFOR_IF)
8763 {
8764 elselevel = 1;
8765 whilelevel = 0;
8766 }
8767 else
8768 {
8769 elselevel = 0;
8770 whilelevel = 1;
8771 }
8772
8773 curwin->w_cursor.col = 0;
8774
8775 while (curwin->w_cursor.lnum > ourscope + 1)
8776 {
8777 curwin->w_cursor.lnum--;
8778 curwin->w_cursor.col = 0;
8779
8780 look = cin_skipcomment(ml_get_curline());
8781 if (cin_iselse(look)
8782 || cin_isif(look)
8783 || cin_isdo(look) /* XXX */
8784 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8785 {
8786 /*
8787 * if we've gone outside the braces entirely,
8788 * we must be out of scope...
8789 */
8790 theirscope = find_start_brace(ind_maxcomment); /* XXX */
8791 if (theirscope == NULL)
8792 break;
8793
8794 /*
8795 * and if the brace enclosing this is further
8796 * back than the one enclosing the else, we're
8797 * out of luck too.
8798 */
8799 if (theirscope->lnum < ourscope)
8800 break;
8801
8802 /*
8803 * and if they're enclosed in a *deeper* brace,
8804 * then we can ignore it because it's in a
8805 * different scope...
8806 */
8807 if (theirscope->lnum > ourscope)
8808 continue;
8809
8810 /*
8811 * if it was an "else" (that's not an "else if")
8812 * then we need to go back to another if, so
8813 * increment elselevel
8814 */
8815 look = cin_skipcomment(ml_get_curline());
8816 if (cin_iselse(look))
8817 {
8818 mightbeif = cin_skipcomment(look + 4);
8819 if (!cin_isif(mightbeif))
8820 ++elselevel;
8821 continue;
8822 }
8823
8824 /*
8825 * if it was a "while" then we need to go back to
8826 * another "do", so increment whilelevel. XXX
8827 */
8828 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8829 {
8830 ++whilelevel;
8831 continue;
8832 }
8833
8834 /* If it's an "if" decrement elselevel */
8835 look = cin_skipcomment(ml_get_curline());
8836 if (cin_isif(look))
8837 {
8838 elselevel--;
8839 /*
8840 * When looking for an "if" ignore "while"s that
8841 * get in the way.
8842 */
8843 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8844 whilelevel = 0;
8845 }
8846
8847 /* If it's a "do" decrement whilelevel */
8848 if (cin_isdo(look))
8849 whilelevel--;
8850
8851 /*
8852 * if we've used up all the elses, then
8853 * this must be the if that we want!
8854 * match the indent level of that if.
8855 */
8856 if (elselevel <= 0 && whilelevel <= 0)
8857 {
8858 return OK;
8859 }
8860 }
8861 }
8862 return FAIL;
8863}
8864
8865# if defined(FEAT_EVAL) || defined(PROTO)
8866/*
8867 * Get indent level from 'indentexpr'.
8868 */
8869 int
8870get_expr_indent()
8871{
8872 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008873 pos_T save_pos;
8874 colnr_T save_curswant;
8875 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008877 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8878 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008880 /* Save and restore cursor position and curswant, in case it was changed
8881 * via :normal commands */
8882 save_pos = curwin->w_cursor;
8883 save_curswant = curwin->w_curswant;
8884 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008886 if (use_sandbox)
8887 ++sandbox;
8888 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008890 if (use_sandbox)
8891 --sandbox;
8892 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
8894 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8895 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8896 * command. */
8897 save_State = State;
8898 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008899 curwin->w_cursor = save_pos;
8900 curwin->w_curswant = save_curswant;
8901 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 check_cursor();
8903 State = save_State;
8904
8905 /* If there is an error, just keep the current indent. */
8906 if (indent < 0)
8907 indent = get_indent();
8908
8909 return indent;
8910}
8911# endif
8912
8913#endif /* FEAT_CINDENT */
8914
8915#if defined(FEAT_LISP) || defined(PROTO)
8916
8917static int lisp_match __ARGS((char_u *p));
8918
8919 static int
8920lisp_match(p)
8921 char_u *p;
8922{
8923 char_u buf[LSIZE];
8924 int len;
8925 char_u *word = p_lispwords;
8926
8927 while (*word != NUL)
8928 {
8929 (void)copy_option_part(&word, buf, LSIZE, ",");
8930 len = (int)STRLEN(buf);
8931 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8932 return TRUE;
8933 }
8934 return FALSE;
8935}
8936
8937/*
8938 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8939 * The incompatible newer method is quite a bit better at indenting
8940 * code in lisp-like languages than the traditional one; it's still
8941 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8942 *
8943 * TODO:
8944 * Findmatch() should be adapted for lisp, also to make showmatch
8945 * work correctly: now (v5.3) it seems all C/C++ oriented:
8946 * - it does not recognize the #\( and #\) notations as character literals
8947 * - it doesn't know about comments starting with a semicolon
8948 * - it incorrectly interprets '(' as a character literal
8949 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008950 * Update from Sergey Khorev:
8951 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 */
8953 int
8954get_lisp_indent()
8955{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008956 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 int amount;
8958 char_u *that;
8959 colnr_T col;
8960 colnr_T firsttry;
8961 int parencount, quotecount;
8962 int vi_lisp;
8963
8964 /* Set vi_lisp to use the vi-compatible method */
8965 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8966
8967 realpos = curwin->w_cursor;
8968 curwin->w_cursor.col = 0;
8969
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008970 if ((pos = findmatch(NULL, '(')) == NULL)
8971 pos = findmatch(NULL, '[');
8972 else
8973 {
8974 paren = *pos;
8975 pos = findmatch(NULL, '[');
8976 if (pos == NULL || ltp(pos, &paren))
8977 pos = &paren;
8978 }
8979 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 {
8981 /* Extra trick: Take the indent of the first previous non-white
8982 * line that is at the same () level. */
8983 amount = -1;
8984 parencount = 0;
8985
8986 while (--curwin->w_cursor.lnum >= pos->lnum)
8987 {
8988 if (linewhite(curwin->w_cursor.lnum))
8989 continue;
8990 for (that = ml_get_curline(); *that != NUL; ++that)
8991 {
8992 if (*that == ';')
8993 {
8994 while (*(that + 1) != NUL)
8995 ++that;
8996 continue;
8997 }
8998 if (*that == '\\')
8999 {
9000 if (*(that + 1) != NUL)
9001 ++that;
9002 continue;
9003 }
9004 if (*that == '"' && *(that + 1) != NUL)
9005 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009006 while (*++that && *that != '"')
9007 {
9008 /* skipping escaped characters in the string */
9009 if (*that == '\\')
9010 {
9011 if (*++that == NUL)
9012 break;
9013 if (that[1] == NUL)
9014 {
9015 ++that;
9016 break;
9017 }
9018 }
9019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009021 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009023 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 --parencount;
9025 }
9026 if (parencount == 0)
9027 {
9028 amount = get_indent();
9029 break;
9030 }
9031 }
9032
9033 if (amount == -1)
9034 {
9035 curwin->w_cursor.lnum = pos->lnum;
9036 curwin->w_cursor.col = pos->col;
9037 col = pos->col;
9038
9039 that = ml_get_curline();
9040
9041 if (vi_lisp && get_indent() == 0)
9042 amount = 2;
9043 else
9044 {
9045 amount = 0;
9046 while (*that && col)
9047 {
9048 amount += lbr_chartabsize_adv(&that, (colnr_T)amount);
9049 col--;
9050 }
9051
9052 /*
9053 * Some keywords require "body" indenting rules (the
9054 * non-standard-lisp ones are Scheme special forms):
9055 *
9056 * (let ((a 1)) instead (let ((a 1))
9057 * (...)) of (...))
9058 */
9059
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009060 if (!vi_lisp && (*that == '(' || *that == '[')
9061 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 amount += 2;
9063 else
9064 {
9065 that++;
9066 amount++;
9067 firsttry = amount;
9068
9069 while (vim_iswhite(*that))
9070 {
9071 amount += lbr_chartabsize(that, (colnr_T)amount);
9072 ++that;
9073 }
9074
9075 if (*that && *that != ';') /* not a comment line */
9076 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009077 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009079 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 firsttry++;
9081
9082 parencount = 0;
9083 quotecount = 0;
9084
9085 if (vi_lisp
9086 || (*that != '"'
9087 && *that != '\''
9088 && *that != '#'
9089 && (*that < '0' || *that > '9')))
9090 {
9091 while (*that
9092 && (!vim_iswhite(*that)
9093 || quotecount
9094 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009095 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 && !quotecount
9097 && !parencount
9098 && vi_lisp)))
9099 {
9100 if (*that == '"')
9101 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009102 if ((*that == '(' || *that == '[')
9103 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009105 if ((*that == ')' || *that == ']')
9106 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 --parencount;
9108 if (*that == '\\' && *(that+1) != NUL)
9109 amount += lbr_chartabsize_adv(&that,
9110 (colnr_T)amount);
9111 amount += lbr_chartabsize_adv(&that,
9112 (colnr_T)amount);
9113 }
9114 }
9115 while (vim_iswhite(*that))
9116 {
9117 amount += lbr_chartabsize(that, (colnr_T)amount);
9118 that++;
9119 }
9120 if (!*that || *that == ';')
9121 amount = firsttry;
9122 }
9123 }
9124 }
9125 }
9126 }
9127 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009128 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129
9130 curwin->w_cursor = realpos;
9131
9132 return amount;
9133}
9134#endif /* FEAT_LISP */
9135
9136 void
9137prepare_to_exit()
9138{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009139#if defined(SIGHUP) && defined(SIG_IGN)
9140 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9141 * makes Vim exit and then handling SIGHUP causes various reentrance
9142 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009143 signal(SIGHUP, SIG_IGN);
9144#endif
9145
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146#ifdef FEAT_GUI
9147 if (gui.in_use)
9148 {
9149 gui.dying = TRUE;
9150 out_trash(); /* trash any pending output */
9151 }
9152 else
9153#endif
9154 {
9155 windgoto((int)Rows - 1, 0);
9156
9157 /*
9158 * Switch terminal mode back now, so messages end up on the "normal"
9159 * screen (if there are two screens).
9160 */
9161 settmode(TMODE_COOK);
9162#ifdef WIN3264
9163 if (can_end_termcap_mode(FALSE) == TRUE)
9164#endif
9165 stoptermcap();
9166 out_flush();
9167 }
9168}
9169
9170/*
9171 * Preserve files and exit.
9172 * When called IObuff must contain a message.
9173 */
9174 void
9175preserve_exit()
9176{
9177 buf_T *buf;
9178
9179 prepare_to_exit();
9180
Bram Moolenaar4770d092006-01-12 23:22:24 +00009181 /* Setting this will prevent free() calls. That avoids calling free()
9182 * recursively when free() was invoked with a bad pointer. */
9183 really_exiting = TRUE;
9184
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 out_str(IObuff);
9186 screen_start(); /* don't know where cursor is now */
9187 out_flush();
9188
9189 ml_close_notmod(); /* close all not-modified buffers */
9190
9191 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9192 {
9193 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9194 {
9195 OUT_STR(_("Vim: preserving files...\n"));
9196 screen_start(); /* don't know where cursor is now */
9197 out_flush();
9198 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9199 break;
9200 }
9201 }
9202
9203 ml_close_all(FALSE); /* close all memfiles, without deleting */
9204
9205 OUT_STR(_("Vim: Finished.\n"));
9206
9207 getout(1);
9208}
9209
9210/*
9211 * return TRUE if "fname" exists.
9212 */
9213 int
9214vim_fexists(fname)
9215 char_u *fname;
9216{
9217 struct stat st;
9218
9219 if (mch_stat((char *)fname, &st))
9220 return FALSE;
9221 return TRUE;
9222}
9223
9224/*
9225 * Check for CTRL-C pressed, but only once in a while.
9226 * Should be used instead of ui_breakcheck() for functions that check for
9227 * each line in the file. Calling ui_breakcheck() each time takes too much
9228 * time, because it can be a system call.
9229 */
9230
9231#ifndef BREAKCHECK_SKIP
9232# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9233# define BREAKCHECK_SKIP 200
9234# else
9235# define BREAKCHECK_SKIP 32
9236# endif
9237#endif
9238
9239static int breakcheck_count = 0;
9240
9241 void
9242line_breakcheck()
9243{
9244 if (++breakcheck_count >= BREAKCHECK_SKIP)
9245 {
9246 breakcheck_count = 0;
9247 ui_breakcheck();
9248 }
9249}
9250
9251/*
9252 * Like line_breakcheck() but check 10 times less often.
9253 */
9254 void
9255fast_breakcheck()
9256{
9257 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9258 {
9259 breakcheck_count = 0;
9260 ui_breakcheck();
9261 }
9262}
9263
9264/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009265 * Invoke expand_wildcards() for one pattern.
9266 * Expand items like "%:h" before the expansion.
9267 * Returns OK or FAIL.
9268 */
9269 int
9270expand_wildcards_eval(pat, num_file, file, flags)
9271 char_u **pat; /* pointer to input pattern */
9272 int *num_file; /* resulting number of files */
9273 char_u ***file; /* array of resulting files */
9274 int flags; /* EW_DIR, etc. */
9275{
9276 int ret = FAIL;
9277 char_u *eval_pat = NULL;
9278 char_u *exp_pat = *pat;
9279 char_u *ignored_msg;
9280 int usedlen;
9281
9282 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9283 {
9284 ++emsg_off;
9285 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9286 NULL, &ignored_msg, NULL);
9287 --emsg_off;
9288 if (eval_pat != NULL)
9289 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9290 }
9291
9292 if (exp_pat != NULL)
9293 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9294
9295 if (eval_pat != NULL)
9296 {
9297 vim_free(exp_pat);
9298 vim_free(eval_pat);
9299 }
9300
9301 return ret;
9302}
9303
9304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9306 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009307 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 */
9309 int
9310expand_wildcards(num_pat, pat, num_file, file, flags)
9311 int num_pat; /* number of input patterns */
9312 char_u **pat; /* array of input patterns */
9313 int *num_file; /* resulting number of files */
9314 char_u ***file; /* array of resulting files */
9315 int flags; /* EW_DIR, etc. */
9316{
9317 int retval;
9318 int i, j;
9319 char_u *p;
9320 int non_suf_match; /* number without matching suffix */
9321
9322 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9323
9324 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009325 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 return retval;
9327
9328#ifdef FEAT_WILDIGN
9329 /*
9330 * Remove names that match 'wildignore'.
9331 */
9332 if (*p_wig)
9333 {
9334 char_u *ffname;
9335
9336 /* check all files in (*file)[] */
9337 for (i = 0; i < *num_file; ++i)
9338 {
9339 ffname = FullName_save((*file)[i], FALSE);
9340 if (ffname == NULL) /* out of memory */
9341 break;
9342# ifdef VMS
9343 vms_remove_version(ffname);
9344# endif
9345 if (match_file_list(p_wig, (*file)[i], ffname))
9346 {
9347 /* remove this matching file from the list */
9348 vim_free((*file)[i]);
9349 for (j = i; j + 1 < *num_file; ++j)
9350 (*file)[j] = (*file)[j + 1];
9351 --*num_file;
9352 --i;
9353 }
9354 vim_free(ffname);
9355 }
9356 }
9357#endif
9358
9359 /*
9360 * Move the names where 'suffixes' match to the end.
9361 */
9362 if (*num_file > 1)
9363 {
9364 non_suf_match = 0;
9365 for (i = 0; i < *num_file; ++i)
9366 {
9367 if (!match_suffix((*file)[i]))
9368 {
9369 /*
9370 * Move the name without matching suffix to the front
9371 * of the list.
9372 */
9373 p = (*file)[i];
9374 for (j = i; j > non_suf_match; --j)
9375 (*file)[j] = (*file)[j - 1];
9376 (*file)[non_suf_match++] = p;
9377 }
9378 }
9379 }
9380
9381 return retval;
9382}
9383
9384/*
9385 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9386 */
9387 int
9388match_suffix(fname)
9389 char_u *fname;
9390{
9391 int fnamelen, setsuflen;
9392 char_u *setsuf;
9393#define MAXSUFLEN 30 /* maximum length of a file suffix */
9394 char_u suf_buf[MAXSUFLEN];
9395
9396 fnamelen = (int)STRLEN(fname);
9397 setsuflen = 0;
9398 for (setsuf = p_su; *setsuf; )
9399 {
9400 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009401 if (setsuflen == 0)
9402 {
9403 char_u *tail = gettail(fname);
9404
9405 /* empty entry: match name without a '.' */
9406 if (vim_strchr(tail, '.') == NULL)
9407 {
9408 setsuflen = 1;
9409 break;
9410 }
9411 }
9412 else
9413 {
9414 if (fnamelen >= setsuflen
9415 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9416 (size_t)setsuflen) == 0)
9417 break;
9418 setsuflen = 0;
9419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 }
9421 return (setsuflen != 0);
9422}
9423
9424#if !defined(NO_EXPANDPATH) || defined(PROTO)
9425
9426# ifdef VIM_BACKTICK
9427static int vim_backtick __ARGS((char_u *p));
9428static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9429# endif
9430
9431# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9432/*
9433 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9434 * it's shared between these systems.
9435 */
9436# if defined(DJGPP) || defined(PROTO)
9437# define _cdecl /* DJGPP doesn't have this */
9438# else
9439# ifdef __BORLANDC__
9440# define _cdecl _RTLENTRYF
9441# endif
9442# endif
9443
9444/*
9445 * comparison function for qsort in dos_expandpath()
9446 */
9447 static int _cdecl
9448pstrcmp(const void *a, const void *b)
9449{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009450 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451}
9452
9453# ifndef WIN3264
9454 static void
9455namelowcpy(
9456 char_u *d,
9457 char_u *s)
9458{
9459# ifdef DJGPP
9460 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9461 while (*s)
9462 *d++ = *s++;
9463 else
9464# endif
9465 while (*s)
9466 *d++ = TOLOWER_LOC(*s++);
9467 *d = NUL;
9468}
9469# endif
9470
9471/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009472 * Recursively expand one path component into all matching files and/or
9473 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 * Return the number of matches found.
9475 * "path" has backslashes before chars that are not to be expanded, starting
9476 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009477 * Return the number of matches found.
9478 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 */
9480 static int
9481dos_expandpath(
9482 garray_T *gap,
9483 char_u *path,
9484 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009485 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009486 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009488 char_u *buf;
9489 char_u *path_end;
9490 char_u *p, *s, *e;
9491 int start_len = gap->ga_len;
9492 char_u *pat;
9493 regmatch_T regmatch;
9494 int starts_with_dot;
9495 int matches;
9496 int len;
9497 int starstar = FALSE;
9498 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499#ifdef WIN3264
9500 WIN32_FIND_DATA fb;
9501 HANDLE hFind = (HANDLE)0;
9502# ifdef FEAT_MBYTE
9503 WIN32_FIND_DATAW wfb;
9504 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9505# endif
9506#else
9507 struct ffblk fb;
9508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009510 int ok;
9511
9512 /* Expanding "**" may take a long time, check for CTRL-C. */
9513 if (stardepth > 0)
9514 {
9515 ui_breakcheck();
9516 if (got_int)
9517 return 0;
9518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
9520 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009521 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 if (buf == NULL)
9523 return 0;
9524
9525 /*
9526 * Find the first part in the path name that contains a wildcard or a ~1.
9527 * Copy it into buf, including the preceding characters.
9528 */
9529 p = buf;
9530 s = buf;
9531 e = NULL;
9532 path_end = path;
9533 while (*path_end != NUL)
9534 {
9535 /* May ignore a wildcard that has a backslash before it; it will
9536 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9537 if (path_end >= path + wildoff && rem_backslash(path_end))
9538 *p++ = *path_end++;
9539 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9540 {
9541 if (e != NULL)
9542 break;
9543 s = p + 1;
9544 }
9545 else if (path_end >= path + wildoff
9546 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9547 e = p;
9548#ifdef FEAT_MBYTE
9549 if (has_mbyte)
9550 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009551 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 STRNCPY(p, path_end, len);
9553 p += len;
9554 path_end += len;
9555 }
9556 else
9557#endif
9558 *p++ = *path_end++;
9559 }
9560 e = p;
9561 *e = NUL;
9562
9563 /* now we have one wildcard component between s and e */
9564 /* Remove backslashes between "wildoff" and the start of the wildcard
9565 * component. */
9566 for (p = buf + wildoff; p < s; ++p)
9567 if (rem_backslash(p))
9568 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009569 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 --e;
9571 --s;
9572 }
9573
Bram Moolenaar231334e2005-07-25 20:46:57 +00009574 /* Check for "**" between "s" and "e". */
9575 for (p = s; p < e; ++p)
9576 if (p[0] == '*' && p[1] == '*')
9577 starstar = TRUE;
9578
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 starts_with_dot = (*s == '.');
9580 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9581 if (pat == NULL)
9582 {
9583 vim_free(buf);
9584 return 0;
9585 }
9586
9587 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009588 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009589 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 regmatch.rm_ic = TRUE; /* Always ignore case */
9591 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009592 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009593 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 vim_free(pat);
9595
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009596 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 {
9598 vim_free(buf);
9599 return 0;
9600 }
9601
9602 /* remember the pattern or file name being looked for */
9603 matchname = vim_strsave(s);
9604
Bram Moolenaar231334e2005-07-25 20:46:57 +00009605 /* If "**" is by itself, this is the first time we encounter it and more
9606 * is following then find matches without any directory. */
9607 if (!didstar && stardepth < 100 && starstar && e - s == 2
9608 && *path_end == '/')
9609 {
9610 STRCPY(s, path_end + 1);
9611 ++stardepth;
9612 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9613 --stardepth;
9614 }
9615
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 /* Scan all files in the directory with "dir/ *.*" */
9617 STRCPY(s, "*.*");
9618#ifdef WIN3264
9619# ifdef FEAT_MBYTE
9620 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9621 {
9622 /* The active codepage differs from 'encoding'. Attempt using the
9623 * wide function. If it fails because it is not implemented fall back
9624 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009625 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 if (wn != NULL)
9627 {
9628 hFind = FindFirstFileW(wn, &wfb);
9629 if (hFind == INVALID_HANDLE_VALUE
9630 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9631 {
9632 vim_free(wn);
9633 wn = NULL;
9634 }
9635 }
9636 }
9637
9638 if (wn == NULL)
9639# endif
9640 hFind = FindFirstFile(buf, &fb);
9641 ok = (hFind != INVALID_HANDLE_VALUE);
9642#else
9643 /* If we are expanding wildcards we try both files and directories */
9644 ok = (findfirst((char *)buf, &fb,
9645 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9646#endif
9647
9648 while (ok)
9649 {
9650#ifdef WIN3264
9651# ifdef FEAT_MBYTE
9652 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009653 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 else
9655# endif
9656 p = (char_u *)fb.cFileName;
9657#else
9658 p = (char_u *)fb.ff_name;
9659#endif
9660 /* Ignore entries starting with a dot, unless when asked for. Accept
9661 * all entries found with "matchname". */
9662 if ((p[0] != '.' || starts_with_dot)
9663 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009664 || (regmatch.regprog != NULL
9665 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009666 || ((flags & EW_NOTWILD)
9667 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 {
9669#ifdef WIN3264
9670 STRCPY(s, p);
9671#else
9672 namelowcpy(s, p);
9673#endif
9674 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009675
9676 if (starstar && stardepth < 100)
9677 {
9678 /* For "**" in the pattern first go deeper in the tree to
9679 * find matches. */
9680 STRCPY(buf + len, "/**");
9681 STRCPY(buf + len + 3, path_end);
9682 ++stardepth;
9683 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9684 --stardepth;
9685 }
9686
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 STRCPY(buf + len, path_end);
9688 if (mch_has_exp_wildcard(path_end))
9689 {
9690 /* need to expand another component of the path */
9691 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009692 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 }
9694 else
9695 {
9696 /* no more wildcards, check if there is a match */
9697 /* remove backslashes for the remaining components only */
9698 if (*path_end != 0)
9699 backslash_halve(buf + len + 1);
9700 if (mch_getperm(buf) >= 0) /* add existing file */
9701 addfile(gap, buf, flags);
9702 }
9703 }
9704
9705#ifdef WIN3264
9706# ifdef FEAT_MBYTE
9707 if (wn != NULL)
9708 {
9709 vim_free(p);
9710 ok = FindNextFileW(hFind, &wfb);
9711 }
9712 else
9713# endif
9714 ok = FindNextFile(hFind, &fb);
9715#else
9716 ok = (findnext(&fb) == 0);
9717#endif
9718
9719 /* If no more matches and no match was used, try expanding the name
9720 * itself. Finds the long name of a short filename. */
9721 if (!ok && matchname != NULL && gap->ga_len == start_len)
9722 {
9723 STRCPY(s, matchname);
9724#ifdef WIN3264
9725 FindClose(hFind);
9726# ifdef FEAT_MBYTE
9727 if (wn != NULL)
9728 {
9729 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009730 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 if (wn != NULL)
9732 hFind = FindFirstFileW(wn, &wfb);
9733 }
9734 if (wn == NULL)
9735# endif
9736 hFind = FindFirstFile(buf, &fb);
9737 ok = (hFind != INVALID_HANDLE_VALUE);
9738#else
9739 ok = (findfirst((char *)buf, &fb,
9740 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9741#endif
9742 vim_free(matchname);
9743 matchname = NULL;
9744 }
9745 }
9746
9747#ifdef WIN3264
9748 FindClose(hFind);
9749# ifdef FEAT_MBYTE
9750 vim_free(wn);
9751# endif
9752#endif
9753 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009754 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 vim_free(matchname);
9756
9757 matches = gap->ga_len - start_len;
9758 if (matches > 0)
9759 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9760 sizeof(char_u *), pstrcmp);
9761 return matches;
9762}
9763
9764 int
9765mch_expandpath(
9766 garray_T *gap,
9767 char_u *path,
9768 int flags) /* EW_* flags */
9769{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009770 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9773
Bram Moolenaar231334e2005-07-25 20:46:57 +00009774#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9775 || defined(PROTO)
9776/*
9777 * Unix style wildcard expansion code.
9778 * It's here because it's used both for Unix and Mac.
9779 */
9780static int pstrcmp __ARGS((const void *, const void *));
9781
9782 static int
9783pstrcmp(a, b)
9784 const void *a, *b;
9785{
9786 return (pathcmp(*(char **)a, *(char **)b, -1));
9787}
9788
9789/*
9790 * Recursively expand one path component into all matching files and/or
9791 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9792 * "path" has backslashes before chars that are not to be expanded, starting
9793 * at "path + wildoff".
9794 * Return the number of matches found.
9795 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9796 */
9797 int
9798unix_expandpath(gap, path, wildoff, flags, didstar)
9799 garray_T *gap;
9800 char_u *path;
9801 int wildoff;
9802 int flags; /* EW_* flags */
9803 int didstar; /* expanded "**" once already */
9804{
9805 char_u *buf;
9806 char_u *path_end;
9807 char_u *p, *s, *e;
9808 int start_len = gap->ga_len;
9809 char_u *pat;
9810 regmatch_T regmatch;
9811 int starts_with_dot;
9812 int matches;
9813 int len;
9814 int starstar = FALSE;
9815 static int stardepth = 0; /* depth for "**" expansion */
9816
9817 DIR *dirp;
9818 struct dirent *dp;
9819
9820 /* Expanding "**" may take a long time, check for CTRL-C. */
9821 if (stardepth > 0)
9822 {
9823 ui_breakcheck();
9824 if (got_int)
9825 return 0;
9826 }
9827
9828 /* make room for file name */
9829 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9830 if (buf == NULL)
9831 return 0;
9832
9833 /*
9834 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009835 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009836 * Copy it into "buf", including the preceding characters.
9837 */
9838 p = buf;
9839 s = buf;
9840 e = NULL;
9841 path_end = path;
9842 while (*path_end != NUL)
9843 {
9844 /* May ignore a wildcard that has a backslash before it; it will
9845 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9846 if (path_end >= path + wildoff && rem_backslash(path_end))
9847 *p++ = *path_end++;
9848 else if (*path_end == '/')
9849 {
9850 if (e != NULL)
9851 break;
9852 s = p + 1;
9853 }
9854 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009855 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009856 || (!p_fic && (flags & EW_ICASE)
9857 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009858 e = p;
9859#ifdef FEAT_MBYTE
9860 if (has_mbyte)
9861 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009862 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009863 STRNCPY(p, path_end, len);
9864 p += len;
9865 path_end += len;
9866 }
9867 else
9868#endif
9869 *p++ = *path_end++;
9870 }
9871 e = p;
9872 *e = NUL;
9873
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009874 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009875 /* Remove backslashes between "wildoff" and the start of the wildcard
9876 * component. */
9877 for (p = buf + wildoff; p < s; ++p)
9878 if (rem_backslash(p))
9879 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009880 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009881 --e;
9882 --s;
9883 }
9884
9885 /* Check for "**" between "s" and "e". */
9886 for (p = s; p < e; ++p)
9887 if (p[0] == '*' && p[1] == '*')
9888 starstar = TRUE;
9889
9890 /* convert the file pattern to a regexp pattern */
9891 starts_with_dot = (*s == '.');
9892 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9893 if (pat == NULL)
9894 {
9895 vim_free(buf);
9896 return 0;
9897 }
9898
9899 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009900 if (flags & EW_ICASE)
9901 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9902 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009903 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009904 if (flags & (EW_NOERROR | EW_NOTWILD))
9905 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009906 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009907 if (flags & (EW_NOERROR | EW_NOTWILD))
9908 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009909 vim_free(pat);
9910
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009911 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009912 {
9913 vim_free(buf);
9914 return 0;
9915 }
9916
9917 /* If "**" is by itself, this is the first time we encounter it and more
9918 * is following then find matches without any directory. */
9919 if (!didstar && stardepth < 100 && starstar && e - s == 2
9920 && *path_end == '/')
9921 {
9922 STRCPY(s, path_end + 1);
9923 ++stardepth;
9924 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9925 --stardepth;
9926 }
9927
9928 /* open the directory for scanning */
9929 *s = NUL;
9930 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9931
9932 /* Find all matching entries */
9933 if (dirp != NULL)
9934 {
9935 for (;;)
9936 {
9937 dp = readdir(dirp);
9938 if (dp == NULL)
9939 break;
9940 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009941 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9942 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009943 || ((flags & EW_NOTWILD)
9944 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009945 {
9946 STRCPY(s, dp->d_name);
9947 len = STRLEN(buf);
9948
9949 if (starstar && stardepth < 100)
9950 {
9951 /* For "**" in the pattern first go deeper in the tree to
9952 * find matches. */
9953 STRCPY(buf + len, "/**");
9954 STRCPY(buf + len + 3, path_end);
9955 ++stardepth;
9956 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9957 --stardepth;
9958 }
9959
9960 STRCPY(buf + len, path_end);
9961 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9962 {
9963 /* need to expand another component of the path */
9964 /* remove backslashes for the remaining components only */
9965 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9966 }
9967 else
9968 {
9969 /* no more wildcards, check if there is a match */
9970 /* remove backslashes for the remaining components only */
9971 if (*path_end != NUL)
9972 backslash_halve(buf + len + 1);
9973 if (mch_getperm(buf) >= 0) /* add existing file */
9974 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009975#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00009976 size_t precomp_len = STRLEN(buf)+1;
9977 char_u *precomp_buf =
9978 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009979
Bram Moolenaar231334e2005-07-25 20:46:57 +00009980 if (precomp_buf)
9981 {
9982 mch_memmove(buf, precomp_buf, precomp_len);
9983 vim_free(precomp_buf);
9984 }
9985#endif
9986 addfile(gap, buf, flags);
9987 }
9988 }
9989 }
9990 }
9991
9992 closedir(dirp);
9993 }
9994
9995 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009996 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009997
9998 matches = gap->ga_len - start_len;
9999 if (matches > 0)
10000 qsort(((char_u **)gap->ga_data) + start_len, matches,
10001 sizeof(char_u *), pstrcmp);
10002 return matches;
10003}
10004#endif
10005
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010006#if defined(FEAT_SEARCHPATH)
10007static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10008static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010009static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10010static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010011static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10012static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10013
10014/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010015 * Moves "*psep" back to the previous path separator in "path".
10016 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010017 */
10018 static int
10019find_previous_pathsep(path, psep)
10020 char_u *path;
10021 char_u **psep;
10022{
10023 /* skip the current separator */
10024 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010025 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010026
10027 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010028 while (*psep > path)
10029 {
10030 if (vim_ispathsep(**psep))
10031 return OK;
10032 mb_ptr_back(path, *psep);
10033 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010034
10035 return FAIL;
10036}
10037
10038/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010039 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10040 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010041 */
10042 static int
10043is_unique(maybe_unique, gap, i)
10044 char_u *maybe_unique;
10045 garray_T *gap;
10046 int i;
10047{
10048 int j;
10049 int candidate_len;
10050 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010051 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010052 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010053
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010054 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010055 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010056 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010057 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010058
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010059 candidate_len = (int)STRLEN(maybe_unique);
10060 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010061 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010062 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010063
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010064 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010065 if (fnamecmp(maybe_unique, rival) == 0
10066 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010067 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010068 }
10069
Bram Moolenaar162bd912010-07-28 22:29:10 +020010070 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010071}
10072
10073/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010074 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010075 * paths are expanded to their equivalent fullpath. This includes the "."
10076 * (relative to current buffer directory) and empty path (relative to current
10077 * directory) notations.
10078 *
10079 * TODO: handle upward search (;) and path limiter (**N) notations by
10080 * expanding each into their equivalent path(s).
10081 */
10082 static void
10083expand_path_option(curdir, gap)
10084 char_u *curdir;
10085 garray_T *gap;
10086{
10087 char_u *path_option = *curbuf->b_p_path == NUL
10088 ? p_path : curbuf->b_p_path;
10089 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010090 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010091 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010092
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010093 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010094 return;
10095
10096 while (*path_option != NUL)
10097 {
10098 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10099
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010100 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010101 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010102 /* Relative to current buffer:
10103 * "/path/file" + "." -> "/path/"
10104 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010105 if (curbuf->b_ffname == NULL)
10106 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010107 p = gettail(curbuf->b_ffname);
10108 len = (int)(p - curbuf->b_ffname);
10109 if (len + (int)STRLEN(buf) >= MAXPATHL)
10110 continue;
10111 if (buf[1] == NUL)
10112 buf[len] = NUL;
10113 else
10114 STRMOVE(buf + len, buf + 2);
10115 mch_memmove(buf, curbuf->b_ffname, len);
10116 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010117 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010118 else if (buf[0] == NUL)
10119 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010120 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010121 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010122 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010123 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010124 else if (!mch_isFullName(buf))
10125 {
10126 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010127 len = (int)STRLEN(curdir);
10128 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010129 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010130 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010131 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010132 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010133 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010134 }
10135
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010136 if (ga_grow(gap, 1) == FAIL)
10137 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010138
10139# if defined(MSWIN) || defined(MSDOS)
10140 /* Avoid the path ending in a backslash, it fails when a comma is
10141 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010142 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010143 if (buf[len - 1] == '\\')
10144 buf[len - 1] = '/';
10145# endif
10146
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010147 p = vim_strsave(buf);
10148 if (p == NULL)
10149 break;
10150 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010151 }
10152
10153 vim_free(buf);
10154}
10155
10156/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010157 * Returns a pointer to the file or directory name in "fname" that matches the
10158 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010159 *
10160 * path: /foo/bar/baz
10161 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010162 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010163 */
10164 static char_u *
10165get_path_cutoff(fname, gap)
10166 char_u *fname;
10167 garray_T *gap;
10168{
10169 int i;
10170 int maxlen = 0;
10171 char_u **path_part = (char_u **)gap->ga_data;
10172 char_u *cutoff = NULL;
10173
10174 for (i = 0; i < gap->ga_len; i++)
10175 {
10176 int j = 0;
10177
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010178 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010179# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010180 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10181#endif
10182 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010183 j++;
10184 if (j > maxlen)
10185 {
10186 maxlen = j;
10187 cutoff = &fname[j];
10188 }
10189 }
10190
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010191 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010192 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010193 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010194 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010195
10196 return cutoff;
10197}
10198
10199/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010200 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10201 * that they are unique with respect to each other while conserving the part
10202 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010203 */
10204 static void
10205uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010206 garray_T *gap;
10207 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010208{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010209 int i;
10210 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010211 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010212 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010213 char_u *pat;
10214 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010215 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010216 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010217 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010218 char_u **in_curdir = NULL;
10219 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010220
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010221 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010222 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010223
10224 /*
10225 * We need to prepend a '*' at the beginning of file_pattern so that the
10226 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010227 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010228 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010229 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010230 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010231 if (file_pattern == NULL)
10232 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010233 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010234 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010235 STRCAT(file_pattern, pattern);
10236 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10237 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010238 if (pat == NULL)
10239 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010240
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010241 regmatch.rm_ic = TRUE; /* always ignore case */
10242 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10243 vim_free(pat);
10244 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010245 return;
10246
Bram Moolenaar162bd912010-07-28 22:29:10 +020010247 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010248 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010249 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010250 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010251
10252 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010253 if (in_curdir == NULL)
10254 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010255
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010256 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010257 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010258 char_u *path = fnames[i];
10259 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010260 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010261 char_u *pathsep_p;
10262 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010263
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010264 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010265 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010266 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010267 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010268 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010269
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010270 /* Shorten the filename while maintaining its uniqueness */
10271 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010272
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010273 /* we start at the end of the path */
10274 pathsep_p = path + len - 1;
10275
10276 while (find_previous_pathsep(path, &pathsep_p))
10277 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10278 && is_unique(pathsep_p + 1, gap, i)
10279 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10280 {
10281 sort_again = TRUE;
10282 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10283 break;
10284 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010285
10286 if (mch_isFullName(path))
10287 {
10288 /*
10289 * Last resort: shorten relative to curdir if possible.
10290 * 'possible' means:
10291 * 1. It is under the current directory.
10292 * 2. The result is actually shorter than the original.
10293 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010294 * Before curdir After
10295 * /foo/bar/file.txt /foo/bar ./file.txt
10296 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10297 * /file.txt / /file.txt
10298 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010299 */
10300 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010301 if (short_name != NULL && short_name > path + 1
10302#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010303 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010304 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010305 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010306 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010307 && !vim_ispathsep(*short_name)
10308#endif
10309 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010310 {
10311 STRCPY(path, ".");
10312 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010313 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010314 }
10315 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010316 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010317 }
10318
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010319 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010320 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010321 {
10322 char_u *rel_path;
10323 char_u *path = in_curdir[i];
10324
10325 if (path == NULL)
10326 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010327
10328 /* If the {filename} is not unique, change it to ./{filename}.
10329 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010330 short_name = shorten_fname(path, curdir);
10331 if (short_name == NULL)
10332 short_name = path;
10333 if (is_unique(short_name, gap, i))
10334 {
10335 STRCPY(fnames[i], short_name);
10336 continue;
10337 }
10338
10339 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10340 if (rel_path == NULL)
10341 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010342 STRCPY(rel_path, ".");
10343 add_pathsep(rel_path);
10344 STRCAT(rel_path, short_name);
10345
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010346 vim_free(fnames[i]);
10347 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010348 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010349 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010350 }
10351
Bram Moolenaar162bd912010-07-28 22:29:10 +020010352theend:
10353 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010354 if (in_curdir != NULL)
10355 {
10356 for (i = 0; i < gap->ga_len; i++)
10357 vim_free(in_curdir[i]);
10358 vim_free(in_curdir);
10359 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010360 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010361 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010362
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010363 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010364 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010365}
10366
10367/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010368 * Calls globpath() with 'path' values for the given pattern and stores the
10369 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010370 * Returns the total number of matches.
10371 */
10372 static int
10373expand_in_path(gap, pattern, flags)
10374 garray_T *gap;
10375 char_u *pattern;
10376 int flags; /* EW_* flags */
10377{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010378 char_u *curdir;
10379 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010380 char_u *files = NULL;
10381 char_u *s; /* start */
10382 char_u *e; /* end */
10383 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010384
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010385 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010386 return 0;
10387 mch_dirname(curdir, MAXPATHL);
10388
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010389 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010390 expand_path_option(curdir, &path_ga);
10391 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010392 if (path_ga.ga_len == 0)
10393 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010394
10395 paths = ga_concat_strings(&path_ga);
10396 ga_clear_strings(&path_ga);
10397 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010398 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010399
Bram Moolenaar94950a92010-12-02 16:01:29 +010010400 files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010401 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010402 if (files == NULL)
10403 return 0;
10404
10405 /* Copy each path in files into gap */
10406 s = e = files;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010407 while (*s != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010408 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010409 while (*e != '\n' && *e != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010410 e++;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010411 if (*e == NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010412 {
10413 addfile(gap, s, flags);
10414 break;
10415 }
10416 else
10417 {
10418 /* *e is '\n' */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010419 *e = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010420 addfile(gap, s, flags);
10421 e++;
10422 s = e;
10423 }
10424 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010425 vim_free(files);
10426
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010427 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010428}
10429#endif
10430
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010431#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10432/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010433 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10434 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010435 */
10436 void
10437remove_duplicates(gap)
10438 garray_T *gap;
10439{
10440 int i;
10441 int j;
10442 char_u **fnames = (char_u **)gap->ga_data;
10443
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010444 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010445 for (i = gap->ga_len - 1; i > 0; --i)
10446 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10447 {
10448 vim_free(fnames[i]);
10449 for (j = i + 1; j < gap->ga_len; ++j)
10450 fnames[j - 1] = fnames[j];
10451 --gap->ga_len;
10452 }
10453}
10454#endif
10455
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456/*
10457 * Generic wildcard expansion code.
10458 *
10459 * Characters in "pat" that should not be expanded must be preceded with a
10460 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10461 *
10462 * Return FAIL when no single file was found. In this case "num_file" is not
10463 * set, and "file" may contain an error message.
10464 * Return OK when some files found. "num_file" is set to the number of
10465 * matches, "file" to the array of matches. Call FreeWild() later.
10466 */
10467 int
10468gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10469 int num_pat; /* number of input patterns */
10470 char_u **pat; /* array of input patterns */
10471 int *num_file; /* resulting number of files */
10472 char_u ***file; /* array of resulting files */
10473 int flags; /* EW_* flags */
10474{
10475 int i;
10476 garray_T ga;
10477 char_u *p;
10478 static int recursive = FALSE;
10479 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010480#if defined(FEAT_SEARCHPATH)
10481 int did_expand_in_path = FALSE;
10482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
10484 /*
10485 * expand_env() is called to expand things like "~user". If this fails,
10486 * it calls ExpandOne(), which brings us back here. In this case, always
10487 * call the machine specific expansion function, if possible. Otherwise,
10488 * return FAIL.
10489 */
10490 if (recursive)
10491#ifdef SPECIAL_WILDCHAR
10492 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10493#else
10494 return FAIL;
10495#endif
10496
10497#ifdef SPECIAL_WILDCHAR
10498 /*
10499 * If there are any special wildcard characters which we cannot handle
10500 * here, call machine specific function for all the expansion. This
10501 * avoids starting the shell for each argument separately.
10502 * For `=expr` do use the internal function.
10503 */
10504 for (i = 0; i < num_pat; i++)
10505 {
10506 if (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL
10507# ifdef VIM_BACKTICK
10508 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10509# endif
10510 )
10511 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10512 }
10513#endif
10514
10515 recursive = TRUE;
10516
10517 /*
10518 * The matching file names are stored in a growarray. Init it empty.
10519 */
10520 ga_init2(&ga, (int)sizeof(char_u *), 30);
10521
10522 for (i = 0; i < num_pat; ++i)
10523 {
10524 add_pat = -1;
10525 p = pat[i];
10526
10527#ifdef VIM_BACKTICK
10528 if (vim_backtick(p))
10529 add_pat = expand_backtick(&ga, p, flags);
10530 else
10531#endif
10532 {
10533 /*
10534 * First expand environment variables, "~/" and "~user/".
10535 */
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010536 if (vim_strchr(p, '$') != NULL || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010538 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 if (p == NULL)
10540 p = pat[i];
10541#ifdef UNIX
10542 /*
10543 * On Unix, if expand_env() can't expand an environment
10544 * variable, use the shell to do that. Discard previously
10545 * found file names and start all over again.
10546 */
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010547 else if (vim_strchr(p, '$') != NULL || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 {
10549 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010550 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10552 flags);
10553 recursive = FALSE;
10554 return i;
10555 }
10556#endif
10557 }
10558
10559 /*
10560 * If there are wildcards: Expand file names and add each match to
10561 * the list. If there is no match, and EW_NOTFOUND is given, add
10562 * the pattern.
10563 * If there are no wildcards: Add the file name if it exists or
10564 * when EW_NOTFOUND is given.
10565 */
10566 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010567 {
10568#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010569 if ((flags & EW_PATH)
10570 && !mch_isFullName(p)
10571 && !(p[0] == '.'
10572 && (vim_ispathsep(p[1])
10573 || (p[1] == '.' && vim_ispathsep(p[2]))))
10574 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010575 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010576 /* :find completion where 'path' is used.
10577 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010578 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010579 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010580 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010581 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010582 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010583 else
10584#endif
10585 add_pat = mch_expandpath(&ga, p, flags);
10586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 }
10588
10589 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10590 {
10591 char_u *t = backslash_halve_save(p);
10592
10593#if defined(MACOS_CLASSIC)
10594 slash_to_colon(t);
10595#endif
10596 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10597 * "vim c:/" work. */
10598 if (flags & EW_NOTFOUND)
10599 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10600 else if (mch_getperm(t) >= 0)
10601 addfile(&ga, t, flags);
10602 vim_free(t);
10603 }
10604
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010605#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010606 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010607 uniquefy_paths(&ga, p);
10608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 if (p != pat[i])
10610 vim_free(p);
10611 }
10612
10613 *num_file = ga.ga_len;
10614 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10615
10616 recursive = FALSE;
10617
10618 return (ga.ga_data != NULL) ? OK : FAIL;
10619}
10620
10621# ifdef VIM_BACKTICK
10622
10623/*
10624 * Return TRUE if we can expand this backtick thing here.
10625 */
10626 static int
10627vim_backtick(p)
10628 char_u *p;
10629{
10630 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10631}
10632
10633/*
10634 * Expand an item in `backticks` by executing it as a command.
10635 * Currently only works when pat[] starts and ends with a `.
10636 * Returns number of file names found.
10637 */
10638 static int
10639expand_backtick(gap, pat, flags)
10640 garray_T *gap;
10641 char_u *pat;
10642 int flags; /* EW_* flags */
10643{
10644 char_u *p;
10645 char_u *cmd;
10646 char_u *buffer;
10647 int cnt = 0;
10648 int i;
10649
10650 /* Create the command: lop off the backticks. */
10651 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10652 if (cmd == NULL)
10653 return 0;
10654
10655#ifdef FEAT_EVAL
10656 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010657 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 else
10659#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010660 buffer = get_cmd_output(cmd, NULL,
10661 (flags & EW_SILENT) ? SHELL_SILENT : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 vim_free(cmd);
10663 if (buffer == NULL)
10664 return 0;
10665
10666 cmd = buffer;
10667 while (*cmd != NUL)
10668 {
10669 cmd = skipwhite(cmd); /* skip over white space */
10670 p = cmd;
10671 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10672 ++p;
10673 /* add an entry if it is not empty */
10674 if (p > cmd)
10675 {
10676 i = *p;
10677 *p = NUL;
10678 addfile(gap, cmd, flags);
10679 *p = i;
10680 ++cnt;
10681 }
10682 cmd = p;
10683 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10684 ++cmd;
10685 }
10686
10687 vim_free(buffer);
10688 return cnt;
10689}
10690# endif /* VIM_BACKTICK */
10691
10692/*
10693 * Add a file to a file list. Accepted flags:
10694 * EW_DIR add directories
10695 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010696 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 * EW_NOTFOUND add even when it doesn't exist
10698 * EW_ADDSLASH add slash after directory name
10699 */
10700 void
10701addfile(gap, f, flags)
10702 garray_T *gap;
10703 char_u *f; /* filename */
10704 int flags;
10705{
10706 char_u *p;
10707 int isdir;
10708
10709 /* if the file/dir doesn't exist, may not add it */
10710 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10711 return;
10712
10713#ifdef FNAME_ILLEGAL
10714 /* if the file/dir contains illegal characters, don't add it */
10715 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10716 return;
10717#endif
10718
10719 isdir = mch_isdir(f);
10720 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10721 return;
10722
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010723 /* If the file isn't executable, may not add it. Do accept directories. */
10724 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f))
10725 return;
10726
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 /* Make room for another item in the file list. */
10728 if (ga_grow(gap, 1) == FAIL)
10729 return;
10730
10731 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10732 if (p == NULL)
10733 return;
10734
10735 STRCPY(p, f);
10736#ifdef BACKSLASH_IN_FILENAME
10737 slash_adjust(p);
10738#endif
10739 /*
10740 * Append a slash or backslash after directory names if none is present.
10741 */
10742#ifndef DONT_ADD_PATHSEP_TO_DIR
10743 if (isdir && (flags & EW_ADDSLASH))
10744 add_pathsep(p);
10745#endif
10746 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747}
10748#endif /* !NO_EXPANDPATH */
10749
10750#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10751
10752#ifndef SEEK_SET
10753# define SEEK_SET 0
10754#endif
10755#ifndef SEEK_END
10756# define SEEK_END 2
10757#endif
10758
10759/*
10760 * Get the stdout of an external command.
10761 * Returns an allocated string, or NULL for error.
10762 */
10763 char_u *
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010764get_cmd_output(cmd, infile, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010766 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 int flags; /* can be SHELL_SILENT */
10768{
10769 char_u *tempname;
10770 char_u *command;
10771 char_u *buffer = NULL;
10772 int len;
10773 int i = 0;
10774 FILE *fd;
10775
10776 if (check_restricted() || check_secure())
10777 return NULL;
10778
10779 /* get a name for the temp file */
10780 if ((tempname = vim_tempname('o')) == NULL)
10781 {
10782 EMSG(_(e_notmp));
10783 return NULL;
10784 }
10785
10786 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010787 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 if (command == NULL)
10789 goto done;
10790
10791 /*
10792 * Call the shell to execute the command (errors are ignored).
10793 * Don't check timestamps here.
10794 */
10795 ++no_check_timestamps;
10796 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10797 --no_check_timestamps;
10798
10799 vim_free(command);
10800
10801 /*
10802 * read the names from the file into memory
10803 */
10804# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010805 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 fd = mch_fopen((char *)tempname, "r");
10807# else
10808 fd = mch_fopen((char *)tempname, READBIN);
10809# endif
10810
10811 if (fd == NULL)
10812 {
10813 EMSG2(_(e_notopen), tempname);
10814 goto done;
10815 }
10816
10817 fseek(fd, 0L, SEEK_END);
10818 len = ftell(fd); /* get size of temp file */
10819 fseek(fd, 0L, SEEK_SET);
10820
10821 buffer = alloc(len + 1);
10822 if (buffer != NULL)
10823 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10824 fclose(fd);
10825 mch_remove(tempname);
10826 if (buffer == NULL)
10827 goto done;
10828#ifdef VMS
10829 len = i; /* VMS doesn't give us what we asked for... */
10830#endif
10831 if (i != len)
10832 {
10833 EMSG2(_(e_notread), tempname);
10834 vim_free(buffer);
10835 buffer = NULL;
10836 }
10837 else
Bram Moolenaar162bd912010-07-28 22:29:10 +020010838 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839
10840done:
10841 vim_free(tempname);
10842 return buffer;
10843}
10844#endif
10845
10846/*
10847 * Free the list of files returned by expand_wildcards() or other expansion
10848 * functions.
10849 */
10850 void
10851FreeWild(count, files)
10852 int count;
10853 char_u **files;
10854{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010855 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 return;
10857#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10858 /*
10859 * Is this still OK for when other functions than expand_wildcards() have
10860 * been used???
10861 */
10862 _fnexplodefree((char **)files);
10863#else
10864 while (count--)
10865 vim_free(files[count]);
10866 vim_free(files);
10867#endif
10868}
10869
10870/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010871 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 * Don't do this when still processing a command or a mapping.
10873 * Don't do this when inside a ":normal" command.
10874 */
10875 int
10876goto_im()
10877{
10878 return (p_im && stuff_empty() && typebuf_typed());
10879}