blob: 1c2f8f274dc73bf56b9d3f904a395c060053df31 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static char_u *vim_version_dir __ARGS((char_u *vimdir));
18static char_u *remove_tail __ARGS((char_u *p, char_u *pend, char_u *name));
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar01b626c2013-06-16 22:49:14 +020020static void init_users __ARGS((void));
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022static int copy_indent __ARGS((int size, char_u *src));
23
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
33get_indent()
34{
Bram Moolenaar597a4222014-06-25 14:39:50 +020035 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Count the size (in window cells) of the indent in line "lnum".
40 */
41 int
42get_indent_lnum(lnum)
43 linenr_T lnum;
44{
Bram Moolenaar597a4222014-06-25 14:39:50 +020045 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046}
47
48#if defined(FEAT_FOLDING) || defined(PROTO)
49/*
50 * Count the size (in window cells) of the indent in line "lnum" of buffer
51 * "buf".
52 */
53 int
54get_indent_buf(buf, lnum)
55 buf_T *buf;
56 linenr_T lnum;
57{
Bram Moolenaar597a4222014-06-25 14:39:50 +020058 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059}
60#endif
61
62/*
63 * count the size (in window cells) of the indent in line "ptr", with
64 * 'tabstop' at "ts"
65 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000066 int
Bram Moolenaar597a4222014-06-25 14:39:50 +020067get_indent_str(ptr, ts, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 char_u *ptr;
69 int ts;
Bram Moolenaar597a4222014-06-25 14:39:50 +020070 int list; /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
72 int count = 0;
73
74 for ( ; *ptr; ++ptr)
75 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020076 if (*ptr == TAB)
77 {
78 if (!list || lcs_tab1) /* count a tab for what it is worth */
79 count += ts - (count % ts);
80 else
81 /* in list mode, when tab is not set, count screen char width for Tab: ^I */
82 count += ptr2cells(ptr);
83 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 else if (*ptr == ' ')
85 ++count; /* count a space for one */
86 else
87 break;
88 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000089 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000090}
91
92/*
93 * Set the indent of the current line.
94 * Leaves the cursor on the first non-blank in the line.
95 * Caller must take care of undo.
96 * "flags":
97 * SIN_CHANGED: call changed_bytes() if the line was changed.
98 * SIN_INSERT: insert the indent in front of the line.
99 * SIN_UNDO: save line for undo before changing it.
100 * Returns TRUE if the line was changed.
101 */
102 int
103set_indent(size, flags)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000104 int size; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 int flags;
106{
107 char_u *p;
108 char_u *newline;
109 char_u *oldline;
110 char_u *s;
111 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000112 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 int line_len;
114 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000115 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000117 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000118 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000119 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121 /*
122 * First check if there is anything to do and compute the number of
123 * characters needed for the indent.
124 */
125 todo = size;
126 ind_len = 0;
127 p = oldline = ml_get_curline();
128
129 /* Calculate the buffer size for the new indent, and check to see if it
130 * isn't already set */
131
Bram Moolenaar5002c292007-07-24 13:26:15 +0000132 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
133 * 'preserveindent' are set count the number of characters at the
134 * beginning of the line to be copied */
135 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 {
137 /* If 'preserveindent' is set then reuse as much as possible of
138 * the existing indent structure for the new indent */
139 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
140 {
141 ind_done = 0;
142
143 /* count as many characters as we can use */
144 while (todo > 0 && vim_iswhite(*p))
145 {
146 if (*p == TAB)
147 {
148 tab_pad = (int)curbuf->b_p_ts
149 - (ind_done % (int)curbuf->b_p_ts);
150 /* stop if this tab will overshoot the target */
151 if (todo < tab_pad)
152 break;
153 todo -= tab_pad;
154 ++ind_len;
155 ind_done += tab_pad;
156 }
157 else
158 {
159 --todo;
160 ++ind_len;
161 ++ind_done;
162 }
163 ++p;
164 }
165
Bram Moolenaar5002c292007-07-24 13:26:15 +0000166 /* Set initial number of whitespace chars to copy if we are
167 * preserving indent but expandtab is set */
168 if (curbuf->b_p_et)
169 orig_char_len = ind_len;
170
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 /* Fill to next tabstop with a tab, if possible */
172 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000173 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 {
175 doit = TRUE;
176 todo -= tab_pad;
177 ++ind_len;
178 /* ind_done += tab_pad; */
179 }
180 }
181
182 /* count tabs required for indent */
183 while (todo >= (int)curbuf->b_p_ts)
184 {
185 if (*p != TAB)
186 doit = TRUE;
187 else
188 ++p;
189 todo -= (int)curbuf->b_p_ts;
190 ++ind_len;
191 /* ind_done += (int)curbuf->b_p_ts; */
192 }
193 }
194 /* count spaces required for indent */
195 while (todo > 0)
196 {
197 if (*p != ' ')
198 doit = TRUE;
199 else
200 ++p;
201 --todo;
202 ++ind_len;
203 /* ++ind_done; */
204 }
205
206 /* Return if the indent is OK already. */
207 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
208 return FALSE;
209
210 /* Allocate memory for the new line. */
211 if (flags & SIN_INSERT)
212 p = oldline;
213 else
214 p = skipwhite(p);
215 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000216
217 /* If 'preserveindent' and 'expandtab' are both set keep the original
218 * characters and allocate accordingly. We will fill the rest with spaces
219 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000220 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000221 {
222 newline = alloc(orig_char_len + size - ind_done + line_len);
223 if (newline == NULL)
224 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000225 todo = size - ind_done;
226 ind_len = orig_char_len + todo; /* Set total length of indent in
227 * characters, which may have been
228 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000229 p = oldline;
230 s = newline;
231 while (orig_char_len > 0)
232 {
233 *s++ = *p++;
234 orig_char_len--;
235 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000236
Bram Moolenaar5002c292007-07-24 13:26:15 +0000237 /* Skip over any additional white space (useful when newindent is less
238 * than old) */
239 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000240 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000241
Bram Moolenaar5002c292007-07-24 13:26:15 +0000242 }
243 else
244 {
245 todo = size;
246 newline = alloc(ind_len + line_len);
247 if (newline == NULL)
248 return FALSE;
249 s = newline;
250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251
252 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 /* if 'expandtab' isn't set: use TABs */
254 if (!curbuf->b_p_et)
255 {
256 /* If 'preserveindent' is set then reuse as much as possible of
257 * the existing indent structure for the new indent */
258 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
259 {
260 p = oldline;
261 ind_done = 0;
262
263 while (todo > 0 && vim_iswhite(*p))
264 {
265 if (*p == TAB)
266 {
267 tab_pad = (int)curbuf->b_p_ts
268 - (ind_done % (int)curbuf->b_p_ts);
269 /* stop if this tab will overshoot the target */
270 if (todo < tab_pad)
271 break;
272 todo -= tab_pad;
273 ind_done += tab_pad;
274 }
275 else
276 {
277 --todo;
278 ++ind_done;
279 }
280 *s++ = *p++;
281 }
282
283 /* Fill to next tabstop with a tab, if possible */
284 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
285 if (todo >= tab_pad)
286 {
287 *s++ = TAB;
288 todo -= tab_pad;
289 }
290
291 p = skipwhite(p);
292 }
293
294 while (todo >= (int)curbuf->b_p_ts)
295 {
296 *s++ = TAB;
297 todo -= (int)curbuf->b_p_ts;
298 }
299 }
300 while (todo > 0)
301 {
302 *s++ = ' ';
303 --todo;
304 }
305 mch_memmove(s, p, (size_t)line_len);
306
307 /* Replace the line (unless undo fails). */
308 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
309 {
310 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
311 if (flags & SIN_CHANGED)
312 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200313 /* Correct saved cursor position if it is in this line. */
314 if (saved_cursor.lnum == curwin->w_cursor.lnum)
315 {
316 if (saved_cursor.col >= (colnr_T)(p - oldline))
317 /* cursor was after the indent, adjust for the number of
318 * bytes added/removed */
319 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
320 else if (saved_cursor.col >= (colnr_T)(s - newline))
321 /* cursor was in the indent, and is now after it, put it back
322 * at the start of the indent (replacing spaces with TAB) */
323 saved_cursor.col = (colnr_T)(s - newline);
324 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000325 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 }
327 else
328 vim_free(newline);
329
330 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000331 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332}
333
334/*
335 * Copy the indent from ptr to the current line (and fill to size)
336 * Leaves the cursor on the first non-blank in the line.
337 * Returns TRUE if the line was changed.
338 */
339 static int
340copy_indent(size, src)
341 int size;
342 char_u *src;
343{
344 char_u *p = NULL;
345 char_u *line = NULL;
346 char_u *s;
347 int todo;
348 int ind_len;
349 int line_len = 0;
350 int tab_pad;
351 int ind_done;
352 int round;
353
354 /* Round 1: compute the number of characters needed for the indent
355 * Round 2: copy the characters. */
356 for (round = 1; round <= 2; ++round)
357 {
358 todo = size;
359 ind_len = 0;
360 ind_done = 0;
361 s = src;
362
363 /* Count/copy the usable portion of the source line */
364 while (todo > 0 && vim_iswhite(*s))
365 {
366 if (*s == TAB)
367 {
368 tab_pad = (int)curbuf->b_p_ts
369 - (ind_done % (int)curbuf->b_p_ts);
370 /* Stop if this tab will overshoot the target */
371 if (todo < tab_pad)
372 break;
373 todo -= tab_pad;
374 ind_done += tab_pad;
375 }
376 else
377 {
378 --todo;
379 ++ind_done;
380 }
381 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000382 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 *p++ = *s;
384 ++s;
385 }
386
387 /* Fill to next tabstop with a tab, if possible */
388 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200389 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {
391 todo -= tab_pad;
392 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000393 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 *p++ = TAB;
395 }
396
397 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200398 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {
400 todo -= (int)curbuf->b_p_ts;
401 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000402 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 *p++ = TAB;
404 }
405
406 /* Count/add spaces required for indent */
407 while (todo > 0)
408 {
409 --todo;
410 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 *p++ = ' ';
413 }
414
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000415 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {
417 /* Allocate memory for the result: the copied indent, new indent
418 * and the rest of the line. */
419 line_len = (int)STRLEN(ml_get_curline()) + 1;
420 line = alloc(ind_len + line_len);
421 if (line == NULL)
422 return FALSE;
423 p = line;
424 }
425 }
426
427 /* Append the original line */
428 mch_memmove(p, ml_get_curline(), (size_t)line_len);
429
430 /* Replace the line */
431 ml_replace(curwin->w_cursor.lnum, line, FALSE);
432
433 /* Put the cursor after the indent. */
434 curwin->w_cursor.col = ind_len;
435 return TRUE;
436}
437
438/*
439 * Return the indent of the current line after a number. Return -1 if no
440 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000441 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 */
443 int
444get_number_indent(lnum)
445 linenr_T lnum;
446{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 colnr_T col;
448 pos_T pos;
449
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200450 regmatch_T regmatch;
451 int lead_len = 0; /* length of comment leader */
452
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (lnum > curbuf->b_ml.ml_line_count)
454 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000455 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200456
457#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200458 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
459 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200460 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000461#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
463 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200464 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200465 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200466
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200467 /* vim_regexec() expects a pointer to a line. This lets us
468 * start matching for the flp beyond any comment leader... */
469 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200471 pos.lnum = lnum;
472 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200474 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200475#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200476 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200477 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200478 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000479
480 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 getvcol(curwin, &pos, &col, NULL, NULL);
483 return (int)col;
484}
485
Bram Moolenaar597a4222014-06-25 14:39:50 +0200486#if defined(FEAT_LINEBREAK) || defined(PROTO)
487/*
488 * Return appropriate space number for breakindent, taking influencing
489 * parameters into account. Window must be specified, since it is not
490 * necessarily always the current one.
491 */
492 int
493get_breakindent_win(wp, line)
494 win_T *wp;
495 char_u *line; /* start of the line */
496{
497 static int prev_indent = 0; /* cached indent value */
498 static long prev_ts = 0L; /* cached tabstop value */
499 static char_u *prev_line = NULL; /* cached pointer to line */
500 int bri = 0;
501 /* window width minus window margin space, i.e. what rests for text */
502 const int eff_wwidth = W_WIDTH(wp)
503 - ((wp->w_p_nu || wp->w_p_rnu)
504 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
505 ? number_width(wp) + 1 : 0);
506
507 /* used cached indent, unless pointer or 'tabstop' changed */
508 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts)
509 {
510 prev_line = line;
511 prev_ts = wp->w_buffer->b_p_ts;
512 prev_indent = get_indent_str(line,
513 (int)wp->w_buffer->b_p_ts, wp->w_p_list) + wp->w_p_brishift;
514 }
515
516 /* indent minus the length of the showbreak string */
517 bri = prev_indent;
518 if (wp->w_p_brisbr)
519 bri -= vim_strsize(p_sbr);
520
521 /* Add offset for number column, if 'n' is in 'cpoptions' */
522 bri += win_col_off2(wp);
523
524 /* never indent past left window margin */
525 if (bri < 0)
526 bri = 0;
527 /* always leave at least bri_min characters on the left,
528 * if text width is sufficient */
529 else if (bri > eff_wwidth - wp->w_p_brimin)
530 bri = (eff_wwidth - wp->w_p_brimin < 0)
531 ? 0 : eff_wwidth - wp->w_p_brimin;
532
533 return bri;
534}
535#endif
536
537
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
539
540static int cin_is_cinword __ARGS((char_u *line));
541
542/*
543 * Return TRUE if the string "line" starts with a word from 'cinwords'.
544 */
545 static int
546cin_is_cinword(line)
547 char_u *line;
548{
549 char_u *cinw;
550 char_u *cinw_buf;
551 int cinw_len;
552 int retval = FALSE;
553 int len;
554
555 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
556 cinw_buf = alloc((unsigned)cinw_len);
557 if (cinw_buf != NULL)
558 {
559 line = skipwhite(line);
560 for (cinw = curbuf->b_p_cinw; *cinw; )
561 {
562 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
563 if (STRNCMP(line, cinw_buf, len) == 0
564 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
565 {
566 retval = TRUE;
567 break;
568 }
569 }
570 vim_free(cinw_buf);
571 }
572 return retval;
573}
574#endif
575
576/*
577 * open_line: Add a new line below or above the current line.
578 *
579 * For VREPLACE mode, we only add a new line when we get to the end of the
580 * file, otherwise we just start replacing the next line.
581 *
582 * Caller must take care of undo. Since VREPLACE may affect any number of
583 * lines however, it may call u_save_cursor() again when starting to change a
584 * new line.
585 * "flags": OPENLINE_DELSPACES delete spaces after cursor
586 * OPENLINE_DO_COM format comments
587 * OPENLINE_KEEPTRAIL keep trailing spaces
588 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200589 * OPENLINE_COM_LIST format comments with list or 2nd line indent
590 *
591 * "second_line_indent": indent for after ^^D in Insert mode or if flag
592 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 *
594 * Return TRUE for success, FALSE for failure
595 */
596 int
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200597open_line(dir, flags, second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 int dir; /* FORWARD or BACKWARD */
599 int flags;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200600 int second_line_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 char_u *saved_line; /* copy of the original line */
603 char_u *next_line = NULL; /* copy of the next line */
604 char_u *p_extra = NULL; /* what goes to next line */
605 int less_cols = 0; /* less columns for mark in new line */
606 int less_cols_off = 0; /* columns to skip for mark adjust */
607 pos_T old_cursor; /* old cursor position */
608 int newcol = 0; /* new cursor column */
609 int newindent = 0; /* auto-indent of the new line */
610 int n;
611 int trunc_line = FALSE; /* truncate current line afterwards */
612 int retval = FALSE; /* return value, default is FAIL */
613#ifdef FEAT_COMMENTS
614 int extra_len = 0; /* length of p_extra string */
615 int lead_len; /* length of comment leader */
616 char_u *lead_flags; /* position in 'comments' for comment leader */
617 char_u *leader = NULL; /* copy of comment leader */
618#endif
619 char_u *allocated = NULL; /* allocated memory */
620#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
621 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
622 char_u *p;
623#endif
624 int saved_char = NUL; /* init for GCC */
625#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
626 pos_T *pos;
627#endif
628#ifdef FEAT_SMARTINDENT
629 int do_si = (!p_paste && curbuf->b_p_si
630# ifdef FEAT_CINDENT
631 && !curbuf->b_p_cin
632# endif
633 );
634 int no_si = FALSE; /* reset did_si afterwards */
635 int first_char = NUL; /* init for GCC */
636#endif
637#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
638 int vreplace_mode;
639#endif
640 int did_append; /* appended a new line */
641 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
642
643 /*
644 * make a copy of the current line so we can mess with it
645 */
646 saved_line = vim_strsave(ml_get_curline());
647 if (saved_line == NULL) /* out of memory! */
648 return FALSE;
649
650#ifdef FEAT_VREPLACE
651 if (State & VREPLACE_FLAG)
652 {
653 /*
654 * With VREPLACE we make a copy of the next line, which we will be
655 * starting to replace. First make the new line empty and let vim play
656 * with the indenting and comment leader to its heart's content. Then
657 * we grab what it ended up putting on the new line, put back the
658 * original line, and call ins_char() to put each new character onto
659 * the line, replacing what was there before and pushing the right
660 * stuff onto the replace stack. -- webb.
661 */
662 if (curwin->w_cursor.lnum < orig_line_count)
663 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
664 else
665 next_line = vim_strsave((char_u *)"");
666 if (next_line == NULL) /* out of memory! */
667 goto theend;
668
669 /*
670 * In VREPLACE mode, a NL replaces the rest of the line, and starts
671 * replacing the next line, so push all of the characters left on the
672 * line onto the replace stack. We'll push any other characters that
673 * might be replaced at the start of the next line (due to autoindent
674 * etc) a bit later.
675 */
676 replace_push(NUL); /* Call twice because BS over NL expects it */
677 replace_push(NUL);
678 p = saved_line + curwin->w_cursor.col;
679 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000680 {
681#ifdef FEAT_MBYTE
682 if (has_mbyte)
683 p += replace_push_mb(p);
684 else
685#endif
686 replace_push(*p++);
687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 saved_line[curwin->w_cursor.col] = NUL;
689 }
690#endif
691
692 if ((State & INSERT)
693#ifdef FEAT_VREPLACE
694 && !(State & VREPLACE_FLAG)
695#endif
696 )
697 {
698 p_extra = saved_line + curwin->w_cursor.col;
699#ifdef FEAT_SMARTINDENT
700 if (do_si) /* need first char after new line break */
701 {
702 p = skipwhite(p_extra);
703 first_char = *p;
704 }
705#endif
706#ifdef FEAT_COMMENTS
707 extra_len = (int)STRLEN(p_extra);
708#endif
709 saved_char = *p_extra;
710 *p_extra = NUL;
711 }
712
713 u_clearline(); /* cannot do "U" command when adding lines */
714#ifdef FEAT_SMARTINDENT
715 did_si = FALSE;
716#endif
717 ai_col = 0;
718
719 /*
720 * If we just did an auto-indent, then we didn't type anything on
721 * the prior line, and it should be truncated. Do this even if 'ai' is not
722 * set because automatically inserting a comment leader also sets did_ai.
723 */
724 if (dir == FORWARD && did_ai)
725 trunc_line = TRUE;
726
727 /*
728 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
729 * indent to use for the new line.
730 */
731 if (curbuf->b_p_ai
732#ifdef FEAT_SMARTINDENT
733 || do_si
734#endif
735 )
736 {
737 /*
738 * count white space on current line
739 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200740 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200741 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
742 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744#ifdef FEAT_SMARTINDENT
745 /*
746 * Do smart indenting.
747 * In insert/replace mode (only when dir == FORWARD)
748 * we may move some text to the next line. If it starts with '{'
749 * don't add an indent. Fixes inserting a NL before '{' in line
750 * "if (condition) {"
751 */
752 if (!trunc_line && do_si && *saved_line != NUL
753 && (p_extra == NULL || first_char != '{'))
754 {
755 char_u *ptr;
756 char_u last_char;
757
758 old_cursor = curwin->w_cursor;
759 ptr = saved_line;
760# ifdef FEAT_COMMENTS
761 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200762 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 else
764 lead_len = 0;
765# endif
766 if (dir == FORWARD)
767 {
768 /*
769 * Skip preprocessor directives, unless they are
770 * recognised as comments.
771 */
772 if (
773# ifdef FEAT_COMMENTS
774 lead_len == 0 &&
775# endif
776 ptr[0] == '#')
777 {
778 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
779 ptr = ml_get(--curwin->w_cursor.lnum);
780 newindent = get_indent();
781 }
782# ifdef FEAT_COMMENTS
783 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200784 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 else
786 lead_len = 0;
787 if (lead_len > 0)
788 {
789 /*
790 * This case gets the following right:
791 * \*
792 * * A comment (read '\' as '/').
793 * *\
794 * #define IN_THE_WAY
795 * This should line up here;
796 */
797 p = skipwhite(ptr);
798 if (p[0] == '/' && p[1] == '*')
799 p++;
800 if (p[0] == '*')
801 {
802 for (p++; *p; p++)
803 {
804 if (p[0] == '/' && p[-1] == '*')
805 {
806 /*
807 * End of C comment, indent should line up
808 * with the line containing the start of
809 * the comment
810 */
811 curwin->w_cursor.col = (colnr_T)(p - ptr);
812 if ((pos = findmatch(NULL, NUL)) != NULL)
813 {
814 curwin->w_cursor.lnum = pos->lnum;
815 newindent = get_indent();
816 }
817 }
818 }
819 }
820 }
821 else /* Not a comment line */
822# endif
823 {
824 /* Find last non-blank in line */
825 p = ptr + STRLEN(ptr) - 1;
826 while (p > ptr && vim_iswhite(*p))
827 --p;
828 last_char = *p;
829
830 /*
831 * find the character just before the '{' or ';'
832 */
833 if (last_char == '{' || last_char == ';')
834 {
835 if (p > ptr)
836 --p;
837 while (p > ptr && vim_iswhite(*p))
838 --p;
839 }
840 /*
841 * Try to catch lines that are split over multiple
842 * lines. eg:
843 * if (condition &&
844 * condition) {
845 * Should line up here!
846 * }
847 */
848 if (*p == ')')
849 {
850 curwin->w_cursor.col = (colnr_T)(p - ptr);
851 if ((pos = findmatch(NULL, '(')) != NULL)
852 {
853 curwin->w_cursor.lnum = pos->lnum;
854 newindent = get_indent();
855 ptr = ml_get_curline();
856 }
857 }
858 /*
859 * If last character is '{' do indent, without
860 * checking for "if" and the like.
861 */
862 if (last_char == '{')
863 {
864 did_si = TRUE; /* do indent */
865 no_si = TRUE; /* don't delete it when '{' typed */
866 }
867 /*
868 * Look for "if" and the like, use 'cinwords'.
869 * Don't do this if the previous line ended in ';' or
870 * '}'.
871 */
872 else if (last_char != ';' && last_char != '}'
873 && cin_is_cinword(ptr))
874 did_si = TRUE;
875 }
876 }
877 else /* dir == BACKWARD */
878 {
879 /*
880 * Skip preprocessor directives, unless they are
881 * recognised as comments.
882 */
883 if (
884# ifdef FEAT_COMMENTS
885 lead_len == 0 &&
886# endif
887 ptr[0] == '#')
888 {
889 int was_backslashed = FALSE;
890
891 while ((ptr[0] == '#' || was_backslashed) &&
892 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
893 {
894 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
895 was_backslashed = TRUE;
896 else
897 was_backslashed = FALSE;
898 ptr = ml_get(++curwin->w_cursor.lnum);
899 }
900 if (was_backslashed)
901 newindent = 0; /* Got to end of file */
902 else
903 newindent = get_indent();
904 }
905 p = skipwhite(ptr);
906 if (*p == '}') /* if line starts with '}': do indent */
907 did_si = TRUE;
908 else /* can delete indent when '{' typed */
909 can_si_back = TRUE;
910 }
911 curwin->w_cursor = old_cursor;
912 }
913 if (do_si)
914 can_si = TRUE;
915#endif /* FEAT_SMARTINDENT */
916
917 did_ai = TRUE;
918 }
919
920#ifdef FEAT_COMMENTS
921 /*
922 * Find out if the current line starts with a comment leader.
923 * This may then be inserted in front of the new line.
924 */
925 end_comment_pending = NUL;
926 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200927 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else
929 lead_len = 0;
930 if (lead_len > 0)
931 {
932 char_u *lead_repl = NULL; /* replaces comment leader */
933 int lead_repl_len = 0; /* length of *lead_repl */
934 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
935 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
936 char_u *comment_end = NULL; /* where lead_end has been found */
937 int extra_space = FALSE; /* append extra space */
938 int current_flag;
939 int require_blank = FALSE; /* requires blank after middle */
940 char_u *p2;
941
942 /*
943 * If the comment leader has the start, middle or end flag, it may not
944 * be used or may be replaced with the middle leader.
945 */
946 for (p = lead_flags; *p && *p != ':'; ++p)
947 {
948 if (*p == COM_BLANK)
949 {
950 require_blank = TRUE;
951 continue;
952 }
953 if (*p == COM_START || *p == COM_MIDDLE)
954 {
955 current_flag = *p;
956 if (*p == COM_START)
957 {
958 /*
959 * Doing "O" on a start of comment does not insert leader.
960 */
961 if (dir == BACKWARD)
962 {
963 lead_len = 0;
964 break;
965 }
966
967 /* find start of middle part */
968 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
969 require_blank = FALSE;
970 }
971
972 /*
973 * Isolate the strings of the middle and end leader.
974 */
975 while (*p && p[-1] != ':') /* find end of middle flags */
976 {
977 if (*p == COM_BLANK)
978 require_blank = TRUE;
979 ++p;
980 }
981 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
982
983 while (*p && p[-1] != ':') /* find end of end flags */
984 {
985 /* Check whether we allow automatic ending of comments */
986 if (*p == COM_AUTO_END)
987 end_comment_pending = -1; /* means we want to set it */
988 ++p;
989 }
990 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
991
992 if (end_comment_pending == -1) /* we can set it now */
993 end_comment_pending = lead_end[n - 1];
994
995 /*
996 * If the end of the comment is in the same line, don't use
997 * the comment leader.
998 */
999 if (dir == FORWARD)
1000 {
1001 for (p = saved_line + lead_len; *p; ++p)
1002 if (STRNCMP(p, lead_end, n) == 0)
1003 {
1004 comment_end = p;
1005 lead_len = 0;
1006 break;
1007 }
1008 }
1009
1010 /*
1011 * Doing "o" on a start of comment inserts the middle leader.
1012 */
1013 if (lead_len > 0)
1014 {
1015 if (current_flag == COM_START)
1016 {
1017 lead_repl = lead_middle;
1018 lead_repl_len = (int)STRLEN(lead_middle);
1019 }
1020
1021 /*
1022 * If we have hit RETURN immediately after the start
1023 * comment leader, then put a space after the middle
1024 * comment leader on the next line.
1025 */
1026 if (!vim_iswhite(saved_line[lead_len - 1])
1027 && ((p_extra != NULL
1028 && (int)curwin->w_cursor.col == lead_len)
1029 || (p_extra == NULL
1030 && saved_line[lead_len] == NUL)
1031 || require_blank))
1032 extra_space = TRUE;
1033 }
1034 break;
1035 }
1036 if (*p == COM_END)
1037 {
1038 /*
1039 * Doing "o" on the end of a comment does not insert leader.
1040 * Remember where the end is, might want to use it to find the
1041 * start (for C-comments).
1042 */
1043 if (dir == FORWARD)
1044 {
1045 comment_end = skipwhite(saved_line);
1046 lead_len = 0;
1047 break;
1048 }
1049
1050 /*
1051 * Doing "O" on the end of a comment inserts the middle leader.
1052 * Find the string for the middle leader, searching backwards.
1053 */
1054 while (p > curbuf->b_p_com && *p != ',')
1055 --p;
1056 for (lead_repl = p; lead_repl > curbuf->b_p_com
1057 && lead_repl[-1] != ':'; --lead_repl)
1058 ;
1059 lead_repl_len = (int)(p - lead_repl);
1060
1061 /* We can probably always add an extra space when doing "O" on
1062 * the comment-end */
1063 extra_space = TRUE;
1064
1065 /* Check whether we allow automatic ending of comments */
1066 for (p2 = p; *p2 && *p2 != ':'; p2++)
1067 {
1068 if (*p2 == COM_AUTO_END)
1069 end_comment_pending = -1; /* means we want to set it */
1070 }
1071 if (end_comment_pending == -1)
1072 {
1073 /* Find last character in end-comment string */
1074 while (*p2 && *p2 != ',')
1075 p2++;
1076 end_comment_pending = p2[-1];
1077 }
1078 break;
1079 }
1080 if (*p == COM_FIRST)
1081 {
1082 /*
1083 * Comment leader for first line only: Don't repeat leader
1084 * when using "O", blank out leader when using "o".
1085 */
1086 if (dir == BACKWARD)
1087 lead_len = 0;
1088 else
1089 {
1090 lead_repl = (char_u *)"";
1091 lead_repl_len = 0;
1092 }
1093 break;
1094 }
1095 }
1096 if (lead_len)
1097 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001098 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001099 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001100 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 allocated = leader; /* remember to free it later */
1102
1103 if (leader == NULL)
1104 lead_len = 0;
1105 else
1106 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001107 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 /*
1110 * Replace leader with lead_repl, right or left adjusted
1111 */
1112 if (lead_repl != NULL)
1113 {
1114 int c = 0;
1115 int off = 0;
1116
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001117 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else if (VIM_ISDIGIT(*p) || *p == '-')
1122 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001123 else
1124 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 if (c == COM_RIGHT) /* right adjusted leader */
1127 {
1128 /* find last non-white in the leader to line up with */
1129 for (p = leader + lead_len - 1; p > leader
1130 && vim_iswhite(*p); --p)
1131 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001133
1134#ifdef FEAT_MBYTE
1135 /* Compute the length of the replaced characters in
1136 * screen characters, not bytes. */
1137 {
1138 int repl_size = vim_strnsize(lead_repl,
1139 lead_repl_len);
1140 int old_size = 0;
1141 char_u *endp = p;
1142 int l;
1143
1144 while (old_size < repl_size && p > leader)
1145 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001146 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 old_size += ptr2cells(p);
1148 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001149 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001150 if (l != 0)
1151 mch_memmove(endp + l, endp,
1152 (size_t)((leader + lead_len) - endp));
1153 lead_len += l;
1154 }
1155#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (p < leader + lead_repl_len)
1157 p = leader;
1158 else
1159 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1162 if (p + lead_repl_len > leader + lead_len)
1163 p[lead_repl_len] = NUL;
1164
1165 /* blank-out any other chars from the old leader. */
1166 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001167 {
1168#ifdef FEAT_MBYTE
1169 int l = mb_head_off(leader, p);
1170
1171 if (l > 1)
1172 {
1173 p -= l;
1174 if (ptr2cells(p) > 1)
1175 {
1176 p[1] = ' ';
1177 --l;
1178 }
1179 mch_memmove(p + 1, p + l + 1,
1180 (size_t)((leader + lead_len) - (p + l + 1)));
1181 lead_len -= l;
1182 *p = ' ';
1183 }
1184 else
1185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (!vim_iswhite(*p))
1187 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else /* left adjusted leader */
1191 {
1192 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001193#ifdef FEAT_MBYTE
1194 /* Compute the length of the replaced characters in
1195 * screen characters, not bytes. Move the part that is
1196 * not to be overwritten. */
1197 {
1198 int repl_size = vim_strnsize(lead_repl,
1199 lead_repl_len);
1200 int i;
1201 int l;
1202
1203 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1204 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001205 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001206 if (vim_strnsize(p, i + l) > repl_size)
1207 break;
1208 }
1209 if (i != lead_repl_len)
1210 {
1211 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001212 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001213 lead_len += lead_repl_len - i;
1214 }
1215 }
1216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1218
1219 /* Replace any remaining non-white chars in the old
1220 * leader by spaces. Keep Tabs, the indent must
1221 * remain the same. */
1222 for (p += lead_repl_len; p < leader + lead_len; ++p)
1223 if (!vim_iswhite(*p))
1224 {
1225 /* Don't put a space before a TAB. */
1226 if (p + 1 < leader + lead_len && p[1] == TAB)
1227 {
1228 --lead_len;
1229 mch_memmove(p, p + 1,
1230 (leader + lead_len) - p);
1231 }
1232 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001233 {
1234#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001235 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001236
1237 if (l > 1)
1238 {
1239 if (ptr2cells(p) > 1)
1240 {
1241 /* Replace a double-wide char with
1242 * two spaces */
1243 --l;
1244 *p++ = ' ';
1245 }
1246 mch_memmove(p + 1, p + l,
1247 (leader + lead_len) - p);
1248 lead_len -= l - 1;
1249 }
1250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 *p = NUL;
1255 }
1256
1257 /* Recompute the indent, it may have changed. */
1258 if (curbuf->b_p_ai
1259#ifdef FEAT_SMARTINDENT
1260 || do_si
1261#endif
1262 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001263 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 /* Add the indent offset */
1266 if (newindent + off < 0)
1267 {
1268 off = -newindent;
1269 newindent = 0;
1270 }
1271 else
1272 newindent += off;
1273
1274 /* Correct trailing spaces for the shift, so that
1275 * alignment remains equal. */
1276 while (off > 0 && lead_len > 0
1277 && leader[lead_len - 1] == ' ')
1278 {
1279 /* Don't do it when there is a tab before the space */
1280 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1281 break;
1282 --lead_len;
1283 --off;
1284 }
1285
1286 /* If the leader ends in white space, don't add an
1287 * extra space */
1288 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1289 extra_space = FALSE;
1290 leader[lead_len] = NUL;
1291 }
1292
1293 if (extra_space)
1294 {
1295 leader[lead_len++] = ' ';
1296 leader[lead_len] = NUL;
1297 }
1298
1299 newcol = lead_len;
1300
1301 /*
1302 * if a new indent will be set below, remove the indent that
1303 * is in the comment leader
1304 */
1305 if (newindent
1306#ifdef FEAT_SMARTINDENT
1307 || did_si
1308#endif
1309 )
1310 {
1311 while (lead_len && vim_iswhite(*leader))
1312 {
1313 --lead_len;
1314 --newcol;
1315 ++leader;
1316 }
1317 }
1318
1319 }
1320#ifdef FEAT_SMARTINDENT
1321 did_si = can_si = FALSE;
1322#endif
1323 }
1324 else if (comment_end != NULL)
1325 {
1326 /*
1327 * We have finished a comment, so we don't use the leader.
1328 * If this was a C-comment and 'ai' or 'si' is set do a normal
1329 * indent to align with the line containing the start of the
1330 * comment.
1331 */
1332 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1333 (curbuf->b_p_ai
1334#ifdef FEAT_SMARTINDENT
1335 || do_si
1336#endif
1337 ))
1338 {
1339 old_cursor = curwin->w_cursor;
1340 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1341 if ((pos = findmatch(NULL, NUL)) != NULL)
1342 {
1343 curwin->w_cursor.lnum = pos->lnum;
1344 newindent = get_indent();
1345 }
1346 curwin->w_cursor = old_cursor;
1347 }
1348 }
1349 }
1350#endif
1351
1352 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1353 if (p_extra != NULL)
1354 {
1355 *p_extra = saved_char; /* restore char that NUL replaced */
1356
1357 /*
1358 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1359 * non-blank.
1360 *
1361 * When in REPLACE mode, put the deleted blanks on the replace stack,
1362 * preceded by a NUL, so they can be put back when a BS is entered.
1363 */
1364 if (REPLACE_NORMAL(State))
1365 replace_push(NUL); /* end of extra blanks */
1366 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1367 {
1368 while ((*p_extra == ' ' || *p_extra == '\t')
1369#ifdef FEAT_MBYTE
1370 && (!enc_utf8
1371 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1372#endif
1373 )
1374 {
1375 if (REPLACE_NORMAL(State))
1376 replace_push(*p_extra);
1377 ++p_extra;
1378 ++less_cols_off;
1379 }
1380 }
1381 if (*p_extra != NUL)
1382 did_ai = FALSE; /* append some text, don't truncate now */
1383
1384 /* columns for marks adjusted for removed columns */
1385 less_cols = (int)(p_extra - saved_line);
1386 }
1387
1388 if (p_extra == NULL)
1389 p_extra = (char_u *)""; /* append empty line */
1390
1391#ifdef FEAT_COMMENTS
1392 /* concatenate leader and p_extra, if there is a leader */
1393 if (lead_len)
1394 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001395 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1396 {
1397 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001398 int padding = second_line_indent
1399 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001400
1401 /* Here whitespace is inserted after the comment char.
1402 * Below, set_indent(newindent, SIN_INSERT) will insert the
1403 * whitespace needed before the comment char. */
1404 for (i = 0; i < padding; i++)
1405 {
1406 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001407 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001408 newcol++;
1409 }
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 STRCAT(leader, p_extra);
1412 p_extra = leader;
1413 did_ai = TRUE; /* So truncating blanks works with comments */
1414 less_cols -= lead_len;
1415 }
1416 else
1417 end_comment_pending = NUL; /* turns out there was no leader */
1418#endif
1419
1420 old_cursor = curwin->w_cursor;
1421 if (dir == BACKWARD)
1422 --curwin->w_cursor.lnum;
1423#ifdef FEAT_VREPLACE
1424 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1425#endif
1426 {
1427 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1428 == FAIL)
1429 goto theend;
1430 /* Postpone calling changed_lines(), because it would mess up folding
1431 * with markers. */
1432 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1433 did_append = TRUE;
1434 }
1435#ifdef FEAT_VREPLACE
1436 else
1437 {
1438 /*
1439 * In VREPLACE mode we are starting to replace the next line.
1440 */
1441 curwin->w_cursor.lnum++;
1442 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1443 {
1444 /* In case we NL to a new line, BS to the previous one, and NL
1445 * again, we don't want to save the new line for undo twice.
1446 */
1447 (void)u_save_cursor(); /* errors are ignored! */
1448 vr_lines_changed++;
1449 }
1450 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1451 changed_bytes(curwin->w_cursor.lnum, 0);
1452 curwin->w_cursor.lnum--;
1453 did_append = FALSE;
1454 }
1455#endif
1456
1457 if (newindent
1458#ifdef FEAT_SMARTINDENT
1459 || did_si
1460#endif
1461 )
1462 {
1463 ++curwin->w_cursor.lnum;
1464#ifdef FEAT_SMARTINDENT
1465 if (did_si)
1466 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001467 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001470 newindent -= newindent % sw;
1471 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001474 /* Copy the indent */
1475 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 (void)copy_indent(newindent, saved_line);
1478
1479 /*
1480 * Set the 'preserveindent' option so that any further screwing
1481 * with the line doesn't entirely destroy our efforts to preserve
1482 * it. It gets restored at the function end.
1483 */
1484 curbuf->b_p_pi = TRUE;
1485 }
1486 else
1487 (void)set_indent(newindent, SIN_INSERT);
1488 less_cols -= curwin->w_cursor.col;
1489
1490 ai_col = curwin->w_cursor.col;
1491
1492 /*
1493 * In REPLACE mode, for each character in the new indent, there must
1494 * be a NUL on the replace stack, for when it is deleted with BS
1495 */
1496 if (REPLACE_NORMAL(State))
1497 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1498 replace_push(NUL);
1499 newcol += curwin->w_cursor.col;
1500#ifdef FEAT_SMARTINDENT
1501 if (no_si)
1502 did_si = FALSE;
1503#endif
1504 }
1505
1506#ifdef FEAT_COMMENTS
1507 /*
1508 * In REPLACE mode, for each character in the extra leader, there must be
1509 * a NUL on the replace stack, for when it is deleted with BS.
1510 */
1511 if (REPLACE_NORMAL(State))
1512 while (lead_len-- > 0)
1513 replace_push(NUL);
1514#endif
1515
1516 curwin->w_cursor = old_cursor;
1517
1518 if (dir == FORWARD)
1519 {
1520 if (trunc_line || (State & INSERT))
1521 {
1522 /* truncate current line at cursor */
1523 saved_line[curwin->w_cursor.col] = NUL;
1524 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1525 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1526 truncate_spaces(saved_line);
1527 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1528 saved_line = NULL;
1529 if (did_append)
1530 {
1531 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1532 curwin->w_cursor.lnum + 1, 1L);
1533 did_append = FALSE;
1534
1535 /* Move marks after the line break to the new line. */
1536 if (flags & OPENLINE_MARKFIX)
1537 mark_col_adjust(curwin->w_cursor.lnum,
1538 curwin->w_cursor.col + less_cols_off,
1539 1L, (long)-less_cols);
1540 }
1541 else
1542 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1543 }
1544
1545 /*
1546 * Put the cursor on the new line. Careful: the scrollup() above may
1547 * have moved w_cursor, we must use old_cursor.
1548 */
1549 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1550 }
1551 if (did_append)
1552 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1553
1554 curwin->w_cursor.col = newcol;
1555#ifdef FEAT_VIRTUALEDIT
1556 curwin->w_cursor.coladd = 0;
1557#endif
1558
1559#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1560 /*
1561 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1562 * fixthisline() from doing it (via change_indent()) by telling it we're in
1563 * normal INSERT mode.
1564 */
1565 if (State & VREPLACE_FLAG)
1566 {
1567 vreplace_mode = State; /* So we know to put things right later */
1568 State = INSERT;
1569 }
1570 else
1571 vreplace_mode = 0;
1572#endif
1573#ifdef FEAT_LISP
1574 /*
1575 * May do lisp indenting.
1576 */
1577 if (!p_paste
1578# ifdef FEAT_COMMENTS
1579 && leader == NULL
1580# endif
1581 && curbuf->b_p_lisp
1582 && curbuf->b_p_ai)
1583 {
1584 fixthisline(get_lisp_indent);
1585 p = ml_get_curline();
1586 ai_col = (colnr_T)(skipwhite(p) - p);
1587 }
1588#endif
1589#ifdef FEAT_CINDENT
1590 /*
1591 * May do indenting after opening a new line.
1592 */
1593 if (!p_paste
1594 && (curbuf->b_p_cin
1595# ifdef FEAT_EVAL
1596 || *curbuf->b_p_inde != NUL
1597# endif
1598 )
1599 && in_cinkeys(dir == FORWARD
1600 ? KEY_OPEN_FORW
1601 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1602 {
1603 do_c_expr_indent();
1604 p = ml_get_curline();
1605 ai_col = (colnr_T)(skipwhite(p) - p);
1606 }
1607#endif
1608#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1609 if (vreplace_mode != 0)
1610 State = vreplace_mode;
1611#endif
1612
1613#ifdef FEAT_VREPLACE
1614 /*
1615 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1616 * original line, and inserts the new stuff char by char, pushing old stuff
1617 * onto the replace stack (via ins_char()).
1618 */
1619 if (State & VREPLACE_FLAG)
1620 {
1621 /* Put new line in p_extra */
1622 p_extra = vim_strsave(ml_get_curline());
1623 if (p_extra == NULL)
1624 goto theend;
1625
1626 /* Put back original line */
1627 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1628
1629 /* Insert new stuff into line again */
1630 curwin->w_cursor.col = 0;
1631#ifdef FEAT_VIRTUALEDIT
1632 curwin->w_cursor.coladd = 0;
1633#endif
1634 ins_bytes(p_extra); /* will call changed_bytes() */
1635 vim_free(p_extra);
1636 next_line = NULL;
1637 }
1638#endif
1639
1640 retval = TRUE; /* success! */
1641theend:
1642 curbuf->b_p_pi = saved_pi;
1643 vim_free(saved_line);
1644 vim_free(next_line);
1645 vim_free(allocated);
1646 return retval;
1647}
1648
1649#if defined(FEAT_COMMENTS) || defined(PROTO)
1650/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001651 * get_leader_len() returns the length in bytes of the prefix of the given
1652 * string which introduces a comment. If this string is not a comment then
1653 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * When "flags" is not NULL, it is set to point to the flags of the recognized
1655 * comment leader.
1656 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001657 * If "include_space" is set, include trailing whitespace while calculating the
1658 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
1660 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001661get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 char_u *line;
1663 char_u **flags;
1664 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001665 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001668 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int got_com = FALSE;
1670 int found_one;
1671 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1672 char_u *string; /* pointer to comment string */
1673 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001674 int middle_match_len = 0;
1675 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001676 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaar81340392012-06-06 16:12:59 +02001678 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 while (vim_iswhite(line[i])) /* leading white space is ignored */
1680 ++i;
1681
1682 /*
1683 * Repeat to match several nested comment strings.
1684 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001685 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 /*
1688 * scan through the 'comments' option for a match
1689 */
1690 found_one = FALSE;
1691 for (list = curbuf->b_p_com; *list; )
1692 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001693 /* Get one option part into part_buf[]. Advance "list" to next
1694 * one. Put "string" at start of string. */
1695 if (!got_com && flags != NULL)
1696 *flags = list; /* remember where flags started */
1697 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1699 string = vim_strchr(part_buf, ':');
1700 if (string == NULL) /* missing ':', ignore this part */
1701 continue;
1702 *string++ = NUL; /* isolate flags from string */
1703
Bram Moolenaara4271d52011-05-10 13:38:27 +02001704 /* If we found a middle match previously, use that match when this
1705 * is not a middle or end. */
1706 if (middle_match_len != 0
1707 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1708 && vim_strchr(part_buf, COM_END) == NULL)
1709 break;
1710
1711 /* When we already found a nested comment, only accept further
1712 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1714 continue;
1715
Bram Moolenaara4271d52011-05-10 13:38:27 +02001716 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1718 continue;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 * When string starts with white space, must have some white space
1722 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001723 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (vim_iswhite(string[0]))
1725 {
1726 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001727 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 while (vim_iswhite(string[0]))
1729 ++string;
1730 }
1731 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1732 ;
1733 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001734 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaara4271d52011-05-10 13:38:27 +02001736 /* When 'b' flag used, there must be white space or an
1737 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (vim_strchr(part_buf, COM_BLANK) != NULL
1739 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1740 continue;
1741
Bram Moolenaara4271d52011-05-10 13:38:27 +02001742 /* We have found a match, stop searching unless this is a middle
1743 * comment. The middle comment can be a substring of the end
1744 * comment in which case it's better to return the length of the
1745 * end comment and its flags. Thus we keep searching with middle
1746 * and end matches and use an end match if it matches better. */
1747 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1748 {
1749 if (middle_match_len == 0)
1750 {
1751 middle_match_len = j;
1752 saved_flags = prev_list;
1753 }
1754 continue;
1755 }
1756 if (middle_match_len != 0 && j > middle_match_len)
1757 /* Use this match instead of the middle match, since it's a
1758 * longer thus better match. */
1759 middle_match_len = 0;
1760
1761 if (middle_match_len == 0)
1762 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 found_one = TRUE;
1764 break;
1765 }
1766
Bram Moolenaara4271d52011-05-10 13:38:27 +02001767 if (middle_match_len != 0)
1768 {
1769 /* Use the previously found middle match after failing to find a
1770 * match with an end. */
1771 if (!got_com && flags != NULL)
1772 *flags = saved_flags;
1773 i += middle_match_len;
1774 found_one = TRUE;
1775 }
1776
1777 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (!found_one)
1779 break;
1780
Bram Moolenaar81340392012-06-06 16:12:59 +02001781 result = i;
1782
Bram Moolenaara4271d52011-05-10 13:38:27 +02001783 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 while (vim_iswhite(line[i]))
1785 ++i;
1786
Bram Moolenaar81340392012-06-06 16:12:59 +02001787 if (include_space)
1788 result = i;
1789
Bram Moolenaara4271d52011-05-10 13:38:27 +02001790 /* If this comment doesn't nest, stop here. */
1791 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (vim_strchr(part_buf, COM_NEST) == NULL)
1793 break;
1794 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001795 return result;
1796}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001797
Bram Moolenaar81340392012-06-06 16:12:59 +02001798/*
1799 * Return the offset at which the last comment in line starts. If there is no
1800 * comment in the whole line, -1 is returned.
1801 *
1802 * When "flags" is not null, it is set to point to the flags describing the
1803 * recognized comment leader.
1804 */
1805 int
1806get_last_leader_offset(line, flags)
1807 char_u *line;
1808 char_u **flags;
1809{
1810 int result = -1;
1811 int i, j;
1812 int lower_check_bound = 0;
1813 char_u *string;
1814 char_u *com_leader;
1815 char_u *com_flags;
1816 char_u *list;
1817 int found_one;
1818 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1819
1820 /*
1821 * Repeat to match several nested comment strings.
1822 */
1823 i = (int)STRLEN(line);
1824 while (--i >= lower_check_bound)
1825 {
1826 /*
1827 * scan through the 'comments' option for a match
1828 */
1829 found_one = FALSE;
1830 for (list = curbuf->b_p_com; *list; )
1831 {
1832 char_u *flags_save = list;
1833
1834 /*
1835 * Get one option part into part_buf[]. Advance list to next one.
1836 * put string at start of string.
1837 */
1838 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1839 string = vim_strchr(part_buf, ':');
1840 if (string == NULL) /* If everything is fine, this cannot actually
1841 * happen. */
1842 {
1843 continue;
1844 }
1845 *string++ = NUL; /* Isolate flags from string. */
1846 com_leader = string;
1847
1848 /*
1849 * Line contents and string must match.
1850 * When string starts with white space, must have some white space
1851 * (but the amount does not need to match, there might be a mix of
1852 * TABs and spaces).
1853 */
1854 if (vim_iswhite(string[0]))
1855 {
1856 if (i == 0 || !vim_iswhite(line[i - 1]))
1857 continue;
1858 while (vim_iswhite(string[0]))
1859 ++string;
1860 }
1861 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1862 /* do nothing */;
1863 if (string[j] != NUL)
1864 continue;
1865
1866 /*
1867 * When 'b' flag used, there must be white space or an
1868 * end-of-line after the string in the line.
1869 */
1870 if (vim_strchr(part_buf, COM_BLANK) != NULL
1871 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1872 {
1873 continue;
1874 }
1875
1876 /*
1877 * We have found a match, stop searching.
1878 */
1879 found_one = TRUE;
1880
1881 if (flags)
1882 *flags = flags_save;
1883 com_flags = flags_save;
1884
1885 break;
1886 }
1887
1888 if (found_one)
1889 {
1890 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1891 int len1, len2, off;
1892
1893 result = i;
1894 /*
1895 * If this comment nests, continue searching.
1896 */
1897 if (vim_strchr(part_buf, COM_NEST) != NULL)
1898 continue;
1899
1900 lower_check_bound = i;
1901
1902 /* Let's verify whether the comment leader found is a substring
1903 * of other comment leaders. If it is, let's adjust the
1904 * lower_check_bound so that we make sure that we have determined
1905 * the comment leader correctly.
1906 */
1907
1908 while (vim_iswhite(*com_leader))
1909 ++com_leader;
1910 len1 = (int)STRLEN(com_leader);
1911
1912 for (list = curbuf->b_p_com; *list; )
1913 {
1914 char_u *flags_save = list;
1915
1916 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1917 if (flags_save == com_flags)
1918 continue;
1919 string = vim_strchr(part_buf2, ':');
1920 ++string;
1921 while (vim_iswhite(*string))
1922 ++string;
1923 len2 = (int)STRLEN(string);
1924 if (len2 == 0)
1925 continue;
1926
1927 /* Now we have to verify whether string ends with a substring
1928 * beginning the com_leader. */
1929 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1930 {
1931 --off;
1932 if (!STRNCMP(string + off, com_leader, len2 - off))
1933 {
1934 if (i - off < lower_check_bound)
1935 lower_check_bound = i - off;
1936 }
1937 }
1938 }
1939 }
1940 }
1941 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942}
1943#endif
1944
1945/*
1946 * Return the number of window lines occupied by buffer line "lnum".
1947 */
1948 int
1949plines(lnum)
1950 linenr_T lnum;
1951{
1952 return plines_win(curwin, lnum, TRUE);
1953}
1954
1955 int
1956plines_win(wp, lnum, winheight)
1957 win_T *wp;
1958 linenr_T lnum;
1959 int winheight; /* when TRUE limit to window height */
1960{
1961#if defined(FEAT_DIFF) || defined(PROTO)
1962 /* Check for filler lines above this buffer line. When folded the result
1963 * is one line anyway. */
1964 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1965}
1966
1967 int
1968plines_nofill(lnum)
1969 linenr_T lnum;
1970{
1971 return plines_win_nofill(curwin, lnum, TRUE);
1972}
1973
1974 int
1975plines_win_nofill(wp, lnum, winheight)
1976 win_T *wp;
1977 linenr_T lnum;
1978 int winheight; /* when TRUE limit to window height */
1979{
1980#endif
1981 int lines;
1982
1983 if (!wp->w_p_wrap)
1984 return 1;
1985
1986#ifdef FEAT_VERTSPLIT
1987 if (wp->w_width == 0)
1988 return 1;
1989#endif
1990
1991#ifdef FEAT_FOLDING
1992 /* A folded lines is handled just like an empty line. */
1993 /* NOTE: Caller must handle lines that are MAYBE folded. */
1994 if (lineFolded(wp, lnum) == TRUE)
1995 return 1;
1996#endif
1997
1998 lines = plines_win_nofold(wp, lnum);
1999 if (winheight > 0 && lines > wp->w_height)
2000 return (int)wp->w_height;
2001 return lines;
2002}
2003
2004/*
2005 * Return number of window lines physical line "lnum" will occupy in window
2006 * "wp". Does not care about folding, 'wrap' or 'diff'.
2007 */
2008 int
2009plines_win_nofold(wp, lnum)
2010 win_T *wp;
2011 linenr_T lnum;
2012{
2013 char_u *s;
2014 long col;
2015 int width;
2016
2017 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2018 if (*s == NUL) /* empty line */
2019 return 1;
2020 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2021
2022 /*
2023 * If list mode is on, then the '$' at the end of the line may take up one
2024 * extra column.
2025 */
2026 if (wp->w_p_list && lcs_eol != NUL)
2027 col += 1;
2028
2029 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002030 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 */
2032 width = W_WIDTH(wp) - win_col_off(wp);
2033 if (width <= 0)
2034 return 32000;
2035 if (col <= width)
2036 return 1;
2037 col -= width;
2038 width += win_col_off2(wp);
2039 return (col + (width - 1)) / width + 1;
2040}
2041
2042/*
2043 * Like plines_win(), but only reports the number of physical screen lines
2044 * used from the start of the line to the given column number.
2045 */
2046 int
2047plines_win_col(wp, lnum, column)
2048 win_T *wp;
2049 linenr_T lnum;
2050 long column;
2051{
2052 long col;
2053 char_u *s;
2054 int lines = 0;
2055 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002056 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058#ifdef FEAT_DIFF
2059 /* Check for filler lines above this buffer line. When folded the result
2060 * is one line anyway. */
2061 lines = diff_check_fill(wp, lnum);
2062#endif
2063
2064 if (!wp->w_p_wrap)
2065 return lines + 1;
2066
2067#ifdef FEAT_VERTSPLIT
2068 if (wp->w_width == 0)
2069 return lines + 1;
2070#endif
2071
Bram Moolenaar597a4222014-06-25 14:39:50 +02002072 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 col = 0;
2075 while (*s != NUL && --column >= 0)
2076 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002077 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002078 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080
2081 /*
2082 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2083 * INSERT mode, then col must be adjusted so that it represents the last
2084 * screen position of the TAB. This only fixes an error when the TAB wraps
2085 * from one screen line to the next (when 'columns' is not a multiple of
2086 * 'ts') -- webb.
2087 */
2088 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002089 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002092 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 */
2094 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002095 if (width <= 0)
2096 return 9999;
2097
2098 lines += 1;
2099 if (col > width)
2100 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2101 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102}
2103
2104 int
2105plines_m_win(wp, first, last)
2106 win_T *wp;
2107 linenr_T first, last;
2108{
2109 int count = 0;
2110
2111 while (first <= last)
2112 {
2113#ifdef FEAT_FOLDING
2114 int x;
2115
2116 /* Check if there are any really folded lines, but also included lines
2117 * that are maybe folded. */
2118 x = foldedCount(wp, first, NULL);
2119 if (x > 0)
2120 {
2121 ++count; /* count 1 for "+-- folded" line */
2122 first += x;
2123 }
2124 else
2125#endif
2126 {
2127#ifdef FEAT_DIFF
2128 if (first == wp->w_topline)
2129 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2130 else
2131#endif
2132 count += plines_win(wp, first, TRUE);
2133 ++first;
2134 }
2135 }
2136 return (count);
2137}
2138
2139#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2140/*
2141 * Insert string "p" at the cursor position. Stops at a NUL byte.
2142 * Handles Replace mode and multi-byte characters.
2143 */
2144 void
2145ins_bytes(p)
2146 char_u *p;
2147{
2148 ins_bytes_len(p, (int)STRLEN(p));
2149}
2150#endif
2151
2152#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2153 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2154/*
2155 * Insert string "p" with length "len" at the cursor position.
2156 * Handles Replace mode and multi-byte characters.
2157 */
2158 void
2159ins_bytes_len(p, len)
2160 char_u *p;
2161 int len;
2162{
2163 int i;
2164# ifdef FEAT_MBYTE
2165 int n;
2166
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002167 if (has_mbyte)
2168 for (i = 0; i < len; i += n)
2169 {
2170 if (enc_utf8)
2171 /* avoid reading past p[len] */
2172 n = utfc_ptr2len_len(p + i, len - i);
2173 else
2174 n = (*mb_ptr2len)(p + i);
2175 ins_char_bytes(p + i, n);
2176 }
2177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002179 for (i = 0; i < len; ++i)
2180 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181}
2182#endif
2183
2184/*
2185 * Insert or replace a single character at the cursor position.
2186 * When in REPLACE or VREPLACE mode, replace any existing character.
2187 * Caller must have prepared for undo.
2188 * For multi-byte characters we get the whole character, the caller must
2189 * convert bytes to a character.
2190 */
2191 void
2192ins_char(c)
2193 int c;
2194{
2195#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002196 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 int n;
2198
2199 n = (*mb_char2bytes)(c, buf);
2200
2201 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2202 * Happens for CTRL-Vu9900. */
2203 if (buf[0] == 0)
2204 buf[0] = '\n';
2205
2206 ins_char_bytes(buf, n);
2207}
2208
2209 void
2210ins_char_bytes(buf, charlen)
2211 char_u *buf;
2212 int charlen;
2213{
2214 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#endif
2216 int newlen; /* nr of bytes inserted */
2217 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2218 char_u *p;
2219 char_u *newp;
2220 char_u *oldp;
2221 int linelen; /* length of old line including NUL */
2222 colnr_T col;
2223 linenr_T lnum = curwin->w_cursor.lnum;
2224 int i;
2225
2226#ifdef FEAT_VIRTUALEDIT
2227 /* Break tabs if needed. */
2228 if (virtual_active() && curwin->w_cursor.coladd > 0)
2229 coladvance_force(getviscol());
2230#endif
2231
2232 col = curwin->w_cursor.col;
2233 oldp = ml_get(lnum);
2234 linelen = (int)STRLEN(oldp) + 1;
2235
2236 /* The lengths default to the values for when not replacing. */
2237 oldlen = 0;
2238#ifdef FEAT_MBYTE
2239 newlen = charlen;
2240#else
2241 newlen = 1;
2242#endif
2243
2244 if (State & REPLACE_FLAG)
2245 {
2246#ifdef FEAT_VREPLACE
2247 if (State & VREPLACE_FLAG)
2248 {
2249 colnr_T new_vcol = 0; /* init for GCC */
2250 colnr_T vcol;
2251 int old_list;
2252#ifndef FEAT_MBYTE
2253 char_u buf[2];
2254#endif
2255
2256 /*
2257 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2258 * Returns the old value of list, so when finished,
2259 * curwin->w_p_list should be set back to this.
2260 */
2261 old_list = curwin->w_p_list;
2262 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2263 curwin->w_p_list = FALSE;
2264
2265 /*
2266 * In virtual replace mode each character may replace one or more
2267 * characters (zero if it's a TAB). Count the number of bytes to
2268 * be deleted to make room for the new character, counting screen
2269 * cells. May result in adding spaces to fill a gap.
2270 */
2271 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2272#ifndef FEAT_MBYTE
2273 buf[0] = c;
2274 buf[1] = NUL;
2275#endif
2276 new_vcol = vcol + chartabsize(buf, vcol);
2277 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2278 {
2279 vcol += chartabsize(oldp + col + oldlen, vcol);
2280 /* Don't need to remove a TAB that takes us to the right
2281 * position. */
2282 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2283 break;
2284#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002285 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286#else
2287 ++oldlen;
2288#endif
2289 /* Deleted a bit too much, insert spaces. */
2290 if (vcol > new_vcol)
2291 newlen += vcol - new_vcol;
2292 }
2293 curwin->w_p_list = old_list;
2294 }
2295 else
2296#endif
2297 if (oldp[col] != NUL)
2298 {
2299 /* normal replace */
2300#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002301 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302#else
2303 oldlen = 1;
2304#endif
2305 }
2306
2307
2308 /* Push the replaced bytes onto the replace stack, so that they can be
2309 * put back when BS is used. The bytes of a multi-byte character are
2310 * done the other way around, so that the first byte is popped off
2311 * first (it tells the byte length of the character). */
2312 replace_push(NUL);
2313 for (i = 0; i < oldlen; ++i)
2314 {
2315#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002316 if (has_mbyte)
2317 i += replace_push_mb(oldp + col + i) - 1;
2318 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002320 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
2322 }
2323
2324 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2325 if (newp == NULL)
2326 return;
2327
2328 /* Copy bytes before the cursor. */
2329 if (col > 0)
2330 mch_memmove(newp, oldp, (size_t)col);
2331
2332 /* Copy bytes after the changed character(s). */
2333 p = newp + col;
2334 mch_memmove(p + newlen, oldp + col + oldlen,
2335 (size_t)(linelen - col - oldlen));
2336
2337 /* Insert or overwrite the new character. */
2338#ifdef FEAT_MBYTE
2339 mch_memmove(p, buf, charlen);
2340 i = charlen;
2341#else
2342 *p = c;
2343 i = 1;
2344#endif
2345
2346 /* Fill with spaces when necessary. */
2347 while (i < newlen)
2348 p[i++] = ' ';
2349
2350 /* Replace the line in the buffer. */
2351 ml_replace(lnum, newp, FALSE);
2352
2353 /* mark the buffer as changed and prepare for displaying */
2354 changed_bytes(lnum, col);
2355
2356 /*
2357 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2358 * show the match for right parens and braces.
2359 */
2360 if (p_sm && (State & INSERT)
2361 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002362#ifdef FEAT_INS_EXPAND
2363 && !ins_compl_active()
2364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002366 {
2367#ifdef FEAT_MBYTE
2368 if (has_mbyte)
2369 showmatch(mb_ptr2char(buf));
2370 else
2371#endif
2372 showmatch(c);
2373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375#ifdef FEAT_RIGHTLEFT
2376 if (!p_ri || (State & REPLACE_FLAG))
2377#endif
2378 {
2379 /* Normal insert: move cursor right */
2380#ifdef FEAT_MBYTE
2381 curwin->w_cursor.col += charlen;
2382#else
2383 ++curwin->w_cursor.col;
2384#endif
2385 }
2386 /*
2387 * TODO: should try to update w_row here, to avoid recomputing it later.
2388 */
2389}
2390
2391/*
2392 * Insert a string at the cursor position.
2393 * Note: Does NOT handle Replace mode.
2394 * Caller must have prepared for undo.
2395 */
2396 void
2397ins_str(s)
2398 char_u *s;
2399{
2400 char_u *oldp, *newp;
2401 int newlen = (int)STRLEN(s);
2402 int oldlen;
2403 colnr_T col;
2404 linenr_T lnum = curwin->w_cursor.lnum;
2405
2406#ifdef FEAT_VIRTUALEDIT
2407 if (virtual_active() && curwin->w_cursor.coladd > 0)
2408 coladvance_force(getviscol());
2409#endif
2410
2411 col = curwin->w_cursor.col;
2412 oldp = ml_get(lnum);
2413 oldlen = (int)STRLEN(oldp);
2414
2415 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2416 if (newp == NULL)
2417 return;
2418 if (col > 0)
2419 mch_memmove(newp, oldp, (size_t)col);
2420 mch_memmove(newp + col, s, (size_t)newlen);
2421 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2422 ml_replace(lnum, newp, FALSE);
2423 changed_bytes(lnum, col);
2424 curwin->w_cursor.col += newlen;
2425}
2426
2427/*
2428 * Delete one character under the cursor.
2429 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2430 * Caller must have prepared for undo.
2431 *
2432 * return FAIL for failure, OK otherwise
2433 */
2434 int
2435del_char(fixpos)
2436 int fixpos;
2437{
2438#ifdef FEAT_MBYTE
2439 if (has_mbyte)
2440 {
2441 /* Make sure the cursor is at the start of a character. */
2442 mb_adjust_cursor();
2443 if (*ml_get_cursor() == NUL)
2444 return FAIL;
2445 return del_chars(1L, fixpos);
2446 }
2447#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002448 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449}
2450
2451#if defined(FEAT_MBYTE) || defined(PROTO)
2452/*
2453 * Like del_bytes(), but delete characters instead of bytes.
2454 */
2455 int
2456del_chars(count, fixpos)
2457 long count;
2458 int fixpos;
2459{
2460 long bytes = 0;
2461 long i;
2462 char_u *p;
2463 int l;
2464
2465 p = ml_get_cursor();
2466 for (i = 0; i < count && *p != NUL; ++i)
2467 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002468 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 bytes += l;
2470 p += l;
2471 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002472 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473}
2474#endif
2475
2476/*
2477 * Delete "count" bytes under the cursor.
2478 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2479 * Caller must have prepared for undo.
2480 *
2481 * return FAIL for failure, OK otherwise
2482 */
2483 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002484del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002486 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002487 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
2489 char_u *oldp, *newp;
2490 colnr_T oldlen;
2491 linenr_T lnum = curwin->w_cursor.lnum;
2492 colnr_T col = curwin->w_cursor.col;
2493 int was_alloced;
2494 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002495 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 oldp = ml_get(lnum);
2498 oldlen = (int)STRLEN(oldp);
2499
2500 /*
2501 * Can't do anything when the cursor is on the NUL after the line.
2502 */
2503 if (col >= oldlen)
2504 return FAIL;
2505
2506#ifdef FEAT_MBYTE
2507 /* If 'delcombine' is set and deleting (less than) one character, only
2508 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002509 if (p_deco && use_delcombine && enc_utf8
2510 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002512 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 int n;
2514
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002515 (void)utfc_ptr2char(oldp + col, cc);
2516 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 /* Find the last composing char, there can be several. */
2519 n = col;
2520 do
2521 {
2522 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002523 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 n += count;
2525 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2526 fixpos = 0;
2527 }
2528 }
2529#endif
2530
2531 /*
2532 * When count is too big, reduce it.
2533 */
2534 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2535 if (movelen <= 1)
2536 {
2537 /*
2538 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002539 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2540 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002542 if (col > 0 && fixpos && restart_edit == 0
2543#ifdef FEAT_VIRTUALEDIT
2544 && (ve_flags & VE_ONEMORE) == 0
2545#endif
2546 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 --curwin->w_cursor.col;
2549#ifdef FEAT_VIRTUALEDIT
2550 curwin->w_cursor.coladd = 0;
2551#endif
2552#ifdef FEAT_MBYTE
2553 if (has_mbyte)
2554 curwin->w_cursor.col -=
2555 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2556#endif
2557 }
2558 count = oldlen - col;
2559 movelen = 1;
2560 }
2561
2562 /*
2563 * If the old line has been allocated the deletion can be done in the
2564 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002565 * Can't do this when using Netbeans, because we would need to invoke
2566 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002567 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002570 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002571 was_alloced = FALSE;
2572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002574 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (was_alloced)
2576 newp = oldp; /* use same allocated memory */
2577 else
2578 { /* need to allocate a new line */
2579 newp = alloc((unsigned)(oldlen + 1 - count));
2580 if (newp == NULL)
2581 return FAIL;
2582 mch_memmove(newp, oldp, (size_t)col);
2583 }
2584 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2585 if (!was_alloced)
2586 ml_replace(lnum, newp, FALSE);
2587
2588 /* mark the buffer as changed and prepare for displaying */
2589 changed_bytes(lnum, curwin->w_cursor.col);
2590
2591 return OK;
2592}
2593
2594/*
2595 * Delete from cursor to end of line.
2596 * Caller must have prepared for undo.
2597 *
2598 * return FAIL for failure, OK otherwise
2599 */
2600 int
2601truncate_line(fixpos)
2602 int fixpos; /* if TRUE fix the cursor position when done */
2603{
2604 char_u *newp;
2605 linenr_T lnum = curwin->w_cursor.lnum;
2606 colnr_T col = curwin->w_cursor.col;
2607
2608 if (col == 0)
2609 newp = vim_strsave((char_u *)"");
2610 else
2611 newp = vim_strnsave(ml_get(lnum), col);
2612
2613 if (newp == NULL)
2614 return FAIL;
2615
2616 ml_replace(lnum, newp, FALSE);
2617
2618 /* mark the buffer as changed and prepare for displaying */
2619 changed_bytes(lnum, curwin->w_cursor.col);
2620
2621 /*
2622 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2623 */
2624 if (fixpos && curwin->w_cursor.col > 0)
2625 --curwin->w_cursor.col;
2626
2627 return OK;
2628}
2629
2630/*
2631 * Delete "nlines" lines at the cursor.
2632 * Saves the lines for undo first if "undo" is TRUE.
2633 */
2634 void
2635del_lines(nlines, undo)
2636 long nlines; /* number of lines to delete */
2637 int undo; /* if TRUE, prepare for undo */
2638{
2639 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002640 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 if (nlines <= 0)
2643 return;
2644
2645 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002646 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 return;
2648
2649 for (n = 0; n < nlines; )
2650 {
2651 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2652 break;
2653
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002654 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 ++n;
2656
2657 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002658 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 break;
2660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002662 /* Correct the cursor position before calling deleted_lines_mark(), it may
2663 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 curwin->w_cursor.col = 0;
2665 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002666
2667 /* adjust marks, mark the buffer as changed and prepare for displaying */
2668 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669}
2670
2671 int
2672gchar_pos(pos)
2673 pos_T *pos;
2674{
2675 char_u *ptr = ml_get_pos(pos);
2676
2677#ifdef FEAT_MBYTE
2678 if (has_mbyte)
2679 return (*mb_ptr2char)(ptr);
2680#endif
2681 return (int)*ptr;
2682}
2683
2684 int
2685gchar_cursor()
2686{
2687#ifdef FEAT_MBYTE
2688 if (has_mbyte)
2689 return (*mb_ptr2char)(ml_get_cursor());
2690#endif
2691 return (int)*ml_get_cursor();
2692}
2693
2694/*
2695 * Write a character at the current cursor position.
2696 * It is directly written into the block.
2697 */
2698 void
2699pchar_cursor(c)
2700 int c;
2701{
2702 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2703 + curwin->w_cursor.col) = c;
2704}
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706/*
2707 * When extra == 0: Return TRUE if the cursor is before or on the first
2708 * non-blank in the line.
2709 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2710 * the line.
2711 */
2712 int
2713inindent(extra)
2714 int extra;
2715{
2716 char_u *ptr;
2717 colnr_T col;
2718
2719 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2720 ++ptr;
2721 if (col >= curwin->w_cursor.col + extra)
2722 return TRUE;
2723 else
2724 return FALSE;
2725}
2726
2727/*
2728 * Skip to next part of an option argument: Skip space and comma.
2729 */
2730 char_u *
2731skip_to_option_part(p)
2732 char_u *p;
2733{
2734 if (*p == ',')
2735 ++p;
2736 while (*p == ' ')
2737 ++p;
2738 return p;
2739}
2740
2741/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002742 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 *
2744 * Most often called through changed_bytes() and changed_lines(), which also
2745 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002746 *
2747 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 */
2749 void
2750changed()
2751{
2752#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2753 /* The text of the preediting area is inserted, but this doesn't
2754 * mean a change of the buffer yet. That is delayed until the
2755 * text is committed. (this means preedit becomes empty) */
2756 if (im_is_preediting() && !xim_changed_while_preediting)
2757 return;
2758 xim_changed_while_preediting = FALSE;
2759#endif
2760
2761 if (!curbuf->b_changed)
2762 {
2763 int save_msg_scroll = msg_scroll;
2764
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002765 /* Give a warning about changing a read-only file. This may also
2766 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 /* Create a swap file if that is wanted.
2770 * Don't do this for "nofile" and "nowrite" buffer types. */
2771 if (curbuf->b_may_swap
2772#ifdef FEAT_QUICKFIX
2773 && !bt_dontwrite(curbuf)
2774#endif
2775 )
2776 {
2777 ml_open_file(curbuf);
2778
2779 /* The ml_open_file() can cause an ATTENTION message.
2780 * Wait two seconds, to make sure the user reads this unexpected
2781 * message. Since we could be anywhere, call wait_return() now,
2782 * and don't let the emsg() set msg_scroll. */
2783 if (need_wait_return && emsg_silent == 0)
2784 {
2785 out_flush();
2786 ui_delay(2000L, TRUE);
2787 wait_return(TRUE);
2788 msg_scroll = save_msg_scroll;
2789 }
2790 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002791 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
2793 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794}
2795
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002796/*
2797 * Internal part of changed(), no user interaction.
2798 */
2799 void
2800changed_int()
2801{
2802 curbuf->b_changed = TRUE;
2803 ml_setflags(curbuf);
2804#ifdef FEAT_WINDOWS
2805 check_status(curbuf);
2806 redraw_tabline = TRUE;
2807#endif
2808#ifdef FEAT_TITLE
2809 need_maketitle = TRUE; /* set window title later */
2810#endif
2811}
2812
Bram Moolenaardba8a912005-04-24 22:08:39 +00002813static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2814static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2816
2817/*
2818 * Changed bytes within a single line for the current buffer.
2819 * - marks the windows on this buffer to be redisplayed
2820 * - marks the buffer changed by calling changed()
2821 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002822 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 */
2824 void
2825changed_bytes(lnum, col)
2826 linenr_T lnum;
2827 colnr_T col;
2828{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002829 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002831
2832#ifdef FEAT_DIFF
2833 /* Diff highlighting in other diff windows may need to be updated too. */
2834 if (curwin->w_p_diff)
2835 {
2836 win_T *wp;
2837 linenr_T wlnum;
2838
2839 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2840 if (wp->w_p_diff && wp != curwin)
2841 {
2842 redraw_win_later(wp, VALID);
2843 wlnum = diff_lnum_win(lnum, wp);
2844 if (wlnum > 0)
2845 changedOneline(wp->w_buffer, wlnum);
2846 }
2847 }
2848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849}
2850
2851 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002852changedOneline(buf, lnum)
2853 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 linenr_T lnum;
2855{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002856 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
2858 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002859 if (lnum < buf->b_mod_top)
2860 buf->b_mod_top = lnum;
2861 else if (lnum >= buf->b_mod_bot)
2862 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864 else
2865 {
2866 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002867 buf->b_mod_set = TRUE;
2868 buf->b_mod_top = lnum;
2869 buf->b_mod_bot = lnum + 1;
2870 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
2872}
2873
2874/*
2875 * Appended "count" lines below line "lnum" in the current buffer.
2876 * Must be called AFTER the change and after mark_adjust().
2877 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2878 */
2879 void
2880appended_lines(lnum, count)
2881 linenr_T lnum;
2882 long count;
2883{
2884 changed_lines(lnum + 1, 0, lnum + 1, count);
2885}
2886
2887/*
2888 * Like appended_lines(), but adjust marks first.
2889 */
2890 void
2891appended_lines_mark(lnum, count)
2892 linenr_T lnum;
2893 long count;
2894{
2895 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2896 changed_lines(lnum + 1, 0, lnum + 1, count);
2897}
2898
2899/*
2900 * Deleted "count" lines at line "lnum" in the current buffer.
2901 * Must be called AFTER the change and after mark_adjust().
2902 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2903 */
2904 void
2905deleted_lines(lnum, count)
2906 linenr_T lnum;
2907 long count;
2908{
2909 changed_lines(lnum, 0, lnum + count, -count);
2910}
2911
2912/*
2913 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002914 * Make sure the cursor is on a valid line before calling, a GUI callback may
2915 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 */
2917 void
2918deleted_lines_mark(lnum, count)
2919 linenr_T lnum;
2920 long count;
2921{
2922 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2923 changed_lines(lnum, 0, lnum + count, -count);
2924}
2925
2926/*
2927 * Changed lines for the current buffer.
2928 * Must be called AFTER the change and after mark_adjust().
2929 * - mark the buffer changed by calling changed()
2930 * - mark the windows on this buffer to be redisplayed
2931 * - invalidate cached values
2932 * "lnum" is the first line that needs displaying, "lnume" the first line
2933 * below the changed lines (BEFORE the change).
2934 * When only inserting lines, "lnum" and "lnume" are equal.
2935 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002936 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
2938 void
2939changed_lines(lnum, col, lnume, xtra)
2940 linenr_T lnum; /* first line with change */
2941 colnr_T col; /* column in first line with change */
2942 linenr_T lnume; /* line below last changed line */
2943 long xtra; /* number of extra lines (negative when deleting) */
2944{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002945 changed_lines_buf(curbuf, lnum, lnume, xtra);
2946
2947#ifdef FEAT_DIFF
2948 if (xtra == 0 && curwin->w_p_diff)
2949 {
2950 /* When the number of lines doesn't change then mark_adjust() isn't
2951 * called and other diff buffers still need to be marked for
2952 * displaying. */
2953 win_T *wp;
2954 linenr_T wlnum;
2955
2956 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2957 if (wp->w_p_diff && wp != curwin)
2958 {
2959 redraw_win_later(wp, VALID);
2960 wlnum = diff_lnum_win(lnum, wp);
2961 if (wlnum > 0)
2962 changed_lines_buf(wp->w_buffer, wlnum,
2963 lnume - lnum + wlnum, 0L);
2964 }
2965 }
2966#endif
2967
2968 changed_common(lnum, col, lnume, xtra);
2969}
2970
2971 static void
2972changed_lines_buf(buf, lnum, lnume, xtra)
2973 buf_T *buf;
2974 linenr_T lnum; /* first line with change */
2975 linenr_T lnume; /* line below last changed line */
2976 long xtra; /* number of extra lines (negative when deleting) */
2977{
2978 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
2980 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002981 if (lnum < buf->b_mod_top)
2982 buf->b_mod_top = lnum;
2983 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 {
2985 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002986 buf->b_mod_bot += xtra;
2987 if (buf->b_mod_bot < lnum)
2988 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002990 if (lnume + xtra > buf->b_mod_bot)
2991 buf->b_mod_bot = lnume + xtra;
2992 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994 else
2995 {
2996 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002997 buf->b_mod_set = TRUE;
2998 buf->b_mod_top = lnum;
2999 buf->b_mod_bot = lnume + xtra;
3000 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002}
3003
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003004/*
3005 * Common code for when a change is was made.
3006 * See changed_lines() for the arguments.
3007 * Careful: may trigger autocommands that reload the buffer.
3008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 static void
3010changed_common(lnum, col, lnume, xtra)
3011 linenr_T lnum;
3012 colnr_T col;
3013 linenr_T lnume;
3014 long xtra;
3015{
3016 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003017#ifdef FEAT_WINDOWS
3018 tabpage_T *tp;
3019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 int i;
3021#ifdef FEAT_JUMPLIST
3022 int cols;
3023 pos_T *p;
3024 int add;
3025#endif
3026
3027 /* mark the buffer as modified */
3028 changed();
3029
3030 /* set the '. mark */
3031 if (!cmdmod.keepjumps)
3032 {
3033 curbuf->b_last_change.lnum = lnum;
3034 curbuf->b_last_change.col = col;
3035
3036#ifdef FEAT_JUMPLIST
3037 /* Create a new entry if a new undo-able change was started or we
3038 * don't have an entry yet. */
3039 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3040 {
3041 if (curbuf->b_changelistlen == 0)
3042 add = TRUE;
3043 else
3044 {
3045 /* Don't create a new entry when the line number is the same
3046 * as the last one and the column is not too far away. Avoids
3047 * creating many entries for typing "xxxxx". */
3048 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3049 if (p->lnum != lnum)
3050 add = TRUE;
3051 else
3052 {
3053 cols = comp_textwidth(FALSE);
3054 if (cols == 0)
3055 cols = 79;
3056 add = (p->col + cols < col || col + cols < p->col);
3057 }
3058 }
3059 if (add)
3060 {
3061 /* This is the first of a new sequence of undo-able changes
3062 * and it's at some distance of the last change. Use a new
3063 * position in the changelist. */
3064 curbuf->b_new_change = FALSE;
3065
3066 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3067 {
3068 /* changelist is full: remove oldest entry */
3069 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3070 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3071 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003072 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
3074 /* Correct position in changelist for other windows on
3075 * this buffer. */
3076 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3077 --wp->w_changelistidx;
3078 }
3079 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003080 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 {
3082 /* For other windows, if the position in the changelist is
3083 * at the end it stays at the end. */
3084 if (wp->w_buffer == curbuf
3085 && wp->w_changelistidx == curbuf->b_changelistlen)
3086 ++wp->w_changelistidx;
3087 }
3088 ++curbuf->b_changelistlen;
3089 }
3090 }
3091 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3092 curbuf->b_last_change;
3093 /* The current window is always after the last change, so that "g,"
3094 * takes you back to it. */
3095 curwin->w_changelistidx = curbuf->b_changelistlen;
3096#endif
3097 }
3098
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003099 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 {
3101 if (wp->w_buffer == curbuf)
3102 {
3103 /* Mark this window to be redrawn later. */
3104 if (wp->w_redr_type < VALID)
3105 wp->w_redr_type = VALID;
3106
3107 /* Check if a change in the buffer has invalidated the cached
3108 * values for the cursor. */
3109#ifdef FEAT_FOLDING
3110 /*
3111 * Update the folds for this window. Can't postpone this, because
3112 * a following operator might work on the whole fold: ">>dd".
3113 */
3114 foldUpdate(wp, lnum, lnume + xtra - 1);
3115
3116 /* The change may cause lines above or below the change to become
3117 * included in a fold. Set lnum/lnume to the first/last line that
3118 * might be displayed differently.
3119 * Set w_cline_folded here as an efficient way to update it when
3120 * inserting lines just above a closed fold. */
3121 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3122 if (wp->w_cursor.lnum == lnum)
3123 wp->w_cline_folded = i;
3124 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3125 if (wp->w_cursor.lnum == lnume)
3126 wp->w_cline_folded = i;
3127
3128 /* If the changed line is in a range of previously folded lines,
3129 * compare with the first line in that range. */
3130 if (wp->w_cursor.lnum <= lnum)
3131 {
3132 i = find_wl_entry(wp, lnum);
3133 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3134 changed_line_abv_curs_win(wp);
3135 }
3136#endif
3137
3138 if (wp->w_cursor.lnum > lnum)
3139 changed_line_abv_curs_win(wp);
3140 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3141 changed_cline_bef_curs_win(wp);
3142 if (wp->w_botline >= lnum)
3143 {
3144 /* Assume that botline doesn't change (inserted lines make
3145 * other lines scroll down below botline). */
3146 approximate_botline_win(wp);
3147 }
3148
3149 /* Check if any w_lines[] entries have become invalid.
3150 * For entries below the change: Correct the lnums for
3151 * inserted/deleted lines. Makes it possible to stop displaying
3152 * after the change. */
3153 for (i = 0; i < wp->w_lines_valid; ++i)
3154 if (wp->w_lines[i].wl_valid)
3155 {
3156 if (wp->w_lines[i].wl_lnum >= lnum)
3157 {
3158 if (wp->w_lines[i].wl_lnum < lnume)
3159 {
3160 /* line included in change */
3161 wp->w_lines[i].wl_valid = FALSE;
3162 }
3163 else if (xtra != 0)
3164 {
3165 /* line below change */
3166 wp->w_lines[i].wl_lnum += xtra;
3167#ifdef FEAT_FOLDING
3168 wp->w_lines[i].wl_lastlnum += xtra;
3169#endif
3170 }
3171 }
3172#ifdef FEAT_FOLDING
3173 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3174 {
3175 /* change somewhere inside this range of folded lines,
3176 * may need to be redrawn */
3177 wp->w_lines[i].wl_valid = FALSE;
3178 }
3179#endif
3180 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003181
3182#ifdef FEAT_FOLDING
3183 /* Take care of side effects for setting w_topline when folds have
3184 * changed. Esp. when the buffer was changed in another window. */
3185 if (hasAnyFolding(wp))
3186 set_topline(wp, wp->w_topline);
3187#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003188 /* relative numbering may require updating more */
3189 if (wp->w_p_rnu)
3190 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 }
3193
3194 /* Call update_screen() later, which checks out what needs to be redrawn,
3195 * since it notices b_mod_set and then uses b_mod_*. */
3196 if (must_redraw < VALID)
3197 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003198
3199#ifdef FEAT_AUTOCMD
3200 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003201 if (lnum <= curwin->w_cursor.lnum
3202 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003203 last_cursormoved.lnum = 0;
3204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
3206
3207/*
3208 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3209 */
3210 void
3211unchanged(buf, ff)
3212 buf_T *buf;
3213 int ff; /* also reset 'fileformat' */
3214{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003215 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
3217 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003218 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (ff)
3220 save_file_ff(buf);
3221#ifdef FEAT_WINDOWS
3222 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003223 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#endif
3225#ifdef FEAT_TITLE
3226 need_maketitle = TRUE; /* set window title later */
3227#endif
3228 }
3229 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230#ifdef FEAT_NETBEANS_INTG
3231 netbeans_unmodified(buf);
3232#endif
3233}
3234
3235#if defined(FEAT_WINDOWS) || defined(PROTO)
3236/*
3237 * check_status: called when the status bars for the buffer 'buf'
3238 * need to be updated
3239 */
3240 void
3241check_status(buf)
3242 buf_T *buf;
3243{
3244 win_T *wp;
3245
3246 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3247 if (wp->w_buffer == buf && wp->w_status_height)
3248 {
3249 wp->w_redr_status = TRUE;
3250 if (must_redraw < VALID)
3251 must_redraw = VALID;
3252 }
3253}
3254#endif
3255
3256/*
3257 * If the file is readonly, give a warning message with the first change.
3258 * Don't do this for autocommands.
3259 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003260 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003262 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 */
3264 void
3265change_warning(col)
3266 int col; /* column for message; non-zero when in insert
3267 mode and 'showmode' is on */
3268{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003269 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (curbuf->b_did_warn == FALSE
3272 && curbufIsChanged() == 0
3273#ifdef FEAT_AUTOCMD
3274 && !autocmd_busy
3275#endif
3276 && curbuf->b_p_ro)
3277 {
3278#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003279 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003281 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (!curbuf->b_p_ro)
3283 return;
3284#endif
3285 /*
3286 * Do what msg() does, but with a column offset if the warning should
3287 * be after the mode message.
3288 */
3289 msg_start();
3290 if (msg_row == Rows - 1)
3291 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003292 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003293 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3294#ifdef FEAT_EVAL
3295 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 msg_clr_eos();
3298 (void)msg_end();
3299 if (msg_silent == 0 && !silent_mode)
3300 {
3301 out_flush();
3302 ui_delay(1000L, TRUE); /* give the user time to think about it */
3303 }
3304 curbuf->b_did_warn = TRUE;
3305 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3306 if (msg_row < Rows - 1)
3307 showmode();
3308 }
3309}
3310
3311/*
3312 * Ask for a reply from the user, a 'y' or a 'n'.
3313 * No other characters are accepted, the message is repeated until a valid
3314 * reply is entered or CTRL-C is hit.
3315 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3316 * from any buffers but directly from the user.
3317 *
3318 * return the 'y' or 'n'
3319 */
3320 int
3321ask_yesno(str, direct)
3322 char_u *str;
3323 int direct;
3324{
3325 int r = ' ';
3326 int save_State = State;
3327
3328 if (exiting) /* put terminal in raw mode for this question */
3329 settmode(TMODE_RAW);
3330 ++no_wait_return;
3331#ifdef USE_ON_FLY_SCROLL
3332 dont_scroll = TRUE; /* disallow scrolling here */
3333#endif
3334 State = CONFIRM; /* mouse behaves like with :confirm */
3335#ifdef FEAT_MOUSE
3336 setmouse(); /* disables mouse for xterm */
3337#endif
3338 ++no_mapping;
3339 ++allow_keys; /* no mapping here, but recognize keys */
3340
3341 while (r != 'y' && r != 'n')
3342 {
3343 /* same highlighting as for wait_return */
3344 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3345 if (direct)
3346 r = get_keystroke();
3347 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003348 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (r == Ctrl_C || r == ESC)
3350 r = 'n';
3351 msg_putchar(r); /* show what you typed */
3352 out_flush();
3353 }
3354 --no_wait_return;
3355 State = save_State;
3356#ifdef FEAT_MOUSE
3357 setmouse();
3358#endif
3359 --no_mapping;
3360 --allow_keys;
3361
3362 return r;
3363}
3364
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003365#if defined(FEAT_MOUSE) || defined(PROTO)
3366/*
3367 * Return TRUE if "c" is a mouse key.
3368 */
3369 int
3370is_mouse_key(c)
3371 int c;
3372{
3373 return c == K_LEFTMOUSE
3374 || c == K_LEFTMOUSE_NM
3375 || c == K_LEFTDRAG
3376 || c == K_LEFTRELEASE
3377 || c == K_LEFTRELEASE_NM
3378 || c == K_MIDDLEMOUSE
3379 || c == K_MIDDLEDRAG
3380 || c == K_MIDDLERELEASE
3381 || c == K_RIGHTMOUSE
3382 || c == K_RIGHTDRAG
3383 || c == K_RIGHTRELEASE
3384 || c == K_MOUSEDOWN
3385 || c == K_MOUSEUP
3386 || c == K_MOUSELEFT
3387 || c == K_MOUSERIGHT
3388 || c == K_X1MOUSE
3389 || c == K_X1DRAG
3390 || c == K_X1RELEASE
3391 || c == K_X2MOUSE
3392 || c == K_X2DRAG
3393 || c == K_X2RELEASE;
3394}
3395#endif
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397/*
3398 * Get a key stroke directly from the user.
3399 * Ignores mouse clicks and scrollbar events, except a click for the left
3400 * button (used at the more prompt).
3401 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3402 * Disadvantage: typeahead is ignored.
3403 * Translates the interrupt character for unix to ESC.
3404 */
3405 int
3406get_keystroke()
3407{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003408 char_u *buf = NULL;
3409 int buflen = 150;
3410 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 int len = 0;
3412 int n;
3413 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003414 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 mapped_ctrl_c = FALSE; /* mappings are not used here */
3417 for (;;)
3418 {
3419 cursor_on();
3420 out_flush();
3421
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003422 /* Leave some room for check_termcode() to insert a key code into (max
3423 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3424 * bytes. */
3425 maxlen = (buflen - 6 - len) / 3;
3426 if (buf == NULL)
3427 buf = alloc(buflen);
3428 else if (maxlen < 10)
3429 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003430 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003431 * escape sequence. */
3432 buflen += 100;
3433 buf = vim_realloc(buf, buflen);
3434 maxlen = (buflen - 6 - len) / 3;
3435 }
3436 if (buf == NULL)
3437 {
3438 do_outofmem_msg((long_u)buflen);
3439 return ESC; /* panic! */
3440 }
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003443 * terminal code to complete. */
3444 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (n > 0)
3446 {
3447 /* Replace zero and CSI by a special key code. */
3448 n = fix_input_buffer(buf + len, n, FALSE);
3449 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003450 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003452 else if (len > 0)
3453 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
Bram Moolenaar4395a712006-09-05 18:57:57 +00003455 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003456 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003457 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003459
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003460 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003461 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003462 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003463 {
3464 /* Redrawing was postponed, do it now. */
3465 update_screen(0);
3466 setcursor(); /* put cursor back where it belongs */
3467 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003468 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003469 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003470 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003472 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 continue;
3474
3475 /* Handle modifier and/or special key code. */
3476 n = buf[0];
3477 if (n == K_SPECIAL)
3478 {
3479 n = TO_SPECIAL(buf[1], buf[2]);
3480 if (buf[1] == KS_MODIFIER
3481 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003482#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003483 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003484#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003485#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 || n == K_VER_SCROLLBAR
3487 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#endif
3489 )
3490 {
3491 if (buf[1] == KS_MODIFIER)
3492 mod_mask = buf[2];
3493 len -= 3;
3494 if (len > 0)
3495 mch_memmove(buf, buf + 3, (size_t)len);
3496 continue;
3497 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
3500#ifdef FEAT_MBYTE
3501 if (has_mbyte)
3502 {
3503 if (MB_BYTE2LEN(n) > len)
3504 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003505 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 n = (*mb_ptr2char)(buf);
3507 }
3508#endif
3509#ifdef UNIX
3510 if (n == intr_char)
3511 n = ESC;
3512#endif
3513 break;
3514 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003515 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
3517 mapped_ctrl_c = save_mapped_ctrl_c;
3518 return n;
3519}
3520
3521/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003522 * Get a number from the user.
3523 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
3525 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003526get_number(colon, mouse_used)
3527 int colon; /* allow colon to abort */
3528 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
3530 int n = 0;
3531 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003532 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003534 if (mouse_used != NULL)
3535 *mouse_used = FALSE;
3536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 /* When not printing messages, the user won't know what to type, return a
3538 * zero (as if CR was hit). */
3539 if (msg_silent != 0)
3540 return 0;
3541
3542#ifdef USE_ON_FLY_SCROLL
3543 dont_scroll = TRUE; /* disallow scrolling here */
3544#endif
3545 ++no_mapping;
3546 ++allow_keys; /* no mapping here, but recognize keys */
3547 for (;;)
3548 {
3549 windgoto(msg_row, msg_col);
3550 c = safe_vgetc();
3551 if (VIM_ISDIGIT(c))
3552 {
3553 n = n * 10 + c - '0';
3554 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003555 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3558 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003559 if (typed > 0)
3560 {
3561 MSG_PUTS("\b \b");
3562 --typed;
3563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003566#ifdef FEAT_MOUSE
3567 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3568 {
3569 *mouse_used = TRUE;
3570 n = mouse_row + 1;
3571 break;
3572 }
3573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 else if (n == 0 && c == ':' && colon)
3575 {
3576 stuffcharReadbuff(':');
3577 if (!exmode_active)
3578 cmdline_row = msg_row;
3579 skip_redraw = TRUE; /* skip redraw once */
3580 do_redraw = FALSE;
3581 break;
3582 }
3583 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3584 break;
3585 }
3586 --no_mapping;
3587 --allow_keys;
3588 return n;
3589}
3590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003591/*
3592 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003593 * When "mouse_used" is not NULL allow using the mouse and in that case return
3594 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003595 */
3596 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003597prompt_for_number(mouse_used)
3598 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003599{
3600 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003601 int save_cmdline_row;
3602 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603
3604 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003605 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003606 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003607 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003608 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003609
Bram Moolenaar203335e2006-09-03 14:35:42 +00003610 /* Set the state such that text can be selected/copied/pasted and we still
3611 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003612 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003613 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003614 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003615 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003616
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003617 i = get_number(TRUE, mouse_used);
3618 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003619 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003620 /* don't call wait_return() now */
3621 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003622 cmdline_row = msg_row - 1;
3623 need_wait_return = FALSE;
3624 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003625 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003626 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003627 else
3628 cmdline_row = save_cmdline_row;
3629 State = save_State;
3630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003631 return i;
3632}
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 void
3635msgmore(n)
3636 long n;
3637{
3638 long pn;
3639
3640 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3642 return;
3643
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003644 /* We don't want to overwrite another important message, but do overwrite
3645 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3646 * then "put" reports the last action. */
3647 if (keep_msg != NULL && !keep_msg_more)
3648 return;
3649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (n > 0)
3651 pn = n;
3652 else
3653 pn = -n;
3654
3655 if (pn > p_report)
3656 {
3657 if (pn == 1)
3658 {
3659 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003660 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3661 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003663 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3664 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666 else
3667 {
3668 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003669 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3670 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003672 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3673 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003676 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (msg(msg_buf))
3678 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003679 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003680 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682 }
3683}
3684
3685/*
3686 * flush map and typeahead buffers and give a warning for an error
3687 */
3688 void
3689beep_flush()
3690{
3691 if (emsg_silent == 0)
3692 {
3693 flush_buffers(FALSE);
3694 vim_beep();
3695 }
3696}
3697
3698/*
3699 * give a warning for an error
3700 */
3701 void
3702vim_beep()
3703{
3704 if (emsg_silent == 0)
3705 {
3706 if (p_vb
3707#ifdef FEAT_GUI
3708 /* While the GUI is starting up the termcap is set for the GUI
3709 * but the output still goes to a terminal. */
3710 && !(gui.in_use && gui.starting)
3711#endif
3712 )
3713 {
3714 out_str(T_VB);
3715 }
3716 else
3717 {
3718#ifdef MSDOS
3719 /*
3720 * The number of beeps outputted is reduced to avoid having to wait
3721 * for all the beeps to finish. This is only a problem on systems
3722 * where the beeps don't overlap.
3723 */
3724 if (beep_count == 0 || beep_count == 10)
3725 {
3726 out_char(BELL);
3727 beep_count = 1;
3728 }
3729 else
3730 ++beep_count;
3731#else
3732 out_char(BELL);
3733#endif
3734 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003735
3736 /* When 'verbose' is set and we are sourcing a script or executing a
3737 * function give the user a hint where the beep comes from. */
3738 if (vim_strchr(p_debug, 'e') != NULL)
3739 {
3740 msg_source(hl_attr(HLF_W));
3741 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744}
3745
3746/*
3747 * To get the "real" home directory:
3748 * - get value of $HOME
3749 * For Unix:
3750 * - go to that directory
3751 * - do mch_dirname() to get the real name of that directory.
3752 * This also works with mounts and links.
3753 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3754 */
3755static char_u *homedir = NULL;
3756
3757 void
3758init_homedir()
3759{
3760 char_u *var;
3761
Bram Moolenaar05159a02005-02-26 23:04:13 +00003762 /* In case we are called a second time (when 'encoding' changes). */
3763 vim_free(homedir);
3764 homedir = NULL;
3765
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766#ifdef VMS
3767 var = mch_getenv((char_u *)"SYS$LOGIN");
3768#else
3769 var = mch_getenv((char_u *)"HOME");
3770#endif
3771
3772 if (var != NULL && *var == NUL) /* empty is same as not set */
3773 var = NULL;
3774
3775#ifdef WIN3264
3776 /*
3777 * Weird but true: $HOME may contain an indirect reference to another
3778 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3779 * when $HOME is being set.
3780 */
3781 if (var != NULL && *var == '%')
3782 {
3783 char_u *p;
3784 char_u *exp;
3785
3786 p = vim_strchr(var + 1, '%');
3787 if (p != NULL)
3788 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003789 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 exp = mch_getenv(NameBuff);
3791 if (exp != NULL && *exp != NUL
3792 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3793 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003794 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 var = NameBuff;
3796 /* Also set $HOME, it's needed for _viminfo. */
3797 vim_setenv((char_u *)"HOME", NameBuff);
3798 }
3799 }
3800 }
3801
3802 /*
3803 * Typically, $HOME is not defined on Windows, unless the user has
3804 * specifically defined it for Vim's sake. However, on Windows NT
3805 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3806 * each user. Try constructing $HOME from these.
3807 */
3808 if (var == NULL)
3809 {
3810 char_u *homedrive, *homepath;
3811
3812 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3813 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003814 if (homepath == NULL || *homepath == NUL)
3815 homepath = "\\";
3816 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3818 {
3819 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3820 if (NameBuff[0] != NUL)
3821 {
3822 var = NameBuff;
3823 /* Also set $HOME, it's needed for _viminfo. */
3824 vim_setenv((char_u *)"HOME", NameBuff);
3825 }
3826 }
3827 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003828
3829# if defined(FEAT_MBYTE)
3830 if (enc_utf8 && var != NULL)
3831 {
3832 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003833 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003834
3835 /* Convert from active codepage to UTF-8. Other conversions are
3836 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003837 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003838 if (pp != NULL)
3839 {
3840 homedir = pp;
3841 return;
3842 }
3843 }
3844# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845#endif
3846
3847#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3848 /*
3849 * Default home dir is C:/
3850 * Best assumption we can make in such a situation.
3851 */
3852 if (var == NULL)
3853 var = "C:/";
3854#endif
3855 if (var != NULL)
3856 {
3857#ifdef UNIX
3858 /*
3859 * Change to the directory and get the actual path. This resolves
3860 * links. Don't do it when we can't return.
3861 */
3862 if (mch_dirname(NameBuff, MAXPATHL) == OK
3863 && mch_chdir((char *)NameBuff) == 0)
3864 {
3865 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3866 var = IObuff;
3867 if (mch_chdir((char *)NameBuff) != 0)
3868 EMSG(_(e_prev_dir));
3869 }
3870#endif
3871 homedir = vim_strsave(var);
3872 }
3873}
3874
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003875#if defined(EXITFREE) || defined(PROTO)
3876 void
3877free_homedir()
3878{
3879 vim_free(homedir);
3880}
Bram Moolenaar24305862012-08-15 14:05:05 +02003881
3882# ifdef FEAT_CMDL_COMPL
3883 void
3884free_users()
3885{
3886 ga_clear_strings(&ga_users);
3887}
3888# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003889#endif
3890
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003892 * Call expand_env() and store the result in an allocated string.
3893 * This is not very memory efficient, this expects the result to be freed
3894 * again soon.
3895 */
3896 char_u *
3897expand_env_save(src)
3898 char_u *src;
3899{
3900 return expand_env_save_opt(src, FALSE);
3901}
3902
3903/*
3904 * Idem, but when "one" is TRUE handle the string as one file name, only
3905 * expand "~" at the start.
3906 */
3907 char_u *
3908expand_env_save_opt(src, one)
3909 char_u *src;
3910 int one;
3911{
3912 char_u *p;
3913
3914 p = alloc(MAXPATHL);
3915 if (p != NULL)
3916 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3917 return p;
3918}
3919
3920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 * Expand environment variable with path name.
3922 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003923 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 * If anything fails no expansion is done and dst equals src.
3925 */
3926 void
3927expand_env(src, dst, dstlen)
3928 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3929 char_u *dst; /* where to put the result */
3930 int dstlen; /* maximum length of the result */
3931{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003932 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933}
3934
3935 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003936expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003937 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 char_u *dst; /* where to put the result */
3939 int dstlen; /* maximum length of the result */
3940 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003941 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003942 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003944 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 char_u *tail;
3946 int c;
3947 char_u *var;
3948 int copy_char;
3949 int mustfree; /* var was allocated, need to free it later */
3950 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003951 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003953 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003954 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003955
3956 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 --dstlen; /* leave one char space for "\," */
3958 while (*src && dstlen > 0)
3959 {
3960 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003961 if ((*src == '$'
3962#ifdef VMS
3963 && at_start
3964#endif
3965 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3967 || *src == '%'
3968#endif
3969 || (*src == '~' && at_start))
3970 {
3971 mustfree = FALSE;
3972
3973 /*
3974 * The variable name is copied into dst temporarily, because it may
3975 * be a string in read-only memory and a NUL needs to be appended.
3976 */
3977 if (*src != '~') /* environment var */
3978 {
3979 tail = src + 1;
3980 var = dst;
3981 c = dstlen - 1;
3982
3983#ifdef UNIX
3984 /* Unix has ${var-name} type environment vars */
3985 if (*tail == '{' && !vim_isIDc('{'))
3986 {
3987 tail++; /* ignore '{' */
3988 while (c-- > 0 && *tail && *tail != '}')
3989 *var++ = *tail++;
3990 }
3991 else
3992#endif
3993 {
3994 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3995#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3996 || (*src == '%' && *tail != '%')
3997#endif
3998 ))
3999 {
4000#ifdef OS2 /* env vars only in uppercase */
4001 *var++ = TOUPPER_LOC(*tail);
4002 tail++; /* toupper() may be a macro! */
4003#else
4004 *var++ = *tail++;
4005#endif
4006 }
4007 }
4008
4009#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
4010# ifdef UNIX
4011 if (src[1] == '{' && *tail != '}')
4012# else
4013 if (*src == '%' && *tail != '%')
4014# endif
4015 var = NULL;
4016 else
4017 {
4018# ifdef UNIX
4019 if (src[1] == '{')
4020# else
4021 if (*src == '%')
4022#endif
4023 ++tail;
4024#endif
4025 *var = NUL;
4026 var = vim_getenv(dst, &mustfree);
4027#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
4028 }
4029#endif
4030 }
4031 /* home directory */
4032 else if ( src[1] == NUL
4033 || vim_ispathsep(src[1])
4034 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4035 {
4036 var = homedir;
4037 tail = src + 1;
4038 }
4039 else /* user directory */
4040 {
4041#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4042 /*
4043 * Copy ~user to dst[], so we can put a NUL after it.
4044 */
4045 tail = src;
4046 var = dst;
4047 c = dstlen - 1;
4048 while ( c-- > 0
4049 && *tail
4050 && vim_isfilec(*tail)
4051 && !vim_ispathsep(*tail))
4052 *var++ = *tail++;
4053 *var = NUL;
4054# ifdef UNIX
4055 /*
4056 * If the system supports getpwnam(), use it.
4057 * Otherwise, or if getpwnam() fails, the shell is used to
4058 * expand ~user. This is slower and may fail if the shell
4059 * does not support ~user (old versions of /bin/sh).
4060 */
4061# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4062 {
4063 struct passwd *pw;
4064
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004065 /* Note: memory allocated by getpwnam() is never freed.
4066 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 pw = getpwnam((char *)dst + 1);
4068 if (pw != NULL)
4069 var = (char_u *)pw->pw_dir;
4070 else
4071 var = NULL;
4072 }
4073 if (var == NULL)
4074# endif
4075 {
4076 expand_T xpc;
4077
4078 ExpandInit(&xpc);
4079 xpc.xp_context = EXPAND_FILES;
4080 var = ExpandOne(&xpc, dst, NULL,
4081 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 mustfree = TRUE;
4083 }
4084
4085# else /* !UNIX, thus VMS */
4086 /*
4087 * USER_HOME is a comma-separated list of
4088 * directories to search for the user account in.
4089 */
4090 {
4091 char_u test[MAXPATHL], paths[MAXPATHL];
4092 char_u *path, *next_path, *ptr;
4093 struct stat st;
4094
4095 STRCPY(paths, USER_HOME);
4096 next_path = paths;
4097 while (*next_path)
4098 {
4099 for (path = next_path; *next_path && *next_path != ',';
4100 next_path++);
4101 if (*next_path)
4102 *next_path++ = NUL;
4103 STRCPY(test, path);
4104 STRCAT(test, "/");
4105 STRCAT(test, dst + 1);
4106 if (mch_stat(test, &st) == 0)
4107 {
4108 var = alloc(STRLEN(test) + 1);
4109 STRCPY(var, test);
4110 mustfree = TRUE;
4111 break;
4112 }
4113 }
4114 }
4115# endif /* UNIX */
4116#else
4117 /* cannot expand user's home directory, so don't try */
4118 var = NULL;
4119 tail = (char_u *)""; /* for gcc */
4120#endif /* UNIX || VMS */
4121 }
4122
4123#ifdef BACKSLASH_IN_FILENAME
4124 /* If 'shellslash' is set change backslashes to forward slashes.
4125 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4126 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4127 {
4128 char_u *p = vim_strsave(var);
4129
4130 if (p != NULL)
4131 {
4132 if (mustfree)
4133 vim_free(var);
4134 var = p;
4135 mustfree = TRUE;
4136 forward_slash(var);
4137 }
4138 }
4139#endif
4140
4141 /* If "var" contains white space, escape it with a backslash.
4142 * Required for ":e ~/tt" when $HOME includes a space. */
4143 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4144 {
4145 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4146
4147 if (p != NULL)
4148 {
4149 if (mustfree)
4150 vim_free(var);
4151 var = p;
4152 mustfree = TRUE;
4153 }
4154 }
4155
4156 if (var != NULL && *var != NUL
4157 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4158 {
4159 STRCPY(dst, var);
4160 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004161 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 /* if var[] ends in a path separator and tail[] starts
4163 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004164 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4166 && dst[-1] != ':'
4167#endif
4168 && vim_ispathsep(*tail))
4169 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004170 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 src = tail;
4172 copy_char = FALSE;
4173 }
4174 if (mustfree)
4175 vim_free(var);
4176 }
4177
4178 if (copy_char) /* copy at least one char */
4179 {
4180 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004181 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004182 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4183 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 */
4185 at_start = FALSE;
4186 if (src[0] == '\\' && src[1] != NUL)
4187 {
4188 *dst++ = *src++;
4189 --dstlen;
4190 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004191 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 at_start = TRUE;
4193 *dst++ = *src++;
4194 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004195
4196 if (startstr != NULL && src - startstr_len >= srcp
4197 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4198 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 }
4201 *dst = NUL;
4202}
4203
4204/*
4205 * Vim's version of getenv().
4206 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004207 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004208 * "mustfree" is set to TRUE when returned is allocated, it must be
4209 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 */
4211 char_u *
4212vim_getenv(name, mustfree)
4213 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004214 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
4216 char_u *p;
4217 char_u *pend;
4218 int vimruntime;
4219
4220#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4221 /* use "C:/" when $HOME is not set */
4222 if (STRCMP(name, "HOME") == 0)
4223 return homedir;
4224#endif
4225
4226 p = mch_getenv(name);
4227 if (p != NULL && *p == NUL) /* empty is the same as not set */
4228 p = NULL;
4229
4230 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004231 {
4232#if defined(FEAT_MBYTE) && defined(WIN3264)
4233 if (enc_utf8)
4234 {
4235 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004236 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004237
4238 /* Convert from active codepage to UTF-8. Other conversions are
4239 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004240 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004241 if (pp != NULL)
4242 {
4243 p = pp;
4244 *mustfree = TRUE;
4245 }
4246 }
4247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4252 if (!vimruntime && STRCMP(name, "VIM") != 0)
4253 return NULL;
4254
4255 /*
4256 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4257 * Don't do this when default_vimruntime_dir is non-empty.
4258 */
4259 if (vimruntime
4260#ifdef HAVE_PATHDEF
4261 && *default_vimruntime_dir == NUL
4262#endif
4263 )
4264 {
4265 p = mch_getenv((char_u *)"VIM");
4266 if (p != NULL && *p == NUL) /* empty is the same as not set */
4267 p = NULL;
4268 if (p != NULL)
4269 {
4270 p = vim_version_dir(p);
4271 if (p != NULL)
4272 *mustfree = TRUE;
4273 else
4274 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004275
4276#if defined(FEAT_MBYTE) && defined(WIN3264)
4277 if (enc_utf8)
4278 {
4279 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004280 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004281
4282 /* Convert from active codepage to UTF-8. Other conversions
4283 * are not done, because they would fail for non-ASCII
4284 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004285 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004286 if (pp != NULL)
4287 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004288 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004289 vim_free(p);
4290 p = pp;
4291 *mustfree = TRUE;
4292 }
4293 }
4294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 }
4297
4298 /*
4299 * When expanding $VIM or $VIMRUNTIME fails, try using:
4300 * - the directory name from 'helpfile' (unless it contains '$')
4301 * - the executable name from argv[0]
4302 */
4303 if (p == NULL)
4304 {
4305 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4306 p = p_hf;
4307#ifdef USE_EXE_NAME
4308 /*
4309 * Use the name of the executable, obtained from argv[0].
4310 */
4311 else
4312 p = exe_name;
4313#endif
4314 if (p != NULL)
4315 {
4316 /* remove the file name */
4317 pend = gettail(p);
4318
4319 /* remove "doc/" from 'helpfile', if present */
4320 if (p == p_hf)
4321 pend = remove_tail(p, pend, (char_u *)"doc");
4322
4323#ifdef USE_EXE_NAME
4324# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004325 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (p == exe_name)
4327 {
4328 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004329 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004331 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4332 if (pend1 != pend)
4333 {
4334 pnew = alloc((unsigned)(pend1 - p) + 15);
4335 if (pnew != NULL)
4336 {
4337 STRNCPY(pnew, p, (pend1 - p));
4338 STRCPY(pnew + (pend1 - p), "Resources/vim");
4339 p = pnew;
4340 pend = p + STRLEN(p);
4341 }
4342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344# endif
4345 /* remove "src/" from exe_name, if present */
4346 if (p == exe_name)
4347 pend = remove_tail(p, pend, (char_u *)"src");
4348#endif
4349
4350 /* for $VIM, remove "runtime/" or "vim54/", if present */
4351 if (!vimruntime)
4352 {
4353 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4354 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4355 }
4356
4357 /* remove trailing path separator */
4358#ifndef MACOS_CLASSIC
4359 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004360 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004361 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 --pend;
4363#endif
4364
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004365#ifdef MACOS_X
4366 if (p == exe_name || p == p_hf)
4367#endif
4368 /* check that the result is a directory name */
4369 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371 if (p != NULL && !mch_isdir(p))
4372 {
4373 vim_free(p);
4374 p = NULL;
4375 }
4376 else
4377 {
4378#ifdef USE_EXE_NAME
4379 /* may add "/vim54" or "/runtime" if it exists */
4380 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4381 {
4382 vim_free(p);
4383 p = pend;
4384 }
4385#endif
4386 *mustfree = TRUE;
4387 }
4388 }
4389 }
4390
4391#ifdef HAVE_PATHDEF
4392 /* When there is a pathdef.c file we can use default_vim_dir and
4393 * default_vimruntime_dir */
4394 if (p == NULL)
4395 {
4396 /* Only use default_vimruntime_dir when it is not empty */
4397 if (vimruntime && *default_vimruntime_dir != NUL)
4398 {
4399 p = default_vimruntime_dir;
4400 *mustfree = FALSE;
4401 }
4402 else if (*default_vim_dir != NUL)
4403 {
4404 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4405 *mustfree = TRUE;
4406 else
4407 {
4408 p = default_vim_dir;
4409 *mustfree = FALSE;
4410 }
4411 }
4412 }
4413#endif
4414
4415 /*
4416 * Set the environment variable, so that the new value can be found fast
4417 * next time, and others can also use it (e.g. Perl).
4418 */
4419 if (p != NULL)
4420 {
4421 if (vimruntime)
4422 {
4423 vim_setenv((char_u *)"VIMRUNTIME", p);
4424 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 else
4427 {
4428 vim_setenv((char_u *)"VIM", p);
4429 didset_vim = TRUE;
4430 }
4431 }
4432 return p;
4433}
4434
4435/*
4436 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4437 * Return NULL if not, return its name in allocated memory otherwise.
4438 */
4439 static char_u *
4440vim_version_dir(vimdir)
4441 char_u *vimdir;
4442{
4443 char_u *p;
4444
4445 if (vimdir == NULL || *vimdir == NUL)
4446 return NULL;
4447 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4448 if (p != NULL && mch_isdir(p))
4449 return p;
4450 vim_free(p);
4451 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4452 if (p != NULL && mch_isdir(p))
4453 return p;
4454 vim_free(p);
4455 return NULL;
4456}
4457
4458/*
4459 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4460 * the length of "name/". Otherwise return "pend".
4461 */
4462 static char_u *
4463remove_tail(p, pend, name)
4464 char_u *p;
4465 char_u *pend;
4466 char_u *name;
4467{
4468 int len = (int)STRLEN(name) + 1;
4469 char_u *newend = pend - len;
4470
4471 if (newend >= p
4472 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004473 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 return newend;
4475 return pend;
4476}
4477
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 * Our portable version of setenv.
4480 */
4481 void
4482vim_setenv(name, val)
4483 char_u *name;
4484 char_u *val;
4485{
4486#ifdef HAVE_SETENV
4487 mch_setenv((char *)name, (char *)val, 1);
4488#else
4489 char_u *envbuf;
4490
4491 /*
4492 * Putenv does not copy the string, it has to remain
4493 * valid. The allocated memory will never be freed.
4494 */
4495 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4496 if (envbuf != NULL)
4497 {
4498 sprintf((char *)envbuf, "%s=%s", name, val);
4499 putenv((char *)envbuf);
4500 }
4501#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004502#ifdef FEAT_GETTEXT
4503 /*
4504 * When setting $VIMRUNTIME adjust the directory to find message
4505 * translations to $VIMRUNTIME/lang.
4506 */
4507 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4508 {
4509 char_u *buf = concat_str(val, (char_u *)"/lang");
4510
4511 if (buf != NULL)
4512 {
4513 bindtextdomain(VIMPACKAGE, (char *)buf);
4514 vim_free(buf);
4515 }
4516 }
4517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518}
4519
4520#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4521/*
4522 * Function given to ExpandGeneric() to obtain an environment variable name.
4523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 char_u *
4525get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004526 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 int idx;
4528{
4529# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4530 /*
4531 * No environ[] on the Amiga and on the Mac (using MPW).
4532 */
4533 return NULL;
4534# else
4535# ifndef __WIN32__
4536 /* Borland C++ 5.2 has this in a header file. */
4537 extern char **environ;
4538# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004539# define ENVNAMELEN 100
4540 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 char_u *str;
4542 int n;
4543
4544 str = (char_u *)environ[idx];
4545 if (str == NULL)
4546 return NULL;
4547
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004548 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 {
4550 if (str[n] == '=' || str[n] == NUL)
4551 break;
4552 name[n] = str[n];
4553 }
4554 name[n] = NUL;
4555 return name;
4556# endif
4557}
Bram Moolenaar24305862012-08-15 14:05:05 +02004558
4559/*
4560 * Find all user names for user completion.
4561 * Done only once and then cached.
4562 */
4563 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004564init_users()
4565{
Bram Moolenaar24305862012-08-15 14:05:05 +02004566 static int lazy_init_done = FALSE;
4567
4568 if (lazy_init_done)
4569 return;
4570
4571 lazy_init_done = TRUE;
4572 ga_init2(&ga_users, sizeof(char_u *), 20);
4573
4574# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4575 {
4576 char_u* user;
4577 struct passwd* pw;
4578
4579 setpwent();
4580 while ((pw = getpwent()) != NULL)
4581 /* pw->pw_name shouldn't be NULL but just in case... */
4582 if (pw->pw_name != NULL)
4583 {
4584 if (ga_grow(&ga_users, 1) == FAIL)
4585 break;
4586 user = vim_strsave((char_u*)pw->pw_name);
4587 if (user == NULL)
4588 break;
4589 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4590 }
4591 endpwent();
4592 }
4593# endif
4594}
4595
4596/*
4597 * Function given to ExpandGeneric() to obtain an user names.
4598 */
4599 char_u*
4600get_users(xp, idx)
4601 expand_T *xp UNUSED;
4602 int idx;
4603{
4604 init_users();
4605 if (idx < ga_users.ga_len)
4606 return ((char_u **)ga_users.ga_data)[idx];
4607 return NULL;
4608}
4609
4610/*
4611 * Check whether name matches a user name. Return:
4612 * 0 if name does not match any user name.
4613 * 1 if name partially matches the beginning of a user name.
4614 * 2 is name fully matches a user name.
4615 */
4616int match_user(name)
4617 char_u* name;
4618{
4619 int i;
4620 int n = (int)STRLEN(name);
4621 int result = 0;
4622
4623 init_users();
4624 for (i = 0; i < ga_users.ga_len; i++)
4625 {
4626 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4627 return 2; /* full match */
4628 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4629 result = 1; /* partial match */
4630 }
4631 return result;
4632}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
4634
4635/*
4636 * Replace home directory by "~" in each space or comma separated file name in
4637 * 'src'.
4638 * If anything fails (except when out of space) dst equals src.
4639 */
4640 void
4641home_replace(buf, src, dst, dstlen, one)
4642 buf_T *buf; /* when not NULL, check for help files */
4643 char_u *src; /* input file name */
4644 char_u *dst; /* where to put the result */
4645 int dstlen; /* maximum length of the result */
4646 int one; /* if TRUE, only replace one file name, include
4647 spaces and commas in the file name. */
4648{
4649 size_t dirlen = 0, envlen = 0;
4650 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004651 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 char_u *p;
4653
4654 if (src == NULL)
4655 {
4656 *dst = NUL;
4657 return;
4658 }
4659
4660 /*
4661 * If the file is a help file, remove the path completely.
4662 */
4663 if (buf != NULL && buf->b_help)
4664 {
4665 STRCPY(dst, gettail(src));
4666 return;
4667 }
4668
4669 /*
4670 * We check both the value of the $HOME environment variable and the
4671 * "real" home directory.
4672 */
4673 if (homedir != NULL)
4674 dirlen = STRLEN(homedir);
4675
4676#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004677 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004679 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4680#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004681 /* Empty is the same as not set. */
4682 if (homedir_env != NULL && *homedir_env == NUL)
4683 homedir_env = NULL;
4684
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004685#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004686 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004687 {
4688 int usedlen = 0;
4689 int flen;
4690 char_u *fbuf = NULL;
4691
4692 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004693 (void)modify_fname((char_u *)":p", &usedlen,
4694 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004695 flen = (int)STRLEN(homedir_env);
4696 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4697 /* Remove the trailing / that is added to a directory. */
4698 homedir_env[flen - 1] = NUL;
4699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#endif
4701
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 if (homedir_env != NULL)
4703 envlen = STRLEN(homedir_env);
4704
4705 if (!one)
4706 src = skipwhite(src);
4707 while (*src && dstlen > 0)
4708 {
4709 /*
4710 * Here we are at the beginning of a file name.
4711 * First, check to see if the beginning of the file name matches
4712 * $HOME or the "real" home directory. Check that there is a '/'
4713 * after the match (so that if e.g. the file is "/home/pieter/bla",
4714 * and the home directory is "/home/piet", the file does not end up
4715 * as "~er/bla" (which would seem to indicate the file "bla" in user
4716 * er's home directory)).
4717 */
4718 p = homedir;
4719 len = dirlen;
4720 for (;;)
4721 {
4722 if ( len
4723 && fnamencmp(src, p, len) == 0
4724 && (vim_ispathsep(src[len])
4725 || (!one && (src[len] == ',' || src[len] == ' '))
4726 || src[len] == NUL))
4727 {
4728 src += len;
4729 if (--dstlen > 0)
4730 *dst++ = '~';
4731
4732 /*
4733 * If it's just the home directory, add "/".
4734 */
4735 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4736 *dst++ = '/';
4737 break;
4738 }
4739 if (p == homedir_env)
4740 break;
4741 p = homedir_env;
4742 len = envlen;
4743 }
4744
4745 /* if (!one) skip to separator: space or comma */
4746 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4747 *dst++ = *src++;
4748 /* skip separator */
4749 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4750 *dst++ = *src++;
4751 }
4752 /* if (dstlen == 0) out of space, what to do??? */
4753
4754 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004755
4756 if (homedir_env != homedir_env_orig)
4757 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758}
4759
4760/*
4761 * Like home_replace, store the replaced string in allocated memory.
4762 * When something fails, NULL is returned.
4763 */
4764 char_u *
4765home_replace_save(buf, src)
4766 buf_T *buf; /* when not NULL, check for help files */
4767 char_u *src; /* input file name */
4768{
4769 char_u *dst;
4770 unsigned len;
4771
4772 len = 3; /* space for "~/" and trailing NUL */
4773 if (src != NULL) /* just in case */
4774 len += (unsigned)STRLEN(src);
4775 dst = alloc(len);
4776 if (dst != NULL)
4777 home_replace(buf, src, dst, len, TRUE);
4778 return dst;
4779}
4780
4781/*
4782 * Compare two file names and return:
4783 * FPC_SAME if they both exist and are the same file.
4784 * FPC_SAMEX if they both don't exist and have the same file name.
4785 * FPC_DIFF if they both exist and are different files.
4786 * FPC_NOTX if they both don't exist.
4787 * FPC_DIFFX if one of them doesn't exist.
4788 * For the first name environment variables are expanded
4789 */
4790 int
4791fullpathcmp(s1, s2, checkname)
4792 char_u *s1, *s2;
4793 int checkname; /* when both don't exist, check file names */
4794{
4795#ifdef UNIX
4796 char_u exp1[MAXPATHL];
4797 char_u full1[MAXPATHL];
4798 char_u full2[MAXPATHL];
4799 struct stat st1, st2;
4800 int r1, r2;
4801
4802 expand_env(s1, exp1, MAXPATHL);
4803 r1 = mch_stat((char *)exp1, &st1);
4804 r2 = mch_stat((char *)s2, &st2);
4805 if (r1 != 0 && r2 != 0)
4806 {
4807 /* if mch_stat() doesn't work, may compare the names */
4808 if (checkname)
4809 {
4810 if (fnamecmp(exp1, s2) == 0)
4811 return FPC_SAMEX;
4812 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4813 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4814 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4815 return FPC_SAMEX;
4816 }
4817 return FPC_NOTX;
4818 }
4819 if (r1 != 0 || r2 != 0)
4820 return FPC_DIFFX;
4821 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4822 return FPC_SAME;
4823 return FPC_DIFF;
4824#else
4825 char_u *exp1; /* expanded s1 */
4826 char_u *full1; /* full path of s1 */
4827 char_u *full2; /* full path of s2 */
4828 int retval = FPC_DIFF;
4829 int r1, r2;
4830
4831 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4832 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4833 {
4834 full1 = exp1 + MAXPATHL;
4835 full2 = full1 + MAXPATHL;
4836
4837 expand_env(s1, exp1, MAXPATHL);
4838 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4839 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4840
4841 /* If vim_FullName() fails, the file probably doesn't exist. */
4842 if (r1 != OK && r2 != OK)
4843 {
4844 if (checkname && fnamecmp(exp1, s2) == 0)
4845 retval = FPC_SAMEX;
4846 else
4847 retval = FPC_NOTX;
4848 }
4849 else if (r1 != OK || r2 != OK)
4850 retval = FPC_DIFFX;
4851 else if (fnamecmp(full1, full2))
4852 retval = FPC_DIFF;
4853 else
4854 retval = FPC_SAME;
4855 vim_free(exp1);
4856 }
4857 return retval;
4858#endif
4859}
4860
4861/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004862 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004863 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004864 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 */
4866 char_u *
4867gettail(fname)
4868 char_u *fname;
4869{
4870 char_u *p1, *p2;
4871
4872 if (fname == NULL)
4873 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004874 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004876 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004878 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880 return p1;
4881}
4882
Bram Moolenaar31710262010-08-13 13:36:15 +02004883#if defined(FEAT_SEARCHPATH)
4884static char_u *gettail_dir __ARGS((char_u *fname));
4885
4886/*
4887 * Return the end of the directory name, on the first path
4888 * separator:
4889 * "/path/file", "/path/dir/", "/path//dir", "/file"
4890 * ^ ^ ^ ^
4891 */
4892 static char_u *
4893gettail_dir(fname)
4894 char_u *fname;
4895{
4896 char_u *dir_end = fname;
4897 char_u *next_dir_end = fname;
4898 int look_for_sep = TRUE;
4899 char_u *p;
4900
4901 for (p = fname; *p != NUL; )
4902 {
4903 if (vim_ispathsep(*p))
4904 {
4905 if (look_for_sep)
4906 {
4907 next_dir_end = p;
4908 look_for_sep = FALSE;
4909 }
4910 }
4911 else
4912 {
4913 if (!look_for_sep)
4914 dir_end = next_dir_end;
4915 look_for_sep = TRUE;
4916 }
4917 mb_ptr_adv(p);
4918 }
4919 return dir_end;
4920}
4921#endif
4922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004924 * Get pointer to tail of "fname", including path separators. Putting a NUL
4925 * here leaves the directory name. Takes care of "c:/" and "//".
4926 * Always returns a valid pointer.
4927 */
4928 char_u *
4929gettail_sep(fname)
4930 char_u *fname;
4931{
4932 char_u *p;
4933 char_u *t;
4934
4935 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4936 t = gettail(fname);
4937 while (t > p && after_pathsep(fname, t))
4938 --t;
4939#ifdef VMS
4940 /* path separator is part of the path */
4941 ++t;
4942#endif
4943 return t;
4944}
4945
4946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * get the next path component (just after the next path separator).
4948 */
4949 char_u *
4950getnextcomp(fname)
4951 char_u *fname;
4952{
4953 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004954 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (*fname)
4956 ++fname;
4957 return fname;
4958}
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960/*
4961 * Get a pointer to one character past the head of a path name.
4962 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4963 * If there is no head, path is returned.
4964 */
4965 char_u *
4966get_past_head(path)
4967 char_u *path;
4968{
4969 char_u *retval;
4970
4971#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4972 /* may skip "c:" */
4973 if (isalpha(path[0]) && path[1] == ':')
4974 retval = path + 2;
4975 else
4976 retval = path;
4977#else
4978# if defined(AMIGA)
4979 /* may skip "label:" */
4980 retval = vim_strchr(path, ':');
4981 if (retval == NULL)
4982 retval = path;
4983# else /* Unix */
4984 retval = path;
4985# endif
4986#endif
4987
4988 while (vim_ispathsep(*retval))
4989 ++retval;
4990
4991 return retval;
4992}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
4994/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004995 * Return TRUE if 'c' is a path separator.
4996 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
4998 int
4999vim_ispathsep(c)
5000 int c;
5001{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005002#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005004#else
5005# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005007# else
5008# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5010 return (c == ':' || c == '[' || c == ']' || c == '/'
5011 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005012# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005014# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017}
5018
Bram Moolenaar69c35002013-11-04 02:54:12 +01005019/*
5020 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5021 */
5022 int
5023vim_ispathsep_nocolon(c)
5024 int c;
5025{
5026 return vim_ispathsep(c)
5027#ifdef BACKSLASH_IN_FILENAME
5028 && c != ':'
5029#endif
5030 ;
5031}
5032
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5034/*
5035 * return TRUE if 'c' is a path list separator.
5036 */
5037 int
5038vim_ispathlistsep(c)
5039 int c;
5040{
5041#ifdef UNIX
5042 return (c == ':');
5043#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005044 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045#endif
5046}
5047#endif
5048
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005049#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5050 || defined(FEAT_EVAL) || defined(PROTO)
5051/*
5052 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5053 * It's done in-place.
5054 */
5055 void
5056shorten_dir(str)
5057 char_u *str;
5058{
5059 char_u *tail, *s, *d;
5060 int skip = FALSE;
5061
5062 tail = gettail(str);
5063 d = str;
5064 for (s = str; ; ++s)
5065 {
5066 if (s >= tail) /* copy the whole tail */
5067 {
5068 *d++ = *s;
5069 if (*s == NUL)
5070 break;
5071 }
5072 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5073 {
5074 *d++ = *s;
5075 skip = FALSE;
5076 }
5077 else if (!skip)
5078 {
5079 *d++ = *s; /* copy next char */
5080 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5081 skip = TRUE;
5082# ifdef FEAT_MBYTE
5083 if (has_mbyte)
5084 {
5085 int l = mb_ptr2len(s);
5086
5087 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005088 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005089 }
5090# endif
5091 }
5092 }
5093}
5094#endif
5095
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005096/*
5097 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5098 * Also returns TRUE if there is no directory name.
5099 * "fname" must be writable!.
5100 */
5101 int
5102dir_of_file_exists(fname)
5103 char_u *fname;
5104{
5105 char_u *p;
5106 int c;
5107 int retval;
5108
5109 p = gettail_sep(fname);
5110 if (p == fname)
5111 return TRUE;
5112 c = *p;
5113 *p = NUL;
5114 retval = mch_isdir(fname);
5115 *p = c;
5116 return retval;
5117}
5118
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005120 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5121 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 */
5123 int
5124vim_fnamecmp(x, y)
5125 char_u *x, *y;
5126{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005127#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005129#else
5130 if (p_fic)
5131 return MB_STRICMP(x, y);
5132 return STRCMP(x, y);
5133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134}
5135
5136 int
5137vim_fnamencmp(x, y, len)
5138 char_u *x, *y;
5139 size_t len;
5140{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005141#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005142 char_u *px = x;
5143 char_u *py = y;
5144 int cx = NUL;
5145 int cy = NUL;
5146
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005147 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005149 cx = PTR2CHAR(px);
5150 cy = PTR2CHAR(py);
5151 if (cx == NUL || cy == NUL
5152 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5153 && !(cx == '/' && cy == '\\')
5154 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005156 len -= MB_PTR2LEN(px);
5157 px += MB_PTR2LEN(px);
5158 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160 if (len == 0)
5161 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005162 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005163#else
5164 if (p_fic)
5165 return MB_STRNICMP(x, y, len);
5166 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005168}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170/*
5171 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005172 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 */
5174 char_u *
5175concat_fnames(fname1, fname2, sep)
5176 char_u *fname1;
5177 char_u *fname2;
5178 int sep;
5179{
5180 char_u *dest;
5181
5182 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5183 if (dest != NULL)
5184 {
5185 STRCPY(dest, fname1);
5186 if (sep)
5187 add_pathsep(dest);
5188 STRCAT(dest, fname2);
5189 }
5190 return dest;
5191}
5192
Bram Moolenaard6754642005-01-17 22:18:45 +00005193/*
5194 * Concatenate two strings and return the result in allocated memory.
5195 * Returns NULL when out of memory.
5196 */
5197 char_u *
5198concat_str(str1, str2)
5199 char_u *str1;
5200 char_u *str2;
5201{
5202 char_u *dest;
5203 size_t l = STRLEN(str1);
5204
5205 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5206 if (dest != NULL)
5207 {
5208 STRCPY(dest, str1);
5209 STRCPY(dest + l, str2);
5210 }
5211 return dest;
5212}
Bram Moolenaard6754642005-01-17 22:18:45 +00005213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214/*
5215 * Add a path separator to a file name, unless it already ends in a path
5216 * separator.
5217 */
5218 void
5219add_pathsep(p)
5220 char_u *p;
5221{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005222 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 STRCAT(p, PATHSEPSTR);
5224}
5225
5226/*
5227 * FullName_save - Make an allocated copy of a full file name.
5228 * Returns NULL when out of memory.
5229 */
5230 char_u *
5231FullName_save(fname, force)
5232 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005233 int force; /* force expansion, even when it already looks
5234 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
5236 char_u *buf;
5237 char_u *new_fname = NULL;
5238
5239 if (fname == NULL)
5240 return NULL;
5241
5242 buf = alloc((unsigned)MAXPATHL);
5243 if (buf != NULL)
5244 {
5245 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5246 new_fname = vim_strsave(buf);
5247 else
5248 new_fname = vim_strsave(fname);
5249 vim_free(buf);
5250 }
5251 return new_fname;
5252}
5253
5254#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5255
5256static char_u *skip_string __ARGS((char_u *p));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005257static pos_T *ind_find_start_comment __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259/*
5260 * Find the start of a comment, not knowing if we are in a comment right now.
5261 * Search starts at w_cursor.lnum and goes backwards.
5262 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005263 static pos_T *
5264ind_find_start_comment() /* XXX */
5265{
5266 return find_start_comment(curbuf->b_ind_maxcomment);
5267}
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 pos_T *
5270find_start_comment(ind_maxcomment) /* XXX */
5271 int ind_maxcomment;
5272{
5273 pos_T *pos;
5274 char_u *line;
5275 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005276 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005278 for (;;)
5279 {
5280 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5281 if (pos == NULL)
5282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005284 /*
5285 * Check if the comment start we found is inside a string.
5286 * If it is then restrict the search to below this line and try again.
5287 */
5288 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005289 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005290 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005291 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005292 break;
5293 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5294 if (cur_maxcomment <= 0)
5295 {
5296 pos = NULL;
5297 break;
5298 }
5299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 return pos;
5301}
5302
5303/*
5304 * Skip to the end of a "string" and a 'c' character.
5305 * If there is no string or character, return argument unmodified.
5306 */
5307 static char_u *
5308skip_string(p)
5309 char_u *p;
5310{
5311 int i;
5312
5313 /*
5314 * We loop, because strings may be concatenated: "date""time".
5315 */
5316 for ( ; ; ++p)
5317 {
5318 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5319 {
5320 if (!p[1]) /* ' at end of line */
5321 break;
5322 i = 2;
5323 if (p[1] == '\\') /* '\n' or '\000' */
5324 {
5325 ++i;
5326 while (vim_isdigit(p[i - 1])) /* '\000' */
5327 ++i;
5328 }
5329 if (p[i] == '\'') /* check for trailing ' */
5330 {
5331 p += i;
5332 continue;
5333 }
5334 }
5335 else if (p[0] == '"') /* start of string */
5336 {
5337 for (++p; p[0]; ++p)
5338 {
5339 if (p[0] == '\\' && p[1] != NUL)
5340 ++p;
5341 else if (p[0] == '"') /* end of string */
5342 break;
5343 }
5344 if (p[0] == '"')
5345 continue;
5346 }
5347 break; /* no string found */
5348 }
5349 if (!*p)
5350 --p; /* backup from NUL */
5351 return p;
5352}
5353#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5354
5355#if defined(FEAT_CINDENT) || defined(PROTO)
5356
5357/*
5358 * Do C or expression indenting on the current line.
5359 */
5360 void
5361do_c_expr_indent()
5362{
5363# ifdef FEAT_EVAL
5364 if (*curbuf->b_p_inde != NUL)
5365 fixthisline(get_expr_indent);
5366 else
5367# endif
5368 fixthisline(get_c_indent);
5369}
5370
5371/*
5372 * Functions for C-indenting.
5373 * Most of this originally comes from Eric Fischer.
5374 */
5375/*
5376 * Below "XXX" means that this function may unlock the current line.
5377 */
5378
5379static char_u *cin_skipcomment __ARGS((char_u *));
5380static int cin_nocode __ARGS((char_u *));
5381static pos_T *find_line_comment __ARGS((void));
5382static int cin_islabel_skip __ARGS((char_u **));
5383static int cin_isdefault __ARGS((char_u *));
5384static char_u *after_label __ARGS((char_u *l));
5385static int get_indent_nolabel __ARGS((linenr_T lnum));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005386static int skip_label __ARGS((linenr_T, char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387static int cin_first_id_amount __ARGS((void));
5388static int cin_get_equal_amount __ARGS((linenr_T lnum));
5389static int cin_ispreproc __ARGS((char_u *));
5390static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5391static int cin_iscomment __ARGS((char_u *));
5392static int cin_islinecomment __ARGS((char_u *));
5393static int cin_isterminated __ARGS((char_u *, int, int));
5394static int cin_isinit __ARGS((void));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005395static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396static int cin_isif __ARGS((char_u *));
5397static int cin_iselse __ARGS((char_u *));
5398static int cin_isdo __ARGS((char_u *));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005399static int cin_iswhileofdo __ARGS((char_u *, linenr_T));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005400static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005401static int cin_iswhileofdo_end __ARGS((int terminated));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005403static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005404static int get_baseclass_amount __ARGS((int col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005406static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407static int cin_skip2pos __ARGS((pos_T *trypos));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005408static pos_T *find_start_brace __ARGS((void));
5409static pos_T *find_match_paren __ARGS((int));
5410static int corr_ind_maxparen __ARGS((pos_T *startpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411static int find_last_paren __ARGS((char_u *l, int start, int end));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005412static int find_match __ARGS((int lookfor, linenr_T ourscope));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005413static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415/*
5416 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005417 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 */
5419 static char_u *
5420cin_skipcomment(s)
5421 char_u *s;
5422{
5423 while (*s)
5424 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005425 char_u *prev_s = s;
5426
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005428
5429 /* Perl/shell # comment comment continues until eol. Require a space
5430 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005431 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005432 {
5433 s += STRLEN(s);
5434 break;
5435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 if (*s != '/')
5437 break;
5438 ++s;
5439 if (*s == '/') /* slash-slash comment continues till eol */
5440 {
5441 s += STRLEN(s);
5442 break;
5443 }
5444 if (*s != '*')
5445 break;
5446 for (++s; *s; ++s) /* skip slash-star comment */
5447 if (s[0] == '*' && s[1] == '/')
5448 {
5449 s += 2;
5450 break;
5451 }
5452 }
5453 return s;
5454}
5455
5456/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005457 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 * not considered code.
5459 */
5460 static int
5461cin_nocode(s)
5462 char_u *s;
5463{
5464 return *cin_skipcomment(s) == NUL;
5465}
5466
5467/*
5468 * Check previous lines for a "//" line comment, skipping over blank lines.
5469 */
5470 static pos_T *
5471find_line_comment() /* XXX */
5472{
5473 static pos_T pos;
5474 char_u *line;
5475 char_u *p;
5476
5477 pos = curwin->w_cursor;
5478 while (--pos.lnum > 0)
5479 {
5480 line = ml_get(pos.lnum);
5481 p = skipwhite(line);
5482 if (cin_islinecomment(p))
5483 {
5484 pos.col = (int)(p - line);
5485 return &pos;
5486 }
5487 if (*p != NUL)
5488 break;
5489 }
5490 return NULL;
5491}
5492
5493/*
5494 * Check if string matches "label:"; move to character after ':' if true.
5495 */
5496 static int
5497cin_islabel_skip(s)
5498 char_u **s;
5499{
5500 if (!vim_isIDc(**s)) /* need at least one ID character */
5501 return FALSE;
5502
5503 while (vim_isIDc(**s))
5504 (*s)++;
5505
5506 *s = cin_skipcomment(*s);
5507
5508 /* "::" is not a label, it's C++ */
5509 return (**s == ':' && *++*s != ':');
5510}
5511
5512/*
5513 * Recognize a label: "label:".
5514 * Note: curwin->w_cursor must be where we are looking for the label.
5515 */
5516 int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005517cin_islabel() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 char_u *s;
5520
5521 s = cin_skipcomment(ml_get_curline());
5522
5523 /*
5524 * Exclude "default" from labels, since it should be indented
5525 * like a switch label. Same for C++ scope declarations.
5526 */
5527 if (cin_isdefault(s))
5528 return FALSE;
5529 if (cin_isscopedecl(s))
5530 return FALSE;
5531
5532 if (cin_islabel_skip(&s))
5533 {
5534 /*
5535 * Only accept a label if the previous line is terminated or is a case
5536 * label.
5537 */
5538 pos_T cursor_save;
5539 pos_T *trypos;
5540 char_u *line;
5541
5542 cursor_save = curwin->w_cursor;
5543 while (curwin->w_cursor.lnum > 1)
5544 {
5545 --curwin->w_cursor.lnum;
5546
5547 /*
5548 * If we're in a comment now, skip to the start of the comment.
5549 */
5550 curwin->w_cursor.col = 0;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005551 if ((trypos = ind_find_start_comment()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 curwin->w_cursor = *trypos;
5553
5554 line = ml_get_curline();
5555 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5556 continue;
5557 if (*(line = cin_skipcomment(line)) == NUL)
5558 continue;
5559
5560 curwin->w_cursor = cursor_save;
5561 if (cin_isterminated(line, TRUE, FALSE)
5562 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005563 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 || (cin_islabel_skip(&line) && cin_nocode(line)))
5565 return TRUE;
5566 return FALSE;
5567 }
5568 curwin->w_cursor = cursor_save;
5569 return TRUE; /* label at start of file??? */
5570 }
5571 return FALSE;
5572}
5573
5574/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005575 * Recognize structure initialization and enumerations:
5576 * "[typedef] [static|public|protected|private] enum"
5577 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 */
5579 static int
5580cin_isinit(void)
5581{
5582 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005583 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
5585 s = cin_skipcomment(ml_get_curline());
5586
Bram Moolenaar75342212013-03-07 13:13:52 +01005587 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 s = cin_skipcomment(s + 7);
5589
Bram Moolenaar75342212013-03-07 13:13:52 +01005590 for (;;)
5591 {
5592 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005593
Bram Moolenaar75342212013-03-07 13:13:52 +01005594 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5595 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005596 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005597 if (cin_starts_with(s, skip[i]))
5598 {
5599 s = cin_skipcomment(s + l);
5600 l = 0;
5601 break;
5602 }
5603 }
5604 if (l != 0)
5605 break;
5606 }
5607
5608 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 return TRUE;
5610
5611 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5612 return TRUE;
5613
5614 return FALSE;
5615}
5616
5617/*
5618 * Recognize a switch label: "case .*:" or "default:".
5619 */
5620 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005621cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005623 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005626 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
5628 for (s += 4; *s; ++s)
5629 {
5630 s = cin_skipcomment(s);
5631 if (*s == ':')
5632 {
5633 if (s[1] == ':') /* skip over "::" for C++ */
5634 ++s;
5635 else
5636 return TRUE;
5637 }
5638 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005639 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5641 return FALSE; /* stop at comment */
5642 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005643 {
5644 /* JS etc. */
5645 if (strict)
5646 return FALSE; /* stop at string */
5647 else
5648 return TRUE;
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 }
5651 return FALSE;
5652 }
5653
5654 if (cin_isdefault(s))
5655 return TRUE;
5656 return FALSE;
5657}
5658
5659/*
5660 * Recognize a "default" switch label.
5661 */
5662 static int
5663cin_isdefault(s)
5664 char_u *s;
5665{
5666 return (STRNCMP(s, "default", 7) == 0
5667 && *(s = cin_skipcomment(s + 7)) == ':'
5668 && s[1] != ':');
5669}
5670
5671/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005672 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 */
5674 int
5675cin_isscopedecl(s)
5676 char_u *s;
5677{
5678 int i;
5679
5680 s = cin_skipcomment(s);
5681 if (STRNCMP(s, "public", 6) == 0)
5682 i = 6;
5683 else if (STRNCMP(s, "protected", 9) == 0)
5684 i = 9;
5685 else if (STRNCMP(s, "private", 7) == 0)
5686 i = 7;
5687 else
5688 return FALSE;
5689 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5690}
5691
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005692/* Maximum number of lines to search back for a "namespace" line. */
5693#define FIND_NAMESPACE_LIM 20
5694
5695/*
5696 * Recognize a "namespace" scope declaration.
5697 */
5698 static int
5699cin_is_cpp_namespace(s)
5700 char_u *s;
5701{
5702 char_u *p;
5703 int has_name = FALSE;
5704
5705 s = cin_skipcomment(s);
5706 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5707 {
5708 p = cin_skipcomment(skipwhite(s + 9));
5709 while (*p != NUL)
5710 {
5711 if (vim_iswhite(*p))
5712 {
5713 has_name = TRUE; /* found end of a name */
5714 p = cin_skipcomment(skipwhite(p));
5715 }
5716 else if (*p == '{')
5717 {
5718 break;
5719 }
5720 else if (vim_iswordc(*p))
5721 {
5722 if (has_name)
5723 return FALSE; /* word character after skipping past name */
5724 ++p;
5725 }
5726 else
5727 {
5728 return FALSE;
5729 }
5730 }
5731 return TRUE;
5732 }
5733 return FALSE;
5734}
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736/*
5737 * Return a pointer to the first non-empty non-comment character after a ':'.
5738 * Return NULL if not found.
5739 * case 234: a = b;
5740 * ^
5741 */
5742 static char_u *
5743after_label(l)
5744 char_u *l;
5745{
5746 for ( ; *l; ++l)
5747 {
5748 if (*l == ':')
5749 {
5750 if (l[1] == ':') /* skip over "::" for C++ */
5751 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005752 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 break;
5754 }
5755 else if (*l == '\'' && l[1] && l[2] == '\'')
5756 l += 2; /* skip over 'x' */
5757 }
5758 if (*l == NUL)
5759 return NULL;
5760 l = cin_skipcomment(l + 1);
5761 if (*l == NUL)
5762 return NULL;
5763 return l;
5764}
5765
5766/*
5767 * Get indent of line "lnum", skipping a label.
5768 * Return 0 if there is nothing after the label.
5769 */
5770 static int
5771get_indent_nolabel(lnum) /* XXX */
5772 linenr_T lnum;
5773{
5774 char_u *l;
5775 pos_T fp;
5776 colnr_T col;
5777 char_u *p;
5778
5779 l = ml_get(lnum);
5780 p = after_label(l);
5781 if (p == NULL)
5782 return 0;
5783
5784 fp.col = (colnr_T)(p - l);
5785 fp.lnum = lnum;
5786 getvcol(curwin, &fp, &col, NULL, NULL);
5787 return (int)col;
5788}
5789
5790/*
5791 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005792 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 * label: if (asdf && asdfasdf)
5794 * ^
5795 */
5796 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005797skip_label(lnum, pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 linenr_T lnum;
5799 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
5801 char_u *l;
5802 int amount;
5803 pos_T cursor_save;
5804
5805 cursor_save = curwin->w_cursor;
5806 curwin->w_cursor.lnum = lnum;
5807 l = ml_get_curline();
5808 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005809 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
5811 amount = get_indent_nolabel(lnum);
5812 l = after_label(ml_get_curline());
5813 if (l == NULL) /* just in case */
5814 l = ml_get_curline();
5815 }
5816 else
5817 {
5818 amount = get_indent();
5819 l = ml_get_curline();
5820 }
5821 *pp = l;
5822
5823 curwin->w_cursor = cursor_save;
5824 return amount;
5825}
5826
5827/*
5828 * Return the indent of the first variable name after a type in a declaration.
5829 * int a, indent of "a"
5830 * static struct foo b, indent of "b"
5831 * enum bla c, indent of "c"
5832 * Returns zero when it doesn't look like a declaration.
5833 */
5834 static int
5835cin_first_id_amount()
5836{
5837 char_u *line, *p, *s;
5838 int len;
5839 pos_T fp;
5840 colnr_T col;
5841
5842 line = ml_get_curline();
5843 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005844 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5846 {
5847 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005848 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
5850 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5851 p = skipwhite(p + 6);
5852 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5853 p = skipwhite(p + 4);
5854 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5855 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5856 {
5857 s = skipwhite(p + len);
5858 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5859 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5860 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5861 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5862 p = s;
5863 }
5864 for (len = 0; vim_isIDc(p[len]); ++len)
5865 ;
5866 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5867 return 0;
5868
5869 p = skipwhite(p + len);
5870 fp.lnum = curwin->w_cursor.lnum;
5871 fp.col = (colnr_T)(p - line);
5872 getvcol(curwin, &fp, &col, NULL, NULL);
5873 return (int)col;
5874}
5875
5876/*
5877 * Return the indent of the first non-blank after an equal sign.
5878 * char *foo = "here";
5879 * Return zero if no (useful) equal sign found.
5880 * Return -1 if the line above "lnum" ends in a backslash.
5881 * foo = "asdf\
5882 * asdf\
5883 * here";
5884 */
5885 static int
5886cin_get_equal_amount(lnum)
5887 linenr_T lnum;
5888{
5889 char_u *line;
5890 char_u *s;
5891 colnr_T col;
5892 pos_T fp;
5893
5894 if (lnum > 1)
5895 {
5896 line = ml_get(lnum - 1);
5897 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5898 return -1;
5899 }
5900
5901 line = s = ml_get(lnum);
5902 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5903 {
5904 if (cin_iscomment(s)) /* ignore comments */
5905 s = cin_skipcomment(s);
5906 else
5907 ++s;
5908 }
5909 if (*s != '=')
5910 return 0;
5911
5912 s = skipwhite(s + 1);
5913 if (cin_nocode(s))
5914 return 0;
5915
5916 if (*s == '"') /* nice alignment for continued strings */
5917 ++s;
5918
5919 fp.lnum = lnum;
5920 fp.col = (colnr_T)(s - line);
5921 getvcol(curwin, &fp, &col, NULL, NULL);
5922 return (int)col;
5923}
5924
5925/*
5926 * Recognize a preprocessor statement: Any line that starts with '#'.
5927 */
5928 static int
5929cin_ispreproc(s)
5930 char_u *s;
5931{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005932 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 return TRUE;
5934 return FALSE;
5935}
5936
5937/*
5938 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5939 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5940 * start and return the line in "*pp".
5941 */
5942 static int
5943cin_ispreproc_cont(pp, lnump)
5944 char_u **pp;
5945 linenr_T *lnump;
5946{
5947 char_u *line = *pp;
5948 linenr_T lnum = *lnump;
5949 int retval = FALSE;
5950
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005951 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {
5953 if (cin_ispreproc(line))
5954 {
5955 retval = TRUE;
5956 *lnump = lnum;
5957 break;
5958 }
5959 if (lnum == 1)
5960 break;
5961 line = ml_get(--lnum);
5962 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5963 break;
5964 }
5965
5966 if (lnum != *lnump)
5967 *pp = ml_get(*lnump);
5968 return retval;
5969}
5970
5971/*
5972 * Recognize the start of a C or C++ comment.
5973 */
5974 static int
5975cin_iscomment(p)
5976 char_u *p;
5977{
5978 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5979}
5980
5981/*
5982 * Recognize the start of a "//" comment.
5983 */
5984 static int
5985cin_islinecomment(p)
5986 char_u *p;
5987{
5988 return (p[0] == '/' && p[1] == '/');
5989}
5990
5991/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005992 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5993 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005995 * If a line begins with an "else", only consider it terminated if no unmatched
5996 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 * Return the character terminating the line (ending char's have precedence if
5998 * both apply in order to determine initializations).
5999 */
6000 static int
6001cin_isterminated(s, incl_open, incl_comma)
6002 char_u *s;
6003 int incl_open; /* include '{' at the end as terminator */
6004 int incl_comma; /* recognize a trailing comma */
6005{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006006 char_u found_start = 0;
6007 unsigned n_open = 0;
6008 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
6010 s = cin_skipcomment(s);
6011
6012 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6013 found_start = *s;
6014
Bram Moolenaar496f9512011-05-19 16:35:09 +02006015 if (!found_start)
6016 is_else = cin_iselse(s);
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 while (*s)
6019 {
6020 /* skip over comments, "" strings and 'c'haracters */
6021 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006022 if (*s == '}' && n_open > 0)
6023 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006024 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006025 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 && cin_nocode(s + 1))
6027 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006028 else if (*s == '{')
6029 {
6030 if (incl_open && cin_nocode(s + 1))
6031 return *s;
6032 else
6033 ++n_open;
6034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
6036 if (*s)
6037 s++;
6038 }
6039 return found_start;
6040}
6041
6042/*
6043 * Recognize the basic picture of a function declaration -- it needs to
6044 * have an open paren somewhere and a close paren at the end of the line and
6045 * no semicolons anywhere.
6046 * When a line ends in a comma we continue looking in the next line.
6047 * "sp" points to a string with the line. When looking at other lines it must
6048 * be restored to the line. When it's NULL fetch lines here.
6049 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006050 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 */
6052 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006053cin_isfuncdecl(sp, first_lnum, min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 char_u **sp;
6055 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006056 linenr_T min_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 char_u *s;
6059 linenr_T lnum = first_lnum;
6060 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006061 pos_T *trypos;
6062 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064 if (sp == NULL)
6065 s = ml_get(lnum);
6066 else
6067 s = *sp;
6068
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006069 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006070 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006071 {
6072 lnum = trypos->lnum;
6073 if (lnum < min_lnum)
6074 return FALSE;
6075
6076 s = ml_get(lnum);
6077 }
6078
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006079 /* Ignore line starting with #. */
6080 if (cin_ispreproc(s))
6081 return FALSE;
6082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6084 {
6085 if (cin_iscomment(s)) /* ignore comments */
6086 s = cin_skipcomment(s);
6087 else
6088 ++s;
6089 }
6090 if (*s != '(')
6091 return FALSE; /* ';', ' or " before any () or no '(' */
6092
6093 while (*s && *s != ';' && *s != '\'' && *s != '"')
6094 {
6095 if (*s == ')' && cin_nocode(s + 1))
6096 {
6097 /* ')' at the end: may have found a match
6098 * Check for he previous line not to end in a backslash:
6099 * #if defined(x) && \
6100 * defined(y)
6101 */
6102 lnum = first_lnum - 1;
6103 s = ml_get(lnum);
6104 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6105 retval = TRUE;
6106 goto done;
6107 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006108 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006110 int comma = (*s == ',');
6111
6112 /* ',' at the end: continue looking in the next line.
6113 * At the end: check for ',' in the next line, for this style:
6114 * func(arg1
6115 * , arg2) */
6116 for (;;)
6117 {
6118 if (lnum >= curbuf->b_ml.ml_line_count)
6119 break;
6120 s = ml_get(++lnum);
6121 if (!cin_ispreproc(s))
6122 break;
6123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (lnum >= curbuf->b_ml.ml_line_count)
6125 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006126 /* Require a comma at end of the line or a comma or ')' at the
6127 * start of next line. */
6128 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006129 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006130 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006131 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 }
6133 else if (cin_iscomment(s)) /* ignore comments */
6134 s = cin_skipcomment(s);
6135 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006138 just_started = FALSE;
6139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
6141
6142done:
6143 if (lnum != first_lnum && sp != NULL)
6144 *sp = ml_get(first_lnum);
6145
6146 return retval;
6147}
6148
6149 static int
6150cin_isif(p)
6151 char_u *p;
6152{
6153 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6154}
6155
6156 static int
6157cin_iselse(p)
6158 char_u *p;
6159{
6160 if (*p == '}') /* accept "} else" */
6161 p = cin_skipcomment(p + 1);
6162 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6163}
6164
6165 static int
6166cin_isdo(p)
6167 char_u *p;
6168{
6169 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6170}
6171
6172/*
6173 * Check if this is a "while" that should have a matching "do".
6174 * We only accept a "while (condition) ;", with only white space between the
6175 * ')' and ';'. The condition may be spread over several lines.
6176 */
6177 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006178cin_iswhileofdo(p, lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 char_u *p;
6180 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 pos_T cursor_save;
6183 pos_T *trypos;
6184 int retval = FALSE;
6185
6186 p = cin_skipcomment(p);
6187 if (*p == '}') /* accept "} while (cond);" */
6188 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006189 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
6191 cursor_save = curwin->w_cursor;
6192 curwin->w_cursor.lnum = lnum;
6193 curwin->w_cursor.col = 0;
6194 p = ml_get_curline();
6195 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6196 {
6197 ++p;
6198 ++curwin->w_cursor.col;
6199 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006200 if ((trypos = findmatchlimit(NULL, 0, 0,
6201 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6203 retval = TRUE;
6204 curwin->w_cursor = cursor_save;
6205 }
6206 return retval;
6207}
6208
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006209/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006210 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006211 * Return 0 if there is none.
6212 * Otherwise return !0 and update "*poffset" to point to the place where the
6213 * string was found.
6214 */
6215 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006216cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006217 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006218 int *poffset;
6219{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006220 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006221
6222 if (offset-- < 2)
6223 return 0;
6224 while (offset > 2 && vim_iswhite(line[offset]))
6225 --offset;
6226
6227 offset -= 1;
6228 if (!STRNCMP(line + offset, "if", 2))
6229 goto probablyFound;
6230
6231 if (offset >= 1)
6232 {
6233 offset -= 1;
6234 if (!STRNCMP(line + offset, "for", 3))
6235 goto probablyFound;
6236
6237 if (offset >= 2)
6238 {
6239 offset -= 2;
6240 if (!STRNCMP(line + offset, "while", 5))
6241 goto probablyFound;
6242 }
6243 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006244 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006245
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006246probablyFound:
6247 if (!offset || !vim_isIDc(line[offset - 1]))
6248 {
6249 *poffset = offset;
6250 return 1;
6251 }
6252 return 0;
6253}
6254
6255/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006256 * Return TRUE if we are at the end of a do-while.
6257 * do
6258 * nothing;
6259 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006260 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006261 * Adjust the cursor to the line with "while".
6262 */
6263 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006264cin_iswhileofdo_end(terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006265 int terminated;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006266{
6267 char_u *line;
6268 char_u *p;
6269 char_u *s;
6270 pos_T *trypos;
6271 int i;
6272
6273 if (terminated != ';') /* there must be a ';' at the end */
6274 return FALSE;
6275
6276 p = line = ml_get_curline();
6277 while (*p != NUL)
6278 {
6279 p = cin_skipcomment(p);
6280 if (*p == ')')
6281 {
6282 s = skipwhite(p + 1);
6283 if (*s == ';' && cin_nocode(s + 1))
6284 {
6285 /* Found ");" at end of the line, now check there is "while"
6286 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006287 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006288 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006289 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006290 if (trypos != NULL)
6291 {
6292 s = cin_skipcomment(ml_get(trypos->lnum));
6293 if (*s == '}') /* accept "} while (cond);" */
6294 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006295 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006296 {
6297 curwin->w_cursor.lnum = trypos->lnum;
6298 return TRUE;
6299 }
6300 }
6301
6302 /* Searching may have made "line" invalid, get it again. */
6303 line = ml_get_curline();
6304 p = line + i;
6305 }
6306 }
6307 if (*p != NUL)
6308 ++p;
6309 }
6310 return FALSE;
6311}
6312
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 static int
6314cin_isbreak(p)
6315 char_u *p;
6316{
6317 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6318}
6319
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006320/*
6321 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 * constructor-initialization. eg:
6323 *
6324 * class MyClass :
6325 * baseClass <-- here
6326 * class MyClass : public baseClass,
6327 * anotherBaseClass <-- here (should probably lineup ??)
6328 * MyClass::MyClass(...) :
6329 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006330 *
6331 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 */
6333 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006334cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006335 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 char_u *s;
6338 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006339 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006340 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
6342 *col = 0;
6343
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006344 s = skipwhite(line);
6345 if (*s == '#') /* skip #define FOO x ? (x) : x */
6346 return FALSE;
6347 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if (*s == NUL)
6349 return FALSE;
6350
6351 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6352
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006353 /* Search for a line starting with '#', empty, ending in ';' or containing
6354 * '{' or '}' and start below it. This handles the following situations:
6355 * a = cond ?
6356 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006357 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006358 * func::foo()
6359 * : something
6360 * {}
6361 * Foo::Foo (int one, int two)
6362 * : something(4),
6363 * somethingelse(3)
6364 * {}
6365 */
6366 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006368 line = ml_get(lnum - 1);
6369 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006370 if (*s == '#' || *s == NUL)
6371 break;
6372 while (*s != NUL)
6373 {
6374 s = cin_skipcomment(s);
6375 if (*s == '{' || *s == '}'
6376 || (*s == ';' && cin_nocode(s + 1)))
6377 break;
6378 if (*s != NUL)
6379 ++s;
6380 }
6381 if (*s != NUL)
6382 break;
6383 --lnum;
6384 }
6385
Bram Moolenaare7c56862007-08-04 10:14:52 +00006386 line = ml_get(lnum);
6387 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006388 for (;;)
6389 {
6390 if (*s == NUL)
6391 {
6392 if (lnum == curwin->w_cursor.lnum)
6393 break;
6394 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006395 line = ml_get(++lnum);
6396 s = cin_skipcomment(line);
6397 if (*s == NUL)
6398 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006399 }
6400
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006401 if (s[0] == '"')
6402 s = skip_string(s) + 1;
6403 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 {
6405 if (s[1] == ':')
6406 {
6407 /* skip double colon. It can't be a constructor
6408 * initialization any more */
6409 lookfor_ctor_init = FALSE;
6410 s = cin_skipcomment(s + 2);
6411 }
6412 else if (lookfor_ctor_init || class_or_struct)
6413 {
6414 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006415 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 cpp_base_class = TRUE;
6417 lookfor_ctor_init = class_or_struct = FALSE;
6418 *col = 0;
6419 s = cin_skipcomment(s + 1);
6420 }
6421 else
6422 s = cin_skipcomment(s + 1);
6423 }
6424 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6425 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6426 {
6427 class_or_struct = TRUE;
6428 lookfor_ctor_init = FALSE;
6429
6430 if (*s == 'c')
6431 s = cin_skipcomment(s + 5);
6432 else
6433 s = cin_skipcomment(s + 6);
6434 }
6435 else
6436 {
6437 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6438 {
6439 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6440 }
6441 else if (s[0] == ')')
6442 {
6443 /* Constructor-initialization is assumed if we come across
6444 * something like "):" */
6445 class_or_struct = FALSE;
6446 lookfor_ctor_init = TRUE;
6447 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006448 else if (s[0] == '?')
6449 {
6450 /* Avoid seeing '() :' after '?' as constructor init. */
6451 return FALSE;
6452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 else if (!vim_isIDc(s[0]))
6454 {
6455 /* if it is not an identifier, we are wrong */
6456 class_or_struct = FALSE;
6457 lookfor_ctor_init = FALSE;
6458 }
6459 else if (*col == 0)
6460 {
6461 /* it can't be a constructor-initialization any more */
6462 lookfor_ctor_init = FALSE;
6463
6464 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006465 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 *col = (colnr_T)(s - line);
6467 }
6468
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006469 /* When the line ends in a comma don't align with it. */
6470 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6471 *col = 0;
6472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 s = cin_skipcomment(s + 1);
6474 }
6475 }
6476
6477 return cpp_base_class;
6478}
6479
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006480 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006481get_baseclass_amount(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006482 int col;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006483{
6484 int amount;
6485 colnr_T vcol;
6486 pos_T *trypos;
6487
6488 if (col == 0)
6489 {
6490 amount = get_indent();
6491 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006492 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006493 amount = get_indent_lnum(trypos->lnum); /* XXX */
6494 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006495 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006496 }
6497 else
6498 {
6499 curwin->w_cursor.col = col;
6500 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6501 amount = (int)vcol;
6502 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006503 if (amount < curbuf->b_ind_cpp_baseclass)
6504 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006505 return amount;
6506}
6507
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508/*
6509 * Return TRUE if string "s" ends with the string "find", possibly followed by
6510 * white space and comments. Skip strings and comments.
6511 * Ignore "ignore" after "find" if it's not NULL.
6512 */
6513 static int
6514cin_ends_in(s, find, ignore)
6515 char_u *s;
6516 char_u *find;
6517 char_u *ignore;
6518{
6519 char_u *p = s;
6520 char_u *r;
6521 int len = (int)STRLEN(find);
6522
6523 while (*p != NUL)
6524 {
6525 p = cin_skipcomment(p);
6526 if (STRNCMP(p, find, len) == 0)
6527 {
6528 r = skipwhite(p + len);
6529 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6530 r = skipwhite(r + STRLEN(ignore));
6531 if (cin_nocode(r))
6532 return TRUE;
6533 }
6534 if (*p != NUL)
6535 ++p;
6536 }
6537 return FALSE;
6538}
6539
6540/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006541 * Return TRUE when "s" starts with "word" and then a non-ID character.
6542 */
6543 static int
6544cin_starts_with(s, word)
6545 char_u *s;
6546 char *word;
6547{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006548 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006549
6550 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6551}
6552
6553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 * Skip strings, chars and comments until at or past "trypos".
6555 * Return the column found.
6556 */
6557 static int
6558cin_skip2pos(trypos)
6559 pos_T *trypos;
6560{
6561 char_u *line;
6562 char_u *p;
6563
6564 p = line = ml_get(trypos->lnum);
6565 while (*p && (colnr_T)(p - line) < trypos->col)
6566 {
6567 if (cin_iscomment(p))
6568 p = cin_skipcomment(p);
6569 else
6570 {
6571 p = skip_string(p);
6572 ++p;
6573 }
6574 }
6575 return (int)(p - line);
6576}
6577
6578/*
6579 * Find the '{' at the start of the block we are in.
6580 * Return NULL if no match found.
6581 * Ignore a '{' that is in a comment, makes indenting the next three lines
6582 * work. */
6583/* foo() */
6584/* { */
6585/* } */
6586
6587 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006588find_start_brace() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
6590 pos_T cursor_save;
6591 pos_T *trypos;
6592 pos_T *pos;
6593 static pos_T pos_copy;
6594
6595 cursor_save = curwin->w_cursor;
6596 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6597 {
6598 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6599 trypos = &pos_copy;
6600 curwin->w_cursor = *trypos;
6601 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006602 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006604 && (pos = ind_find_start_comment()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 break;
6606 if (pos != NULL)
6607 curwin->w_cursor.lnum = pos->lnum;
6608 }
6609 curwin->w_cursor = cursor_save;
6610 return trypos;
6611}
6612
6613/*
6614 * Find the matching '(', failing if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006615 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 */
6617 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006618find_match_paren(ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 int ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 pos_T cursor_save;
6622 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006623 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625 cursor_save = curwin->w_cursor;
6626 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6627 {
6628 /* check if the ( is in a // comment */
6629 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6630 trypos = NULL;
6631 else
6632 {
6633 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6634 trypos = &pos_copy;
6635 curwin->w_cursor = *trypos;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006636 if (ind_find_start_comment() != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 trypos = NULL;
6638 }
6639 }
6640 curwin->w_cursor = cursor_save;
6641 return trypos;
6642}
6643
6644/*
6645 * Return ind_maxparen corrected for the difference in line number between the
6646 * cursor position and "startpos". This makes sure that searching for a
6647 * matching paren above the cursor line doesn't find a match because of
6648 * looking a few lines further.
6649 */
6650 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006651corr_ind_maxparen(startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 pos_T *startpos;
6653{
6654 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6655
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006656 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6657 return curbuf->b_ind_maxparen - (int)n;
6658 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659}
6660
6661/*
6662 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006663 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 */
6665 static int
6666find_last_paren(l, start, end)
6667 char_u *l;
6668 int start, end;
6669{
6670 int i;
6671 int retval = FALSE;
6672 int open_count = 0;
6673
6674 curwin->w_cursor.col = 0; /* default is start of line */
6675
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006676 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
6678 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6679 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6680 if (l[i] == start)
6681 ++open_count;
6682 else if (l[i] == end)
6683 {
6684 if (open_count > 0)
6685 --open_count;
6686 else
6687 {
6688 curwin->w_cursor.col = i;
6689 retval = TRUE;
6690 }
6691 }
6692 }
6693 return retval;
6694}
6695
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006696/*
6697 * Parse 'cinoptions' and set the values in "curbuf".
6698 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6699 */
6700 void
6701parse_cino(buf)
6702 buf_T *buf;
6703{
6704 char_u *p;
6705 char_u *l;
6706 char_u *digits;
6707 int n;
6708 int divider;
6709 int fraction = 0;
6710 int sw = (int)get_sw_value(buf);
6711
6712 /*
6713 * Set the default values.
6714 */
6715 /* Spaces from a block's opening brace the prevailing indent for that
6716 * block should be. */
6717 buf->b_ind_level = sw;
6718
6719 /* Spaces from the edge of the line an open brace that's at the end of a
6720 * line is imagined to be. */
6721 buf->b_ind_open_imag = 0;
6722
6723 /* Spaces from the prevailing indent for a line that is not preceded by
6724 * an opening brace. */
6725 buf->b_ind_no_brace = 0;
6726
6727 /* Column where the first { of a function should be located }. */
6728 buf->b_ind_first_open = 0;
6729
6730 /* Spaces from the prevailing indent a leftmost open brace should be
6731 * located. */
6732 buf->b_ind_open_extra = 0;
6733
6734 /* Spaces from the matching open brace (real location for one at the left
6735 * edge; imaginary location from one that ends a line) the matching close
6736 * brace should be located. */
6737 buf->b_ind_close_extra = 0;
6738
6739 /* Spaces from the edge of the line an open brace sitting in the leftmost
6740 * column is imagined to be. */
6741 buf->b_ind_open_left_imag = 0;
6742
6743 /* Spaces jump labels should be shifted to the left if N is non-negative,
6744 * otherwise the jump label will be put to column 1. */
6745 buf->b_ind_jump_label = -1;
6746
6747 /* Spaces from the switch() indent a "case xx" label should be located. */
6748 buf->b_ind_case = sw;
6749
6750 /* Spaces from the "case xx:" code after a switch() should be located. */
6751 buf->b_ind_case_code = sw;
6752
6753 /* Lineup break at end of case in switch() with case label. */
6754 buf->b_ind_case_break = 0;
6755
6756 /* Spaces from the class declaration indent a scope declaration label
6757 * should be located. */
6758 buf->b_ind_scopedecl = sw;
6759
6760 /* Spaces from the scope declaration label code should be located. */
6761 buf->b_ind_scopedecl_code = sw;
6762
6763 /* Amount K&R-style parameters should be indented. */
6764 buf->b_ind_param = sw;
6765
6766 /* Amount a function type spec should be indented. */
6767 buf->b_ind_func_type = sw;
6768
6769 /* Amount a cpp base class declaration or constructor initialization
6770 * should be indented. */
6771 buf->b_ind_cpp_baseclass = sw;
6772
6773 /* additional spaces beyond the prevailing indent a continuation line
6774 * should be located. */
6775 buf->b_ind_continuation = sw;
6776
6777 /* Spaces from the indent of the line with an unclosed parentheses. */
6778 buf->b_ind_unclosed = sw * 2;
6779
6780 /* Spaces from the indent of the line with an unclosed parentheses, which
6781 * itself is also unclosed. */
6782 buf->b_ind_unclosed2 = sw;
6783
6784 /* Suppress ignoring spaces from the indent of a line starting with an
6785 * unclosed parentheses. */
6786 buf->b_ind_unclosed_noignore = 0;
6787
6788 /* If the opening paren is the last nonwhite character on the line, and
6789 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6790 * context (for very long lines). */
6791 buf->b_ind_unclosed_wrapped = 0;
6792
6793 /* Suppress ignoring white space when lining up with the character after
6794 * an unclosed parentheses. */
6795 buf->b_ind_unclosed_whiteok = 0;
6796
6797 /* Indent a closing parentheses under the line start of the matching
6798 * opening parentheses. */
6799 buf->b_ind_matching_paren = 0;
6800
6801 /* Indent a closing parentheses under the previous line. */
6802 buf->b_ind_paren_prev = 0;
6803
6804 /* Extra indent for comments. */
6805 buf->b_ind_comment = 0;
6806
6807 /* Spaces from the comment opener when there is nothing after it. */
6808 buf->b_ind_in_comment = 3;
6809
6810 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6811 * after the comment opener. */
6812 buf->b_ind_in_comment2 = 0;
6813
6814 /* Max lines to search for an open paren. */
6815 buf->b_ind_maxparen = 20;
6816
6817 /* Max lines to search for an open comment. */
6818 buf->b_ind_maxcomment = 70;
6819
6820 /* Handle braces for java code. */
6821 buf->b_ind_java = 0;
6822
6823 /* Not to confuse JS object properties with labels. */
6824 buf->b_ind_js = 0;
6825
6826 /* Handle blocked cases correctly. */
6827 buf->b_ind_keep_case_label = 0;
6828
6829 /* Handle C++ namespace. */
6830 buf->b_ind_cpp_namespace = 0;
6831
6832 /* Handle continuation lines containing conditions of if(), for() and
6833 * while(). */
6834 buf->b_ind_if_for_while = 0;
6835
6836 for (p = buf->b_p_cino; *p; )
6837 {
6838 l = p++;
6839 if (*p == '-')
6840 ++p;
6841 digits = p; /* remember where the digits start */
6842 n = getdigits(&p);
6843 divider = 0;
6844 if (*p == '.') /* ".5s" means a fraction */
6845 {
6846 fraction = atol((char *)++p);
6847 while (VIM_ISDIGIT(*p))
6848 {
6849 ++p;
6850 if (divider)
6851 divider *= 10;
6852 else
6853 divider = 10;
6854 }
6855 }
6856 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6857 {
6858 if (p == digits)
6859 n = sw; /* just "s" is one 'shiftwidth' */
6860 else
6861 {
6862 n *= sw;
6863 if (divider)
6864 n += (sw * fraction + divider / 2) / divider;
6865 }
6866 ++p;
6867 }
6868 if (l[1] == '-')
6869 n = -n;
6870
6871 /* When adding an entry here, also update the default 'cinoptions' in
6872 * doc/indent.txt, and add explanation for it! */
6873 switch (*l)
6874 {
6875 case '>': buf->b_ind_level = n; break;
6876 case 'e': buf->b_ind_open_imag = n; break;
6877 case 'n': buf->b_ind_no_brace = n; break;
6878 case 'f': buf->b_ind_first_open = n; break;
6879 case '{': buf->b_ind_open_extra = n; break;
6880 case '}': buf->b_ind_close_extra = n; break;
6881 case '^': buf->b_ind_open_left_imag = n; break;
6882 case 'L': buf->b_ind_jump_label = n; break;
6883 case ':': buf->b_ind_case = n; break;
6884 case '=': buf->b_ind_case_code = n; break;
6885 case 'b': buf->b_ind_case_break = n; break;
6886 case 'p': buf->b_ind_param = n; break;
6887 case 't': buf->b_ind_func_type = n; break;
6888 case '/': buf->b_ind_comment = n; break;
6889 case 'c': buf->b_ind_in_comment = n; break;
6890 case 'C': buf->b_ind_in_comment2 = n; break;
6891 case 'i': buf->b_ind_cpp_baseclass = n; break;
6892 case '+': buf->b_ind_continuation = n; break;
6893 case '(': buf->b_ind_unclosed = n; break;
6894 case 'u': buf->b_ind_unclosed2 = n; break;
6895 case 'U': buf->b_ind_unclosed_noignore = n; break;
6896 case 'W': buf->b_ind_unclosed_wrapped = n; break;
6897 case 'w': buf->b_ind_unclosed_whiteok = n; break;
6898 case 'm': buf->b_ind_matching_paren = n; break;
6899 case 'M': buf->b_ind_paren_prev = n; break;
6900 case ')': buf->b_ind_maxparen = n; break;
6901 case '*': buf->b_ind_maxcomment = n; break;
6902 case 'g': buf->b_ind_scopedecl = n; break;
6903 case 'h': buf->b_ind_scopedecl_code = n; break;
6904 case 'j': buf->b_ind_java = n; break;
6905 case 'J': buf->b_ind_js = n; break;
6906 case 'l': buf->b_ind_keep_case_label = n; break;
6907 case '#': buf->b_ind_hash_comment = n; break;
6908 case 'N': buf->b_ind_cpp_namespace = n; break;
6909 case 'k': buf->b_ind_if_for_while = n; break;
6910 }
6911 if (*p == ',')
6912 ++p;
6913 }
6914}
6915
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 int
6917get_c_indent()
6918{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 pos_T cur_curpos;
6920 int amount;
6921 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006922 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 colnr_T col;
6924 char_u *theline;
6925 char_u *linecopy;
6926 pos_T *trypos;
6927 pos_T *tryposBrace = NULL;
6928 pos_T our_paren_pos;
6929 char_u *start;
6930 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006931#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932#define BRACE_AT_START 2 /* '{' is at start of line */
6933#define BRACE_AT_END 3 /* '{' is at end of line */
6934 linenr_T ourscope;
6935 char_u *l;
6936 char_u *look;
6937 char_u terminated;
6938 int lookfor;
6939#define LOOKFOR_INITIAL 0
6940#define LOOKFOR_IF 1
6941#define LOOKFOR_DO 2
6942#define LOOKFOR_CASE 3
6943#define LOOKFOR_ANY 4
6944#define LOOKFOR_TERM 5
6945#define LOOKFOR_UNTERM 6
6946#define LOOKFOR_SCOPEDECL 7
6947#define LOOKFOR_NOBREAK 8
6948#define LOOKFOR_CPP_BASECLASS 9
6949#define LOOKFOR_ENUM_OR_INIT 10
6950
6951 int whilelevel;
6952 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 int n;
6954 int iscase;
6955 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006956 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006958 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006959 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006961 /* make a copy, value is changed below */
6962 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964 /* remember where the cursor was when we started */
6965 cur_curpos = curwin->w_cursor;
6966
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006967 /* if we are at line 1 0 is fine, right? */
6968 if (cur_curpos.lnum == 1)
6969 return 0;
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 /* Get a copy of the current contents of the line.
6972 * This is required, because only the most recent line obtained with
6973 * ml_get is valid! */
6974 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6975 if (linecopy == NULL)
6976 return 0;
6977
6978 /*
6979 * In insert mode and the cursor is on a ')' truncate the line at the
6980 * cursor position. We don't want to line up with the matching '(' when
6981 * inserting new stuff.
6982 * For unknown reasons the cursor might be past the end of the line, thus
6983 * check for that.
6984 */
6985 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006986 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 && linecopy[curwin->w_cursor.col] == ')')
6988 linecopy[curwin->w_cursor.col] = NUL;
6989
6990 theline = skipwhite(linecopy);
6991
6992 /* move the cursor to the start of the line */
6993
6994 curwin->w_cursor.col = 0;
6995
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006996 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 /*
6999 * #defines and so on always go at the left when included in 'cinkeys'.
7000 */
7001 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007002 amount = curbuf->b_ind_hash_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007005 * Is it a non-case label? Then that goes at the left margin too unless:
7006 * - JS flag is set.
7007 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007009 else if (original_line_islabel && !curbuf->b_ind_js
7010 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
7012 amount = 0;
7013 }
7014
7015 /*
7016 * If we're inside a "//" comment and there is a "//" comment in a
7017 * previous line, lineup with that one.
7018 */
7019 else if (cin_islinecomment(theline)
7020 && (trypos = find_line_comment()) != NULL) /* XXX */
7021 {
7022 /* find how indented the line beginning the comment is */
7023 getvcol(curwin, trypos, &col, NULL, NULL);
7024 amount = col;
7025 }
7026
7027 /*
7028 * If we're inside a comment and not looking at the start of the
7029 * comment, try using the 'comments' option.
7030 */
7031 else if (!cin_iscomment(theline)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007032 && (trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007033 /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {
7035 int lead_start_len = 2;
7036 int lead_middle_len = 1;
7037 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7038 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7039 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7040 char_u *p;
7041 int start_align = 0;
7042 int start_off = 0;
7043 int done = FALSE;
7044
7045 /* find how indented the line beginning the comment is */
7046 getvcol(curwin, trypos, &col, NULL, NULL);
7047 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007048 *lead_start = NUL;
7049 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
7051 p = curbuf->b_p_com;
7052 while (*p != NUL)
7053 {
7054 int align = 0;
7055 int off = 0;
7056 int what = 0;
7057
7058 while (*p != NUL && *p != ':')
7059 {
7060 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7061 what = *p++;
7062 else if (*p == COM_LEFT || *p == COM_RIGHT)
7063 align = *p++;
7064 else if (VIM_ISDIGIT(*p) || *p == '-')
7065 off = getdigits(&p);
7066 else
7067 ++p;
7068 }
7069
7070 if (*p == ':')
7071 ++p;
7072 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7073 if (what == COM_START)
7074 {
7075 STRCPY(lead_start, lead_end);
7076 lead_start_len = (int)STRLEN(lead_start);
7077 start_off = off;
7078 start_align = align;
7079 }
7080 else if (what == COM_MIDDLE)
7081 {
7082 STRCPY(lead_middle, lead_end);
7083 lead_middle_len = (int)STRLEN(lead_middle);
7084 }
7085 else if (what == COM_END)
7086 {
7087 /* If our line starts with the middle comment string, line it
7088 * up with the comment opener per the 'comments' option. */
7089 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7090 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7091 {
7092 done = TRUE;
7093 if (curwin->w_cursor.lnum > 1)
7094 {
7095 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007096 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 * the middle comment string matches in the previous
7098 * line, use the indent of that line. XXX */
7099 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7100 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7101 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7102 else if (STRNCMP(look, lead_middle,
7103 lead_middle_len) == 0)
7104 {
7105 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7106 break;
7107 }
7108 /* If the start comment string doesn't match with the
7109 * start of the comment, skip this entry. XXX */
7110 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7111 lead_start, lead_start_len) != 0)
7112 continue;
7113 }
7114 if (start_off != 0)
7115 amount += start_off;
7116 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007117 amount += vim_strsize(lead_start)
7118 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 break;
7120 }
7121
7122 /* If our line starts with the end comment string, line it up
7123 * with the middle comment */
7124 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7125 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7126 {
7127 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7128 /* XXX */
7129 if (off != 0)
7130 amount += off;
7131 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007132 amount += vim_strsize(lead_start)
7133 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 done = TRUE;
7135 break;
7136 }
7137 }
7138 }
7139
7140 /* If our line starts with an asterisk, line up with the
7141 * asterisk in the comment opener; otherwise, line up
7142 * with the first character of the comment text.
7143 */
7144 if (done)
7145 ;
7146 else if (theline[0] == '*')
7147 amount += 1;
7148 else
7149 {
7150 /*
7151 * If we are more than one line away from the comment opener, take
7152 * the indent of the previous non-empty line. If 'cino' has "CO"
7153 * and we are just below the comment opener and there are any
7154 * white characters after it line up with the text after it;
7155 * otherwise, add the amount specified by "c" in 'cino'
7156 */
7157 amount = -1;
7158 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7159 {
7160 if (linewhite(lnum)) /* skip blank lines */
7161 continue;
7162 amount = get_indent_lnum(lnum); /* XXX */
7163 break;
7164 }
7165 if (amount == -1) /* use the comment opener */
7166 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007167 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {
7169 start = ml_get(trypos->lnum);
7170 look = start + trypos->col + 2; /* skip / and * */
7171 if (*look != NUL) /* if something after it */
7172 trypos->col = (colnr_T)(skipwhite(look) - start);
7173 }
7174 getvcol(curwin, trypos, &col, NULL, NULL);
7175 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007176 if (curbuf->b_ind_in_comment2 || *look == NUL)
7177 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
7179 }
7180 }
7181
7182 /*
7183 * Are we inside parentheses or braces?
7184 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007185 else if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007186 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007187 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 || trypos != NULL)
7189 {
7190 if (trypos != NULL && tryposBrace != NULL)
7191 {
7192 /* Both an unmatched '(' and '{' is found. Use the one which is
7193 * closer to the current cursor position, set the other to NULL. */
7194 if (trypos->lnum != tryposBrace->lnum
7195 ? trypos->lnum < tryposBrace->lnum
7196 : trypos->col < tryposBrace->col)
7197 trypos = NULL;
7198 else
7199 tryposBrace = NULL;
7200 }
7201
7202 if (trypos != NULL)
7203 {
7204 /*
7205 * If the matching paren is more than one line away, use the indent of
7206 * a previous non-empty line that matches the same paren.
7207 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007208 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007210 /* Line up with the start of the matching paren line. */
7211 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7212 }
7213 else
7214 {
7215 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007216 our_paren_pos = *trypos;
7217 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007219 l = skipwhite(ml_get(lnum));
7220 if (cin_nocode(l)) /* skip comment lines */
7221 continue;
7222 if (cin_ispreproc_cont(&l, &lnum))
7223 continue; /* ignore #define, #if, etc. */
7224 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007226 /* Skip a comment. XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007227 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007228 {
7229 lnum = trypos->lnum + 1;
7230 continue;
7231 }
7232
7233 /* XXX */
7234 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007235 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007236 && trypos->lnum == our_paren_pos.lnum
7237 && trypos->col == our_paren_pos.col)
7238 {
7239 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007241 if (theline[0] == ')')
7242 {
7243 if (our_paren_pos.lnum != lnum
7244 && cur_amount > amount)
7245 cur_amount = amount;
7246 amount = -1;
7247 }
7248 break;
7249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
7251 }
7252
7253 /*
7254 * Line up with line where the matching paren is. XXX
7255 * If the line starts with a '(' or the indent for unclosed
7256 * parentheses is zero, line up with the unclosed parentheses.
7257 */
7258 if (amount == -1)
7259 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007260 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007261 int is_if_for_while = 0;
7262
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007263 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007264 {
7265 /* Look for the outermost opening parenthesis on this line
7266 * and check whether it belongs to an "if", "for" or "while". */
7267
7268 pos_T cursor_save = curwin->w_cursor;
7269 pos_T outermost;
7270 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007271
7272 trypos = &our_paren_pos;
7273 do {
7274 outermost = *trypos;
7275 curwin->w_cursor.lnum = outermost.lnum;
7276 curwin->w_cursor.col = outermost.col;
7277
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007278 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007279 } while (trypos && trypos->lnum == outermost.lnum);
7280
7281 curwin->w_cursor = cursor_save;
7282
7283 line = ml_get(outermost.lnum);
7284
7285 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007286 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007287 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007288
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007289 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007290 look = skipwhite(look);
7291 if (*look == '(')
7292 {
7293 linenr_T save_lnum = curwin->w_cursor.lnum;
7294 char_u *line;
7295 int look_col;
7296
7297 /* Ignore a '(' in front of the line that has a match before
7298 * our matching '('. */
7299 curwin->w_cursor.lnum = our_paren_pos.lnum;
7300 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007301 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007302 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007303 if ((trypos = findmatchlimit(NULL, ')', 0,
7304 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007305 != NULL
7306 && trypos->lnum == our_paren_pos.lnum
7307 && trypos->col < our_paren_pos.col)
7308 ignore_paren_col = trypos->col + 1;
7309
7310 curwin->w_cursor.lnum = save_lnum;
7311 look = ml_get(our_paren_pos.lnum) + look_col;
7312 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007313 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7314 && is_if_for_while == 0)
7315 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007316 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 {
7318 /*
7319 * If we're looking at a close paren, line up right there;
7320 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007321 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 * the last nonwhite character of the line, use either the
7323 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007324 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 * lines).
7326 */
7327 if (theline[0] != ')')
7328 {
7329 cur_amount = MAXCOL;
7330 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007331 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 && cin_ends_in(l, (char_u *)"(", NULL))
7333 {
7334 /* look for opening unmatched paren, indent one level
7335 * for each additional level */
7336 n = 1;
7337 for (col = 0; col < our_paren_pos.col; ++col)
7338 {
7339 switch (l[col])
7340 {
7341 case '(':
7342 case '{': ++n;
7343 break;
7344
7345 case ')':
7346 case '}': if (n > 1)
7347 --n;
7348 break;
7349 }
7350 }
7351
7352 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007353 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007355 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 our_paren_pos.col++;
7357 else
7358 {
7359 col = our_paren_pos.col + 1;
7360 while (vim_iswhite(l[col]))
7361 col++;
7362 if (l[col] != NUL) /* In case of trailing space */
7363 our_paren_pos.col = col;
7364 else
7365 our_paren_pos.col++;
7366 }
7367 }
7368
7369 /*
7370 * Find how indented the paren is, or the character after it
7371 * if we did the above "if".
7372 */
7373 if (our_paren_pos.col > 0)
7374 {
7375 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7376 if (cur_amount > (int)col)
7377 cur_amount = col;
7378 }
7379 }
7380
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007381 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
7383 /* Line up with the start of the matching paren line. */
7384 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007385 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7386 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007387 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 {
7389 if (cur_amount != MAXCOL)
7390 amount = cur_amount;
7391 }
7392 else
7393 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007394 /* Add b_ind_unclosed2 for each '(' before our matching one,
7395 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007397 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 --our_paren_pos.col;
7400 switch (*ml_get_pos(&our_paren_pos))
7401 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007402 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 col = our_paren_pos.col;
7404 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007405 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 col = MAXCOL;
7407 break;
7408 }
7409 }
7410
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007411 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 * braces */
7413 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007414 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 else
7416 {
7417 curwin->w_cursor.lnum = our_paren_pos.lnum;
7418 curwin->w_cursor.col = col;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007419 if (find_match_paren(curbuf->b_ind_maxparen) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007420 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007422 {
7423 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007424 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007425 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007426 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429 /*
7430 * For a line starting with ')' use the minimum of the two
7431 * positions, to avoid giving it more indent than the previous
7432 * lines:
7433 * func_long_name( if (x
7434 * arg && yy
7435 * ) ^ not here ) ^ not here
7436 */
7437 if (cur_amount < amount)
7438 amount = cur_amount;
7439 }
7440 }
7441
7442 /* add extra indent for a comment */
7443 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007444 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 }
7446
7447 /*
7448 * Are we at least inside braces, then?
7449 */
7450 else
7451 {
7452 trypos = tryposBrace;
7453
7454 ourscope = trypos->lnum;
7455 start = ml_get(ourscope);
7456
7457 /*
7458 * Now figure out how indented the line is in general.
7459 * If the brace was at the start of the line, we use that;
7460 * otherwise, check out the indentation of the line as
7461 * a whole and then add the "imaginary indent" to that.
7462 */
7463 look = skipwhite(start);
7464 if (*look == '{')
7465 {
7466 getvcol(curwin, trypos, &col, NULL, NULL);
7467 amount = col;
7468 if (*start == '{')
7469 start_brace = BRACE_IN_COL0;
7470 else
7471 start_brace = BRACE_AT_START;
7472 }
7473 else
7474 {
7475 /*
7476 * that opening brace might have been on a continuation
7477 * line. if so, find the start of the line.
7478 */
7479 curwin->w_cursor.lnum = ourscope;
7480
7481 /*
7482 * position the cursor over the rightmost paren, so that
7483 * matching it will take us back to the start of the line.
7484 */
7485 lnum = ourscope;
7486 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007487 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7488 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 lnum = trypos->lnum;
7490
7491 /*
7492 * It could have been something like
7493 * case 1: if (asdf &&
7494 * ldfd) {
7495 * }
7496 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007497 if (curbuf->b_ind_js || (curbuf->b_ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007498 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 amount = get_indent();
7500 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007501 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
7503 start_brace = BRACE_AT_END;
7504 }
7505
7506 /*
7507 * if we're looking at a closing brace, that's where
7508 * we want to be. otherwise, add the amount of room
7509 * that an indent is supposed to be.
7510 */
7511 if (theline[0] == '}')
7512 {
7513 /*
7514 * they may want closing braces to line up with something
7515 * other than the open brace. indulge them, if so.
7516 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007517 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
7519 else
7520 {
7521 /*
7522 * If we're looking at an "else", try to find an "if"
7523 * to match it with.
7524 * If we're looking at a "while", try to find a "do"
7525 * to match it with.
7526 */
7527 lookfor = LOOKFOR_INITIAL;
7528 if (cin_iselse(theline))
7529 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007530 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 lookfor = LOOKFOR_DO;
7532 if (lookfor != LOOKFOR_INITIAL)
7533 {
7534 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007535 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 {
7537 amount = get_indent(); /* XXX */
7538 goto theend;
7539 }
7540 }
7541
7542 /*
7543 * We get here if we are not on an "while-of-do" or "else" (or
7544 * failed to find a matching "if").
7545 * Search backwards for something to line up with.
7546 * First set amount for when we don't find anything.
7547 */
7548
7549 /*
7550 * if the '{' is _really_ at the left margin, use the imaginary
7551 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007552 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 */
7554
7555 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7556 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007557 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007558 lookfor_cpp_namespace = TRUE;
7559 }
7560 else if (start_brace == BRACE_AT_START &&
7561 lookfor_cpp_namespace) /* '{' is at start */
7562 {
7563
7564 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
7566 else
7567 {
7568 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007569 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007570 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007571
7572 l = skipwhite(ml_get_curline());
7573 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007574 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 else
7577 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007578 /* Compensate for adding b_ind_open_extra later. */
7579 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (amount < 0)
7581 amount = 0;
7582 }
7583 }
7584
7585 lookfor_break = FALSE;
7586
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007587 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {
7589 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007590 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
7592 else if (cin_isscopedecl(theline)) /* private:, ... */
7593 {
7594 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007595 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597 else
7598 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007599 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7600 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 lookfor_break = TRUE;
7602
7603 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007604 /* b_ind_level from start of block */
7605 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607 scope_amount = amount;
7608 whilelevel = 0;
7609
7610 /*
7611 * Search backwards. If we find something we recognize, line up
7612 * with that.
7613 *
7614 * if we're looking at an open brace, indent
7615 * the usual amount relative to the conditional
7616 * that opens the block.
7617 */
7618 curwin->w_cursor = cur_curpos;
7619 for (;;)
7620 {
7621 curwin->w_cursor.lnum--;
7622 curwin->w_cursor.col = 0;
7623
7624 /*
7625 * If we went all the way back to the start of our scope, line
7626 * up with it.
7627 */
7628 if (curwin->w_cursor.lnum <= ourscope)
7629 {
7630 /* we reached end of scope:
7631 * if looking for a enum or structure initialization
7632 * go further back:
7633 * if it is an initializer (enum xxx or xxx =), then
7634 * don't add ind_continuation, otherwise it is a variable
7635 * declaration:
7636 * int x,
7637 * here; <-- add ind_continuation
7638 */
7639 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7640 {
7641 if (curwin->w_cursor.lnum == 0
7642 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007643 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007645 /* nothing found (abuse curbuf->b_ind_maxparen as
7646 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 * initialization) */
7648 if (cont_amount > 0)
7649 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007650 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 amount += ind_continuation;
7652 break;
7653 }
7654
7655 l = ml_get_curline();
7656
7657 /*
7658 * If we're in a comment now, skip to the start of the
7659 * comment.
7660 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007661 trypos = ind_find_start_comment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 if (trypos != NULL)
7663 {
7664 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007665 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 continue;
7667 }
7668
7669 /*
7670 * Skip preprocessor directives and blank lines.
7671 */
7672 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7673 continue;
7674
7675 if (cin_nocode(l))
7676 continue;
7677
7678 terminated = cin_isterminated(l, FALSE, TRUE);
7679
7680 /*
7681 * If we are at top level and the line looks like a
7682 * function declaration, we are done
7683 * (it's a variable declaration).
7684 */
7685 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007686 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
7688 /* if the line is terminated with another ','
7689 * it is a continued variable initialization.
7690 * don't add extra indent.
7691 * TODO: does not work, if a function
7692 * declaration is split over multiple lines:
7693 * cin_isfuncdecl returns FALSE then.
7694 */
7695 if (terminated == ',')
7696 break;
7697
7698 /* if it es a enum declaration or an assignment,
7699 * we are done.
7700 */
7701 if (terminated != ';' && cin_isinit())
7702 break;
7703
7704 /* nothing useful found */
7705 if (terminated == 0 || terminated == '{')
7706 continue;
7707 }
7708
7709 if (terminated != ';')
7710 {
7711 /* Skip parens and braces. Position the cursor
7712 * over the rightmost paren, so that matching it
7713 * will take us back to the start of the line.
7714 */ /* XXX */
7715 trypos = NULL;
7716 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007717 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007718 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719
7720 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007721 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722
7723 if (trypos != NULL)
7724 {
7725 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007726 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 continue;
7728 }
7729 }
7730
7731 /* it's a variable declaration, add indentation
7732 * like in
7733 * int a,
7734 * b;
7735 */
7736 if (cont_amount > 0)
7737 amount = cont_amount;
7738 else
7739 amount += ind_continuation;
7740 }
7741 else if (lookfor == LOOKFOR_UNTERM)
7742 {
7743 if (cont_amount > 0)
7744 amount = cont_amount;
7745 else
7746 amount += ind_continuation;
7747 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007748 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007749 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007750 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007752 {
7753 amount = scope_amount;
7754 if (theline[0] == '{')
7755 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007756 amount += curbuf->b_ind_open_extra;
7757 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007758 }
7759 }
7760
7761 if (lookfor_cpp_namespace)
7762 {
7763 /*
7764 * Looking for C++ namespace, need to look further
7765 * back.
7766 */
7767 if (curwin->w_cursor.lnum == ourscope)
7768 continue;
7769
7770 if (curwin->w_cursor.lnum == 0
7771 || curwin->w_cursor.lnum
7772 < ourscope - FIND_NAMESPACE_LIM)
7773 break;
7774
7775 l = ml_get_curline();
7776
7777 /* If we're in a comment now, skip to the start of
7778 * the comment. */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007779 trypos = ind_find_start_comment();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007780 if (trypos != NULL)
7781 {
7782 curwin->w_cursor.lnum = trypos->lnum + 1;
7783 curwin->w_cursor.col = 0;
7784 continue;
7785 }
7786
7787 /* Skip preprocessor directives and blank lines. */
7788 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7789 continue;
7790
7791 /* Finally the actual check for "namespace". */
7792 if (cin_is_cpp_namespace(l))
7793 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007794 amount += curbuf->b_ind_cpp_namespace
7795 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007796 break;
7797 }
7798
7799 if (cin_nocode(l))
7800 continue;
7801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 }
7803 break;
7804 }
7805
7806 /*
7807 * If we're in a comment now, skip to the start of the comment.
7808 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007809 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 {
7811 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007812 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 continue;
7814 }
7815
7816 l = ml_get_curline();
7817
7818 /*
7819 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007820 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007822 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (iscase || cin_isscopedecl(l))
7824 {
7825 /* we are only looking for cpp base class
7826 * declaration/initialization any longer */
7827 if (lookfor == LOOKFOR_CPP_BASECLASS)
7828 break;
7829
7830 /* When looking for a "do" we are not interested in
7831 * labels. */
7832 if (whilelevel > 0)
7833 continue;
7834
7835 /*
7836 * case xx:
7837 * c = 99 + <- this indent plus continuation
7838 *-> here;
7839 */
7840 if (lookfor == LOOKFOR_UNTERM
7841 || lookfor == LOOKFOR_ENUM_OR_INIT)
7842 {
7843 if (cont_amount > 0)
7844 amount = cont_amount;
7845 else
7846 amount += ind_continuation;
7847 break;
7848 }
7849
7850 /*
7851 * case xx: <- line up with this case
7852 * x = 333;
7853 * case yy:
7854 */
7855 if ( (iscase && lookfor == LOOKFOR_CASE)
7856 || (iscase && lookfor_break)
7857 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7858 {
7859 /*
7860 * Check that this case label is not for another
7861 * switch()
7862 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007863 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007864 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {
7866 amount = get_indent(); /* XXX */
7867 break;
7868 }
7869 continue;
7870 }
7871
7872 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7873
7874 /*
7875 * case xx: if (cond) <- line up with this if
7876 * y = y + 1;
7877 * -> s = 99;
7878 *
7879 * case xx:
7880 * if (cond) <- line up with this line
7881 * y = y + 1;
7882 * -> s = 99;
7883 */
7884 if (lookfor == LOOKFOR_TERM)
7885 {
7886 if (n)
7887 amount = n;
7888
7889 if (!lookfor_break)
7890 break;
7891 }
7892
7893 /*
7894 * case xx: x = x + 1; <- line up with this x
7895 * -> y = y + 1;
7896 *
7897 * case xx: if (cond) <- line up with this if
7898 * -> y = y + 1;
7899 */
7900 if (n)
7901 {
7902 amount = n;
7903 l = after_label(ml_get_curline());
7904 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007905 {
7906 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007907 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007908 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007909 amount += curbuf->b_ind_level
7910 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 break;
7913 }
7914
7915 /*
7916 * Try to get the indent of a statement before the switch
7917 * label. If nothing is found, line up relative to the
7918 * switch label.
7919 * break; <- may line up with this line
7920 * case xx:
7921 * -> y = 1;
7922 */
7923 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007924 ? curbuf->b_ind_case_code
7925 : curbuf->b_ind_scopedecl_code);
7926 lookfor = curbuf->b_ind_case_break
7927 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 continue;
7929 }
7930
7931 /*
7932 * Looking for a switch() label or C++ scope declaration,
7933 * ignore other lines, skip {}-blocks.
7934 */
7935 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7936 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007937 if (find_last_paren(l, '{', '}')
7938 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007941 curwin->w_cursor.col = 0;
7942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 continue;
7944 }
7945
7946 /*
7947 * Ignore jump labels with nothing after them.
7948 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007949 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {
7951 l = after_label(ml_get_curline());
7952 if (l == NULL || cin_nocode(l))
7953 continue;
7954 }
7955
7956 /*
7957 * Ignore #defines, #if, etc.
7958 * Ignore comment and empty lines.
7959 * (need to get the line again, cin_islabel() may have
7960 * unlocked it)
7961 */
7962 l = ml_get_curline();
7963 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7964 || cin_nocode(l))
7965 continue;
7966
7967 /*
7968 * Are we at the start of a cpp base class declaration or
7969 * constructor initialization?
7970 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007971 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007972 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00007973 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007974 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007975 l = ml_get_curline();
7976 }
7977 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {
7979 if (lookfor == LOOKFOR_UNTERM)
7980 {
7981 if (cont_amount > 0)
7982 amount = cont_amount;
7983 else
7984 amount += ind_continuation;
7985 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007986 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007988 /* Need to find start of the declaration. */
7989 lookfor = LOOKFOR_UNTERM;
7990 ind_continuation = 0;
7991 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 }
7993 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007994 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007995 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 break;
7997 }
7998 else if (lookfor == LOOKFOR_CPP_BASECLASS)
7999 {
8000 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008001 * declaration or initialization before the opening brace.
8002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 if (cin_isterminated(l, TRUE, FALSE))
8004 break;
8005 else
8006 continue;
8007 }
8008
8009 /*
8010 * What happens next depends on the line being terminated.
8011 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008012 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 * 123,
8014 * sizeof
8015 * here
8016 * Otherwise check whether it is a enumeration or structure
8017 * initialisation (not indented) or a variable declaration
8018 * (indented).
8019 */
8020 terminated = cin_isterminated(l, FALSE, TRUE);
8021
8022 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8023 && terminated == ','))
8024 {
8025 /*
8026 * if we're in the middle of a paren thing,
8027 * go back to the line that starts it so
8028 * we can get the right prevailing indent
8029 * if ( foo &&
8030 * bar )
8031 */
8032 /*
8033 * position the cursor over the rightmost paren, so that
8034 * matching it will take us back to the start of the line.
8035 */
8036 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008037 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038
8039 /*
8040 * If we are looking for ',', we also look for matching
8041 * braces.
8042 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008043 if (trypos == NULL && terminated == ','
8044 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008045 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
8047 if (trypos != NULL)
8048 {
8049 /*
8050 * Check if we are on a case label now. This is
8051 * handled above.
8052 * case xx: if ( asdf &&
8053 * asdf)
8054 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008055 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008057 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {
8059 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008060 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 continue;
8062 }
8063 }
8064
8065 /*
8066 * Skip over continuation lines to find the one to get the
8067 * indent from
8068 * char *usethis = "bla\
8069 * bla",
8070 * here;
8071 */
8072 if (terminated == ',')
8073 {
8074 while (curwin->w_cursor.lnum > 1)
8075 {
8076 l = ml_get(curwin->w_cursor.lnum - 1);
8077 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8078 break;
8079 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008080 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 }
8082 }
8083
8084 /*
8085 * Get indent and pointer to text for current line,
8086 * ignoring any jump label. XXX
8087 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008088 if (!curbuf->b_ind_js)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008089 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008090 else
8091 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 /*
8093 * If this is just above the line we are indenting, and it
8094 * starts with a '{', line it up with this line.
8095 * while (not)
8096 * -> {
8097 * }
8098 */
8099 if (terminated != ',' && lookfor != LOOKFOR_TERM
8100 && theline[0] == '{')
8101 {
8102 amount = cur_amount;
8103 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008104 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 * doesn't start with a '{', which must have a match
8106 * in the same line (scope is the same). Probably:
8107 * { 1, 2 },
8108 * -> { 3, 4 }
8109 */
8110 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008111 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008113 if (curbuf->b_ind_cpp_baseclass)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
8115 /* have to look back, whether it is a cpp base
8116 * class declaration or initialization */
8117 lookfor = LOOKFOR_CPP_BASECLASS;
8118 continue;
8119 }
8120 break;
8121 }
8122
8123 /*
8124 * Check if we are after an "if", "while", etc.
8125 * Also allow " } else".
8126 */
8127 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8128 {
8129 /*
8130 * Found an unterminated line after an if (), line up
8131 * with the last one.
8132 * if (cond)
8133 * 100 +
8134 * -> here;
8135 */
8136 if (lookfor == LOOKFOR_UNTERM
8137 || lookfor == LOOKFOR_ENUM_OR_INIT)
8138 {
8139 if (cont_amount > 0)
8140 amount = cont_amount;
8141 else
8142 amount += ind_continuation;
8143 break;
8144 }
8145
8146 /*
8147 * If this is just above the line we are indenting, we
8148 * are finished.
8149 * while (not)
8150 * -> here;
8151 * Otherwise this indent can be used when the line
8152 * before this is terminated.
8153 * yyy;
8154 * if (stat)
8155 * while (not)
8156 * xxx;
8157 * -> here;
8158 */
8159 amount = cur_amount;
8160 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008161 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 if (lookfor != LOOKFOR_TERM)
8163 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008164 amount += curbuf->b_ind_level
8165 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 break;
8167 }
8168
8169 /*
8170 * Special trick: when expecting the while () after a
8171 * do, line up with the while()
8172 * do
8173 * x = 1;
8174 * -> here
8175 */
8176 l = skipwhite(ml_get_curline());
8177 if (cin_isdo(l))
8178 {
8179 if (whilelevel == 0)
8180 break;
8181 --whilelevel;
8182 }
8183
8184 /*
8185 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008186 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 * Need to use the scope of this "else". XXX
8188 * If whilelevel != 0 continue looking for a "do {".
8189 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008190 if (cin_iselse(l) && whilelevel == 0)
8191 {
8192 /* If we're looking at "} else", let's make sure we
8193 * find the opening brace of the enclosing scope,
8194 * not the one from "if () {". */
8195 if (*l == '}')
8196 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008197 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008198
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008199 if ((trypos = find_start_brace()) == NULL
8200 || find_match(LOOKFOR_IF, trypos->lnum)
8201 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008202 break;
8203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 }
8205
8206 /*
8207 * If we're below an unterminated line that is not an
8208 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008209 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 * the line before this one.
8211 */
8212 else
8213 {
8214 /*
8215 * Found two unterminated lines on a row, line up with
8216 * the last one.
8217 * c = 99 +
8218 * 100 +
8219 * -> here;
8220 */
8221 if (lookfor == LOOKFOR_UNTERM)
8222 {
8223 /* When line ends in a comma add extra indent */
8224 if (terminated == ',')
8225 amount += ind_continuation;
8226 break;
8227 }
8228
8229 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8230 {
8231 /* Found two lines ending in ',', lineup with the
8232 * lowest one, but check for cpp base class
8233 * declaration/initialization, if it is an
8234 * opening brace or we are looking just for
8235 * enumerations/initializations. */
8236 if (terminated == ',')
8237 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008238 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 break;
8240
8241 lookfor = LOOKFOR_CPP_BASECLASS;
8242 continue;
8243 }
8244
8245 /* Ignore unterminated lines in between, but
8246 * reduce indent. */
8247 if (amount > cur_amount)
8248 amount = cur_amount;
8249 }
8250 else
8251 {
8252 /*
8253 * Found first unterminated line on a row, may
8254 * line up with this line, remember its indent
8255 * 100 +
8256 * -> here;
8257 */
8258 amount = cur_amount;
8259
8260 /*
8261 * If previous line ends in ',', check whether we
8262 * are in an initialization or enum
8263 * struct xxx =
8264 * {
8265 * sizeof a,
8266 * 124 };
8267 * or a normal possible continuation line.
8268 * but only, of no other statement has been found
8269 * yet.
8270 */
8271 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8272 {
8273 lookfor = LOOKFOR_ENUM_OR_INIT;
8274 cont_amount = cin_first_id_amount();
8275 }
8276 else
8277 {
8278 if (lookfor == LOOKFOR_INITIAL
8279 && *l != NUL
8280 && l[STRLEN(l) - 1] == '\\')
8281 /* XXX */
8282 cont_amount = cin_get_equal_amount(
8283 curwin->w_cursor.lnum);
8284 if (lookfor != LOOKFOR_TERM)
8285 lookfor = LOOKFOR_UNTERM;
8286 }
8287 }
8288 }
8289 }
8290
8291 /*
8292 * Check if we are after a while (cond);
8293 * If so: Ignore until the matching "do".
8294 */
8295 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008296 else if (cin_iswhileofdo_end(terminated))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {
8298 /*
8299 * Found an unterminated line after a while ();, line up
8300 * with the last one.
8301 * while (cond);
8302 * 100 + <- line up with this one
8303 * -> here;
8304 */
8305 if (lookfor == LOOKFOR_UNTERM
8306 || lookfor == LOOKFOR_ENUM_OR_INIT)
8307 {
8308 if (cont_amount > 0)
8309 amount = cont_amount;
8310 else
8311 amount += ind_continuation;
8312 break;
8313 }
8314
8315 if (whilelevel == 0)
8316 {
8317 lookfor = LOOKFOR_TERM;
8318 amount = get_indent(); /* XXX */
8319 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008320 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
8322 ++whilelevel;
8323 }
8324
8325 /*
8326 * We are after a "normal" statement.
8327 * If we had another statement we can stop now and use the
8328 * indent of that other statement.
8329 * Otherwise the indent of the current statement may be used,
8330 * search backwards for the next "normal" statement.
8331 */
8332 else
8333 {
8334 /*
8335 * Skip single break line, if before a switch label. It
8336 * may be lined up with the case label.
8337 */
8338 if (lookfor == LOOKFOR_NOBREAK
8339 && cin_isbreak(skipwhite(ml_get_curline())))
8340 {
8341 lookfor = LOOKFOR_ANY;
8342 continue;
8343 }
8344
8345 /*
8346 * Handle "do {" line.
8347 */
8348 if (whilelevel > 0)
8349 {
8350 l = cin_skipcomment(ml_get_curline());
8351 if (cin_isdo(l))
8352 {
8353 amount = get_indent(); /* XXX */
8354 --whilelevel;
8355 continue;
8356 }
8357 }
8358
8359 /*
8360 * Found a terminated line above an unterminated line. Add
8361 * the amount for a continuation line.
8362 * x = 1;
8363 * y = foo +
8364 * -> here;
8365 * or
8366 * int x = 1;
8367 * int foo,
8368 * -> here;
8369 */
8370 if (lookfor == LOOKFOR_UNTERM
8371 || lookfor == LOOKFOR_ENUM_OR_INIT)
8372 {
8373 if (cont_amount > 0)
8374 amount = cont_amount;
8375 else
8376 amount += ind_continuation;
8377 break;
8378 }
8379
8380 /*
8381 * Found a terminated line above a terminated line or "if"
8382 * etc. line. Use the amount of the line below us.
8383 * x = 1; x = 1;
8384 * if (asdf) y = 2;
8385 * while (asdf) ->here;
8386 * here;
8387 * ->foo;
8388 */
8389 if (lookfor == LOOKFOR_TERM)
8390 {
8391 if (!lookfor_break && whilelevel == 0)
8392 break;
8393 }
8394
8395 /*
8396 * First line above the one we're indenting is terminated.
8397 * To know what needs to be done look further backward for
8398 * a terminated line.
8399 */
8400 else
8401 {
8402 /*
8403 * position the cursor over the rightmost paren, so
8404 * that matching it will take us back to the start of
8405 * the line. Helps for:
8406 * func(asdr,
8407 * asdfasdf);
8408 * here;
8409 */
8410term_again:
8411 l = ml_get_curline();
8412 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008413 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008414 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {
8416 /*
8417 * Check if we are on a case label now. This is
8418 * handled above.
8419 * case xx: if ( asdf &&
8420 * asdf)
8421 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008422 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008424 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {
8426 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008427 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 continue;
8429 }
8430 }
8431
8432 /* When aligning with the case statement, don't align
8433 * with a statement after it.
8434 * case 1: { <-- don't use this { position
8435 * stat;
8436 * }
8437 * case 2:
8438 * stat;
8439 * }
8440 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008441 iscase = (curbuf->b_ind_keep_case_label
8442 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443
8444 /*
8445 * Get indent and pointer to text for current line,
8446 * ignoring any jump label.
8447 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008448 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449
8450 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008451 amount += curbuf->b_ind_open_extra;
8452 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008453 l = skipwhite(l);
8454 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008455 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8457
8458 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008459 * When a terminated line starts with "else" skip to
8460 * the matching "if":
8461 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008462 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008463 * Need to use the scope of this "else". XXX
8464 * If whilelevel != 0 continue looking for a "do {".
8465 */
8466 if (lookfor == LOOKFOR_TERM
8467 && *l != '}'
8468 && cin_iselse(l)
8469 && whilelevel == 0)
8470 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008471 if ((trypos = find_start_brace()) == NULL
8472 || find_match(LOOKFOR_IF, trypos->lnum)
8473 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008474 break;
8475 continue;
8476 }
8477
8478 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 * If we're at the end of a block, skip to the start of
8480 * that block.
8481 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008482 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008483 if (find_last_paren(l, '{', '}') /* XXX */
8484 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008486 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 /* if not "else {" check for terminated again */
8488 /* but skip block for "} else {" */
8489 l = cin_skipcomment(ml_get_curline());
8490 if (*l == '}' || !cin_iselse(l))
8491 goto term_again;
8492 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008493 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495 }
8496 }
8497 }
8498 }
8499 }
8500
8501 /* add extra indent for a comment */
8502 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008503 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008504
8505 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008506 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8507 amount -= curbuf->b_ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
8509
8510 /*
8511 * ok -- we're not inside any sort of structure at all!
8512 *
8513 * this means we're at the top level, and everything should
8514 * basically just match where the previous line is, except
8515 * for the lines immediately following a function declaration,
8516 * which are K&R-style parameters and need to be indented.
8517 */
8518 else
8519 {
8520 /*
8521 * if our line starts with an open brace, forget about any
8522 * prevailing indent and make sure it looks like the start
8523 * of a function
8524 */
8525
8526 if (theline[0] == '{')
8527 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008528 amount = curbuf->b_ind_first_open;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 }
8530
8531 /*
8532 * If the NEXT line is a function declaration, the current
8533 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008534 * Don't do this if the current line looks like a comment or if the
8535 * current line is terminated, ie. ends in ';', or if the current line
8536 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 */
8538 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8539 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008540 && vim_strchr(theline, '{') == NULL
8541 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 && !cin_ends_in(theline, (char_u *)":", NULL)
8543 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008544 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008545 cur_curpos.lnum + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 && !cin_isterminated(theline, FALSE, TRUE))
8547 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008548 amount = curbuf->b_ind_func_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 }
8550 else
8551 {
8552 amount = 0;
8553 curwin->w_cursor = cur_curpos;
8554
8555 /* search backwards until we find something we recognize */
8556
8557 while (curwin->w_cursor.lnum > 1)
8558 {
8559 curwin->w_cursor.lnum--;
8560 curwin->w_cursor.col = 0;
8561
8562 l = ml_get_curline();
8563
8564 /*
8565 * If we're in a comment now, skip to the start of the comment.
8566 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008567 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {
8569 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008570 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 continue;
8572 }
8573
8574 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008575 * Are we at the start of a cpp base class declaration or
8576 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008578 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008579 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar18144c82006-04-12 21:52:12 +00008580 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008581 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008582 l = ml_get_curline();
8583 }
8584 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008586 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008587 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 break;
8589 }
8590
8591 /*
8592 * Skip preprocessor directives and blank lines.
8593 */
8594 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8595 continue;
8596
8597 if (cin_nocode(l))
8598 continue;
8599
8600 /*
8601 * If the previous line ends in ',', use one level of
8602 * indentation:
8603 * int foo,
8604 * bar;
8605 * do this before checking for '}' in case of eg.
8606 * enum foobar
8607 * {
8608 * ...
8609 * } foo,
8610 * bar;
8611 */
8612 n = 0;
8613 if (cin_ends_in(l, (char_u *)",", NULL)
8614 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8615 {
8616 /* take us back to opening paren */
8617 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008618 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008619 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008620 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621
8622 /* For a line ending in ',' that is a continuation line go
8623 * back to the first line with a backslash:
8624 * char *foo = "bla\
8625 * bla",
8626 * here;
8627 */
8628 while (n == 0 && curwin->w_cursor.lnum > 1)
8629 {
8630 l = ml_get(curwin->w_cursor.lnum - 1);
8631 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8632 break;
8633 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008634 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636
8637 amount = get_indent(); /* XXX */
8638
8639 if (amount == 0)
8640 amount = cin_first_id_amount();
8641 if (amount == 0)
8642 amount = ind_continuation;
8643 break;
8644 }
8645
8646 /*
8647 * If the line looks like a function declaration, and we're
8648 * not in a comment, put it the left margin.
8649 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008650 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 break;
8652 l = ml_get_curline();
8653
8654 /*
8655 * Finding the closing '}' of a previous function. Put
8656 * current line at the left margin. For when 'cino' has "fs".
8657 */
8658 if (*skipwhite(l) == '}')
8659 break;
8660
8661 /* (matching {)
8662 * If the previous line ends on '};' (maybe followed by
8663 * comments) align at column 0. For example:
8664 * char *string_array[] = { "foo",
8665 * / * x * / "b};ar" }; / * foobar * /
8666 */
8667 if (cin_ends_in(l, (char_u *)"};", NULL))
8668 break;
8669
8670 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008671 * Find a line only has a semicolon that belongs to a previous
8672 * line ending in '}', e.g. before an #endif. Don't increase
8673 * indent then.
8674 */
8675 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8676 {
8677 pos_T curpos_save = curwin->w_cursor;
8678
8679 while (curwin->w_cursor.lnum > 1)
8680 {
8681 look = ml_get(--curwin->w_cursor.lnum);
8682 if (!(cin_nocode(look) || cin_ispreproc_cont(
8683 &look, &curwin->w_cursor.lnum)))
8684 break;
8685 }
8686 if (curwin->w_cursor.lnum > 0
8687 && cin_ends_in(look, (char_u *)"}", NULL))
8688 break;
8689
8690 curwin->w_cursor = curpos_save;
8691 }
8692
8693 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 * If the PREVIOUS line is a function declaration, the current
8695 * line (and the ones that follow) needs to be indented as
8696 * parameters.
8697 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008698 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008700 amount = curbuf->b_ind_param;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 break;
8702 }
8703
8704 /*
8705 * If the previous line ends in ';' and the line before the
8706 * previous line ends in ',' or '\', ident to column zero:
8707 * int foo,
8708 * bar;
8709 * indent_to_0 here;
8710 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008711 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
8713 l = ml_get(curwin->w_cursor.lnum - 1);
8714 if (cin_ends_in(l, (char_u *)",", NULL)
8715 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8716 break;
8717 l = ml_get_curline();
8718 }
8719
8720 /*
8721 * Doesn't look like anything interesting -- so just
8722 * use the indent of this line.
8723 *
8724 * Position the cursor over the rightmost paren, so that
8725 * matching it will take us back to the start of the line.
8726 */
8727 find_last_paren(l, '(', ')');
8728
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008729 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008730 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 amount = get_indent(); /* XXX */
8732 break;
8733 }
8734
8735 /* add extra indent for a comment */
8736 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008737 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
8739 /* add extra indent if the previous line ended in a backslash:
8740 * "asdfasdf\
8741 * here";
8742 * char *foo = "asdf\
8743 * here";
8744 */
8745 if (cur_curpos.lnum > 1)
8746 {
8747 l = ml_get(cur_curpos.lnum - 1);
8748 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8749 {
8750 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8751 if (cur_amount > 0)
8752 amount = cur_amount;
8753 else if (cur_amount == 0)
8754 amount += ind_continuation;
8755 }
8756 }
8757 }
8758 }
8759
8760theend:
8761 /* put the cursor back where it belongs */
8762 curwin->w_cursor = cur_curpos;
8763
8764 vim_free(linecopy);
8765
8766 if (amount < 0)
8767 return 0;
8768 return amount;
8769}
8770
8771 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008772find_match(lookfor, ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 int lookfor;
8774 linenr_T ourscope;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775{
8776 char_u *look;
8777 pos_T *theirscope;
8778 char_u *mightbeif;
8779 int elselevel;
8780 int whilelevel;
8781
8782 if (lookfor == LOOKFOR_IF)
8783 {
8784 elselevel = 1;
8785 whilelevel = 0;
8786 }
8787 else
8788 {
8789 elselevel = 0;
8790 whilelevel = 1;
8791 }
8792
8793 curwin->w_cursor.col = 0;
8794
8795 while (curwin->w_cursor.lnum > ourscope + 1)
8796 {
8797 curwin->w_cursor.lnum--;
8798 curwin->w_cursor.col = 0;
8799
8800 look = cin_skipcomment(ml_get_curline());
8801 if (cin_iselse(look)
8802 || cin_isif(look)
8803 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008804 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 {
8806 /*
8807 * if we've gone outside the braces entirely,
8808 * we must be out of scope...
8809 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008810 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 if (theirscope == NULL)
8812 break;
8813
8814 /*
8815 * and if the brace enclosing this is further
8816 * back than the one enclosing the else, we're
8817 * out of luck too.
8818 */
8819 if (theirscope->lnum < ourscope)
8820 break;
8821
8822 /*
8823 * and if they're enclosed in a *deeper* brace,
8824 * then we can ignore it because it's in a
8825 * different scope...
8826 */
8827 if (theirscope->lnum > ourscope)
8828 continue;
8829
8830 /*
8831 * if it was an "else" (that's not an "else if")
8832 * then we need to go back to another if, so
8833 * increment elselevel
8834 */
8835 look = cin_skipcomment(ml_get_curline());
8836 if (cin_iselse(look))
8837 {
8838 mightbeif = cin_skipcomment(look + 4);
8839 if (!cin_isif(mightbeif))
8840 ++elselevel;
8841 continue;
8842 }
8843
8844 /*
8845 * if it was a "while" then we need to go back to
8846 * another "do", so increment whilelevel. XXX
8847 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008848 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 {
8850 ++whilelevel;
8851 continue;
8852 }
8853
8854 /* If it's an "if" decrement elselevel */
8855 look = cin_skipcomment(ml_get_curline());
8856 if (cin_isif(look))
8857 {
8858 elselevel--;
8859 /*
8860 * When looking for an "if" ignore "while"s that
8861 * get in the way.
8862 */
8863 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8864 whilelevel = 0;
8865 }
8866
8867 /* If it's a "do" decrement whilelevel */
8868 if (cin_isdo(look))
8869 whilelevel--;
8870
8871 /*
8872 * if we've used up all the elses, then
8873 * this must be the if that we want!
8874 * match the indent level of that if.
8875 */
8876 if (elselevel <= 0 && whilelevel <= 0)
8877 {
8878 return OK;
8879 }
8880 }
8881 }
8882 return FAIL;
8883}
8884
8885# if defined(FEAT_EVAL) || defined(PROTO)
8886/*
8887 * Get indent level from 'indentexpr'.
8888 */
8889 int
8890get_expr_indent()
8891{
8892 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008893 pos_T save_pos;
8894 colnr_T save_curswant;
8895 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008897 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8898 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008900 /* Save and restore cursor position and curswant, in case it was changed
8901 * via :normal commands */
8902 save_pos = curwin->w_cursor;
8903 save_curswant = curwin->w_curswant;
8904 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008906 if (use_sandbox)
8907 ++sandbox;
8908 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008910 if (use_sandbox)
8911 --sandbox;
8912 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
8914 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8915 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8916 * command. */
8917 save_State = State;
8918 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008919 curwin->w_cursor = save_pos;
8920 curwin->w_curswant = save_curswant;
8921 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 check_cursor();
8923 State = save_State;
8924
8925 /* If there is an error, just keep the current indent. */
8926 if (indent < 0)
8927 indent = get_indent();
8928
8929 return indent;
8930}
8931# endif
8932
8933#endif /* FEAT_CINDENT */
8934
8935#if defined(FEAT_LISP) || defined(PROTO)
8936
8937static int lisp_match __ARGS((char_u *p));
8938
8939 static int
8940lisp_match(p)
8941 char_u *p;
8942{
8943 char_u buf[LSIZE];
8944 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01008945 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946
8947 while (*word != NUL)
8948 {
8949 (void)copy_option_part(&word, buf, LSIZE, ",");
8950 len = (int)STRLEN(buf);
8951 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8952 return TRUE;
8953 }
8954 return FALSE;
8955}
8956
8957/*
8958 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8959 * The incompatible newer method is quite a bit better at indenting
8960 * code in lisp-like languages than the traditional one; it's still
8961 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8962 *
8963 * TODO:
8964 * Findmatch() should be adapted for lisp, also to make showmatch
8965 * work correctly: now (v5.3) it seems all C/C++ oriented:
8966 * - it does not recognize the #\( and #\) notations as character literals
8967 * - it doesn't know about comments starting with a semicolon
8968 * - it incorrectly interprets '(' as a character literal
8969 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008970 * Update from Sergey Khorev:
8971 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 */
8973 int
8974get_lisp_indent()
8975{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008976 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 int amount;
8978 char_u *that;
8979 colnr_T col;
8980 colnr_T firsttry;
8981 int parencount, quotecount;
8982 int vi_lisp;
8983
8984 /* Set vi_lisp to use the vi-compatible method */
8985 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8986
8987 realpos = curwin->w_cursor;
8988 curwin->w_cursor.col = 0;
8989
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008990 if ((pos = findmatch(NULL, '(')) == NULL)
8991 pos = findmatch(NULL, '[');
8992 else
8993 {
8994 paren = *pos;
8995 pos = findmatch(NULL, '[');
8996 if (pos == NULL || ltp(pos, &paren))
8997 pos = &paren;
8998 }
8999 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 {
9001 /* Extra trick: Take the indent of the first previous non-white
9002 * line that is at the same () level. */
9003 amount = -1;
9004 parencount = 0;
9005
9006 while (--curwin->w_cursor.lnum >= pos->lnum)
9007 {
9008 if (linewhite(curwin->w_cursor.lnum))
9009 continue;
9010 for (that = ml_get_curline(); *that != NUL; ++that)
9011 {
9012 if (*that == ';')
9013 {
9014 while (*(that + 1) != NUL)
9015 ++that;
9016 continue;
9017 }
9018 if (*that == '\\')
9019 {
9020 if (*(that + 1) != NUL)
9021 ++that;
9022 continue;
9023 }
9024 if (*that == '"' && *(that + 1) != NUL)
9025 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009026 while (*++that && *that != '"')
9027 {
9028 /* skipping escaped characters in the string */
9029 if (*that == '\\')
9030 {
9031 if (*++that == NUL)
9032 break;
9033 if (that[1] == NUL)
9034 {
9035 ++that;
9036 break;
9037 }
9038 }
9039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009041 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009043 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 --parencount;
9045 }
9046 if (parencount == 0)
9047 {
9048 amount = get_indent();
9049 break;
9050 }
9051 }
9052
9053 if (amount == -1)
9054 {
9055 curwin->w_cursor.lnum = pos->lnum;
9056 curwin->w_cursor.col = pos->col;
9057 col = pos->col;
9058
9059 that = ml_get_curline();
9060
9061 if (vi_lisp && get_indent() == 0)
9062 amount = 2;
9063 else
9064 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009065 char_u *line = that;
9066
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 amount = 0;
9068 while (*that && col)
9069 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009070 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 col--;
9072 }
9073
9074 /*
9075 * Some keywords require "body" indenting rules (the
9076 * non-standard-lisp ones are Scheme special forms):
9077 *
9078 * (let ((a 1)) instead (let ((a 1))
9079 * (...)) of (...))
9080 */
9081
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009082 if (!vi_lisp && (*that == '(' || *that == '[')
9083 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 amount += 2;
9085 else
9086 {
9087 that++;
9088 amount++;
9089 firsttry = amount;
9090
9091 while (vim_iswhite(*that))
9092 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009093 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 ++that;
9095 }
9096
9097 if (*that && *that != ';') /* not a comment line */
9098 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009099 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009101 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 firsttry++;
9103
9104 parencount = 0;
9105 quotecount = 0;
9106
9107 if (vi_lisp
9108 || (*that != '"'
9109 && *that != '\''
9110 && *that != '#'
9111 && (*that < '0' || *that > '9')))
9112 {
9113 while (*that
9114 && (!vim_iswhite(*that)
9115 || quotecount
9116 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009117 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 && !quotecount
9119 && !parencount
9120 && vi_lisp)))
9121 {
9122 if (*that == '"')
9123 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009124 if ((*that == '(' || *that == '[')
9125 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009127 if ((*that == ')' || *that == ']')
9128 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 --parencount;
9130 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009131 amount += lbr_chartabsize_adv(
9132 line, &that, (colnr_T)amount);
9133 amount += lbr_chartabsize_adv(
9134 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 }
9136 }
9137 while (vim_iswhite(*that))
9138 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009139 amount += lbr_chartabsize(
9140 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 that++;
9142 }
9143 if (!*that || *that == ';')
9144 amount = firsttry;
9145 }
9146 }
9147 }
9148 }
9149 }
9150 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009151 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152
9153 curwin->w_cursor = realpos;
9154
9155 return amount;
9156}
9157#endif /* FEAT_LISP */
9158
9159 void
9160prepare_to_exit()
9161{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009162#if defined(SIGHUP) && defined(SIG_IGN)
9163 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9164 * makes Vim exit and then handling SIGHUP causes various reentrance
9165 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009166 signal(SIGHUP, SIG_IGN);
9167#endif
9168
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169#ifdef FEAT_GUI
9170 if (gui.in_use)
9171 {
9172 gui.dying = TRUE;
9173 out_trash(); /* trash any pending output */
9174 }
9175 else
9176#endif
9177 {
9178 windgoto((int)Rows - 1, 0);
9179
9180 /*
9181 * Switch terminal mode back now, so messages end up on the "normal"
9182 * screen (if there are two screens).
9183 */
9184 settmode(TMODE_COOK);
9185#ifdef WIN3264
9186 if (can_end_termcap_mode(FALSE) == TRUE)
9187#endif
9188 stoptermcap();
9189 out_flush();
9190 }
9191}
9192
9193/*
9194 * Preserve files and exit.
9195 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009196 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9197 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 */
9199 void
9200preserve_exit()
9201{
9202 buf_T *buf;
9203
9204 prepare_to_exit();
9205
Bram Moolenaar4770d092006-01-12 23:22:24 +00009206 /* Setting this will prevent free() calls. That avoids calling free()
9207 * recursively when free() was invoked with a bad pointer. */
9208 really_exiting = TRUE;
9209
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 out_str(IObuff);
9211 screen_start(); /* don't know where cursor is now */
9212 out_flush();
9213
9214 ml_close_notmod(); /* close all not-modified buffers */
9215
9216 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9217 {
9218 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9219 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009220 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 screen_start(); /* don't know where cursor is now */
9222 out_flush();
9223 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9224 break;
9225 }
9226 }
9227
9228 ml_close_all(FALSE); /* close all memfiles, without deleting */
9229
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009230 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231
9232 getout(1);
9233}
9234
9235/*
9236 * return TRUE if "fname" exists.
9237 */
9238 int
9239vim_fexists(fname)
9240 char_u *fname;
9241{
9242 struct stat st;
9243
9244 if (mch_stat((char *)fname, &st))
9245 return FALSE;
9246 return TRUE;
9247}
9248
9249/*
9250 * Check for CTRL-C pressed, but only once in a while.
9251 * Should be used instead of ui_breakcheck() for functions that check for
9252 * each line in the file. Calling ui_breakcheck() each time takes too much
9253 * time, because it can be a system call.
9254 */
9255
9256#ifndef BREAKCHECK_SKIP
9257# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9258# define BREAKCHECK_SKIP 200
9259# else
9260# define BREAKCHECK_SKIP 32
9261# endif
9262#endif
9263
9264static int breakcheck_count = 0;
9265
9266 void
9267line_breakcheck()
9268{
9269 if (++breakcheck_count >= BREAKCHECK_SKIP)
9270 {
9271 breakcheck_count = 0;
9272 ui_breakcheck();
9273 }
9274}
9275
9276/*
9277 * Like line_breakcheck() but check 10 times less often.
9278 */
9279 void
9280fast_breakcheck()
9281{
9282 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9283 {
9284 breakcheck_count = 0;
9285 ui_breakcheck();
9286 }
9287}
9288
9289/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009290 * Invoke expand_wildcards() for one pattern.
9291 * Expand items like "%:h" before the expansion.
9292 * Returns OK or FAIL.
9293 */
9294 int
9295expand_wildcards_eval(pat, num_file, file, flags)
9296 char_u **pat; /* pointer to input pattern */
9297 int *num_file; /* resulting number of files */
9298 char_u ***file; /* array of resulting files */
9299 int flags; /* EW_DIR, etc. */
9300{
9301 int ret = FAIL;
9302 char_u *eval_pat = NULL;
9303 char_u *exp_pat = *pat;
9304 char_u *ignored_msg;
9305 int usedlen;
9306
9307 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9308 {
9309 ++emsg_off;
9310 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9311 NULL, &ignored_msg, NULL);
9312 --emsg_off;
9313 if (eval_pat != NULL)
9314 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9315 }
9316
9317 if (exp_pat != NULL)
9318 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9319
9320 if (eval_pat != NULL)
9321 {
9322 vim_free(exp_pat);
9323 vim_free(eval_pat);
9324 }
9325
9326 return ret;
9327}
9328
9329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9331 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009332 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 */
9334 int
9335expand_wildcards(num_pat, pat, num_file, file, flags)
9336 int num_pat; /* number of input patterns */
9337 char_u **pat; /* array of input patterns */
9338 int *num_file; /* resulting number of files */
9339 char_u ***file; /* array of resulting files */
9340 int flags; /* EW_DIR, etc. */
9341{
9342 int retval;
9343 int i, j;
9344 char_u *p;
9345 int non_suf_match; /* number without matching suffix */
9346
9347 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9348
9349 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009350 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 return retval;
9352
9353#ifdef FEAT_WILDIGN
9354 /*
9355 * Remove names that match 'wildignore'.
9356 */
9357 if (*p_wig)
9358 {
9359 char_u *ffname;
9360
9361 /* check all files in (*file)[] */
9362 for (i = 0; i < *num_file; ++i)
9363 {
9364 ffname = FullName_save((*file)[i], FALSE);
9365 if (ffname == NULL) /* out of memory */
9366 break;
9367# ifdef VMS
9368 vms_remove_version(ffname);
9369# endif
9370 if (match_file_list(p_wig, (*file)[i], ffname))
9371 {
9372 /* remove this matching file from the list */
9373 vim_free((*file)[i]);
9374 for (j = i; j + 1 < *num_file; ++j)
9375 (*file)[j] = (*file)[j + 1];
9376 --*num_file;
9377 --i;
9378 }
9379 vim_free(ffname);
9380 }
9381 }
9382#endif
9383
9384 /*
9385 * Move the names where 'suffixes' match to the end.
9386 */
9387 if (*num_file > 1)
9388 {
9389 non_suf_match = 0;
9390 for (i = 0; i < *num_file; ++i)
9391 {
9392 if (!match_suffix((*file)[i]))
9393 {
9394 /*
9395 * Move the name without matching suffix to the front
9396 * of the list.
9397 */
9398 p = (*file)[i];
9399 for (j = i; j > non_suf_match; --j)
9400 (*file)[j] = (*file)[j - 1];
9401 (*file)[non_suf_match++] = p;
9402 }
9403 }
9404 }
9405
9406 return retval;
9407}
9408
9409/*
9410 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9411 */
9412 int
9413match_suffix(fname)
9414 char_u *fname;
9415{
9416 int fnamelen, setsuflen;
9417 char_u *setsuf;
9418#define MAXSUFLEN 30 /* maximum length of a file suffix */
9419 char_u suf_buf[MAXSUFLEN];
9420
9421 fnamelen = (int)STRLEN(fname);
9422 setsuflen = 0;
9423 for (setsuf = p_su; *setsuf; )
9424 {
9425 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009426 if (setsuflen == 0)
9427 {
9428 char_u *tail = gettail(fname);
9429
9430 /* empty entry: match name without a '.' */
9431 if (vim_strchr(tail, '.') == NULL)
9432 {
9433 setsuflen = 1;
9434 break;
9435 }
9436 }
9437 else
9438 {
9439 if (fnamelen >= setsuflen
9440 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9441 (size_t)setsuflen) == 0)
9442 break;
9443 setsuflen = 0;
9444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 }
9446 return (setsuflen != 0);
9447}
9448
9449#if !defined(NO_EXPANDPATH) || defined(PROTO)
9450
9451# ifdef VIM_BACKTICK
9452static int vim_backtick __ARGS((char_u *p));
9453static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9454# endif
9455
9456# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9457/*
9458 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9459 * it's shared between these systems.
9460 */
9461# if defined(DJGPP) || defined(PROTO)
9462# define _cdecl /* DJGPP doesn't have this */
9463# else
9464# ifdef __BORLANDC__
9465# define _cdecl _RTLENTRYF
9466# endif
9467# endif
9468
9469/*
9470 * comparison function for qsort in dos_expandpath()
9471 */
9472 static int _cdecl
9473pstrcmp(const void *a, const void *b)
9474{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009475 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478# ifndef WIN3264
9479 static void
9480namelowcpy(
9481 char_u *d,
9482 char_u *s)
9483{
9484# ifdef DJGPP
9485 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9486 while (*s)
9487 *d++ = *s++;
9488 else
9489# endif
9490 while (*s)
9491 *d++ = TOLOWER_LOC(*s++);
9492 *d = NUL;
9493}
9494# endif
9495
9496/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009497 * Recursively expand one path component into all matching files and/or
9498 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 * Return the number of matches found.
9500 * "path" has backslashes before chars that are not to be expanded, starting
9501 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009502 * Return the number of matches found.
9503 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 */
9505 static int
9506dos_expandpath(
9507 garray_T *gap,
9508 char_u *path,
9509 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009510 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009511 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009513 char_u *buf;
9514 char_u *path_end;
9515 char_u *p, *s, *e;
9516 int start_len = gap->ga_len;
9517 char_u *pat;
9518 regmatch_T regmatch;
9519 int starts_with_dot;
9520 int matches;
9521 int len;
9522 int starstar = FALSE;
9523 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524#ifdef WIN3264
9525 WIN32_FIND_DATA fb;
9526 HANDLE hFind = (HANDLE)0;
9527# ifdef FEAT_MBYTE
9528 WIN32_FIND_DATAW wfb;
9529 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9530# endif
9531#else
9532 struct ffblk fb;
9533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009535 int ok;
9536
9537 /* Expanding "**" may take a long time, check for CTRL-C. */
9538 if (stardepth > 0)
9539 {
9540 ui_breakcheck();
9541 if (got_int)
9542 return 0;
9543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
9545 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009546 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 if (buf == NULL)
9548 return 0;
9549
9550 /*
9551 * Find the first part in the path name that contains a wildcard or a ~1.
9552 * Copy it into buf, including the preceding characters.
9553 */
9554 p = buf;
9555 s = buf;
9556 e = NULL;
9557 path_end = path;
9558 while (*path_end != NUL)
9559 {
9560 /* May ignore a wildcard that has a backslash before it; it will
9561 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9562 if (path_end >= path + wildoff && rem_backslash(path_end))
9563 *p++ = *path_end++;
9564 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9565 {
9566 if (e != NULL)
9567 break;
9568 s = p + 1;
9569 }
9570 else if (path_end >= path + wildoff
9571 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9572 e = p;
9573#ifdef FEAT_MBYTE
9574 if (has_mbyte)
9575 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009576 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 STRNCPY(p, path_end, len);
9578 p += len;
9579 path_end += len;
9580 }
9581 else
9582#endif
9583 *p++ = *path_end++;
9584 }
9585 e = p;
9586 *e = NUL;
9587
9588 /* now we have one wildcard component between s and e */
9589 /* Remove backslashes between "wildoff" and the start of the wildcard
9590 * component. */
9591 for (p = buf + wildoff; p < s; ++p)
9592 if (rem_backslash(p))
9593 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009594 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 --e;
9596 --s;
9597 }
9598
Bram Moolenaar231334e2005-07-25 20:46:57 +00009599 /* Check for "**" between "s" and "e". */
9600 for (p = s; p < e; ++p)
9601 if (p[0] == '*' && p[1] == '*')
9602 starstar = TRUE;
9603
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 starts_with_dot = (*s == '.');
9605 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9606 if (pat == NULL)
9607 {
9608 vim_free(buf);
9609 return 0;
9610 }
9611
9612 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009613 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009614 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 regmatch.rm_ic = TRUE; /* Always ignore case */
9616 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009617 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009618 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 vim_free(pat);
9620
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009621 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 {
9623 vim_free(buf);
9624 return 0;
9625 }
9626
9627 /* remember the pattern or file name being looked for */
9628 matchname = vim_strsave(s);
9629
Bram Moolenaar231334e2005-07-25 20:46:57 +00009630 /* If "**" is by itself, this is the first time we encounter it and more
9631 * is following then find matches without any directory. */
9632 if (!didstar && stardepth < 100 && starstar && e - s == 2
9633 && *path_end == '/')
9634 {
9635 STRCPY(s, path_end + 1);
9636 ++stardepth;
9637 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9638 --stardepth;
9639 }
9640
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 /* Scan all files in the directory with "dir/ *.*" */
9642 STRCPY(s, "*.*");
9643#ifdef WIN3264
9644# ifdef FEAT_MBYTE
9645 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9646 {
9647 /* The active codepage differs from 'encoding'. Attempt using the
9648 * wide function. If it fails because it is not implemented fall back
9649 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009650 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 if (wn != NULL)
9652 {
9653 hFind = FindFirstFileW(wn, &wfb);
9654 if (hFind == INVALID_HANDLE_VALUE
9655 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9656 {
9657 vim_free(wn);
9658 wn = NULL;
9659 }
9660 }
9661 }
9662
9663 if (wn == NULL)
9664# endif
9665 hFind = FindFirstFile(buf, &fb);
9666 ok = (hFind != INVALID_HANDLE_VALUE);
9667#else
9668 /* If we are expanding wildcards we try both files and directories */
9669 ok = (findfirst((char *)buf, &fb,
9670 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9671#endif
9672
9673 while (ok)
9674 {
9675#ifdef WIN3264
9676# ifdef FEAT_MBYTE
9677 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009678 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 else
9680# endif
9681 p = (char_u *)fb.cFileName;
9682#else
9683 p = (char_u *)fb.ff_name;
9684#endif
9685 /* Ignore entries starting with a dot, unless when asked for. Accept
9686 * all entries found with "matchname". */
9687 if ((p[0] != '.' || starts_with_dot)
9688 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009689 || (regmatch.regprog != NULL
9690 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009691 || ((flags & EW_NOTWILD)
9692 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 {
9694#ifdef WIN3264
9695 STRCPY(s, p);
9696#else
9697 namelowcpy(s, p);
9698#endif
9699 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009700
9701 if (starstar && stardepth < 100)
9702 {
9703 /* For "**" in the pattern first go deeper in the tree to
9704 * find matches. */
9705 STRCPY(buf + len, "/**");
9706 STRCPY(buf + len + 3, path_end);
9707 ++stardepth;
9708 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9709 --stardepth;
9710 }
9711
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 STRCPY(buf + len, path_end);
9713 if (mch_has_exp_wildcard(path_end))
9714 {
9715 /* need to expand another component of the path */
9716 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009717 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 }
9719 else
9720 {
9721 /* no more wildcards, check if there is a match */
9722 /* remove backslashes for the remaining components only */
9723 if (*path_end != 0)
9724 backslash_halve(buf + len + 1);
9725 if (mch_getperm(buf) >= 0) /* add existing file */
9726 addfile(gap, buf, flags);
9727 }
9728 }
9729
9730#ifdef WIN3264
9731# ifdef FEAT_MBYTE
9732 if (wn != NULL)
9733 {
9734 vim_free(p);
9735 ok = FindNextFileW(hFind, &wfb);
9736 }
9737 else
9738# endif
9739 ok = FindNextFile(hFind, &fb);
9740#else
9741 ok = (findnext(&fb) == 0);
9742#endif
9743
9744 /* If no more matches and no match was used, try expanding the name
9745 * itself. Finds the long name of a short filename. */
9746 if (!ok && matchname != NULL && gap->ga_len == start_len)
9747 {
9748 STRCPY(s, matchname);
9749#ifdef WIN3264
9750 FindClose(hFind);
9751# ifdef FEAT_MBYTE
9752 if (wn != NULL)
9753 {
9754 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009755 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 if (wn != NULL)
9757 hFind = FindFirstFileW(wn, &wfb);
9758 }
9759 if (wn == NULL)
9760# endif
9761 hFind = FindFirstFile(buf, &fb);
9762 ok = (hFind != INVALID_HANDLE_VALUE);
9763#else
9764 ok = (findfirst((char *)buf, &fb,
9765 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9766#endif
9767 vim_free(matchname);
9768 matchname = NULL;
9769 }
9770 }
9771
9772#ifdef WIN3264
9773 FindClose(hFind);
9774# ifdef FEAT_MBYTE
9775 vim_free(wn);
9776# endif
9777#endif
9778 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009779 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 vim_free(matchname);
9781
9782 matches = gap->ga_len - start_len;
9783 if (matches > 0)
9784 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9785 sizeof(char_u *), pstrcmp);
9786 return matches;
9787}
9788
9789 int
9790mch_expandpath(
9791 garray_T *gap,
9792 char_u *path,
9793 int flags) /* EW_* flags */
9794{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009795 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796}
9797# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9798
Bram Moolenaar231334e2005-07-25 20:46:57 +00009799#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9800 || defined(PROTO)
9801/*
9802 * Unix style wildcard expansion code.
9803 * It's here because it's used both for Unix and Mac.
9804 */
9805static int pstrcmp __ARGS((const void *, const void *));
9806
9807 static int
9808pstrcmp(a, b)
9809 const void *a, *b;
9810{
9811 return (pathcmp(*(char **)a, *(char **)b, -1));
9812}
9813
9814/*
9815 * Recursively expand one path component into all matching files and/or
9816 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9817 * "path" has backslashes before chars that are not to be expanded, starting
9818 * at "path + wildoff".
9819 * Return the number of matches found.
9820 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9821 */
9822 int
9823unix_expandpath(gap, path, wildoff, flags, didstar)
9824 garray_T *gap;
9825 char_u *path;
9826 int wildoff;
9827 int flags; /* EW_* flags */
9828 int didstar; /* expanded "**" once already */
9829{
9830 char_u *buf;
9831 char_u *path_end;
9832 char_u *p, *s, *e;
9833 int start_len = gap->ga_len;
9834 char_u *pat;
9835 regmatch_T regmatch;
9836 int starts_with_dot;
9837 int matches;
9838 int len;
9839 int starstar = FALSE;
9840 static int stardepth = 0; /* depth for "**" expansion */
9841
9842 DIR *dirp;
9843 struct dirent *dp;
9844
9845 /* Expanding "**" may take a long time, check for CTRL-C. */
9846 if (stardepth > 0)
9847 {
9848 ui_breakcheck();
9849 if (got_int)
9850 return 0;
9851 }
9852
9853 /* make room for file name */
9854 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9855 if (buf == NULL)
9856 return 0;
9857
9858 /*
9859 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009860 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009861 * Copy it into "buf", including the preceding characters.
9862 */
9863 p = buf;
9864 s = buf;
9865 e = NULL;
9866 path_end = path;
9867 while (*path_end != NUL)
9868 {
9869 /* May ignore a wildcard that has a backslash before it; it will
9870 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9871 if (path_end >= path + wildoff && rem_backslash(path_end))
9872 *p++ = *path_end++;
9873 else if (*path_end == '/')
9874 {
9875 if (e != NULL)
9876 break;
9877 s = p + 1;
9878 }
9879 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009880 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009881 || (!p_fic && (flags & EW_ICASE)
9882 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009883 e = p;
9884#ifdef FEAT_MBYTE
9885 if (has_mbyte)
9886 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009887 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009888 STRNCPY(p, path_end, len);
9889 p += len;
9890 path_end += len;
9891 }
9892 else
9893#endif
9894 *p++ = *path_end++;
9895 }
9896 e = p;
9897 *e = NUL;
9898
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009899 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009900 /* Remove backslashes between "wildoff" and the start of the wildcard
9901 * component. */
9902 for (p = buf + wildoff; p < s; ++p)
9903 if (rem_backslash(p))
9904 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009905 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009906 --e;
9907 --s;
9908 }
9909
9910 /* Check for "**" between "s" and "e". */
9911 for (p = s; p < e; ++p)
9912 if (p[0] == '*' && p[1] == '*')
9913 starstar = TRUE;
9914
9915 /* convert the file pattern to a regexp pattern */
9916 starts_with_dot = (*s == '.');
9917 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9918 if (pat == NULL)
9919 {
9920 vim_free(buf);
9921 return 0;
9922 }
9923
9924 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009925 if (flags & EW_ICASE)
9926 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9927 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009928 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009929 if (flags & (EW_NOERROR | EW_NOTWILD))
9930 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009931 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009932 if (flags & (EW_NOERROR | EW_NOTWILD))
9933 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009934 vim_free(pat);
9935
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009936 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009937 {
9938 vim_free(buf);
9939 return 0;
9940 }
9941
9942 /* If "**" is by itself, this is the first time we encounter it and more
9943 * is following then find matches without any directory. */
9944 if (!didstar && stardepth < 100 && starstar && e - s == 2
9945 && *path_end == '/')
9946 {
9947 STRCPY(s, path_end + 1);
9948 ++stardepth;
9949 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9950 --stardepth;
9951 }
9952
9953 /* open the directory for scanning */
9954 *s = NUL;
9955 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9956
9957 /* Find all matching entries */
9958 if (dirp != NULL)
9959 {
9960 for (;;)
9961 {
9962 dp = readdir(dirp);
9963 if (dp == NULL)
9964 break;
9965 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009966 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9967 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009968 || ((flags & EW_NOTWILD)
9969 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009970 {
9971 STRCPY(s, dp->d_name);
9972 len = STRLEN(buf);
9973
9974 if (starstar && stardepth < 100)
9975 {
9976 /* For "**" in the pattern first go deeper in the tree to
9977 * find matches. */
9978 STRCPY(buf + len, "/**");
9979 STRCPY(buf + len + 3, path_end);
9980 ++stardepth;
9981 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9982 --stardepth;
9983 }
9984
9985 STRCPY(buf + len, path_end);
9986 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9987 {
9988 /* need to expand another component of the path */
9989 /* remove backslashes for the remaining components only */
9990 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9991 }
9992 else
9993 {
9994 /* no more wildcards, check if there is a match */
9995 /* remove backslashes for the remaining components only */
9996 if (*path_end != NUL)
9997 backslash_halve(buf + len + 1);
9998 if (mch_getperm(buf) >= 0) /* add existing file */
9999 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010000#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010001 size_t precomp_len = STRLEN(buf)+1;
10002 char_u *precomp_buf =
10003 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010004
Bram Moolenaar231334e2005-07-25 20:46:57 +000010005 if (precomp_buf)
10006 {
10007 mch_memmove(buf, precomp_buf, precomp_len);
10008 vim_free(precomp_buf);
10009 }
10010#endif
10011 addfile(gap, buf, flags);
10012 }
10013 }
10014 }
10015 }
10016
10017 closedir(dirp);
10018 }
10019
10020 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010021 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010022
10023 matches = gap->ga_len - start_len;
10024 if (matches > 0)
10025 qsort(((char_u **)gap->ga_data) + start_len, matches,
10026 sizeof(char_u *), pstrcmp);
10027 return matches;
10028}
10029#endif
10030
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010031#if defined(FEAT_SEARCHPATH)
10032static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10033static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010034static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10035static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010036static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10037static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10038
10039/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010040 * Moves "*psep" back to the previous path separator in "path".
10041 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010042 */
10043 static int
10044find_previous_pathsep(path, psep)
10045 char_u *path;
10046 char_u **psep;
10047{
10048 /* skip the current separator */
10049 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010050 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010051
10052 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010053 while (*psep > path)
10054 {
10055 if (vim_ispathsep(**psep))
10056 return OK;
10057 mb_ptr_back(path, *psep);
10058 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010059
10060 return FAIL;
10061}
10062
10063/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010064 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10065 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010066 */
10067 static int
10068is_unique(maybe_unique, gap, i)
10069 char_u *maybe_unique;
10070 garray_T *gap;
10071 int i;
10072{
10073 int j;
10074 int candidate_len;
10075 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010076 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010077 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010078
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010079 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010080 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010081 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010082 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010083
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010084 candidate_len = (int)STRLEN(maybe_unique);
10085 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010086 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010087 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010088
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010089 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010090 if (fnamecmp(maybe_unique, rival) == 0
10091 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010092 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010093 }
10094
Bram Moolenaar162bd912010-07-28 22:29:10 +020010095 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010096}
10097
10098/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010099 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010100 * paths are expanded to their equivalent fullpath. This includes the "."
10101 * (relative to current buffer directory) and empty path (relative to current
10102 * directory) notations.
10103 *
10104 * TODO: handle upward search (;) and path limiter (**N) notations by
10105 * expanding each into their equivalent path(s).
10106 */
10107 static void
10108expand_path_option(curdir, gap)
10109 char_u *curdir;
10110 garray_T *gap;
10111{
10112 char_u *path_option = *curbuf->b_p_path == NUL
10113 ? p_path : curbuf->b_p_path;
10114 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010115 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010116 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010117
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010118 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010119 return;
10120
10121 while (*path_option != NUL)
10122 {
10123 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10124
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010125 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010126 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010127 /* Relative to current buffer:
10128 * "/path/file" + "." -> "/path/"
10129 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010130 if (curbuf->b_ffname == NULL)
10131 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010132 p = gettail(curbuf->b_ffname);
10133 len = (int)(p - curbuf->b_ffname);
10134 if (len + (int)STRLEN(buf) >= MAXPATHL)
10135 continue;
10136 if (buf[1] == NUL)
10137 buf[len] = NUL;
10138 else
10139 STRMOVE(buf + len, buf + 2);
10140 mch_memmove(buf, curbuf->b_ffname, len);
10141 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010142 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010143 else if (buf[0] == NUL)
10144 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010145 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010146 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010147 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010148 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010149 else if (!mch_isFullName(buf))
10150 {
10151 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010152 len = (int)STRLEN(curdir);
10153 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010154 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010155 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010156 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010157 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010158 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010159 }
10160
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010161 if (ga_grow(gap, 1) == FAIL)
10162 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010163
10164# if defined(MSWIN) || defined(MSDOS)
10165 /* Avoid the path ending in a backslash, it fails when a comma is
10166 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010167 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010168 if (buf[len - 1] == '\\')
10169 buf[len - 1] = '/';
10170# endif
10171
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010172 p = vim_strsave(buf);
10173 if (p == NULL)
10174 break;
10175 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010176 }
10177
10178 vim_free(buf);
10179}
10180
10181/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010182 * Returns a pointer to the file or directory name in "fname" that matches the
10183 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010184 *
10185 * path: /foo/bar/baz
10186 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010187 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010188 */
10189 static char_u *
10190get_path_cutoff(fname, gap)
10191 char_u *fname;
10192 garray_T *gap;
10193{
10194 int i;
10195 int maxlen = 0;
10196 char_u **path_part = (char_u **)gap->ga_data;
10197 char_u *cutoff = NULL;
10198
10199 for (i = 0; i < gap->ga_len; i++)
10200 {
10201 int j = 0;
10202
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010203 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010204# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010205 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10206#endif
10207 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010208 j++;
10209 if (j > maxlen)
10210 {
10211 maxlen = j;
10212 cutoff = &fname[j];
10213 }
10214 }
10215
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010216 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010217 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010218 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010219 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010220
10221 return cutoff;
10222}
10223
10224/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010225 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10226 * that they are unique with respect to each other while conserving the part
10227 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010228 */
10229 static void
10230uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010231 garray_T *gap;
10232 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010233{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010234 int i;
10235 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010236 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010237 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010238 char_u *pat;
10239 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010240 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010241 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010242 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010243 char_u **in_curdir = NULL;
10244 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010245
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010246 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010247 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010248
10249 /*
10250 * We need to prepend a '*' at the beginning of file_pattern so that the
10251 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010252 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010253 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010254 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010255 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010256 if (file_pattern == NULL)
10257 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010258 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010259 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010260 STRCAT(file_pattern, pattern);
10261 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10262 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010263 if (pat == NULL)
10264 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010265
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010266 regmatch.rm_ic = TRUE; /* always ignore case */
10267 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10268 vim_free(pat);
10269 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010270 return;
10271
Bram Moolenaar162bd912010-07-28 22:29:10 +020010272 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010273 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010274 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010275 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010276
10277 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010278 if (in_curdir == NULL)
10279 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010280
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010281 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010282 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010283 char_u *path = fnames[i];
10284 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010285 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010286 char_u *pathsep_p;
10287 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010288
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010289 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010290 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010291 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010292 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010293 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010294
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010295 /* Shorten the filename while maintaining its uniqueness */
10296 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010297
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010298 /* we start at the end of the path */
10299 pathsep_p = path + len - 1;
10300
10301 while (find_previous_pathsep(path, &pathsep_p))
10302 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10303 && is_unique(pathsep_p + 1, gap, i)
10304 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10305 {
10306 sort_again = TRUE;
10307 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10308 break;
10309 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010310
10311 if (mch_isFullName(path))
10312 {
10313 /*
10314 * Last resort: shorten relative to curdir if possible.
10315 * 'possible' means:
10316 * 1. It is under the current directory.
10317 * 2. The result is actually shorter than the original.
10318 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010319 * Before curdir After
10320 * /foo/bar/file.txt /foo/bar ./file.txt
10321 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10322 * /file.txt / /file.txt
10323 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010324 */
10325 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010326 if (short_name != NULL && short_name > path + 1
10327#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010328 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010329 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010330 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010331 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010332 && !vim_ispathsep(*short_name)
10333#endif
10334 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010335 {
10336 STRCPY(path, ".");
10337 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010338 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010339 }
10340 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010341 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010342 }
10343
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010344 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010345 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010346 {
10347 char_u *rel_path;
10348 char_u *path = in_curdir[i];
10349
10350 if (path == NULL)
10351 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010352
10353 /* If the {filename} is not unique, change it to ./{filename}.
10354 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010355 short_name = shorten_fname(path, curdir);
10356 if (short_name == NULL)
10357 short_name = path;
10358 if (is_unique(short_name, gap, i))
10359 {
10360 STRCPY(fnames[i], short_name);
10361 continue;
10362 }
10363
10364 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10365 if (rel_path == NULL)
10366 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010367 STRCPY(rel_path, ".");
10368 add_pathsep(rel_path);
10369 STRCAT(rel_path, short_name);
10370
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010371 vim_free(fnames[i]);
10372 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010373 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010374 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010375 }
10376
Bram Moolenaar162bd912010-07-28 22:29:10 +020010377theend:
10378 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010379 if (in_curdir != NULL)
10380 {
10381 for (i = 0; i < gap->ga_len; i++)
10382 vim_free(in_curdir[i]);
10383 vim_free(in_curdir);
10384 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010385 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010386 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010387
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010388 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010389 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010390}
10391
10392/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010393 * Calls globpath() with 'path' values for the given pattern and stores the
10394 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010395 * Returns the total number of matches.
10396 */
10397 static int
10398expand_in_path(gap, pattern, flags)
10399 garray_T *gap;
10400 char_u *pattern;
10401 int flags; /* EW_* flags */
10402{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010403 char_u *curdir;
10404 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010405 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010406
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010407 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010408 return 0;
10409 mch_dirname(curdir, MAXPATHL);
10410
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010411 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010412 expand_path_option(curdir, &path_ga);
10413 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010414 if (path_ga.ga_len == 0)
10415 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010416
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010417 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010418 ga_clear_strings(&path_ga);
10419 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010420 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010421
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010422 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010423 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010424
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010425 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010426}
10427#endif
10428
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010429#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10430/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010431 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10432 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010433 */
10434 void
10435remove_duplicates(gap)
10436 garray_T *gap;
10437{
10438 int i;
10439 int j;
10440 char_u **fnames = (char_u **)gap->ga_data;
10441
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010442 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010443 for (i = gap->ga_len - 1; i > 0; --i)
10444 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10445 {
10446 vim_free(fnames[i]);
10447 for (j = i + 1; j < gap->ga_len; ++j)
10448 fnames[j - 1] = fnames[j];
10449 --gap->ga_len;
10450 }
10451}
10452#endif
10453
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010454static int has_env_var __ARGS((char_u *p));
10455
10456/*
10457 * Return TRUE if "p" contains what looks like an environment variable.
10458 * Allowing for escaping.
10459 */
10460 static int
10461has_env_var(p)
10462 char_u *p;
10463{
10464 for ( ; *p; mb_ptr_adv(p))
10465 {
10466 if (*p == '\\' && p[1] != NUL)
10467 ++p;
10468 else if (vim_strchr((char_u *)
10469#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
10470 "$%"
10471#else
10472 "$"
10473#endif
10474 , *p) != NULL)
10475 return TRUE;
10476 }
10477 return FALSE;
10478}
10479
10480#ifdef SPECIAL_WILDCHAR
10481static int has_special_wildchar __ARGS((char_u *p));
10482
10483/*
10484 * Return TRUE if "p" contains a special wildcard character.
10485 * Allowing for escaping.
10486 */
10487 static int
10488has_special_wildchar(p)
10489 char_u *p;
10490{
10491 for ( ; *p; mb_ptr_adv(p))
10492 {
10493 if (*p == '\\' && p[1] != NUL)
10494 ++p;
10495 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10496 return TRUE;
10497 }
10498 return FALSE;
10499}
10500#endif
10501
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502/*
10503 * Generic wildcard expansion code.
10504 *
10505 * Characters in "pat" that should not be expanded must be preceded with a
10506 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10507 *
10508 * Return FAIL when no single file was found. In this case "num_file" is not
10509 * set, and "file" may contain an error message.
10510 * Return OK when some files found. "num_file" is set to the number of
10511 * matches, "file" to the array of matches. Call FreeWild() later.
10512 */
10513 int
10514gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10515 int num_pat; /* number of input patterns */
10516 char_u **pat; /* array of input patterns */
10517 int *num_file; /* resulting number of files */
10518 char_u ***file; /* array of resulting files */
10519 int flags; /* EW_* flags */
10520{
10521 int i;
10522 garray_T ga;
10523 char_u *p;
10524 static int recursive = FALSE;
10525 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010526#if defined(FEAT_SEARCHPATH)
10527 int did_expand_in_path = FALSE;
10528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529
10530 /*
10531 * expand_env() is called to expand things like "~user". If this fails,
10532 * it calls ExpandOne(), which brings us back here. In this case, always
10533 * call the machine specific expansion function, if possible. Otherwise,
10534 * return FAIL.
10535 */
10536 if (recursive)
10537#ifdef SPECIAL_WILDCHAR
10538 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10539#else
10540 return FAIL;
10541#endif
10542
10543#ifdef SPECIAL_WILDCHAR
10544 /*
10545 * If there are any special wildcard characters which we cannot handle
10546 * here, call machine specific function for all the expansion. This
10547 * avoids starting the shell for each argument separately.
10548 * For `=expr` do use the internal function.
10549 */
10550 for (i = 0; i < num_pat; i++)
10551 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010552 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553# ifdef VIM_BACKTICK
10554 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10555# endif
10556 )
10557 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10558 }
10559#endif
10560
10561 recursive = TRUE;
10562
10563 /*
10564 * The matching file names are stored in a growarray. Init it empty.
10565 */
10566 ga_init2(&ga, (int)sizeof(char_u *), 30);
10567
10568 for (i = 0; i < num_pat; ++i)
10569 {
10570 add_pat = -1;
10571 p = pat[i];
10572
10573#ifdef VIM_BACKTICK
10574 if (vim_backtick(p))
10575 add_pat = expand_backtick(&ga, p, flags);
10576 else
10577#endif
10578 {
10579 /*
10580 * First expand environment variables, "~/" and "~user/".
10581 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010582 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010584 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 if (p == NULL)
10586 p = pat[i];
10587#ifdef UNIX
10588 /*
10589 * On Unix, if expand_env() can't expand an environment
10590 * variable, use the shell to do that. Discard previously
10591 * found file names and start all over again.
10592 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010593 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 {
10595 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010596 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10598 flags);
10599 recursive = FALSE;
10600 return i;
10601 }
10602#endif
10603 }
10604
10605 /*
10606 * If there are wildcards: Expand file names and add each match to
10607 * the list. If there is no match, and EW_NOTFOUND is given, add
10608 * the pattern.
10609 * If there are no wildcards: Add the file name if it exists or
10610 * when EW_NOTFOUND is given.
10611 */
10612 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010613 {
10614#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010615 if ((flags & EW_PATH)
10616 && !mch_isFullName(p)
10617 && !(p[0] == '.'
10618 && (vim_ispathsep(p[1])
10619 || (p[1] == '.' && vim_ispathsep(p[2]))))
10620 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010621 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010622 /* :find completion where 'path' is used.
10623 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010624 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010625 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010626 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010627 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010628 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010629 else
10630#endif
10631 add_pat = mch_expandpath(&ga, p, flags);
10632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 }
10634
10635 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10636 {
10637 char_u *t = backslash_halve_save(p);
10638
10639#if defined(MACOS_CLASSIC)
10640 slash_to_colon(t);
10641#endif
10642 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10643 * "vim c:/" work. */
10644 if (flags & EW_NOTFOUND)
10645 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10646 else if (mch_getperm(t) >= 0)
10647 addfile(&ga, t, flags);
10648 vim_free(t);
10649 }
10650
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010651#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010652 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010653 uniquefy_paths(&ga, p);
10654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 if (p != pat[i])
10656 vim_free(p);
10657 }
10658
10659 *num_file = ga.ga_len;
10660 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10661
10662 recursive = FALSE;
10663
10664 return (ga.ga_data != NULL) ? OK : FAIL;
10665}
10666
10667# ifdef VIM_BACKTICK
10668
10669/*
10670 * Return TRUE if we can expand this backtick thing here.
10671 */
10672 static int
10673vim_backtick(p)
10674 char_u *p;
10675{
10676 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10677}
10678
10679/*
10680 * Expand an item in `backticks` by executing it as a command.
10681 * Currently only works when pat[] starts and ends with a `.
10682 * Returns number of file names found.
10683 */
10684 static int
10685expand_backtick(gap, pat, flags)
10686 garray_T *gap;
10687 char_u *pat;
10688 int flags; /* EW_* flags */
10689{
10690 char_u *p;
10691 char_u *cmd;
10692 char_u *buffer;
10693 int cnt = 0;
10694 int i;
10695
10696 /* Create the command: lop off the backticks. */
10697 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10698 if (cmd == NULL)
10699 return 0;
10700
10701#ifdef FEAT_EVAL
10702 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010703 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 else
10705#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010706 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010707 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 vim_free(cmd);
10709 if (buffer == NULL)
10710 return 0;
10711
10712 cmd = buffer;
10713 while (*cmd != NUL)
10714 {
10715 cmd = skipwhite(cmd); /* skip over white space */
10716 p = cmd;
10717 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10718 ++p;
10719 /* add an entry if it is not empty */
10720 if (p > cmd)
10721 {
10722 i = *p;
10723 *p = NUL;
10724 addfile(gap, cmd, flags);
10725 *p = i;
10726 ++cnt;
10727 }
10728 cmd = p;
10729 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10730 ++cmd;
10731 }
10732
10733 vim_free(buffer);
10734 return cnt;
10735}
10736# endif /* VIM_BACKTICK */
10737
10738/*
10739 * Add a file to a file list. Accepted flags:
10740 * EW_DIR add directories
10741 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010742 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 * EW_NOTFOUND add even when it doesn't exist
10744 * EW_ADDSLASH add slash after directory name
10745 */
10746 void
10747addfile(gap, f, flags)
10748 garray_T *gap;
10749 char_u *f; /* filename */
10750 int flags;
10751{
10752 char_u *p;
10753 int isdir;
10754
10755 /* if the file/dir doesn't exist, may not add it */
10756 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10757 return;
10758
10759#ifdef FNAME_ILLEGAL
10760 /* if the file/dir contains illegal characters, don't add it */
10761 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10762 return;
10763#endif
10764
10765 isdir = mch_isdir(f);
10766 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10767 return;
10768
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010769 /* If the file isn't executable, may not add it. Do accept directories. */
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010770 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f, NULL))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010771 return;
10772
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 /* Make room for another item in the file list. */
10774 if (ga_grow(gap, 1) == FAIL)
10775 return;
10776
10777 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10778 if (p == NULL)
10779 return;
10780
10781 STRCPY(p, f);
10782#ifdef BACKSLASH_IN_FILENAME
10783 slash_adjust(p);
10784#endif
10785 /*
10786 * Append a slash or backslash after directory names if none is present.
10787 */
10788#ifndef DONT_ADD_PATHSEP_TO_DIR
10789 if (isdir && (flags & EW_ADDSLASH))
10790 add_pathsep(p);
10791#endif
10792 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793}
10794#endif /* !NO_EXPANDPATH */
10795
10796#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10797
10798#ifndef SEEK_SET
10799# define SEEK_SET 0
10800#endif
10801#ifndef SEEK_END
10802# define SEEK_END 2
10803#endif
10804
10805/*
10806 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010807 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
10808 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 * Returns an allocated string, or NULL for error.
10810 */
10811 char_u *
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010812get_cmd_output(cmd, infile, flags, ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010814 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 int flags; /* can be SHELL_SILENT */
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010816 int *ret_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817{
10818 char_u *tempname;
10819 char_u *command;
10820 char_u *buffer = NULL;
10821 int len;
10822 int i = 0;
10823 FILE *fd;
10824
10825 if (check_restricted() || check_secure())
10826 return NULL;
10827
10828 /* get a name for the temp file */
10829 if ((tempname = vim_tempname('o')) == NULL)
10830 {
10831 EMSG(_(e_notmp));
10832 return NULL;
10833 }
10834
10835 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010836 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 if (command == NULL)
10838 goto done;
10839
10840 /*
10841 * Call the shell to execute the command (errors are ignored).
10842 * Don't check timestamps here.
10843 */
10844 ++no_check_timestamps;
10845 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10846 --no_check_timestamps;
10847
10848 vim_free(command);
10849
10850 /*
10851 * read the names from the file into memory
10852 */
10853# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010854 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 fd = mch_fopen((char *)tempname, "r");
10856# else
10857 fd = mch_fopen((char *)tempname, READBIN);
10858# endif
10859
10860 if (fd == NULL)
10861 {
10862 EMSG2(_(e_notopen), tempname);
10863 goto done;
10864 }
10865
10866 fseek(fd, 0L, SEEK_END);
10867 len = ftell(fd); /* get size of temp file */
10868 fseek(fd, 0L, SEEK_SET);
10869
10870 buffer = alloc(len + 1);
10871 if (buffer != NULL)
10872 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10873 fclose(fd);
10874 mch_remove(tempname);
10875 if (buffer == NULL)
10876 goto done;
10877#ifdef VMS
10878 len = i; /* VMS doesn't give us what we asked for... */
10879#endif
10880 if (i != len)
10881 {
10882 EMSG2(_(e_notread), tempname);
10883 vim_free(buffer);
10884 buffer = NULL;
10885 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010886 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010887 {
10888 /* Change NUL into SOH, otherwise the string is truncated. */
10889 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020010890 if (buffer[i] == NUL)
10891 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010892
Bram Moolenaar162bd912010-07-28 22:29:10 +020010893 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010894 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010895 else
10896 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897
10898done:
10899 vim_free(tempname);
10900 return buffer;
10901}
10902#endif
10903
10904/*
10905 * Free the list of files returned by expand_wildcards() or other expansion
10906 * functions.
10907 */
10908 void
10909FreeWild(count, files)
10910 int count;
10911 char_u **files;
10912{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010913 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 return;
10915#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10916 /*
10917 * Is this still OK for when other functions than expand_wildcards() have
10918 * been used???
10919 */
10920 _fnexplodefree((char **)files);
10921#else
10922 while (count--)
10923 vim_free(files[count]);
10924 vim_free(files);
10925#endif
10926}
10927
10928/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010929 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 * Don't do this when still processing a command or a mapping.
10931 * Don't do this when inside a ":normal" command.
10932 */
10933 int
10934goto_im()
10935{
10936 return (p_im && stuff_empty() && typebuf_typed());
10937}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010938
10939/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020010940 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010941 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
10942 * - Remove any argument. E.g., "csh -f" -> "csh".
10943 * But don't allow a space in the path, so that this works:
10944 * "/usr/bin/csh --rcfile ~/.cshrc"
10945 * But don't do that for Windows, it's common to have a space in the path.
10946 */
10947 char_u *
10948get_isolated_shell_name()
10949{
10950 char_u *p;
10951
10952#ifdef WIN3264
10953 p = gettail(p_sh);
10954 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
10955#else
10956 p = skiptowhite(p_sh);
10957 if (*p == NUL)
10958 {
10959 /* No white space, use the tail. */
10960 p = vim_strsave(gettail(p_sh));
10961 }
10962 else
10963 {
10964 char_u *p1, *p2;
10965
10966 /* Find the last path separator before the space. */
10967 p1 = p_sh;
10968 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
10969 if (vim_ispathsep(*p2))
10970 p1 = p2 + 1;
10971 p = vim_strnsave(p1, (int)(p - p1));
10972 }
10973#endif
10974 return p;
10975}