blob: fbebd5aec08d19157a39c3b43dfa5a84fcdf37cb [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 */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200500 static int prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200501 int bri = 0;
502 /* window width minus window margin space, i.e. what rests for text */
503 const int eff_wwidth = W_WIDTH(wp)
504 - ((wp->w_p_nu || wp->w_p_rnu)
505 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
506 ? number_width(wp) + 1 : 0);
507
508 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200509 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
510 || prev_tick != wp->w_buffer->b_changedtick)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200511 {
512 prev_line = line;
513 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaara40aa762014-06-25 22:55:38 +0200514 prev_tick = wp->w_buffer->b_changedtick;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200515 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200516 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200517 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200518 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200519
520 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200521 if (wp->w_p_brisbr)
522 bri -= vim_strsize(p_sbr);
523
524 /* Add offset for number column, if 'n' is in 'cpoptions' */
525 bri += win_col_off2(wp);
526
527 /* never indent past left window margin */
528 if (bri < 0)
529 bri = 0;
530 /* always leave at least bri_min characters on the left,
531 * if text width is sufficient */
532 else if (bri > eff_wwidth - wp->w_p_brimin)
533 bri = (eff_wwidth - wp->w_p_brimin < 0)
534 ? 0 : eff_wwidth - wp->w_p_brimin;
535
536 return bri;
537}
538#endif
539
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
542
543static int cin_is_cinword __ARGS((char_u *line));
544
545/*
546 * Return TRUE if the string "line" starts with a word from 'cinwords'.
547 */
548 static int
549cin_is_cinword(line)
550 char_u *line;
551{
552 char_u *cinw;
553 char_u *cinw_buf;
554 int cinw_len;
555 int retval = FALSE;
556 int len;
557
558 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
559 cinw_buf = alloc((unsigned)cinw_len);
560 if (cinw_buf != NULL)
561 {
562 line = skipwhite(line);
563 for (cinw = curbuf->b_p_cinw; *cinw; )
564 {
565 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
566 if (STRNCMP(line, cinw_buf, len) == 0
567 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
568 {
569 retval = TRUE;
570 break;
571 }
572 }
573 vim_free(cinw_buf);
574 }
575 return retval;
576}
577#endif
578
579/*
580 * open_line: Add a new line below or above the current line.
581 *
582 * For VREPLACE mode, we only add a new line when we get to the end of the
583 * file, otherwise we just start replacing the next line.
584 *
585 * Caller must take care of undo. Since VREPLACE may affect any number of
586 * lines however, it may call u_save_cursor() again when starting to change a
587 * new line.
588 * "flags": OPENLINE_DELSPACES delete spaces after cursor
589 * OPENLINE_DO_COM format comments
590 * OPENLINE_KEEPTRAIL keep trailing spaces
591 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200592 * OPENLINE_COM_LIST format comments with list or 2nd line indent
593 *
594 * "second_line_indent": indent for after ^^D in Insert mode or if flag
595 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 *
597 * Return TRUE for success, FALSE for failure
598 */
599 int
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200600open_line(dir, flags, second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 int dir; /* FORWARD or BACKWARD */
602 int flags;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200603 int second_line_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
605 char_u *saved_line; /* copy of the original line */
606 char_u *next_line = NULL; /* copy of the next line */
607 char_u *p_extra = NULL; /* what goes to next line */
608 int less_cols = 0; /* less columns for mark in new line */
609 int less_cols_off = 0; /* columns to skip for mark adjust */
610 pos_T old_cursor; /* old cursor position */
611 int newcol = 0; /* new cursor column */
612 int newindent = 0; /* auto-indent of the new line */
613 int n;
614 int trunc_line = FALSE; /* truncate current line afterwards */
615 int retval = FALSE; /* return value, default is FAIL */
616#ifdef FEAT_COMMENTS
617 int extra_len = 0; /* length of p_extra string */
618 int lead_len; /* length of comment leader */
619 char_u *lead_flags; /* position in 'comments' for comment leader */
620 char_u *leader = NULL; /* copy of comment leader */
621#endif
622 char_u *allocated = NULL; /* allocated memory */
623#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
624 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
625 char_u *p;
626#endif
627 int saved_char = NUL; /* init for GCC */
628#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
629 pos_T *pos;
630#endif
631#ifdef FEAT_SMARTINDENT
632 int do_si = (!p_paste && curbuf->b_p_si
633# ifdef FEAT_CINDENT
634 && !curbuf->b_p_cin
635# endif
636 );
637 int no_si = FALSE; /* reset did_si afterwards */
638 int first_char = NUL; /* init for GCC */
639#endif
640#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
641 int vreplace_mode;
642#endif
643 int did_append; /* appended a new line */
644 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
645
646 /*
647 * make a copy of the current line so we can mess with it
648 */
649 saved_line = vim_strsave(ml_get_curline());
650 if (saved_line == NULL) /* out of memory! */
651 return FALSE;
652
653#ifdef FEAT_VREPLACE
654 if (State & VREPLACE_FLAG)
655 {
656 /*
657 * With VREPLACE we make a copy of the next line, which we will be
658 * starting to replace. First make the new line empty and let vim play
659 * with the indenting and comment leader to its heart's content. Then
660 * we grab what it ended up putting on the new line, put back the
661 * original line, and call ins_char() to put each new character onto
662 * the line, replacing what was there before and pushing the right
663 * stuff onto the replace stack. -- webb.
664 */
665 if (curwin->w_cursor.lnum < orig_line_count)
666 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
667 else
668 next_line = vim_strsave((char_u *)"");
669 if (next_line == NULL) /* out of memory! */
670 goto theend;
671
672 /*
673 * In VREPLACE mode, a NL replaces the rest of the line, and starts
674 * replacing the next line, so push all of the characters left on the
675 * line onto the replace stack. We'll push any other characters that
676 * might be replaced at the start of the next line (due to autoindent
677 * etc) a bit later.
678 */
679 replace_push(NUL); /* Call twice because BS over NL expects it */
680 replace_push(NUL);
681 p = saved_line + curwin->w_cursor.col;
682 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000683 {
684#ifdef FEAT_MBYTE
685 if (has_mbyte)
686 p += replace_push_mb(p);
687 else
688#endif
689 replace_push(*p++);
690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 saved_line[curwin->w_cursor.col] = NUL;
692 }
693#endif
694
695 if ((State & INSERT)
696#ifdef FEAT_VREPLACE
697 && !(State & VREPLACE_FLAG)
698#endif
699 )
700 {
701 p_extra = saved_line + curwin->w_cursor.col;
702#ifdef FEAT_SMARTINDENT
703 if (do_si) /* need first char after new line break */
704 {
705 p = skipwhite(p_extra);
706 first_char = *p;
707 }
708#endif
709#ifdef FEAT_COMMENTS
710 extra_len = (int)STRLEN(p_extra);
711#endif
712 saved_char = *p_extra;
713 *p_extra = NUL;
714 }
715
716 u_clearline(); /* cannot do "U" command when adding lines */
717#ifdef FEAT_SMARTINDENT
718 did_si = FALSE;
719#endif
720 ai_col = 0;
721
722 /*
723 * If we just did an auto-indent, then we didn't type anything on
724 * the prior line, and it should be truncated. Do this even if 'ai' is not
725 * set because automatically inserting a comment leader also sets did_ai.
726 */
727 if (dir == FORWARD && did_ai)
728 trunc_line = TRUE;
729
730 /*
731 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
732 * indent to use for the new line.
733 */
734 if (curbuf->b_p_ai
735#ifdef FEAT_SMARTINDENT
736 || do_si
737#endif
738 )
739 {
740 /*
741 * count white space on current line
742 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200743 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200744 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
745 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
747#ifdef FEAT_SMARTINDENT
748 /*
749 * Do smart indenting.
750 * In insert/replace mode (only when dir == FORWARD)
751 * we may move some text to the next line. If it starts with '{'
752 * don't add an indent. Fixes inserting a NL before '{' in line
753 * "if (condition) {"
754 */
755 if (!trunc_line && do_si && *saved_line != NUL
756 && (p_extra == NULL || first_char != '{'))
757 {
758 char_u *ptr;
759 char_u last_char;
760
761 old_cursor = curwin->w_cursor;
762 ptr = saved_line;
763# ifdef FEAT_COMMENTS
764 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200765 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 else
767 lead_len = 0;
768# endif
769 if (dir == FORWARD)
770 {
771 /*
772 * Skip preprocessor directives, unless they are
773 * recognised as comments.
774 */
775 if (
776# ifdef FEAT_COMMENTS
777 lead_len == 0 &&
778# endif
779 ptr[0] == '#')
780 {
781 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
782 ptr = ml_get(--curwin->w_cursor.lnum);
783 newindent = get_indent();
784 }
785# ifdef FEAT_COMMENTS
786 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200787 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 else
789 lead_len = 0;
790 if (lead_len > 0)
791 {
792 /*
793 * This case gets the following right:
794 * \*
795 * * A comment (read '\' as '/').
796 * *\
797 * #define IN_THE_WAY
798 * This should line up here;
799 */
800 p = skipwhite(ptr);
801 if (p[0] == '/' && p[1] == '*')
802 p++;
803 if (p[0] == '*')
804 {
805 for (p++; *p; p++)
806 {
807 if (p[0] == '/' && p[-1] == '*')
808 {
809 /*
810 * End of C comment, indent should line up
811 * with the line containing the start of
812 * the comment
813 */
814 curwin->w_cursor.col = (colnr_T)(p - ptr);
815 if ((pos = findmatch(NULL, NUL)) != NULL)
816 {
817 curwin->w_cursor.lnum = pos->lnum;
818 newindent = get_indent();
819 }
820 }
821 }
822 }
823 }
824 else /* Not a comment line */
825# endif
826 {
827 /* Find last non-blank in line */
828 p = ptr + STRLEN(ptr) - 1;
829 while (p > ptr && vim_iswhite(*p))
830 --p;
831 last_char = *p;
832
833 /*
834 * find the character just before the '{' or ';'
835 */
836 if (last_char == '{' || last_char == ';')
837 {
838 if (p > ptr)
839 --p;
840 while (p > ptr && vim_iswhite(*p))
841 --p;
842 }
843 /*
844 * Try to catch lines that are split over multiple
845 * lines. eg:
846 * if (condition &&
847 * condition) {
848 * Should line up here!
849 * }
850 */
851 if (*p == ')')
852 {
853 curwin->w_cursor.col = (colnr_T)(p - ptr);
854 if ((pos = findmatch(NULL, '(')) != NULL)
855 {
856 curwin->w_cursor.lnum = pos->lnum;
857 newindent = get_indent();
858 ptr = ml_get_curline();
859 }
860 }
861 /*
862 * If last character is '{' do indent, without
863 * checking for "if" and the like.
864 */
865 if (last_char == '{')
866 {
867 did_si = TRUE; /* do indent */
868 no_si = TRUE; /* don't delete it when '{' typed */
869 }
870 /*
871 * Look for "if" and the like, use 'cinwords'.
872 * Don't do this if the previous line ended in ';' or
873 * '}'.
874 */
875 else if (last_char != ';' && last_char != '}'
876 && cin_is_cinword(ptr))
877 did_si = TRUE;
878 }
879 }
880 else /* dir == BACKWARD */
881 {
882 /*
883 * Skip preprocessor directives, unless they are
884 * recognised as comments.
885 */
886 if (
887# ifdef FEAT_COMMENTS
888 lead_len == 0 &&
889# endif
890 ptr[0] == '#')
891 {
892 int was_backslashed = FALSE;
893
894 while ((ptr[0] == '#' || was_backslashed) &&
895 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
896 {
897 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
898 was_backslashed = TRUE;
899 else
900 was_backslashed = FALSE;
901 ptr = ml_get(++curwin->w_cursor.lnum);
902 }
903 if (was_backslashed)
904 newindent = 0; /* Got to end of file */
905 else
906 newindent = get_indent();
907 }
908 p = skipwhite(ptr);
909 if (*p == '}') /* if line starts with '}': do indent */
910 did_si = TRUE;
911 else /* can delete indent when '{' typed */
912 can_si_back = TRUE;
913 }
914 curwin->w_cursor = old_cursor;
915 }
916 if (do_si)
917 can_si = TRUE;
918#endif /* FEAT_SMARTINDENT */
919
920 did_ai = TRUE;
921 }
922
923#ifdef FEAT_COMMENTS
924 /*
925 * Find out if the current line starts with a comment leader.
926 * This may then be inserted in front of the new line.
927 */
928 end_comment_pending = NUL;
929 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200930 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 else
932 lead_len = 0;
933 if (lead_len > 0)
934 {
935 char_u *lead_repl = NULL; /* replaces comment leader */
936 int lead_repl_len = 0; /* length of *lead_repl */
937 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
938 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
939 char_u *comment_end = NULL; /* where lead_end has been found */
940 int extra_space = FALSE; /* append extra space */
941 int current_flag;
942 int require_blank = FALSE; /* requires blank after middle */
943 char_u *p2;
944
945 /*
946 * If the comment leader has the start, middle or end flag, it may not
947 * be used or may be replaced with the middle leader.
948 */
949 for (p = lead_flags; *p && *p != ':'; ++p)
950 {
951 if (*p == COM_BLANK)
952 {
953 require_blank = TRUE;
954 continue;
955 }
956 if (*p == COM_START || *p == COM_MIDDLE)
957 {
958 current_flag = *p;
959 if (*p == COM_START)
960 {
961 /*
962 * Doing "O" on a start of comment does not insert leader.
963 */
964 if (dir == BACKWARD)
965 {
966 lead_len = 0;
967 break;
968 }
969
970 /* find start of middle part */
971 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
972 require_blank = FALSE;
973 }
974
975 /*
976 * Isolate the strings of the middle and end leader.
977 */
978 while (*p && p[-1] != ':') /* find end of middle flags */
979 {
980 if (*p == COM_BLANK)
981 require_blank = TRUE;
982 ++p;
983 }
984 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
985
986 while (*p && p[-1] != ':') /* find end of end flags */
987 {
988 /* Check whether we allow automatic ending of comments */
989 if (*p == COM_AUTO_END)
990 end_comment_pending = -1; /* means we want to set it */
991 ++p;
992 }
993 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
994
995 if (end_comment_pending == -1) /* we can set it now */
996 end_comment_pending = lead_end[n - 1];
997
998 /*
999 * If the end of the comment is in the same line, don't use
1000 * the comment leader.
1001 */
1002 if (dir == FORWARD)
1003 {
1004 for (p = saved_line + lead_len; *p; ++p)
1005 if (STRNCMP(p, lead_end, n) == 0)
1006 {
1007 comment_end = p;
1008 lead_len = 0;
1009 break;
1010 }
1011 }
1012
1013 /*
1014 * Doing "o" on a start of comment inserts the middle leader.
1015 */
1016 if (lead_len > 0)
1017 {
1018 if (current_flag == COM_START)
1019 {
1020 lead_repl = lead_middle;
1021 lead_repl_len = (int)STRLEN(lead_middle);
1022 }
1023
1024 /*
1025 * If we have hit RETURN immediately after the start
1026 * comment leader, then put a space after the middle
1027 * comment leader on the next line.
1028 */
1029 if (!vim_iswhite(saved_line[lead_len - 1])
1030 && ((p_extra != NULL
1031 && (int)curwin->w_cursor.col == lead_len)
1032 || (p_extra == NULL
1033 && saved_line[lead_len] == NUL)
1034 || require_blank))
1035 extra_space = TRUE;
1036 }
1037 break;
1038 }
1039 if (*p == COM_END)
1040 {
1041 /*
1042 * Doing "o" on the end of a comment does not insert leader.
1043 * Remember where the end is, might want to use it to find the
1044 * start (for C-comments).
1045 */
1046 if (dir == FORWARD)
1047 {
1048 comment_end = skipwhite(saved_line);
1049 lead_len = 0;
1050 break;
1051 }
1052
1053 /*
1054 * Doing "O" on the end of a comment inserts the middle leader.
1055 * Find the string for the middle leader, searching backwards.
1056 */
1057 while (p > curbuf->b_p_com && *p != ',')
1058 --p;
1059 for (lead_repl = p; lead_repl > curbuf->b_p_com
1060 && lead_repl[-1] != ':'; --lead_repl)
1061 ;
1062 lead_repl_len = (int)(p - lead_repl);
1063
1064 /* We can probably always add an extra space when doing "O" on
1065 * the comment-end */
1066 extra_space = TRUE;
1067
1068 /* Check whether we allow automatic ending of comments */
1069 for (p2 = p; *p2 && *p2 != ':'; p2++)
1070 {
1071 if (*p2 == COM_AUTO_END)
1072 end_comment_pending = -1; /* means we want to set it */
1073 }
1074 if (end_comment_pending == -1)
1075 {
1076 /* Find last character in end-comment string */
1077 while (*p2 && *p2 != ',')
1078 p2++;
1079 end_comment_pending = p2[-1];
1080 }
1081 break;
1082 }
1083 if (*p == COM_FIRST)
1084 {
1085 /*
1086 * Comment leader for first line only: Don't repeat leader
1087 * when using "O", blank out leader when using "o".
1088 */
1089 if (dir == BACKWARD)
1090 lead_len = 0;
1091 else
1092 {
1093 lead_repl = (char_u *)"";
1094 lead_repl_len = 0;
1095 }
1096 break;
1097 }
1098 }
1099 if (lead_len)
1100 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001101 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001102 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001103 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 allocated = leader; /* remember to free it later */
1105
1106 if (leader == NULL)
1107 lead_len = 0;
1108 else
1109 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001110 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
1112 /*
1113 * Replace leader with lead_repl, right or left adjusted
1114 */
1115 if (lead_repl != NULL)
1116 {
1117 int c = 0;
1118 int off = 0;
1119
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
1122 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001123 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 else if (VIM_ISDIGIT(*p) || *p == '-')
1125 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001126 else
1127 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 if (c == COM_RIGHT) /* right adjusted leader */
1130 {
1131 /* find last non-white in the leader to line up with */
1132 for (p = leader + lead_len - 1; p > leader
1133 && vim_iswhite(*p); --p)
1134 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001136
1137#ifdef FEAT_MBYTE
1138 /* Compute the length of the replaced characters in
1139 * screen characters, not bytes. */
1140 {
1141 int repl_size = vim_strnsize(lead_repl,
1142 lead_repl_len);
1143 int old_size = 0;
1144 char_u *endp = p;
1145 int l;
1146
1147 while (old_size < repl_size && p > leader)
1148 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001149 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001150 old_size += ptr2cells(p);
1151 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001152 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001153 if (l != 0)
1154 mch_memmove(endp + l, endp,
1155 (size_t)((leader + lead_len) - endp));
1156 lead_len += l;
1157 }
1158#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (p < leader + lead_repl_len)
1160 p = leader;
1161 else
1162 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1165 if (p + lead_repl_len > leader + lead_len)
1166 p[lead_repl_len] = NUL;
1167
1168 /* blank-out any other chars from the old leader. */
1169 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001170 {
1171#ifdef FEAT_MBYTE
1172 int l = mb_head_off(leader, p);
1173
1174 if (l > 1)
1175 {
1176 p -= l;
1177 if (ptr2cells(p) > 1)
1178 {
1179 p[1] = ' ';
1180 --l;
1181 }
1182 mch_memmove(p + 1, p + l + 1,
1183 (size_t)((leader + lead_len) - (p + l + 1)));
1184 lead_len -= l;
1185 *p = ' ';
1186 }
1187 else
1188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (!vim_iswhite(*p))
1190 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 else /* left adjusted leader */
1194 {
1195 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001196#ifdef FEAT_MBYTE
1197 /* Compute the length of the replaced characters in
1198 * screen characters, not bytes. Move the part that is
1199 * not to be overwritten. */
1200 {
1201 int repl_size = vim_strnsize(lead_repl,
1202 lead_repl_len);
1203 int i;
1204 int l;
1205
1206 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1207 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001208 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001209 if (vim_strnsize(p, i + l) > repl_size)
1210 break;
1211 }
1212 if (i != lead_repl_len)
1213 {
1214 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001215 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001216 lead_len += lead_repl_len - i;
1217 }
1218 }
1219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1221
1222 /* Replace any remaining non-white chars in the old
1223 * leader by spaces. Keep Tabs, the indent must
1224 * remain the same. */
1225 for (p += lead_repl_len; p < leader + lead_len; ++p)
1226 if (!vim_iswhite(*p))
1227 {
1228 /* Don't put a space before a TAB. */
1229 if (p + 1 < leader + lead_len && p[1] == TAB)
1230 {
1231 --lead_len;
1232 mch_memmove(p, p + 1,
1233 (leader + lead_len) - p);
1234 }
1235 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001236 {
1237#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001238 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001239
1240 if (l > 1)
1241 {
1242 if (ptr2cells(p) > 1)
1243 {
1244 /* Replace a double-wide char with
1245 * two spaces */
1246 --l;
1247 *p++ = ' ';
1248 }
1249 mch_memmove(p + 1, p + l,
1250 (leader + lead_len) - p);
1251 lead_len -= l - 1;
1252 }
1253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 }
1257 *p = NUL;
1258 }
1259
1260 /* Recompute the indent, it may have changed. */
1261 if (curbuf->b_p_ai
1262#ifdef FEAT_SMARTINDENT
1263 || do_si
1264#endif
1265 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001266 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
1268 /* Add the indent offset */
1269 if (newindent + off < 0)
1270 {
1271 off = -newindent;
1272 newindent = 0;
1273 }
1274 else
1275 newindent += off;
1276
1277 /* Correct trailing spaces for the shift, so that
1278 * alignment remains equal. */
1279 while (off > 0 && lead_len > 0
1280 && leader[lead_len - 1] == ' ')
1281 {
1282 /* Don't do it when there is a tab before the space */
1283 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1284 break;
1285 --lead_len;
1286 --off;
1287 }
1288
1289 /* If the leader ends in white space, don't add an
1290 * extra space */
1291 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1292 extra_space = FALSE;
1293 leader[lead_len] = NUL;
1294 }
1295
1296 if (extra_space)
1297 {
1298 leader[lead_len++] = ' ';
1299 leader[lead_len] = NUL;
1300 }
1301
1302 newcol = lead_len;
1303
1304 /*
1305 * if a new indent will be set below, remove the indent that
1306 * is in the comment leader
1307 */
1308 if (newindent
1309#ifdef FEAT_SMARTINDENT
1310 || did_si
1311#endif
1312 )
1313 {
1314 while (lead_len && vim_iswhite(*leader))
1315 {
1316 --lead_len;
1317 --newcol;
1318 ++leader;
1319 }
1320 }
1321
1322 }
1323#ifdef FEAT_SMARTINDENT
1324 did_si = can_si = FALSE;
1325#endif
1326 }
1327 else if (comment_end != NULL)
1328 {
1329 /*
1330 * We have finished a comment, so we don't use the leader.
1331 * If this was a C-comment and 'ai' or 'si' is set do a normal
1332 * indent to align with the line containing the start of the
1333 * comment.
1334 */
1335 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1336 (curbuf->b_p_ai
1337#ifdef FEAT_SMARTINDENT
1338 || do_si
1339#endif
1340 ))
1341 {
1342 old_cursor = curwin->w_cursor;
1343 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1344 if ((pos = findmatch(NULL, NUL)) != NULL)
1345 {
1346 curwin->w_cursor.lnum = pos->lnum;
1347 newindent = get_indent();
1348 }
1349 curwin->w_cursor = old_cursor;
1350 }
1351 }
1352 }
1353#endif
1354
1355 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1356 if (p_extra != NULL)
1357 {
1358 *p_extra = saved_char; /* restore char that NUL replaced */
1359
1360 /*
1361 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1362 * non-blank.
1363 *
1364 * When in REPLACE mode, put the deleted blanks on the replace stack,
1365 * preceded by a NUL, so they can be put back when a BS is entered.
1366 */
1367 if (REPLACE_NORMAL(State))
1368 replace_push(NUL); /* end of extra blanks */
1369 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1370 {
1371 while ((*p_extra == ' ' || *p_extra == '\t')
1372#ifdef FEAT_MBYTE
1373 && (!enc_utf8
1374 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1375#endif
1376 )
1377 {
1378 if (REPLACE_NORMAL(State))
1379 replace_push(*p_extra);
1380 ++p_extra;
1381 ++less_cols_off;
1382 }
1383 }
1384 if (*p_extra != NUL)
1385 did_ai = FALSE; /* append some text, don't truncate now */
1386
1387 /* columns for marks adjusted for removed columns */
1388 less_cols = (int)(p_extra - saved_line);
1389 }
1390
1391 if (p_extra == NULL)
1392 p_extra = (char_u *)""; /* append empty line */
1393
1394#ifdef FEAT_COMMENTS
1395 /* concatenate leader and p_extra, if there is a leader */
1396 if (lead_len)
1397 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001398 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1399 {
1400 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001401 int padding = second_line_indent
1402 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001403
1404 /* Here whitespace is inserted after the comment char.
1405 * Below, set_indent(newindent, SIN_INSERT) will insert the
1406 * whitespace needed before the comment char. */
1407 for (i = 0; i < padding; i++)
1408 {
1409 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001410 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001411 newcol++;
1412 }
1413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 STRCAT(leader, p_extra);
1415 p_extra = leader;
1416 did_ai = TRUE; /* So truncating blanks works with comments */
1417 less_cols -= lead_len;
1418 }
1419 else
1420 end_comment_pending = NUL; /* turns out there was no leader */
1421#endif
1422
1423 old_cursor = curwin->w_cursor;
1424 if (dir == BACKWARD)
1425 --curwin->w_cursor.lnum;
1426#ifdef FEAT_VREPLACE
1427 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1428#endif
1429 {
1430 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1431 == FAIL)
1432 goto theend;
1433 /* Postpone calling changed_lines(), because it would mess up folding
1434 * with markers. */
1435 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1436 did_append = TRUE;
1437 }
1438#ifdef FEAT_VREPLACE
1439 else
1440 {
1441 /*
1442 * In VREPLACE mode we are starting to replace the next line.
1443 */
1444 curwin->w_cursor.lnum++;
1445 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1446 {
1447 /* In case we NL to a new line, BS to the previous one, and NL
1448 * again, we don't want to save the new line for undo twice.
1449 */
1450 (void)u_save_cursor(); /* errors are ignored! */
1451 vr_lines_changed++;
1452 }
1453 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1454 changed_bytes(curwin->w_cursor.lnum, 0);
1455 curwin->w_cursor.lnum--;
1456 did_append = FALSE;
1457 }
1458#endif
1459
1460 if (newindent
1461#ifdef FEAT_SMARTINDENT
1462 || did_si
1463#endif
1464 )
1465 {
1466 ++curwin->w_cursor.lnum;
1467#ifdef FEAT_SMARTINDENT
1468 if (did_si)
1469 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001470 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001473 newindent -= newindent % sw;
1474 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001477 /* Copy the indent */
1478 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
1480 (void)copy_indent(newindent, saved_line);
1481
1482 /*
1483 * Set the 'preserveindent' option so that any further screwing
1484 * with the line doesn't entirely destroy our efforts to preserve
1485 * it. It gets restored at the function end.
1486 */
1487 curbuf->b_p_pi = TRUE;
1488 }
1489 else
1490 (void)set_indent(newindent, SIN_INSERT);
1491 less_cols -= curwin->w_cursor.col;
1492
1493 ai_col = curwin->w_cursor.col;
1494
1495 /*
1496 * In REPLACE mode, for each character in the new indent, there must
1497 * be a NUL on the replace stack, for when it is deleted with BS
1498 */
1499 if (REPLACE_NORMAL(State))
1500 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1501 replace_push(NUL);
1502 newcol += curwin->w_cursor.col;
1503#ifdef FEAT_SMARTINDENT
1504 if (no_si)
1505 did_si = FALSE;
1506#endif
1507 }
1508
1509#ifdef FEAT_COMMENTS
1510 /*
1511 * In REPLACE mode, for each character in the extra leader, there must be
1512 * a NUL on the replace stack, for when it is deleted with BS.
1513 */
1514 if (REPLACE_NORMAL(State))
1515 while (lead_len-- > 0)
1516 replace_push(NUL);
1517#endif
1518
1519 curwin->w_cursor = old_cursor;
1520
1521 if (dir == FORWARD)
1522 {
1523 if (trunc_line || (State & INSERT))
1524 {
1525 /* truncate current line at cursor */
1526 saved_line[curwin->w_cursor.col] = NUL;
1527 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1528 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1529 truncate_spaces(saved_line);
1530 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1531 saved_line = NULL;
1532 if (did_append)
1533 {
1534 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1535 curwin->w_cursor.lnum + 1, 1L);
1536 did_append = FALSE;
1537
1538 /* Move marks after the line break to the new line. */
1539 if (flags & OPENLINE_MARKFIX)
1540 mark_col_adjust(curwin->w_cursor.lnum,
1541 curwin->w_cursor.col + less_cols_off,
1542 1L, (long)-less_cols);
1543 }
1544 else
1545 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1546 }
1547
1548 /*
1549 * Put the cursor on the new line. Careful: the scrollup() above may
1550 * have moved w_cursor, we must use old_cursor.
1551 */
1552 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1553 }
1554 if (did_append)
1555 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1556
1557 curwin->w_cursor.col = newcol;
1558#ifdef FEAT_VIRTUALEDIT
1559 curwin->w_cursor.coladd = 0;
1560#endif
1561
1562#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1563 /*
1564 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1565 * fixthisline() from doing it (via change_indent()) by telling it we're in
1566 * normal INSERT mode.
1567 */
1568 if (State & VREPLACE_FLAG)
1569 {
1570 vreplace_mode = State; /* So we know to put things right later */
1571 State = INSERT;
1572 }
1573 else
1574 vreplace_mode = 0;
1575#endif
1576#ifdef FEAT_LISP
1577 /*
1578 * May do lisp indenting.
1579 */
1580 if (!p_paste
1581# ifdef FEAT_COMMENTS
1582 && leader == NULL
1583# endif
1584 && curbuf->b_p_lisp
1585 && curbuf->b_p_ai)
1586 {
1587 fixthisline(get_lisp_indent);
1588 p = ml_get_curline();
1589 ai_col = (colnr_T)(skipwhite(p) - p);
1590 }
1591#endif
1592#ifdef FEAT_CINDENT
1593 /*
1594 * May do indenting after opening a new line.
1595 */
1596 if (!p_paste
1597 && (curbuf->b_p_cin
1598# ifdef FEAT_EVAL
1599 || *curbuf->b_p_inde != NUL
1600# endif
1601 )
1602 && in_cinkeys(dir == FORWARD
1603 ? KEY_OPEN_FORW
1604 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1605 {
1606 do_c_expr_indent();
1607 p = ml_get_curline();
1608 ai_col = (colnr_T)(skipwhite(p) - p);
1609 }
1610#endif
1611#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1612 if (vreplace_mode != 0)
1613 State = vreplace_mode;
1614#endif
1615
1616#ifdef FEAT_VREPLACE
1617 /*
1618 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1619 * original line, and inserts the new stuff char by char, pushing old stuff
1620 * onto the replace stack (via ins_char()).
1621 */
1622 if (State & VREPLACE_FLAG)
1623 {
1624 /* Put new line in p_extra */
1625 p_extra = vim_strsave(ml_get_curline());
1626 if (p_extra == NULL)
1627 goto theend;
1628
1629 /* Put back original line */
1630 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1631
1632 /* Insert new stuff into line again */
1633 curwin->w_cursor.col = 0;
1634#ifdef FEAT_VIRTUALEDIT
1635 curwin->w_cursor.coladd = 0;
1636#endif
1637 ins_bytes(p_extra); /* will call changed_bytes() */
1638 vim_free(p_extra);
1639 next_line = NULL;
1640 }
1641#endif
1642
1643 retval = TRUE; /* success! */
1644theend:
1645 curbuf->b_p_pi = saved_pi;
1646 vim_free(saved_line);
1647 vim_free(next_line);
1648 vim_free(allocated);
1649 return retval;
1650}
1651
1652#if defined(FEAT_COMMENTS) || defined(PROTO)
1653/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001654 * get_leader_len() returns the length in bytes of the prefix of the given
1655 * string which introduces a comment. If this string is not a comment then
1656 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 * When "flags" is not NULL, it is set to point to the flags of the recognized
1658 * comment leader.
1659 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001660 * If "include_space" is set, include trailing whitespace while calculating the
1661 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 */
1663 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001664get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 char_u *line;
1666 char_u **flags;
1667 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001668 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
1670 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001671 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 int got_com = FALSE;
1673 int found_one;
1674 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1675 char_u *string; /* pointer to comment string */
1676 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001677 int middle_match_len = 0;
1678 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001679 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
Bram Moolenaar81340392012-06-06 16:12:59 +02001681 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 while (vim_iswhite(line[i])) /* leading white space is ignored */
1683 ++i;
1684
1685 /*
1686 * Repeat to match several nested comment strings.
1687 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001688 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 /*
1691 * scan through the 'comments' option for a match
1692 */
1693 found_one = FALSE;
1694 for (list = curbuf->b_p_com; *list; )
1695 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001696 /* Get one option part into part_buf[]. Advance "list" to next
1697 * one. Put "string" at start of string. */
1698 if (!got_com && flags != NULL)
1699 *flags = list; /* remember where flags started */
1700 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1702 string = vim_strchr(part_buf, ':');
1703 if (string == NULL) /* missing ':', ignore this part */
1704 continue;
1705 *string++ = NUL; /* isolate flags from string */
1706
Bram Moolenaara4271d52011-05-10 13:38:27 +02001707 /* If we found a middle match previously, use that match when this
1708 * is not a middle or end. */
1709 if (middle_match_len != 0
1710 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1711 && vim_strchr(part_buf, COM_END) == NULL)
1712 break;
1713
1714 /* When we already found a nested comment, only accept further
1715 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1717 continue;
1718
Bram Moolenaara4271d52011-05-10 13:38:27 +02001719 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1721 continue;
1722
Bram Moolenaara4271d52011-05-10 13:38:27 +02001723 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 * When string starts with white space, must have some white space
1725 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001726 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (vim_iswhite(string[0]))
1728 {
1729 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001730 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 while (vim_iswhite(string[0]))
1732 ++string;
1733 }
1734 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1735 ;
1736 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001737 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
Bram Moolenaara4271d52011-05-10 13:38:27 +02001739 /* When 'b' flag used, there must be white space or an
1740 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if (vim_strchr(part_buf, COM_BLANK) != NULL
1742 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1743 continue;
1744
Bram Moolenaara4271d52011-05-10 13:38:27 +02001745 /* We have found a match, stop searching unless this is a middle
1746 * comment. The middle comment can be a substring of the end
1747 * comment in which case it's better to return the length of the
1748 * end comment and its flags. Thus we keep searching with middle
1749 * and end matches and use an end match if it matches better. */
1750 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1751 {
1752 if (middle_match_len == 0)
1753 {
1754 middle_match_len = j;
1755 saved_flags = prev_list;
1756 }
1757 continue;
1758 }
1759 if (middle_match_len != 0 && j > middle_match_len)
1760 /* Use this match instead of the middle match, since it's a
1761 * longer thus better match. */
1762 middle_match_len = 0;
1763
1764 if (middle_match_len == 0)
1765 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 found_one = TRUE;
1767 break;
1768 }
1769
Bram Moolenaara4271d52011-05-10 13:38:27 +02001770 if (middle_match_len != 0)
1771 {
1772 /* Use the previously found middle match after failing to find a
1773 * match with an end. */
1774 if (!got_com && flags != NULL)
1775 *flags = saved_flags;
1776 i += middle_match_len;
1777 found_one = TRUE;
1778 }
1779
1780 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (!found_one)
1782 break;
1783
Bram Moolenaar81340392012-06-06 16:12:59 +02001784 result = i;
1785
Bram Moolenaara4271d52011-05-10 13:38:27 +02001786 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 while (vim_iswhite(line[i]))
1788 ++i;
1789
Bram Moolenaar81340392012-06-06 16:12:59 +02001790 if (include_space)
1791 result = i;
1792
Bram Moolenaara4271d52011-05-10 13:38:27 +02001793 /* If this comment doesn't nest, stop here. */
1794 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (vim_strchr(part_buf, COM_NEST) == NULL)
1796 break;
1797 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001798 return result;
1799}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001800
Bram Moolenaar81340392012-06-06 16:12:59 +02001801/*
1802 * Return the offset at which the last comment in line starts. If there is no
1803 * comment in the whole line, -1 is returned.
1804 *
1805 * When "flags" is not null, it is set to point to the flags describing the
1806 * recognized comment leader.
1807 */
1808 int
1809get_last_leader_offset(line, flags)
1810 char_u *line;
1811 char_u **flags;
1812{
1813 int result = -1;
1814 int i, j;
1815 int lower_check_bound = 0;
1816 char_u *string;
1817 char_u *com_leader;
1818 char_u *com_flags;
1819 char_u *list;
1820 int found_one;
1821 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1822
1823 /*
1824 * Repeat to match several nested comment strings.
1825 */
1826 i = (int)STRLEN(line);
1827 while (--i >= lower_check_bound)
1828 {
1829 /*
1830 * scan through the 'comments' option for a match
1831 */
1832 found_one = FALSE;
1833 for (list = curbuf->b_p_com; *list; )
1834 {
1835 char_u *flags_save = list;
1836
1837 /*
1838 * Get one option part into part_buf[]. Advance list to next one.
1839 * put string at start of string.
1840 */
1841 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1842 string = vim_strchr(part_buf, ':');
1843 if (string == NULL) /* If everything is fine, this cannot actually
1844 * happen. */
1845 {
1846 continue;
1847 }
1848 *string++ = NUL; /* Isolate flags from string. */
1849 com_leader = string;
1850
1851 /*
1852 * Line contents and string must match.
1853 * When string starts with white space, must have some white space
1854 * (but the amount does not need to match, there might be a mix of
1855 * TABs and spaces).
1856 */
1857 if (vim_iswhite(string[0]))
1858 {
1859 if (i == 0 || !vim_iswhite(line[i - 1]))
1860 continue;
1861 while (vim_iswhite(string[0]))
1862 ++string;
1863 }
1864 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1865 /* do nothing */;
1866 if (string[j] != NUL)
1867 continue;
1868
1869 /*
1870 * When 'b' flag used, there must be white space or an
1871 * end-of-line after the string in the line.
1872 */
1873 if (vim_strchr(part_buf, COM_BLANK) != NULL
1874 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1875 {
1876 continue;
1877 }
1878
1879 /*
1880 * We have found a match, stop searching.
1881 */
1882 found_one = TRUE;
1883
1884 if (flags)
1885 *flags = flags_save;
1886 com_flags = flags_save;
1887
1888 break;
1889 }
1890
1891 if (found_one)
1892 {
1893 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1894 int len1, len2, off;
1895
1896 result = i;
1897 /*
1898 * If this comment nests, continue searching.
1899 */
1900 if (vim_strchr(part_buf, COM_NEST) != NULL)
1901 continue;
1902
1903 lower_check_bound = i;
1904
1905 /* Let's verify whether the comment leader found is a substring
1906 * of other comment leaders. If it is, let's adjust the
1907 * lower_check_bound so that we make sure that we have determined
1908 * the comment leader correctly.
1909 */
1910
1911 while (vim_iswhite(*com_leader))
1912 ++com_leader;
1913 len1 = (int)STRLEN(com_leader);
1914
1915 for (list = curbuf->b_p_com; *list; )
1916 {
1917 char_u *flags_save = list;
1918
1919 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1920 if (flags_save == com_flags)
1921 continue;
1922 string = vim_strchr(part_buf2, ':');
1923 ++string;
1924 while (vim_iswhite(*string))
1925 ++string;
1926 len2 = (int)STRLEN(string);
1927 if (len2 == 0)
1928 continue;
1929
1930 /* Now we have to verify whether string ends with a substring
1931 * beginning the com_leader. */
1932 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1933 {
1934 --off;
1935 if (!STRNCMP(string + off, com_leader, len2 - off))
1936 {
1937 if (i - off < lower_check_bound)
1938 lower_check_bound = i - off;
1939 }
1940 }
1941 }
1942 }
1943 }
1944 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945}
1946#endif
1947
1948/*
1949 * Return the number of window lines occupied by buffer line "lnum".
1950 */
1951 int
1952plines(lnum)
1953 linenr_T lnum;
1954{
1955 return plines_win(curwin, lnum, TRUE);
1956}
1957
1958 int
1959plines_win(wp, lnum, winheight)
1960 win_T *wp;
1961 linenr_T lnum;
1962 int winheight; /* when TRUE limit to window height */
1963{
1964#if defined(FEAT_DIFF) || defined(PROTO)
1965 /* Check for filler lines above this buffer line. When folded the result
1966 * is one line anyway. */
1967 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1968}
1969
1970 int
1971plines_nofill(lnum)
1972 linenr_T lnum;
1973{
1974 return plines_win_nofill(curwin, lnum, TRUE);
1975}
1976
1977 int
1978plines_win_nofill(wp, lnum, winheight)
1979 win_T *wp;
1980 linenr_T lnum;
1981 int winheight; /* when TRUE limit to window height */
1982{
1983#endif
1984 int lines;
1985
1986 if (!wp->w_p_wrap)
1987 return 1;
1988
1989#ifdef FEAT_VERTSPLIT
1990 if (wp->w_width == 0)
1991 return 1;
1992#endif
1993
1994#ifdef FEAT_FOLDING
1995 /* A folded lines is handled just like an empty line. */
1996 /* NOTE: Caller must handle lines that are MAYBE folded. */
1997 if (lineFolded(wp, lnum) == TRUE)
1998 return 1;
1999#endif
2000
2001 lines = plines_win_nofold(wp, lnum);
2002 if (winheight > 0 && lines > wp->w_height)
2003 return (int)wp->w_height;
2004 return lines;
2005}
2006
2007/*
2008 * Return number of window lines physical line "lnum" will occupy in window
2009 * "wp". Does not care about folding, 'wrap' or 'diff'.
2010 */
2011 int
2012plines_win_nofold(wp, lnum)
2013 win_T *wp;
2014 linenr_T lnum;
2015{
2016 char_u *s;
2017 long col;
2018 int width;
2019
2020 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2021 if (*s == NUL) /* empty line */
2022 return 1;
2023 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2024
2025 /*
2026 * If list mode is on, then the '$' at the end of the line may take up one
2027 * extra column.
2028 */
2029 if (wp->w_p_list && lcs_eol != NUL)
2030 col += 1;
2031
2032 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002033 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 */
2035 width = W_WIDTH(wp) - win_col_off(wp);
2036 if (width <= 0)
2037 return 32000;
2038 if (col <= width)
2039 return 1;
2040 col -= width;
2041 width += win_col_off2(wp);
2042 return (col + (width - 1)) / width + 1;
2043}
2044
2045/*
2046 * Like plines_win(), but only reports the number of physical screen lines
2047 * used from the start of the line to the given column number.
2048 */
2049 int
2050plines_win_col(wp, lnum, column)
2051 win_T *wp;
2052 linenr_T lnum;
2053 long column;
2054{
2055 long col;
2056 char_u *s;
2057 int lines = 0;
2058 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002059 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061#ifdef FEAT_DIFF
2062 /* Check for filler lines above this buffer line. When folded the result
2063 * is one line anyway. */
2064 lines = diff_check_fill(wp, lnum);
2065#endif
2066
2067 if (!wp->w_p_wrap)
2068 return lines + 1;
2069
2070#ifdef FEAT_VERTSPLIT
2071 if (wp->w_width == 0)
2072 return lines + 1;
2073#endif
2074
Bram Moolenaar597a4222014-06-25 14:39:50 +02002075 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
2077 col = 0;
2078 while (*s != NUL && --column >= 0)
2079 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002080 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002081 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083
2084 /*
2085 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2086 * INSERT mode, then col must be adjusted so that it represents the last
2087 * screen position of the TAB. This only fixes an error when the TAB wraps
2088 * from one screen line to the next (when 'columns' is not a multiple of
2089 * 'ts') -- webb.
2090 */
2091 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002092 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
2094 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002095 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 */
2097 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002098 if (width <= 0)
2099 return 9999;
2100
2101 lines += 1;
2102 if (col > width)
2103 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2104 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105}
2106
2107 int
2108plines_m_win(wp, first, last)
2109 win_T *wp;
2110 linenr_T first, last;
2111{
2112 int count = 0;
2113
2114 while (first <= last)
2115 {
2116#ifdef FEAT_FOLDING
2117 int x;
2118
2119 /* Check if there are any really folded lines, but also included lines
2120 * that are maybe folded. */
2121 x = foldedCount(wp, first, NULL);
2122 if (x > 0)
2123 {
2124 ++count; /* count 1 for "+-- folded" line */
2125 first += x;
2126 }
2127 else
2128#endif
2129 {
2130#ifdef FEAT_DIFF
2131 if (first == wp->w_topline)
2132 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2133 else
2134#endif
2135 count += plines_win(wp, first, TRUE);
2136 ++first;
2137 }
2138 }
2139 return (count);
2140}
2141
2142#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2143/*
2144 * Insert string "p" at the cursor position. Stops at a NUL byte.
2145 * Handles Replace mode and multi-byte characters.
2146 */
2147 void
2148ins_bytes(p)
2149 char_u *p;
2150{
2151 ins_bytes_len(p, (int)STRLEN(p));
2152}
2153#endif
2154
2155#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2156 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2157/*
2158 * Insert string "p" with length "len" at the cursor position.
2159 * Handles Replace mode and multi-byte characters.
2160 */
2161 void
2162ins_bytes_len(p, len)
2163 char_u *p;
2164 int len;
2165{
2166 int i;
2167# ifdef FEAT_MBYTE
2168 int n;
2169
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002170 if (has_mbyte)
2171 for (i = 0; i < len; i += n)
2172 {
2173 if (enc_utf8)
2174 /* avoid reading past p[len] */
2175 n = utfc_ptr2len_len(p + i, len - i);
2176 else
2177 n = (*mb_ptr2len)(p + i);
2178 ins_char_bytes(p + i, n);
2179 }
2180 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002182 for (i = 0; i < len; ++i)
2183 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184}
2185#endif
2186
2187/*
2188 * Insert or replace a single character at the cursor position.
2189 * When in REPLACE or VREPLACE mode, replace any existing character.
2190 * Caller must have prepared for undo.
2191 * For multi-byte characters we get the whole character, the caller must
2192 * convert bytes to a character.
2193 */
2194 void
2195ins_char(c)
2196 int c;
2197{
2198#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002199 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 int n;
2201
2202 n = (*mb_char2bytes)(c, buf);
2203
2204 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2205 * Happens for CTRL-Vu9900. */
2206 if (buf[0] == 0)
2207 buf[0] = '\n';
2208
2209 ins_char_bytes(buf, n);
2210}
2211
2212 void
2213ins_char_bytes(buf, charlen)
2214 char_u *buf;
2215 int charlen;
2216{
2217 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218#endif
2219 int newlen; /* nr of bytes inserted */
2220 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2221 char_u *p;
2222 char_u *newp;
2223 char_u *oldp;
2224 int linelen; /* length of old line including NUL */
2225 colnr_T col;
2226 linenr_T lnum = curwin->w_cursor.lnum;
2227 int i;
2228
2229#ifdef FEAT_VIRTUALEDIT
2230 /* Break tabs if needed. */
2231 if (virtual_active() && curwin->w_cursor.coladd > 0)
2232 coladvance_force(getviscol());
2233#endif
2234
2235 col = curwin->w_cursor.col;
2236 oldp = ml_get(lnum);
2237 linelen = (int)STRLEN(oldp) + 1;
2238
2239 /* The lengths default to the values for when not replacing. */
2240 oldlen = 0;
2241#ifdef FEAT_MBYTE
2242 newlen = charlen;
2243#else
2244 newlen = 1;
2245#endif
2246
2247 if (State & REPLACE_FLAG)
2248 {
2249#ifdef FEAT_VREPLACE
2250 if (State & VREPLACE_FLAG)
2251 {
2252 colnr_T new_vcol = 0; /* init for GCC */
2253 colnr_T vcol;
2254 int old_list;
2255#ifndef FEAT_MBYTE
2256 char_u buf[2];
2257#endif
2258
2259 /*
2260 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2261 * Returns the old value of list, so when finished,
2262 * curwin->w_p_list should be set back to this.
2263 */
2264 old_list = curwin->w_p_list;
2265 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2266 curwin->w_p_list = FALSE;
2267
2268 /*
2269 * In virtual replace mode each character may replace one or more
2270 * characters (zero if it's a TAB). Count the number of bytes to
2271 * be deleted to make room for the new character, counting screen
2272 * cells. May result in adding spaces to fill a gap.
2273 */
2274 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2275#ifndef FEAT_MBYTE
2276 buf[0] = c;
2277 buf[1] = NUL;
2278#endif
2279 new_vcol = vcol + chartabsize(buf, vcol);
2280 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2281 {
2282 vcol += chartabsize(oldp + col + oldlen, vcol);
2283 /* Don't need to remove a TAB that takes us to the right
2284 * position. */
2285 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2286 break;
2287#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002288 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#else
2290 ++oldlen;
2291#endif
2292 /* Deleted a bit too much, insert spaces. */
2293 if (vcol > new_vcol)
2294 newlen += vcol - new_vcol;
2295 }
2296 curwin->w_p_list = old_list;
2297 }
2298 else
2299#endif
2300 if (oldp[col] != NUL)
2301 {
2302 /* normal replace */
2303#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002304 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#else
2306 oldlen = 1;
2307#endif
2308 }
2309
2310
2311 /* Push the replaced bytes onto the replace stack, so that they can be
2312 * put back when BS is used. The bytes of a multi-byte character are
2313 * done the other way around, so that the first byte is popped off
2314 * first (it tells the byte length of the character). */
2315 replace_push(NUL);
2316 for (i = 0; i < oldlen; ++i)
2317 {
2318#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002319 if (has_mbyte)
2320 i += replace_push_mb(oldp + col + i) - 1;
2321 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002323 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325 }
2326
2327 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2328 if (newp == NULL)
2329 return;
2330
2331 /* Copy bytes before the cursor. */
2332 if (col > 0)
2333 mch_memmove(newp, oldp, (size_t)col);
2334
2335 /* Copy bytes after the changed character(s). */
2336 p = newp + col;
2337 mch_memmove(p + newlen, oldp + col + oldlen,
2338 (size_t)(linelen - col - oldlen));
2339
2340 /* Insert or overwrite the new character. */
2341#ifdef FEAT_MBYTE
2342 mch_memmove(p, buf, charlen);
2343 i = charlen;
2344#else
2345 *p = c;
2346 i = 1;
2347#endif
2348
2349 /* Fill with spaces when necessary. */
2350 while (i < newlen)
2351 p[i++] = ' ';
2352
2353 /* Replace the line in the buffer. */
2354 ml_replace(lnum, newp, FALSE);
2355
2356 /* mark the buffer as changed and prepare for displaying */
2357 changed_bytes(lnum, col);
2358
2359 /*
2360 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2361 * show the match for right parens and braces.
2362 */
2363 if (p_sm && (State & INSERT)
2364 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002365#ifdef FEAT_INS_EXPAND
2366 && !ins_compl_active()
2367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002369 {
2370#ifdef FEAT_MBYTE
2371 if (has_mbyte)
2372 showmatch(mb_ptr2char(buf));
2373 else
2374#endif
2375 showmatch(c);
2376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378#ifdef FEAT_RIGHTLEFT
2379 if (!p_ri || (State & REPLACE_FLAG))
2380#endif
2381 {
2382 /* Normal insert: move cursor right */
2383#ifdef FEAT_MBYTE
2384 curwin->w_cursor.col += charlen;
2385#else
2386 ++curwin->w_cursor.col;
2387#endif
2388 }
2389 /*
2390 * TODO: should try to update w_row here, to avoid recomputing it later.
2391 */
2392}
2393
2394/*
2395 * Insert a string at the cursor position.
2396 * Note: Does NOT handle Replace mode.
2397 * Caller must have prepared for undo.
2398 */
2399 void
2400ins_str(s)
2401 char_u *s;
2402{
2403 char_u *oldp, *newp;
2404 int newlen = (int)STRLEN(s);
2405 int oldlen;
2406 colnr_T col;
2407 linenr_T lnum = curwin->w_cursor.lnum;
2408
2409#ifdef FEAT_VIRTUALEDIT
2410 if (virtual_active() && curwin->w_cursor.coladd > 0)
2411 coladvance_force(getviscol());
2412#endif
2413
2414 col = curwin->w_cursor.col;
2415 oldp = ml_get(lnum);
2416 oldlen = (int)STRLEN(oldp);
2417
2418 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2419 if (newp == NULL)
2420 return;
2421 if (col > 0)
2422 mch_memmove(newp, oldp, (size_t)col);
2423 mch_memmove(newp + col, s, (size_t)newlen);
2424 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2425 ml_replace(lnum, newp, FALSE);
2426 changed_bytes(lnum, col);
2427 curwin->w_cursor.col += newlen;
2428}
2429
2430/*
2431 * Delete one character under the cursor.
2432 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2433 * Caller must have prepared for undo.
2434 *
2435 * return FAIL for failure, OK otherwise
2436 */
2437 int
2438del_char(fixpos)
2439 int fixpos;
2440{
2441#ifdef FEAT_MBYTE
2442 if (has_mbyte)
2443 {
2444 /* Make sure the cursor is at the start of a character. */
2445 mb_adjust_cursor();
2446 if (*ml_get_cursor() == NUL)
2447 return FAIL;
2448 return del_chars(1L, fixpos);
2449 }
2450#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002451 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452}
2453
2454#if defined(FEAT_MBYTE) || defined(PROTO)
2455/*
2456 * Like del_bytes(), but delete characters instead of bytes.
2457 */
2458 int
2459del_chars(count, fixpos)
2460 long count;
2461 int fixpos;
2462{
2463 long bytes = 0;
2464 long i;
2465 char_u *p;
2466 int l;
2467
2468 p = ml_get_cursor();
2469 for (i = 0; i < count && *p != NUL; ++i)
2470 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002471 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 bytes += l;
2473 p += l;
2474 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002475 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476}
2477#endif
2478
2479/*
2480 * Delete "count" bytes under the cursor.
2481 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2482 * Caller must have prepared for undo.
2483 *
2484 * return FAIL for failure, OK otherwise
2485 */
2486 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002487del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002489 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002490 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 char_u *oldp, *newp;
2493 colnr_T oldlen;
2494 linenr_T lnum = curwin->w_cursor.lnum;
2495 colnr_T col = curwin->w_cursor.col;
2496 int was_alloced;
2497 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002498 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 oldp = ml_get(lnum);
2501 oldlen = (int)STRLEN(oldp);
2502
2503 /*
2504 * Can't do anything when the cursor is on the NUL after the line.
2505 */
2506 if (col >= oldlen)
2507 return FAIL;
2508
2509#ifdef FEAT_MBYTE
2510 /* If 'delcombine' is set and deleting (less than) one character, only
2511 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002512 if (p_deco && use_delcombine && enc_utf8
2513 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002515 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 int n;
2517
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002518 (void)utfc_ptr2char(oldp + col, cc);
2519 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 /* Find the last composing char, there can be several. */
2522 n = col;
2523 do
2524 {
2525 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002526 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 n += count;
2528 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2529 fixpos = 0;
2530 }
2531 }
2532#endif
2533
2534 /*
2535 * When count is too big, reduce it.
2536 */
2537 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2538 if (movelen <= 1)
2539 {
2540 /*
2541 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002542 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2543 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002545 if (col > 0 && fixpos && restart_edit == 0
2546#ifdef FEAT_VIRTUALEDIT
2547 && (ve_flags & VE_ONEMORE) == 0
2548#endif
2549 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
2551 --curwin->w_cursor.col;
2552#ifdef FEAT_VIRTUALEDIT
2553 curwin->w_cursor.coladd = 0;
2554#endif
2555#ifdef FEAT_MBYTE
2556 if (has_mbyte)
2557 curwin->w_cursor.col -=
2558 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2559#endif
2560 }
2561 count = oldlen - col;
2562 movelen = 1;
2563 }
2564
2565 /*
2566 * If the old line has been allocated the deletion can be done in the
2567 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002568 * Can't do this when using Netbeans, because we would need to invoke
2569 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002570 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002573 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002574 was_alloced = FALSE;
2575 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002577 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (was_alloced)
2579 newp = oldp; /* use same allocated memory */
2580 else
2581 { /* need to allocate a new line */
2582 newp = alloc((unsigned)(oldlen + 1 - count));
2583 if (newp == NULL)
2584 return FAIL;
2585 mch_memmove(newp, oldp, (size_t)col);
2586 }
2587 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2588 if (!was_alloced)
2589 ml_replace(lnum, newp, FALSE);
2590
2591 /* mark the buffer as changed and prepare for displaying */
2592 changed_bytes(lnum, curwin->w_cursor.col);
2593
2594 return OK;
2595}
2596
2597/*
2598 * Delete from cursor to end of line.
2599 * Caller must have prepared for undo.
2600 *
2601 * return FAIL for failure, OK otherwise
2602 */
2603 int
2604truncate_line(fixpos)
2605 int fixpos; /* if TRUE fix the cursor position when done */
2606{
2607 char_u *newp;
2608 linenr_T lnum = curwin->w_cursor.lnum;
2609 colnr_T col = curwin->w_cursor.col;
2610
2611 if (col == 0)
2612 newp = vim_strsave((char_u *)"");
2613 else
2614 newp = vim_strnsave(ml_get(lnum), col);
2615
2616 if (newp == NULL)
2617 return FAIL;
2618
2619 ml_replace(lnum, newp, FALSE);
2620
2621 /* mark the buffer as changed and prepare for displaying */
2622 changed_bytes(lnum, curwin->w_cursor.col);
2623
2624 /*
2625 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2626 */
2627 if (fixpos && curwin->w_cursor.col > 0)
2628 --curwin->w_cursor.col;
2629
2630 return OK;
2631}
2632
2633/*
2634 * Delete "nlines" lines at the cursor.
2635 * Saves the lines for undo first if "undo" is TRUE.
2636 */
2637 void
2638del_lines(nlines, undo)
2639 long nlines; /* number of lines to delete */
2640 int undo; /* if TRUE, prepare for undo */
2641{
2642 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002643 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 if (nlines <= 0)
2646 return;
2647
2648 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002649 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 return;
2651
2652 for (n = 0; n < nlines; )
2653 {
2654 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2655 break;
2656
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002657 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 ++n;
2659
2660 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002661 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 break;
2663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002665 /* Correct the cursor position before calling deleted_lines_mark(), it may
2666 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 curwin->w_cursor.col = 0;
2668 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002669
2670 /* adjust marks, mark the buffer as changed and prepare for displaying */
2671 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672}
2673
2674 int
2675gchar_pos(pos)
2676 pos_T *pos;
2677{
2678 char_u *ptr = ml_get_pos(pos);
2679
2680#ifdef FEAT_MBYTE
2681 if (has_mbyte)
2682 return (*mb_ptr2char)(ptr);
2683#endif
2684 return (int)*ptr;
2685}
2686
2687 int
2688gchar_cursor()
2689{
2690#ifdef FEAT_MBYTE
2691 if (has_mbyte)
2692 return (*mb_ptr2char)(ml_get_cursor());
2693#endif
2694 return (int)*ml_get_cursor();
2695}
2696
2697/*
2698 * Write a character at the current cursor position.
2699 * It is directly written into the block.
2700 */
2701 void
2702pchar_cursor(c)
2703 int c;
2704{
2705 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2706 + curwin->w_cursor.col) = c;
2707}
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709/*
2710 * When extra == 0: Return TRUE if the cursor is before or on the first
2711 * non-blank in the line.
2712 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2713 * the line.
2714 */
2715 int
2716inindent(extra)
2717 int extra;
2718{
2719 char_u *ptr;
2720 colnr_T col;
2721
2722 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2723 ++ptr;
2724 if (col >= curwin->w_cursor.col + extra)
2725 return TRUE;
2726 else
2727 return FALSE;
2728}
2729
2730/*
2731 * Skip to next part of an option argument: Skip space and comma.
2732 */
2733 char_u *
2734skip_to_option_part(p)
2735 char_u *p;
2736{
2737 if (*p == ',')
2738 ++p;
2739 while (*p == ' ')
2740 ++p;
2741 return p;
2742}
2743
2744/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002745 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 *
2747 * Most often called through changed_bytes() and changed_lines(), which also
2748 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002749 *
2750 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 */
2752 void
2753changed()
2754{
2755#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2756 /* The text of the preediting area is inserted, but this doesn't
2757 * mean a change of the buffer yet. That is delayed until the
2758 * text is committed. (this means preedit becomes empty) */
2759 if (im_is_preediting() && !xim_changed_while_preediting)
2760 return;
2761 xim_changed_while_preediting = FALSE;
2762#endif
2763
2764 if (!curbuf->b_changed)
2765 {
2766 int save_msg_scroll = msg_scroll;
2767
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002768 /* Give a warning about changing a read-only file. This may also
2769 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002771
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 /* Create a swap file if that is wanted.
2773 * Don't do this for "nofile" and "nowrite" buffer types. */
2774 if (curbuf->b_may_swap
2775#ifdef FEAT_QUICKFIX
2776 && !bt_dontwrite(curbuf)
2777#endif
2778 )
2779 {
2780 ml_open_file(curbuf);
2781
2782 /* The ml_open_file() can cause an ATTENTION message.
2783 * Wait two seconds, to make sure the user reads this unexpected
2784 * message. Since we could be anywhere, call wait_return() now,
2785 * and don't let the emsg() set msg_scroll. */
2786 if (need_wait_return && emsg_silent == 0)
2787 {
2788 out_flush();
2789 ui_delay(2000L, TRUE);
2790 wait_return(TRUE);
2791 msg_scroll = save_msg_scroll;
2792 }
2793 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002794 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797}
2798
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002799/*
2800 * Internal part of changed(), no user interaction.
2801 */
2802 void
2803changed_int()
2804{
2805 curbuf->b_changed = TRUE;
2806 ml_setflags(curbuf);
2807#ifdef FEAT_WINDOWS
2808 check_status(curbuf);
2809 redraw_tabline = TRUE;
2810#endif
2811#ifdef FEAT_TITLE
2812 need_maketitle = TRUE; /* set window title later */
2813#endif
2814}
2815
Bram Moolenaardba8a912005-04-24 22:08:39 +00002816static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2817static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2819
2820/*
2821 * Changed bytes within a single line for the current buffer.
2822 * - marks the windows on this buffer to be redisplayed
2823 * - marks the buffer changed by calling changed()
2824 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002825 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 */
2827 void
2828changed_bytes(lnum, col)
2829 linenr_T lnum;
2830 colnr_T col;
2831{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002832 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002834
2835#ifdef FEAT_DIFF
2836 /* Diff highlighting in other diff windows may need to be updated too. */
2837 if (curwin->w_p_diff)
2838 {
2839 win_T *wp;
2840 linenr_T wlnum;
2841
2842 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2843 if (wp->w_p_diff && wp != curwin)
2844 {
2845 redraw_win_later(wp, VALID);
2846 wlnum = diff_lnum_win(lnum, wp);
2847 if (wlnum > 0)
2848 changedOneline(wp->w_buffer, wlnum);
2849 }
2850 }
2851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852}
2853
2854 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002855changedOneline(buf, lnum)
2856 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 linenr_T lnum;
2858{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002859 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
2861 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002862 if (lnum < buf->b_mod_top)
2863 buf->b_mod_top = lnum;
2864 else if (lnum >= buf->b_mod_bot)
2865 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 else
2868 {
2869 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002870 buf->b_mod_set = TRUE;
2871 buf->b_mod_top = lnum;
2872 buf->b_mod_bot = lnum + 1;
2873 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875}
2876
2877/*
2878 * Appended "count" lines below line "lnum" in the current buffer.
2879 * Must be called AFTER the change and after mark_adjust().
2880 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2881 */
2882 void
2883appended_lines(lnum, count)
2884 linenr_T lnum;
2885 long count;
2886{
2887 changed_lines(lnum + 1, 0, lnum + 1, count);
2888}
2889
2890/*
2891 * Like appended_lines(), but adjust marks first.
2892 */
2893 void
2894appended_lines_mark(lnum, count)
2895 linenr_T lnum;
2896 long count;
2897{
2898 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2899 changed_lines(lnum + 1, 0, lnum + 1, count);
2900}
2901
2902/*
2903 * Deleted "count" lines at line "lnum" in the current buffer.
2904 * Must be called AFTER the change and after mark_adjust().
2905 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2906 */
2907 void
2908deleted_lines(lnum, count)
2909 linenr_T lnum;
2910 long count;
2911{
2912 changed_lines(lnum, 0, lnum + count, -count);
2913}
2914
2915/*
2916 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002917 * Make sure the cursor is on a valid line before calling, a GUI callback may
2918 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 */
2920 void
2921deleted_lines_mark(lnum, count)
2922 linenr_T lnum;
2923 long count;
2924{
2925 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2926 changed_lines(lnum, 0, lnum + count, -count);
2927}
2928
2929/*
2930 * Changed lines for the current buffer.
2931 * Must be called AFTER the change and after mark_adjust().
2932 * - mark the buffer changed by calling changed()
2933 * - mark the windows on this buffer to be redisplayed
2934 * - invalidate cached values
2935 * "lnum" is the first line that needs displaying, "lnume" the first line
2936 * below the changed lines (BEFORE the change).
2937 * When only inserting lines, "lnum" and "lnume" are equal.
2938 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002939 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 */
2941 void
2942changed_lines(lnum, col, lnume, xtra)
2943 linenr_T lnum; /* first line with change */
2944 colnr_T col; /* column in first line with change */
2945 linenr_T lnume; /* line below last changed line */
2946 long xtra; /* number of extra lines (negative when deleting) */
2947{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002948 changed_lines_buf(curbuf, lnum, lnume, xtra);
2949
2950#ifdef FEAT_DIFF
2951 if (xtra == 0 && curwin->w_p_diff)
2952 {
2953 /* When the number of lines doesn't change then mark_adjust() isn't
2954 * called and other diff buffers still need to be marked for
2955 * displaying. */
2956 win_T *wp;
2957 linenr_T wlnum;
2958
2959 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2960 if (wp->w_p_diff && wp != curwin)
2961 {
2962 redraw_win_later(wp, VALID);
2963 wlnum = diff_lnum_win(lnum, wp);
2964 if (wlnum > 0)
2965 changed_lines_buf(wp->w_buffer, wlnum,
2966 lnume - lnum + wlnum, 0L);
2967 }
2968 }
2969#endif
2970
2971 changed_common(lnum, col, lnume, xtra);
2972}
2973
2974 static void
2975changed_lines_buf(buf, lnum, lnume, xtra)
2976 buf_T *buf;
2977 linenr_T lnum; /* first line with change */
2978 linenr_T lnume; /* line below last changed line */
2979 long xtra; /* number of extra lines (negative when deleting) */
2980{
2981 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
2983 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002984 if (lnum < buf->b_mod_top)
2985 buf->b_mod_top = lnum;
2986 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002989 buf->b_mod_bot += xtra;
2990 if (buf->b_mod_bot < lnum)
2991 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002993 if (lnume + xtra > buf->b_mod_bot)
2994 buf->b_mod_bot = lnume + xtra;
2995 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997 else
2998 {
2999 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003000 buf->b_mod_set = TRUE;
3001 buf->b_mod_top = lnum;
3002 buf->b_mod_bot = lnume + xtra;
3003 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005}
3006
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003007/*
3008 * Common code for when a change is was made.
3009 * See changed_lines() for the arguments.
3010 * Careful: may trigger autocommands that reload the buffer.
3011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 static void
3013changed_common(lnum, col, lnume, xtra)
3014 linenr_T lnum;
3015 colnr_T col;
3016 linenr_T lnume;
3017 long xtra;
3018{
3019 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003020#ifdef FEAT_WINDOWS
3021 tabpage_T *tp;
3022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 int i;
3024#ifdef FEAT_JUMPLIST
3025 int cols;
3026 pos_T *p;
3027 int add;
3028#endif
3029
3030 /* mark the buffer as modified */
3031 changed();
3032
3033 /* set the '. mark */
3034 if (!cmdmod.keepjumps)
3035 {
3036 curbuf->b_last_change.lnum = lnum;
3037 curbuf->b_last_change.col = col;
3038
3039#ifdef FEAT_JUMPLIST
3040 /* Create a new entry if a new undo-able change was started or we
3041 * don't have an entry yet. */
3042 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3043 {
3044 if (curbuf->b_changelistlen == 0)
3045 add = TRUE;
3046 else
3047 {
3048 /* Don't create a new entry when the line number is the same
3049 * as the last one and the column is not too far away. Avoids
3050 * creating many entries for typing "xxxxx". */
3051 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3052 if (p->lnum != lnum)
3053 add = TRUE;
3054 else
3055 {
3056 cols = comp_textwidth(FALSE);
3057 if (cols == 0)
3058 cols = 79;
3059 add = (p->col + cols < col || col + cols < p->col);
3060 }
3061 }
3062 if (add)
3063 {
3064 /* This is the first of a new sequence of undo-able changes
3065 * and it's at some distance of the last change. Use a new
3066 * position in the changelist. */
3067 curbuf->b_new_change = FALSE;
3068
3069 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3070 {
3071 /* changelist is full: remove oldest entry */
3072 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3073 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3074 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003075 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 {
3077 /* Correct position in changelist for other windows on
3078 * this buffer. */
3079 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3080 --wp->w_changelistidx;
3081 }
3082 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003083 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
3085 /* For other windows, if the position in the changelist is
3086 * at the end it stays at the end. */
3087 if (wp->w_buffer == curbuf
3088 && wp->w_changelistidx == curbuf->b_changelistlen)
3089 ++wp->w_changelistidx;
3090 }
3091 ++curbuf->b_changelistlen;
3092 }
3093 }
3094 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3095 curbuf->b_last_change;
3096 /* The current window is always after the last change, so that "g,"
3097 * takes you back to it. */
3098 curwin->w_changelistidx = curbuf->b_changelistlen;
3099#endif
3100 }
3101
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003102 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
3104 if (wp->w_buffer == curbuf)
3105 {
3106 /* Mark this window to be redrawn later. */
3107 if (wp->w_redr_type < VALID)
3108 wp->w_redr_type = VALID;
3109
3110 /* Check if a change in the buffer has invalidated the cached
3111 * values for the cursor. */
3112#ifdef FEAT_FOLDING
3113 /*
3114 * Update the folds for this window. Can't postpone this, because
3115 * a following operator might work on the whole fold: ">>dd".
3116 */
3117 foldUpdate(wp, lnum, lnume + xtra - 1);
3118
3119 /* The change may cause lines above or below the change to become
3120 * included in a fold. Set lnum/lnume to the first/last line that
3121 * might be displayed differently.
3122 * Set w_cline_folded here as an efficient way to update it when
3123 * inserting lines just above a closed fold. */
3124 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3125 if (wp->w_cursor.lnum == lnum)
3126 wp->w_cline_folded = i;
3127 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3128 if (wp->w_cursor.lnum == lnume)
3129 wp->w_cline_folded = i;
3130
3131 /* If the changed line is in a range of previously folded lines,
3132 * compare with the first line in that range. */
3133 if (wp->w_cursor.lnum <= lnum)
3134 {
3135 i = find_wl_entry(wp, lnum);
3136 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3137 changed_line_abv_curs_win(wp);
3138 }
3139#endif
3140
3141 if (wp->w_cursor.lnum > lnum)
3142 changed_line_abv_curs_win(wp);
3143 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3144 changed_cline_bef_curs_win(wp);
3145 if (wp->w_botline >= lnum)
3146 {
3147 /* Assume that botline doesn't change (inserted lines make
3148 * other lines scroll down below botline). */
3149 approximate_botline_win(wp);
3150 }
3151
3152 /* Check if any w_lines[] entries have become invalid.
3153 * For entries below the change: Correct the lnums for
3154 * inserted/deleted lines. Makes it possible to stop displaying
3155 * after the change. */
3156 for (i = 0; i < wp->w_lines_valid; ++i)
3157 if (wp->w_lines[i].wl_valid)
3158 {
3159 if (wp->w_lines[i].wl_lnum >= lnum)
3160 {
3161 if (wp->w_lines[i].wl_lnum < lnume)
3162 {
3163 /* line included in change */
3164 wp->w_lines[i].wl_valid = FALSE;
3165 }
3166 else if (xtra != 0)
3167 {
3168 /* line below change */
3169 wp->w_lines[i].wl_lnum += xtra;
3170#ifdef FEAT_FOLDING
3171 wp->w_lines[i].wl_lastlnum += xtra;
3172#endif
3173 }
3174 }
3175#ifdef FEAT_FOLDING
3176 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3177 {
3178 /* change somewhere inside this range of folded lines,
3179 * may need to be redrawn */
3180 wp->w_lines[i].wl_valid = FALSE;
3181 }
3182#endif
3183 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003184
3185#ifdef FEAT_FOLDING
3186 /* Take care of side effects for setting w_topline when folds have
3187 * changed. Esp. when the buffer was changed in another window. */
3188 if (hasAnyFolding(wp))
3189 set_topline(wp, wp->w_topline);
3190#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003191 /* relative numbering may require updating more */
3192 if (wp->w_p_rnu)
3193 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195 }
3196
3197 /* Call update_screen() later, which checks out what needs to be redrawn,
3198 * since it notices b_mod_set and then uses b_mod_*. */
3199 if (must_redraw < VALID)
3200 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003201
3202#ifdef FEAT_AUTOCMD
3203 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003204 if (lnum <= curwin->w_cursor.lnum
3205 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003206 last_cursormoved.lnum = 0;
3207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208}
3209
3210/*
3211 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3212 */
3213 void
3214unchanged(buf, ff)
3215 buf_T *buf;
3216 int ff; /* also reset 'fileformat' */
3217{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003218 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
3220 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003221 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (ff)
3223 save_file_ff(buf);
3224#ifdef FEAT_WINDOWS
3225 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003226 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227#endif
3228#ifdef FEAT_TITLE
3229 need_maketitle = TRUE; /* set window title later */
3230#endif
3231 }
3232 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#ifdef FEAT_NETBEANS_INTG
3234 netbeans_unmodified(buf);
3235#endif
3236}
3237
3238#if defined(FEAT_WINDOWS) || defined(PROTO)
3239/*
3240 * check_status: called when the status bars for the buffer 'buf'
3241 * need to be updated
3242 */
3243 void
3244check_status(buf)
3245 buf_T *buf;
3246{
3247 win_T *wp;
3248
3249 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3250 if (wp->w_buffer == buf && wp->w_status_height)
3251 {
3252 wp->w_redr_status = TRUE;
3253 if (must_redraw < VALID)
3254 must_redraw = VALID;
3255 }
3256}
3257#endif
3258
3259/*
3260 * If the file is readonly, give a warning message with the first change.
3261 * Don't do this for autocommands.
3262 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003263 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003265 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 */
3267 void
3268change_warning(col)
3269 int col; /* column for message; non-zero when in insert
3270 mode and 'showmode' is on */
3271{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003272 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 if (curbuf->b_did_warn == FALSE
3275 && curbufIsChanged() == 0
3276#ifdef FEAT_AUTOCMD
3277 && !autocmd_busy
3278#endif
3279 && curbuf->b_p_ro)
3280 {
3281#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003282 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003284 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (!curbuf->b_p_ro)
3286 return;
3287#endif
3288 /*
3289 * Do what msg() does, but with a column offset if the warning should
3290 * be after the mode message.
3291 */
3292 msg_start();
3293 if (msg_row == Rows - 1)
3294 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003295 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003296 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3297#ifdef FEAT_EVAL
3298 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 msg_clr_eos();
3301 (void)msg_end();
3302 if (msg_silent == 0 && !silent_mode)
3303 {
3304 out_flush();
3305 ui_delay(1000L, TRUE); /* give the user time to think about it */
3306 }
3307 curbuf->b_did_warn = TRUE;
3308 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3309 if (msg_row < Rows - 1)
3310 showmode();
3311 }
3312}
3313
3314/*
3315 * Ask for a reply from the user, a 'y' or a 'n'.
3316 * No other characters are accepted, the message is repeated until a valid
3317 * reply is entered or CTRL-C is hit.
3318 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3319 * from any buffers but directly from the user.
3320 *
3321 * return the 'y' or 'n'
3322 */
3323 int
3324ask_yesno(str, direct)
3325 char_u *str;
3326 int direct;
3327{
3328 int r = ' ';
3329 int save_State = State;
3330
3331 if (exiting) /* put terminal in raw mode for this question */
3332 settmode(TMODE_RAW);
3333 ++no_wait_return;
3334#ifdef USE_ON_FLY_SCROLL
3335 dont_scroll = TRUE; /* disallow scrolling here */
3336#endif
3337 State = CONFIRM; /* mouse behaves like with :confirm */
3338#ifdef FEAT_MOUSE
3339 setmouse(); /* disables mouse for xterm */
3340#endif
3341 ++no_mapping;
3342 ++allow_keys; /* no mapping here, but recognize keys */
3343
3344 while (r != 'y' && r != 'n')
3345 {
3346 /* same highlighting as for wait_return */
3347 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3348 if (direct)
3349 r = get_keystroke();
3350 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003351 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (r == Ctrl_C || r == ESC)
3353 r = 'n';
3354 msg_putchar(r); /* show what you typed */
3355 out_flush();
3356 }
3357 --no_wait_return;
3358 State = save_State;
3359#ifdef FEAT_MOUSE
3360 setmouse();
3361#endif
3362 --no_mapping;
3363 --allow_keys;
3364
3365 return r;
3366}
3367
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003368#if defined(FEAT_MOUSE) || defined(PROTO)
3369/*
3370 * Return TRUE if "c" is a mouse key.
3371 */
3372 int
3373is_mouse_key(c)
3374 int c;
3375{
3376 return c == K_LEFTMOUSE
3377 || c == K_LEFTMOUSE_NM
3378 || c == K_LEFTDRAG
3379 || c == K_LEFTRELEASE
3380 || c == K_LEFTRELEASE_NM
3381 || c == K_MIDDLEMOUSE
3382 || c == K_MIDDLEDRAG
3383 || c == K_MIDDLERELEASE
3384 || c == K_RIGHTMOUSE
3385 || c == K_RIGHTDRAG
3386 || c == K_RIGHTRELEASE
3387 || c == K_MOUSEDOWN
3388 || c == K_MOUSEUP
3389 || c == K_MOUSELEFT
3390 || c == K_MOUSERIGHT
3391 || c == K_X1MOUSE
3392 || c == K_X1DRAG
3393 || c == K_X1RELEASE
3394 || c == K_X2MOUSE
3395 || c == K_X2DRAG
3396 || c == K_X2RELEASE;
3397}
3398#endif
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
3401 * Get a key stroke directly from the user.
3402 * Ignores mouse clicks and scrollbar events, except a click for the left
3403 * button (used at the more prompt).
3404 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3405 * Disadvantage: typeahead is ignored.
3406 * Translates the interrupt character for unix to ESC.
3407 */
3408 int
3409get_keystroke()
3410{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003411 char_u *buf = NULL;
3412 int buflen = 150;
3413 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 int len = 0;
3415 int n;
3416 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003417 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419 mapped_ctrl_c = FALSE; /* mappings are not used here */
3420 for (;;)
3421 {
3422 cursor_on();
3423 out_flush();
3424
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003425 /* Leave some room for check_termcode() to insert a key code into (max
3426 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3427 * bytes. */
3428 maxlen = (buflen - 6 - len) / 3;
3429 if (buf == NULL)
3430 buf = alloc(buflen);
3431 else if (maxlen < 10)
3432 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003433 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003434 * escape sequence. */
3435 buflen += 100;
3436 buf = vim_realloc(buf, buflen);
3437 maxlen = (buflen - 6 - len) / 3;
3438 }
3439 if (buf == NULL)
3440 {
3441 do_outofmem_msg((long_u)buflen);
3442 return ESC; /* panic! */
3443 }
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003446 * terminal code to complete. */
3447 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (n > 0)
3449 {
3450 /* Replace zero and CSI by a special key code. */
3451 n = fix_input_buffer(buf + len, n, FALSE);
3452 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003453 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003455 else if (len > 0)
3456 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaar4395a712006-09-05 18:57:57 +00003458 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003459 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003460 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003462
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003463 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003464 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003465 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003466 {
3467 /* Redrawing was postponed, do it now. */
3468 update_screen(0);
3469 setcursor(); /* put cursor back where it belongs */
3470 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003471 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003472 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003473 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003475 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 continue;
3477
3478 /* Handle modifier and/or special key code. */
3479 n = buf[0];
3480 if (n == K_SPECIAL)
3481 {
3482 n = TO_SPECIAL(buf[1], buf[2]);
3483 if (buf[1] == KS_MODIFIER
3484 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003485#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003486 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003487#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003488#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 || n == K_VER_SCROLLBAR
3490 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491#endif
3492 )
3493 {
3494 if (buf[1] == KS_MODIFIER)
3495 mod_mask = buf[2];
3496 len -= 3;
3497 if (len > 0)
3498 mch_memmove(buf, buf + 3, (size_t)len);
3499 continue;
3500 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503#ifdef FEAT_MBYTE
3504 if (has_mbyte)
3505 {
3506 if (MB_BYTE2LEN(n) > len)
3507 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003508 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 n = (*mb_ptr2char)(buf);
3510 }
3511#endif
3512#ifdef UNIX
3513 if (n == intr_char)
3514 n = ESC;
3515#endif
3516 break;
3517 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003518 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
3520 mapped_ctrl_c = save_mapped_ctrl_c;
3521 return n;
3522}
3523
3524/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003525 * Get a number from the user.
3526 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 */
3528 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003529get_number(colon, mouse_used)
3530 int colon; /* allow colon to abort */
3531 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
3533 int n = 0;
3534 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003535 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003537 if (mouse_used != NULL)
3538 *mouse_used = FALSE;
3539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 /* When not printing messages, the user won't know what to type, return a
3541 * zero (as if CR was hit). */
3542 if (msg_silent != 0)
3543 return 0;
3544
3545#ifdef USE_ON_FLY_SCROLL
3546 dont_scroll = TRUE; /* disallow scrolling here */
3547#endif
3548 ++no_mapping;
3549 ++allow_keys; /* no mapping here, but recognize keys */
3550 for (;;)
3551 {
3552 windgoto(msg_row, msg_col);
3553 c = safe_vgetc();
3554 if (VIM_ISDIGIT(c))
3555 {
3556 n = n * 10 + c - '0';
3557 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003558 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
3560 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3561 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003562 if (typed > 0)
3563 {
3564 MSG_PUTS("\b \b");
3565 --typed;
3566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003569#ifdef FEAT_MOUSE
3570 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3571 {
3572 *mouse_used = TRUE;
3573 n = mouse_row + 1;
3574 break;
3575 }
3576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 else if (n == 0 && c == ':' && colon)
3578 {
3579 stuffcharReadbuff(':');
3580 if (!exmode_active)
3581 cmdline_row = msg_row;
3582 skip_redraw = TRUE; /* skip redraw once */
3583 do_redraw = FALSE;
3584 break;
3585 }
3586 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3587 break;
3588 }
3589 --no_mapping;
3590 --allow_keys;
3591 return n;
3592}
3593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003594/*
3595 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003596 * When "mouse_used" is not NULL allow using the mouse and in that case return
3597 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003598 */
3599 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003600prompt_for_number(mouse_used)
3601 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003602{
3603 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003604 int save_cmdline_row;
3605 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003606
3607 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003608 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003609 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003610 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003611 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003612
Bram Moolenaar203335e2006-09-03 14:35:42 +00003613 /* Set the state such that text can be selected/copied/pasted and we still
3614 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003615 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003616 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003617 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003618 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003619
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003620 i = get_number(TRUE, mouse_used);
3621 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003622 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003623 /* don't call wait_return() now */
3624 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003625 cmdline_row = msg_row - 1;
3626 need_wait_return = FALSE;
3627 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003628 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003629 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003630 else
3631 cmdline_row = save_cmdline_row;
3632 State = save_State;
3633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003634 return i;
3635}
3636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 void
3638msgmore(n)
3639 long n;
3640{
3641 long pn;
3642
3643 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3645 return;
3646
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003647 /* We don't want to overwrite another important message, but do overwrite
3648 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3649 * then "put" reports the last action. */
3650 if (keep_msg != NULL && !keep_msg_more)
3651 return;
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (n > 0)
3654 pn = n;
3655 else
3656 pn = -n;
3657
3658 if (pn > p_report)
3659 {
3660 if (pn == 1)
3661 {
3662 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003663 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3664 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003666 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3667 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669 else
3670 {
3671 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003672 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3673 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003675 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3676 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003679 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (msg(msg_buf))
3681 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003682 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003683 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 }
3686}
3687
3688/*
3689 * flush map and typeahead buffers and give a warning for an error
3690 */
3691 void
3692beep_flush()
3693{
3694 if (emsg_silent == 0)
3695 {
3696 flush_buffers(FALSE);
3697 vim_beep();
3698 }
3699}
3700
3701/*
3702 * give a warning for an error
3703 */
3704 void
3705vim_beep()
3706{
3707 if (emsg_silent == 0)
3708 {
3709 if (p_vb
3710#ifdef FEAT_GUI
3711 /* While the GUI is starting up the termcap is set for the GUI
3712 * but the output still goes to a terminal. */
3713 && !(gui.in_use && gui.starting)
3714#endif
3715 )
3716 {
3717 out_str(T_VB);
3718 }
3719 else
3720 {
3721#ifdef MSDOS
3722 /*
3723 * The number of beeps outputted is reduced to avoid having to wait
3724 * for all the beeps to finish. This is only a problem on systems
3725 * where the beeps don't overlap.
3726 */
3727 if (beep_count == 0 || beep_count == 10)
3728 {
3729 out_char(BELL);
3730 beep_count = 1;
3731 }
3732 else
3733 ++beep_count;
3734#else
3735 out_char(BELL);
3736#endif
3737 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003738
3739 /* When 'verbose' is set and we are sourcing a script or executing a
3740 * function give the user a hint where the beep comes from. */
3741 if (vim_strchr(p_debug, 'e') != NULL)
3742 {
3743 msg_source(hl_attr(HLF_W));
3744 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747}
3748
3749/*
3750 * To get the "real" home directory:
3751 * - get value of $HOME
3752 * For Unix:
3753 * - go to that directory
3754 * - do mch_dirname() to get the real name of that directory.
3755 * This also works with mounts and links.
3756 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3757 */
3758static char_u *homedir = NULL;
3759
3760 void
3761init_homedir()
3762{
3763 char_u *var;
3764
Bram Moolenaar05159a02005-02-26 23:04:13 +00003765 /* In case we are called a second time (when 'encoding' changes). */
3766 vim_free(homedir);
3767 homedir = NULL;
3768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#ifdef VMS
3770 var = mch_getenv((char_u *)"SYS$LOGIN");
3771#else
3772 var = mch_getenv((char_u *)"HOME");
3773#endif
3774
3775 if (var != NULL && *var == NUL) /* empty is same as not set */
3776 var = NULL;
3777
3778#ifdef WIN3264
3779 /*
3780 * Weird but true: $HOME may contain an indirect reference to another
3781 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3782 * when $HOME is being set.
3783 */
3784 if (var != NULL && *var == '%')
3785 {
3786 char_u *p;
3787 char_u *exp;
3788
3789 p = vim_strchr(var + 1, '%');
3790 if (p != NULL)
3791 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003792 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 exp = mch_getenv(NameBuff);
3794 if (exp != NULL && *exp != NUL
3795 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3796 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003797 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 var = NameBuff;
3799 /* Also set $HOME, it's needed for _viminfo. */
3800 vim_setenv((char_u *)"HOME", NameBuff);
3801 }
3802 }
3803 }
3804
3805 /*
3806 * Typically, $HOME is not defined on Windows, unless the user has
3807 * specifically defined it for Vim's sake. However, on Windows NT
3808 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3809 * each user. Try constructing $HOME from these.
3810 */
3811 if (var == NULL)
3812 {
3813 char_u *homedrive, *homepath;
3814
3815 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3816 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003817 if (homepath == NULL || *homepath == NUL)
3818 homepath = "\\";
3819 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3821 {
3822 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3823 if (NameBuff[0] != NUL)
3824 {
3825 var = NameBuff;
3826 /* Also set $HOME, it's needed for _viminfo. */
3827 vim_setenv((char_u *)"HOME", NameBuff);
3828 }
3829 }
3830 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003831
3832# if defined(FEAT_MBYTE)
3833 if (enc_utf8 && var != NULL)
3834 {
3835 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003836 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003837
3838 /* Convert from active codepage to UTF-8. Other conversions are
3839 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003840 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003841 if (pp != NULL)
3842 {
3843 homedir = pp;
3844 return;
3845 }
3846 }
3847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848#endif
3849
3850#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3851 /*
3852 * Default home dir is C:/
3853 * Best assumption we can make in such a situation.
3854 */
3855 if (var == NULL)
3856 var = "C:/";
3857#endif
3858 if (var != NULL)
3859 {
3860#ifdef UNIX
3861 /*
3862 * Change to the directory and get the actual path. This resolves
3863 * links. Don't do it when we can't return.
3864 */
3865 if (mch_dirname(NameBuff, MAXPATHL) == OK
3866 && mch_chdir((char *)NameBuff) == 0)
3867 {
3868 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3869 var = IObuff;
3870 if (mch_chdir((char *)NameBuff) != 0)
3871 EMSG(_(e_prev_dir));
3872 }
3873#endif
3874 homedir = vim_strsave(var);
3875 }
3876}
3877
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003878#if defined(EXITFREE) || defined(PROTO)
3879 void
3880free_homedir()
3881{
3882 vim_free(homedir);
3883}
Bram Moolenaar24305862012-08-15 14:05:05 +02003884
3885# ifdef FEAT_CMDL_COMPL
3886 void
3887free_users()
3888{
3889 ga_clear_strings(&ga_users);
3890}
3891# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003892#endif
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003895 * Call expand_env() and store the result in an allocated string.
3896 * This is not very memory efficient, this expects the result to be freed
3897 * again soon.
3898 */
3899 char_u *
3900expand_env_save(src)
3901 char_u *src;
3902{
3903 return expand_env_save_opt(src, FALSE);
3904}
3905
3906/*
3907 * Idem, but when "one" is TRUE handle the string as one file name, only
3908 * expand "~" at the start.
3909 */
3910 char_u *
3911expand_env_save_opt(src, one)
3912 char_u *src;
3913 int one;
3914{
3915 char_u *p;
3916
3917 p = alloc(MAXPATHL);
3918 if (p != NULL)
3919 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3920 return p;
3921}
3922
3923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 * Expand environment variable with path name.
3925 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003926 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 * If anything fails no expansion is done and dst equals src.
3928 */
3929 void
3930expand_env(src, dst, dstlen)
3931 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3932 char_u *dst; /* where to put the result */
3933 int dstlen; /* maximum length of the result */
3934{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003935 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936}
3937
3938 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003939expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003940 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 char_u *dst; /* where to put the result */
3942 int dstlen; /* maximum length of the result */
3943 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003944 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003945 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003947 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 char_u *tail;
3949 int c;
3950 char_u *var;
3951 int copy_char;
3952 int mustfree; /* var was allocated, need to free it later */
3953 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003954 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003956 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003957 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003958
3959 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 --dstlen; /* leave one char space for "\," */
3961 while (*src && dstlen > 0)
3962 {
3963 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003964 if ((*src == '$'
3965#ifdef VMS
3966 && at_start
3967#endif
3968 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3970 || *src == '%'
3971#endif
3972 || (*src == '~' && at_start))
3973 {
3974 mustfree = FALSE;
3975
3976 /*
3977 * The variable name is copied into dst temporarily, because it may
3978 * be a string in read-only memory and a NUL needs to be appended.
3979 */
3980 if (*src != '~') /* environment var */
3981 {
3982 tail = src + 1;
3983 var = dst;
3984 c = dstlen - 1;
3985
3986#ifdef UNIX
3987 /* Unix has ${var-name} type environment vars */
3988 if (*tail == '{' && !vim_isIDc('{'))
3989 {
3990 tail++; /* ignore '{' */
3991 while (c-- > 0 && *tail && *tail != '}')
3992 *var++ = *tail++;
3993 }
3994 else
3995#endif
3996 {
3997 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3998#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3999 || (*src == '%' && *tail != '%')
4000#endif
4001 ))
4002 {
4003#ifdef OS2 /* env vars only in uppercase */
4004 *var++ = TOUPPER_LOC(*tail);
4005 tail++; /* toupper() may be a macro! */
4006#else
4007 *var++ = *tail++;
4008#endif
4009 }
4010 }
4011
4012#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
4013# ifdef UNIX
4014 if (src[1] == '{' && *tail != '}')
4015# else
4016 if (*src == '%' && *tail != '%')
4017# endif
4018 var = NULL;
4019 else
4020 {
4021# ifdef UNIX
4022 if (src[1] == '{')
4023# else
4024 if (*src == '%')
4025#endif
4026 ++tail;
4027#endif
4028 *var = NUL;
4029 var = vim_getenv(dst, &mustfree);
4030#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
4031 }
4032#endif
4033 }
4034 /* home directory */
4035 else if ( src[1] == NUL
4036 || vim_ispathsep(src[1])
4037 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4038 {
4039 var = homedir;
4040 tail = src + 1;
4041 }
4042 else /* user directory */
4043 {
4044#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4045 /*
4046 * Copy ~user to dst[], so we can put a NUL after it.
4047 */
4048 tail = src;
4049 var = dst;
4050 c = dstlen - 1;
4051 while ( c-- > 0
4052 && *tail
4053 && vim_isfilec(*tail)
4054 && !vim_ispathsep(*tail))
4055 *var++ = *tail++;
4056 *var = NUL;
4057# ifdef UNIX
4058 /*
4059 * If the system supports getpwnam(), use it.
4060 * Otherwise, or if getpwnam() fails, the shell is used to
4061 * expand ~user. This is slower and may fail if the shell
4062 * does not support ~user (old versions of /bin/sh).
4063 */
4064# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4065 {
4066 struct passwd *pw;
4067
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004068 /* Note: memory allocated by getpwnam() is never freed.
4069 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 pw = getpwnam((char *)dst + 1);
4071 if (pw != NULL)
4072 var = (char_u *)pw->pw_dir;
4073 else
4074 var = NULL;
4075 }
4076 if (var == NULL)
4077# endif
4078 {
4079 expand_T xpc;
4080
4081 ExpandInit(&xpc);
4082 xpc.xp_context = EXPAND_FILES;
4083 var = ExpandOne(&xpc, dst, NULL,
4084 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 mustfree = TRUE;
4086 }
4087
4088# else /* !UNIX, thus VMS */
4089 /*
4090 * USER_HOME is a comma-separated list of
4091 * directories to search for the user account in.
4092 */
4093 {
4094 char_u test[MAXPATHL], paths[MAXPATHL];
4095 char_u *path, *next_path, *ptr;
4096 struct stat st;
4097
4098 STRCPY(paths, USER_HOME);
4099 next_path = paths;
4100 while (*next_path)
4101 {
4102 for (path = next_path; *next_path && *next_path != ',';
4103 next_path++);
4104 if (*next_path)
4105 *next_path++ = NUL;
4106 STRCPY(test, path);
4107 STRCAT(test, "/");
4108 STRCAT(test, dst + 1);
4109 if (mch_stat(test, &st) == 0)
4110 {
4111 var = alloc(STRLEN(test) + 1);
4112 STRCPY(var, test);
4113 mustfree = TRUE;
4114 break;
4115 }
4116 }
4117 }
4118# endif /* UNIX */
4119#else
4120 /* cannot expand user's home directory, so don't try */
4121 var = NULL;
4122 tail = (char_u *)""; /* for gcc */
4123#endif /* UNIX || VMS */
4124 }
4125
4126#ifdef BACKSLASH_IN_FILENAME
4127 /* If 'shellslash' is set change backslashes to forward slashes.
4128 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4129 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4130 {
4131 char_u *p = vim_strsave(var);
4132
4133 if (p != NULL)
4134 {
4135 if (mustfree)
4136 vim_free(var);
4137 var = p;
4138 mustfree = TRUE;
4139 forward_slash(var);
4140 }
4141 }
4142#endif
4143
4144 /* If "var" contains white space, escape it with a backslash.
4145 * Required for ":e ~/tt" when $HOME includes a space. */
4146 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4147 {
4148 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4149
4150 if (p != NULL)
4151 {
4152 if (mustfree)
4153 vim_free(var);
4154 var = p;
4155 mustfree = TRUE;
4156 }
4157 }
4158
4159 if (var != NULL && *var != NUL
4160 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4161 {
4162 STRCPY(dst, var);
4163 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004164 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 /* if var[] ends in a path separator and tail[] starts
4166 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004167 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4169 && dst[-1] != ':'
4170#endif
4171 && vim_ispathsep(*tail))
4172 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004173 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 src = tail;
4175 copy_char = FALSE;
4176 }
4177 if (mustfree)
4178 vim_free(var);
4179 }
4180
4181 if (copy_char) /* copy at least one char */
4182 {
4183 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004184 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004185 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4186 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 */
4188 at_start = FALSE;
4189 if (src[0] == '\\' && src[1] != NUL)
4190 {
4191 *dst++ = *src++;
4192 --dstlen;
4193 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004194 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 at_start = TRUE;
4196 *dst++ = *src++;
4197 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004198
4199 if (startstr != NULL && src - startstr_len >= srcp
4200 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4201 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 }
4204 *dst = NUL;
4205}
4206
4207/*
4208 * Vim's version of getenv().
4209 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004210 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004211 * "mustfree" is set to TRUE when returned is allocated, it must be
4212 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
4214 char_u *
4215vim_getenv(name, mustfree)
4216 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004217 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
4219 char_u *p;
4220 char_u *pend;
4221 int vimruntime;
4222
4223#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4224 /* use "C:/" when $HOME is not set */
4225 if (STRCMP(name, "HOME") == 0)
4226 return homedir;
4227#endif
4228
4229 p = mch_getenv(name);
4230 if (p != NULL && *p == NUL) /* empty is the same as not set */
4231 p = NULL;
4232
4233 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004234 {
4235#if defined(FEAT_MBYTE) && defined(WIN3264)
4236 if (enc_utf8)
4237 {
4238 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004239 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004240
4241 /* Convert from active codepage to UTF-8. Other conversions are
4242 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004243 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004244 if (pp != NULL)
4245 {
4246 p = pp;
4247 *mustfree = TRUE;
4248 }
4249 }
4250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4255 if (!vimruntime && STRCMP(name, "VIM") != 0)
4256 return NULL;
4257
4258 /*
4259 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4260 * Don't do this when default_vimruntime_dir is non-empty.
4261 */
4262 if (vimruntime
4263#ifdef HAVE_PATHDEF
4264 && *default_vimruntime_dir == NUL
4265#endif
4266 )
4267 {
4268 p = mch_getenv((char_u *)"VIM");
4269 if (p != NULL && *p == NUL) /* empty is the same as not set */
4270 p = NULL;
4271 if (p != NULL)
4272 {
4273 p = vim_version_dir(p);
4274 if (p != NULL)
4275 *mustfree = TRUE;
4276 else
4277 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004278
4279#if defined(FEAT_MBYTE) && defined(WIN3264)
4280 if (enc_utf8)
4281 {
4282 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004283 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004284
4285 /* Convert from active codepage to UTF-8. Other conversions
4286 * are not done, because they would fail for non-ASCII
4287 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004288 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004289 if (pp != NULL)
4290 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004291 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004292 vim_free(p);
4293 p = pp;
4294 *mustfree = TRUE;
4295 }
4296 }
4297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 }
4300
4301 /*
4302 * When expanding $VIM or $VIMRUNTIME fails, try using:
4303 * - the directory name from 'helpfile' (unless it contains '$')
4304 * - the executable name from argv[0]
4305 */
4306 if (p == NULL)
4307 {
4308 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4309 p = p_hf;
4310#ifdef USE_EXE_NAME
4311 /*
4312 * Use the name of the executable, obtained from argv[0].
4313 */
4314 else
4315 p = exe_name;
4316#endif
4317 if (p != NULL)
4318 {
4319 /* remove the file name */
4320 pend = gettail(p);
4321
4322 /* remove "doc/" from 'helpfile', if present */
4323 if (p == p_hf)
4324 pend = remove_tail(p, pend, (char_u *)"doc");
4325
4326#ifdef USE_EXE_NAME
4327# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004328 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 if (p == exe_name)
4330 {
4331 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004332 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004334 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4335 if (pend1 != pend)
4336 {
4337 pnew = alloc((unsigned)(pend1 - p) + 15);
4338 if (pnew != NULL)
4339 {
4340 STRNCPY(pnew, p, (pend1 - p));
4341 STRCPY(pnew + (pend1 - p), "Resources/vim");
4342 p = pnew;
4343 pend = p + STRLEN(p);
4344 }
4345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347# endif
4348 /* remove "src/" from exe_name, if present */
4349 if (p == exe_name)
4350 pend = remove_tail(p, pend, (char_u *)"src");
4351#endif
4352
4353 /* for $VIM, remove "runtime/" or "vim54/", if present */
4354 if (!vimruntime)
4355 {
4356 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4357 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4358 }
4359
4360 /* remove trailing path separator */
4361#ifndef MACOS_CLASSIC
4362 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004363 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004364 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 --pend;
4366#endif
4367
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004368#ifdef MACOS_X
4369 if (p == exe_name || p == p_hf)
4370#endif
4371 /* check that the result is a directory name */
4372 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 if (p != NULL && !mch_isdir(p))
4375 {
4376 vim_free(p);
4377 p = NULL;
4378 }
4379 else
4380 {
4381#ifdef USE_EXE_NAME
4382 /* may add "/vim54" or "/runtime" if it exists */
4383 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4384 {
4385 vim_free(p);
4386 p = pend;
4387 }
4388#endif
4389 *mustfree = TRUE;
4390 }
4391 }
4392 }
4393
4394#ifdef HAVE_PATHDEF
4395 /* When there is a pathdef.c file we can use default_vim_dir and
4396 * default_vimruntime_dir */
4397 if (p == NULL)
4398 {
4399 /* Only use default_vimruntime_dir when it is not empty */
4400 if (vimruntime && *default_vimruntime_dir != NUL)
4401 {
4402 p = default_vimruntime_dir;
4403 *mustfree = FALSE;
4404 }
4405 else if (*default_vim_dir != NUL)
4406 {
4407 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4408 *mustfree = TRUE;
4409 else
4410 {
4411 p = default_vim_dir;
4412 *mustfree = FALSE;
4413 }
4414 }
4415 }
4416#endif
4417
4418 /*
4419 * Set the environment variable, so that the new value can be found fast
4420 * next time, and others can also use it (e.g. Perl).
4421 */
4422 if (p != NULL)
4423 {
4424 if (vimruntime)
4425 {
4426 vim_setenv((char_u *)"VIMRUNTIME", p);
4427 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 else
4430 {
4431 vim_setenv((char_u *)"VIM", p);
4432 didset_vim = TRUE;
4433 }
4434 }
4435 return p;
4436}
4437
4438/*
4439 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4440 * Return NULL if not, return its name in allocated memory otherwise.
4441 */
4442 static char_u *
4443vim_version_dir(vimdir)
4444 char_u *vimdir;
4445{
4446 char_u *p;
4447
4448 if (vimdir == NULL || *vimdir == NUL)
4449 return NULL;
4450 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4451 if (p != NULL && mch_isdir(p))
4452 return p;
4453 vim_free(p);
4454 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4455 if (p != NULL && mch_isdir(p))
4456 return p;
4457 vim_free(p);
4458 return NULL;
4459}
4460
4461/*
4462 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4463 * the length of "name/". Otherwise return "pend".
4464 */
4465 static char_u *
4466remove_tail(p, pend, name)
4467 char_u *p;
4468 char_u *pend;
4469 char_u *name;
4470{
4471 int len = (int)STRLEN(name) + 1;
4472 char_u *newend = pend - len;
4473
4474 if (newend >= p
4475 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004476 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 return newend;
4478 return pend;
4479}
4480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 * Our portable version of setenv.
4483 */
4484 void
4485vim_setenv(name, val)
4486 char_u *name;
4487 char_u *val;
4488{
4489#ifdef HAVE_SETENV
4490 mch_setenv((char *)name, (char *)val, 1);
4491#else
4492 char_u *envbuf;
4493
4494 /*
4495 * Putenv does not copy the string, it has to remain
4496 * valid. The allocated memory will never be freed.
4497 */
4498 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4499 if (envbuf != NULL)
4500 {
4501 sprintf((char *)envbuf, "%s=%s", name, val);
4502 putenv((char *)envbuf);
4503 }
4504#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004505#ifdef FEAT_GETTEXT
4506 /*
4507 * When setting $VIMRUNTIME adjust the directory to find message
4508 * translations to $VIMRUNTIME/lang.
4509 */
4510 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4511 {
4512 char_u *buf = concat_str(val, (char_u *)"/lang");
4513
4514 if (buf != NULL)
4515 {
4516 bindtextdomain(VIMPACKAGE, (char *)buf);
4517 vim_free(buf);
4518 }
4519 }
4520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521}
4522
4523#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4524/*
4525 * Function given to ExpandGeneric() to obtain an environment variable name.
4526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 char_u *
4528get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004529 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 int idx;
4531{
4532# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4533 /*
4534 * No environ[] on the Amiga and on the Mac (using MPW).
4535 */
4536 return NULL;
4537# else
4538# ifndef __WIN32__
4539 /* Borland C++ 5.2 has this in a header file. */
4540 extern char **environ;
4541# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004542# define ENVNAMELEN 100
4543 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 char_u *str;
4545 int n;
4546
4547 str = (char_u *)environ[idx];
4548 if (str == NULL)
4549 return NULL;
4550
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004551 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
4553 if (str[n] == '=' || str[n] == NUL)
4554 break;
4555 name[n] = str[n];
4556 }
4557 name[n] = NUL;
4558 return name;
4559# endif
4560}
Bram Moolenaar24305862012-08-15 14:05:05 +02004561
4562/*
4563 * Find all user names for user completion.
4564 * Done only once and then cached.
4565 */
4566 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004567init_users()
4568{
Bram Moolenaar24305862012-08-15 14:05:05 +02004569 static int lazy_init_done = FALSE;
4570
4571 if (lazy_init_done)
4572 return;
4573
4574 lazy_init_done = TRUE;
4575 ga_init2(&ga_users, sizeof(char_u *), 20);
4576
4577# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4578 {
4579 char_u* user;
4580 struct passwd* pw;
4581
4582 setpwent();
4583 while ((pw = getpwent()) != NULL)
4584 /* pw->pw_name shouldn't be NULL but just in case... */
4585 if (pw->pw_name != NULL)
4586 {
4587 if (ga_grow(&ga_users, 1) == FAIL)
4588 break;
4589 user = vim_strsave((char_u*)pw->pw_name);
4590 if (user == NULL)
4591 break;
4592 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4593 }
4594 endpwent();
4595 }
4596# endif
4597}
4598
4599/*
4600 * Function given to ExpandGeneric() to obtain an user names.
4601 */
4602 char_u*
4603get_users(xp, idx)
4604 expand_T *xp UNUSED;
4605 int idx;
4606{
4607 init_users();
4608 if (idx < ga_users.ga_len)
4609 return ((char_u **)ga_users.ga_data)[idx];
4610 return NULL;
4611}
4612
4613/*
4614 * Check whether name matches a user name. Return:
4615 * 0 if name does not match any user name.
4616 * 1 if name partially matches the beginning of a user name.
4617 * 2 is name fully matches a user name.
4618 */
4619int match_user(name)
4620 char_u* name;
4621{
4622 int i;
4623 int n = (int)STRLEN(name);
4624 int result = 0;
4625
4626 init_users();
4627 for (i = 0; i < ga_users.ga_len; i++)
4628 {
4629 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4630 return 2; /* full match */
4631 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4632 result = 1; /* partial match */
4633 }
4634 return result;
4635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636#endif
4637
4638/*
4639 * Replace home directory by "~" in each space or comma separated file name in
4640 * 'src'.
4641 * If anything fails (except when out of space) dst equals src.
4642 */
4643 void
4644home_replace(buf, src, dst, dstlen, one)
4645 buf_T *buf; /* when not NULL, check for help files */
4646 char_u *src; /* input file name */
4647 char_u *dst; /* where to put the result */
4648 int dstlen; /* maximum length of the result */
4649 int one; /* if TRUE, only replace one file name, include
4650 spaces and commas in the file name. */
4651{
4652 size_t dirlen = 0, envlen = 0;
4653 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004654 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 char_u *p;
4656
4657 if (src == NULL)
4658 {
4659 *dst = NUL;
4660 return;
4661 }
4662
4663 /*
4664 * If the file is a help file, remove the path completely.
4665 */
4666 if (buf != NULL && buf->b_help)
4667 {
4668 STRCPY(dst, gettail(src));
4669 return;
4670 }
4671
4672 /*
4673 * We check both the value of the $HOME environment variable and the
4674 * "real" home directory.
4675 */
4676 if (homedir != NULL)
4677 dirlen = STRLEN(homedir);
4678
4679#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004680 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004682 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4683#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004684 /* Empty is the same as not set. */
4685 if (homedir_env != NULL && *homedir_env == NUL)
4686 homedir_env = NULL;
4687
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004688#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004689 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004690 {
4691 int usedlen = 0;
4692 int flen;
4693 char_u *fbuf = NULL;
4694
4695 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004696 (void)modify_fname((char_u *)":p", &usedlen,
4697 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004698 flen = (int)STRLEN(homedir_env);
4699 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4700 /* Remove the trailing / that is added to a directory. */
4701 homedir_env[flen - 1] = NUL;
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703#endif
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if (homedir_env != NULL)
4706 envlen = STRLEN(homedir_env);
4707
4708 if (!one)
4709 src = skipwhite(src);
4710 while (*src && dstlen > 0)
4711 {
4712 /*
4713 * Here we are at the beginning of a file name.
4714 * First, check to see if the beginning of the file name matches
4715 * $HOME or the "real" home directory. Check that there is a '/'
4716 * after the match (so that if e.g. the file is "/home/pieter/bla",
4717 * and the home directory is "/home/piet", the file does not end up
4718 * as "~er/bla" (which would seem to indicate the file "bla" in user
4719 * er's home directory)).
4720 */
4721 p = homedir;
4722 len = dirlen;
4723 for (;;)
4724 {
4725 if ( len
4726 && fnamencmp(src, p, len) == 0
4727 && (vim_ispathsep(src[len])
4728 || (!one && (src[len] == ',' || src[len] == ' '))
4729 || src[len] == NUL))
4730 {
4731 src += len;
4732 if (--dstlen > 0)
4733 *dst++ = '~';
4734
4735 /*
4736 * If it's just the home directory, add "/".
4737 */
4738 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4739 *dst++ = '/';
4740 break;
4741 }
4742 if (p == homedir_env)
4743 break;
4744 p = homedir_env;
4745 len = envlen;
4746 }
4747
4748 /* if (!one) skip to separator: space or comma */
4749 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4750 *dst++ = *src++;
4751 /* skip separator */
4752 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4753 *dst++ = *src++;
4754 }
4755 /* if (dstlen == 0) out of space, what to do??? */
4756
4757 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004758
4759 if (homedir_env != homedir_env_orig)
4760 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761}
4762
4763/*
4764 * Like home_replace, store the replaced string in allocated memory.
4765 * When something fails, NULL is returned.
4766 */
4767 char_u *
4768home_replace_save(buf, src)
4769 buf_T *buf; /* when not NULL, check for help files */
4770 char_u *src; /* input file name */
4771{
4772 char_u *dst;
4773 unsigned len;
4774
4775 len = 3; /* space for "~/" and trailing NUL */
4776 if (src != NULL) /* just in case */
4777 len += (unsigned)STRLEN(src);
4778 dst = alloc(len);
4779 if (dst != NULL)
4780 home_replace(buf, src, dst, len, TRUE);
4781 return dst;
4782}
4783
4784/*
4785 * Compare two file names and return:
4786 * FPC_SAME if they both exist and are the same file.
4787 * FPC_SAMEX if they both don't exist and have the same file name.
4788 * FPC_DIFF if they both exist and are different files.
4789 * FPC_NOTX if they both don't exist.
4790 * FPC_DIFFX if one of them doesn't exist.
4791 * For the first name environment variables are expanded
4792 */
4793 int
4794fullpathcmp(s1, s2, checkname)
4795 char_u *s1, *s2;
4796 int checkname; /* when both don't exist, check file names */
4797{
4798#ifdef UNIX
4799 char_u exp1[MAXPATHL];
4800 char_u full1[MAXPATHL];
4801 char_u full2[MAXPATHL];
4802 struct stat st1, st2;
4803 int r1, r2;
4804
4805 expand_env(s1, exp1, MAXPATHL);
4806 r1 = mch_stat((char *)exp1, &st1);
4807 r2 = mch_stat((char *)s2, &st2);
4808 if (r1 != 0 && r2 != 0)
4809 {
4810 /* if mch_stat() doesn't work, may compare the names */
4811 if (checkname)
4812 {
4813 if (fnamecmp(exp1, s2) == 0)
4814 return FPC_SAMEX;
4815 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4816 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4817 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4818 return FPC_SAMEX;
4819 }
4820 return FPC_NOTX;
4821 }
4822 if (r1 != 0 || r2 != 0)
4823 return FPC_DIFFX;
4824 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4825 return FPC_SAME;
4826 return FPC_DIFF;
4827#else
4828 char_u *exp1; /* expanded s1 */
4829 char_u *full1; /* full path of s1 */
4830 char_u *full2; /* full path of s2 */
4831 int retval = FPC_DIFF;
4832 int r1, r2;
4833
4834 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4835 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4836 {
4837 full1 = exp1 + MAXPATHL;
4838 full2 = full1 + MAXPATHL;
4839
4840 expand_env(s1, exp1, MAXPATHL);
4841 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4842 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4843
4844 /* If vim_FullName() fails, the file probably doesn't exist. */
4845 if (r1 != OK && r2 != OK)
4846 {
4847 if (checkname && fnamecmp(exp1, s2) == 0)
4848 retval = FPC_SAMEX;
4849 else
4850 retval = FPC_NOTX;
4851 }
4852 else if (r1 != OK || r2 != OK)
4853 retval = FPC_DIFFX;
4854 else if (fnamecmp(full1, full2))
4855 retval = FPC_DIFF;
4856 else
4857 retval = FPC_SAME;
4858 vim_free(exp1);
4859 }
4860 return retval;
4861#endif
4862}
4863
4864/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004865 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004866 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004867 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 */
4869 char_u *
4870gettail(fname)
4871 char_u *fname;
4872{
4873 char_u *p1, *p2;
4874
4875 if (fname == NULL)
4876 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004877 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004879 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004881 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 return p1;
4884}
4885
Bram Moolenaar31710262010-08-13 13:36:15 +02004886#if defined(FEAT_SEARCHPATH)
4887static char_u *gettail_dir __ARGS((char_u *fname));
4888
4889/*
4890 * Return the end of the directory name, on the first path
4891 * separator:
4892 * "/path/file", "/path/dir/", "/path//dir", "/file"
4893 * ^ ^ ^ ^
4894 */
4895 static char_u *
4896gettail_dir(fname)
4897 char_u *fname;
4898{
4899 char_u *dir_end = fname;
4900 char_u *next_dir_end = fname;
4901 int look_for_sep = TRUE;
4902 char_u *p;
4903
4904 for (p = fname; *p != NUL; )
4905 {
4906 if (vim_ispathsep(*p))
4907 {
4908 if (look_for_sep)
4909 {
4910 next_dir_end = p;
4911 look_for_sep = FALSE;
4912 }
4913 }
4914 else
4915 {
4916 if (!look_for_sep)
4917 dir_end = next_dir_end;
4918 look_for_sep = TRUE;
4919 }
4920 mb_ptr_adv(p);
4921 }
4922 return dir_end;
4923}
4924#endif
4925
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004927 * Get pointer to tail of "fname", including path separators. Putting a NUL
4928 * here leaves the directory name. Takes care of "c:/" and "//".
4929 * Always returns a valid pointer.
4930 */
4931 char_u *
4932gettail_sep(fname)
4933 char_u *fname;
4934{
4935 char_u *p;
4936 char_u *t;
4937
4938 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4939 t = gettail(fname);
4940 while (t > p && after_pathsep(fname, t))
4941 --t;
4942#ifdef VMS
4943 /* path separator is part of the path */
4944 ++t;
4945#endif
4946 return t;
4947}
4948
4949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 * get the next path component (just after the next path separator).
4951 */
4952 char_u *
4953getnextcomp(fname)
4954 char_u *fname;
4955{
4956 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004957 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (*fname)
4959 ++fname;
4960 return fname;
4961}
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * Get a pointer to one character past the head of a path name.
4965 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4966 * If there is no head, path is returned.
4967 */
4968 char_u *
4969get_past_head(path)
4970 char_u *path;
4971{
4972 char_u *retval;
4973
4974#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4975 /* may skip "c:" */
4976 if (isalpha(path[0]) && path[1] == ':')
4977 retval = path + 2;
4978 else
4979 retval = path;
4980#else
4981# if defined(AMIGA)
4982 /* may skip "label:" */
4983 retval = vim_strchr(path, ':');
4984 if (retval == NULL)
4985 retval = path;
4986# else /* Unix */
4987 retval = path;
4988# endif
4989#endif
4990
4991 while (vim_ispathsep(*retval))
4992 ++retval;
4993
4994 return retval;
4995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004998 * Return TRUE if 'c' is a path separator.
4999 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 */
5001 int
5002vim_ispathsep(c)
5003 int c;
5004{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005005#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005007#else
5008# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005010# else
5011# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5013 return (c == ':' || c == '[' || c == ']' || c == '/'
5014 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005015# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005017# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020}
5021
Bram Moolenaar69c35002013-11-04 02:54:12 +01005022/*
5023 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5024 */
5025 int
5026vim_ispathsep_nocolon(c)
5027 int c;
5028{
5029 return vim_ispathsep(c)
5030#ifdef BACKSLASH_IN_FILENAME
5031 && c != ':'
5032#endif
5033 ;
5034}
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5037/*
5038 * return TRUE if 'c' is a path list separator.
5039 */
5040 int
5041vim_ispathlistsep(c)
5042 int c;
5043{
5044#ifdef UNIX
5045 return (c == ':');
5046#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005047 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048#endif
5049}
5050#endif
5051
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005052#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5053 || defined(FEAT_EVAL) || defined(PROTO)
5054/*
5055 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5056 * It's done in-place.
5057 */
5058 void
5059shorten_dir(str)
5060 char_u *str;
5061{
5062 char_u *tail, *s, *d;
5063 int skip = FALSE;
5064
5065 tail = gettail(str);
5066 d = str;
5067 for (s = str; ; ++s)
5068 {
5069 if (s >= tail) /* copy the whole tail */
5070 {
5071 *d++ = *s;
5072 if (*s == NUL)
5073 break;
5074 }
5075 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5076 {
5077 *d++ = *s;
5078 skip = FALSE;
5079 }
5080 else if (!skip)
5081 {
5082 *d++ = *s; /* copy next char */
5083 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5084 skip = TRUE;
5085# ifdef FEAT_MBYTE
5086 if (has_mbyte)
5087 {
5088 int l = mb_ptr2len(s);
5089
5090 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005091 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005092 }
5093# endif
5094 }
5095 }
5096}
5097#endif
5098
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005099/*
5100 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5101 * Also returns TRUE if there is no directory name.
5102 * "fname" must be writable!.
5103 */
5104 int
5105dir_of_file_exists(fname)
5106 char_u *fname;
5107{
5108 char_u *p;
5109 int c;
5110 int retval;
5111
5112 p = gettail_sep(fname);
5113 if (p == fname)
5114 return TRUE;
5115 c = *p;
5116 *p = NUL;
5117 retval = mch_isdir(fname);
5118 *p = c;
5119 return retval;
5120}
5121
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005123 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5124 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 */
5126 int
5127vim_fnamecmp(x, y)
5128 char_u *x, *y;
5129{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005130#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005132#else
5133 if (p_fic)
5134 return MB_STRICMP(x, y);
5135 return STRCMP(x, y);
5136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137}
5138
5139 int
5140vim_fnamencmp(x, y, len)
5141 char_u *x, *y;
5142 size_t len;
5143{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005144#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005145 char_u *px = x;
5146 char_u *py = y;
5147 int cx = NUL;
5148 int cy = NUL;
5149
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005150 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005152 cx = PTR2CHAR(px);
5153 cy = PTR2CHAR(py);
5154 if (cx == NUL || cy == NUL
5155 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5156 && !(cx == '/' && cy == '\\')
5157 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005159 len -= MB_PTR2LEN(px);
5160 px += MB_PTR2LEN(px);
5161 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163 if (len == 0)
5164 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005165 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005166#else
5167 if (p_fic)
5168 return MB_STRNICMP(x, y, len);
5169 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005171}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173/*
5174 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005175 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 */
5177 char_u *
5178concat_fnames(fname1, fname2, sep)
5179 char_u *fname1;
5180 char_u *fname2;
5181 int sep;
5182{
5183 char_u *dest;
5184
5185 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5186 if (dest != NULL)
5187 {
5188 STRCPY(dest, fname1);
5189 if (sep)
5190 add_pathsep(dest);
5191 STRCAT(dest, fname2);
5192 }
5193 return dest;
5194}
5195
Bram Moolenaard6754642005-01-17 22:18:45 +00005196/*
5197 * Concatenate two strings and return the result in allocated memory.
5198 * Returns NULL when out of memory.
5199 */
5200 char_u *
5201concat_str(str1, str2)
5202 char_u *str1;
5203 char_u *str2;
5204{
5205 char_u *dest;
5206 size_t l = STRLEN(str1);
5207
5208 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5209 if (dest != NULL)
5210 {
5211 STRCPY(dest, str1);
5212 STRCPY(dest + l, str2);
5213 }
5214 return dest;
5215}
Bram Moolenaard6754642005-01-17 22:18:45 +00005216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217/*
5218 * Add a path separator to a file name, unless it already ends in a path
5219 * separator.
5220 */
5221 void
5222add_pathsep(p)
5223 char_u *p;
5224{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005225 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 STRCAT(p, PATHSEPSTR);
5227}
5228
5229/*
5230 * FullName_save - Make an allocated copy of a full file name.
5231 * Returns NULL when out of memory.
5232 */
5233 char_u *
5234FullName_save(fname, force)
5235 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005236 int force; /* force expansion, even when it already looks
5237 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
5239 char_u *buf;
5240 char_u *new_fname = NULL;
5241
5242 if (fname == NULL)
5243 return NULL;
5244
5245 buf = alloc((unsigned)MAXPATHL);
5246 if (buf != NULL)
5247 {
5248 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5249 new_fname = vim_strsave(buf);
5250 else
5251 new_fname = vim_strsave(fname);
5252 vim_free(buf);
5253 }
5254 return new_fname;
5255}
5256
5257#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5258
5259static char_u *skip_string __ARGS((char_u *p));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005260static pos_T *ind_find_start_comment __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
5262/*
5263 * Find the start of a comment, not knowing if we are in a comment right now.
5264 * Search starts at w_cursor.lnum and goes backwards.
5265 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005266 static pos_T *
5267ind_find_start_comment() /* XXX */
5268{
5269 return find_start_comment(curbuf->b_ind_maxcomment);
5270}
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 pos_T *
5273find_start_comment(ind_maxcomment) /* XXX */
5274 int ind_maxcomment;
5275{
5276 pos_T *pos;
5277 char_u *line;
5278 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005279 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005281 for (;;)
5282 {
5283 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5284 if (pos == NULL)
5285 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005287 /*
5288 * Check if the comment start we found is inside a string.
5289 * If it is then restrict the search to below this line and try again.
5290 */
5291 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005292 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005293 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005294 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005295 break;
5296 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5297 if (cur_maxcomment <= 0)
5298 {
5299 pos = NULL;
5300 break;
5301 }
5302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 return pos;
5304}
5305
5306/*
5307 * Skip to the end of a "string" and a 'c' character.
5308 * If there is no string or character, return argument unmodified.
5309 */
5310 static char_u *
5311skip_string(p)
5312 char_u *p;
5313{
5314 int i;
5315
5316 /*
5317 * We loop, because strings may be concatenated: "date""time".
5318 */
5319 for ( ; ; ++p)
5320 {
5321 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5322 {
5323 if (!p[1]) /* ' at end of line */
5324 break;
5325 i = 2;
5326 if (p[1] == '\\') /* '\n' or '\000' */
5327 {
5328 ++i;
5329 while (vim_isdigit(p[i - 1])) /* '\000' */
5330 ++i;
5331 }
5332 if (p[i] == '\'') /* check for trailing ' */
5333 {
5334 p += i;
5335 continue;
5336 }
5337 }
5338 else if (p[0] == '"') /* start of string */
5339 {
5340 for (++p; p[0]; ++p)
5341 {
5342 if (p[0] == '\\' && p[1] != NUL)
5343 ++p;
5344 else if (p[0] == '"') /* end of string */
5345 break;
5346 }
5347 if (p[0] == '"')
5348 continue;
5349 }
5350 break; /* no string found */
5351 }
5352 if (!*p)
5353 --p; /* backup from NUL */
5354 return p;
5355}
5356#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5357
5358#if defined(FEAT_CINDENT) || defined(PROTO)
5359
5360/*
5361 * Do C or expression indenting on the current line.
5362 */
5363 void
5364do_c_expr_indent()
5365{
5366# ifdef FEAT_EVAL
5367 if (*curbuf->b_p_inde != NUL)
5368 fixthisline(get_expr_indent);
5369 else
5370# endif
5371 fixthisline(get_c_indent);
5372}
5373
5374/*
5375 * Functions for C-indenting.
5376 * Most of this originally comes from Eric Fischer.
5377 */
5378/*
5379 * Below "XXX" means that this function may unlock the current line.
5380 */
5381
5382static char_u *cin_skipcomment __ARGS((char_u *));
5383static int cin_nocode __ARGS((char_u *));
5384static pos_T *find_line_comment __ARGS((void));
5385static int cin_islabel_skip __ARGS((char_u **));
5386static int cin_isdefault __ARGS((char_u *));
5387static char_u *after_label __ARGS((char_u *l));
5388static int get_indent_nolabel __ARGS((linenr_T lnum));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005389static int skip_label __ARGS((linenr_T, char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390static int cin_first_id_amount __ARGS((void));
5391static int cin_get_equal_amount __ARGS((linenr_T lnum));
5392static int cin_ispreproc __ARGS((char_u *));
5393static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5394static int cin_iscomment __ARGS((char_u *));
5395static int cin_islinecomment __ARGS((char_u *));
5396static int cin_isterminated __ARGS((char_u *, int, int));
5397static int cin_isinit __ARGS((void));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005398static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399static int cin_isif __ARGS((char_u *));
5400static int cin_iselse __ARGS((char_u *));
5401static int cin_isdo __ARGS((char_u *));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005402static int cin_iswhileofdo __ARGS((char_u *, linenr_T));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005403static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005404static int cin_iswhileofdo_end __ARGS((int terminated));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005406static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005407static int get_baseclass_amount __ARGS((int col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005409static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410static int cin_skip2pos __ARGS((pos_T *trypos));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005411static pos_T *find_start_brace __ARGS((void));
5412static pos_T *find_match_paren __ARGS((int));
5413static int corr_ind_maxparen __ARGS((pos_T *startpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414static int find_last_paren __ARGS((char_u *l, int start, int end));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005415static int find_match __ARGS((int lookfor, linenr_T ourscope));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005416static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
5418/*
5419 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005420 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 */
5422 static char_u *
5423cin_skipcomment(s)
5424 char_u *s;
5425{
5426 while (*s)
5427 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005428 char_u *prev_s = s;
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005431
5432 /* Perl/shell # comment comment continues until eol. Require a space
5433 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005434 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005435 {
5436 s += STRLEN(s);
5437 break;
5438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 if (*s != '/')
5440 break;
5441 ++s;
5442 if (*s == '/') /* slash-slash comment continues till eol */
5443 {
5444 s += STRLEN(s);
5445 break;
5446 }
5447 if (*s != '*')
5448 break;
5449 for (++s; *s; ++s) /* skip slash-star comment */
5450 if (s[0] == '*' && s[1] == '/')
5451 {
5452 s += 2;
5453 break;
5454 }
5455 }
5456 return s;
5457}
5458
5459/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005460 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 * not considered code.
5462 */
5463 static int
5464cin_nocode(s)
5465 char_u *s;
5466{
5467 return *cin_skipcomment(s) == NUL;
5468}
5469
5470/*
5471 * Check previous lines for a "//" line comment, skipping over blank lines.
5472 */
5473 static pos_T *
5474find_line_comment() /* XXX */
5475{
5476 static pos_T pos;
5477 char_u *line;
5478 char_u *p;
5479
5480 pos = curwin->w_cursor;
5481 while (--pos.lnum > 0)
5482 {
5483 line = ml_get(pos.lnum);
5484 p = skipwhite(line);
5485 if (cin_islinecomment(p))
5486 {
5487 pos.col = (int)(p - line);
5488 return &pos;
5489 }
5490 if (*p != NUL)
5491 break;
5492 }
5493 return NULL;
5494}
5495
5496/*
5497 * Check if string matches "label:"; move to character after ':' if true.
5498 */
5499 static int
5500cin_islabel_skip(s)
5501 char_u **s;
5502{
5503 if (!vim_isIDc(**s)) /* need at least one ID character */
5504 return FALSE;
5505
5506 while (vim_isIDc(**s))
5507 (*s)++;
5508
5509 *s = cin_skipcomment(*s);
5510
5511 /* "::" is not a label, it's C++ */
5512 return (**s == ':' && *++*s != ':');
5513}
5514
5515/*
5516 * Recognize a label: "label:".
5517 * Note: curwin->w_cursor must be where we are looking for the label.
5518 */
5519 int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005520cin_islabel() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 char_u *s;
5523
5524 s = cin_skipcomment(ml_get_curline());
5525
5526 /*
5527 * Exclude "default" from labels, since it should be indented
5528 * like a switch label. Same for C++ scope declarations.
5529 */
5530 if (cin_isdefault(s))
5531 return FALSE;
5532 if (cin_isscopedecl(s))
5533 return FALSE;
5534
5535 if (cin_islabel_skip(&s))
5536 {
5537 /*
5538 * Only accept a label if the previous line is terminated or is a case
5539 * label.
5540 */
5541 pos_T cursor_save;
5542 pos_T *trypos;
5543 char_u *line;
5544
5545 cursor_save = curwin->w_cursor;
5546 while (curwin->w_cursor.lnum > 1)
5547 {
5548 --curwin->w_cursor.lnum;
5549
5550 /*
5551 * If we're in a comment now, skip to the start of the comment.
5552 */
5553 curwin->w_cursor.col = 0;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005554 if ((trypos = ind_find_start_comment()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 curwin->w_cursor = *trypos;
5556
5557 line = ml_get_curline();
5558 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5559 continue;
5560 if (*(line = cin_skipcomment(line)) == NUL)
5561 continue;
5562
5563 curwin->w_cursor = cursor_save;
5564 if (cin_isterminated(line, TRUE, FALSE)
5565 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005566 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 || (cin_islabel_skip(&line) && cin_nocode(line)))
5568 return TRUE;
5569 return FALSE;
5570 }
5571 curwin->w_cursor = cursor_save;
5572 return TRUE; /* label at start of file??? */
5573 }
5574 return FALSE;
5575}
5576
5577/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005578 * Recognize structure initialization and enumerations:
5579 * "[typedef] [static|public|protected|private] enum"
5580 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 */
5582 static int
5583cin_isinit(void)
5584{
5585 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005586 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588 s = cin_skipcomment(ml_get_curline());
5589
Bram Moolenaar75342212013-03-07 13:13:52 +01005590 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 s = cin_skipcomment(s + 7);
5592
Bram Moolenaar75342212013-03-07 13:13:52 +01005593 for (;;)
5594 {
5595 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005596
Bram Moolenaar75342212013-03-07 13:13:52 +01005597 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5598 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005599 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005600 if (cin_starts_with(s, skip[i]))
5601 {
5602 s = cin_skipcomment(s + l);
5603 l = 0;
5604 break;
5605 }
5606 }
5607 if (l != 0)
5608 break;
5609 }
5610
5611 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 return TRUE;
5613
5614 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5615 return TRUE;
5616
5617 return FALSE;
5618}
5619
5620/*
5621 * Recognize a switch label: "case .*:" or "default:".
5622 */
5623 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005624cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005626 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005629 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
5631 for (s += 4; *s; ++s)
5632 {
5633 s = cin_skipcomment(s);
5634 if (*s == ':')
5635 {
5636 if (s[1] == ':') /* skip over "::" for C++ */
5637 ++s;
5638 else
5639 return TRUE;
5640 }
5641 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005642 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5644 return FALSE; /* stop at comment */
5645 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005646 {
5647 /* JS etc. */
5648 if (strict)
5649 return FALSE; /* stop at string */
5650 else
5651 return TRUE;
5652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
5654 return FALSE;
5655 }
5656
5657 if (cin_isdefault(s))
5658 return TRUE;
5659 return FALSE;
5660}
5661
5662/*
5663 * Recognize a "default" switch label.
5664 */
5665 static int
5666cin_isdefault(s)
5667 char_u *s;
5668{
5669 return (STRNCMP(s, "default", 7) == 0
5670 && *(s = cin_skipcomment(s + 7)) == ':'
5671 && s[1] != ':');
5672}
5673
5674/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005675 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 */
5677 int
5678cin_isscopedecl(s)
5679 char_u *s;
5680{
5681 int i;
5682
5683 s = cin_skipcomment(s);
5684 if (STRNCMP(s, "public", 6) == 0)
5685 i = 6;
5686 else if (STRNCMP(s, "protected", 9) == 0)
5687 i = 9;
5688 else if (STRNCMP(s, "private", 7) == 0)
5689 i = 7;
5690 else
5691 return FALSE;
5692 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5693}
5694
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005695/* Maximum number of lines to search back for a "namespace" line. */
5696#define FIND_NAMESPACE_LIM 20
5697
5698/*
5699 * Recognize a "namespace" scope declaration.
5700 */
5701 static int
5702cin_is_cpp_namespace(s)
5703 char_u *s;
5704{
5705 char_u *p;
5706 int has_name = FALSE;
5707
5708 s = cin_skipcomment(s);
5709 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5710 {
5711 p = cin_skipcomment(skipwhite(s + 9));
5712 while (*p != NUL)
5713 {
5714 if (vim_iswhite(*p))
5715 {
5716 has_name = TRUE; /* found end of a name */
5717 p = cin_skipcomment(skipwhite(p));
5718 }
5719 else if (*p == '{')
5720 {
5721 break;
5722 }
5723 else if (vim_iswordc(*p))
5724 {
5725 if (has_name)
5726 return FALSE; /* word character after skipping past name */
5727 ++p;
5728 }
5729 else
5730 {
5731 return FALSE;
5732 }
5733 }
5734 return TRUE;
5735 }
5736 return FALSE;
5737}
5738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739/*
5740 * Return a pointer to the first non-empty non-comment character after a ':'.
5741 * Return NULL if not found.
5742 * case 234: a = b;
5743 * ^
5744 */
5745 static char_u *
5746after_label(l)
5747 char_u *l;
5748{
5749 for ( ; *l; ++l)
5750 {
5751 if (*l == ':')
5752 {
5753 if (l[1] == ':') /* skip over "::" for C++ */
5754 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005755 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 break;
5757 }
5758 else if (*l == '\'' && l[1] && l[2] == '\'')
5759 l += 2; /* skip over 'x' */
5760 }
5761 if (*l == NUL)
5762 return NULL;
5763 l = cin_skipcomment(l + 1);
5764 if (*l == NUL)
5765 return NULL;
5766 return l;
5767}
5768
5769/*
5770 * Get indent of line "lnum", skipping a label.
5771 * Return 0 if there is nothing after the label.
5772 */
5773 static int
5774get_indent_nolabel(lnum) /* XXX */
5775 linenr_T lnum;
5776{
5777 char_u *l;
5778 pos_T fp;
5779 colnr_T col;
5780 char_u *p;
5781
5782 l = ml_get(lnum);
5783 p = after_label(l);
5784 if (p == NULL)
5785 return 0;
5786
5787 fp.col = (colnr_T)(p - l);
5788 fp.lnum = lnum;
5789 getvcol(curwin, &fp, &col, NULL, NULL);
5790 return (int)col;
5791}
5792
5793/*
5794 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005795 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 * label: if (asdf && asdfasdf)
5797 * ^
5798 */
5799 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005800skip_label(lnum, pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 linenr_T lnum;
5802 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 char_u *l;
5805 int amount;
5806 pos_T cursor_save;
5807
5808 cursor_save = curwin->w_cursor;
5809 curwin->w_cursor.lnum = lnum;
5810 l = ml_get_curline();
5811 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005812 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {
5814 amount = get_indent_nolabel(lnum);
5815 l = after_label(ml_get_curline());
5816 if (l == NULL) /* just in case */
5817 l = ml_get_curline();
5818 }
5819 else
5820 {
5821 amount = get_indent();
5822 l = ml_get_curline();
5823 }
5824 *pp = l;
5825
5826 curwin->w_cursor = cursor_save;
5827 return amount;
5828}
5829
5830/*
5831 * Return the indent of the first variable name after a type in a declaration.
5832 * int a, indent of "a"
5833 * static struct foo b, indent of "b"
5834 * enum bla c, indent of "c"
5835 * Returns zero when it doesn't look like a declaration.
5836 */
5837 static int
5838cin_first_id_amount()
5839{
5840 char_u *line, *p, *s;
5841 int len;
5842 pos_T fp;
5843 colnr_T col;
5844
5845 line = ml_get_curline();
5846 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005847 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5849 {
5850 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005851 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 }
5853 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5854 p = skipwhite(p + 6);
5855 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5856 p = skipwhite(p + 4);
5857 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5858 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5859 {
5860 s = skipwhite(p + len);
5861 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5862 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5863 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5864 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5865 p = s;
5866 }
5867 for (len = 0; vim_isIDc(p[len]); ++len)
5868 ;
5869 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5870 return 0;
5871
5872 p = skipwhite(p + len);
5873 fp.lnum = curwin->w_cursor.lnum;
5874 fp.col = (colnr_T)(p - line);
5875 getvcol(curwin, &fp, &col, NULL, NULL);
5876 return (int)col;
5877}
5878
5879/*
5880 * Return the indent of the first non-blank after an equal sign.
5881 * char *foo = "here";
5882 * Return zero if no (useful) equal sign found.
5883 * Return -1 if the line above "lnum" ends in a backslash.
5884 * foo = "asdf\
5885 * asdf\
5886 * here";
5887 */
5888 static int
5889cin_get_equal_amount(lnum)
5890 linenr_T lnum;
5891{
5892 char_u *line;
5893 char_u *s;
5894 colnr_T col;
5895 pos_T fp;
5896
5897 if (lnum > 1)
5898 {
5899 line = ml_get(lnum - 1);
5900 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5901 return -1;
5902 }
5903
5904 line = s = ml_get(lnum);
5905 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5906 {
5907 if (cin_iscomment(s)) /* ignore comments */
5908 s = cin_skipcomment(s);
5909 else
5910 ++s;
5911 }
5912 if (*s != '=')
5913 return 0;
5914
5915 s = skipwhite(s + 1);
5916 if (cin_nocode(s))
5917 return 0;
5918
5919 if (*s == '"') /* nice alignment for continued strings */
5920 ++s;
5921
5922 fp.lnum = lnum;
5923 fp.col = (colnr_T)(s - line);
5924 getvcol(curwin, &fp, &col, NULL, NULL);
5925 return (int)col;
5926}
5927
5928/*
5929 * Recognize a preprocessor statement: Any line that starts with '#'.
5930 */
5931 static int
5932cin_ispreproc(s)
5933 char_u *s;
5934{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005935 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 return TRUE;
5937 return FALSE;
5938}
5939
5940/*
5941 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5942 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5943 * start and return the line in "*pp".
5944 */
5945 static int
5946cin_ispreproc_cont(pp, lnump)
5947 char_u **pp;
5948 linenr_T *lnump;
5949{
5950 char_u *line = *pp;
5951 linenr_T lnum = *lnump;
5952 int retval = FALSE;
5953
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005954 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 {
5956 if (cin_ispreproc(line))
5957 {
5958 retval = TRUE;
5959 *lnump = lnum;
5960 break;
5961 }
5962 if (lnum == 1)
5963 break;
5964 line = ml_get(--lnum);
5965 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5966 break;
5967 }
5968
5969 if (lnum != *lnump)
5970 *pp = ml_get(*lnump);
5971 return retval;
5972}
5973
5974/*
5975 * Recognize the start of a C or C++ comment.
5976 */
5977 static int
5978cin_iscomment(p)
5979 char_u *p;
5980{
5981 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5982}
5983
5984/*
5985 * Recognize the start of a "//" comment.
5986 */
5987 static int
5988cin_islinecomment(p)
5989 char_u *p;
5990{
5991 return (p[0] == '/' && p[1] == '/');
5992}
5993
5994/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005995 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5996 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005998 * If a line begins with an "else", only consider it terminated if no unmatched
5999 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 * Return the character terminating the line (ending char's have precedence if
6001 * both apply in order to determine initializations).
6002 */
6003 static int
6004cin_isterminated(s, incl_open, incl_comma)
6005 char_u *s;
6006 int incl_open; /* include '{' at the end as terminator */
6007 int incl_comma; /* recognize a trailing comma */
6008{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006009 char_u found_start = 0;
6010 unsigned n_open = 0;
6011 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
6013 s = cin_skipcomment(s);
6014
6015 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6016 found_start = *s;
6017
Bram Moolenaar496f9512011-05-19 16:35:09 +02006018 if (!found_start)
6019 is_else = cin_iselse(s);
6020
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 while (*s)
6022 {
6023 /* skip over comments, "" strings and 'c'haracters */
6024 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006025 if (*s == '}' && n_open > 0)
6026 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006027 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006028 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 && cin_nocode(s + 1))
6030 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006031 else if (*s == '{')
6032 {
6033 if (incl_open && cin_nocode(s + 1))
6034 return *s;
6035 else
6036 ++n_open;
6037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
6039 if (*s)
6040 s++;
6041 }
6042 return found_start;
6043}
6044
6045/*
6046 * Recognize the basic picture of a function declaration -- it needs to
6047 * have an open paren somewhere and a close paren at the end of the line and
6048 * no semicolons anywhere.
6049 * When a line ends in a comma we continue looking in the next line.
6050 * "sp" points to a string with the line. When looking at other lines it must
6051 * be restored to the line. When it's NULL fetch lines here.
6052 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006053 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 */
6055 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006056cin_isfuncdecl(sp, first_lnum, min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 char_u **sp;
6058 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006059 linenr_T min_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
6061 char_u *s;
6062 linenr_T lnum = first_lnum;
6063 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006064 pos_T *trypos;
6065 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
6067 if (sp == NULL)
6068 s = ml_get(lnum);
6069 else
6070 s = *sp;
6071
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006072 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006073 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006074 {
6075 lnum = trypos->lnum;
6076 if (lnum < min_lnum)
6077 return FALSE;
6078
6079 s = ml_get(lnum);
6080 }
6081
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006082 /* Ignore line starting with #. */
6083 if (cin_ispreproc(s))
6084 return FALSE;
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6087 {
6088 if (cin_iscomment(s)) /* ignore comments */
6089 s = cin_skipcomment(s);
6090 else
6091 ++s;
6092 }
6093 if (*s != '(')
6094 return FALSE; /* ';', ' or " before any () or no '(' */
6095
6096 while (*s && *s != ';' && *s != '\'' && *s != '"')
6097 {
6098 if (*s == ')' && cin_nocode(s + 1))
6099 {
6100 /* ')' at the end: may have found a match
6101 * Check for he previous line not to end in a backslash:
6102 * #if defined(x) && \
6103 * defined(y)
6104 */
6105 lnum = first_lnum - 1;
6106 s = ml_get(lnum);
6107 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6108 retval = TRUE;
6109 goto done;
6110 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006111 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006113 int comma = (*s == ',');
6114
6115 /* ',' at the end: continue looking in the next line.
6116 * At the end: check for ',' in the next line, for this style:
6117 * func(arg1
6118 * , arg2) */
6119 for (;;)
6120 {
6121 if (lnum >= curbuf->b_ml.ml_line_count)
6122 break;
6123 s = ml_get(++lnum);
6124 if (!cin_ispreproc(s))
6125 break;
6126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 if (lnum >= curbuf->b_ml.ml_line_count)
6128 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006129 /* Require a comma at end of the line or a comma or ')' at the
6130 * start of next line. */
6131 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006132 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006133 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006134 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 }
6136 else if (cin_iscomment(s)) /* ignore comments */
6137 s = cin_skipcomment(s);
6138 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006141 just_started = FALSE;
6142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 }
6144
6145done:
6146 if (lnum != first_lnum && sp != NULL)
6147 *sp = ml_get(first_lnum);
6148
6149 return retval;
6150}
6151
6152 static int
6153cin_isif(p)
6154 char_u *p;
6155{
6156 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6157}
6158
6159 static int
6160cin_iselse(p)
6161 char_u *p;
6162{
6163 if (*p == '}') /* accept "} else" */
6164 p = cin_skipcomment(p + 1);
6165 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6166}
6167
6168 static int
6169cin_isdo(p)
6170 char_u *p;
6171{
6172 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6173}
6174
6175/*
6176 * Check if this is a "while" that should have a matching "do".
6177 * We only accept a "while (condition) ;", with only white space between the
6178 * ')' and ';'. The condition may be spread over several lines.
6179 */
6180 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006181cin_iswhileofdo(p, lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 char_u *p;
6183 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
6185 pos_T cursor_save;
6186 pos_T *trypos;
6187 int retval = FALSE;
6188
6189 p = cin_skipcomment(p);
6190 if (*p == '}') /* accept "} while (cond);" */
6191 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006192 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
6194 cursor_save = curwin->w_cursor;
6195 curwin->w_cursor.lnum = lnum;
6196 curwin->w_cursor.col = 0;
6197 p = ml_get_curline();
6198 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6199 {
6200 ++p;
6201 ++curwin->w_cursor.col;
6202 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006203 if ((trypos = findmatchlimit(NULL, 0, 0,
6204 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6206 retval = TRUE;
6207 curwin->w_cursor = cursor_save;
6208 }
6209 return retval;
6210}
6211
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006212/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006213 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006214 * Return 0 if there is none.
6215 * Otherwise return !0 and update "*poffset" to point to the place where the
6216 * string was found.
6217 */
6218 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006219cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006220 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006221 int *poffset;
6222{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006223 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006224
6225 if (offset-- < 2)
6226 return 0;
6227 while (offset > 2 && vim_iswhite(line[offset]))
6228 --offset;
6229
6230 offset -= 1;
6231 if (!STRNCMP(line + offset, "if", 2))
6232 goto probablyFound;
6233
6234 if (offset >= 1)
6235 {
6236 offset -= 1;
6237 if (!STRNCMP(line + offset, "for", 3))
6238 goto probablyFound;
6239
6240 if (offset >= 2)
6241 {
6242 offset -= 2;
6243 if (!STRNCMP(line + offset, "while", 5))
6244 goto probablyFound;
6245 }
6246 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006247 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006248
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006249probablyFound:
6250 if (!offset || !vim_isIDc(line[offset - 1]))
6251 {
6252 *poffset = offset;
6253 return 1;
6254 }
6255 return 0;
6256}
6257
6258/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006259 * Return TRUE if we are at the end of a do-while.
6260 * do
6261 * nothing;
6262 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006263 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006264 * Adjust the cursor to the line with "while".
6265 */
6266 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006267cin_iswhileofdo_end(terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006268 int terminated;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006269{
6270 char_u *line;
6271 char_u *p;
6272 char_u *s;
6273 pos_T *trypos;
6274 int i;
6275
6276 if (terminated != ';') /* there must be a ';' at the end */
6277 return FALSE;
6278
6279 p = line = ml_get_curline();
6280 while (*p != NUL)
6281 {
6282 p = cin_skipcomment(p);
6283 if (*p == ')')
6284 {
6285 s = skipwhite(p + 1);
6286 if (*s == ';' && cin_nocode(s + 1))
6287 {
6288 /* Found ");" at end of the line, now check there is "while"
6289 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006290 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006291 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006292 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006293 if (trypos != NULL)
6294 {
6295 s = cin_skipcomment(ml_get(trypos->lnum));
6296 if (*s == '}') /* accept "} while (cond);" */
6297 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006298 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006299 {
6300 curwin->w_cursor.lnum = trypos->lnum;
6301 return TRUE;
6302 }
6303 }
6304
6305 /* Searching may have made "line" invalid, get it again. */
6306 line = ml_get_curline();
6307 p = line + i;
6308 }
6309 }
6310 if (*p != NUL)
6311 ++p;
6312 }
6313 return FALSE;
6314}
6315
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 static int
6317cin_isbreak(p)
6318 char_u *p;
6319{
6320 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6321}
6322
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006323/*
6324 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 * constructor-initialization. eg:
6326 *
6327 * class MyClass :
6328 * baseClass <-- here
6329 * class MyClass : public baseClass,
6330 * anotherBaseClass <-- here (should probably lineup ??)
6331 * MyClass::MyClass(...) :
6332 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006333 *
6334 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 */
6336 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006337cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006338 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 char_u *s;
6341 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006342 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006343 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344
6345 *col = 0;
6346
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006347 s = skipwhite(line);
6348 if (*s == '#') /* skip #define FOO x ? (x) : x */
6349 return FALSE;
6350 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 if (*s == NUL)
6352 return FALSE;
6353
6354 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6355
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006356 /* Search for a line starting with '#', empty, ending in ';' or containing
6357 * '{' or '}' and start below it. This handles the following situations:
6358 * a = cond ?
6359 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006360 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006361 * func::foo()
6362 * : something
6363 * {}
6364 * Foo::Foo (int one, int two)
6365 * : something(4),
6366 * somethingelse(3)
6367 * {}
6368 */
6369 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006371 line = ml_get(lnum - 1);
6372 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006373 if (*s == '#' || *s == NUL)
6374 break;
6375 while (*s != NUL)
6376 {
6377 s = cin_skipcomment(s);
6378 if (*s == '{' || *s == '}'
6379 || (*s == ';' && cin_nocode(s + 1)))
6380 break;
6381 if (*s != NUL)
6382 ++s;
6383 }
6384 if (*s != NUL)
6385 break;
6386 --lnum;
6387 }
6388
Bram Moolenaare7c56862007-08-04 10:14:52 +00006389 line = ml_get(lnum);
6390 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006391 for (;;)
6392 {
6393 if (*s == NUL)
6394 {
6395 if (lnum == curwin->w_cursor.lnum)
6396 break;
6397 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006398 line = ml_get(++lnum);
6399 s = cin_skipcomment(line);
6400 if (*s == NUL)
6401 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006402 }
6403
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006404 if (s[0] == '"')
6405 s = skip_string(s) + 1;
6406 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
6408 if (s[1] == ':')
6409 {
6410 /* skip double colon. It can't be a constructor
6411 * initialization any more */
6412 lookfor_ctor_init = FALSE;
6413 s = cin_skipcomment(s + 2);
6414 }
6415 else if (lookfor_ctor_init || class_or_struct)
6416 {
6417 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006418 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 cpp_base_class = TRUE;
6420 lookfor_ctor_init = class_or_struct = FALSE;
6421 *col = 0;
6422 s = cin_skipcomment(s + 1);
6423 }
6424 else
6425 s = cin_skipcomment(s + 1);
6426 }
6427 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6428 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6429 {
6430 class_or_struct = TRUE;
6431 lookfor_ctor_init = FALSE;
6432
6433 if (*s == 'c')
6434 s = cin_skipcomment(s + 5);
6435 else
6436 s = cin_skipcomment(s + 6);
6437 }
6438 else
6439 {
6440 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6441 {
6442 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6443 }
6444 else if (s[0] == ')')
6445 {
6446 /* Constructor-initialization is assumed if we come across
6447 * something like "):" */
6448 class_or_struct = FALSE;
6449 lookfor_ctor_init = TRUE;
6450 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006451 else if (s[0] == '?')
6452 {
6453 /* Avoid seeing '() :' after '?' as constructor init. */
6454 return FALSE;
6455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 else if (!vim_isIDc(s[0]))
6457 {
6458 /* if it is not an identifier, we are wrong */
6459 class_or_struct = FALSE;
6460 lookfor_ctor_init = FALSE;
6461 }
6462 else if (*col == 0)
6463 {
6464 /* it can't be a constructor-initialization any more */
6465 lookfor_ctor_init = FALSE;
6466
6467 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006468 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 *col = (colnr_T)(s - line);
6470 }
6471
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006472 /* When the line ends in a comma don't align with it. */
6473 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6474 *col = 0;
6475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 s = cin_skipcomment(s + 1);
6477 }
6478 }
6479
6480 return cpp_base_class;
6481}
6482
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006483 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006484get_baseclass_amount(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006485 int col;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006486{
6487 int amount;
6488 colnr_T vcol;
6489 pos_T *trypos;
6490
6491 if (col == 0)
6492 {
6493 amount = get_indent();
6494 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006495 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006496 amount = get_indent_lnum(trypos->lnum); /* XXX */
6497 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006498 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006499 }
6500 else
6501 {
6502 curwin->w_cursor.col = col;
6503 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6504 amount = (int)vcol;
6505 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006506 if (amount < curbuf->b_ind_cpp_baseclass)
6507 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006508 return amount;
6509}
6510
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511/*
6512 * Return TRUE if string "s" ends with the string "find", possibly followed by
6513 * white space and comments. Skip strings and comments.
6514 * Ignore "ignore" after "find" if it's not NULL.
6515 */
6516 static int
6517cin_ends_in(s, find, ignore)
6518 char_u *s;
6519 char_u *find;
6520 char_u *ignore;
6521{
6522 char_u *p = s;
6523 char_u *r;
6524 int len = (int)STRLEN(find);
6525
6526 while (*p != NUL)
6527 {
6528 p = cin_skipcomment(p);
6529 if (STRNCMP(p, find, len) == 0)
6530 {
6531 r = skipwhite(p + len);
6532 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6533 r = skipwhite(r + STRLEN(ignore));
6534 if (cin_nocode(r))
6535 return TRUE;
6536 }
6537 if (*p != NUL)
6538 ++p;
6539 }
6540 return FALSE;
6541}
6542
6543/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006544 * Return TRUE when "s" starts with "word" and then a non-ID character.
6545 */
6546 static int
6547cin_starts_with(s, word)
6548 char_u *s;
6549 char *word;
6550{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006551 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006552
6553 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6554}
6555
6556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 * Skip strings, chars and comments until at or past "trypos".
6558 * Return the column found.
6559 */
6560 static int
6561cin_skip2pos(trypos)
6562 pos_T *trypos;
6563{
6564 char_u *line;
6565 char_u *p;
6566
6567 p = line = ml_get(trypos->lnum);
6568 while (*p && (colnr_T)(p - line) < trypos->col)
6569 {
6570 if (cin_iscomment(p))
6571 p = cin_skipcomment(p);
6572 else
6573 {
6574 p = skip_string(p);
6575 ++p;
6576 }
6577 }
6578 return (int)(p - line);
6579}
6580
6581/*
6582 * Find the '{' at the start of the block we are in.
6583 * Return NULL if no match found.
6584 * Ignore a '{' that is in a comment, makes indenting the next three lines
6585 * work. */
6586/* foo() */
6587/* { */
6588/* } */
6589
6590 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006591find_start_brace() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
6593 pos_T cursor_save;
6594 pos_T *trypos;
6595 pos_T *pos;
6596 static pos_T pos_copy;
6597
6598 cursor_save = curwin->w_cursor;
6599 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6600 {
6601 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6602 trypos = &pos_copy;
6603 curwin->w_cursor = *trypos;
6604 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006605 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006607 && (pos = ind_find_start_comment()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 break;
6609 if (pos != NULL)
6610 curwin->w_cursor.lnum = pos->lnum;
6611 }
6612 curwin->w_cursor = cursor_save;
6613 return trypos;
6614}
6615
6616/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006617 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006618 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 */
6620 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006621find_match_paren(ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 int ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 pos_T cursor_save;
6625 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006626 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
6628 cursor_save = curwin->w_cursor;
6629 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6630 {
6631 /* check if the ( is in a // comment */
6632 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6633 trypos = NULL;
6634 else
6635 {
6636 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6637 trypos = &pos_copy;
6638 curwin->w_cursor = *trypos;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006639 if (ind_find_start_comment() != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 trypos = NULL;
6641 }
6642 }
6643 curwin->w_cursor = cursor_save;
6644 return trypos;
6645}
6646
6647/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006648 * Find the matching '(', ignoring it if it is in a comment or before an
6649 * unmatched {.
6650 * Return NULL if no match found.
6651 */
6652 static pos_T *
6653find_match_paren_after_brace(ind_maxparen) /* XXX */
6654 int ind_maxparen;
6655{
6656 pos_T *trypos = find_match_paren(ind_maxparen);
6657
6658 if (trypos != NULL)
6659 {
6660 pos_T *tryposBrace = find_start_brace();
6661
6662 /* If both an unmatched '(' and '{' is found. Ignore the '('
6663 * position if the '{' is further down. */
6664 if (tryposBrace != NULL
6665 && (trypos->lnum != tryposBrace->lnum
6666 ? trypos->lnum < tryposBrace->lnum
6667 : trypos->col < tryposBrace->col))
6668 trypos = NULL;
6669 }
6670 return trypos;
6671}
6672
6673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 * Return ind_maxparen corrected for the difference in line number between the
6675 * cursor position and "startpos". This makes sure that searching for a
6676 * matching paren above the cursor line doesn't find a match because of
6677 * looking a few lines further.
6678 */
6679 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006680corr_ind_maxparen(startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 pos_T *startpos;
6682{
6683 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6684
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006685 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6686 return curbuf->b_ind_maxparen - (int)n;
6687 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688}
6689
6690/*
6691 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006692 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 */
6694 static int
6695find_last_paren(l, start, end)
6696 char_u *l;
6697 int start, end;
6698{
6699 int i;
6700 int retval = FALSE;
6701 int open_count = 0;
6702
6703 curwin->w_cursor.col = 0; /* default is start of line */
6704
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006705 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {
6707 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6708 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6709 if (l[i] == start)
6710 ++open_count;
6711 else if (l[i] == end)
6712 {
6713 if (open_count > 0)
6714 --open_count;
6715 else
6716 {
6717 curwin->w_cursor.col = i;
6718 retval = TRUE;
6719 }
6720 }
6721 }
6722 return retval;
6723}
6724
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006725/*
6726 * Parse 'cinoptions' and set the values in "curbuf".
6727 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6728 */
6729 void
6730parse_cino(buf)
6731 buf_T *buf;
6732{
6733 char_u *p;
6734 char_u *l;
6735 char_u *digits;
6736 int n;
6737 int divider;
6738 int fraction = 0;
6739 int sw = (int)get_sw_value(buf);
6740
6741 /*
6742 * Set the default values.
6743 */
6744 /* Spaces from a block's opening brace the prevailing indent for that
6745 * block should be. */
6746 buf->b_ind_level = sw;
6747
6748 /* Spaces from the edge of the line an open brace that's at the end of a
6749 * line is imagined to be. */
6750 buf->b_ind_open_imag = 0;
6751
6752 /* Spaces from the prevailing indent for a line that is not preceded by
6753 * an opening brace. */
6754 buf->b_ind_no_brace = 0;
6755
6756 /* Column where the first { of a function should be located }. */
6757 buf->b_ind_first_open = 0;
6758
6759 /* Spaces from the prevailing indent a leftmost open brace should be
6760 * located. */
6761 buf->b_ind_open_extra = 0;
6762
6763 /* Spaces from the matching open brace (real location for one at the left
6764 * edge; imaginary location from one that ends a line) the matching close
6765 * brace should be located. */
6766 buf->b_ind_close_extra = 0;
6767
6768 /* Spaces from the edge of the line an open brace sitting in the leftmost
6769 * column is imagined to be. */
6770 buf->b_ind_open_left_imag = 0;
6771
6772 /* Spaces jump labels should be shifted to the left if N is non-negative,
6773 * otherwise the jump label will be put to column 1. */
6774 buf->b_ind_jump_label = -1;
6775
6776 /* Spaces from the switch() indent a "case xx" label should be located. */
6777 buf->b_ind_case = sw;
6778
6779 /* Spaces from the "case xx:" code after a switch() should be located. */
6780 buf->b_ind_case_code = sw;
6781
6782 /* Lineup break at end of case in switch() with case label. */
6783 buf->b_ind_case_break = 0;
6784
6785 /* Spaces from the class declaration indent a scope declaration label
6786 * should be located. */
6787 buf->b_ind_scopedecl = sw;
6788
6789 /* Spaces from the scope declaration label code should be located. */
6790 buf->b_ind_scopedecl_code = sw;
6791
6792 /* Amount K&R-style parameters should be indented. */
6793 buf->b_ind_param = sw;
6794
6795 /* Amount a function type spec should be indented. */
6796 buf->b_ind_func_type = sw;
6797
6798 /* Amount a cpp base class declaration or constructor initialization
6799 * should be indented. */
6800 buf->b_ind_cpp_baseclass = sw;
6801
6802 /* additional spaces beyond the prevailing indent a continuation line
6803 * should be located. */
6804 buf->b_ind_continuation = sw;
6805
6806 /* Spaces from the indent of the line with an unclosed parentheses. */
6807 buf->b_ind_unclosed = sw * 2;
6808
6809 /* Spaces from the indent of the line with an unclosed parentheses, which
6810 * itself is also unclosed. */
6811 buf->b_ind_unclosed2 = sw;
6812
6813 /* Suppress ignoring spaces from the indent of a line starting with an
6814 * unclosed parentheses. */
6815 buf->b_ind_unclosed_noignore = 0;
6816
6817 /* If the opening paren is the last nonwhite character on the line, and
6818 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6819 * context (for very long lines). */
6820 buf->b_ind_unclosed_wrapped = 0;
6821
6822 /* Suppress ignoring white space when lining up with the character after
6823 * an unclosed parentheses. */
6824 buf->b_ind_unclosed_whiteok = 0;
6825
6826 /* Indent a closing parentheses under the line start of the matching
6827 * opening parentheses. */
6828 buf->b_ind_matching_paren = 0;
6829
6830 /* Indent a closing parentheses under the previous line. */
6831 buf->b_ind_paren_prev = 0;
6832
6833 /* Extra indent for comments. */
6834 buf->b_ind_comment = 0;
6835
6836 /* Spaces from the comment opener when there is nothing after it. */
6837 buf->b_ind_in_comment = 3;
6838
6839 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6840 * after the comment opener. */
6841 buf->b_ind_in_comment2 = 0;
6842
6843 /* Max lines to search for an open paren. */
6844 buf->b_ind_maxparen = 20;
6845
6846 /* Max lines to search for an open comment. */
6847 buf->b_ind_maxcomment = 70;
6848
6849 /* Handle braces for java code. */
6850 buf->b_ind_java = 0;
6851
6852 /* Not to confuse JS object properties with labels. */
6853 buf->b_ind_js = 0;
6854
6855 /* Handle blocked cases correctly. */
6856 buf->b_ind_keep_case_label = 0;
6857
6858 /* Handle C++ namespace. */
6859 buf->b_ind_cpp_namespace = 0;
6860
6861 /* Handle continuation lines containing conditions of if(), for() and
6862 * while(). */
6863 buf->b_ind_if_for_while = 0;
6864
6865 for (p = buf->b_p_cino; *p; )
6866 {
6867 l = p++;
6868 if (*p == '-')
6869 ++p;
6870 digits = p; /* remember where the digits start */
6871 n = getdigits(&p);
6872 divider = 0;
6873 if (*p == '.') /* ".5s" means a fraction */
6874 {
6875 fraction = atol((char *)++p);
6876 while (VIM_ISDIGIT(*p))
6877 {
6878 ++p;
6879 if (divider)
6880 divider *= 10;
6881 else
6882 divider = 10;
6883 }
6884 }
6885 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6886 {
6887 if (p == digits)
6888 n = sw; /* just "s" is one 'shiftwidth' */
6889 else
6890 {
6891 n *= sw;
6892 if (divider)
6893 n += (sw * fraction + divider / 2) / divider;
6894 }
6895 ++p;
6896 }
6897 if (l[1] == '-')
6898 n = -n;
6899
6900 /* When adding an entry here, also update the default 'cinoptions' in
6901 * doc/indent.txt, and add explanation for it! */
6902 switch (*l)
6903 {
6904 case '>': buf->b_ind_level = n; break;
6905 case 'e': buf->b_ind_open_imag = n; break;
6906 case 'n': buf->b_ind_no_brace = n; break;
6907 case 'f': buf->b_ind_first_open = n; break;
6908 case '{': buf->b_ind_open_extra = n; break;
6909 case '}': buf->b_ind_close_extra = n; break;
6910 case '^': buf->b_ind_open_left_imag = n; break;
6911 case 'L': buf->b_ind_jump_label = n; break;
6912 case ':': buf->b_ind_case = n; break;
6913 case '=': buf->b_ind_case_code = n; break;
6914 case 'b': buf->b_ind_case_break = n; break;
6915 case 'p': buf->b_ind_param = n; break;
6916 case 't': buf->b_ind_func_type = n; break;
6917 case '/': buf->b_ind_comment = n; break;
6918 case 'c': buf->b_ind_in_comment = n; break;
6919 case 'C': buf->b_ind_in_comment2 = n; break;
6920 case 'i': buf->b_ind_cpp_baseclass = n; break;
6921 case '+': buf->b_ind_continuation = n; break;
6922 case '(': buf->b_ind_unclosed = n; break;
6923 case 'u': buf->b_ind_unclosed2 = n; break;
6924 case 'U': buf->b_ind_unclosed_noignore = n; break;
6925 case 'W': buf->b_ind_unclosed_wrapped = n; break;
6926 case 'w': buf->b_ind_unclosed_whiteok = n; break;
6927 case 'm': buf->b_ind_matching_paren = n; break;
6928 case 'M': buf->b_ind_paren_prev = n; break;
6929 case ')': buf->b_ind_maxparen = n; break;
6930 case '*': buf->b_ind_maxcomment = n; break;
6931 case 'g': buf->b_ind_scopedecl = n; break;
6932 case 'h': buf->b_ind_scopedecl_code = n; break;
6933 case 'j': buf->b_ind_java = n; break;
6934 case 'J': buf->b_ind_js = n; break;
6935 case 'l': buf->b_ind_keep_case_label = n; break;
6936 case '#': buf->b_ind_hash_comment = n; break;
6937 case 'N': buf->b_ind_cpp_namespace = n; break;
6938 case 'k': buf->b_ind_if_for_while = n; break;
6939 }
6940 if (*p == ',')
6941 ++p;
6942 }
6943}
6944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 int
6946get_c_indent()
6947{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 pos_T cur_curpos;
6949 int amount;
6950 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006951 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 colnr_T col;
6953 char_u *theline;
6954 char_u *linecopy;
6955 pos_T *trypos;
6956 pos_T *tryposBrace = NULL;
6957 pos_T our_paren_pos;
6958 char_u *start;
6959 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006960#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961#define BRACE_AT_START 2 /* '{' is at start of line */
6962#define BRACE_AT_END 3 /* '{' is at end of line */
6963 linenr_T ourscope;
6964 char_u *l;
6965 char_u *look;
6966 char_u terminated;
6967 int lookfor;
6968#define LOOKFOR_INITIAL 0
6969#define LOOKFOR_IF 1
6970#define LOOKFOR_DO 2
6971#define LOOKFOR_CASE 3
6972#define LOOKFOR_ANY 4
6973#define LOOKFOR_TERM 5
6974#define LOOKFOR_UNTERM 6
6975#define LOOKFOR_SCOPEDECL 7
6976#define LOOKFOR_NOBREAK 8
6977#define LOOKFOR_CPP_BASECLASS 9
6978#define LOOKFOR_ENUM_OR_INIT 10
6979
6980 int whilelevel;
6981 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 int n;
6983 int iscase;
6984 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006985 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006987 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006988 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006990 /* make a copy, value is changed below */
6991 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
6993 /* remember where the cursor was when we started */
6994 cur_curpos = curwin->w_cursor;
6995
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006996 /* if we are at line 1 0 is fine, right? */
6997 if (cur_curpos.lnum == 1)
6998 return 0;
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 /* Get a copy of the current contents of the line.
7001 * This is required, because only the most recent line obtained with
7002 * ml_get is valid! */
7003 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7004 if (linecopy == NULL)
7005 return 0;
7006
7007 /*
7008 * In insert mode and the cursor is on a ')' truncate the line at the
7009 * cursor position. We don't want to line up with the matching '(' when
7010 * inserting new stuff.
7011 * For unknown reasons the cursor might be past the end of the line, thus
7012 * check for that.
7013 */
7014 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007015 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 && linecopy[curwin->w_cursor.col] == ')')
7017 linecopy[curwin->w_cursor.col] = NUL;
7018
7019 theline = skipwhite(linecopy);
7020
7021 /* move the cursor to the start of the line */
7022
7023 curwin->w_cursor.col = 0;
7024
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007025 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 /*
7028 * #defines and so on always go at the left when included in 'cinkeys'.
7029 */
7030 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007031 amount = curbuf->b_ind_hash_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
7033 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007034 * Is it a non-case label? Then that goes at the left margin too unless:
7035 * - JS flag is set.
7036 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007038 else if (original_line_islabel && !curbuf->b_ind_js
7039 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {
7041 amount = 0;
7042 }
7043
7044 /*
7045 * If we're inside a "//" comment and there is a "//" comment in a
7046 * previous line, lineup with that one.
7047 */
7048 else if (cin_islinecomment(theline)
7049 && (trypos = find_line_comment()) != NULL) /* XXX */
7050 {
7051 /* find how indented the line beginning the comment is */
7052 getvcol(curwin, trypos, &col, NULL, NULL);
7053 amount = col;
7054 }
7055
7056 /*
7057 * If we're inside a comment and not looking at the start of the
7058 * comment, try using the 'comments' option.
7059 */
7060 else if (!cin_iscomment(theline)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007061 && (trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007062 /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
7064 int lead_start_len = 2;
7065 int lead_middle_len = 1;
7066 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7067 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7068 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7069 char_u *p;
7070 int start_align = 0;
7071 int start_off = 0;
7072 int done = FALSE;
7073
7074 /* find how indented the line beginning the comment is */
7075 getvcol(curwin, trypos, &col, NULL, NULL);
7076 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007077 *lead_start = NUL;
7078 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
7080 p = curbuf->b_p_com;
7081 while (*p != NUL)
7082 {
7083 int align = 0;
7084 int off = 0;
7085 int what = 0;
7086
7087 while (*p != NUL && *p != ':')
7088 {
7089 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7090 what = *p++;
7091 else if (*p == COM_LEFT || *p == COM_RIGHT)
7092 align = *p++;
7093 else if (VIM_ISDIGIT(*p) || *p == '-')
7094 off = getdigits(&p);
7095 else
7096 ++p;
7097 }
7098
7099 if (*p == ':')
7100 ++p;
7101 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7102 if (what == COM_START)
7103 {
7104 STRCPY(lead_start, lead_end);
7105 lead_start_len = (int)STRLEN(lead_start);
7106 start_off = off;
7107 start_align = align;
7108 }
7109 else if (what == COM_MIDDLE)
7110 {
7111 STRCPY(lead_middle, lead_end);
7112 lead_middle_len = (int)STRLEN(lead_middle);
7113 }
7114 else if (what == COM_END)
7115 {
7116 /* If our line starts with the middle comment string, line it
7117 * up with the comment opener per the 'comments' option. */
7118 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7119 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7120 {
7121 done = TRUE;
7122 if (curwin->w_cursor.lnum > 1)
7123 {
7124 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007125 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 * the middle comment string matches in the previous
7127 * line, use the indent of that line. XXX */
7128 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7129 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7130 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7131 else if (STRNCMP(look, lead_middle,
7132 lead_middle_len) == 0)
7133 {
7134 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7135 break;
7136 }
7137 /* If the start comment string doesn't match with the
7138 * start of the comment, skip this entry. XXX */
7139 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7140 lead_start, lead_start_len) != 0)
7141 continue;
7142 }
7143 if (start_off != 0)
7144 amount += start_off;
7145 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007146 amount += vim_strsize(lead_start)
7147 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 break;
7149 }
7150
7151 /* If our line starts with the end comment string, line it up
7152 * with the middle comment */
7153 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7154 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7155 {
7156 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7157 /* XXX */
7158 if (off != 0)
7159 amount += off;
7160 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007161 amount += vim_strsize(lead_start)
7162 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 done = TRUE;
7164 break;
7165 }
7166 }
7167 }
7168
7169 /* If our line starts with an asterisk, line up with the
7170 * asterisk in the comment opener; otherwise, line up
7171 * with the first character of the comment text.
7172 */
7173 if (done)
7174 ;
7175 else if (theline[0] == '*')
7176 amount += 1;
7177 else
7178 {
7179 /*
7180 * If we are more than one line away from the comment opener, take
7181 * the indent of the previous non-empty line. If 'cino' has "CO"
7182 * and we are just below the comment opener and there are any
7183 * white characters after it line up with the text after it;
7184 * otherwise, add the amount specified by "c" in 'cino'
7185 */
7186 amount = -1;
7187 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7188 {
7189 if (linewhite(lnum)) /* skip blank lines */
7190 continue;
7191 amount = get_indent_lnum(lnum); /* XXX */
7192 break;
7193 }
7194 if (amount == -1) /* use the comment opener */
7195 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007196 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {
7198 start = ml_get(trypos->lnum);
7199 look = start + trypos->col + 2; /* skip / and * */
7200 if (*look != NUL) /* if something after it */
7201 trypos->col = (colnr_T)(skipwhite(look) - start);
7202 }
7203 getvcol(curwin, trypos, &col, NULL, NULL);
7204 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007205 if (curbuf->b_ind_in_comment2 || *look == NUL)
7206 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208 }
7209 }
7210
7211 /*
7212 * Are we inside parentheses or braces?
7213 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007214 else if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007215 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007216 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 || trypos != NULL)
7218 {
7219 if (trypos != NULL && tryposBrace != NULL)
7220 {
7221 /* Both an unmatched '(' and '{' is found. Use the one which is
7222 * closer to the current cursor position, set the other to NULL. */
7223 if (trypos->lnum != tryposBrace->lnum
7224 ? trypos->lnum < tryposBrace->lnum
7225 : trypos->col < tryposBrace->col)
7226 trypos = NULL;
7227 else
7228 tryposBrace = NULL;
7229 }
7230
7231 if (trypos != NULL)
7232 {
7233 /*
7234 * If the matching paren is more than one line away, use the indent of
7235 * a previous non-empty line that matches the same paren.
7236 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007237 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007239 /* Line up with the start of the matching paren line. */
7240 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7241 }
7242 else
7243 {
7244 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007245 our_paren_pos = *trypos;
7246 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007248 l = skipwhite(ml_get(lnum));
7249 if (cin_nocode(l)) /* skip comment lines */
7250 continue;
7251 if (cin_ispreproc_cont(&l, &lnum))
7252 continue; /* ignore #define, #if, etc. */
7253 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007255 /* Skip a comment. XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007256 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007257 {
7258 lnum = trypos->lnum + 1;
7259 continue;
7260 }
7261
7262 /* XXX */
7263 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007264 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007265 && trypos->lnum == our_paren_pos.lnum
7266 && trypos->col == our_paren_pos.col)
7267 {
7268 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007270 if (theline[0] == ')')
7271 {
7272 if (our_paren_pos.lnum != lnum
7273 && cur_amount > amount)
7274 cur_amount = amount;
7275 amount = -1;
7276 }
7277 break;
7278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
7280 }
7281
7282 /*
7283 * Line up with line where the matching paren is. XXX
7284 * If the line starts with a '(' or the indent for unclosed
7285 * parentheses is zero, line up with the unclosed parentheses.
7286 */
7287 if (amount == -1)
7288 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007289 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007290 int is_if_for_while = 0;
7291
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007292 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007293 {
7294 /* Look for the outermost opening parenthesis on this line
7295 * and check whether it belongs to an "if", "for" or "while". */
7296
7297 pos_T cursor_save = curwin->w_cursor;
7298 pos_T outermost;
7299 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007300
7301 trypos = &our_paren_pos;
7302 do {
7303 outermost = *trypos;
7304 curwin->w_cursor.lnum = outermost.lnum;
7305 curwin->w_cursor.col = outermost.col;
7306
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007307 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007308 } while (trypos && trypos->lnum == outermost.lnum);
7309
7310 curwin->w_cursor = cursor_save;
7311
7312 line = ml_get(outermost.lnum);
7313
7314 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007315 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007316 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007317
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007318 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007319 look = skipwhite(look);
7320 if (*look == '(')
7321 {
7322 linenr_T save_lnum = curwin->w_cursor.lnum;
7323 char_u *line;
7324 int look_col;
7325
7326 /* Ignore a '(' in front of the line that has a match before
7327 * our matching '('. */
7328 curwin->w_cursor.lnum = our_paren_pos.lnum;
7329 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007330 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007331 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007332 if ((trypos = findmatchlimit(NULL, ')', 0,
7333 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007334 != NULL
7335 && trypos->lnum == our_paren_pos.lnum
7336 && trypos->col < our_paren_pos.col)
7337 ignore_paren_col = trypos->col + 1;
7338
7339 curwin->w_cursor.lnum = save_lnum;
7340 look = ml_get(our_paren_pos.lnum) + look_col;
7341 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007342 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7343 && is_if_for_while == 0)
7344 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007345 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
7347 /*
7348 * If we're looking at a close paren, line up right there;
7349 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007350 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 * the last nonwhite character of the line, use either the
7352 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007353 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 * lines).
7355 */
7356 if (theline[0] != ')')
7357 {
7358 cur_amount = MAXCOL;
7359 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007360 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 && cin_ends_in(l, (char_u *)"(", NULL))
7362 {
7363 /* look for opening unmatched paren, indent one level
7364 * for each additional level */
7365 n = 1;
7366 for (col = 0; col < our_paren_pos.col; ++col)
7367 {
7368 switch (l[col])
7369 {
7370 case '(':
7371 case '{': ++n;
7372 break;
7373
7374 case ')':
7375 case '}': if (n > 1)
7376 --n;
7377 break;
7378 }
7379 }
7380
7381 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007382 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007384 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 our_paren_pos.col++;
7386 else
7387 {
7388 col = our_paren_pos.col + 1;
7389 while (vim_iswhite(l[col]))
7390 col++;
7391 if (l[col] != NUL) /* In case of trailing space */
7392 our_paren_pos.col = col;
7393 else
7394 our_paren_pos.col++;
7395 }
7396 }
7397
7398 /*
7399 * Find how indented the paren is, or the character after it
7400 * if we did the above "if".
7401 */
7402 if (our_paren_pos.col > 0)
7403 {
7404 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7405 if (cur_amount > (int)col)
7406 cur_amount = col;
7407 }
7408 }
7409
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007410 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 {
7412 /* Line up with the start of the matching paren line. */
7413 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007414 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7415 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007416 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
7418 if (cur_amount != MAXCOL)
7419 amount = cur_amount;
7420 }
7421 else
7422 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007423 /* Add b_ind_unclosed2 for each '(' before our matching one,
7424 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007426 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 {
7428 --our_paren_pos.col;
7429 switch (*ml_get_pos(&our_paren_pos))
7430 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007431 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 col = our_paren_pos.col;
7433 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007434 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 col = MAXCOL;
7436 break;
7437 }
7438 }
7439
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007440 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 * braces */
7442 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007443 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 else
7445 {
7446 curwin->w_cursor.lnum = our_paren_pos.lnum;
7447 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007448 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7449 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007450 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007452 {
7453 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007454 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007455 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007456 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459 /*
7460 * For a line starting with ')' use the minimum of the two
7461 * positions, to avoid giving it more indent than the previous
7462 * lines:
7463 * func_long_name( if (x
7464 * arg && yy
7465 * ) ^ not here ) ^ not here
7466 */
7467 if (cur_amount < amount)
7468 amount = cur_amount;
7469 }
7470 }
7471
7472 /* add extra indent for a comment */
7473 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007474 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476
7477 /*
7478 * Are we at least inside braces, then?
7479 */
7480 else
7481 {
7482 trypos = tryposBrace;
7483
7484 ourscope = trypos->lnum;
7485 start = ml_get(ourscope);
7486
7487 /*
7488 * Now figure out how indented the line is in general.
7489 * If the brace was at the start of the line, we use that;
7490 * otherwise, check out the indentation of the line as
7491 * a whole and then add the "imaginary indent" to that.
7492 */
7493 look = skipwhite(start);
7494 if (*look == '{')
7495 {
7496 getvcol(curwin, trypos, &col, NULL, NULL);
7497 amount = col;
7498 if (*start == '{')
7499 start_brace = BRACE_IN_COL0;
7500 else
7501 start_brace = BRACE_AT_START;
7502 }
7503 else
7504 {
7505 /*
7506 * that opening brace might have been on a continuation
7507 * line. if so, find the start of the line.
7508 */
7509 curwin->w_cursor.lnum = ourscope;
7510
7511 /*
7512 * position the cursor over the rightmost paren, so that
7513 * matching it will take us back to the start of the line.
7514 */
7515 lnum = ourscope;
7516 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007517 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7518 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 lnum = trypos->lnum;
7520
7521 /*
7522 * It could have been something like
7523 * case 1: if (asdf &&
7524 * ldfd) {
7525 * }
7526 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007527 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7528 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007530 else if (curbuf->b_ind_js)
7531 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007533 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534
7535 start_brace = BRACE_AT_END;
7536 }
7537
7538 /*
7539 * if we're looking at a closing brace, that's where
7540 * we want to be. otherwise, add the amount of room
7541 * that an indent is supposed to be.
7542 */
7543 if (theline[0] == '}')
7544 {
7545 /*
7546 * they may want closing braces to line up with something
7547 * other than the open brace. indulge them, if so.
7548 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007549 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
7551 else
7552 {
7553 /*
7554 * If we're looking at an "else", try to find an "if"
7555 * to match it with.
7556 * If we're looking at a "while", try to find a "do"
7557 * to match it with.
7558 */
7559 lookfor = LOOKFOR_INITIAL;
7560 if (cin_iselse(theline))
7561 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007562 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 lookfor = LOOKFOR_DO;
7564 if (lookfor != LOOKFOR_INITIAL)
7565 {
7566 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007567 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
7569 amount = get_indent(); /* XXX */
7570 goto theend;
7571 }
7572 }
7573
7574 /*
7575 * We get here if we are not on an "while-of-do" or "else" (or
7576 * failed to find a matching "if").
7577 * Search backwards for something to line up with.
7578 * First set amount for when we don't find anything.
7579 */
7580
7581 /*
7582 * if the '{' is _really_ at the left margin, use the imaginary
7583 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007584 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 */
7586
7587 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7588 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007589 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007590 lookfor_cpp_namespace = TRUE;
7591 }
7592 else if (start_brace == BRACE_AT_START &&
7593 lookfor_cpp_namespace) /* '{' is at start */
7594 {
7595
7596 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 }
7598 else
7599 {
7600 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007601 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007602 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007603
7604 l = skipwhite(ml_get_curline());
7605 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007606 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 else
7609 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007610 /* Compensate for adding b_ind_open_extra later. */
7611 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (amount < 0)
7613 amount = 0;
7614 }
7615 }
7616
7617 lookfor_break = FALSE;
7618
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007619 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {
7621 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007622 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
7624 else if (cin_isscopedecl(theline)) /* private:, ... */
7625 {
7626 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007627 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
7629 else
7630 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007631 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7632 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 lookfor_break = TRUE;
7634
7635 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007636 /* b_ind_level from start of block */
7637 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 }
7639 scope_amount = amount;
7640 whilelevel = 0;
7641
7642 /*
7643 * Search backwards. If we find something we recognize, line up
7644 * with that.
7645 *
7646 * if we're looking at an open brace, indent
7647 * the usual amount relative to the conditional
7648 * that opens the block.
7649 */
7650 curwin->w_cursor = cur_curpos;
7651 for (;;)
7652 {
7653 curwin->w_cursor.lnum--;
7654 curwin->w_cursor.col = 0;
7655
7656 /*
7657 * If we went all the way back to the start of our scope, line
7658 * up with it.
7659 */
7660 if (curwin->w_cursor.lnum <= ourscope)
7661 {
7662 /* we reached end of scope:
7663 * if looking for a enum or structure initialization
7664 * go further back:
7665 * if it is an initializer (enum xxx or xxx =), then
7666 * don't add ind_continuation, otherwise it is a variable
7667 * declaration:
7668 * int x,
7669 * here; <-- add ind_continuation
7670 */
7671 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7672 {
7673 if (curwin->w_cursor.lnum == 0
7674 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007675 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007677 /* nothing found (abuse curbuf->b_ind_maxparen as
7678 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 * initialization) */
7680 if (cont_amount > 0)
7681 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007682 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 amount += ind_continuation;
7684 break;
7685 }
7686
7687 l = ml_get_curline();
7688
7689 /*
7690 * If we're in a comment now, skip to the start of the
7691 * comment.
7692 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007693 trypos = ind_find_start_comment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 if (trypos != NULL)
7695 {
7696 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007697 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 continue;
7699 }
7700
7701 /*
7702 * Skip preprocessor directives and blank lines.
7703 */
7704 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7705 continue;
7706
7707 if (cin_nocode(l))
7708 continue;
7709
7710 terminated = cin_isterminated(l, FALSE, TRUE);
7711
7712 /*
7713 * If we are at top level and the line looks like a
7714 * function declaration, we are done
7715 * (it's a variable declaration).
7716 */
7717 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007718 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {
7720 /* if the line is terminated with another ','
7721 * it is a continued variable initialization.
7722 * don't add extra indent.
7723 * TODO: does not work, if a function
7724 * declaration is split over multiple lines:
7725 * cin_isfuncdecl returns FALSE then.
7726 */
7727 if (terminated == ',')
7728 break;
7729
7730 /* if it es a enum declaration or an assignment,
7731 * we are done.
7732 */
7733 if (terminated != ';' && cin_isinit())
7734 break;
7735
7736 /* nothing useful found */
7737 if (terminated == 0 || terminated == '{')
7738 continue;
7739 }
7740
7741 if (terminated != ';')
7742 {
7743 /* Skip parens and braces. Position the cursor
7744 * over the rightmost paren, so that matching it
7745 * will take us back to the start of the line.
7746 */ /* XXX */
7747 trypos = NULL;
7748 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007749 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007750 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
7752 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007753 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754
7755 if (trypos != NULL)
7756 {
7757 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007758 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 continue;
7760 }
7761 }
7762
7763 /* it's a variable declaration, add indentation
7764 * like in
7765 * int a,
7766 * b;
7767 */
7768 if (cont_amount > 0)
7769 amount = cont_amount;
7770 else
7771 amount += ind_continuation;
7772 }
7773 else if (lookfor == LOOKFOR_UNTERM)
7774 {
7775 if (cont_amount > 0)
7776 amount = cont_amount;
7777 else
7778 amount += ind_continuation;
7779 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007780 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007781 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007782 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007784 {
7785 amount = scope_amount;
7786 if (theline[0] == '{')
7787 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007788 amount += curbuf->b_ind_open_extra;
7789 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007790 }
7791 }
7792
7793 if (lookfor_cpp_namespace)
7794 {
7795 /*
7796 * Looking for C++ namespace, need to look further
7797 * back.
7798 */
7799 if (curwin->w_cursor.lnum == ourscope)
7800 continue;
7801
7802 if (curwin->w_cursor.lnum == 0
7803 || curwin->w_cursor.lnum
7804 < ourscope - FIND_NAMESPACE_LIM)
7805 break;
7806
7807 l = ml_get_curline();
7808
7809 /* If we're in a comment now, skip to the start of
7810 * the comment. */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007811 trypos = ind_find_start_comment();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007812 if (trypos != NULL)
7813 {
7814 curwin->w_cursor.lnum = trypos->lnum + 1;
7815 curwin->w_cursor.col = 0;
7816 continue;
7817 }
7818
7819 /* Skip preprocessor directives and blank lines. */
7820 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7821 continue;
7822
7823 /* Finally the actual check for "namespace". */
7824 if (cin_is_cpp_namespace(l))
7825 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007826 amount += curbuf->b_ind_cpp_namespace
7827 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007828 break;
7829 }
7830
7831 if (cin_nocode(l))
7832 continue;
7833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 }
7835 break;
7836 }
7837
7838 /*
7839 * If we're in a comment now, skip to the start of the comment.
7840 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007841 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {
7843 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007844 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 continue;
7846 }
7847
7848 l = ml_get_curline();
7849
7850 /*
7851 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007852 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007854 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 if (iscase || cin_isscopedecl(l))
7856 {
7857 /* we are only looking for cpp base class
7858 * declaration/initialization any longer */
7859 if (lookfor == LOOKFOR_CPP_BASECLASS)
7860 break;
7861
7862 /* When looking for a "do" we are not interested in
7863 * labels. */
7864 if (whilelevel > 0)
7865 continue;
7866
7867 /*
7868 * case xx:
7869 * c = 99 + <- this indent plus continuation
7870 *-> here;
7871 */
7872 if (lookfor == LOOKFOR_UNTERM
7873 || lookfor == LOOKFOR_ENUM_OR_INIT)
7874 {
7875 if (cont_amount > 0)
7876 amount = cont_amount;
7877 else
7878 amount += ind_continuation;
7879 break;
7880 }
7881
7882 /*
7883 * case xx: <- line up with this case
7884 * x = 333;
7885 * case yy:
7886 */
7887 if ( (iscase && lookfor == LOOKFOR_CASE)
7888 || (iscase && lookfor_break)
7889 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7890 {
7891 /*
7892 * Check that this case label is not for another
7893 * switch()
7894 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007895 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007896 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {
7898 amount = get_indent(); /* XXX */
7899 break;
7900 }
7901 continue;
7902 }
7903
7904 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7905
7906 /*
7907 * case xx: if (cond) <- line up with this if
7908 * y = y + 1;
7909 * -> s = 99;
7910 *
7911 * case xx:
7912 * if (cond) <- line up with this line
7913 * y = y + 1;
7914 * -> s = 99;
7915 */
7916 if (lookfor == LOOKFOR_TERM)
7917 {
7918 if (n)
7919 amount = n;
7920
7921 if (!lookfor_break)
7922 break;
7923 }
7924
7925 /*
7926 * case xx: x = x + 1; <- line up with this x
7927 * -> y = y + 1;
7928 *
7929 * case xx: if (cond) <- line up with this if
7930 * -> y = y + 1;
7931 */
7932 if (n)
7933 {
7934 amount = n;
7935 l = after_label(ml_get_curline());
7936 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007937 {
7938 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007939 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007940 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007941 amount += curbuf->b_ind_level
7942 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 break;
7945 }
7946
7947 /*
7948 * Try to get the indent of a statement before the switch
7949 * label. If nothing is found, line up relative to the
7950 * switch label.
7951 * break; <- may line up with this line
7952 * case xx:
7953 * -> y = 1;
7954 */
7955 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007956 ? curbuf->b_ind_case_code
7957 : curbuf->b_ind_scopedecl_code);
7958 lookfor = curbuf->b_ind_case_break
7959 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 continue;
7961 }
7962
7963 /*
7964 * Looking for a switch() label or C++ scope declaration,
7965 * ignore other lines, skip {}-blocks.
7966 */
7967 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7968 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007969 if (find_last_paren(l, '{', '}')
7970 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007971 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007973 curwin->w_cursor.col = 0;
7974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 continue;
7976 }
7977
7978 /*
7979 * Ignore jump labels with nothing after them.
7980 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007981 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {
7983 l = after_label(ml_get_curline());
7984 if (l == NULL || cin_nocode(l))
7985 continue;
7986 }
7987
7988 /*
7989 * Ignore #defines, #if, etc.
7990 * Ignore comment and empty lines.
7991 * (need to get the line again, cin_islabel() may have
7992 * unlocked it)
7993 */
7994 l = ml_get_curline();
7995 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7996 || cin_nocode(l))
7997 continue;
7998
7999 /*
8000 * Are we at the start of a cpp base class declaration or
8001 * constructor initialization?
8002 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008003 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008004 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008005 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008006 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008007 l = ml_get_curline();
8008 }
8009 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {
8011 if (lookfor == LOOKFOR_UNTERM)
8012 {
8013 if (cont_amount > 0)
8014 amount = cont_amount;
8015 else
8016 amount += ind_continuation;
8017 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008018 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008020 /* Need to find start of the declaration. */
8021 lookfor = LOOKFOR_UNTERM;
8022 ind_continuation = 0;
8023 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008026 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008027 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 break;
8029 }
8030 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8031 {
8032 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008033 * declaration or initialization before the opening brace.
8034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 if (cin_isterminated(l, TRUE, FALSE))
8036 break;
8037 else
8038 continue;
8039 }
8040
8041 /*
8042 * What happens next depends on the line being terminated.
8043 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008044 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 * 123,
8046 * sizeof
8047 * here
8048 * Otherwise check whether it is a enumeration or structure
8049 * initialisation (not indented) or a variable declaration
8050 * (indented).
8051 */
8052 terminated = cin_isterminated(l, FALSE, TRUE);
8053
8054 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8055 && terminated == ','))
8056 {
8057 /*
8058 * if we're in the middle of a paren thing,
8059 * go back to the line that starts it so
8060 * we can get the right prevailing indent
8061 * if ( foo &&
8062 * bar )
8063 */
8064 /*
8065 * position the cursor over the rightmost paren, so that
8066 * matching it will take us back to the start of the line.
8067 */
8068 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008069 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070
8071 /*
8072 * If we are looking for ',', we also look for matching
8073 * braces.
8074 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008075 if (trypos == NULL && terminated == ','
8076 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008077 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078
8079 if (trypos != NULL)
8080 {
8081 /*
8082 * Check if we are on a case label now. This is
8083 * handled above.
8084 * case xx: if ( asdf &&
8085 * asdf)
8086 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008087 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008089 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
8091 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008092 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 continue;
8094 }
8095 }
8096
8097 /*
8098 * Skip over continuation lines to find the one to get the
8099 * indent from
8100 * char *usethis = "bla\
8101 * bla",
8102 * here;
8103 */
8104 if (terminated == ',')
8105 {
8106 while (curwin->w_cursor.lnum > 1)
8107 {
8108 l = ml_get(curwin->w_cursor.lnum - 1);
8109 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8110 break;
8111 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008112 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 }
8114 }
8115
8116 /*
8117 * Get indent and pointer to text for current line,
8118 * ignoring any jump label. XXX
8119 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008120 if (!curbuf->b_ind_js)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008121 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008122 else
8123 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 /*
8125 * If this is just above the line we are indenting, and it
8126 * starts with a '{', line it up with this line.
8127 * while (not)
8128 * -> {
8129 * }
8130 */
8131 if (terminated != ',' && lookfor != LOOKFOR_TERM
8132 && theline[0] == '{')
8133 {
8134 amount = cur_amount;
8135 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008136 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 * doesn't start with a '{', which must have a match
8138 * in the same line (scope is the same). Probably:
8139 * { 1, 2 },
8140 * -> { 3, 4 }
8141 */
8142 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008143 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008145 if (curbuf->b_ind_cpp_baseclass)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {
8147 /* have to look back, whether it is a cpp base
8148 * class declaration or initialization */
8149 lookfor = LOOKFOR_CPP_BASECLASS;
8150 continue;
8151 }
8152 break;
8153 }
8154
8155 /*
8156 * Check if we are after an "if", "while", etc.
8157 * Also allow " } else".
8158 */
8159 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8160 {
8161 /*
8162 * Found an unterminated line after an if (), line up
8163 * with the last one.
8164 * if (cond)
8165 * 100 +
8166 * -> here;
8167 */
8168 if (lookfor == LOOKFOR_UNTERM
8169 || lookfor == LOOKFOR_ENUM_OR_INIT)
8170 {
8171 if (cont_amount > 0)
8172 amount = cont_amount;
8173 else
8174 amount += ind_continuation;
8175 break;
8176 }
8177
8178 /*
8179 * If this is just above the line we are indenting, we
8180 * are finished.
8181 * while (not)
8182 * -> here;
8183 * Otherwise this indent can be used when the line
8184 * before this is terminated.
8185 * yyy;
8186 * if (stat)
8187 * while (not)
8188 * xxx;
8189 * -> here;
8190 */
8191 amount = cur_amount;
8192 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008193 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 if (lookfor != LOOKFOR_TERM)
8195 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008196 amount += curbuf->b_ind_level
8197 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 break;
8199 }
8200
8201 /*
8202 * Special trick: when expecting the while () after a
8203 * do, line up with the while()
8204 * do
8205 * x = 1;
8206 * -> here
8207 */
8208 l = skipwhite(ml_get_curline());
8209 if (cin_isdo(l))
8210 {
8211 if (whilelevel == 0)
8212 break;
8213 --whilelevel;
8214 }
8215
8216 /*
8217 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008218 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 * Need to use the scope of this "else". XXX
8220 * If whilelevel != 0 continue looking for a "do {".
8221 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008222 if (cin_iselse(l) && whilelevel == 0)
8223 {
8224 /* If we're looking at "} else", let's make sure we
8225 * find the opening brace of the enclosing scope,
8226 * not the one from "if () {". */
8227 if (*l == '}')
8228 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008229 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008230
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008231 if ((trypos = find_start_brace()) == NULL
8232 || find_match(LOOKFOR_IF, trypos->lnum)
8233 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008234 break;
8235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237
8238 /*
8239 * If we're below an unterminated line that is not an
8240 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008241 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 * the line before this one.
8243 */
8244 else
8245 {
8246 /*
8247 * Found two unterminated lines on a row, line up with
8248 * the last one.
8249 * c = 99 +
8250 * 100 +
8251 * -> here;
8252 */
8253 if (lookfor == LOOKFOR_UNTERM)
8254 {
8255 /* When line ends in a comma add extra indent */
8256 if (terminated == ',')
8257 amount += ind_continuation;
8258 break;
8259 }
8260
8261 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8262 {
8263 /* Found two lines ending in ',', lineup with the
8264 * lowest one, but check for cpp base class
8265 * declaration/initialization, if it is an
8266 * opening brace or we are looking just for
8267 * enumerations/initializations. */
8268 if (terminated == ',')
8269 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008270 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 break;
8272
8273 lookfor = LOOKFOR_CPP_BASECLASS;
8274 continue;
8275 }
8276
8277 /* Ignore unterminated lines in between, but
8278 * reduce indent. */
8279 if (amount > cur_amount)
8280 amount = cur_amount;
8281 }
8282 else
8283 {
8284 /*
8285 * Found first unterminated line on a row, may
8286 * line up with this line, remember its indent
8287 * 100 +
8288 * -> here;
8289 */
8290 amount = cur_amount;
8291
8292 /*
8293 * If previous line ends in ',', check whether we
8294 * are in an initialization or enum
8295 * struct xxx =
8296 * {
8297 * sizeof a,
8298 * 124 };
8299 * or a normal possible continuation line.
8300 * but only, of no other statement has been found
8301 * yet.
8302 */
8303 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8304 {
8305 lookfor = LOOKFOR_ENUM_OR_INIT;
8306 cont_amount = cin_first_id_amount();
8307 }
8308 else
8309 {
8310 if (lookfor == LOOKFOR_INITIAL
8311 && *l != NUL
8312 && l[STRLEN(l) - 1] == '\\')
8313 /* XXX */
8314 cont_amount = cin_get_equal_amount(
8315 curwin->w_cursor.lnum);
8316 if (lookfor != LOOKFOR_TERM)
8317 lookfor = LOOKFOR_UNTERM;
8318 }
8319 }
8320 }
8321 }
8322
8323 /*
8324 * Check if we are after a while (cond);
8325 * If so: Ignore until the matching "do".
8326 */
8327 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008328 else if (cin_iswhileofdo_end(terminated))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {
8330 /*
8331 * Found an unterminated line after a while ();, line up
8332 * with the last one.
8333 * while (cond);
8334 * 100 + <- line up with this one
8335 * -> here;
8336 */
8337 if (lookfor == LOOKFOR_UNTERM
8338 || lookfor == LOOKFOR_ENUM_OR_INIT)
8339 {
8340 if (cont_amount > 0)
8341 amount = cont_amount;
8342 else
8343 amount += ind_continuation;
8344 break;
8345 }
8346
8347 if (whilelevel == 0)
8348 {
8349 lookfor = LOOKFOR_TERM;
8350 amount = get_indent(); /* XXX */
8351 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008352 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 }
8354 ++whilelevel;
8355 }
8356
8357 /*
8358 * We are after a "normal" statement.
8359 * If we had another statement we can stop now and use the
8360 * indent of that other statement.
8361 * Otherwise the indent of the current statement may be used,
8362 * search backwards for the next "normal" statement.
8363 */
8364 else
8365 {
8366 /*
8367 * Skip single break line, if before a switch label. It
8368 * may be lined up with the case label.
8369 */
8370 if (lookfor == LOOKFOR_NOBREAK
8371 && cin_isbreak(skipwhite(ml_get_curline())))
8372 {
8373 lookfor = LOOKFOR_ANY;
8374 continue;
8375 }
8376
8377 /*
8378 * Handle "do {" line.
8379 */
8380 if (whilelevel > 0)
8381 {
8382 l = cin_skipcomment(ml_get_curline());
8383 if (cin_isdo(l))
8384 {
8385 amount = get_indent(); /* XXX */
8386 --whilelevel;
8387 continue;
8388 }
8389 }
8390
8391 /*
8392 * Found a terminated line above an unterminated line. Add
8393 * the amount for a continuation line.
8394 * x = 1;
8395 * y = foo +
8396 * -> here;
8397 * or
8398 * int x = 1;
8399 * int foo,
8400 * -> here;
8401 */
8402 if (lookfor == LOOKFOR_UNTERM
8403 || lookfor == LOOKFOR_ENUM_OR_INIT)
8404 {
8405 if (cont_amount > 0)
8406 amount = cont_amount;
8407 else
8408 amount += ind_continuation;
8409 break;
8410 }
8411
8412 /*
8413 * Found a terminated line above a terminated line or "if"
8414 * etc. line. Use the amount of the line below us.
8415 * x = 1; x = 1;
8416 * if (asdf) y = 2;
8417 * while (asdf) ->here;
8418 * here;
8419 * ->foo;
8420 */
8421 if (lookfor == LOOKFOR_TERM)
8422 {
8423 if (!lookfor_break && whilelevel == 0)
8424 break;
8425 }
8426
8427 /*
8428 * First line above the one we're indenting is terminated.
8429 * To know what needs to be done look further backward for
8430 * a terminated line.
8431 */
8432 else
8433 {
8434 /*
8435 * position the cursor over the rightmost paren, so
8436 * that matching it will take us back to the start of
8437 * the line. Helps for:
8438 * func(asdr,
8439 * asdfasdf);
8440 * here;
8441 */
8442term_again:
8443 l = ml_get_curline();
8444 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008445 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008446 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
8448 /*
8449 * Check if we are on a case label now. This is
8450 * handled above.
8451 * case xx: if ( asdf &&
8452 * asdf)
8453 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008454 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008456 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008459 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 continue;
8461 }
8462 }
8463
8464 /* When aligning with the case statement, don't align
8465 * with a statement after it.
8466 * case 1: { <-- don't use this { position
8467 * stat;
8468 * }
8469 * case 2:
8470 * stat;
8471 * }
8472 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008473 iscase = (curbuf->b_ind_keep_case_label
8474 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
8476 /*
8477 * Get indent and pointer to text for current line,
8478 * ignoring any jump label.
8479 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008480 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481
8482 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008483 amount += curbuf->b_ind_open_extra;
8484 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008485 l = skipwhite(l);
8486 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008487 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8489
8490 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008491 * When a terminated line starts with "else" skip to
8492 * the matching "if":
8493 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008494 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008495 * Need to use the scope of this "else". XXX
8496 * If whilelevel != 0 continue looking for a "do {".
8497 */
8498 if (lookfor == LOOKFOR_TERM
8499 && *l != '}'
8500 && cin_iselse(l)
8501 && whilelevel == 0)
8502 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008503 if ((trypos = find_start_brace()) == NULL
8504 || find_match(LOOKFOR_IF, trypos->lnum)
8505 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008506 break;
8507 continue;
8508 }
8509
8510 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 * If we're at the end of a block, skip to the start of
8512 * that block.
8513 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008514 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008515 if (find_last_paren(l, '{', '}') /* XXX */
8516 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008518 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 /* if not "else {" check for terminated again */
8520 /* but skip block for "} else {" */
8521 l = cin_skipcomment(ml_get_curline());
8522 if (*l == '}' || !cin_iselse(l))
8523 goto term_again;
8524 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008525 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 }
8527 }
8528 }
8529 }
8530 }
8531 }
8532
8533 /* add extra indent for a comment */
8534 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008535 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008536
8537 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008538 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8539 amount -= curbuf->b_ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 }
8541
8542 /*
8543 * ok -- we're not inside any sort of structure at all!
8544 *
8545 * this means we're at the top level, and everything should
8546 * basically just match where the previous line is, except
8547 * for the lines immediately following a function declaration,
8548 * which are K&R-style parameters and need to be indented.
8549 */
8550 else
8551 {
8552 /*
8553 * if our line starts with an open brace, forget about any
8554 * prevailing indent and make sure it looks like the start
8555 * of a function
8556 */
8557
8558 if (theline[0] == '{')
8559 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008560 amount = curbuf->b_ind_first_open;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 }
8562
8563 /*
8564 * If the NEXT line is a function declaration, the current
8565 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008566 * Don't do this if the current line looks like a comment or if the
8567 * current line is terminated, ie. ends in ';', or if the current line
8568 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 */
8570 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8571 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008572 && vim_strchr(theline, '{') == NULL
8573 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 && !cin_ends_in(theline, (char_u *)":", NULL)
8575 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008576 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008577 cur_curpos.lnum + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 && !cin_isterminated(theline, FALSE, TRUE))
8579 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008580 amount = curbuf->b_ind_func_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 }
8582 else
8583 {
8584 amount = 0;
8585 curwin->w_cursor = cur_curpos;
8586
8587 /* search backwards until we find something we recognize */
8588
8589 while (curwin->w_cursor.lnum > 1)
8590 {
8591 curwin->w_cursor.lnum--;
8592 curwin->w_cursor.col = 0;
8593
8594 l = ml_get_curline();
8595
8596 /*
8597 * If we're in a comment now, skip to the start of the comment.
8598 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008599 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {
8601 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008602 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 continue;
8604 }
8605
8606 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008607 * Are we at the start of a cpp base class declaration or
8608 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008610 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008611 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar18144c82006-04-12 21:52:12 +00008612 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008613 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008614 l = ml_get_curline();
8615 }
8616 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008618 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008619 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 break;
8621 }
8622
8623 /*
8624 * Skip preprocessor directives and blank lines.
8625 */
8626 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8627 continue;
8628
8629 if (cin_nocode(l))
8630 continue;
8631
8632 /*
8633 * If the previous line ends in ',', use one level of
8634 * indentation:
8635 * int foo,
8636 * bar;
8637 * do this before checking for '}' in case of eg.
8638 * enum foobar
8639 * {
8640 * ...
8641 * } foo,
8642 * bar;
8643 */
8644 n = 0;
8645 if (cin_ends_in(l, (char_u *)",", NULL)
8646 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8647 {
8648 /* take us back to opening paren */
8649 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008650 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008651 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008652 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 /* For a line ending in ',' that is a continuation line go
8655 * back to the first line with a backslash:
8656 * char *foo = "bla\
8657 * bla",
8658 * here;
8659 */
8660 while (n == 0 && curwin->w_cursor.lnum > 1)
8661 {
8662 l = ml_get(curwin->w_cursor.lnum - 1);
8663 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8664 break;
8665 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008666 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 }
8668
8669 amount = get_indent(); /* XXX */
8670
8671 if (amount == 0)
8672 amount = cin_first_id_amount();
8673 if (amount == 0)
8674 amount = ind_continuation;
8675 break;
8676 }
8677
8678 /*
8679 * If the line looks like a function declaration, and we're
8680 * not in a comment, put it the left margin.
8681 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008682 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 break;
8684 l = ml_get_curline();
8685
8686 /*
8687 * Finding the closing '}' of a previous function. Put
8688 * current line at the left margin. For when 'cino' has "fs".
8689 */
8690 if (*skipwhite(l) == '}')
8691 break;
8692
8693 /* (matching {)
8694 * If the previous line ends on '};' (maybe followed by
8695 * comments) align at column 0. For example:
8696 * char *string_array[] = { "foo",
8697 * / * x * / "b};ar" }; / * foobar * /
8698 */
8699 if (cin_ends_in(l, (char_u *)"};", NULL))
8700 break;
8701
8702 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008703 * Find a line only has a semicolon that belongs to a previous
8704 * line ending in '}', e.g. before an #endif. Don't increase
8705 * indent then.
8706 */
8707 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8708 {
8709 pos_T curpos_save = curwin->w_cursor;
8710
8711 while (curwin->w_cursor.lnum > 1)
8712 {
8713 look = ml_get(--curwin->w_cursor.lnum);
8714 if (!(cin_nocode(look) || cin_ispreproc_cont(
8715 &look, &curwin->w_cursor.lnum)))
8716 break;
8717 }
8718 if (curwin->w_cursor.lnum > 0
8719 && cin_ends_in(look, (char_u *)"}", NULL))
8720 break;
8721
8722 curwin->w_cursor = curpos_save;
8723 }
8724
8725 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 * If the PREVIOUS line is a function declaration, the current
8727 * line (and the ones that follow) needs to be indented as
8728 * parameters.
8729 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008730 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008732 amount = curbuf->b_ind_param;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 break;
8734 }
8735
8736 /*
8737 * If the previous line ends in ';' and the line before the
8738 * previous line ends in ',' or '\', ident to column zero:
8739 * int foo,
8740 * bar;
8741 * indent_to_0 here;
8742 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008743 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {
8745 l = ml_get(curwin->w_cursor.lnum - 1);
8746 if (cin_ends_in(l, (char_u *)",", NULL)
8747 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8748 break;
8749 l = ml_get_curline();
8750 }
8751
8752 /*
8753 * Doesn't look like anything interesting -- so just
8754 * use the indent of this line.
8755 *
8756 * Position the cursor over the rightmost paren, so that
8757 * matching it will take us back to the start of the line.
8758 */
8759 find_last_paren(l, '(', ')');
8760
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008761 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008762 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 amount = get_indent(); /* XXX */
8764 break;
8765 }
8766
8767 /* add extra indent for a comment */
8768 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008769 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770
8771 /* add extra indent if the previous line ended in a backslash:
8772 * "asdfasdf\
8773 * here";
8774 * char *foo = "asdf\
8775 * here";
8776 */
8777 if (cur_curpos.lnum > 1)
8778 {
8779 l = ml_get(cur_curpos.lnum - 1);
8780 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8781 {
8782 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8783 if (cur_amount > 0)
8784 amount = cur_amount;
8785 else if (cur_amount == 0)
8786 amount += ind_continuation;
8787 }
8788 }
8789 }
8790 }
8791
8792theend:
8793 /* put the cursor back where it belongs */
8794 curwin->w_cursor = cur_curpos;
8795
8796 vim_free(linecopy);
8797
8798 if (amount < 0)
8799 return 0;
8800 return amount;
8801}
8802
8803 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008804find_match(lookfor, ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 int lookfor;
8806 linenr_T ourscope;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
8808 char_u *look;
8809 pos_T *theirscope;
8810 char_u *mightbeif;
8811 int elselevel;
8812 int whilelevel;
8813
8814 if (lookfor == LOOKFOR_IF)
8815 {
8816 elselevel = 1;
8817 whilelevel = 0;
8818 }
8819 else
8820 {
8821 elselevel = 0;
8822 whilelevel = 1;
8823 }
8824
8825 curwin->w_cursor.col = 0;
8826
8827 while (curwin->w_cursor.lnum > ourscope + 1)
8828 {
8829 curwin->w_cursor.lnum--;
8830 curwin->w_cursor.col = 0;
8831
8832 look = cin_skipcomment(ml_get_curline());
8833 if (cin_iselse(look)
8834 || cin_isif(look)
8835 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008836 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
8838 /*
8839 * if we've gone outside the braces entirely,
8840 * we must be out of scope...
8841 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008842 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (theirscope == NULL)
8844 break;
8845
8846 /*
8847 * and if the brace enclosing this is further
8848 * back than the one enclosing the else, we're
8849 * out of luck too.
8850 */
8851 if (theirscope->lnum < ourscope)
8852 break;
8853
8854 /*
8855 * and if they're enclosed in a *deeper* brace,
8856 * then we can ignore it because it's in a
8857 * different scope...
8858 */
8859 if (theirscope->lnum > ourscope)
8860 continue;
8861
8862 /*
8863 * if it was an "else" (that's not an "else if")
8864 * then we need to go back to another if, so
8865 * increment elselevel
8866 */
8867 look = cin_skipcomment(ml_get_curline());
8868 if (cin_iselse(look))
8869 {
8870 mightbeif = cin_skipcomment(look + 4);
8871 if (!cin_isif(mightbeif))
8872 ++elselevel;
8873 continue;
8874 }
8875
8876 /*
8877 * if it was a "while" then we need to go back to
8878 * another "do", so increment whilelevel. XXX
8879 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008880 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {
8882 ++whilelevel;
8883 continue;
8884 }
8885
8886 /* If it's an "if" decrement elselevel */
8887 look = cin_skipcomment(ml_get_curline());
8888 if (cin_isif(look))
8889 {
8890 elselevel--;
8891 /*
8892 * When looking for an "if" ignore "while"s that
8893 * get in the way.
8894 */
8895 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8896 whilelevel = 0;
8897 }
8898
8899 /* If it's a "do" decrement whilelevel */
8900 if (cin_isdo(look))
8901 whilelevel--;
8902
8903 /*
8904 * if we've used up all the elses, then
8905 * this must be the if that we want!
8906 * match the indent level of that if.
8907 */
8908 if (elselevel <= 0 && whilelevel <= 0)
8909 {
8910 return OK;
8911 }
8912 }
8913 }
8914 return FAIL;
8915}
8916
8917# if defined(FEAT_EVAL) || defined(PROTO)
8918/*
8919 * Get indent level from 'indentexpr'.
8920 */
8921 int
8922get_expr_indent()
8923{
8924 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008925 pos_T save_pos;
8926 colnr_T save_curswant;
8927 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008929 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8930 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008932 /* Save and restore cursor position and curswant, in case it was changed
8933 * via :normal commands */
8934 save_pos = curwin->w_cursor;
8935 save_curswant = curwin->w_curswant;
8936 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008938 if (use_sandbox)
8939 ++sandbox;
8940 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008942 if (use_sandbox)
8943 --sandbox;
8944 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
8946 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8947 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8948 * command. */
8949 save_State = State;
8950 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008951 curwin->w_cursor = save_pos;
8952 curwin->w_curswant = save_curswant;
8953 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 check_cursor();
8955 State = save_State;
8956
8957 /* If there is an error, just keep the current indent. */
8958 if (indent < 0)
8959 indent = get_indent();
8960
8961 return indent;
8962}
8963# endif
8964
8965#endif /* FEAT_CINDENT */
8966
8967#if defined(FEAT_LISP) || defined(PROTO)
8968
8969static int lisp_match __ARGS((char_u *p));
8970
8971 static int
8972lisp_match(p)
8973 char_u *p;
8974{
8975 char_u buf[LSIZE];
8976 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01008977 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
8979 while (*word != NUL)
8980 {
8981 (void)copy_option_part(&word, buf, LSIZE, ",");
8982 len = (int)STRLEN(buf);
8983 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8984 return TRUE;
8985 }
8986 return FALSE;
8987}
8988
8989/*
8990 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8991 * The incompatible newer method is quite a bit better at indenting
8992 * code in lisp-like languages than the traditional one; it's still
8993 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8994 *
8995 * TODO:
8996 * Findmatch() should be adapted for lisp, also to make showmatch
8997 * work correctly: now (v5.3) it seems all C/C++ oriented:
8998 * - it does not recognize the #\( and #\) notations as character literals
8999 * - it doesn't know about comments starting with a semicolon
9000 * - it incorrectly interprets '(' as a character literal
9001 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009002 * Update from Sergey Khorev:
9003 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 */
9005 int
9006get_lisp_indent()
9007{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009008 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 int amount;
9010 char_u *that;
9011 colnr_T col;
9012 colnr_T firsttry;
9013 int parencount, quotecount;
9014 int vi_lisp;
9015
9016 /* Set vi_lisp to use the vi-compatible method */
9017 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9018
9019 realpos = curwin->w_cursor;
9020 curwin->w_cursor.col = 0;
9021
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009022 if ((pos = findmatch(NULL, '(')) == NULL)
9023 pos = findmatch(NULL, '[');
9024 else
9025 {
9026 paren = *pos;
9027 pos = findmatch(NULL, '[');
9028 if (pos == NULL || ltp(pos, &paren))
9029 pos = &paren;
9030 }
9031 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 {
9033 /* Extra trick: Take the indent of the first previous non-white
9034 * line that is at the same () level. */
9035 amount = -1;
9036 parencount = 0;
9037
9038 while (--curwin->w_cursor.lnum >= pos->lnum)
9039 {
9040 if (linewhite(curwin->w_cursor.lnum))
9041 continue;
9042 for (that = ml_get_curline(); *that != NUL; ++that)
9043 {
9044 if (*that == ';')
9045 {
9046 while (*(that + 1) != NUL)
9047 ++that;
9048 continue;
9049 }
9050 if (*that == '\\')
9051 {
9052 if (*(that + 1) != NUL)
9053 ++that;
9054 continue;
9055 }
9056 if (*that == '"' && *(that + 1) != NUL)
9057 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009058 while (*++that && *that != '"')
9059 {
9060 /* skipping escaped characters in the string */
9061 if (*that == '\\')
9062 {
9063 if (*++that == NUL)
9064 break;
9065 if (that[1] == NUL)
9066 {
9067 ++that;
9068 break;
9069 }
9070 }
9071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009073 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009075 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 --parencount;
9077 }
9078 if (parencount == 0)
9079 {
9080 amount = get_indent();
9081 break;
9082 }
9083 }
9084
9085 if (amount == -1)
9086 {
9087 curwin->w_cursor.lnum = pos->lnum;
9088 curwin->w_cursor.col = pos->col;
9089 col = pos->col;
9090
9091 that = ml_get_curline();
9092
9093 if (vi_lisp && get_indent() == 0)
9094 amount = 2;
9095 else
9096 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009097 char_u *line = that;
9098
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 amount = 0;
9100 while (*that && col)
9101 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009102 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 col--;
9104 }
9105
9106 /*
9107 * Some keywords require "body" indenting rules (the
9108 * non-standard-lisp ones are Scheme special forms):
9109 *
9110 * (let ((a 1)) instead (let ((a 1))
9111 * (...)) of (...))
9112 */
9113
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009114 if (!vi_lisp && (*that == '(' || *that == '[')
9115 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 amount += 2;
9117 else
9118 {
9119 that++;
9120 amount++;
9121 firsttry = amount;
9122
9123 while (vim_iswhite(*that))
9124 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009125 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 ++that;
9127 }
9128
9129 if (*that && *that != ';') /* not a comment line */
9130 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009131 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009133 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 firsttry++;
9135
9136 parencount = 0;
9137 quotecount = 0;
9138
9139 if (vi_lisp
9140 || (*that != '"'
9141 && *that != '\''
9142 && *that != '#'
9143 && (*that < '0' || *that > '9')))
9144 {
9145 while (*that
9146 && (!vim_iswhite(*that)
9147 || quotecount
9148 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009149 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 && !quotecount
9151 && !parencount
9152 && vi_lisp)))
9153 {
9154 if (*that == '"')
9155 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009156 if ((*that == '(' || *that == '[')
9157 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009159 if ((*that == ')' || *that == ']')
9160 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 --parencount;
9162 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009163 amount += lbr_chartabsize_adv(
9164 line, &that, (colnr_T)amount);
9165 amount += lbr_chartabsize_adv(
9166 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 }
9168 }
9169 while (vim_iswhite(*that))
9170 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009171 amount += lbr_chartabsize(
9172 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 that++;
9174 }
9175 if (!*that || *that == ';')
9176 amount = firsttry;
9177 }
9178 }
9179 }
9180 }
9181 }
9182 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009183 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184
9185 curwin->w_cursor = realpos;
9186
9187 return amount;
9188}
9189#endif /* FEAT_LISP */
9190
9191 void
9192prepare_to_exit()
9193{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009194#if defined(SIGHUP) && defined(SIG_IGN)
9195 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9196 * makes Vim exit and then handling SIGHUP causes various reentrance
9197 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009198 signal(SIGHUP, SIG_IGN);
9199#endif
9200
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201#ifdef FEAT_GUI
9202 if (gui.in_use)
9203 {
9204 gui.dying = TRUE;
9205 out_trash(); /* trash any pending output */
9206 }
9207 else
9208#endif
9209 {
9210 windgoto((int)Rows - 1, 0);
9211
9212 /*
9213 * Switch terminal mode back now, so messages end up on the "normal"
9214 * screen (if there are two screens).
9215 */
9216 settmode(TMODE_COOK);
9217#ifdef WIN3264
9218 if (can_end_termcap_mode(FALSE) == TRUE)
9219#endif
9220 stoptermcap();
9221 out_flush();
9222 }
9223}
9224
9225/*
9226 * Preserve files and exit.
9227 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009228 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9229 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 */
9231 void
9232preserve_exit()
9233{
9234 buf_T *buf;
9235
9236 prepare_to_exit();
9237
Bram Moolenaar4770d092006-01-12 23:22:24 +00009238 /* Setting this will prevent free() calls. That avoids calling free()
9239 * recursively when free() was invoked with a bad pointer. */
9240 really_exiting = TRUE;
9241
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 out_str(IObuff);
9243 screen_start(); /* don't know where cursor is now */
9244 out_flush();
9245
9246 ml_close_notmod(); /* close all not-modified buffers */
9247
9248 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9249 {
9250 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9251 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009252 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 screen_start(); /* don't know where cursor is now */
9254 out_flush();
9255 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9256 break;
9257 }
9258 }
9259
9260 ml_close_all(FALSE); /* close all memfiles, without deleting */
9261
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009262 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263
9264 getout(1);
9265}
9266
9267/*
9268 * return TRUE if "fname" exists.
9269 */
9270 int
9271vim_fexists(fname)
9272 char_u *fname;
9273{
9274 struct stat st;
9275
9276 if (mch_stat((char *)fname, &st))
9277 return FALSE;
9278 return TRUE;
9279}
9280
9281/*
9282 * Check for CTRL-C pressed, but only once in a while.
9283 * Should be used instead of ui_breakcheck() for functions that check for
9284 * each line in the file. Calling ui_breakcheck() each time takes too much
9285 * time, because it can be a system call.
9286 */
9287
9288#ifndef BREAKCHECK_SKIP
9289# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9290# define BREAKCHECK_SKIP 200
9291# else
9292# define BREAKCHECK_SKIP 32
9293# endif
9294#endif
9295
9296static int breakcheck_count = 0;
9297
9298 void
9299line_breakcheck()
9300{
9301 if (++breakcheck_count >= BREAKCHECK_SKIP)
9302 {
9303 breakcheck_count = 0;
9304 ui_breakcheck();
9305 }
9306}
9307
9308/*
9309 * Like line_breakcheck() but check 10 times less often.
9310 */
9311 void
9312fast_breakcheck()
9313{
9314 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9315 {
9316 breakcheck_count = 0;
9317 ui_breakcheck();
9318 }
9319}
9320
9321/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009322 * Invoke expand_wildcards() for one pattern.
9323 * Expand items like "%:h" before the expansion.
9324 * Returns OK or FAIL.
9325 */
9326 int
9327expand_wildcards_eval(pat, num_file, file, flags)
9328 char_u **pat; /* pointer to input pattern */
9329 int *num_file; /* resulting number of files */
9330 char_u ***file; /* array of resulting files */
9331 int flags; /* EW_DIR, etc. */
9332{
9333 int ret = FAIL;
9334 char_u *eval_pat = NULL;
9335 char_u *exp_pat = *pat;
9336 char_u *ignored_msg;
9337 int usedlen;
9338
9339 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9340 {
9341 ++emsg_off;
9342 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9343 NULL, &ignored_msg, NULL);
9344 --emsg_off;
9345 if (eval_pat != NULL)
9346 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9347 }
9348
9349 if (exp_pat != NULL)
9350 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9351
9352 if (eval_pat != NULL)
9353 {
9354 vim_free(exp_pat);
9355 vim_free(eval_pat);
9356 }
9357
9358 return ret;
9359}
9360
9361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9363 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009364 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 */
9366 int
9367expand_wildcards(num_pat, pat, num_file, file, flags)
9368 int num_pat; /* number of input patterns */
9369 char_u **pat; /* array of input patterns */
9370 int *num_file; /* resulting number of files */
9371 char_u ***file; /* array of resulting files */
9372 int flags; /* EW_DIR, etc. */
9373{
9374 int retval;
9375 int i, j;
9376 char_u *p;
9377 int non_suf_match; /* number without matching suffix */
9378
9379 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9380
9381 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009382 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 return retval;
9384
9385#ifdef FEAT_WILDIGN
9386 /*
9387 * Remove names that match 'wildignore'.
9388 */
9389 if (*p_wig)
9390 {
9391 char_u *ffname;
9392
9393 /* check all files in (*file)[] */
9394 for (i = 0; i < *num_file; ++i)
9395 {
9396 ffname = FullName_save((*file)[i], FALSE);
9397 if (ffname == NULL) /* out of memory */
9398 break;
9399# ifdef VMS
9400 vms_remove_version(ffname);
9401# endif
9402 if (match_file_list(p_wig, (*file)[i], ffname))
9403 {
9404 /* remove this matching file from the list */
9405 vim_free((*file)[i]);
9406 for (j = i; j + 1 < *num_file; ++j)
9407 (*file)[j] = (*file)[j + 1];
9408 --*num_file;
9409 --i;
9410 }
9411 vim_free(ffname);
9412 }
9413 }
9414#endif
9415
9416 /*
9417 * Move the names where 'suffixes' match to the end.
9418 */
9419 if (*num_file > 1)
9420 {
9421 non_suf_match = 0;
9422 for (i = 0; i < *num_file; ++i)
9423 {
9424 if (!match_suffix((*file)[i]))
9425 {
9426 /*
9427 * Move the name without matching suffix to the front
9428 * of the list.
9429 */
9430 p = (*file)[i];
9431 for (j = i; j > non_suf_match; --j)
9432 (*file)[j] = (*file)[j - 1];
9433 (*file)[non_suf_match++] = p;
9434 }
9435 }
9436 }
9437
9438 return retval;
9439}
9440
9441/*
9442 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9443 */
9444 int
9445match_suffix(fname)
9446 char_u *fname;
9447{
9448 int fnamelen, setsuflen;
9449 char_u *setsuf;
9450#define MAXSUFLEN 30 /* maximum length of a file suffix */
9451 char_u suf_buf[MAXSUFLEN];
9452
9453 fnamelen = (int)STRLEN(fname);
9454 setsuflen = 0;
9455 for (setsuf = p_su; *setsuf; )
9456 {
9457 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009458 if (setsuflen == 0)
9459 {
9460 char_u *tail = gettail(fname);
9461
9462 /* empty entry: match name without a '.' */
9463 if (vim_strchr(tail, '.') == NULL)
9464 {
9465 setsuflen = 1;
9466 break;
9467 }
9468 }
9469 else
9470 {
9471 if (fnamelen >= setsuflen
9472 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9473 (size_t)setsuflen) == 0)
9474 break;
9475 setsuflen = 0;
9476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 }
9478 return (setsuflen != 0);
9479}
9480
9481#if !defined(NO_EXPANDPATH) || defined(PROTO)
9482
9483# ifdef VIM_BACKTICK
9484static int vim_backtick __ARGS((char_u *p));
9485static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9486# endif
9487
9488# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9489/*
9490 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9491 * it's shared between these systems.
9492 */
9493# if defined(DJGPP) || defined(PROTO)
9494# define _cdecl /* DJGPP doesn't have this */
9495# else
9496# ifdef __BORLANDC__
9497# define _cdecl _RTLENTRYF
9498# endif
9499# endif
9500
9501/*
9502 * comparison function for qsort in dos_expandpath()
9503 */
9504 static int _cdecl
9505pstrcmp(const void *a, const void *b)
9506{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009507 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508}
9509
9510# ifndef WIN3264
9511 static void
9512namelowcpy(
9513 char_u *d,
9514 char_u *s)
9515{
9516# ifdef DJGPP
9517 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9518 while (*s)
9519 *d++ = *s++;
9520 else
9521# endif
9522 while (*s)
9523 *d++ = TOLOWER_LOC(*s++);
9524 *d = NUL;
9525}
9526# endif
9527
9528/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009529 * Recursively expand one path component into all matching files and/or
9530 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 * Return the number of matches found.
9532 * "path" has backslashes before chars that are not to be expanded, starting
9533 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009534 * Return the number of matches found.
9535 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 */
9537 static int
9538dos_expandpath(
9539 garray_T *gap,
9540 char_u *path,
9541 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009542 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009543 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009545 char_u *buf;
9546 char_u *path_end;
9547 char_u *p, *s, *e;
9548 int start_len = gap->ga_len;
9549 char_u *pat;
9550 regmatch_T regmatch;
9551 int starts_with_dot;
9552 int matches;
9553 int len;
9554 int starstar = FALSE;
9555 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556#ifdef WIN3264
9557 WIN32_FIND_DATA fb;
9558 HANDLE hFind = (HANDLE)0;
9559# ifdef FEAT_MBYTE
9560 WIN32_FIND_DATAW wfb;
9561 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9562# endif
9563#else
9564 struct ffblk fb;
9565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009567 int ok;
9568
9569 /* Expanding "**" may take a long time, check for CTRL-C. */
9570 if (stardepth > 0)
9571 {
9572 ui_breakcheck();
9573 if (got_int)
9574 return 0;
9575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576
9577 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009578 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 if (buf == NULL)
9580 return 0;
9581
9582 /*
9583 * Find the first part in the path name that contains a wildcard or a ~1.
9584 * Copy it into buf, including the preceding characters.
9585 */
9586 p = buf;
9587 s = buf;
9588 e = NULL;
9589 path_end = path;
9590 while (*path_end != NUL)
9591 {
9592 /* May ignore a wildcard that has a backslash before it; it will
9593 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9594 if (path_end >= path + wildoff && rem_backslash(path_end))
9595 *p++ = *path_end++;
9596 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9597 {
9598 if (e != NULL)
9599 break;
9600 s = p + 1;
9601 }
9602 else if (path_end >= path + wildoff
9603 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9604 e = p;
9605#ifdef FEAT_MBYTE
9606 if (has_mbyte)
9607 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009608 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 STRNCPY(p, path_end, len);
9610 p += len;
9611 path_end += len;
9612 }
9613 else
9614#endif
9615 *p++ = *path_end++;
9616 }
9617 e = p;
9618 *e = NUL;
9619
9620 /* now we have one wildcard component between s and e */
9621 /* Remove backslashes between "wildoff" and the start of the wildcard
9622 * component. */
9623 for (p = buf + wildoff; p < s; ++p)
9624 if (rem_backslash(p))
9625 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009626 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 --e;
9628 --s;
9629 }
9630
Bram Moolenaar231334e2005-07-25 20:46:57 +00009631 /* Check for "**" between "s" and "e". */
9632 for (p = s; p < e; ++p)
9633 if (p[0] == '*' && p[1] == '*')
9634 starstar = TRUE;
9635
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 starts_with_dot = (*s == '.');
9637 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9638 if (pat == NULL)
9639 {
9640 vim_free(buf);
9641 return 0;
9642 }
9643
9644 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009645 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009646 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 regmatch.rm_ic = TRUE; /* Always ignore case */
9648 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009649 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009650 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 vim_free(pat);
9652
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009653 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 {
9655 vim_free(buf);
9656 return 0;
9657 }
9658
9659 /* remember the pattern or file name being looked for */
9660 matchname = vim_strsave(s);
9661
Bram Moolenaar231334e2005-07-25 20:46:57 +00009662 /* If "**" is by itself, this is the first time we encounter it and more
9663 * is following then find matches without any directory. */
9664 if (!didstar && stardepth < 100 && starstar && e - s == 2
9665 && *path_end == '/')
9666 {
9667 STRCPY(s, path_end + 1);
9668 ++stardepth;
9669 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9670 --stardepth;
9671 }
9672
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 /* Scan all files in the directory with "dir/ *.*" */
9674 STRCPY(s, "*.*");
9675#ifdef WIN3264
9676# ifdef FEAT_MBYTE
9677 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9678 {
9679 /* The active codepage differs from 'encoding'. Attempt using the
9680 * wide function. If it fails because it is not implemented fall back
9681 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009682 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 if (wn != NULL)
9684 {
9685 hFind = FindFirstFileW(wn, &wfb);
9686 if (hFind == INVALID_HANDLE_VALUE
9687 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9688 {
9689 vim_free(wn);
9690 wn = NULL;
9691 }
9692 }
9693 }
9694
9695 if (wn == NULL)
9696# endif
9697 hFind = FindFirstFile(buf, &fb);
9698 ok = (hFind != INVALID_HANDLE_VALUE);
9699#else
9700 /* If we are expanding wildcards we try both files and directories */
9701 ok = (findfirst((char *)buf, &fb,
9702 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9703#endif
9704
9705 while (ok)
9706 {
9707#ifdef WIN3264
9708# ifdef FEAT_MBYTE
9709 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009710 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 else
9712# endif
9713 p = (char_u *)fb.cFileName;
9714#else
9715 p = (char_u *)fb.ff_name;
9716#endif
9717 /* Ignore entries starting with a dot, unless when asked for. Accept
9718 * all entries found with "matchname". */
9719 if ((p[0] != '.' || starts_with_dot)
9720 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009721 || (regmatch.regprog != NULL
9722 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009723 || ((flags & EW_NOTWILD)
9724 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 {
9726#ifdef WIN3264
9727 STRCPY(s, p);
9728#else
9729 namelowcpy(s, p);
9730#endif
9731 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009732
9733 if (starstar && stardepth < 100)
9734 {
9735 /* For "**" in the pattern first go deeper in the tree to
9736 * find matches. */
9737 STRCPY(buf + len, "/**");
9738 STRCPY(buf + len + 3, path_end);
9739 ++stardepth;
9740 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9741 --stardepth;
9742 }
9743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 STRCPY(buf + len, path_end);
9745 if (mch_has_exp_wildcard(path_end))
9746 {
9747 /* need to expand another component of the path */
9748 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009749 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 }
9751 else
9752 {
9753 /* no more wildcards, check if there is a match */
9754 /* remove backslashes for the remaining components only */
9755 if (*path_end != 0)
9756 backslash_halve(buf + len + 1);
9757 if (mch_getperm(buf) >= 0) /* add existing file */
9758 addfile(gap, buf, flags);
9759 }
9760 }
9761
9762#ifdef WIN3264
9763# ifdef FEAT_MBYTE
9764 if (wn != NULL)
9765 {
9766 vim_free(p);
9767 ok = FindNextFileW(hFind, &wfb);
9768 }
9769 else
9770# endif
9771 ok = FindNextFile(hFind, &fb);
9772#else
9773 ok = (findnext(&fb) == 0);
9774#endif
9775
9776 /* If no more matches and no match was used, try expanding the name
9777 * itself. Finds the long name of a short filename. */
9778 if (!ok && matchname != NULL && gap->ga_len == start_len)
9779 {
9780 STRCPY(s, matchname);
9781#ifdef WIN3264
9782 FindClose(hFind);
9783# ifdef FEAT_MBYTE
9784 if (wn != NULL)
9785 {
9786 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009787 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (wn != NULL)
9789 hFind = FindFirstFileW(wn, &wfb);
9790 }
9791 if (wn == NULL)
9792# endif
9793 hFind = FindFirstFile(buf, &fb);
9794 ok = (hFind != INVALID_HANDLE_VALUE);
9795#else
9796 ok = (findfirst((char *)buf, &fb,
9797 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9798#endif
9799 vim_free(matchname);
9800 matchname = NULL;
9801 }
9802 }
9803
9804#ifdef WIN3264
9805 FindClose(hFind);
9806# ifdef FEAT_MBYTE
9807 vim_free(wn);
9808# endif
9809#endif
9810 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009811 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 vim_free(matchname);
9813
9814 matches = gap->ga_len - start_len;
9815 if (matches > 0)
9816 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9817 sizeof(char_u *), pstrcmp);
9818 return matches;
9819}
9820
9821 int
9822mch_expandpath(
9823 garray_T *gap,
9824 char_u *path,
9825 int flags) /* EW_* flags */
9826{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009827 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828}
9829# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9830
Bram Moolenaar231334e2005-07-25 20:46:57 +00009831#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9832 || defined(PROTO)
9833/*
9834 * Unix style wildcard expansion code.
9835 * It's here because it's used both for Unix and Mac.
9836 */
9837static int pstrcmp __ARGS((const void *, const void *));
9838
9839 static int
9840pstrcmp(a, b)
9841 const void *a, *b;
9842{
9843 return (pathcmp(*(char **)a, *(char **)b, -1));
9844}
9845
9846/*
9847 * Recursively expand one path component into all matching files and/or
9848 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9849 * "path" has backslashes before chars that are not to be expanded, starting
9850 * at "path + wildoff".
9851 * Return the number of matches found.
9852 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9853 */
9854 int
9855unix_expandpath(gap, path, wildoff, flags, didstar)
9856 garray_T *gap;
9857 char_u *path;
9858 int wildoff;
9859 int flags; /* EW_* flags */
9860 int didstar; /* expanded "**" once already */
9861{
9862 char_u *buf;
9863 char_u *path_end;
9864 char_u *p, *s, *e;
9865 int start_len = gap->ga_len;
9866 char_u *pat;
9867 regmatch_T regmatch;
9868 int starts_with_dot;
9869 int matches;
9870 int len;
9871 int starstar = FALSE;
9872 static int stardepth = 0; /* depth for "**" expansion */
9873
9874 DIR *dirp;
9875 struct dirent *dp;
9876
9877 /* Expanding "**" may take a long time, check for CTRL-C. */
9878 if (stardepth > 0)
9879 {
9880 ui_breakcheck();
9881 if (got_int)
9882 return 0;
9883 }
9884
9885 /* make room for file name */
9886 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9887 if (buf == NULL)
9888 return 0;
9889
9890 /*
9891 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009892 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009893 * Copy it into "buf", including the preceding characters.
9894 */
9895 p = buf;
9896 s = buf;
9897 e = NULL;
9898 path_end = path;
9899 while (*path_end != NUL)
9900 {
9901 /* May ignore a wildcard that has a backslash before it; it will
9902 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9903 if (path_end >= path + wildoff && rem_backslash(path_end))
9904 *p++ = *path_end++;
9905 else if (*path_end == '/')
9906 {
9907 if (e != NULL)
9908 break;
9909 s = p + 1;
9910 }
9911 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009912 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009913 || (!p_fic && (flags & EW_ICASE)
9914 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009915 e = p;
9916#ifdef FEAT_MBYTE
9917 if (has_mbyte)
9918 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009919 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009920 STRNCPY(p, path_end, len);
9921 p += len;
9922 path_end += len;
9923 }
9924 else
9925#endif
9926 *p++ = *path_end++;
9927 }
9928 e = p;
9929 *e = NUL;
9930
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009931 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009932 /* Remove backslashes between "wildoff" and the start of the wildcard
9933 * component. */
9934 for (p = buf + wildoff; p < s; ++p)
9935 if (rem_backslash(p))
9936 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009937 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009938 --e;
9939 --s;
9940 }
9941
9942 /* Check for "**" between "s" and "e". */
9943 for (p = s; p < e; ++p)
9944 if (p[0] == '*' && p[1] == '*')
9945 starstar = TRUE;
9946
9947 /* convert the file pattern to a regexp pattern */
9948 starts_with_dot = (*s == '.');
9949 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9950 if (pat == NULL)
9951 {
9952 vim_free(buf);
9953 return 0;
9954 }
9955
9956 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009957 if (flags & EW_ICASE)
9958 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9959 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009960 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009961 if (flags & (EW_NOERROR | EW_NOTWILD))
9962 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009963 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009964 if (flags & (EW_NOERROR | EW_NOTWILD))
9965 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009966 vim_free(pat);
9967
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009968 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009969 {
9970 vim_free(buf);
9971 return 0;
9972 }
9973
9974 /* If "**" is by itself, this is the first time we encounter it and more
9975 * is following then find matches without any directory. */
9976 if (!didstar && stardepth < 100 && starstar && e - s == 2
9977 && *path_end == '/')
9978 {
9979 STRCPY(s, path_end + 1);
9980 ++stardepth;
9981 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9982 --stardepth;
9983 }
9984
9985 /* open the directory for scanning */
9986 *s = NUL;
9987 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9988
9989 /* Find all matching entries */
9990 if (dirp != NULL)
9991 {
9992 for (;;)
9993 {
9994 dp = readdir(dirp);
9995 if (dp == NULL)
9996 break;
9997 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009998 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9999 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010000 || ((flags & EW_NOTWILD)
10001 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010002 {
10003 STRCPY(s, dp->d_name);
10004 len = STRLEN(buf);
10005
10006 if (starstar && stardepth < 100)
10007 {
10008 /* For "**" in the pattern first go deeper in the tree to
10009 * find matches. */
10010 STRCPY(buf + len, "/**");
10011 STRCPY(buf + len + 3, path_end);
10012 ++stardepth;
10013 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10014 --stardepth;
10015 }
10016
10017 STRCPY(buf + len, path_end);
10018 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10019 {
10020 /* need to expand another component of the path */
10021 /* remove backslashes for the remaining components only */
10022 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10023 }
10024 else
10025 {
10026 /* no more wildcards, check if there is a match */
10027 /* remove backslashes for the remaining components only */
10028 if (*path_end != NUL)
10029 backslash_halve(buf + len + 1);
10030 if (mch_getperm(buf) >= 0) /* add existing file */
10031 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010032#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010033 size_t precomp_len = STRLEN(buf)+1;
10034 char_u *precomp_buf =
10035 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010036
Bram Moolenaar231334e2005-07-25 20:46:57 +000010037 if (precomp_buf)
10038 {
10039 mch_memmove(buf, precomp_buf, precomp_len);
10040 vim_free(precomp_buf);
10041 }
10042#endif
10043 addfile(gap, buf, flags);
10044 }
10045 }
10046 }
10047 }
10048
10049 closedir(dirp);
10050 }
10051
10052 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010053 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010054
10055 matches = gap->ga_len - start_len;
10056 if (matches > 0)
10057 qsort(((char_u **)gap->ga_data) + start_len, matches,
10058 sizeof(char_u *), pstrcmp);
10059 return matches;
10060}
10061#endif
10062
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010063#if defined(FEAT_SEARCHPATH)
10064static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10065static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010066static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10067static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010068static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10069static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10070
10071/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010072 * Moves "*psep" back to the previous path separator in "path".
10073 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010074 */
10075 static int
10076find_previous_pathsep(path, psep)
10077 char_u *path;
10078 char_u **psep;
10079{
10080 /* skip the current separator */
10081 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010082 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010083
10084 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010085 while (*psep > path)
10086 {
10087 if (vim_ispathsep(**psep))
10088 return OK;
10089 mb_ptr_back(path, *psep);
10090 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010091
10092 return FAIL;
10093}
10094
10095/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010096 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10097 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010098 */
10099 static int
10100is_unique(maybe_unique, gap, i)
10101 char_u *maybe_unique;
10102 garray_T *gap;
10103 int i;
10104{
10105 int j;
10106 int candidate_len;
10107 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010108 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010109 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010110
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010111 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010112 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010113 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010114 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010115
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010116 candidate_len = (int)STRLEN(maybe_unique);
10117 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010118 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010119 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010120
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010121 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010122 if (fnamecmp(maybe_unique, rival) == 0
10123 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010124 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010125 }
10126
Bram Moolenaar162bd912010-07-28 22:29:10 +020010127 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010128}
10129
10130/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010131 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010132 * paths are expanded to their equivalent fullpath. This includes the "."
10133 * (relative to current buffer directory) and empty path (relative to current
10134 * directory) notations.
10135 *
10136 * TODO: handle upward search (;) and path limiter (**N) notations by
10137 * expanding each into their equivalent path(s).
10138 */
10139 static void
10140expand_path_option(curdir, gap)
10141 char_u *curdir;
10142 garray_T *gap;
10143{
10144 char_u *path_option = *curbuf->b_p_path == NUL
10145 ? p_path : curbuf->b_p_path;
10146 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010147 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010148 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010149
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010150 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010151 return;
10152
10153 while (*path_option != NUL)
10154 {
10155 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10156
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010157 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010158 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010159 /* Relative to current buffer:
10160 * "/path/file" + "." -> "/path/"
10161 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010162 if (curbuf->b_ffname == NULL)
10163 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010164 p = gettail(curbuf->b_ffname);
10165 len = (int)(p - curbuf->b_ffname);
10166 if (len + (int)STRLEN(buf) >= MAXPATHL)
10167 continue;
10168 if (buf[1] == NUL)
10169 buf[len] = NUL;
10170 else
10171 STRMOVE(buf + len, buf + 2);
10172 mch_memmove(buf, curbuf->b_ffname, len);
10173 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010174 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010175 else if (buf[0] == NUL)
10176 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010177 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010178 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010179 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010180 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010181 else if (!mch_isFullName(buf))
10182 {
10183 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010184 len = (int)STRLEN(curdir);
10185 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010186 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010187 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010188 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010189 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010190 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010191 }
10192
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010193 if (ga_grow(gap, 1) == FAIL)
10194 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010195
10196# if defined(MSWIN) || defined(MSDOS)
10197 /* Avoid the path ending in a backslash, it fails when a comma is
10198 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010199 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010200 if (buf[len - 1] == '\\')
10201 buf[len - 1] = '/';
10202# endif
10203
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010204 p = vim_strsave(buf);
10205 if (p == NULL)
10206 break;
10207 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010208 }
10209
10210 vim_free(buf);
10211}
10212
10213/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010214 * Returns a pointer to the file or directory name in "fname" that matches the
10215 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010216 *
10217 * path: /foo/bar/baz
10218 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010219 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010220 */
10221 static char_u *
10222get_path_cutoff(fname, gap)
10223 char_u *fname;
10224 garray_T *gap;
10225{
10226 int i;
10227 int maxlen = 0;
10228 char_u **path_part = (char_u **)gap->ga_data;
10229 char_u *cutoff = NULL;
10230
10231 for (i = 0; i < gap->ga_len; i++)
10232 {
10233 int j = 0;
10234
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010235 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010236# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010237 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10238#endif
10239 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010240 j++;
10241 if (j > maxlen)
10242 {
10243 maxlen = j;
10244 cutoff = &fname[j];
10245 }
10246 }
10247
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010248 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010249 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010250 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010251 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010252
10253 return cutoff;
10254}
10255
10256/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010257 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10258 * that they are unique with respect to each other while conserving the part
10259 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010260 */
10261 static void
10262uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010263 garray_T *gap;
10264 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010265{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010266 int i;
10267 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010268 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010269 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010270 char_u *pat;
10271 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010272 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010273 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010274 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010275 char_u **in_curdir = NULL;
10276 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010277
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010278 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010279 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010280
10281 /*
10282 * We need to prepend a '*' at the beginning of file_pattern so that the
10283 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010284 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010285 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010286 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010287 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010288 if (file_pattern == NULL)
10289 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010290 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010291 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010292 STRCAT(file_pattern, pattern);
10293 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10294 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010295 if (pat == NULL)
10296 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010297
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010298 regmatch.rm_ic = TRUE; /* always ignore case */
10299 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10300 vim_free(pat);
10301 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010302 return;
10303
Bram Moolenaar162bd912010-07-28 22:29:10 +020010304 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010305 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010306 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010307 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010308
10309 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010310 if (in_curdir == NULL)
10311 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010312
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010313 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010314 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010315 char_u *path = fnames[i];
10316 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010317 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010318 char_u *pathsep_p;
10319 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010320
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010321 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010322 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010323 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010324 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010325 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010326
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010327 /* Shorten the filename while maintaining its uniqueness */
10328 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010329
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010330 /* we start at the end of the path */
10331 pathsep_p = path + len - 1;
10332
10333 while (find_previous_pathsep(path, &pathsep_p))
10334 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10335 && is_unique(pathsep_p + 1, gap, i)
10336 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10337 {
10338 sort_again = TRUE;
10339 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10340 break;
10341 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010342
10343 if (mch_isFullName(path))
10344 {
10345 /*
10346 * Last resort: shorten relative to curdir if possible.
10347 * 'possible' means:
10348 * 1. It is under the current directory.
10349 * 2. The result is actually shorter than the original.
10350 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010351 * Before curdir After
10352 * /foo/bar/file.txt /foo/bar ./file.txt
10353 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10354 * /file.txt / /file.txt
10355 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010356 */
10357 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010358 if (short_name != NULL && short_name > path + 1
10359#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010360 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010361 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010362 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010363 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010364 && !vim_ispathsep(*short_name)
10365#endif
10366 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010367 {
10368 STRCPY(path, ".");
10369 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010370 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010371 }
10372 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010373 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010374 }
10375
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010376 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010377 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010378 {
10379 char_u *rel_path;
10380 char_u *path = in_curdir[i];
10381
10382 if (path == NULL)
10383 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010384
10385 /* If the {filename} is not unique, change it to ./{filename}.
10386 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010387 short_name = shorten_fname(path, curdir);
10388 if (short_name == NULL)
10389 short_name = path;
10390 if (is_unique(short_name, gap, i))
10391 {
10392 STRCPY(fnames[i], short_name);
10393 continue;
10394 }
10395
10396 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10397 if (rel_path == NULL)
10398 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010399 STRCPY(rel_path, ".");
10400 add_pathsep(rel_path);
10401 STRCAT(rel_path, short_name);
10402
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010403 vim_free(fnames[i]);
10404 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010405 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010406 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010407 }
10408
Bram Moolenaar162bd912010-07-28 22:29:10 +020010409theend:
10410 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010411 if (in_curdir != NULL)
10412 {
10413 for (i = 0; i < gap->ga_len; i++)
10414 vim_free(in_curdir[i]);
10415 vim_free(in_curdir);
10416 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010417 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010418 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010419
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010420 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010421 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010422}
10423
10424/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010425 * Calls globpath() with 'path' values for the given pattern and stores the
10426 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010427 * Returns the total number of matches.
10428 */
10429 static int
10430expand_in_path(gap, pattern, flags)
10431 garray_T *gap;
10432 char_u *pattern;
10433 int flags; /* EW_* flags */
10434{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010435 char_u *curdir;
10436 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010437 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010438
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010439 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010440 return 0;
10441 mch_dirname(curdir, MAXPATHL);
10442
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010443 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010444 expand_path_option(curdir, &path_ga);
10445 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010446 if (path_ga.ga_len == 0)
10447 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010448
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010449 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010450 ga_clear_strings(&path_ga);
10451 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010452 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010453
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010454 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010455 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010456
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010457 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010458}
10459#endif
10460
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010461#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10462/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010463 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10464 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010465 */
10466 void
10467remove_duplicates(gap)
10468 garray_T *gap;
10469{
10470 int i;
10471 int j;
10472 char_u **fnames = (char_u **)gap->ga_data;
10473
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010474 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010475 for (i = gap->ga_len - 1; i > 0; --i)
10476 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10477 {
10478 vim_free(fnames[i]);
10479 for (j = i + 1; j < gap->ga_len; ++j)
10480 fnames[j - 1] = fnames[j];
10481 --gap->ga_len;
10482 }
10483}
10484#endif
10485
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010486static int has_env_var __ARGS((char_u *p));
10487
10488/*
10489 * Return TRUE if "p" contains what looks like an environment variable.
10490 * Allowing for escaping.
10491 */
10492 static int
10493has_env_var(p)
10494 char_u *p;
10495{
10496 for ( ; *p; mb_ptr_adv(p))
10497 {
10498 if (*p == '\\' && p[1] != NUL)
10499 ++p;
10500 else if (vim_strchr((char_u *)
10501#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
10502 "$%"
10503#else
10504 "$"
10505#endif
10506 , *p) != NULL)
10507 return TRUE;
10508 }
10509 return FALSE;
10510}
10511
10512#ifdef SPECIAL_WILDCHAR
10513static int has_special_wildchar __ARGS((char_u *p));
10514
10515/*
10516 * Return TRUE if "p" contains a special wildcard character.
10517 * Allowing for escaping.
10518 */
10519 static int
10520has_special_wildchar(p)
10521 char_u *p;
10522{
10523 for ( ; *p; mb_ptr_adv(p))
10524 {
10525 if (*p == '\\' && p[1] != NUL)
10526 ++p;
10527 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10528 return TRUE;
10529 }
10530 return FALSE;
10531}
10532#endif
10533
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534/*
10535 * Generic wildcard expansion code.
10536 *
10537 * Characters in "pat" that should not be expanded must be preceded with a
10538 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10539 *
10540 * Return FAIL when no single file was found. In this case "num_file" is not
10541 * set, and "file" may contain an error message.
10542 * Return OK when some files found. "num_file" is set to the number of
10543 * matches, "file" to the array of matches. Call FreeWild() later.
10544 */
10545 int
10546gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10547 int num_pat; /* number of input patterns */
10548 char_u **pat; /* array of input patterns */
10549 int *num_file; /* resulting number of files */
10550 char_u ***file; /* array of resulting files */
10551 int flags; /* EW_* flags */
10552{
10553 int i;
10554 garray_T ga;
10555 char_u *p;
10556 static int recursive = FALSE;
10557 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010558#if defined(FEAT_SEARCHPATH)
10559 int did_expand_in_path = FALSE;
10560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561
10562 /*
10563 * expand_env() is called to expand things like "~user". If this fails,
10564 * it calls ExpandOne(), which brings us back here. In this case, always
10565 * call the machine specific expansion function, if possible. Otherwise,
10566 * return FAIL.
10567 */
10568 if (recursive)
10569#ifdef SPECIAL_WILDCHAR
10570 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10571#else
10572 return FAIL;
10573#endif
10574
10575#ifdef SPECIAL_WILDCHAR
10576 /*
10577 * If there are any special wildcard characters which we cannot handle
10578 * here, call machine specific function for all the expansion. This
10579 * avoids starting the shell for each argument separately.
10580 * For `=expr` do use the internal function.
10581 */
10582 for (i = 0; i < num_pat; i++)
10583 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010584 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585# ifdef VIM_BACKTICK
10586 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10587# endif
10588 )
10589 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10590 }
10591#endif
10592
10593 recursive = TRUE;
10594
10595 /*
10596 * The matching file names are stored in a growarray. Init it empty.
10597 */
10598 ga_init2(&ga, (int)sizeof(char_u *), 30);
10599
10600 for (i = 0; i < num_pat; ++i)
10601 {
10602 add_pat = -1;
10603 p = pat[i];
10604
10605#ifdef VIM_BACKTICK
10606 if (vim_backtick(p))
10607 add_pat = expand_backtick(&ga, p, flags);
10608 else
10609#endif
10610 {
10611 /*
10612 * First expand environment variables, "~/" and "~user/".
10613 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010614 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010616 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 if (p == NULL)
10618 p = pat[i];
10619#ifdef UNIX
10620 /*
10621 * On Unix, if expand_env() can't expand an environment
10622 * variable, use the shell to do that. Discard previously
10623 * found file names and start all over again.
10624 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010625 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 {
10627 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010628 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10630 flags);
10631 recursive = FALSE;
10632 return i;
10633 }
10634#endif
10635 }
10636
10637 /*
10638 * If there are wildcards: Expand file names and add each match to
10639 * the list. If there is no match, and EW_NOTFOUND is given, add
10640 * the pattern.
10641 * If there are no wildcards: Add the file name if it exists or
10642 * when EW_NOTFOUND is given.
10643 */
10644 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010645 {
10646#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010647 if ((flags & EW_PATH)
10648 && !mch_isFullName(p)
10649 && !(p[0] == '.'
10650 && (vim_ispathsep(p[1])
10651 || (p[1] == '.' && vim_ispathsep(p[2]))))
10652 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010653 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010654 /* :find completion where 'path' is used.
10655 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010656 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010657 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010658 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010659 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010660 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010661 else
10662#endif
10663 add_pat = mch_expandpath(&ga, p, flags);
10664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 }
10666
10667 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10668 {
10669 char_u *t = backslash_halve_save(p);
10670
10671#if defined(MACOS_CLASSIC)
10672 slash_to_colon(t);
10673#endif
10674 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10675 * "vim c:/" work. */
10676 if (flags & EW_NOTFOUND)
10677 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10678 else if (mch_getperm(t) >= 0)
10679 addfile(&ga, t, flags);
10680 vim_free(t);
10681 }
10682
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010683#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010684 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010685 uniquefy_paths(&ga, p);
10686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 if (p != pat[i])
10688 vim_free(p);
10689 }
10690
10691 *num_file = ga.ga_len;
10692 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10693
10694 recursive = FALSE;
10695
10696 return (ga.ga_data != NULL) ? OK : FAIL;
10697}
10698
10699# ifdef VIM_BACKTICK
10700
10701/*
10702 * Return TRUE if we can expand this backtick thing here.
10703 */
10704 static int
10705vim_backtick(p)
10706 char_u *p;
10707{
10708 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10709}
10710
10711/*
10712 * Expand an item in `backticks` by executing it as a command.
10713 * Currently only works when pat[] starts and ends with a `.
10714 * Returns number of file names found.
10715 */
10716 static int
10717expand_backtick(gap, pat, flags)
10718 garray_T *gap;
10719 char_u *pat;
10720 int flags; /* EW_* flags */
10721{
10722 char_u *p;
10723 char_u *cmd;
10724 char_u *buffer;
10725 int cnt = 0;
10726 int i;
10727
10728 /* Create the command: lop off the backticks. */
10729 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10730 if (cmd == NULL)
10731 return 0;
10732
10733#ifdef FEAT_EVAL
10734 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010735 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 else
10737#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010738 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010739 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 vim_free(cmd);
10741 if (buffer == NULL)
10742 return 0;
10743
10744 cmd = buffer;
10745 while (*cmd != NUL)
10746 {
10747 cmd = skipwhite(cmd); /* skip over white space */
10748 p = cmd;
10749 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10750 ++p;
10751 /* add an entry if it is not empty */
10752 if (p > cmd)
10753 {
10754 i = *p;
10755 *p = NUL;
10756 addfile(gap, cmd, flags);
10757 *p = i;
10758 ++cnt;
10759 }
10760 cmd = p;
10761 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10762 ++cmd;
10763 }
10764
10765 vim_free(buffer);
10766 return cnt;
10767}
10768# endif /* VIM_BACKTICK */
10769
10770/*
10771 * Add a file to a file list. Accepted flags:
10772 * EW_DIR add directories
10773 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010774 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 * EW_NOTFOUND add even when it doesn't exist
10776 * EW_ADDSLASH add slash after directory name
10777 */
10778 void
10779addfile(gap, f, flags)
10780 garray_T *gap;
10781 char_u *f; /* filename */
10782 int flags;
10783{
10784 char_u *p;
10785 int isdir;
10786
10787 /* if the file/dir doesn't exist, may not add it */
10788 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10789 return;
10790
10791#ifdef FNAME_ILLEGAL
10792 /* if the file/dir contains illegal characters, don't add it */
10793 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10794 return;
10795#endif
10796
10797 isdir = mch_isdir(f);
10798 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10799 return;
10800
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010801 /* If the file isn't executable, may not add it. Do accept directories. */
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010802 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f, NULL))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010803 return;
10804
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 /* Make room for another item in the file list. */
10806 if (ga_grow(gap, 1) == FAIL)
10807 return;
10808
10809 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10810 if (p == NULL)
10811 return;
10812
10813 STRCPY(p, f);
10814#ifdef BACKSLASH_IN_FILENAME
10815 slash_adjust(p);
10816#endif
10817 /*
10818 * Append a slash or backslash after directory names if none is present.
10819 */
10820#ifndef DONT_ADD_PATHSEP_TO_DIR
10821 if (isdir && (flags & EW_ADDSLASH))
10822 add_pathsep(p);
10823#endif
10824 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825}
10826#endif /* !NO_EXPANDPATH */
10827
10828#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10829
10830#ifndef SEEK_SET
10831# define SEEK_SET 0
10832#endif
10833#ifndef SEEK_END
10834# define SEEK_END 2
10835#endif
10836
10837/*
10838 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010839 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
10840 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 * Returns an allocated string, or NULL for error.
10842 */
10843 char_u *
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010844get_cmd_output(cmd, infile, flags, ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010846 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 int flags; /* can be SHELL_SILENT */
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010848 int *ret_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849{
10850 char_u *tempname;
10851 char_u *command;
10852 char_u *buffer = NULL;
10853 int len;
10854 int i = 0;
10855 FILE *fd;
10856
10857 if (check_restricted() || check_secure())
10858 return NULL;
10859
10860 /* get a name for the temp file */
10861 if ((tempname = vim_tempname('o')) == NULL)
10862 {
10863 EMSG(_(e_notmp));
10864 return NULL;
10865 }
10866
10867 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010868 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 if (command == NULL)
10870 goto done;
10871
10872 /*
10873 * Call the shell to execute the command (errors are ignored).
10874 * Don't check timestamps here.
10875 */
10876 ++no_check_timestamps;
10877 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10878 --no_check_timestamps;
10879
10880 vim_free(command);
10881
10882 /*
10883 * read the names from the file into memory
10884 */
10885# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010886 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 fd = mch_fopen((char *)tempname, "r");
10888# else
10889 fd = mch_fopen((char *)tempname, READBIN);
10890# endif
10891
10892 if (fd == NULL)
10893 {
10894 EMSG2(_(e_notopen), tempname);
10895 goto done;
10896 }
10897
10898 fseek(fd, 0L, SEEK_END);
10899 len = ftell(fd); /* get size of temp file */
10900 fseek(fd, 0L, SEEK_SET);
10901
10902 buffer = alloc(len + 1);
10903 if (buffer != NULL)
10904 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10905 fclose(fd);
10906 mch_remove(tempname);
10907 if (buffer == NULL)
10908 goto done;
10909#ifdef VMS
10910 len = i; /* VMS doesn't give us what we asked for... */
10911#endif
10912 if (i != len)
10913 {
10914 EMSG2(_(e_notread), tempname);
10915 vim_free(buffer);
10916 buffer = NULL;
10917 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010918 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010919 {
10920 /* Change NUL into SOH, otherwise the string is truncated. */
10921 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020010922 if (buffer[i] == NUL)
10923 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010924
Bram Moolenaar162bd912010-07-28 22:29:10 +020010925 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010926 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010927 else
10928 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929
10930done:
10931 vim_free(tempname);
10932 return buffer;
10933}
10934#endif
10935
10936/*
10937 * Free the list of files returned by expand_wildcards() or other expansion
10938 * functions.
10939 */
10940 void
10941FreeWild(count, files)
10942 int count;
10943 char_u **files;
10944{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010945 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 return;
10947#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10948 /*
10949 * Is this still OK for when other functions than expand_wildcards() have
10950 * been used???
10951 */
10952 _fnexplodefree((char **)files);
10953#else
10954 while (count--)
10955 vim_free(files[count]);
10956 vim_free(files);
10957#endif
10958}
10959
10960/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010961 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 * Don't do this when still processing a command or a mapping.
10963 * Don't do this when inside a ":normal" command.
10964 */
10965 int
10966goto_im()
10967{
10968 return (p_im && stuff_empty() && typebuf_typed());
10969}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010970
10971/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020010972 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010973 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
10974 * - Remove any argument. E.g., "csh -f" -> "csh".
10975 * But don't allow a space in the path, so that this works:
10976 * "/usr/bin/csh --rcfile ~/.cshrc"
10977 * But don't do that for Windows, it's common to have a space in the path.
10978 */
10979 char_u *
10980get_isolated_shell_name()
10981{
10982 char_u *p;
10983
10984#ifdef WIN3264
10985 p = gettail(p_sh);
10986 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
10987#else
10988 p = skiptowhite(p_sh);
10989 if (*p == NUL)
10990 {
10991 /* No white space, use the tail. */
10992 p = vim_strsave(gettail(p_sh));
10993 }
10994 else
10995 {
10996 char_u *p1, *p2;
10997
10998 /* Find the last path separator before the space. */
10999 p1 = p_sh;
11000 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11001 if (vim_ispathsep(*p2))
11002 p1 = p2 + 1;
11003 p = vim_strnsave(p1, (int)(p - p1));
11004 }
11005#endif
11006 return p;
11007}