blob: fbd94d94627e308fc8c18167827051a98df7db74 [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/*
6617 * Find the matching '(', failing 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/*
6648 * Return ind_maxparen corrected for the difference in line number between the
6649 * cursor position and "startpos". This makes sure that searching for a
6650 * matching paren above the cursor line doesn't find a match because of
6651 * looking a few lines further.
6652 */
6653 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006654corr_ind_maxparen(startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 pos_T *startpos;
6656{
6657 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6658
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006659 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6660 return curbuf->b_ind_maxparen - (int)n;
6661 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662}
6663
6664/*
6665 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006666 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 */
6668 static int
6669find_last_paren(l, start, end)
6670 char_u *l;
6671 int start, end;
6672{
6673 int i;
6674 int retval = FALSE;
6675 int open_count = 0;
6676
6677 curwin->w_cursor.col = 0; /* default is start of line */
6678
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006679 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {
6681 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6682 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6683 if (l[i] == start)
6684 ++open_count;
6685 else if (l[i] == end)
6686 {
6687 if (open_count > 0)
6688 --open_count;
6689 else
6690 {
6691 curwin->w_cursor.col = i;
6692 retval = TRUE;
6693 }
6694 }
6695 }
6696 return retval;
6697}
6698
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006699/*
6700 * Parse 'cinoptions' and set the values in "curbuf".
6701 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6702 */
6703 void
6704parse_cino(buf)
6705 buf_T *buf;
6706{
6707 char_u *p;
6708 char_u *l;
6709 char_u *digits;
6710 int n;
6711 int divider;
6712 int fraction = 0;
6713 int sw = (int)get_sw_value(buf);
6714
6715 /*
6716 * Set the default values.
6717 */
6718 /* Spaces from a block's opening brace the prevailing indent for that
6719 * block should be. */
6720 buf->b_ind_level = sw;
6721
6722 /* Spaces from the edge of the line an open brace that's at the end of a
6723 * line is imagined to be. */
6724 buf->b_ind_open_imag = 0;
6725
6726 /* Spaces from the prevailing indent for a line that is not preceded by
6727 * an opening brace. */
6728 buf->b_ind_no_brace = 0;
6729
6730 /* Column where the first { of a function should be located }. */
6731 buf->b_ind_first_open = 0;
6732
6733 /* Spaces from the prevailing indent a leftmost open brace should be
6734 * located. */
6735 buf->b_ind_open_extra = 0;
6736
6737 /* Spaces from the matching open brace (real location for one at the left
6738 * edge; imaginary location from one that ends a line) the matching close
6739 * brace should be located. */
6740 buf->b_ind_close_extra = 0;
6741
6742 /* Spaces from the edge of the line an open brace sitting in the leftmost
6743 * column is imagined to be. */
6744 buf->b_ind_open_left_imag = 0;
6745
6746 /* Spaces jump labels should be shifted to the left if N is non-negative,
6747 * otherwise the jump label will be put to column 1. */
6748 buf->b_ind_jump_label = -1;
6749
6750 /* Spaces from the switch() indent a "case xx" label should be located. */
6751 buf->b_ind_case = sw;
6752
6753 /* Spaces from the "case xx:" code after a switch() should be located. */
6754 buf->b_ind_case_code = sw;
6755
6756 /* Lineup break at end of case in switch() with case label. */
6757 buf->b_ind_case_break = 0;
6758
6759 /* Spaces from the class declaration indent a scope declaration label
6760 * should be located. */
6761 buf->b_ind_scopedecl = sw;
6762
6763 /* Spaces from the scope declaration label code should be located. */
6764 buf->b_ind_scopedecl_code = sw;
6765
6766 /* Amount K&R-style parameters should be indented. */
6767 buf->b_ind_param = sw;
6768
6769 /* Amount a function type spec should be indented. */
6770 buf->b_ind_func_type = sw;
6771
6772 /* Amount a cpp base class declaration or constructor initialization
6773 * should be indented. */
6774 buf->b_ind_cpp_baseclass = sw;
6775
6776 /* additional spaces beyond the prevailing indent a continuation line
6777 * should be located. */
6778 buf->b_ind_continuation = sw;
6779
6780 /* Spaces from the indent of the line with an unclosed parentheses. */
6781 buf->b_ind_unclosed = sw * 2;
6782
6783 /* Spaces from the indent of the line with an unclosed parentheses, which
6784 * itself is also unclosed. */
6785 buf->b_ind_unclosed2 = sw;
6786
6787 /* Suppress ignoring spaces from the indent of a line starting with an
6788 * unclosed parentheses. */
6789 buf->b_ind_unclosed_noignore = 0;
6790
6791 /* If the opening paren is the last nonwhite character on the line, and
6792 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6793 * context (for very long lines). */
6794 buf->b_ind_unclosed_wrapped = 0;
6795
6796 /* Suppress ignoring white space when lining up with the character after
6797 * an unclosed parentheses. */
6798 buf->b_ind_unclosed_whiteok = 0;
6799
6800 /* Indent a closing parentheses under the line start of the matching
6801 * opening parentheses. */
6802 buf->b_ind_matching_paren = 0;
6803
6804 /* Indent a closing parentheses under the previous line. */
6805 buf->b_ind_paren_prev = 0;
6806
6807 /* Extra indent for comments. */
6808 buf->b_ind_comment = 0;
6809
6810 /* Spaces from the comment opener when there is nothing after it. */
6811 buf->b_ind_in_comment = 3;
6812
6813 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6814 * after the comment opener. */
6815 buf->b_ind_in_comment2 = 0;
6816
6817 /* Max lines to search for an open paren. */
6818 buf->b_ind_maxparen = 20;
6819
6820 /* Max lines to search for an open comment. */
6821 buf->b_ind_maxcomment = 70;
6822
6823 /* Handle braces for java code. */
6824 buf->b_ind_java = 0;
6825
6826 /* Not to confuse JS object properties with labels. */
6827 buf->b_ind_js = 0;
6828
6829 /* Handle blocked cases correctly. */
6830 buf->b_ind_keep_case_label = 0;
6831
6832 /* Handle C++ namespace. */
6833 buf->b_ind_cpp_namespace = 0;
6834
6835 /* Handle continuation lines containing conditions of if(), for() and
6836 * while(). */
6837 buf->b_ind_if_for_while = 0;
6838
6839 for (p = buf->b_p_cino; *p; )
6840 {
6841 l = p++;
6842 if (*p == '-')
6843 ++p;
6844 digits = p; /* remember where the digits start */
6845 n = getdigits(&p);
6846 divider = 0;
6847 if (*p == '.') /* ".5s" means a fraction */
6848 {
6849 fraction = atol((char *)++p);
6850 while (VIM_ISDIGIT(*p))
6851 {
6852 ++p;
6853 if (divider)
6854 divider *= 10;
6855 else
6856 divider = 10;
6857 }
6858 }
6859 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6860 {
6861 if (p == digits)
6862 n = sw; /* just "s" is one 'shiftwidth' */
6863 else
6864 {
6865 n *= sw;
6866 if (divider)
6867 n += (sw * fraction + divider / 2) / divider;
6868 }
6869 ++p;
6870 }
6871 if (l[1] == '-')
6872 n = -n;
6873
6874 /* When adding an entry here, also update the default 'cinoptions' in
6875 * doc/indent.txt, and add explanation for it! */
6876 switch (*l)
6877 {
6878 case '>': buf->b_ind_level = n; break;
6879 case 'e': buf->b_ind_open_imag = n; break;
6880 case 'n': buf->b_ind_no_brace = n; break;
6881 case 'f': buf->b_ind_first_open = n; break;
6882 case '{': buf->b_ind_open_extra = n; break;
6883 case '}': buf->b_ind_close_extra = n; break;
6884 case '^': buf->b_ind_open_left_imag = n; break;
6885 case 'L': buf->b_ind_jump_label = n; break;
6886 case ':': buf->b_ind_case = n; break;
6887 case '=': buf->b_ind_case_code = n; break;
6888 case 'b': buf->b_ind_case_break = n; break;
6889 case 'p': buf->b_ind_param = n; break;
6890 case 't': buf->b_ind_func_type = n; break;
6891 case '/': buf->b_ind_comment = n; break;
6892 case 'c': buf->b_ind_in_comment = n; break;
6893 case 'C': buf->b_ind_in_comment2 = n; break;
6894 case 'i': buf->b_ind_cpp_baseclass = n; break;
6895 case '+': buf->b_ind_continuation = n; break;
6896 case '(': buf->b_ind_unclosed = n; break;
6897 case 'u': buf->b_ind_unclosed2 = n; break;
6898 case 'U': buf->b_ind_unclosed_noignore = n; break;
6899 case 'W': buf->b_ind_unclosed_wrapped = n; break;
6900 case 'w': buf->b_ind_unclosed_whiteok = n; break;
6901 case 'm': buf->b_ind_matching_paren = n; break;
6902 case 'M': buf->b_ind_paren_prev = n; break;
6903 case ')': buf->b_ind_maxparen = n; break;
6904 case '*': buf->b_ind_maxcomment = n; break;
6905 case 'g': buf->b_ind_scopedecl = n; break;
6906 case 'h': buf->b_ind_scopedecl_code = n; break;
6907 case 'j': buf->b_ind_java = n; break;
6908 case 'J': buf->b_ind_js = n; break;
6909 case 'l': buf->b_ind_keep_case_label = n; break;
6910 case '#': buf->b_ind_hash_comment = n; break;
6911 case 'N': buf->b_ind_cpp_namespace = n; break;
6912 case 'k': buf->b_ind_if_for_while = n; break;
6913 }
6914 if (*p == ',')
6915 ++p;
6916 }
6917}
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 int
6920get_c_indent()
6921{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 pos_T cur_curpos;
6923 int amount;
6924 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006925 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 colnr_T col;
6927 char_u *theline;
6928 char_u *linecopy;
6929 pos_T *trypos;
6930 pos_T *tryposBrace = NULL;
6931 pos_T our_paren_pos;
6932 char_u *start;
6933 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006934#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935#define BRACE_AT_START 2 /* '{' is at start of line */
6936#define BRACE_AT_END 3 /* '{' is at end of line */
6937 linenr_T ourscope;
6938 char_u *l;
6939 char_u *look;
6940 char_u terminated;
6941 int lookfor;
6942#define LOOKFOR_INITIAL 0
6943#define LOOKFOR_IF 1
6944#define LOOKFOR_DO 2
6945#define LOOKFOR_CASE 3
6946#define LOOKFOR_ANY 4
6947#define LOOKFOR_TERM 5
6948#define LOOKFOR_UNTERM 6
6949#define LOOKFOR_SCOPEDECL 7
6950#define LOOKFOR_NOBREAK 8
6951#define LOOKFOR_CPP_BASECLASS 9
6952#define LOOKFOR_ENUM_OR_INIT 10
6953
6954 int whilelevel;
6955 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 int n;
6957 int iscase;
6958 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006959 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006961 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006962 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006964 /* make a copy, value is changed below */
6965 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967 /* remember where the cursor was when we started */
6968 cur_curpos = curwin->w_cursor;
6969
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006970 /* if we are at line 1 0 is fine, right? */
6971 if (cur_curpos.lnum == 1)
6972 return 0;
6973
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 /* Get a copy of the current contents of the line.
6975 * This is required, because only the most recent line obtained with
6976 * ml_get is valid! */
6977 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6978 if (linecopy == NULL)
6979 return 0;
6980
6981 /*
6982 * In insert mode and the cursor is on a ')' truncate the line at the
6983 * cursor position. We don't want to line up with the matching '(' when
6984 * inserting new stuff.
6985 * For unknown reasons the cursor might be past the end of the line, thus
6986 * check for that.
6987 */
6988 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006989 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 && linecopy[curwin->w_cursor.col] == ')')
6991 linecopy[curwin->w_cursor.col] = NUL;
6992
6993 theline = skipwhite(linecopy);
6994
6995 /* move the cursor to the start of the line */
6996
6997 curwin->w_cursor.col = 0;
6998
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006999 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007000
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 /*
7002 * #defines and so on always go at the left when included in 'cinkeys'.
7003 */
7004 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007005 amount = curbuf->b_ind_hash_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
7007 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007008 * Is it a non-case label? Then that goes at the left margin too unless:
7009 * - JS flag is set.
7010 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007012 else if (original_line_islabel && !curbuf->b_ind_js
7013 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {
7015 amount = 0;
7016 }
7017
7018 /*
7019 * If we're inside a "//" comment and there is a "//" comment in a
7020 * previous line, lineup with that one.
7021 */
7022 else if (cin_islinecomment(theline)
7023 && (trypos = find_line_comment()) != NULL) /* XXX */
7024 {
7025 /* find how indented the line beginning the comment is */
7026 getvcol(curwin, trypos, &col, NULL, NULL);
7027 amount = col;
7028 }
7029
7030 /*
7031 * If we're inside a comment and not looking at the start of the
7032 * comment, try using the 'comments' option.
7033 */
7034 else if (!cin_iscomment(theline)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007035 && (trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007036 /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 int lead_start_len = 2;
7039 int lead_middle_len = 1;
7040 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7041 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7042 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7043 char_u *p;
7044 int start_align = 0;
7045 int start_off = 0;
7046 int done = FALSE;
7047
7048 /* find how indented the line beginning the comment is */
7049 getvcol(curwin, trypos, &col, NULL, NULL);
7050 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007051 *lead_start = NUL;
7052 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
7054 p = curbuf->b_p_com;
7055 while (*p != NUL)
7056 {
7057 int align = 0;
7058 int off = 0;
7059 int what = 0;
7060
7061 while (*p != NUL && *p != ':')
7062 {
7063 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7064 what = *p++;
7065 else if (*p == COM_LEFT || *p == COM_RIGHT)
7066 align = *p++;
7067 else if (VIM_ISDIGIT(*p) || *p == '-')
7068 off = getdigits(&p);
7069 else
7070 ++p;
7071 }
7072
7073 if (*p == ':')
7074 ++p;
7075 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7076 if (what == COM_START)
7077 {
7078 STRCPY(lead_start, lead_end);
7079 lead_start_len = (int)STRLEN(lead_start);
7080 start_off = off;
7081 start_align = align;
7082 }
7083 else if (what == COM_MIDDLE)
7084 {
7085 STRCPY(lead_middle, lead_end);
7086 lead_middle_len = (int)STRLEN(lead_middle);
7087 }
7088 else if (what == COM_END)
7089 {
7090 /* If our line starts with the middle comment string, line it
7091 * up with the comment opener per the 'comments' option. */
7092 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7093 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7094 {
7095 done = TRUE;
7096 if (curwin->w_cursor.lnum > 1)
7097 {
7098 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007099 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 * the middle comment string matches in the previous
7101 * line, use the indent of that line. XXX */
7102 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7103 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7104 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7105 else if (STRNCMP(look, lead_middle,
7106 lead_middle_len) == 0)
7107 {
7108 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7109 break;
7110 }
7111 /* If the start comment string doesn't match with the
7112 * start of the comment, skip this entry. XXX */
7113 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7114 lead_start, lead_start_len) != 0)
7115 continue;
7116 }
7117 if (start_off != 0)
7118 amount += start_off;
7119 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007120 amount += vim_strsize(lead_start)
7121 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 break;
7123 }
7124
7125 /* If our line starts with the end comment string, line it up
7126 * with the middle comment */
7127 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7128 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7129 {
7130 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7131 /* XXX */
7132 if (off != 0)
7133 amount += off;
7134 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007135 amount += vim_strsize(lead_start)
7136 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 done = TRUE;
7138 break;
7139 }
7140 }
7141 }
7142
7143 /* If our line starts with an asterisk, line up with the
7144 * asterisk in the comment opener; otherwise, line up
7145 * with the first character of the comment text.
7146 */
7147 if (done)
7148 ;
7149 else if (theline[0] == '*')
7150 amount += 1;
7151 else
7152 {
7153 /*
7154 * If we are more than one line away from the comment opener, take
7155 * the indent of the previous non-empty line. If 'cino' has "CO"
7156 * and we are just below the comment opener and there are any
7157 * white characters after it line up with the text after it;
7158 * otherwise, add the amount specified by "c" in 'cino'
7159 */
7160 amount = -1;
7161 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7162 {
7163 if (linewhite(lnum)) /* skip blank lines */
7164 continue;
7165 amount = get_indent_lnum(lnum); /* XXX */
7166 break;
7167 }
7168 if (amount == -1) /* use the comment opener */
7169 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007170 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {
7172 start = ml_get(trypos->lnum);
7173 look = start + trypos->col + 2; /* skip / and * */
7174 if (*look != NUL) /* if something after it */
7175 trypos->col = (colnr_T)(skipwhite(look) - start);
7176 }
7177 getvcol(curwin, trypos, &col, NULL, NULL);
7178 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007179 if (curbuf->b_ind_in_comment2 || *look == NUL)
7180 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
7182 }
7183 }
7184
7185 /*
7186 * Are we inside parentheses or braces?
7187 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007188 else if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007189 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007190 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 || trypos != NULL)
7192 {
7193 if (trypos != NULL && tryposBrace != NULL)
7194 {
7195 /* Both an unmatched '(' and '{' is found. Use the one which is
7196 * closer to the current cursor position, set the other to NULL. */
7197 if (trypos->lnum != tryposBrace->lnum
7198 ? trypos->lnum < tryposBrace->lnum
7199 : trypos->col < tryposBrace->col)
7200 trypos = NULL;
7201 else
7202 tryposBrace = NULL;
7203 }
7204
7205 if (trypos != NULL)
7206 {
7207 /*
7208 * If the matching paren is more than one line away, use the indent of
7209 * a previous non-empty line that matches the same paren.
7210 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007211 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007213 /* Line up with the start of the matching paren line. */
7214 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7215 }
7216 else
7217 {
7218 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007219 our_paren_pos = *trypos;
7220 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007222 l = skipwhite(ml_get(lnum));
7223 if (cin_nocode(l)) /* skip comment lines */
7224 continue;
7225 if (cin_ispreproc_cont(&l, &lnum))
7226 continue; /* ignore #define, #if, etc. */
7227 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007229 /* Skip a comment. XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007230 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007231 {
7232 lnum = trypos->lnum + 1;
7233 continue;
7234 }
7235
7236 /* XXX */
7237 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007238 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007239 && trypos->lnum == our_paren_pos.lnum
7240 && trypos->col == our_paren_pos.col)
7241 {
7242 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007244 if (theline[0] == ')')
7245 {
7246 if (our_paren_pos.lnum != lnum
7247 && cur_amount > amount)
7248 cur_amount = amount;
7249 amount = -1;
7250 }
7251 break;
7252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254 }
7255
7256 /*
7257 * Line up with line where the matching paren is. XXX
7258 * If the line starts with a '(' or the indent for unclosed
7259 * parentheses is zero, line up with the unclosed parentheses.
7260 */
7261 if (amount == -1)
7262 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007263 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007264 int is_if_for_while = 0;
7265
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007266 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007267 {
7268 /* Look for the outermost opening parenthesis on this line
7269 * and check whether it belongs to an "if", "for" or "while". */
7270
7271 pos_T cursor_save = curwin->w_cursor;
7272 pos_T outermost;
7273 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007274
7275 trypos = &our_paren_pos;
7276 do {
7277 outermost = *trypos;
7278 curwin->w_cursor.lnum = outermost.lnum;
7279 curwin->w_cursor.col = outermost.col;
7280
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007281 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007282 } while (trypos && trypos->lnum == outermost.lnum);
7283
7284 curwin->w_cursor = cursor_save;
7285
7286 line = ml_get(outermost.lnum);
7287
7288 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007289 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007290 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007291
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007292 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007293 look = skipwhite(look);
7294 if (*look == '(')
7295 {
7296 linenr_T save_lnum = curwin->w_cursor.lnum;
7297 char_u *line;
7298 int look_col;
7299
7300 /* Ignore a '(' in front of the line that has a match before
7301 * our matching '('. */
7302 curwin->w_cursor.lnum = our_paren_pos.lnum;
7303 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007304 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007305 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007306 if ((trypos = findmatchlimit(NULL, ')', 0,
7307 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007308 != NULL
7309 && trypos->lnum == our_paren_pos.lnum
7310 && trypos->col < our_paren_pos.col)
7311 ignore_paren_col = trypos->col + 1;
7312
7313 curwin->w_cursor.lnum = save_lnum;
7314 look = ml_get(our_paren_pos.lnum) + look_col;
7315 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007316 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7317 && is_if_for_while == 0)
7318 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007319 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 {
7321 /*
7322 * If we're looking at a close paren, line up right there;
7323 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007324 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 * the last nonwhite character of the line, use either the
7326 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007327 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 * lines).
7329 */
7330 if (theline[0] != ')')
7331 {
7332 cur_amount = MAXCOL;
7333 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007334 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 && cin_ends_in(l, (char_u *)"(", NULL))
7336 {
7337 /* look for opening unmatched paren, indent one level
7338 * for each additional level */
7339 n = 1;
7340 for (col = 0; col < our_paren_pos.col; ++col)
7341 {
7342 switch (l[col])
7343 {
7344 case '(':
7345 case '{': ++n;
7346 break;
7347
7348 case ')':
7349 case '}': if (n > 1)
7350 --n;
7351 break;
7352 }
7353 }
7354
7355 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007356 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007358 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 our_paren_pos.col++;
7360 else
7361 {
7362 col = our_paren_pos.col + 1;
7363 while (vim_iswhite(l[col]))
7364 col++;
7365 if (l[col] != NUL) /* In case of trailing space */
7366 our_paren_pos.col = col;
7367 else
7368 our_paren_pos.col++;
7369 }
7370 }
7371
7372 /*
7373 * Find how indented the paren is, or the character after it
7374 * if we did the above "if".
7375 */
7376 if (our_paren_pos.col > 0)
7377 {
7378 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7379 if (cur_amount > (int)col)
7380 cur_amount = col;
7381 }
7382 }
7383
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007384 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
7386 /* Line up with the start of the matching paren line. */
7387 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007388 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7389 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007390 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 {
7392 if (cur_amount != MAXCOL)
7393 amount = cur_amount;
7394 }
7395 else
7396 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007397 /* Add b_ind_unclosed2 for each '(' before our matching one,
7398 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007400 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 {
7402 --our_paren_pos.col;
7403 switch (*ml_get_pos(&our_paren_pos))
7404 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007405 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 col = our_paren_pos.col;
7407 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007408 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 col = MAXCOL;
7410 break;
7411 }
7412 }
7413
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007414 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 * braces */
7416 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007417 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 else
7419 {
7420 curwin->w_cursor.lnum = our_paren_pos.lnum;
7421 curwin->w_cursor.col = col;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007422 if (find_match_paren(curbuf->b_ind_maxparen) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007423 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007425 {
7426 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007427 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007428 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007429 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 /*
7433 * For a line starting with ')' use the minimum of the two
7434 * positions, to avoid giving it more indent than the previous
7435 * lines:
7436 * func_long_name( if (x
7437 * arg && yy
7438 * ) ^ not here ) ^ not here
7439 */
7440 if (cur_amount < amount)
7441 amount = cur_amount;
7442 }
7443 }
7444
7445 /* add extra indent for a comment */
7446 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007447 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
7449
7450 /*
7451 * Are we at least inside braces, then?
7452 */
7453 else
7454 {
7455 trypos = tryposBrace;
7456
7457 ourscope = trypos->lnum;
7458 start = ml_get(ourscope);
7459
7460 /*
7461 * Now figure out how indented the line is in general.
7462 * If the brace was at the start of the line, we use that;
7463 * otherwise, check out the indentation of the line as
7464 * a whole and then add the "imaginary indent" to that.
7465 */
7466 look = skipwhite(start);
7467 if (*look == '{')
7468 {
7469 getvcol(curwin, trypos, &col, NULL, NULL);
7470 amount = col;
7471 if (*start == '{')
7472 start_brace = BRACE_IN_COL0;
7473 else
7474 start_brace = BRACE_AT_START;
7475 }
7476 else
7477 {
7478 /*
7479 * that opening brace might have been on a continuation
7480 * line. if so, find the start of the line.
7481 */
7482 curwin->w_cursor.lnum = ourscope;
7483
7484 /*
7485 * position the cursor over the rightmost paren, so that
7486 * matching it will take us back to the start of the line.
7487 */
7488 lnum = ourscope;
7489 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007490 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7491 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 lnum = trypos->lnum;
7493
7494 /*
7495 * It could have been something like
7496 * case 1: if (asdf &&
7497 * ldfd) {
7498 * }
7499 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007500 if (curbuf->b_ind_js || (curbuf->b_ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007501 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 amount = get_indent();
7503 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007504 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
7506 start_brace = BRACE_AT_END;
7507 }
7508
7509 /*
7510 * if we're looking at a closing brace, that's where
7511 * we want to be. otherwise, add the amount of room
7512 * that an indent is supposed to be.
7513 */
7514 if (theline[0] == '}')
7515 {
7516 /*
7517 * they may want closing braces to line up with something
7518 * other than the open brace. indulge them, if so.
7519 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007520 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522 else
7523 {
7524 /*
7525 * If we're looking at an "else", try to find an "if"
7526 * to match it with.
7527 * If we're looking at a "while", try to find a "do"
7528 * to match it with.
7529 */
7530 lookfor = LOOKFOR_INITIAL;
7531 if (cin_iselse(theline))
7532 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007533 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 lookfor = LOOKFOR_DO;
7535 if (lookfor != LOOKFOR_INITIAL)
7536 {
7537 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007538 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {
7540 amount = get_indent(); /* XXX */
7541 goto theend;
7542 }
7543 }
7544
7545 /*
7546 * We get here if we are not on an "while-of-do" or "else" (or
7547 * failed to find a matching "if").
7548 * Search backwards for something to line up with.
7549 * First set amount for when we don't find anything.
7550 */
7551
7552 /*
7553 * if the '{' is _really_ at the left margin, use the imaginary
7554 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007555 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 */
7557
7558 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7559 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007560 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007561 lookfor_cpp_namespace = TRUE;
7562 }
7563 else if (start_brace == BRACE_AT_START &&
7564 lookfor_cpp_namespace) /* '{' is at start */
7565 {
7566
7567 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 }
7569 else
7570 {
7571 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007572 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007573 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007574
7575 l = skipwhite(ml_get_curline());
7576 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007577 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 else
7580 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007581 /* Compensate for adding b_ind_open_extra later. */
7582 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (amount < 0)
7584 amount = 0;
7585 }
7586 }
7587
7588 lookfor_break = FALSE;
7589
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007590 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
7592 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007593 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 }
7595 else if (cin_isscopedecl(theline)) /* private:, ... */
7596 {
7597 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007598 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
7600 else
7601 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007602 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7603 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 lookfor_break = TRUE;
7605
7606 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007607 /* b_ind_level from start of block */
7608 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
7610 scope_amount = amount;
7611 whilelevel = 0;
7612
7613 /*
7614 * Search backwards. If we find something we recognize, line up
7615 * with that.
7616 *
7617 * if we're looking at an open brace, indent
7618 * the usual amount relative to the conditional
7619 * that opens the block.
7620 */
7621 curwin->w_cursor = cur_curpos;
7622 for (;;)
7623 {
7624 curwin->w_cursor.lnum--;
7625 curwin->w_cursor.col = 0;
7626
7627 /*
7628 * If we went all the way back to the start of our scope, line
7629 * up with it.
7630 */
7631 if (curwin->w_cursor.lnum <= ourscope)
7632 {
7633 /* we reached end of scope:
7634 * if looking for a enum or structure initialization
7635 * go further back:
7636 * if it is an initializer (enum xxx or xxx =), then
7637 * don't add ind_continuation, otherwise it is a variable
7638 * declaration:
7639 * int x,
7640 * here; <-- add ind_continuation
7641 */
7642 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7643 {
7644 if (curwin->w_cursor.lnum == 0
7645 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007646 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007648 /* nothing found (abuse curbuf->b_ind_maxparen as
7649 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 * initialization) */
7651 if (cont_amount > 0)
7652 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007653 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 amount += ind_continuation;
7655 break;
7656 }
7657
7658 l = ml_get_curline();
7659
7660 /*
7661 * If we're in a comment now, skip to the start of the
7662 * comment.
7663 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007664 trypos = ind_find_start_comment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 if (trypos != NULL)
7666 {
7667 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007668 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 continue;
7670 }
7671
7672 /*
7673 * Skip preprocessor directives and blank lines.
7674 */
7675 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7676 continue;
7677
7678 if (cin_nocode(l))
7679 continue;
7680
7681 terminated = cin_isterminated(l, FALSE, TRUE);
7682
7683 /*
7684 * If we are at top level and the line looks like a
7685 * function declaration, we are done
7686 * (it's a variable declaration).
7687 */
7688 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007689 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {
7691 /* if the line is terminated with another ','
7692 * it is a continued variable initialization.
7693 * don't add extra indent.
7694 * TODO: does not work, if a function
7695 * declaration is split over multiple lines:
7696 * cin_isfuncdecl returns FALSE then.
7697 */
7698 if (terminated == ',')
7699 break;
7700
7701 /* if it es a enum declaration or an assignment,
7702 * we are done.
7703 */
7704 if (terminated != ';' && cin_isinit())
7705 break;
7706
7707 /* nothing useful found */
7708 if (terminated == 0 || terminated == '{')
7709 continue;
7710 }
7711
7712 if (terminated != ';')
7713 {
7714 /* Skip parens and braces. Position the cursor
7715 * over the rightmost paren, so that matching it
7716 * will take us back to the start of the line.
7717 */ /* XXX */
7718 trypos = NULL;
7719 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007720 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007721 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722
7723 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007724 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
7726 if (trypos != NULL)
7727 {
7728 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007729 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 continue;
7731 }
7732 }
7733
7734 /* it's a variable declaration, add indentation
7735 * like in
7736 * int a,
7737 * b;
7738 */
7739 if (cont_amount > 0)
7740 amount = cont_amount;
7741 else
7742 amount += ind_continuation;
7743 }
7744 else if (lookfor == LOOKFOR_UNTERM)
7745 {
7746 if (cont_amount > 0)
7747 amount = cont_amount;
7748 else
7749 amount += ind_continuation;
7750 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007751 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007752 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007753 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007755 {
7756 amount = scope_amount;
7757 if (theline[0] == '{')
7758 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007759 amount += curbuf->b_ind_open_extra;
7760 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007761 }
7762 }
7763
7764 if (lookfor_cpp_namespace)
7765 {
7766 /*
7767 * Looking for C++ namespace, need to look further
7768 * back.
7769 */
7770 if (curwin->w_cursor.lnum == ourscope)
7771 continue;
7772
7773 if (curwin->w_cursor.lnum == 0
7774 || curwin->w_cursor.lnum
7775 < ourscope - FIND_NAMESPACE_LIM)
7776 break;
7777
7778 l = ml_get_curline();
7779
7780 /* If we're in a comment now, skip to the start of
7781 * the comment. */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007782 trypos = ind_find_start_comment();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007783 if (trypos != NULL)
7784 {
7785 curwin->w_cursor.lnum = trypos->lnum + 1;
7786 curwin->w_cursor.col = 0;
7787 continue;
7788 }
7789
7790 /* Skip preprocessor directives and blank lines. */
7791 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7792 continue;
7793
7794 /* Finally the actual check for "namespace". */
7795 if (cin_is_cpp_namespace(l))
7796 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007797 amount += curbuf->b_ind_cpp_namespace
7798 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007799 break;
7800 }
7801
7802 if (cin_nocode(l))
7803 continue;
7804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 }
7806 break;
7807 }
7808
7809 /*
7810 * If we're in a comment now, skip to the start of the comment.
7811 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007812 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {
7814 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007815 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 continue;
7817 }
7818
7819 l = ml_get_curline();
7820
7821 /*
7822 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007823 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007825 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (iscase || cin_isscopedecl(l))
7827 {
7828 /* we are only looking for cpp base class
7829 * declaration/initialization any longer */
7830 if (lookfor == LOOKFOR_CPP_BASECLASS)
7831 break;
7832
7833 /* When looking for a "do" we are not interested in
7834 * labels. */
7835 if (whilelevel > 0)
7836 continue;
7837
7838 /*
7839 * case xx:
7840 * c = 99 + <- this indent plus continuation
7841 *-> here;
7842 */
7843 if (lookfor == LOOKFOR_UNTERM
7844 || lookfor == LOOKFOR_ENUM_OR_INIT)
7845 {
7846 if (cont_amount > 0)
7847 amount = cont_amount;
7848 else
7849 amount += ind_continuation;
7850 break;
7851 }
7852
7853 /*
7854 * case xx: <- line up with this case
7855 * x = 333;
7856 * case yy:
7857 */
7858 if ( (iscase && lookfor == LOOKFOR_CASE)
7859 || (iscase && lookfor_break)
7860 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7861 {
7862 /*
7863 * Check that this case label is not for another
7864 * switch()
7865 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007866 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007867 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {
7869 amount = get_indent(); /* XXX */
7870 break;
7871 }
7872 continue;
7873 }
7874
7875 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7876
7877 /*
7878 * case xx: if (cond) <- line up with this if
7879 * y = y + 1;
7880 * -> s = 99;
7881 *
7882 * case xx:
7883 * if (cond) <- line up with this line
7884 * y = y + 1;
7885 * -> s = 99;
7886 */
7887 if (lookfor == LOOKFOR_TERM)
7888 {
7889 if (n)
7890 amount = n;
7891
7892 if (!lookfor_break)
7893 break;
7894 }
7895
7896 /*
7897 * case xx: x = x + 1; <- line up with this x
7898 * -> y = y + 1;
7899 *
7900 * case xx: if (cond) <- line up with this if
7901 * -> y = y + 1;
7902 */
7903 if (n)
7904 {
7905 amount = n;
7906 l = after_label(ml_get_curline());
7907 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007908 {
7909 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007910 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007911 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007912 amount += curbuf->b_ind_level
7913 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 break;
7916 }
7917
7918 /*
7919 * Try to get the indent of a statement before the switch
7920 * label. If nothing is found, line up relative to the
7921 * switch label.
7922 * break; <- may line up with this line
7923 * case xx:
7924 * -> y = 1;
7925 */
7926 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007927 ? curbuf->b_ind_case_code
7928 : curbuf->b_ind_scopedecl_code);
7929 lookfor = curbuf->b_ind_case_break
7930 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 continue;
7932 }
7933
7934 /*
7935 * Looking for a switch() label or C++ scope declaration,
7936 * ignore other lines, skip {}-blocks.
7937 */
7938 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7939 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007940 if (find_last_paren(l, '{', '}')
7941 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007944 curwin->w_cursor.col = 0;
7945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 continue;
7947 }
7948
7949 /*
7950 * Ignore jump labels with nothing after them.
7951 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007952 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {
7954 l = after_label(ml_get_curline());
7955 if (l == NULL || cin_nocode(l))
7956 continue;
7957 }
7958
7959 /*
7960 * Ignore #defines, #if, etc.
7961 * Ignore comment and empty lines.
7962 * (need to get the line again, cin_islabel() may have
7963 * unlocked it)
7964 */
7965 l = ml_get_curline();
7966 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7967 || cin_nocode(l))
7968 continue;
7969
7970 /*
7971 * Are we at the start of a cpp base class declaration or
7972 * constructor initialization?
7973 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007974 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007975 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00007976 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007977 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007978 l = ml_get_curline();
7979 }
7980 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
7982 if (lookfor == LOOKFOR_UNTERM)
7983 {
7984 if (cont_amount > 0)
7985 amount = cont_amount;
7986 else
7987 amount += ind_continuation;
7988 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007989 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007991 /* Need to find start of the declaration. */
7992 lookfor = LOOKFOR_UNTERM;
7993 ind_continuation = 0;
7994 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 }
7996 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007997 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007998 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 break;
8000 }
8001 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8002 {
8003 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008004 * declaration or initialization before the opening brace.
8005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if (cin_isterminated(l, TRUE, FALSE))
8007 break;
8008 else
8009 continue;
8010 }
8011
8012 /*
8013 * What happens next depends on the line being terminated.
8014 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008015 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 * 123,
8017 * sizeof
8018 * here
8019 * Otherwise check whether it is a enumeration or structure
8020 * initialisation (not indented) or a variable declaration
8021 * (indented).
8022 */
8023 terminated = cin_isterminated(l, FALSE, TRUE);
8024
8025 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8026 && terminated == ','))
8027 {
8028 /*
8029 * if we're in the middle of a paren thing,
8030 * go back to the line that starts it so
8031 * we can get the right prevailing indent
8032 * if ( foo &&
8033 * bar )
8034 */
8035 /*
8036 * position the cursor over the rightmost paren, so that
8037 * matching it will take us back to the start of the line.
8038 */
8039 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008040 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041
8042 /*
8043 * If we are looking for ',', we also look for matching
8044 * braces.
8045 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008046 if (trypos == NULL && terminated == ','
8047 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008048 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
8050 if (trypos != NULL)
8051 {
8052 /*
8053 * Check if we are on a case label now. This is
8054 * handled above.
8055 * case xx: if ( asdf &&
8056 * asdf)
8057 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008058 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008060 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {
8062 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008063 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 continue;
8065 }
8066 }
8067
8068 /*
8069 * Skip over continuation lines to find the one to get the
8070 * indent from
8071 * char *usethis = "bla\
8072 * bla",
8073 * here;
8074 */
8075 if (terminated == ',')
8076 {
8077 while (curwin->w_cursor.lnum > 1)
8078 {
8079 l = ml_get(curwin->w_cursor.lnum - 1);
8080 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8081 break;
8082 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008083 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 }
8085 }
8086
8087 /*
8088 * Get indent and pointer to text for current line,
8089 * ignoring any jump label. XXX
8090 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008091 if (!curbuf->b_ind_js)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008092 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008093 else
8094 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 /*
8096 * If this is just above the line we are indenting, and it
8097 * starts with a '{', line it up with this line.
8098 * while (not)
8099 * -> {
8100 * }
8101 */
8102 if (terminated != ',' && lookfor != LOOKFOR_TERM
8103 && theline[0] == '{')
8104 {
8105 amount = cur_amount;
8106 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008107 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 * doesn't start with a '{', which must have a match
8109 * in the same line (scope is the same). Probably:
8110 * { 1, 2 },
8111 * -> { 3, 4 }
8112 */
8113 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008114 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008116 if (curbuf->b_ind_cpp_baseclass)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {
8118 /* have to look back, whether it is a cpp base
8119 * class declaration or initialization */
8120 lookfor = LOOKFOR_CPP_BASECLASS;
8121 continue;
8122 }
8123 break;
8124 }
8125
8126 /*
8127 * Check if we are after an "if", "while", etc.
8128 * Also allow " } else".
8129 */
8130 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8131 {
8132 /*
8133 * Found an unterminated line after an if (), line up
8134 * with the last one.
8135 * if (cond)
8136 * 100 +
8137 * -> here;
8138 */
8139 if (lookfor == LOOKFOR_UNTERM
8140 || lookfor == LOOKFOR_ENUM_OR_INIT)
8141 {
8142 if (cont_amount > 0)
8143 amount = cont_amount;
8144 else
8145 amount += ind_continuation;
8146 break;
8147 }
8148
8149 /*
8150 * If this is just above the line we are indenting, we
8151 * are finished.
8152 * while (not)
8153 * -> here;
8154 * Otherwise this indent can be used when the line
8155 * before this is terminated.
8156 * yyy;
8157 * if (stat)
8158 * while (not)
8159 * xxx;
8160 * -> here;
8161 */
8162 amount = cur_amount;
8163 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008164 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 if (lookfor != LOOKFOR_TERM)
8166 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008167 amount += curbuf->b_ind_level
8168 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 break;
8170 }
8171
8172 /*
8173 * Special trick: when expecting the while () after a
8174 * do, line up with the while()
8175 * do
8176 * x = 1;
8177 * -> here
8178 */
8179 l = skipwhite(ml_get_curline());
8180 if (cin_isdo(l))
8181 {
8182 if (whilelevel == 0)
8183 break;
8184 --whilelevel;
8185 }
8186
8187 /*
8188 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008189 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 * Need to use the scope of this "else". XXX
8191 * If whilelevel != 0 continue looking for a "do {".
8192 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008193 if (cin_iselse(l) && whilelevel == 0)
8194 {
8195 /* If we're looking at "} else", let's make sure we
8196 * find the opening brace of the enclosing scope,
8197 * not the one from "if () {". */
8198 if (*l == '}')
8199 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008200 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008201
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008202 if ((trypos = find_start_brace()) == NULL
8203 || find_match(LOOKFOR_IF, trypos->lnum)
8204 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008205 break;
8206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 }
8208
8209 /*
8210 * If we're below an unterminated line that is not an
8211 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008212 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 * the line before this one.
8214 */
8215 else
8216 {
8217 /*
8218 * Found two unterminated lines on a row, line up with
8219 * the last one.
8220 * c = 99 +
8221 * 100 +
8222 * -> here;
8223 */
8224 if (lookfor == LOOKFOR_UNTERM)
8225 {
8226 /* When line ends in a comma add extra indent */
8227 if (terminated == ',')
8228 amount += ind_continuation;
8229 break;
8230 }
8231
8232 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8233 {
8234 /* Found two lines ending in ',', lineup with the
8235 * lowest one, but check for cpp base class
8236 * declaration/initialization, if it is an
8237 * opening brace or we are looking just for
8238 * enumerations/initializations. */
8239 if (terminated == ',')
8240 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008241 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 break;
8243
8244 lookfor = LOOKFOR_CPP_BASECLASS;
8245 continue;
8246 }
8247
8248 /* Ignore unterminated lines in between, but
8249 * reduce indent. */
8250 if (amount > cur_amount)
8251 amount = cur_amount;
8252 }
8253 else
8254 {
8255 /*
8256 * Found first unterminated line on a row, may
8257 * line up with this line, remember its indent
8258 * 100 +
8259 * -> here;
8260 */
8261 amount = cur_amount;
8262
8263 /*
8264 * If previous line ends in ',', check whether we
8265 * are in an initialization or enum
8266 * struct xxx =
8267 * {
8268 * sizeof a,
8269 * 124 };
8270 * or a normal possible continuation line.
8271 * but only, of no other statement has been found
8272 * yet.
8273 */
8274 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8275 {
8276 lookfor = LOOKFOR_ENUM_OR_INIT;
8277 cont_amount = cin_first_id_amount();
8278 }
8279 else
8280 {
8281 if (lookfor == LOOKFOR_INITIAL
8282 && *l != NUL
8283 && l[STRLEN(l) - 1] == '\\')
8284 /* XXX */
8285 cont_amount = cin_get_equal_amount(
8286 curwin->w_cursor.lnum);
8287 if (lookfor != LOOKFOR_TERM)
8288 lookfor = LOOKFOR_UNTERM;
8289 }
8290 }
8291 }
8292 }
8293
8294 /*
8295 * Check if we are after a while (cond);
8296 * If so: Ignore until the matching "do".
8297 */
8298 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008299 else if (cin_iswhileofdo_end(terminated))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {
8301 /*
8302 * Found an unterminated line after a while ();, line up
8303 * with the last one.
8304 * while (cond);
8305 * 100 + <- line up with this one
8306 * -> here;
8307 */
8308 if (lookfor == LOOKFOR_UNTERM
8309 || lookfor == LOOKFOR_ENUM_OR_INIT)
8310 {
8311 if (cont_amount > 0)
8312 amount = cont_amount;
8313 else
8314 amount += ind_continuation;
8315 break;
8316 }
8317
8318 if (whilelevel == 0)
8319 {
8320 lookfor = LOOKFOR_TERM;
8321 amount = get_indent(); /* XXX */
8322 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008323 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325 ++whilelevel;
8326 }
8327
8328 /*
8329 * We are after a "normal" statement.
8330 * If we had another statement we can stop now and use the
8331 * indent of that other statement.
8332 * Otherwise the indent of the current statement may be used,
8333 * search backwards for the next "normal" statement.
8334 */
8335 else
8336 {
8337 /*
8338 * Skip single break line, if before a switch label. It
8339 * may be lined up with the case label.
8340 */
8341 if (lookfor == LOOKFOR_NOBREAK
8342 && cin_isbreak(skipwhite(ml_get_curline())))
8343 {
8344 lookfor = LOOKFOR_ANY;
8345 continue;
8346 }
8347
8348 /*
8349 * Handle "do {" line.
8350 */
8351 if (whilelevel > 0)
8352 {
8353 l = cin_skipcomment(ml_get_curline());
8354 if (cin_isdo(l))
8355 {
8356 amount = get_indent(); /* XXX */
8357 --whilelevel;
8358 continue;
8359 }
8360 }
8361
8362 /*
8363 * Found a terminated line above an unterminated line. Add
8364 * the amount for a continuation line.
8365 * x = 1;
8366 * y = foo +
8367 * -> here;
8368 * or
8369 * int x = 1;
8370 * int foo,
8371 * -> here;
8372 */
8373 if (lookfor == LOOKFOR_UNTERM
8374 || lookfor == LOOKFOR_ENUM_OR_INIT)
8375 {
8376 if (cont_amount > 0)
8377 amount = cont_amount;
8378 else
8379 amount += ind_continuation;
8380 break;
8381 }
8382
8383 /*
8384 * Found a terminated line above a terminated line or "if"
8385 * etc. line. Use the amount of the line below us.
8386 * x = 1; x = 1;
8387 * if (asdf) y = 2;
8388 * while (asdf) ->here;
8389 * here;
8390 * ->foo;
8391 */
8392 if (lookfor == LOOKFOR_TERM)
8393 {
8394 if (!lookfor_break && whilelevel == 0)
8395 break;
8396 }
8397
8398 /*
8399 * First line above the one we're indenting is terminated.
8400 * To know what needs to be done look further backward for
8401 * a terminated line.
8402 */
8403 else
8404 {
8405 /*
8406 * position the cursor over the rightmost paren, so
8407 * that matching it will take us back to the start of
8408 * the line. Helps for:
8409 * func(asdr,
8410 * asdfasdf);
8411 * here;
8412 */
8413term_again:
8414 l = ml_get_curline();
8415 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008416 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008417 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {
8419 /*
8420 * Check if we are on a case label now. This is
8421 * handled above.
8422 * case xx: if ( asdf &&
8423 * asdf)
8424 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008425 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008427 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {
8429 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008430 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 continue;
8432 }
8433 }
8434
8435 /* When aligning with the case statement, don't align
8436 * with a statement after it.
8437 * case 1: { <-- don't use this { position
8438 * stat;
8439 * }
8440 * case 2:
8441 * stat;
8442 * }
8443 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008444 iscase = (curbuf->b_ind_keep_case_label
8445 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
8447 /*
8448 * Get indent and pointer to text for current line,
8449 * ignoring any jump label.
8450 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008451 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452
8453 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008454 amount += curbuf->b_ind_open_extra;
8455 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008456 l = skipwhite(l);
8457 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008458 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8460
8461 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008462 * When a terminated line starts with "else" skip to
8463 * the matching "if":
8464 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008465 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008466 * Need to use the scope of this "else". XXX
8467 * If whilelevel != 0 continue looking for a "do {".
8468 */
8469 if (lookfor == LOOKFOR_TERM
8470 && *l != '}'
8471 && cin_iselse(l)
8472 && whilelevel == 0)
8473 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008474 if ((trypos = find_start_brace()) == NULL
8475 || find_match(LOOKFOR_IF, trypos->lnum)
8476 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008477 break;
8478 continue;
8479 }
8480
8481 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 * If we're at the end of a block, skip to the start of
8483 * that block.
8484 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008485 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008486 if (find_last_paren(l, '{', '}') /* XXX */
8487 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008489 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 /* if not "else {" check for terminated again */
8491 /* but skip block for "} else {" */
8492 l = cin_skipcomment(ml_get_curline());
8493 if (*l == '}' || !cin_iselse(l))
8494 goto term_again;
8495 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008496 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498 }
8499 }
8500 }
8501 }
8502 }
8503
8504 /* add extra indent for a comment */
8505 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008506 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008507
8508 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008509 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8510 amount -= curbuf->b_ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 }
8512
8513 /*
8514 * ok -- we're not inside any sort of structure at all!
8515 *
8516 * this means we're at the top level, and everything should
8517 * basically just match where the previous line is, except
8518 * for the lines immediately following a function declaration,
8519 * which are K&R-style parameters and need to be indented.
8520 */
8521 else
8522 {
8523 /*
8524 * if our line starts with an open brace, forget about any
8525 * prevailing indent and make sure it looks like the start
8526 * of a function
8527 */
8528
8529 if (theline[0] == '{')
8530 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008531 amount = curbuf->b_ind_first_open;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 }
8533
8534 /*
8535 * If the NEXT line is a function declaration, the current
8536 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008537 * Don't do this if the current line looks like a comment or if the
8538 * current line is terminated, ie. ends in ';', or if the current line
8539 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 */
8541 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8542 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008543 && vim_strchr(theline, '{') == NULL
8544 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 && !cin_ends_in(theline, (char_u *)":", NULL)
8546 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008547 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008548 cur_curpos.lnum + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 && !cin_isterminated(theline, FALSE, TRUE))
8550 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008551 amount = curbuf->b_ind_func_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 }
8553 else
8554 {
8555 amount = 0;
8556 curwin->w_cursor = cur_curpos;
8557
8558 /* search backwards until we find something we recognize */
8559
8560 while (curwin->w_cursor.lnum > 1)
8561 {
8562 curwin->w_cursor.lnum--;
8563 curwin->w_cursor.col = 0;
8564
8565 l = ml_get_curline();
8566
8567 /*
8568 * If we're in a comment now, skip to the start of the comment.
8569 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008570 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {
8572 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008573 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 continue;
8575 }
8576
8577 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008578 * Are we at the start of a cpp base class declaration or
8579 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008581 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008582 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar18144c82006-04-12 21:52:12 +00008583 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008584 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008585 l = ml_get_curline();
8586 }
8587 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008589 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008590 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 break;
8592 }
8593
8594 /*
8595 * Skip preprocessor directives and blank lines.
8596 */
8597 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8598 continue;
8599
8600 if (cin_nocode(l))
8601 continue;
8602
8603 /*
8604 * If the previous line ends in ',', use one level of
8605 * indentation:
8606 * int foo,
8607 * bar;
8608 * do this before checking for '}' in case of eg.
8609 * enum foobar
8610 * {
8611 * ...
8612 * } foo,
8613 * bar;
8614 */
8615 n = 0;
8616 if (cin_ends_in(l, (char_u *)",", NULL)
8617 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8618 {
8619 /* take us back to opening paren */
8620 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008621 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008622 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008623 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624
8625 /* For a line ending in ',' that is a continuation line go
8626 * back to the first line with a backslash:
8627 * char *foo = "bla\
8628 * bla",
8629 * here;
8630 */
8631 while (n == 0 && curwin->w_cursor.lnum > 1)
8632 {
8633 l = ml_get(curwin->w_cursor.lnum - 1);
8634 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8635 break;
8636 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008637 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 }
8639
8640 amount = get_indent(); /* XXX */
8641
8642 if (amount == 0)
8643 amount = cin_first_id_amount();
8644 if (amount == 0)
8645 amount = ind_continuation;
8646 break;
8647 }
8648
8649 /*
8650 * If the line looks like a function declaration, and we're
8651 * not in a comment, put it the left margin.
8652 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008653 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 break;
8655 l = ml_get_curline();
8656
8657 /*
8658 * Finding the closing '}' of a previous function. Put
8659 * current line at the left margin. For when 'cino' has "fs".
8660 */
8661 if (*skipwhite(l) == '}')
8662 break;
8663
8664 /* (matching {)
8665 * If the previous line ends on '};' (maybe followed by
8666 * comments) align at column 0. For example:
8667 * char *string_array[] = { "foo",
8668 * / * x * / "b};ar" }; / * foobar * /
8669 */
8670 if (cin_ends_in(l, (char_u *)"};", NULL))
8671 break;
8672
8673 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008674 * Find a line only has a semicolon that belongs to a previous
8675 * line ending in '}', e.g. before an #endif. Don't increase
8676 * indent then.
8677 */
8678 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8679 {
8680 pos_T curpos_save = curwin->w_cursor;
8681
8682 while (curwin->w_cursor.lnum > 1)
8683 {
8684 look = ml_get(--curwin->w_cursor.lnum);
8685 if (!(cin_nocode(look) || cin_ispreproc_cont(
8686 &look, &curwin->w_cursor.lnum)))
8687 break;
8688 }
8689 if (curwin->w_cursor.lnum > 0
8690 && cin_ends_in(look, (char_u *)"}", NULL))
8691 break;
8692
8693 curwin->w_cursor = curpos_save;
8694 }
8695
8696 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 * If the PREVIOUS line is a function declaration, the current
8698 * line (and the ones that follow) needs to be indented as
8699 * parameters.
8700 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008701 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008703 amount = curbuf->b_ind_param;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 break;
8705 }
8706
8707 /*
8708 * If the previous line ends in ';' and the line before the
8709 * previous line ends in ',' or '\', ident to column zero:
8710 * int foo,
8711 * bar;
8712 * indent_to_0 here;
8713 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008714 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 {
8716 l = ml_get(curwin->w_cursor.lnum - 1);
8717 if (cin_ends_in(l, (char_u *)",", NULL)
8718 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8719 break;
8720 l = ml_get_curline();
8721 }
8722
8723 /*
8724 * Doesn't look like anything interesting -- so just
8725 * use the indent of this line.
8726 *
8727 * Position the cursor over the rightmost paren, so that
8728 * matching it will take us back to the start of the line.
8729 */
8730 find_last_paren(l, '(', ')');
8731
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008732 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008733 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 amount = get_indent(); /* XXX */
8735 break;
8736 }
8737
8738 /* add extra indent for a comment */
8739 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008740 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
8742 /* add extra indent if the previous line ended in a backslash:
8743 * "asdfasdf\
8744 * here";
8745 * char *foo = "asdf\
8746 * here";
8747 */
8748 if (cur_curpos.lnum > 1)
8749 {
8750 l = ml_get(cur_curpos.lnum - 1);
8751 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8752 {
8753 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8754 if (cur_amount > 0)
8755 amount = cur_amount;
8756 else if (cur_amount == 0)
8757 amount += ind_continuation;
8758 }
8759 }
8760 }
8761 }
8762
8763theend:
8764 /* put the cursor back where it belongs */
8765 curwin->w_cursor = cur_curpos;
8766
8767 vim_free(linecopy);
8768
8769 if (amount < 0)
8770 return 0;
8771 return amount;
8772}
8773
8774 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008775find_match(lookfor, ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 int lookfor;
8777 linenr_T ourscope;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 char_u *look;
8780 pos_T *theirscope;
8781 char_u *mightbeif;
8782 int elselevel;
8783 int whilelevel;
8784
8785 if (lookfor == LOOKFOR_IF)
8786 {
8787 elselevel = 1;
8788 whilelevel = 0;
8789 }
8790 else
8791 {
8792 elselevel = 0;
8793 whilelevel = 1;
8794 }
8795
8796 curwin->w_cursor.col = 0;
8797
8798 while (curwin->w_cursor.lnum > ourscope + 1)
8799 {
8800 curwin->w_cursor.lnum--;
8801 curwin->w_cursor.col = 0;
8802
8803 look = cin_skipcomment(ml_get_curline());
8804 if (cin_iselse(look)
8805 || cin_isif(look)
8806 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008807 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 {
8809 /*
8810 * if we've gone outside the braces entirely,
8811 * we must be out of scope...
8812 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008813 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 if (theirscope == NULL)
8815 break;
8816
8817 /*
8818 * and if the brace enclosing this is further
8819 * back than the one enclosing the else, we're
8820 * out of luck too.
8821 */
8822 if (theirscope->lnum < ourscope)
8823 break;
8824
8825 /*
8826 * and if they're enclosed in a *deeper* brace,
8827 * then we can ignore it because it's in a
8828 * different scope...
8829 */
8830 if (theirscope->lnum > ourscope)
8831 continue;
8832
8833 /*
8834 * if it was an "else" (that's not an "else if")
8835 * then we need to go back to another if, so
8836 * increment elselevel
8837 */
8838 look = cin_skipcomment(ml_get_curline());
8839 if (cin_iselse(look))
8840 {
8841 mightbeif = cin_skipcomment(look + 4);
8842 if (!cin_isif(mightbeif))
8843 ++elselevel;
8844 continue;
8845 }
8846
8847 /*
8848 * if it was a "while" then we need to go back to
8849 * another "do", so increment whilelevel. XXX
8850 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008851 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {
8853 ++whilelevel;
8854 continue;
8855 }
8856
8857 /* If it's an "if" decrement elselevel */
8858 look = cin_skipcomment(ml_get_curline());
8859 if (cin_isif(look))
8860 {
8861 elselevel--;
8862 /*
8863 * When looking for an "if" ignore "while"s that
8864 * get in the way.
8865 */
8866 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8867 whilelevel = 0;
8868 }
8869
8870 /* If it's a "do" decrement whilelevel */
8871 if (cin_isdo(look))
8872 whilelevel--;
8873
8874 /*
8875 * if we've used up all the elses, then
8876 * this must be the if that we want!
8877 * match the indent level of that if.
8878 */
8879 if (elselevel <= 0 && whilelevel <= 0)
8880 {
8881 return OK;
8882 }
8883 }
8884 }
8885 return FAIL;
8886}
8887
8888# if defined(FEAT_EVAL) || defined(PROTO)
8889/*
8890 * Get indent level from 'indentexpr'.
8891 */
8892 int
8893get_expr_indent()
8894{
8895 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008896 pos_T save_pos;
8897 colnr_T save_curswant;
8898 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008900 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8901 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008903 /* Save and restore cursor position and curswant, in case it was changed
8904 * via :normal commands */
8905 save_pos = curwin->w_cursor;
8906 save_curswant = curwin->w_curswant;
8907 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008909 if (use_sandbox)
8910 ++sandbox;
8911 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008913 if (use_sandbox)
8914 --sandbox;
8915 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
8917 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8918 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8919 * command. */
8920 save_State = State;
8921 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008922 curwin->w_cursor = save_pos;
8923 curwin->w_curswant = save_curswant;
8924 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 check_cursor();
8926 State = save_State;
8927
8928 /* If there is an error, just keep the current indent. */
8929 if (indent < 0)
8930 indent = get_indent();
8931
8932 return indent;
8933}
8934# endif
8935
8936#endif /* FEAT_CINDENT */
8937
8938#if defined(FEAT_LISP) || defined(PROTO)
8939
8940static int lisp_match __ARGS((char_u *p));
8941
8942 static int
8943lisp_match(p)
8944 char_u *p;
8945{
8946 char_u buf[LSIZE];
8947 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01008948 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
8950 while (*word != NUL)
8951 {
8952 (void)copy_option_part(&word, buf, LSIZE, ",");
8953 len = (int)STRLEN(buf);
8954 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8955 return TRUE;
8956 }
8957 return FALSE;
8958}
8959
8960/*
8961 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8962 * The incompatible newer method is quite a bit better at indenting
8963 * code in lisp-like languages than the traditional one; it's still
8964 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8965 *
8966 * TODO:
8967 * Findmatch() should be adapted for lisp, also to make showmatch
8968 * work correctly: now (v5.3) it seems all C/C++ oriented:
8969 * - it does not recognize the #\( and #\) notations as character literals
8970 * - it doesn't know about comments starting with a semicolon
8971 * - it incorrectly interprets '(' as a character literal
8972 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008973 * Update from Sergey Khorev:
8974 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 */
8976 int
8977get_lisp_indent()
8978{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008979 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 int amount;
8981 char_u *that;
8982 colnr_T col;
8983 colnr_T firsttry;
8984 int parencount, quotecount;
8985 int vi_lisp;
8986
8987 /* Set vi_lisp to use the vi-compatible method */
8988 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8989
8990 realpos = curwin->w_cursor;
8991 curwin->w_cursor.col = 0;
8992
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008993 if ((pos = findmatch(NULL, '(')) == NULL)
8994 pos = findmatch(NULL, '[');
8995 else
8996 {
8997 paren = *pos;
8998 pos = findmatch(NULL, '[');
8999 if (pos == NULL || ltp(pos, &paren))
9000 pos = &paren;
9001 }
9002 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 {
9004 /* Extra trick: Take the indent of the first previous non-white
9005 * line that is at the same () level. */
9006 amount = -1;
9007 parencount = 0;
9008
9009 while (--curwin->w_cursor.lnum >= pos->lnum)
9010 {
9011 if (linewhite(curwin->w_cursor.lnum))
9012 continue;
9013 for (that = ml_get_curline(); *that != NUL; ++that)
9014 {
9015 if (*that == ';')
9016 {
9017 while (*(that + 1) != NUL)
9018 ++that;
9019 continue;
9020 }
9021 if (*that == '\\')
9022 {
9023 if (*(that + 1) != NUL)
9024 ++that;
9025 continue;
9026 }
9027 if (*that == '"' && *(that + 1) != NUL)
9028 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009029 while (*++that && *that != '"')
9030 {
9031 /* skipping escaped characters in the string */
9032 if (*that == '\\')
9033 {
9034 if (*++that == NUL)
9035 break;
9036 if (that[1] == NUL)
9037 {
9038 ++that;
9039 break;
9040 }
9041 }
9042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009044 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009046 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 --parencount;
9048 }
9049 if (parencount == 0)
9050 {
9051 amount = get_indent();
9052 break;
9053 }
9054 }
9055
9056 if (amount == -1)
9057 {
9058 curwin->w_cursor.lnum = pos->lnum;
9059 curwin->w_cursor.col = pos->col;
9060 col = pos->col;
9061
9062 that = ml_get_curline();
9063
9064 if (vi_lisp && get_indent() == 0)
9065 amount = 2;
9066 else
9067 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009068 char_u *line = that;
9069
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 amount = 0;
9071 while (*that && col)
9072 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009073 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 col--;
9075 }
9076
9077 /*
9078 * Some keywords require "body" indenting rules (the
9079 * non-standard-lisp ones are Scheme special forms):
9080 *
9081 * (let ((a 1)) instead (let ((a 1))
9082 * (...)) of (...))
9083 */
9084
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009085 if (!vi_lisp && (*that == '(' || *that == '[')
9086 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 amount += 2;
9088 else
9089 {
9090 that++;
9091 amount++;
9092 firsttry = amount;
9093
9094 while (vim_iswhite(*that))
9095 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009096 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 ++that;
9098 }
9099
9100 if (*that && *that != ';') /* not a comment line */
9101 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009102 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009104 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 firsttry++;
9106
9107 parencount = 0;
9108 quotecount = 0;
9109
9110 if (vi_lisp
9111 || (*that != '"'
9112 && *that != '\''
9113 && *that != '#'
9114 && (*that < '0' || *that > '9')))
9115 {
9116 while (*that
9117 && (!vim_iswhite(*that)
9118 || quotecount
9119 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009120 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 && !quotecount
9122 && !parencount
9123 && vi_lisp)))
9124 {
9125 if (*that == '"')
9126 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009127 if ((*that == '(' || *that == '[')
9128 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009130 if ((*that == ')' || *that == ']')
9131 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 --parencount;
9133 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009134 amount += lbr_chartabsize_adv(
9135 line, &that, (colnr_T)amount);
9136 amount += lbr_chartabsize_adv(
9137 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 }
9139 }
9140 while (vim_iswhite(*that))
9141 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009142 amount += lbr_chartabsize(
9143 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 that++;
9145 }
9146 if (!*that || *that == ';')
9147 amount = firsttry;
9148 }
9149 }
9150 }
9151 }
9152 }
9153 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009154 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155
9156 curwin->w_cursor = realpos;
9157
9158 return amount;
9159}
9160#endif /* FEAT_LISP */
9161
9162 void
9163prepare_to_exit()
9164{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009165#if defined(SIGHUP) && defined(SIG_IGN)
9166 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9167 * makes Vim exit and then handling SIGHUP causes various reentrance
9168 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009169 signal(SIGHUP, SIG_IGN);
9170#endif
9171
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172#ifdef FEAT_GUI
9173 if (gui.in_use)
9174 {
9175 gui.dying = TRUE;
9176 out_trash(); /* trash any pending output */
9177 }
9178 else
9179#endif
9180 {
9181 windgoto((int)Rows - 1, 0);
9182
9183 /*
9184 * Switch terminal mode back now, so messages end up on the "normal"
9185 * screen (if there are two screens).
9186 */
9187 settmode(TMODE_COOK);
9188#ifdef WIN3264
9189 if (can_end_termcap_mode(FALSE) == TRUE)
9190#endif
9191 stoptermcap();
9192 out_flush();
9193 }
9194}
9195
9196/*
9197 * Preserve files and exit.
9198 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009199 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9200 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 */
9202 void
9203preserve_exit()
9204{
9205 buf_T *buf;
9206
9207 prepare_to_exit();
9208
Bram Moolenaar4770d092006-01-12 23:22:24 +00009209 /* Setting this will prevent free() calls. That avoids calling free()
9210 * recursively when free() was invoked with a bad pointer. */
9211 really_exiting = TRUE;
9212
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 out_str(IObuff);
9214 screen_start(); /* don't know where cursor is now */
9215 out_flush();
9216
9217 ml_close_notmod(); /* close all not-modified buffers */
9218
9219 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9220 {
9221 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9222 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009223 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 screen_start(); /* don't know where cursor is now */
9225 out_flush();
9226 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9227 break;
9228 }
9229 }
9230
9231 ml_close_all(FALSE); /* close all memfiles, without deleting */
9232
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009233 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
9235 getout(1);
9236}
9237
9238/*
9239 * return TRUE if "fname" exists.
9240 */
9241 int
9242vim_fexists(fname)
9243 char_u *fname;
9244{
9245 struct stat st;
9246
9247 if (mch_stat((char *)fname, &st))
9248 return FALSE;
9249 return TRUE;
9250}
9251
9252/*
9253 * Check for CTRL-C pressed, but only once in a while.
9254 * Should be used instead of ui_breakcheck() for functions that check for
9255 * each line in the file. Calling ui_breakcheck() each time takes too much
9256 * time, because it can be a system call.
9257 */
9258
9259#ifndef BREAKCHECK_SKIP
9260# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9261# define BREAKCHECK_SKIP 200
9262# else
9263# define BREAKCHECK_SKIP 32
9264# endif
9265#endif
9266
9267static int breakcheck_count = 0;
9268
9269 void
9270line_breakcheck()
9271{
9272 if (++breakcheck_count >= BREAKCHECK_SKIP)
9273 {
9274 breakcheck_count = 0;
9275 ui_breakcheck();
9276 }
9277}
9278
9279/*
9280 * Like line_breakcheck() but check 10 times less often.
9281 */
9282 void
9283fast_breakcheck()
9284{
9285 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9286 {
9287 breakcheck_count = 0;
9288 ui_breakcheck();
9289 }
9290}
9291
9292/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009293 * Invoke expand_wildcards() for one pattern.
9294 * Expand items like "%:h" before the expansion.
9295 * Returns OK or FAIL.
9296 */
9297 int
9298expand_wildcards_eval(pat, num_file, file, flags)
9299 char_u **pat; /* pointer to input pattern */
9300 int *num_file; /* resulting number of files */
9301 char_u ***file; /* array of resulting files */
9302 int flags; /* EW_DIR, etc. */
9303{
9304 int ret = FAIL;
9305 char_u *eval_pat = NULL;
9306 char_u *exp_pat = *pat;
9307 char_u *ignored_msg;
9308 int usedlen;
9309
9310 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9311 {
9312 ++emsg_off;
9313 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9314 NULL, &ignored_msg, NULL);
9315 --emsg_off;
9316 if (eval_pat != NULL)
9317 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9318 }
9319
9320 if (exp_pat != NULL)
9321 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9322
9323 if (eval_pat != NULL)
9324 {
9325 vim_free(exp_pat);
9326 vim_free(eval_pat);
9327 }
9328
9329 return ret;
9330}
9331
9332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9334 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009335 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 */
9337 int
9338expand_wildcards(num_pat, pat, num_file, file, flags)
9339 int num_pat; /* number of input patterns */
9340 char_u **pat; /* array of input patterns */
9341 int *num_file; /* resulting number of files */
9342 char_u ***file; /* array of resulting files */
9343 int flags; /* EW_DIR, etc. */
9344{
9345 int retval;
9346 int i, j;
9347 char_u *p;
9348 int non_suf_match; /* number without matching suffix */
9349
9350 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9351
9352 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009353 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 return retval;
9355
9356#ifdef FEAT_WILDIGN
9357 /*
9358 * Remove names that match 'wildignore'.
9359 */
9360 if (*p_wig)
9361 {
9362 char_u *ffname;
9363
9364 /* check all files in (*file)[] */
9365 for (i = 0; i < *num_file; ++i)
9366 {
9367 ffname = FullName_save((*file)[i], FALSE);
9368 if (ffname == NULL) /* out of memory */
9369 break;
9370# ifdef VMS
9371 vms_remove_version(ffname);
9372# endif
9373 if (match_file_list(p_wig, (*file)[i], ffname))
9374 {
9375 /* remove this matching file from the list */
9376 vim_free((*file)[i]);
9377 for (j = i; j + 1 < *num_file; ++j)
9378 (*file)[j] = (*file)[j + 1];
9379 --*num_file;
9380 --i;
9381 }
9382 vim_free(ffname);
9383 }
9384 }
9385#endif
9386
9387 /*
9388 * Move the names where 'suffixes' match to the end.
9389 */
9390 if (*num_file > 1)
9391 {
9392 non_suf_match = 0;
9393 for (i = 0; i < *num_file; ++i)
9394 {
9395 if (!match_suffix((*file)[i]))
9396 {
9397 /*
9398 * Move the name without matching suffix to the front
9399 * of the list.
9400 */
9401 p = (*file)[i];
9402 for (j = i; j > non_suf_match; --j)
9403 (*file)[j] = (*file)[j - 1];
9404 (*file)[non_suf_match++] = p;
9405 }
9406 }
9407 }
9408
9409 return retval;
9410}
9411
9412/*
9413 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9414 */
9415 int
9416match_suffix(fname)
9417 char_u *fname;
9418{
9419 int fnamelen, setsuflen;
9420 char_u *setsuf;
9421#define MAXSUFLEN 30 /* maximum length of a file suffix */
9422 char_u suf_buf[MAXSUFLEN];
9423
9424 fnamelen = (int)STRLEN(fname);
9425 setsuflen = 0;
9426 for (setsuf = p_su; *setsuf; )
9427 {
9428 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009429 if (setsuflen == 0)
9430 {
9431 char_u *tail = gettail(fname);
9432
9433 /* empty entry: match name without a '.' */
9434 if (vim_strchr(tail, '.') == NULL)
9435 {
9436 setsuflen = 1;
9437 break;
9438 }
9439 }
9440 else
9441 {
9442 if (fnamelen >= setsuflen
9443 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9444 (size_t)setsuflen) == 0)
9445 break;
9446 setsuflen = 0;
9447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
9449 return (setsuflen != 0);
9450}
9451
9452#if !defined(NO_EXPANDPATH) || defined(PROTO)
9453
9454# ifdef VIM_BACKTICK
9455static int vim_backtick __ARGS((char_u *p));
9456static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9457# endif
9458
9459# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9460/*
9461 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9462 * it's shared between these systems.
9463 */
9464# if defined(DJGPP) || defined(PROTO)
9465# define _cdecl /* DJGPP doesn't have this */
9466# else
9467# ifdef __BORLANDC__
9468# define _cdecl _RTLENTRYF
9469# endif
9470# endif
9471
9472/*
9473 * comparison function for qsort in dos_expandpath()
9474 */
9475 static int _cdecl
9476pstrcmp(const void *a, const void *b)
9477{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009478 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479}
9480
9481# ifndef WIN3264
9482 static void
9483namelowcpy(
9484 char_u *d,
9485 char_u *s)
9486{
9487# ifdef DJGPP
9488 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9489 while (*s)
9490 *d++ = *s++;
9491 else
9492# endif
9493 while (*s)
9494 *d++ = TOLOWER_LOC(*s++);
9495 *d = NUL;
9496}
9497# endif
9498
9499/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009500 * Recursively expand one path component into all matching files and/or
9501 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 * Return the number of matches found.
9503 * "path" has backslashes before chars that are not to be expanded, starting
9504 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009505 * Return the number of matches found.
9506 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 */
9508 static int
9509dos_expandpath(
9510 garray_T *gap,
9511 char_u *path,
9512 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009513 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009514 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009516 char_u *buf;
9517 char_u *path_end;
9518 char_u *p, *s, *e;
9519 int start_len = gap->ga_len;
9520 char_u *pat;
9521 regmatch_T regmatch;
9522 int starts_with_dot;
9523 int matches;
9524 int len;
9525 int starstar = FALSE;
9526 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527#ifdef WIN3264
9528 WIN32_FIND_DATA fb;
9529 HANDLE hFind = (HANDLE)0;
9530# ifdef FEAT_MBYTE
9531 WIN32_FIND_DATAW wfb;
9532 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9533# endif
9534#else
9535 struct ffblk fb;
9536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009538 int ok;
9539
9540 /* Expanding "**" may take a long time, check for CTRL-C. */
9541 if (stardepth > 0)
9542 {
9543 ui_breakcheck();
9544 if (got_int)
9545 return 0;
9546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
9548 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009549 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 if (buf == NULL)
9551 return 0;
9552
9553 /*
9554 * Find the first part in the path name that contains a wildcard or a ~1.
9555 * Copy it into buf, including the preceding characters.
9556 */
9557 p = buf;
9558 s = buf;
9559 e = NULL;
9560 path_end = path;
9561 while (*path_end != NUL)
9562 {
9563 /* May ignore a wildcard that has a backslash before it; it will
9564 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9565 if (path_end >= path + wildoff && rem_backslash(path_end))
9566 *p++ = *path_end++;
9567 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9568 {
9569 if (e != NULL)
9570 break;
9571 s = p + 1;
9572 }
9573 else if (path_end >= path + wildoff
9574 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9575 e = p;
9576#ifdef FEAT_MBYTE
9577 if (has_mbyte)
9578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009579 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 STRNCPY(p, path_end, len);
9581 p += len;
9582 path_end += len;
9583 }
9584 else
9585#endif
9586 *p++ = *path_end++;
9587 }
9588 e = p;
9589 *e = NUL;
9590
9591 /* now we have one wildcard component between s and e */
9592 /* Remove backslashes between "wildoff" and the start of the wildcard
9593 * component. */
9594 for (p = buf + wildoff; p < s; ++p)
9595 if (rem_backslash(p))
9596 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009597 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 --e;
9599 --s;
9600 }
9601
Bram Moolenaar231334e2005-07-25 20:46:57 +00009602 /* Check for "**" between "s" and "e". */
9603 for (p = s; p < e; ++p)
9604 if (p[0] == '*' && p[1] == '*')
9605 starstar = TRUE;
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 starts_with_dot = (*s == '.');
9608 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9609 if (pat == NULL)
9610 {
9611 vim_free(buf);
9612 return 0;
9613 }
9614
9615 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009616 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009617 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 regmatch.rm_ic = TRUE; /* Always ignore case */
9619 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009620 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009621 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 vim_free(pat);
9623
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009624 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 {
9626 vim_free(buf);
9627 return 0;
9628 }
9629
9630 /* remember the pattern or file name being looked for */
9631 matchname = vim_strsave(s);
9632
Bram Moolenaar231334e2005-07-25 20:46:57 +00009633 /* If "**" is by itself, this is the first time we encounter it and more
9634 * is following then find matches without any directory. */
9635 if (!didstar && stardepth < 100 && starstar && e - s == 2
9636 && *path_end == '/')
9637 {
9638 STRCPY(s, path_end + 1);
9639 ++stardepth;
9640 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9641 --stardepth;
9642 }
9643
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 /* Scan all files in the directory with "dir/ *.*" */
9645 STRCPY(s, "*.*");
9646#ifdef WIN3264
9647# ifdef FEAT_MBYTE
9648 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9649 {
9650 /* The active codepage differs from 'encoding'. Attempt using the
9651 * wide function. If it fails because it is not implemented fall back
9652 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009653 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (wn != NULL)
9655 {
9656 hFind = FindFirstFileW(wn, &wfb);
9657 if (hFind == INVALID_HANDLE_VALUE
9658 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9659 {
9660 vim_free(wn);
9661 wn = NULL;
9662 }
9663 }
9664 }
9665
9666 if (wn == NULL)
9667# endif
9668 hFind = FindFirstFile(buf, &fb);
9669 ok = (hFind != INVALID_HANDLE_VALUE);
9670#else
9671 /* If we are expanding wildcards we try both files and directories */
9672 ok = (findfirst((char *)buf, &fb,
9673 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9674#endif
9675
9676 while (ok)
9677 {
9678#ifdef WIN3264
9679# ifdef FEAT_MBYTE
9680 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009681 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 else
9683# endif
9684 p = (char_u *)fb.cFileName;
9685#else
9686 p = (char_u *)fb.ff_name;
9687#endif
9688 /* Ignore entries starting with a dot, unless when asked for. Accept
9689 * all entries found with "matchname". */
9690 if ((p[0] != '.' || starts_with_dot)
9691 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009692 || (regmatch.regprog != NULL
9693 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009694 || ((flags & EW_NOTWILD)
9695 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 {
9697#ifdef WIN3264
9698 STRCPY(s, p);
9699#else
9700 namelowcpy(s, p);
9701#endif
9702 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009703
9704 if (starstar && stardepth < 100)
9705 {
9706 /* For "**" in the pattern first go deeper in the tree to
9707 * find matches. */
9708 STRCPY(buf + len, "/**");
9709 STRCPY(buf + len + 3, path_end);
9710 ++stardepth;
9711 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9712 --stardepth;
9713 }
9714
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 STRCPY(buf + len, path_end);
9716 if (mch_has_exp_wildcard(path_end))
9717 {
9718 /* need to expand another component of the path */
9719 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009720 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 }
9722 else
9723 {
9724 /* no more wildcards, check if there is a match */
9725 /* remove backslashes for the remaining components only */
9726 if (*path_end != 0)
9727 backslash_halve(buf + len + 1);
9728 if (mch_getperm(buf) >= 0) /* add existing file */
9729 addfile(gap, buf, flags);
9730 }
9731 }
9732
9733#ifdef WIN3264
9734# ifdef FEAT_MBYTE
9735 if (wn != NULL)
9736 {
9737 vim_free(p);
9738 ok = FindNextFileW(hFind, &wfb);
9739 }
9740 else
9741# endif
9742 ok = FindNextFile(hFind, &fb);
9743#else
9744 ok = (findnext(&fb) == 0);
9745#endif
9746
9747 /* If no more matches and no match was used, try expanding the name
9748 * itself. Finds the long name of a short filename. */
9749 if (!ok && matchname != NULL && gap->ga_len == start_len)
9750 {
9751 STRCPY(s, matchname);
9752#ifdef WIN3264
9753 FindClose(hFind);
9754# ifdef FEAT_MBYTE
9755 if (wn != NULL)
9756 {
9757 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009758 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (wn != NULL)
9760 hFind = FindFirstFileW(wn, &wfb);
9761 }
9762 if (wn == NULL)
9763# endif
9764 hFind = FindFirstFile(buf, &fb);
9765 ok = (hFind != INVALID_HANDLE_VALUE);
9766#else
9767 ok = (findfirst((char *)buf, &fb,
9768 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9769#endif
9770 vim_free(matchname);
9771 matchname = NULL;
9772 }
9773 }
9774
9775#ifdef WIN3264
9776 FindClose(hFind);
9777# ifdef FEAT_MBYTE
9778 vim_free(wn);
9779# endif
9780#endif
9781 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009782 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 vim_free(matchname);
9784
9785 matches = gap->ga_len - start_len;
9786 if (matches > 0)
9787 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9788 sizeof(char_u *), pstrcmp);
9789 return matches;
9790}
9791
9792 int
9793mch_expandpath(
9794 garray_T *gap,
9795 char_u *path,
9796 int flags) /* EW_* flags */
9797{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009798 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799}
9800# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9801
Bram Moolenaar231334e2005-07-25 20:46:57 +00009802#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9803 || defined(PROTO)
9804/*
9805 * Unix style wildcard expansion code.
9806 * It's here because it's used both for Unix and Mac.
9807 */
9808static int pstrcmp __ARGS((const void *, const void *));
9809
9810 static int
9811pstrcmp(a, b)
9812 const void *a, *b;
9813{
9814 return (pathcmp(*(char **)a, *(char **)b, -1));
9815}
9816
9817/*
9818 * Recursively expand one path component into all matching files and/or
9819 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9820 * "path" has backslashes before chars that are not to be expanded, starting
9821 * at "path + wildoff".
9822 * Return the number of matches found.
9823 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9824 */
9825 int
9826unix_expandpath(gap, path, wildoff, flags, didstar)
9827 garray_T *gap;
9828 char_u *path;
9829 int wildoff;
9830 int flags; /* EW_* flags */
9831 int didstar; /* expanded "**" once already */
9832{
9833 char_u *buf;
9834 char_u *path_end;
9835 char_u *p, *s, *e;
9836 int start_len = gap->ga_len;
9837 char_u *pat;
9838 regmatch_T regmatch;
9839 int starts_with_dot;
9840 int matches;
9841 int len;
9842 int starstar = FALSE;
9843 static int stardepth = 0; /* depth for "**" expansion */
9844
9845 DIR *dirp;
9846 struct dirent *dp;
9847
9848 /* Expanding "**" may take a long time, check for CTRL-C. */
9849 if (stardepth > 0)
9850 {
9851 ui_breakcheck();
9852 if (got_int)
9853 return 0;
9854 }
9855
9856 /* make room for file name */
9857 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9858 if (buf == NULL)
9859 return 0;
9860
9861 /*
9862 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009863 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009864 * Copy it into "buf", including the preceding characters.
9865 */
9866 p = buf;
9867 s = buf;
9868 e = NULL;
9869 path_end = path;
9870 while (*path_end != NUL)
9871 {
9872 /* May ignore a wildcard that has a backslash before it; it will
9873 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9874 if (path_end >= path + wildoff && rem_backslash(path_end))
9875 *p++ = *path_end++;
9876 else if (*path_end == '/')
9877 {
9878 if (e != NULL)
9879 break;
9880 s = p + 1;
9881 }
9882 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009883 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009884 || (!p_fic && (flags & EW_ICASE)
9885 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009886 e = p;
9887#ifdef FEAT_MBYTE
9888 if (has_mbyte)
9889 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009890 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009891 STRNCPY(p, path_end, len);
9892 p += len;
9893 path_end += len;
9894 }
9895 else
9896#endif
9897 *p++ = *path_end++;
9898 }
9899 e = p;
9900 *e = NUL;
9901
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009902 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009903 /* Remove backslashes between "wildoff" and the start of the wildcard
9904 * component. */
9905 for (p = buf + wildoff; p < s; ++p)
9906 if (rem_backslash(p))
9907 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009908 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009909 --e;
9910 --s;
9911 }
9912
9913 /* Check for "**" between "s" and "e". */
9914 for (p = s; p < e; ++p)
9915 if (p[0] == '*' && p[1] == '*')
9916 starstar = TRUE;
9917
9918 /* convert the file pattern to a regexp pattern */
9919 starts_with_dot = (*s == '.');
9920 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9921 if (pat == NULL)
9922 {
9923 vim_free(buf);
9924 return 0;
9925 }
9926
9927 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009928 if (flags & EW_ICASE)
9929 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9930 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009931 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009932 if (flags & (EW_NOERROR | EW_NOTWILD))
9933 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009934 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009935 if (flags & (EW_NOERROR | EW_NOTWILD))
9936 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009937 vim_free(pat);
9938
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009939 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009940 {
9941 vim_free(buf);
9942 return 0;
9943 }
9944
9945 /* If "**" is by itself, this is the first time we encounter it and more
9946 * is following then find matches without any directory. */
9947 if (!didstar && stardepth < 100 && starstar && e - s == 2
9948 && *path_end == '/')
9949 {
9950 STRCPY(s, path_end + 1);
9951 ++stardepth;
9952 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9953 --stardepth;
9954 }
9955
9956 /* open the directory for scanning */
9957 *s = NUL;
9958 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9959
9960 /* Find all matching entries */
9961 if (dirp != NULL)
9962 {
9963 for (;;)
9964 {
9965 dp = readdir(dirp);
9966 if (dp == NULL)
9967 break;
9968 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009969 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9970 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009971 || ((flags & EW_NOTWILD)
9972 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009973 {
9974 STRCPY(s, dp->d_name);
9975 len = STRLEN(buf);
9976
9977 if (starstar && stardepth < 100)
9978 {
9979 /* For "**" in the pattern first go deeper in the tree to
9980 * find matches. */
9981 STRCPY(buf + len, "/**");
9982 STRCPY(buf + len + 3, path_end);
9983 ++stardepth;
9984 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9985 --stardepth;
9986 }
9987
9988 STRCPY(buf + len, path_end);
9989 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9990 {
9991 /* need to expand another component of the path */
9992 /* remove backslashes for the remaining components only */
9993 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9994 }
9995 else
9996 {
9997 /* no more wildcards, check if there is a match */
9998 /* remove backslashes for the remaining components only */
9999 if (*path_end != NUL)
10000 backslash_halve(buf + len + 1);
10001 if (mch_getperm(buf) >= 0) /* add existing file */
10002 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010003#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010004 size_t precomp_len = STRLEN(buf)+1;
10005 char_u *precomp_buf =
10006 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010007
Bram Moolenaar231334e2005-07-25 20:46:57 +000010008 if (precomp_buf)
10009 {
10010 mch_memmove(buf, precomp_buf, precomp_len);
10011 vim_free(precomp_buf);
10012 }
10013#endif
10014 addfile(gap, buf, flags);
10015 }
10016 }
10017 }
10018 }
10019
10020 closedir(dirp);
10021 }
10022
10023 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010024 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010025
10026 matches = gap->ga_len - start_len;
10027 if (matches > 0)
10028 qsort(((char_u **)gap->ga_data) + start_len, matches,
10029 sizeof(char_u *), pstrcmp);
10030 return matches;
10031}
10032#endif
10033
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010034#if defined(FEAT_SEARCHPATH)
10035static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10036static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010037static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10038static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010039static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10040static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10041
10042/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010043 * Moves "*psep" back to the previous path separator in "path".
10044 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010045 */
10046 static int
10047find_previous_pathsep(path, psep)
10048 char_u *path;
10049 char_u **psep;
10050{
10051 /* skip the current separator */
10052 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010053 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010054
10055 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010056 while (*psep > path)
10057 {
10058 if (vim_ispathsep(**psep))
10059 return OK;
10060 mb_ptr_back(path, *psep);
10061 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010062
10063 return FAIL;
10064}
10065
10066/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010067 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10068 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010069 */
10070 static int
10071is_unique(maybe_unique, gap, i)
10072 char_u *maybe_unique;
10073 garray_T *gap;
10074 int i;
10075{
10076 int j;
10077 int candidate_len;
10078 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010079 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010080 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010081
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010082 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010083 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010084 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010085 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010086
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010087 candidate_len = (int)STRLEN(maybe_unique);
10088 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010089 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010090 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010091
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010092 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010093 if (fnamecmp(maybe_unique, rival) == 0
10094 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010095 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010096 }
10097
Bram Moolenaar162bd912010-07-28 22:29:10 +020010098 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010099}
10100
10101/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010102 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010103 * paths are expanded to their equivalent fullpath. This includes the "."
10104 * (relative to current buffer directory) and empty path (relative to current
10105 * directory) notations.
10106 *
10107 * TODO: handle upward search (;) and path limiter (**N) notations by
10108 * expanding each into their equivalent path(s).
10109 */
10110 static void
10111expand_path_option(curdir, gap)
10112 char_u *curdir;
10113 garray_T *gap;
10114{
10115 char_u *path_option = *curbuf->b_p_path == NUL
10116 ? p_path : curbuf->b_p_path;
10117 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010118 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010119 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010120
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010121 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010122 return;
10123
10124 while (*path_option != NUL)
10125 {
10126 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10127
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010128 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010129 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010130 /* Relative to current buffer:
10131 * "/path/file" + "." -> "/path/"
10132 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010133 if (curbuf->b_ffname == NULL)
10134 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010135 p = gettail(curbuf->b_ffname);
10136 len = (int)(p - curbuf->b_ffname);
10137 if (len + (int)STRLEN(buf) >= MAXPATHL)
10138 continue;
10139 if (buf[1] == NUL)
10140 buf[len] = NUL;
10141 else
10142 STRMOVE(buf + len, buf + 2);
10143 mch_memmove(buf, curbuf->b_ffname, len);
10144 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010145 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010146 else if (buf[0] == NUL)
10147 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010148 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010149 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010150 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010151 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010152 else if (!mch_isFullName(buf))
10153 {
10154 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010155 len = (int)STRLEN(curdir);
10156 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010157 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010158 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010159 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010160 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010161 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010162 }
10163
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010164 if (ga_grow(gap, 1) == FAIL)
10165 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010166
10167# if defined(MSWIN) || defined(MSDOS)
10168 /* Avoid the path ending in a backslash, it fails when a comma is
10169 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010170 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010171 if (buf[len - 1] == '\\')
10172 buf[len - 1] = '/';
10173# endif
10174
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010175 p = vim_strsave(buf);
10176 if (p == NULL)
10177 break;
10178 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010179 }
10180
10181 vim_free(buf);
10182}
10183
10184/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010185 * Returns a pointer to the file or directory name in "fname" that matches the
10186 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010187 *
10188 * path: /foo/bar/baz
10189 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010190 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010191 */
10192 static char_u *
10193get_path_cutoff(fname, gap)
10194 char_u *fname;
10195 garray_T *gap;
10196{
10197 int i;
10198 int maxlen = 0;
10199 char_u **path_part = (char_u **)gap->ga_data;
10200 char_u *cutoff = NULL;
10201
10202 for (i = 0; i < gap->ga_len; i++)
10203 {
10204 int j = 0;
10205
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010206 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010207# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010208 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10209#endif
10210 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010211 j++;
10212 if (j > maxlen)
10213 {
10214 maxlen = j;
10215 cutoff = &fname[j];
10216 }
10217 }
10218
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010219 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010220 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010221 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010222 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010223
10224 return cutoff;
10225}
10226
10227/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010228 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10229 * that they are unique with respect to each other while conserving the part
10230 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010231 */
10232 static void
10233uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010234 garray_T *gap;
10235 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010236{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010237 int i;
10238 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010239 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010240 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010241 char_u *pat;
10242 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010243 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010244 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010245 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010246 char_u **in_curdir = NULL;
10247 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010248
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010249 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010250 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010251
10252 /*
10253 * We need to prepend a '*' at the beginning of file_pattern so that the
10254 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010255 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010256 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010257 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010258 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010259 if (file_pattern == NULL)
10260 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010261 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010262 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010263 STRCAT(file_pattern, pattern);
10264 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10265 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010266 if (pat == NULL)
10267 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010268
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010269 regmatch.rm_ic = TRUE; /* always ignore case */
10270 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10271 vim_free(pat);
10272 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010273 return;
10274
Bram Moolenaar162bd912010-07-28 22:29:10 +020010275 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010276 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010277 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010278 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010279
10280 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010281 if (in_curdir == NULL)
10282 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010283
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010284 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010285 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010286 char_u *path = fnames[i];
10287 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010288 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010289 char_u *pathsep_p;
10290 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010291
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010292 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010293 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010294 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010295 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010296 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010297
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010298 /* Shorten the filename while maintaining its uniqueness */
10299 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010300
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010301 /* we start at the end of the path */
10302 pathsep_p = path + len - 1;
10303
10304 while (find_previous_pathsep(path, &pathsep_p))
10305 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10306 && is_unique(pathsep_p + 1, gap, i)
10307 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10308 {
10309 sort_again = TRUE;
10310 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10311 break;
10312 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010313
10314 if (mch_isFullName(path))
10315 {
10316 /*
10317 * Last resort: shorten relative to curdir if possible.
10318 * 'possible' means:
10319 * 1. It is under the current directory.
10320 * 2. The result is actually shorter than the original.
10321 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010322 * Before curdir After
10323 * /foo/bar/file.txt /foo/bar ./file.txt
10324 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10325 * /file.txt / /file.txt
10326 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010327 */
10328 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010329 if (short_name != NULL && short_name > path + 1
10330#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010331 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010332 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010333 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010334 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010335 && !vim_ispathsep(*short_name)
10336#endif
10337 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010338 {
10339 STRCPY(path, ".");
10340 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010341 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010342 }
10343 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010344 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010345 }
10346
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010347 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010348 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010349 {
10350 char_u *rel_path;
10351 char_u *path = in_curdir[i];
10352
10353 if (path == NULL)
10354 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010355
10356 /* If the {filename} is not unique, change it to ./{filename}.
10357 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010358 short_name = shorten_fname(path, curdir);
10359 if (short_name == NULL)
10360 short_name = path;
10361 if (is_unique(short_name, gap, i))
10362 {
10363 STRCPY(fnames[i], short_name);
10364 continue;
10365 }
10366
10367 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10368 if (rel_path == NULL)
10369 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010370 STRCPY(rel_path, ".");
10371 add_pathsep(rel_path);
10372 STRCAT(rel_path, short_name);
10373
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010374 vim_free(fnames[i]);
10375 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010376 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010377 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010378 }
10379
Bram Moolenaar162bd912010-07-28 22:29:10 +020010380theend:
10381 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010382 if (in_curdir != NULL)
10383 {
10384 for (i = 0; i < gap->ga_len; i++)
10385 vim_free(in_curdir[i]);
10386 vim_free(in_curdir);
10387 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010388 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010389 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010390
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010391 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010392 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010393}
10394
10395/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010396 * Calls globpath() with 'path' values for the given pattern and stores the
10397 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010398 * Returns the total number of matches.
10399 */
10400 static int
10401expand_in_path(gap, pattern, flags)
10402 garray_T *gap;
10403 char_u *pattern;
10404 int flags; /* EW_* flags */
10405{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010406 char_u *curdir;
10407 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010408 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010409
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010410 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010411 return 0;
10412 mch_dirname(curdir, MAXPATHL);
10413
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010414 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010415 expand_path_option(curdir, &path_ga);
10416 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010417 if (path_ga.ga_len == 0)
10418 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010419
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010420 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010421 ga_clear_strings(&path_ga);
10422 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010423 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010424
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010425 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010426 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010427
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010428 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429}
10430#endif
10431
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010432#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10433/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010434 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10435 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010436 */
10437 void
10438remove_duplicates(gap)
10439 garray_T *gap;
10440{
10441 int i;
10442 int j;
10443 char_u **fnames = (char_u **)gap->ga_data;
10444
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010445 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010446 for (i = gap->ga_len - 1; i > 0; --i)
10447 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10448 {
10449 vim_free(fnames[i]);
10450 for (j = i + 1; j < gap->ga_len; ++j)
10451 fnames[j - 1] = fnames[j];
10452 --gap->ga_len;
10453 }
10454}
10455#endif
10456
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010457static int has_env_var __ARGS((char_u *p));
10458
10459/*
10460 * Return TRUE if "p" contains what looks like an environment variable.
10461 * Allowing for escaping.
10462 */
10463 static int
10464has_env_var(p)
10465 char_u *p;
10466{
10467 for ( ; *p; mb_ptr_adv(p))
10468 {
10469 if (*p == '\\' && p[1] != NUL)
10470 ++p;
10471 else if (vim_strchr((char_u *)
10472#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
10473 "$%"
10474#else
10475 "$"
10476#endif
10477 , *p) != NULL)
10478 return TRUE;
10479 }
10480 return FALSE;
10481}
10482
10483#ifdef SPECIAL_WILDCHAR
10484static int has_special_wildchar __ARGS((char_u *p));
10485
10486/*
10487 * Return TRUE if "p" contains a special wildcard character.
10488 * Allowing for escaping.
10489 */
10490 static int
10491has_special_wildchar(p)
10492 char_u *p;
10493{
10494 for ( ; *p; mb_ptr_adv(p))
10495 {
10496 if (*p == '\\' && p[1] != NUL)
10497 ++p;
10498 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10499 return TRUE;
10500 }
10501 return FALSE;
10502}
10503#endif
10504
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505/*
10506 * Generic wildcard expansion code.
10507 *
10508 * Characters in "pat" that should not be expanded must be preceded with a
10509 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10510 *
10511 * Return FAIL when no single file was found. In this case "num_file" is not
10512 * set, and "file" may contain an error message.
10513 * Return OK when some files found. "num_file" is set to the number of
10514 * matches, "file" to the array of matches. Call FreeWild() later.
10515 */
10516 int
10517gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10518 int num_pat; /* number of input patterns */
10519 char_u **pat; /* array of input patterns */
10520 int *num_file; /* resulting number of files */
10521 char_u ***file; /* array of resulting files */
10522 int flags; /* EW_* flags */
10523{
10524 int i;
10525 garray_T ga;
10526 char_u *p;
10527 static int recursive = FALSE;
10528 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010529#if defined(FEAT_SEARCHPATH)
10530 int did_expand_in_path = FALSE;
10531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532
10533 /*
10534 * expand_env() is called to expand things like "~user". If this fails,
10535 * it calls ExpandOne(), which brings us back here. In this case, always
10536 * call the machine specific expansion function, if possible. Otherwise,
10537 * return FAIL.
10538 */
10539 if (recursive)
10540#ifdef SPECIAL_WILDCHAR
10541 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10542#else
10543 return FAIL;
10544#endif
10545
10546#ifdef SPECIAL_WILDCHAR
10547 /*
10548 * If there are any special wildcard characters which we cannot handle
10549 * here, call machine specific function for all the expansion. This
10550 * avoids starting the shell for each argument separately.
10551 * For `=expr` do use the internal function.
10552 */
10553 for (i = 0; i < num_pat; i++)
10554 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010555 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556# ifdef VIM_BACKTICK
10557 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10558# endif
10559 )
10560 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10561 }
10562#endif
10563
10564 recursive = TRUE;
10565
10566 /*
10567 * The matching file names are stored in a growarray. Init it empty.
10568 */
10569 ga_init2(&ga, (int)sizeof(char_u *), 30);
10570
10571 for (i = 0; i < num_pat; ++i)
10572 {
10573 add_pat = -1;
10574 p = pat[i];
10575
10576#ifdef VIM_BACKTICK
10577 if (vim_backtick(p))
10578 add_pat = expand_backtick(&ga, p, flags);
10579 else
10580#endif
10581 {
10582 /*
10583 * First expand environment variables, "~/" and "~user/".
10584 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010585 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010587 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 if (p == NULL)
10589 p = pat[i];
10590#ifdef UNIX
10591 /*
10592 * On Unix, if expand_env() can't expand an environment
10593 * variable, use the shell to do that. Discard previously
10594 * found file names and start all over again.
10595 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010596 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 {
10598 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010599 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10601 flags);
10602 recursive = FALSE;
10603 return i;
10604 }
10605#endif
10606 }
10607
10608 /*
10609 * If there are wildcards: Expand file names and add each match to
10610 * the list. If there is no match, and EW_NOTFOUND is given, add
10611 * the pattern.
10612 * If there are no wildcards: Add the file name if it exists or
10613 * when EW_NOTFOUND is given.
10614 */
10615 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010616 {
10617#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010618 if ((flags & EW_PATH)
10619 && !mch_isFullName(p)
10620 && !(p[0] == '.'
10621 && (vim_ispathsep(p[1])
10622 || (p[1] == '.' && vim_ispathsep(p[2]))))
10623 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010624 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010625 /* :find completion where 'path' is used.
10626 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010627 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010628 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010629 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010630 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010631 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010632 else
10633#endif
10634 add_pat = mch_expandpath(&ga, p, flags);
10635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 }
10637
10638 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10639 {
10640 char_u *t = backslash_halve_save(p);
10641
10642#if defined(MACOS_CLASSIC)
10643 slash_to_colon(t);
10644#endif
10645 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10646 * "vim c:/" work. */
10647 if (flags & EW_NOTFOUND)
10648 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10649 else if (mch_getperm(t) >= 0)
10650 addfile(&ga, t, flags);
10651 vim_free(t);
10652 }
10653
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010654#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010655 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010656 uniquefy_paths(&ga, p);
10657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 if (p != pat[i])
10659 vim_free(p);
10660 }
10661
10662 *num_file = ga.ga_len;
10663 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10664
10665 recursive = FALSE;
10666
10667 return (ga.ga_data != NULL) ? OK : FAIL;
10668}
10669
10670# ifdef VIM_BACKTICK
10671
10672/*
10673 * Return TRUE if we can expand this backtick thing here.
10674 */
10675 static int
10676vim_backtick(p)
10677 char_u *p;
10678{
10679 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10680}
10681
10682/*
10683 * Expand an item in `backticks` by executing it as a command.
10684 * Currently only works when pat[] starts and ends with a `.
10685 * Returns number of file names found.
10686 */
10687 static int
10688expand_backtick(gap, pat, flags)
10689 garray_T *gap;
10690 char_u *pat;
10691 int flags; /* EW_* flags */
10692{
10693 char_u *p;
10694 char_u *cmd;
10695 char_u *buffer;
10696 int cnt = 0;
10697 int i;
10698
10699 /* Create the command: lop off the backticks. */
10700 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10701 if (cmd == NULL)
10702 return 0;
10703
10704#ifdef FEAT_EVAL
10705 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010706 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 else
10708#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010709 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010710 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 vim_free(cmd);
10712 if (buffer == NULL)
10713 return 0;
10714
10715 cmd = buffer;
10716 while (*cmd != NUL)
10717 {
10718 cmd = skipwhite(cmd); /* skip over white space */
10719 p = cmd;
10720 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10721 ++p;
10722 /* add an entry if it is not empty */
10723 if (p > cmd)
10724 {
10725 i = *p;
10726 *p = NUL;
10727 addfile(gap, cmd, flags);
10728 *p = i;
10729 ++cnt;
10730 }
10731 cmd = p;
10732 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10733 ++cmd;
10734 }
10735
10736 vim_free(buffer);
10737 return cnt;
10738}
10739# endif /* VIM_BACKTICK */
10740
10741/*
10742 * Add a file to a file list. Accepted flags:
10743 * EW_DIR add directories
10744 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010745 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 * EW_NOTFOUND add even when it doesn't exist
10747 * EW_ADDSLASH add slash after directory name
10748 */
10749 void
10750addfile(gap, f, flags)
10751 garray_T *gap;
10752 char_u *f; /* filename */
10753 int flags;
10754{
10755 char_u *p;
10756 int isdir;
10757
10758 /* if the file/dir doesn't exist, may not add it */
10759 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10760 return;
10761
10762#ifdef FNAME_ILLEGAL
10763 /* if the file/dir contains illegal characters, don't add it */
10764 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10765 return;
10766#endif
10767
10768 isdir = mch_isdir(f);
10769 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10770 return;
10771
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010772 /* If the file isn't executable, may not add it. Do accept directories. */
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010773 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f, NULL))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010774 return;
10775
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 /* Make room for another item in the file list. */
10777 if (ga_grow(gap, 1) == FAIL)
10778 return;
10779
10780 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10781 if (p == NULL)
10782 return;
10783
10784 STRCPY(p, f);
10785#ifdef BACKSLASH_IN_FILENAME
10786 slash_adjust(p);
10787#endif
10788 /*
10789 * Append a slash or backslash after directory names if none is present.
10790 */
10791#ifndef DONT_ADD_PATHSEP_TO_DIR
10792 if (isdir && (flags & EW_ADDSLASH))
10793 add_pathsep(p);
10794#endif
10795 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796}
10797#endif /* !NO_EXPANDPATH */
10798
10799#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10800
10801#ifndef SEEK_SET
10802# define SEEK_SET 0
10803#endif
10804#ifndef SEEK_END
10805# define SEEK_END 2
10806#endif
10807
10808/*
10809 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010810 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
10811 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 * Returns an allocated string, or NULL for error.
10813 */
10814 char_u *
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010815get_cmd_output(cmd, infile, flags, ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010817 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 int flags; /* can be SHELL_SILENT */
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010819 int *ret_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821 char_u *tempname;
10822 char_u *command;
10823 char_u *buffer = NULL;
10824 int len;
10825 int i = 0;
10826 FILE *fd;
10827
10828 if (check_restricted() || check_secure())
10829 return NULL;
10830
10831 /* get a name for the temp file */
10832 if ((tempname = vim_tempname('o')) == NULL)
10833 {
10834 EMSG(_(e_notmp));
10835 return NULL;
10836 }
10837
10838 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010839 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 if (command == NULL)
10841 goto done;
10842
10843 /*
10844 * Call the shell to execute the command (errors are ignored).
10845 * Don't check timestamps here.
10846 */
10847 ++no_check_timestamps;
10848 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10849 --no_check_timestamps;
10850
10851 vim_free(command);
10852
10853 /*
10854 * read the names from the file into memory
10855 */
10856# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010857 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 fd = mch_fopen((char *)tempname, "r");
10859# else
10860 fd = mch_fopen((char *)tempname, READBIN);
10861# endif
10862
10863 if (fd == NULL)
10864 {
10865 EMSG2(_(e_notopen), tempname);
10866 goto done;
10867 }
10868
10869 fseek(fd, 0L, SEEK_END);
10870 len = ftell(fd); /* get size of temp file */
10871 fseek(fd, 0L, SEEK_SET);
10872
10873 buffer = alloc(len + 1);
10874 if (buffer != NULL)
10875 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10876 fclose(fd);
10877 mch_remove(tempname);
10878 if (buffer == NULL)
10879 goto done;
10880#ifdef VMS
10881 len = i; /* VMS doesn't give us what we asked for... */
10882#endif
10883 if (i != len)
10884 {
10885 EMSG2(_(e_notread), tempname);
10886 vim_free(buffer);
10887 buffer = NULL;
10888 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010889 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010890 {
10891 /* Change NUL into SOH, otherwise the string is truncated. */
10892 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020010893 if (buffer[i] == NUL)
10894 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010895
Bram Moolenaar162bd912010-07-28 22:29:10 +020010896 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010897 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010898 else
10899 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900
10901done:
10902 vim_free(tempname);
10903 return buffer;
10904}
10905#endif
10906
10907/*
10908 * Free the list of files returned by expand_wildcards() or other expansion
10909 * functions.
10910 */
10911 void
10912FreeWild(count, files)
10913 int count;
10914 char_u **files;
10915{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010916 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 return;
10918#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10919 /*
10920 * Is this still OK for when other functions than expand_wildcards() have
10921 * been used???
10922 */
10923 _fnexplodefree((char **)files);
10924#else
10925 while (count--)
10926 vim_free(files[count]);
10927 vim_free(files);
10928#endif
10929}
10930
10931/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010932 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 * Don't do this when still processing a command or a mapping.
10934 * Don't do this when inside a ":normal" command.
10935 */
10936 int
10937goto_im()
10938{
10939 return (p_im && stuff_empty() && typebuf_typed());
10940}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010941
10942/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020010943 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010944 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
10945 * - Remove any argument. E.g., "csh -f" -> "csh".
10946 * But don't allow a space in the path, so that this works:
10947 * "/usr/bin/csh --rcfile ~/.cshrc"
10948 * But don't do that for Windows, it's common to have a space in the path.
10949 */
10950 char_u *
10951get_isolated_shell_name()
10952{
10953 char_u *p;
10954
10955#ifdef WIN3264
10956 p = gettail(p_sh);
10957 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
10958#else
10959 p = skiptowhite(p_sh);
10960 if (*p == NUL)
10961 {
10962 /* No white space, use the tail. */
10963 p = vim_strsave(gettail(p_sh));
10964 }
10965 else
10966 {
10967 char_u *p1, *p2;
10968
10969 /* Find the last path separator before the space. */
10970 p1 = p_sh;
10971 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
10972 if (vim_ispathsep(*p2))
10973 p1 = p2 + 1;
10974 p = vim_strnsave(p1, (int)(p - p1));
10975 }
10976#endif
10977 return p;
10978}