blob: d43212538536aa52197dace555ba5d7ab625d6cc [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 Moolenaar92b8b2d2016-01-29 22:36:45 +010017static char_u *vim_version_dir(char_u *vimdir);
18static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
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
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
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
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
Bram Moolenaar597a4222014-06-25 14:39:50 +020044 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47#if defined(FEAT_FOLDING) || defined(PROTO)
48/*
49 * Count the size (in window cells) of the indent in line "lnum" of buffer
50 * "buf".
51 */
52 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010053get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054{
Bram Moolenaar597a4222014-06-25 14:39:50 +020055 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000056}
57#endif
58
59/*
60 * count the size (in window cells) of the indent in line "ptr", with
61 * 'tabstop' at "ts"
62 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010064get_indent_str(
65 char_u *ptr,
66 int ts,
67 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
69 int count = 0;
70
71 for ( ; *ptr; ++ptr)
72 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 if (*ptr == TAB)
74 {
75 if (!list || lcs_tab1) /* count a tab for what it is worth */
76 count += ts - (count % ts);
77 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020078 /* In list mode, when tab is not set, count screen char width
79 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020080 count += ptr2cells(ptr);
81 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else if (*ptr == ' ')
83 ++count; /* count a space for one */
84 else
85 break;
86 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000087 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000088}
89
90/*
91 * Set the indent of the current line.
92 * Leaves the cursor on the first non-blank in the line.
93 * Caller must take care of undo.
94 * "flags":
95 * SIN_CHANGED: call changed_bytes() if the line was changed.
96 * SIN_INSERT: insert the indent in front of the line.
97 * SIN_UNDO: save line for undo before changing it.
98 * Returns TRUE if the line was changed.
99 */
100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100101set_indent(
102 int size, /* measured in spaces */
103 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
105 char_u *p;
106 char_u *newline;
107 char_u *oldline;
108 char_u *s;
109 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000110 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 int line_len;
112 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000115 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000116 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000117 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119 /*
120 * First check if there is anything to do and compute the number of
121 * characters needed for the indent.
122 */
123 todo = size;
124 ind_len = 0;
125 p = oldline = ml_get_curline();
126
127 /* Calculate the buffer size for the new indent, and check to see if it
128 * isn't already set */
129
Bram Moolenaar5002c292007-07-24 13:26:15 +0000130 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
131 * 'preserveindent' are set count the number of characters at the
132 * beginning of the line to be copied */
133 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 /* If 'preserveindent' is set then reuse as much as possible of
136 * the existing indent structure for the new indent */
137 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
138 {
139 ind_done = 0;
140
141 /* count as many characters as we can use */
142 while (todo > 0 && vim_iswhite(*p))
143 {
144 if (*p == TAB)
145 {
146 tab_pad = (int)curbuf->b_p_ts
147 - (ind_done % (int)curbuf->b_p_ts);
148 /* stop if this tab will overshoot the target */
149 if (todo < tab_pad)
150 break;
151 todo -= tab_pad;
152 ++ind_len;
153 ind_done += tab_pad;
154 }
155 else
156 {
157 --todo;
158 ++ind_len;
159 ++ind_done;
160 }
161 ++p;
162 }
163
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 /* Set initial number of whitespace chars to copy if we are
165 * preserving indent but expandtab is set */
166 if (curbuf->b_p_et)
167 orig_char_len = ind_len;
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /* Fill to next tabstop with a tab, if possible */
170 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000171 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 doit = TRUE;
174 todo -= tab_pad;
175 ++ind_len;
176 /* ind_done += tab_pad; */
177 }
178 }
179
180 /* count tabs required for indent */
181 while (todo >= (int)curbuf->b_p_ts)
182 {
183 if (*p != TAB)
184 doit = TRUE;
185 else
186 ++p;
187 todo -= (int)curbuf->b_p_ts;
188 ++ind_len;
189 /* ind_done += (int)curbuf->b_p_ts; */
190 }
191 }
192 /* count spaces required for indent */
193 while (todo > 0)
194 {
195 if (*p != ' ')
196 doit = TRUE;
197 else
198 ++p;
199 --todo;
200 ++ind_len;
201 /* ++ind_done; */
202 }
203
204 /* Return if the indent is OK already. */
205 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
206 return FALSE;
207
208 /* Allocate memory for the new line. */
209 if (flags & SIN_INSERT)
210 p = oldline;
211 else
212 p = skipwhite(p);
213 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000214
215 /* If 'preserveindent' and 'expandtab' are both set keep the original
216 * characters and allocate accordingly. We will fill the rest with spaces
217 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000218 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000219 {
220 newline = alloc(orig_char_len + size - ind_done + line_len);
221 if (newline == NULL)
222 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000223 todo = size - ind_done;
224 ind_len = orig_char_len + todo; /* Set total length of indent in
225 * characters, which may have been
226 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000227 p = oldline;
228 s = newline;
229 while (orig_char_len > 0)
230 {
231 *s++ = *p++;
232 orig_char_len--;
233 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000234
Bram Moolenaar5002c292007-07-24 13:26:15 +0000235 /* Skip over any additional white space (useful when newindent is less
236 * than old) */
237 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000238 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000239
Bram Moolenaar5002c292007-07-24 13:26:15 +0000240 }
241 else
242 {
243 todo = size;
244 newline = alloc(ind_len + line_len);
245 if (newline == NULL)
246 return FALSE;
247 s = newline;
248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* if 'expandtab' isn't set: use TABs */
252 if (!curbuf->b_p_et)
253 {
254 /* If 'preserveindent' is set then reuse as much as possible of
255 * the existing indent structure for the new indent */
256 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
257 {
258 p = oldline;
259 ind_done = 0;
260
261 while (todo > 0 && vim_iswhite(*p))
262 {
263 if (*p == TAB)
264 {
265 tab_pad = (int)curbuf->b_p_ts
266 - (ind_done % (int)curbuf->b_p_ts);
267 /* stop if this tab will overshoot the target */
268 if (todo < tab_pad)
269 break;
270 todo -= tab_pad;
271 ind_done += tab_pad;
272 }
273 else
274 {
275 --todo;
276 ++ind_done;
277 }
278 *s++ = *p++;
279 }
280
281 /* Fill to next tabstop with a tab, if possible */
282 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
283 if (todo >= tab_pad)
284 {
285 *s++ = TAB;
286 todo -= tab_pad;
287 }
288
289 p = skipwhite(p);
290 }
291
292 while (todo >= (int)curbuf->b_p_ts)
293 {
294 *s++ = TAB;
295 todo -= (int)curbuf->b_p_ts;
296 }
297 }
298 while (todo > 0)
299 {
300 *s++ = ' ';
301 --todo;
302 }
303 mch_memmove(s, p, (size_t)line_len);
304
305 /* Replace the line (unless undo fails). */
306 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
307 {
308 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
309 if (flags & SIN_CHANGED)
310 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200311 /* Correct saved cursor position if it is in this line. */
312 if (saved_cursor.lnum == curwin->w_cursor.lnum)
313 {
314 if (saved_cursor.col >= (colnr_T)(p - oldline))
315 /* cursor was after the indent, adjust for the number of
316 * bytes added/removed */
317 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
318 else if (saved_cursor.col >= (colnr_T)(s - newline))
319 /* cursor was in the indent, and is now after it, put it back
320 * at the start of the indent (replacing spaces with TAB) */
321 saved_cursor.col = (colnr_T)(s - newline);
322 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000323 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 else
326 vim_free(newline);
327
328 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000329 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
332/*
333 * Copy the indent from ptr to the current line (and fill to size)
334 * Leaves the cursor on the first non-blank in the line.
335 * Returns TRUE if the line was changed.
336 */
337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 char_u *p = NULL;
341 char_u *line = NULL;
342 char_u *s;
343 int todo;
344 int ind_len;
345 int line_len = 0;
346 int tab_pad;
347 int ind_done;
348 int round;
349
350 /* Round 1: compute the number of characters needed for the indent
351 * Round 2: copy the characters. */
352 for (round = 1; round <= 2; ++round)
353 {
354 todo = size;
355 ind_len = 0;
356 ind_done = 0;
357 s = src;
358
359 /* Count/copy the usable portion of the source line */
360 while (todo > 0 && vim_iswhite(*s))
361 {
362 if (*s == TAB)
363 {
364 tab_pad = (int)curbuf->b_p_ts
365 - (ind_done % (int)curbuf->b_p_ts);
366 /* Stop if this tab will overshoot the target */
367 if (todo < tab_pad)
368 break;
369 todo -= tab_pad;
370 ind_done += tab_pad;
371 }
372 else
373 {
374 --todo;
375 ++ind_done;
376 }
377 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000378 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 *p++ = *s;
380 ++s;
381 }
382
383 /* Fill to next tabstop with a tab, if possible */
384 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200385 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
387 todo -= tab_pad;
388 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000389 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 *p++ = TAB;
391 }
392
393 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200394 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
396 todo -= (int)curbuf->b_p_ts;
397 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000398 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 *p++ = TAB;
400 }
401
402 /* Count/add spaces required for indent */
403 while (todo > 0)
404 {
405 --todo;
406 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000407 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 *p++ = ' ';
409 }
410
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
413 /* Allocate memory for the result: the copied indent, new indent
414 * and the rest of the line. */
415 line_len = (int)STRLEN(ml_get_curline()) + 1;
416 line = alloc(ind_len + line_len);
417 if (line == NULL)
418 return FALSE;
419 p = line;
420 }
421 }
422
423 /* Append the original line */
424 mch_memmove(p, ml_get_curline(), (size_t)line_len);
425
426 /* Replace the line */
427 ml_replace(curwin->w_cursor.lnum, line, FALSE);
428
429 /* Put the cursor after the indent. */
430 curwin->w_cursor.col = ind_len;
431 return TRUE;
432}
433
434/*
435 * Return the indent of the current line after a number. Return -1 if no
436 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000437 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 colnr_T col;
443 pos_T pos;
444
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200445 regmatch_T regmatch;
446 int lead_len = 0; /* length of comment leader */
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if (lnum > curbuf->b_ml.ml_line_count)
449 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000450 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200451
452#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200453 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
454 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
458 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 /* vim_regexec() expects a pointer to a line. This lets us
463 * start matching for the flp beyond any comment leader... */
464 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 pos.lnum = lnum;
467 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200469 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200472 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000474
475 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 getvcol(curwin, &pos, &col, NULL, NULL);
478 return (int)col;
479}
480
Bram Moolenaar597a4222014-06-25 14:39:50 +0200481#if defined(FEAT_LINEBREAK) || defined(PROTO)
482/*
483 * Return appropriate space number for breakindent, taking influencing
484 * parameters into account. Window must be specified, since it is not
485 * necessarily always the current one.
486 */
487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488get_breakindent_win(
489 win_T *wp,
490 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200491{
492 static int prev_indent = 0; /* cached indent value */
493 static long prev_ts = 0L; /* cached tabstop value */
494 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200495 static int prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496 int bri = 0;
497 /* window width minus window margin space, i.e. what rests for text */
498 const int eff_wwidth = W_WIDTH(wp)
499 - ((wp->w_p_nu || wp->w_p_rnu)
500 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
501 ? number_width(wp) + 1 : 0);
502
503 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200504 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
505 || prev_tick != wp->w_buffer->b_changedtick)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 {
507 prev_line = line;
508 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaara40aa762014-06-25 22:55:38 +0200509 prev_tick = wp->w_buffer->b_changedtick;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200511 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200513 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514
515 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 if (wp->w_p_brisbr)
517 bri -= vim_strsize(p_sbr);
518
519 /* Add offset for number column, if 'n' is in 'cpoptions' */
520 bri += win_col_off2(wp);
521
522 /* never indent past left window margin */
523 if (bri < 0)
524 bri = 0;
525 /* always leave at least bri_min characters on the left,
526 * if text width is sufficient */
527 else if (bri > eff_wwidth - wp->w_p_brimin)
528 bri = (eff_wwidth - wp->w_p_brimin < 0)
529 ? 0 : eff_wwidth - wp->w_p_brimin;
530
531 return bri;
532}
533#endif
534
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
537
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100538static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540/*
541 * Return TRUE if the string "line" starts with a word from 'cinwords'.
542 */
543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 char_u *cinw;
547 char_u *cinw_buf;
548 int cinw_len;
549 int retval = FALSE;
550 int len;
551
552 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
553 cinw_buf = alloc((unsigned)cinw_len);
554 if (cinw_buf != NULL)
555 {
556 line = skipwhite(line);
557 for (cinw = curbuf->b_p_cinw; *cinw; )
558 {
559 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
560 if (STRNCMP(line, cinw_buf, len) == 0
561 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
562 {
563 retval = TRUE;
564 break;
565 }
566 }
567 vim_free(cinw_buf);
568 }
569 return retval;
570}
571#endif
572
573/*
574 * open_line: Add a new line below or above the current line.
575 *
576 * For VREPLACE mode, we only add a new line when we get to the end of the
577 * file, otherwise we just start replacing the next line.
578 *
579 * Caller must take care of undo. Since VREPLACE may affect any number of
580 * lines however, it may call u_save_cursor() again when starting to change a
581 * new line.
582 * "flags": OPENLINE_DELSPACES delete spaces after cursor
583 * OPENLINE_DO_COM format comments
584 * OPENLINE_KEEPTRAIL keep trailing spaces
585 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200586 * OPENLINE_COM_LIST format comments with list or 2nd line indent
587 *
588 * "second_line_indent": indent for after ^^D in Insert mode or if flag
589 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 *
591 * Return TRUE for success, FALSE for failure
592 */
593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594open_line(
595 int dir, /* FORWARD or BACKWARD */
596 int flags,
597 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 char_u *saved_line; /* copy of the original line */
600 char_u *next_line = NULL; /* copy of the next line */
601 char_u *p_extra = NULL; /* what goes to next line */
602 int less_cols = 0; /* less columns for mark in new line */
603 int less_cols_off = 0; /* columns to skip for mark adjust */
604 pos_T old_cursor; /* old cursor position */
605 int newcol = 0; /* new cursor column */
606 int newindent = 0; /* auto-indent of the new line */
607 int n;
608 int trunc_line = FALSE; /* truncate current line afterwards */
609 int retval = FALSE; /* return value, default is FAIL */
610#ifdef FEAT_COMMENTS
611 int extra_len = 0; /* length of p_extra string */
612 int lead_len; /* length of comment leader */
613 char_u *lead_flags; /* position in 'comments' for comment leader */
614 char_u *leader = NULL; /* copy of comment leader */
615#endif
616 char_u *allocated = NULL; /* allocated memory */
617#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
618 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
619 char_u *p;
620#endif
621 int saved_char = NUL; /* init for GCC */
622#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
623 pos_T *pos;
624#endif
625#ifdef FEAT_SMARTINDENT
626 int do_si = (!p_paste && curbuf->b_p_si
627# ifdef FEAT_CINDENT
628 && !curbuf->b_p_cin
629# endif
630 );
631 int no_si = FALSE; /* reset did_si afterwards */
632 int first_char = NUL; /* init for GCC */
633#endif
634#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
635 int vreplace_mode;
636#endif
637 int did_append; /* appended a new line */
638 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
639
640 /*
641 * make a copy of the current line so we can mess with it
642 */
643 saved_line = vim_strsave(ml_get_curline());
644 if (saved_line == NULL) /* out of memory! */
645 return FALSE;
646
647#ifdef FEAT_VREPLACE
648 if (State & VREPLACE_FLAG)
649 {
650 /*
651 * With VREPLACE we make a copy of the next line, which we will be
652 * starting to replace. First make the new line empty and let vim play
653 * with the indenting and comment leader to its heart's content. Then
654 * we grab what it ended up putting on the new line, put back the
655 * original line, and call ins_char() to put each new character onto
656 * the line, replacing what was there before and pushing the right
657 * stuff onto the replace stack. -- webb.
658 */
659 if (curwin->w_cursor.lnum < orig_line_count)
660 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
661 else
662 next_line = vim_strsave((char_u *)"");
663 if (next_line == NULL) /* out of memory! */
664 goto theend;
665
666 /*
667 * In VREPLACE mode, a NL replaces the rest of the line, and starts
668 * replacing the next line, so push all of the characters left on the
669 * line onto the replace stack. We'll push any other characters that
670 * might be replaced at the start of the next line (due to autoindent
671 * etc) a bit later.
672 */
673 replace_push(NUL); /* Call twice because BS over NL expects it */
674 replace_push(NUL);
675 p = saved_line + curwin->w_cursor.col;
676 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000677 {
678#ifdef FEAT_MBYTE
679 if (has_mbyte)
680 p += replace_push_mb(p);
681 else
682#endif
683 replace_push(*p++);
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 saved_line[curwin->w_cursor.col] = NUL;
686 }
687#endif
688
689 if ((State & INSERT)
690#ifdef FEAT_VREPLACE
691 && !(State & VREPLACE_FLAG)
692#endif
693 )
694 {
695 p_extra = saved_line + curwin->w_cursor.col;
696#ifdef FEAT_SMARTINDENT
697 if (do_si) /* need first char after new line break */
698 {
699 p = skipwhite(p_extra);
700 first_char = *p;
701 }
702#endif
703#ifdef FEAT_COMMENTS
704 extra_len = (int)STRLEN(p_extra);
705#endif
706 saved_char = *p_extra;
707 *p_extra = NUL;
708 }
709
710 u_clearline(); /* cannot do "U" command when adding lines */
711#ifdef FEAT_SMARTINDENT
712 did_si = FALSE;
713#endif
714 ai_col = 0;
715
716 /*
717 * If we just did an auto-indent, then we didn't type anything on
718 * the prior line, and it should be truncated. Do this even if 'ai' is not
719 * set because automatically inserting a comment leader also sets did_ai.
720 */
721 if (dir == FORWARD && did_ai)
722 trunc_line = TRUE;
723
724 /*
725 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
726 * indent to use for the new line.
727 */
728 if (curbuf->b_p_ai
729#ifdef FEAT_SMARTINDENT
730 || do_si
731#endif
732 )
733 {
734 /*
735 * count white space on current line
736 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200737 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200738 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
739 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741#ifdef FEAT_SMARTINDENT
742 /*
743 * Do smart indenting.
744 * In insert/replace mode (only when dir == FORWARD)
745 * we may move some text to the next line. If it starts with '{'
746 * don't add an indent. Fixes inserting a NL before '{' in line
747 * "if (condition) {"
748 */
749 if (!trunc_line && do_si && *saved_line != NUL
750 && (p_extra == NULL || first_char != '{'))
751 {
752 char_u *ptr;
753 char_u last_char;
754
755 old_cursor = curwin->w_cursor;
756 ptr = saved_line;
757# ifdef FEAT_COMMENTS
758 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200759 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 else
761 lead_len = 0;
762# endif
763 if (dir == FORWARD)
764 {
765 /*
766 * Skip preprocessor directives, unless they are
767 * recognised as comments.
768 */
769 if (
770# ifdef FEAT_COMMENTS
771 lead_len == 0 &&
772# endif
773 ptr[0] == '#')
774 {
775 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
776 ptr = ml_get(--curwin->w_cursor.lnum);
777 newindent = get_indent();
778 }
779# ifdef FEAT_COMMENTS
780 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200781 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 else
783 lead_len = 0;
784 if (lead_len > 0)
785 {
786 /*
787 * This case gets the following right:
788 * \*
789 * * A comment (read '\' as '/').
790 * *\
791 * #define IN_THE_WAY
792 * This should line up here;
793 */
794 p = skipwhite(ptr);
795 if (p[0] == '/' && p[1] == '*')
796 p++;
797 if (p[0] == '*')
798 {
799 for (p++; *p; p++)
800 {
801 if (p[0] == '/' && p[-1] == '*')
802 {
803 /*
804 * End of C comment, indent should line up
805 * with the line containing the start of
806 * the comment
807 */
808 curwin->w_cursor.col = (colnr_T)(p - ptr);
809 if ((pos = findmatch(NULL, NUL)) != NULL)
810 {
811 curwin->w_cursor.lnum = pos->lnum;
812 newindent = get_indent();
813 }
814 }
815 }
816 }
817 }
818 else /* Not a comment line */
819# endif
820 {
821 /* Find last non-blank in line */
822 p = ptr + STRLEN(ptr) - 1;
823 while (p > ptr && vim_iswhite(*p))
824 --p;
825 last_char = *p;
826
827 /*
828 * find the character just before the '{' or ';'
829 */
830 if (last_char == '{' || last_char == ';')
831 {
832 if (p > ptr)
833 --p;
834 while (p > ptr && vim_iswhite(*p))
835 --p;
836 }
837 /*
838 * Try to catch lines that are split over multiple
839 * lines. eg:
840 * if (condition &&
841 * condition) {
842 * Should line up here!
843 * }
844 */
845 if (*p == ')')
846 {
847 curwin->w_cursor.col = (colnr_T)(p - ptr);
848 if ((pos = findmatch(NULL, '(')) != NULL)
849 {
850 curwin->w_cursor.lnum = pos->lnum;
851 newindent = get_indent();
852 ptr = ml_get_curline();
853 }
854 }
855 /*
856 * If last character is '{' do indent, without
857 * checking for "if" and the like.
858 */
859 if (last_char == '{')
860 {
861 did_si = TRUE; /* do indent */
862 no_si = TRUE; /* don't delete it when '{' typed */
863 }
864 /*
865 * Look for "if" and the like, use 'cinwords'.
866 * Don't do this if the previous line ended in ';' or
867 * '}'.
868 */
869 else if (last_char != ';' && last_char != '}'
870 && cin_is_cinword(ptr))
871 did_si = TRUE;
872 }
873 }
874 else /* dir == BACKWARD */
875 {
876 /*
877 * Skip preprocessor directives, unless they are
878 * recognised as comments.
879 */
880 if (
881# ifdef FEAT_COMMENTS
882 lead_len == 0 &&
883# endif
884 ptr[0] == '#')
885 {
886 int was_backslashed = FALSE;
887
888 while ((ptr[0] == '#' || was_backslashed) &&
889 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
890 {
891 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
892 was_backslashed = TRUE;
893 else
894 was_backslashed = FALSE;
895 ptr = ml_get(++curwin->w_cursor.lnum);
896 }
897 if (was_backslashed)
898 newindent = 0; /* Got to end of file */
899 else
900 newindent = get_indent();
901 }
902 p = skipwhite(ptr);
903 if (*p == '}') /* if line starts with '}': do indent */
904 did_si = TRUE;
905 else /* can delete indent when '{' typed */
906 can_si_back = TRUE;
907 }
908 curwin->w_cursor = old_cursor;
909 }
910 if (do_si)
911 can_si = TRUE;
912#endif /* FEAT_SMARTINDENT */
913
914 did_ai = TRUE;
915 }
916
917#ifdef FEAT_COMMENTS
918 /*
919 * Find out if the current line starts with a comment leader.
920 * This may then be inserted in front of the new line.
921 */
922 end_comment_pending = NUL;
923 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200924 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else
926 lead_len = 0;
927 if (lead_len > 0)
928 {
929 char_u *lead_repl = NULL; /* replaces comment leader */
930 int lead_repl_len = 0; /* length of *lead_repl */
931 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
932 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
933 char_u *comment_end = NULL; /* where lead_end has been found */
934 int extra_space = FALSE; /* append extra space */
935 int current_flag;
936 int require_blank = FALSE; /* requires blank after middle */
937 char_u *p2;
938
939 /*
940 * If the comment leader has the start, middle or end flag, it may not
941 * be used or may be replaced with the middle leader.
942 */
943 for (p = lead_flags; *p && *p != ':'; ++p)
944 {
945 if (*p == COM_BLANK)
946 {
947 require_blank = TRUE;
948 continue;
949 }
950 if (*p == COM_START || *p == COM_MIDDLE)
951 {
952 current_flag = *p;
953 if (*p == COM_START)
954 {
955 /*
956 * Doing "O" on a start of comment does not insert leader.
957 */
958 if (dir == BACKWARD)
959 {
960 lead_len = 0;
961 break;
962 }
963
964 /* find start of middle part */
965 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
966 require_blank = FALSE;
967 }
968
969 /*
970 * Isolate the strings of the middle and end leader.
971 */
972 while (*p && p[-1] != ':') /* find end of middle flags */
973 {
974 if (*p == COM_BLANK)
975 require_blank = TRUE;
976 ++p;
977 }
978 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
979
980 while (*p && p[-1] != ':') /* find end of end flags */
981 {
982 /* Check whether we allow automatic ending of comments */
983 if (*p == COM_AUTO_END)
984 end_comment_pending = -1; /* means we want to set it */
985 ++p;
986 }
987 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
988
989 if (end_comment_pending == -1) /* we can set it now */
990 end_comment_pending = lead_end[n - 1];
991
992 /*
993 * If the end of the comment is in the same line, don't use
994 * the comment leader.
995 */
996 if (dir == FORWARD)
997 {
998 for (p = saved_line + lead_len; *p; ++p)
999 if (STRNCMP(p, lead_end, n) == 0)
1000 {
1001 comment_end = p;
1002 lead_len = 0;
1003 break;
1004 }
1005 }
1006
1007 /*
1008 * Doing "o" on a start of comment inserts the middle leader.
1009 */
1010 if (lead_len > 0)
1011 {
1012 if (current_flag == COM_START)
1013 {
1014 lead_repl = lead_middle;
1015 lead_repl_len = (int)STRLEN(lead_middle);
1016 }
1017
1018 /*
1019 * If we have hit RETURN immediately after the start
1020 * comment leader, then put a space after the middle
1021 * comment leader on the next line.
1022 */
1023 if (!vim_iswhite(saved_line[lead_len - 1])
1024 && ((p_extra != NULL
1025 && (int)curwin->w_cursor.col == lead_len)
1026 || (p_extra == NULL
1027 && saved_line[lead_len] == NUL)
1028 || require_blank))
1029 extra_space = TRUE;
1030 }
1031 break;
1032 }
1033 if (*p == COM_END)
1034 {
1035 /*
1036 * Doing "o" on the end of a comment does not insert leader.
1037 * Remember where the end is, might want to use it to find the
1038 * start (for C-comments).
1039 */
1040 if (dir == FORWARD)
1041 {
1042 comment_end = skipwhite(saved_line);
1043 lead_len = 0;
1044 break;
1045 }
1046
1047 /*
1048 * Doing "O" on the end of a comment inserts the middle leader.
1049 * Find the string for the middle leader, searching backwards.
1050 */
1051 while (p > curbuf->b_p_com && *p != ',')
1052 --p;
1053 for (lead_repl = p; lead_repl > curbuf->b_p_com
1054 && lead_repl[-1] != ':'; --lead_repl)
1055 ;
1056 lead_repl_len = (int)(p - lead_repl);
1057
1058 /* We can probably always add an extra space when doing "O" on
1059 * the comment-end */
1060 extra_space = TRUE;
1061
1062 /* Check whether we allow automatic ending of comments */
1063 for (p2 = p; *p2 && *p2 != ':'; p2++)
1064 {
1065 if (*p2 == COM_AUTO_END)
1066 end_comment_pending = -1; /* means we want to set it */
1067 }
1068 if (end_comment_pending == -1)
1069 {
1070 /* Find last character in end-comment string */
1071 while (*p2 && *p2 != ',')
1072 p2++;
1073 end_comment_pending = p2[-1];
1074 }
1075 break;
1076 }
1077 if (*p == COM_FIRST)
1078 {
1079 /*
1080 * Comment leader for first line only: Don't repeat leader
1081 * when using "O", blank out leader when using "o".
1082 */
1083 if (dir == BACKWARD)
1084 lead_len = 0;
1085 else
1086 {
1087 lead_repl = (char_u *)"";
1088 lead_repl_len = 0;
1089 }
1090 break;
1091 }
1092 }
1093 if (lead_len)
1094 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001095 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001096 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001097 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 allocated = leader; /* remember to free it later */
1099
1100 if (leader == NULL)
1101 lead_len = 0;
1102 else
1103 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001104 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106 /*
1107 * Replace leader with lead_repl, right or left adjusted
1108 */
1109 if (lead_repl != NULL)
1110 {
1111 int c = 0;
1112 int off = 0;
1113
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001114 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001117 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else if (VIM_ISDIGIT(*p) || *p == '-')
1119 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 else
1121 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 if (c == COM_RIGHT) /* right adjusted leader */
1124 {
1125 /* find last non-white in the leader to line up with */
1126 for (p = leader + lead_len - 1; p > leader
1127 && vim_iswhite(*p); --p)
1128 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001130
1131#ifdef FEAT_MBYTE
1132 /* Compute the length of the replaced characters in
1133 * screen characters, not bytes. */
1134 {
1135 int repl_size = vim_strnsize(lead_repl,
1136 lead_repl_len);
1137 int old_size = 0;
1138 char_u *endp = p;
1139 int l;
1140
1141 while (old_size < repl_size && p > leader)
1142 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001143 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001144 old_size += ptr2cells(p);
1145 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001146 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 if (l != 0)
1148 mch_memmove(endp + l, endp,
1149 (size_t)((leader + lead_len) - endp));
1150 lead_len += l;
1151 }
1152#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (p < leader + lead_repl_len)
1154 p = leader;
1155 else
1156 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1159 if (p + lead_repl_len > leader + lead_len)
1160 p[lead_repl_len] = NUL;
1161
1162 /* blank-out any other chars from the old leader. */
1163 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001164 {
1165#ifdef FEAT_MBYTE
1166 int l = mb_head_off(leader, p);
1167
1168 if (l > 1)
1169 {
1170 p -= l;
1171 if (ptr2cells(p) > 1)
1172 {
1173 p[1] = ' ';
1174 --l;
1175 }
1176 mch_memmove(p + 1, p + l + 1,
1177 (size_t)((leader + lead_len) - (p + l + 1)));
1178 lead_len -= l;
1179 *p = ' ';
1180 }
1181 else
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (!vim_iswhite(*p))
1184 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 else /* left adjusted leader */
1188 {
1189 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001190#ifdef FEAT_MBYTE
1191 /* Compute the length of the replaced characters in
1192 * screen characters, not bytes. Move the part that is
1193 * not to be overwritten. */
1194 {
1195 int repl_size = vim_strnsize(lead_repl,
1196 lead_repl_len);
1197 int i;
1198 int l;
1199
1200 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1201 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001202 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001203 if (vim_strnsize(p, i + l) > repl_size)
1204 break;
1205 }
1206 if (i != lead_repl_len)
1207 {
1208 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001209 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001210 lead_len += lead_repl_len - i;
1211 }
1212 }
1213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1215
1216 /* Replace any remaining non-white chars in the old
1217 * leader by spaces. Keep Tabs, the indent must
1218 * remain the same. */
1219 for (p += lead_repl_len; p < leader + lead_len; ++p)
1220 if (!vim_iswhite(*p))
1221 {
1222 /* Don't put a space before a TAB. */
1223 if (p + 1 < leader + lead_len && p[1] == TAB)
1224 {
1225 --lead_len;
1226 mch_memmove(p, p + 1,
1227 (leader + lead_len) - p);
1228 }
1229 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001230 {
1231#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001232 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001233
1234 if (l > 1)
1235 {
1236 if (ptr2cells(p) > 1)
1237 {
1238 /* Replace a double-wide char with
1239 * two spaces */
1240 --l;
1241 *p++ = ' ';
1242 }
1243 mch_memmove(p + 1, p + l,
1244 (leader + lead_len) - p);
1245 lead_len -= l - 1;
1246 }
1247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 *p = NUL;
1252 }
1253
1254 /* Recompute the indent, it may have changed. */
1255 if (curbuf->b_p_ai
1256#ifdef FEAT_SMARTINDENT
1257 || do_si
1258#endif
1259 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001260 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
1262 /* Add the indent offset */
1263 if (newindent + off < 0)
1264 {
1265 off = -newindent;
1266 newindent = 0;
1267 }
1268 else
1269 newindent += off;
1270
1271 /* Correct trailing spaces for the shift, so that
1272 * alignment remains equal. */
1273 while (off > 0 && lead_len > 0
1274 && leader[lead_len - 1] == ' ')
1275 {
1276 /* Don't do it when there is a tab before the space */
1277 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1278 break;
1279 --lead_len;
1280 --off;
1281 }
1282
1283 /* If the leader ends in white space, don't add an
1284 * extra space */
1285 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1286 extra_space = FALSE;
1287 leader[lead_len] = NUL;
1288 }
1289
1290 if (extra_space)
1291 {
1292 leader[lead_len++] = ' ';
1293 leader[lead_len] = NUL;
1294 }
1295
1296 newcol = lead_len;
1297
1298 /*
1299 * if a new indent will be set below, remove the indent that
1300 * is in the comment leader
1301 */
1302 if (newindent
1303#ifdef FEAT_SMARTINDENT
1304 || did_si
1305#endif
1306 )
1307 {
1308 while (lead_len && vim_iswhite(*leader))
1309 {
1310 --lead_len;
1311 --newcol;
1312 ++leader;
1313 }
1314 }
1315
1316 }
1317#ifdef FEAT_SMARTINDENT
1318 did_si = can_si = FALSE;
1319#endif
1320 }
1321 else if (comment_end != NULL)
1322 {
1323 /*
1324 * We have finished a comment, so we don't use the leader.
1325 * If this was a C-comment and 'ai' or 'si' is set do a normal
1326 * indent to align with the line containing the start of the
1327 * comment.
1328 */
1329 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1330 (curbuf->b_p_ai
1331#ifdef FEAT_SMARTINDENT
1332 || do_si
1333#endif
1334 ))
1335 {
1336 old_cursor = curwin->w_cursor;
1337 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1338 if ((pos = findmatch(NULL, NUL)) != NULL)
1339 {
1340 curwin->w_cursor.lnum = pos->lnum;
1341 newindent = get_indent();
1342 }
1343 curwin->w_cursor = old_cursor;
1344 }
1345 }
1346 }
1347#endif
1348
1349 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1350 if (p_extra != NULL)
1351 {
1352 *p_extra = saved_char; /* restore char that NUL replaced */
1353
1354 /*
1355 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1356 * non-blank.
1357 *
1358 * When in REPLACE mode, put the deleted blanks on the replace stack,
1359 * preceded by a NUL, so they can be put back when a BS is entered.
1360 */
1361 if (REPLACE_NORMAL(State))
1362 replace_push(NUL); /* end of extra blanks */
1363 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1364 {
1365 while ((*p_extra == ' ' || *p_extra == '\t')
1366#ifdef FEAT_MBYTE
1367 && (!enc_utf8
1368 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1369#endif
1370 )
1371 {
1372 if (REPLACE_NORMAL(State))
1373 replace_push(*p_extra);
1374 ++p_extra;
1375 ++less_cols_off;
1376 }
1377 }
1378 if (*p_extra != NUL)
1379 did_ai = FALSE; /* append some text, don't truncate now */
1380
1381 /* columns for marks adjusted for removed columns */
1382 less_cols = (int)(p_extra - saved_line);
1383 }
1384
1385 if (p_extra == NULL)
1386 p_extra = (char_u *)""; /* append empty line */
1387
1388#ifdef FEAT_COMMENTS
1389 /* concatenate leader and p_extra, if there is a leader */
1390 if (lead_len)
1391 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001392 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1393 {
1394 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001395 int padding = second_line_indent
1396 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001397
1398 /* Here whitespace is inserted after the comment char.
1399 * Below, set_indent(newindent, SIN_INSERT) will insert the
1400 * whitespace needed before the comment char. */
1401 for (i = 0; i < padding; i++)
1402 {
1403 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001404 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001405 newcol++;
1406 }
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 STRCAT(leader, p_extra);
1409 p_extra = leader;
1410 did_ai = TRUE; /* So truncating blanks works with comments */
1411 less_cols -= lead_len;
1412 }
1413 else
1414 end_comment_pending = NUL; /* turns out there was no leader */
1415#endif
1416
1417 old_cursor = curwin->w_cursor;
1418 if (dir == BACKWARD)
1419 --curwin->w_cursor.lnum;
1420#ifdef FEAT_VREPLACE
1421 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1422#endif
1423 {
1424 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1425 == FAIL)
1426 goto theend;
1427 /* Postpone calling changed_lines(), because it would mess up folding
1428 * with markers. */
1429 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1430 did_append = TRUE;
1431 }
1432#ifdef FEAT_VREPLACE
1433 else
1434 {
1435 /*
1436 * In VREPLACE mode we are starting to replace the next line.
1437 */
1438 curwin->w_cursor.lnum++;
1439 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1440 {
1441 /* In case we NL to a new line, BS to the previous one, and NL
1442 * again, we don't want to save the new line for undo twice.
1443 */
1444 (void)u_save_cursor(); /* errors are ignored! */
1445 vr_lines_changed++;
1446 }
1447 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1448 changed_bytes(curwin->w_cursor.lnum, 0);
1449 curwin->w_cursor.lnum--;
1450 did_append = FALSE;
1451 }
1452#endif
1453
1454 if (newindent
1455#ifdef FEAT_SMARTINDENT
1456 || did_si
1457#endif
1458 )
1459 {
1460 ++curwin->w_cursor.lnum;
1461#ifdef FEAT_SMARTINDENT
1462 if (did_si)
1463 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001464 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001467 newindent -= newindent % sw;
1468 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001471 /* Copy the indent */
1472 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 {
1474 (void)copy_indent(newindent, saved_line);
1475
1476 /*
1477 * Set the 'preserveindent' option so that any further screwing
1478 * with the line doesn't entirely destroy our efforts to preserve
1479 * it. It gets restored at the function end.
1480 */
1481 curbuf->b_p_pi = TRUE;
1482 }
1483 else
1484 (void)set_indent(newindent, SIN_INSERT);
1485 less_cols -= curwin->w_cursor.col;
1486
1487 ai_col = curwin->w_cursor.col;
1488
1489 /*
1490 * In REPLACE mode, for each character in the new indent, there must
1491 * be a NUL on the replace stack, for when it is deleted with BS
1492 */
1493 if (REPLACE_NORMAL(State))
1494 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1495 replace_push(NUL);
1496 newcol += curwin->w_cursor.col;
1497#ifdef FEAT_SMARTINDENT
1498 if (no_si)
1499 did_si = FALSE;
1500#endif
1501 }
1502
1503#ifdef FEAT_COMMENTS
1504 /*
1505 * In REPLACE mode, for each character in the extra leader, there must be
1506 * a NUL on the replace stack, for when it is deleted with BS.
1507 */
1508 if (REPLACE_NORMAL(State))
1509 while (lead_len-- > 0)
1510 replace_push(NUL);
1511#endif
1512
1513 curwin->w_cursor = old_cursor;
1514
1515 if (dir == FORWARD)
1516 {
1517 if (trunc_line || (State & INSERT))
1518 {
1519 /* truncate current line at cursor */
1520 saved_line[curwin->w_cursor.col] = NUL;
1521 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1522 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1523 truncate_spaces(saved_line);
1524 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1525 saved_line = NULL;
1526 if (did_append)
1527 {
1528 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1529 curwin->w_cursor.lnum + 1, 1L);
1530 did_append = FALSE;
1531
1532 /* Move marks after the line break to the new line. */
1533 if (flags & OPENLINE_MARKFIX)
1534 mark_col_adjust(curwin->w_cursor.lnum,
1535 curwin->w_cursor.col + less_cols_off,
1536 1L, (long)-less_cols);
1537 }
1538 else
1539 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1540 }
1541
1542 /*
1543 * Put the cursor on the new line. Careful: the scrollup() above may
1544 * have moved w_cursor, we must use old_cursor.
1545 */
1546 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1547 }
1548 if (did_append)
1549 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1550
1551 curwin->w_cursor.col = newcol;
1552#ifdef FEAT_VIRTUALEDIT
1553 curwin->w_cursor.coladd = 0;
1554#endif
1555
1556#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1557 /*
1558 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1559 * fixthisline() from doing it (via change_indent()) by telling it we're in
1560 * normal INSERT mode.
1561 */
1562 if (State & VREPLACE_FLAG)
1563 {
1564 vreplace_mode = State; /* So we know to put things right later */
1565 State = INSERT;
1566 }
1567 else
1568 vreplace_mode = 0;
1569#endif
1570#ifdef FEAT_LISP
1571 /*
1572 * May do lisp indenting.
1573 */
1574 if (!p_paste
1575# ifdef FEAT_COMMENTS
1576 && leader == NULL
1577# endif
1578 && curbuf->b_p_lisp
1579 && curbuf->b_p_ai)
1580 {
1581 fixthisline(get_lisp_indent);
1582 p = ml_get_curline();
1583 ai_col = (colnr_T)(skipwhite(p) - p);
1584 }
1585#endif
1586#ifdef FEAT_CINDENT
1587 /*
1588 * May do indenting after opening a new line.
1589 */
1590 if (!p_paste
1591 && (curbuf->b_p_cin
1592# ifdef FEAT_EVAL
1593 || *curbuf->b_p_inde != NUL
1594# endif
1595 )
1596 && in_cinkeys(dir == FORWARD
1597 ? KEY_OPEN_FORW
1598 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1599 {
1600 do_c_expr_indent();
1601 p = ml_get_curline();
1602 ai_col = (colnr_T)(skipwhite(p) - p);
1603 }
1604#endif
1605#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1606 if (vreplace_mode != 0)
1607 State = vreplace_mode;
1608#endif
1609
1610#ifdef FEAT_VREPLACE
1611 /*
1612 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1613 * original line, and inserts the new stuff char by char, pushing old stuff
1614 * onto the replace stack (via ins_char()).
1615 */
1616 if (State & VREPLACE_FLAG)
1617 {
1618 /* Put new line in p_extra */
1619 p_extra = vim_strsave(ml_get_curline());
1620 if (p_extra == NULL)
1621 goto theend;
1622
1623 /* Put back original line */
1624 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1625
1626 /* Insert new stuff into line again */
1627 curwin->w_cursor.col = 0;
1628#ifdef FEAT_VIRTUALEDIT
1629 curwin->w_cursor.coladd = 0;
1630#endif
1631 ins_bytes(p_extra); /* will call changed_bytes() */
1632 vim_free(p_extra);
1633 next_line = NULL;
1634 }
1635#endif
1636
1637 retval = TRUE; /* success! */
1638theend:
1639 curbuf->b_p_pi = saved_pi;
1640 vim_free(saved_line);
1641 vim_free(next_line);
1642 vim_free(allocated);
1643 return retval;
1644}
1645
1646#if defined(FEAT_COMMENTS) || defined(PROTO)
1647/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001648 * get_leader_len() returns the length in bytes of the prefix of the given
1649 * string which introduces a comment. If this string is not a comment then
1650 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 * When "flags" is not NULL, it is set to point to the flags of the recognized
1652 * comment leader.
1653 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001654 * If "include_space" is set, include trailing whitespace while calculating the
1655 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
1657 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001658get_leader_len(
1659 char_u *line,
1660 char_u **flags,
1661 int backward,
1662 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
1664 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001665 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 int got_com = FALSE;
1667 int found_one;
1668 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1669 char_u *string; /* pointer to comment string */
1670 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001671 int middle_match_len = 0;
1672 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001673 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaar81340392012-06-06 16:12:59 +02001675 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 while (vim_iswhite(line[i])) /* leading white space is ignored */
1677 ++i;
1678
1679 /*
1680 * Repeat to match several nested comment strings.
1681 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001682 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 {
1684 /*
1685 * scan through the 'comments' option for a match
1686 */
1687 found_one = FALSE;
1688 for (list = curbuf->b_p_com; *list; )
1689 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001690 /* Get one option part into part_buf[]. Advance "list" to next
1691 * one. Put "string" at start of string. */
1692 if (!got_com && flags != NULL)
1693 *flags = list; /* remember where flags started */
1694 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1696 string = vim_strchr(part_buf, ':');
1697 if (string == NULL) /* missing ':', ignore this part */
1698 continue;
1699 *string++ = NUL; /* isolate flags from string */
1700
Bram Moolenaara4271d52011-05-10 13:38:27 +02001701 /* If we found a middle match previously, use that match when this
1702 * is not a middle or end. */
1703 if (middle_match_len != 0
1704 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1705 && vim_strchr(part_buf, COM_END) == NULL)
1706 break;
1707
1708 /* When we already found a nested comment, only accept further
1709 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1711 continue;
1712
Bram Moolenaara4271d52011-05-10 13:38:27 +02001713 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1715 continue;
1716
Bram Moolenaara4271d52011-05-10 13:38:27 +02001717 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 * When string starts with white space, must have some white space
1719 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (vim_iswhite(string[0]))
1722 {
1723 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001724 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 while (vim_iswhite(string[0]))
1726 ++string;
1727 }
1728 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1729 ;
1730 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001731 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaara4271d52011-05-10 13:38:27 +02001733 /* When 'b' flag used, there must be white space or an
1734 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (vim_strchr(part_buf, COM_BLANK) != NULL
1736 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1737 continue;
1738
Bram Moolenaara4271d52011-05-10 13:38:27 +02001739 /* We have found a match, stop searching unless this is a middle
1740 * comment. The middle comment can be a substring of the end
1741 * comment in which case it's better to return the length of the
1742 * end comment and its flags. Thus we keep searching with middle
1743 * and end matches and use an end match if it matches better. */
1744 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1745 {
1746 if (middle_match_len == 0)
1747 {
1748 middle_match_len = j;
1749 saved_flags = prev_list;
1750 }
1751 continue;
1752 }
1753 if (middle_match_len != 0 && j > middle_match_len)
1754 /* Use this match instead of the middle match, since it's a
1755 * longer thus better match. */
1756 middle_match_len = 0;
1757
1758 if (middle_match_len == 0)
1759 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 found_one = TRUE;
1761 break;
1762 }
1763
Bram Moolenaara4271d52011-05-10 13:38:27 +02001764 if (middle_match_len != 0)
1765 {
1766 /* Use the previously found middle match after failing to find a
1767 * match with an end. */
1768 if (!got_com && flags != NULL)
1769 *flags = saved_flags;
1770 i += middle_match_len;
1771 found_one = TRUE;
1772 }
1773
1774 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 if (!found_one)
1776 break;
1777
Bram Moolenaar81340392012-06-06 16:12:59 +02001778 result = i;
1779
Bram Moolenaara4271d52011-05-10 13:38:27 +02001780 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 while (vim_iswhite(line[i]))
1782 ++i;
1783
Bram Moolenaar81340392012-06-06 16:12:59 +02001784 if (include_space)
1785 result = i;
1786
Bram Moolenaara4271d52011-05-10 13:38:27 +02001787 /* If this comment doesn't nest, stop here. */
1788 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (vim_strchr(part_buf, COM_NEST) == NULL)
1790 break;
1791 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001792 return result;
1793}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001794
Bram Moolenaar81340392012-06-06 16:12:59 +02001795/*
1796 * Return the offset at which the last comment in line starts. If there is no
1797 * comment in the whole line, -1 is returned.
1798 *
1799 * When "flags" is not null, it is set to point to the flags describing the
1800 * recognized comment leader.
1801 */
1802 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001803get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001804{
1805 int result = -1;
1806 int i, j;
1807 int lower_check_bound = 0;
1808 char_u *string;
1809 char_u *com_leader;
1810 char_u *com_flags;
1811 char_u *list;
1812 int found_one;
1813 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1814
1815 /*
1816 * Repeat to match several nested comment strings.
1817 */
1818 i = (int)STRLEN(line);
1819 while (--i >= lower_check_bound)
1820 {
1821 /*
1822 * scan through the 'comments' option for a match
1823 */
1824 found_one = FALSE;
1825 for (list = curbuf->b_p_com; *list; )
1826 {
1827 char_u *flags_save = list;
1828
1829 /*
1830 * Get one option part into part_buf[]. Advance list to next one.
1831 * put string at start of string.
1832 */
1833 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1834 string = vim_strchr(part_buf, ':');
1835 if (string == NULL) /* If everything is fine, this cannot actually
1836 * happen. */
1837 {
1838 continue;
1839 }
1840 *string++ = NUL; /* Isolate flags from string. */
1841 com_leader = string;
1842
1843 /*
1844 * Line contents and string must match.
1845 * When string starts with white space, must have some white space
1846 * (but the amount does not need to match, there might be a mix of
1847 * TABs and spaces).
1848 */
1849 if (vim_iswhite(string[0]))
1850 {
1851 if (i == 0 || !vim_iswhite(line[i - 1]))
1852 continue;
1853 while (vim_iswhite(string[0]))
1854 ++string;
1855 }
1856 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1857 /* do nothing */;
1858 if (string[j] != NUL)
1859 continue;
1860
1861 /*
1862 * When 'b' flag used, there must be white space or an
1863 * end-of-line after the string in the line.
1864 */
1865 if (vim_strchr(part_buf, COM_BLANK) != NULL
1866 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1867 {
1868 continue;
1869 }
1870
1871 /*
1872 * We have found a match, stop searching.
1873 */
1874 found_one = TRUE;
1875
1876 if (flags)
1877 *flags = flags_save;
1878 com_flags = flags_save;
1879
1880 break;
1881 }
1882
1883 if (found_one)
1884 {
1885 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1886 int len1, len2, off;
1887
1888 result = i;
1889 /*
1890 * If this comment nests, continue searching.
1891 */
1892 if (vim_strchr(part_buf, COM_NEST) != NULL)
1893 continue;
1894
1895 lower_check_bound = i;
1896
1897 /* Let's verify whether the comment leader found is a substring
1898 * of other comment leaders. If it is, let's adjust the
1899 * lower_check_bound so that we make sure that we have determined
1900 * the comment leader correctly.
1901 */
1902
1903 while (vim_iswhite(*com_leader))
1904 ++com_leader;
1905 len1 = (int)STRLEN(com_leader);
1906
1907 for (list = curbuf->b_p_com; *list; )
1908 {
1909 char_u *flags_save = list;
1910
1911 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1912 if (flags_save == com_flags)
1913 continue;
1914 string = vim_strchr(part_buf2, ':');
1915 ++string;
1916 while (vim_iswhite(*string))
1917 ++string;
1918 len2 = (int)STRLEN(string);
1919 if (len2 == 0)
1920 continue;
1921
1922 /* Now we have to verify whether string ends with a substring
1923 * beginning the com_leader. */
1924 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1925 {
1926 --off;
1927 if (!STRNCMP(string + off, com_leader, len2 - off))
1928 {
1929 if (i - off < lower_check_bound)
1930 lower_check_bound = i - off;
1931 }
1932 }
1933 }
1934 }
1935 }
1936 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937}
1938#endif
1939
1940/*
1941 * Return the number of window lines occupied by buffer line "lnum".
1942 */
1943 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001944plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 return plines_win(curwin, lnum, TRUE);
1947}
1948
1949 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001950plines_win(
1951 win_T *wp,
1952 linenr_T lnum,
1953 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
1955#if defined(FEAT_DIFF) || defined(PROTO)
1956 /* Check for filler lines above this buffer line. When folded the result
1957 * is one line anyway. */
1958 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1959}
1960
1961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001962plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
1964 return plines_win_nofill(curwin, lnum, TRUE);
1965}
1966
1967 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001968plines_win_nofill(
1969 win_T *wp,
1970 linenr_T lnum,
1971 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
1973#endif
1974 int lines;
1975
1976 if (!wp->w_p_wrap)
1977 return 1;
1978
1979#ifdef FEAT_VERTSPLIT
1980 if (wp->w_width == 0)
1981 return 1;
1982#endif
1983
1984#ifdef FEAT_FOLDING
1985 /* A folded lines is handled just like an empty line. */
1986 /* NOTE: Caller must handle lines that are MAYBE folded. */
1987 if (lineFolded(wp, lnum) == TRUE)
1988 return 1;
1989#endif
1990
1991 lines = plines_win_nofold(wp, lnum);
1992 if (winheight > 0 && lines > wp->w_height)
1993 return (int)wp->w_height;
1994 return lines;
1995}
1996
1997/*
1998 * Return number of window lines physical line "lnum" will occupy in window
1999 * "wp". Does not care about folding, 'wrap' or 'diff'.
2000 */
2001 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002002plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
2004 char_u *s;
2005 long col;
2006 int width;
2007
2008 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2009 if (*s == NUL) /* empty line */
2010 return 1;
2011 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2012
2013 /*
2014 * If list mode is on, then the '$' at the end of the line may take up one
2015 * extra column.
2016 */
2017 if (wp->w_p_list && lcs_eol != NUL)
2018 col += 1;
2019
2020 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002021 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 */
2023 width = W_WIDTH(wp) - win_col_off(wp);
2024 if (width <= 0)
2025 return 32000;
2026 if (col <= width)
2027 return 1;
2028 col -= width;
2029 width += win_col_off2(wp);
2030 return (col + (width - 1)) / width + 1;
2031}
2032
2033/*
2034 * Like plines_win(), but only reports the number of physical screen lines
2035 * used from the start of the line to the given column number.
2036 */
2037 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002038plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
2040 long col;
2041 char_u *s;
2042 int lines = 0;
2043 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002044 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046#ifdef FEAT_DIFF
2047 /* Check for filler lines above this buffer line. When folded the result
2048 * is one line anyway. */
2049 lines = diff_check_fill(wp, lnum);
2050#endif
2051
2052 if (!wp->w_p_wrap)
2053 return lines + 1;
2054
2055#ifdef FEAT_VERTSPLIT
2056 if (wp->w_width == 0)
2057 return lines + 1;
2058#endif
2059
Bram Moolenaar597a4222014-06-25 14:39:50 +02002060 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 col = 0;
2063 while (*s != NUL && --column >= 0)
2064 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002065 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002066 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068
2069 /*
2070 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2071 * INSERT mode, then col must be adjusted so that it represents the last
2072 * screen position of the TAB. This only fixes an error when the TAB wraps
2073 * from one screen line to the next (when 'columns' is not a multiple of
2074 * 'ts') -- webb.
2075 */
2076 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002077 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
2079 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002080 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 */
2082 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002083 if (width <= 0)
2084 return 9999;
2085
2086 lines += 1;
2087 if (col > width)
2088 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2089 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090}
2091
2092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002093plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
2095 int count = 0;
2096
2097 while (first <= last)
2098 {
2099#ifdef FEAT_FOLDING
2100 int x;
2101
2102 /* Check if there are any really folded lines, but also included lines
2103 * that are maybe folded. */
2104 x = foldedCount(wp, first, NULL);
2105 if (x > 0)
2106 {
2107 ++count; /* count 1 for "+-- folded" line */
2108 first += x;
2109 }
2110 else
2111#endif
2112 {
2113#ifdef FEAT_DIFF
2114 if (first == wp->w_topline)
2115 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2116 else
2117#endif
2118 count += plines_win(wp, first, TRUE);
2119 ++first;
2120 }
2121 }
2122 return (count);
2123}
2124
2125#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2126/*
2127 * Insert string "p" at the cursor position. Stops at a NUL byte.
2128 * Handles Replace mode and multi-byte characters.
2129 */
2130 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002131ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 ins_bytes_len(p, (int)STRLEN(p));
2134}
2135#endif
2136
2137#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2138 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2139/*
2140 * Insert string "p" with length "len" at the cursor position.
2141 * Handles Replace mode and multi-byte characters.
2142 */
2143 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002144ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145{
2146 int i;
2147# ifdef FEAT_MBYTE
2148 int n;
2149
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002150 if (has_mbyte)
2151 for (i = 0; i < len; i += n)
2152 {
2153 if (enc_utf8)
2154 /* avoid reading past p[len] */
2155 n = utfc_ptr2len_len(p + i, len - i);
2156 else
2157 n = (*mb_ptr2len)(p + i);
2158 ins_char_bytes(p + i, n);
2159 }
2160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002162 for (i = 0; i < len; ++i)
2163 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164}
2165#endif
2166
2167/*
2168 * Insert or replace a single character at the cursor position.
2169 * When in REPLACE or VREPLACE mode, replace any existing character.
2170 * Caller must have prepared for undo.
2171 * For multi-byte characters we get the whole character, the caller must
2172 * convert bytes to a character.
2173 */
2174 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002175ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002178 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 int n;
2180
2181 n = (*mb_char2bytes)(c, buf);
2182
2183 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2184 * Happens for CTRL-Vu9900. */
2185 if (buf[0] == 0)
2186 buf[0] = '\n';
2187
2188 ins_char_bytes(buf, n);
2189}
2190
2191 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002192ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193{
2194 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
2196 int newlen; /* nr of bytes inserted */
2197 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2198 char_u *p;
2199 char_u *newp;
2200 char_u *oldp;
2201 int linelen; /* length of old line including NUL */
2202 colnr_T col;
2203 linenr_T lnum = curwin->w_cursor.lnum;
2204 int i;
2205
2206#ifdef FEAT_VIRTUALEDIT
2207 /* Break tabs if needed. */
2208 if (virtual_active() && curwin->w_cursor.coladd > 0)
2209 coladvance_force(getviscol());
2210#endif
2211
2212 col = curwin->w_cursor.col;
2213 oldp = ml_get(lnum);
2214 linelen = (int)STRLEN(oldp) + 1;
2215
2216 /* The lengths default to the values for when not replacing. */
2217 oldlen = 0;
2218#ifdef FEAT_MBYTE
2219 newlen = charlen;
2220#else
2221 newlen = 1;
2222#endif
2223
2224 if (State & REPLACE_FLAG)
2225 {
2226#ifdef FEAT_VREPLACE
2227 if (State & VREPLACE_FLAG)
2228 {
2229 colnr_T new_vcol = 0; /* init for GCC */
2230 colnr_T vcol;
2231 int old_list;
2232#ifndef FEAT_MBYTE
2233 char_u buf[2];
2234#endif
2235
2236 /*
2237 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2238 * Returns the old value of list, so when finished,
2239 * curwin->w_p_list should be set back to this.
2240 */
2241 old_list = curwin->w_p_list;
2242 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2243 curwin->w_p_list = FALSE;
2244
2245 /*
2246 * In virtual replace mode each character may replace one or more
2247 * characters (zero if it's a TAB). Count the number of bytes to
2248 * be deleted to make room for the new character, counting screen
2249 * cells. May result in adding spaces to fill a gap.
2250 */
2251 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2252#ifndef FEAT_MBYTE
2253 buf[0] = c;
2254 buf[1] = NUL;
2255#endif
2256 new_vcol = vcol + chartabsize(buf, vcol);
2257 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2258 {
2259 vcol += chartabsize(oldp + col + oldlen, vcol);
2260 /* Don't need to remove a TAB that takes us to the right
2261 * position. */
2262 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2263 break;
2264#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002265 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#else
2267 ++oldlen;
2268#endif
2269 /* Deleted a bit too much, insert spaces. */
2270 if (vcol > new_vcol)
2271 newlen += vcol - new_vcol;
2272 }
2273 curwin->w_p_list = old_list;
2274 }
2275 else
2276#endif
2277 if (oldp[col] != NUL)
2278 {
2279 /* normal replace */
2280#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002281 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#else
2283 oldlen = 1;
2284#endif
2285 }
2286
2287
2288 /* Push the replaced bytes onto the replace stack, so that they can be
2289 * put back when BS is used. The bytes of a multi-byte character are
2290 * done the other way around, so that the first byte is popped off
2291 * first (it tells the byte length of the character). */
2292 replace_push(NUL);
2293 for (i = 0; i < oldlen; ++i)
2294 {
2295#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002296 if (has_mbyte)
2297 i += replace_push_mb(oldp + col + i) - 1;
2298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002300 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302 }
2303
2304 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2305 if (newp == NULL)
2306 return;
2307
2308 /* Copy bytes before the cursor. */
2309 if (col > 0)
2310 mch_memmove(newp, oldp, (size_t)col);
2311
2312 /* Copy bytes after the changed character(s). */
2313 p = newp + col;
2314 mch_memmove(p + newlen, oldp + col + oldlen,
2315 (size_t)(linelen - col - oldlen));
2316
2317 /* Insert or overwrite the new character. */
2318#ifdef FEAT_MBYTE
2319 mch_memmove(p, buf, charlen);
2320 i = charlen;
2321#else
2322 *p = c;
2323 i = 1;
2324#endif
2325
2326 /* Fill with spaces when necessary. */
2327 while (i < newlen)
2328 p[i++] = ' ';
2329
2330 /* Replace the line in the buffer. */
2331 ml_replace(lnum, newp, FALSE);
2332
2333 /* mark the buffer as changed and prepare for displaying */
2334 changed_bytes(lnum, col);
2335
2336 /*
2337 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2338 * show the match for right parens and braces.
2339 */
2340 if (p_sm && (State & INSERT)
2341 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002342#ifdef FEAT_INS_EXPAND
2343 && !ins_compl_active()
2344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002346 {
2347#ifdef FEAT_MBYTE
2348 if (has_mbyte)
2349 showmatch(mb_ptr2char(buf));
2350 else
2351#endif
2352 showmatch(c);
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355#ifdef FEAT_RIGHTLEFT
2356 if (!p_ri || (State & REPLACE_FLAG))
2357#endif
2358 {
2359 /* Normal insert: move cursor right */
2360#ifdef FEAT_MBYTE
2361 curwin->w_cursor.col += charlen;
2362#else
2363 ++curwin->w_cursor.col;
2364#endif
2365 }
2366 /*
2367 * TODO: should try to update w_row here, to avoid recomputing it later.
2368 */
2369}
2370
2371/*
2372 * Insert a string at the cursor position.
2373 * Note: Does NOT handle Replace mode.
2374 * Caller must have prepared for undo.
2375 */
2376 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002377ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 char_u *oldp, *newp;
2380 int newlen = (int)STRLEN(s);
2381 int oldlen;
2382 colnr_T col;
2383 linenr_T lnum = curwin->w_cursor.lnum;
2384
2385#ifdef FEAT_VIRTUALEDIT
2386 if (virtual_active() && curwin->w_cursor.coladd > 0)
2387 coladvance_force(getviscol());
2388#endif
2389
2390 col = curwin->w_cursor.col;
2391 oldp = ml_get(lnum);
2392 oldlen = (int)STRLEN(oldp);
2393
2394 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2395 if (newp == NULL)
2396 return;
2397 if (col > 0)
2398 mch_memmove(newp, oldp, (size_t)col);
2399 mch_memmove(newp + col, s, (size_t)newlen);
2400 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2401 ml_replace(lnum, newp, FALSE);
2402 changed_bytes(lnum, col);
2403 curwin->w_cursor.col += newlen;
2404}
2405
2406/*
2407 * Delete one character under the cursor.
2408 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2409 * Caller must have prepared for undo.
2410 *
2411 * return FAIL for failure, OK otherwise
2412 */
2413 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002414del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416#ifdef FEAT_MBYTE
2417 if (has_mbyte)
2418 {
2419 /* Make sure the cursor is at the start of a character. */
2420 mb_adjust_cursor();
2421 if (*ml_get_cursor() == NUL)
2422 return FAIL;
2423 return del_chars(1L, fixpos);
2424 }
2425#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002426 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427}
2428
2429#if defined(FEAT_MBYTE) || defined(PROTO)
2430/*
2431 * Like del_bytes(), but delete characters instead of bytes.
2432 */
2433 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002434del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436 long bytes = 0;
2437 long i;
2438 char_u *p;
2439 int l;
2440
2441 p = ml_get_cursor();
2442 for (i = 0; i < count && *p != NUL; ++i)
2443 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002444 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 bytes += l;
2446 p += l;
2447 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002448 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449}
2450#endif
2451
2452/*
2453 * Delete "count" bytes under the cursor.
2454 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2455 * Caller must have prepared for undo.
2456 *
2457 * return FAIL for failure, OK otherwise
2458 */
2459 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002460del_bytes(
2461 long count,
2462 int fixpos_arg,
2463 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 char_u *oldp, *newp;
2466 colnr_T oldlen;
2467 linenr_T lnum = curwin->w_cursor.lnum;
2468 colnr_T col = curwin->w_cursor.col;
2469 int was_alloced;
2470 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002471 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473 oldp = ml_get(lnum);
2474 oldlen = (int)STRLEN(oldp);
2475
2476 /*
2477 * Can't do anything when the cursor is on the NUL after the line.
2478 */
2479 if (col >= oldlen)
2480 return FAIL;
2481
2482#ifdef FEAT_MBYTE
2483 /* If 'delcombine' is set and deleting (less than) one character, only
2484 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002485 if (p_deco && use_delcombine && enc_utf8
2486 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002488 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 int n;
2490
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002491 (void)utfc_ptr2char(oldp + col, cc);
2492 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 /* Find the last composing char, there can be several. */
2495 n = col;
2496 do
2497 {
2498 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002499 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 n += count;
2501 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2502 fixpos = 0;
2503 }
2504 }
2505#endif
2506
2507 /*
2508 * When count is too big, reduce it.
2509 */
2510 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2511 if (movelen <= 1)
2512 {
2513 /*
2514 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002515 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2516 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002518 if (col > 0 && fixpos && restart_edit == 0
2519#ifdef FEAT_VIRTUALEDIT
2520 && (ve_flags & VE_ONEMORE) == 0
2521#endif
2522 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
2524 --curwin->w_cursor.col;
2525#ifdef FEAT_VIRTUALEDIT
2526 curwin->w_cursor.coladd = 0;
2527#endif
2528#ifdef FEAT_MBYTE
2529 if (has_mbyte)
2530 curwin->w_cursor.col -=
2531 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2532#endif
2533 }
2534 count = oldlen - col;
2535 movelen = 1;
2536 }
2537
2538 /*
2539 * If the old line has been allocated the deletion can be done in the
2540 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002541 * Can't do this when using Netbeans, because we would need to invoke
2542 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002543 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002546 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002547 was_alloced = FALSE;
2548 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002550 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (was_alloced)
2552 newp = oldp; /* use same allocated memory */
2553 else
2554 { /* need to allocate a new line */
2555 newp = alloc((unsigned)(oldlen + 1 - count));
2556 if (newp == NULL)
2557 return FAIL;
2558 mch_memmove(newp, oldp, (size_t)col);
2559 }
2560 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2561 if (!was_alloced)
2562 ml_replace(lnum, newp, FALSE);
2563
2564 /* mark the buffer as changed and prepare for displaying */
2565 changed_bytes(lnum, curwin->w_cursor.col);
2566
2567 return OK;
2568}
2569
2570/*
2571 * Delete from cursor to end of line.
2572 * Caller must have prepared for undo.
2573 *
2574 * return FAIL for failure, OK otherwise
2575 */
2576 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002577truncate_line(
2578 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580 char_u *newp;
2581 linenr_T lnum = curwin->w_cursor.lnum;
2582 colnr_T col = curwin->w_cursor.col;
2583
2584 if (col == 0)
2585 newp = vim_strsave((char_u *)"");
2586 else
2587 newp = vim_strnsave(ml_get(lnum), col);
2588
2589 if (newp == NULL)
2590 return FAIL;
2591
2592 ml_replace(lnum, newp, FALSE);
2593
2594 /* mark the buffer as changed and prepare for displaying */
2595 changed_bytes(lnum, curwin->w_cursor.col);
2596
2597 /*
2598 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2599 */
2600 if (fixpos && curwin->w_cursor.col > 0)
2601 --curwin->w_cursor.col;
2602
2603 return OK;
2604}
2605
2606/*
2607 * Delete "nlines" lines at the cursor.
2608 * Saves the lines for undo first if "undo" is TRUE.
2609 */
2610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002611del_lines(
2612 long nlines, /* number of lines to delete */
2613 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002616 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
2618 if (nlines <= 0)
2619 return;
2620
2621 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002622 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 return;
2624
2625 for (n = 0; n < nlines; )
2626 {
2627 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2628 break;
2629
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002630 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 ++n;
2632
2633 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002634 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 break;
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002638 /* Correct the cursor position before calling deleted_lines_mark(), it may
2639 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 curwin->w_cursor.col = 0;
2641 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002642
2643 /* adjust marks, mark the buffer as changed and prepare for displaying */
2644 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645}
2646
2647 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002648gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650 char_u *ptr = ml_get_pos(pos);
2651
2652#ifdef FEAT_MBYTE
2653 if (has_mbyte)
2654 return (*mb_ptr2char)(ptr);
2655#endif
2656 return (int)*ptr;
2657}
2658
2659 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002660gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662#ifdef FEAT_MBYTE
2663 if (has_mbyte)
2664 return (*mb_ptr2char)(ml_get_cursor());
2665#endif
2666 return (int)*ml_get_cursor();
2667}
2668
2669/*
2670 * Write a character at the current cursor position.
2671 * It is directly written into the block.
2672 */
2673 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002674pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
2676 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2677 + curwin->w_cursor.col) = c;
2678}
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680/*
2681 * When extra == 0: Return TRUE if the cursor is before or on the first
2682 * non-blank in the line.
2683 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2684 * the line.
2685 */
2686 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002687inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 char_u *ptr;
2690 colnr_T col;
2691
2692 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2693 ++ptr;
2694 if (col >= curwin->w_cursor.col + extra)
2695 return TRUE;
2696 else
2697 return FALSE;
2698}
2699
2700/*
2701 * Skip to next part of an option argument: Skip space and comma.
2702 */
2703 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002704skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
2706 if (*p == ',')
2707 ++p;
2708 while (*p == ' ')
2709 ++p;
2710 return p;
2711}
2712
2713/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002714 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 *
2716 * Most often called through changed_bytes() and changed_lines(), which also
2717 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002718 *
2719 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 */
2721 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002722changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2725 /* The text of the preediting area is inserted, but this doesn't
2726 * mean a change of the buffer yet. That is delayed until the
2727 * text is committed. (this means preedit becomes empty) */
2728 if (im_is_preediting() && !xim_changed_while_preediting)
2729 return;
2730 xim_changed_while_preediting = FALSE;
2731#endif
2732
2733 if (!curbuf->b_changed)
2734 {
2735 int save_msg_scroll = msg_scroll;
2736
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002737 /* Give a warning about changing a read-only file. This may also
2738 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002740
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 /* Create a swap file if that is wanted.
2742 * Don't do this for "nofile" and "nowrite" buffer types. */
2743 if (curbuf->b_may_swap
2744#ifdef FEAT_QUICKFIX
2745 && !bt_dontwrite(curbuf)
2746#endif
2747 )
2748 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002749 int save_need_wait_return = need_wait_return;
2750
2751 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 ml_open_file(curbuf);
2753
2754 /* The ml_open_file() can cause an ATTENTION message.
2755 * Wait two seconds, to make sure the user reads this unexpected
2756 * message. Since we could be anywhere, call wait_return() now,
2757 * and don't let the emsg() set msg_scroll. */
2758 if (need_wait_return && emsg_silent == 0)
2759 {
2760 out_flush();
2761 ui_delay(2000L, TRUE);
2762 wait_return(TRUE);
2763 msg_scroll = save_msg_scroll;
2764 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002765 else
2766 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002768 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002773/*
2774 * Internal part of changed(), no user interaction.
2775 */
2776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002777changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002778{
2779 curbuf->b_changed = TRUE;
2780 ml_setflags(curbuf);
2781#ifdef FEAT_WINDOWS
2782 check_status(curbuf);
2783 redraw_tabline = TRUE;
2784#endif
2785#ifdef FEAT_TITLE
2786 need_maketitle = TRUE; /* set window title later */
2787#endif
2788}
2789
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002790static void changedOneline(buf_T *buf, linenr_T lnum);
2791static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2792static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794/*
2795 * Changed bytes within a single line for the current buffer.
2796 * - marks the windows on this buffer to be redisplayed
2797 * - marks the buffer changed by calling changed()
2798 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002799 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 */
2801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002802changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002804 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002806
2807#ifdef FEAT_DIFF
2808 /* Diff highlighting in other diff windows may need to be updated too. */
2809 if (curwin->w_p_diff)
2810 {
2811 win_T *wp;
2812 linenr_T wlnum;
2813
2814 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2815 if (wp->w_p_diff && wp != curwin)
2816 {
2817 redraw_win_later(wp, VALID);
2818 wlnum = diff_lnum_win(lnum, wp);
2819 if (wlnum > 0)
2820 changedOneline(wp->w_buffer, wlnum);
2821 }
2822 }
2823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824}
2825
2826 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002827changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002829 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
2831 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002832 if (lnum < buf->b_mod_top)
2833 buf->b_mod_top = lnum;
2834 else if (lnum >= buf->b_mod_bot)
2835 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837 else
2838 {
2839 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002840 buf->b_mod_set = TRUE;
2841 buf->b_mod_top = lnum;
2842 buf->b_mod_bot = lnum + 1;
2843 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
2845}
2846
2847/*
2848 * Appended "count" lines below line "lnum" in the current buffer.
2849 * Must be called AFTER the change and after mark_adjust().
2850 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2851 */
2852 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002853appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 changed_lines(lnum + 1, 0, lnum + 1, count);
2856}
2857
2858/*
2859 * Like appended_lines(), but adjust marks first.
2860 */
2861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002862appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2865 changed_lines(lnum + 1, 0, lnum + 1, count);
2866}
2867
2868/*
2869 * Deleted "count" lines at line "lnum" in the current buffer.
2870 * Must be called AFTER the change and after mark_adjust().
2871 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2872 */
2873 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002874deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 changed_lines(lnum, 0, lnum + count, -count);
2877}
2878
2879/*
2880 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002881 * Make sure the cursor is on a valid line before calling, a GUI callback may
2882 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 */
2884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002885deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2888 changed_lines(lnum, 0, lnum + count, -count);
2889}
2890
2891/*
2892 * Changed lines for the current buffer.
2893 * Must be called AFTER the change and after mark_adjust().
2894 * - mark the buffer changed by calling changed()
2895 * - mark the windows on this buffer to be redisplayed
2896 * - invalidate cached values
2897 * "lnum" is the first line that needs displaying, "lnume" the first line
2898 * below the changed lines (BEFORE the change).
2899 * When only inserting lines, "lnum" and "lnume" are equal.
2900 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002901 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 */
2903 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002904changed_lines(
2905 linenr_T lnum, /* first line with change */
2906 colnr_T col, /* column in first line with change */
2907 linenr_T lnume, /* line below last changed line */
2908 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002910 changed_lines_buf(curbuf, lnum, lnume, xtra);
2911
2912#ifdef FEAT_DIFF
2913 if (xtra == 0 && curwin->w_p_diff)
2914 {
2915 /* When the number of lines doesn't change then mark_adjust() isn't
2916 * called and other diff buffers still need to be marked for
2917 * displaying. */
2918 win_T *wp;
2919 linenr_T wlnum;
2920
2921 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2922 if (wp->w_p_diff && wp != curwin)
2923 {
2924 redraw_win_later(wp, VALID);
2925 wlnum = diff_lnum_win(lnum, wp);
2926 if (wlnum > 0)
2927 changed_lines_buf(wp->w_buffer, wlnum,
2928 lnume - lnum + wlnum, 0L);
2929 }
2930 }
2931#endif
2932
2933 changed_common(lnum, col, lnume, xtra);
2934}
2935
2936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002937changed_lines_buf(
2938 buf_T *buf,
2939 linenr_T lnum, /* first line with change */
2940 linenr_T lnume, /* line below last changed line */
2941 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002942{
2943 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {
2945 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002946 if (lnum < buf->b_mod_top)
2947 buf->b_mod_top = lnum;
2948 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
2950 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002951 buf->b_mod_bot += xtra;
2952 if (buf->b_mod_bot < lnum)
2953 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002955 if (lnume + xtra > buf->b_mod_bot)
2956 buf->b_mod_bot = lnume + xtra;
2957 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959 else
2960 {
2961 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002962 buf->b_mod_set = TRUE;
2963 buf->b_mod_top = lnum;
2964 buf->b_mod_bot = lnume + xtra;
2965 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967}
2968
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002969/*
2970 * Common code for when a change is was made.
2971 * See changed_lines() for the arguments.
2972 * Careful: may trigger autocommands that reload the buffer.
2973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002975changed_common(
2976 linenr_T lnum,
2977 colnr_T col,
2978 linenr_T lnume,
2979 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002982#ifdef FEAT_WINDOWS
2983 tabpage_T *tp;
2984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 int i;
2986#ifdef FEAT_JUMPLIST
2987 int cols;
2988 pos_T *p;
2989 int add;
2990#endif
2991
2992 /* mark the buffer as modified */
2993 changed();
2994
2995 /* set the '. mark */
2996 if (!cmdmod.keepjumps)
2997 {
2998 curbuf->b_last_change.lnum = lnum;
2999 curbuf->b_last_change.col = col;
3000
3001#ifdef FEAT_JUMPLIST
3002 /* Create a new entry if a new undo-able change was started or we
3003 * don't have an entry yet. */
3004 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3005 {
3006 if (curbuf->b_changelistlen == 0)
3007 add = TRUE;
3008 else
3009 {
3010 /* Don't create a new entry when the line number is the same
3011 * as the last one and the column is not too far away. Avoids
3012 * creating many entries for typing "xxxxx". */
3013 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3014 if (p->lnum != lnum)
3015 add = TRUE;
3016 else
3017 {
3018 cols = comp_textwidth(FALSE);
3019 if (cols == 0)
3020 cols = 79;
3021 add = (p->col + cols < col || col + cols < p->col);
3022 }
3023 }
3024 if (add)
3025 {
3026 /* This is the first of a new sequence of undo-able changes
3027 * and it's at some distance of the last change. Use a new
3028 * position in the changelist. */
3029 curbuf->b_new_change = FALSE;
3030
3031 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3032 {
3033 /* changelist is full: remove oldest entry */
3034 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3035 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3036 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003037 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
3039 /* Correct position in changelist for other windows on
3040 * this buffer. */
3041 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3042 --wp->w_changelistidx;
3043 }
3044 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003045 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 {
3047 /* For other windows, if the position in the changelist is
3048 * at the end it stays at the end. */
3049 if (wp->w_buffer == curbuf
3050 && wp->w_changelistidx == curbuf->b_changelistlen)
3051 ++wp->w_changelistidx;
3052 }
3053 ++curbuf->b_changelistlen;
3054 }
3055 }
3056 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3057 curbuf->b_last_change;
3058 /* The current window is always after the last change, so that "g,"
3059 * takes you back to it. */
3060 curwin->w_changelistidx = curbuf->b_changelistlen;
3061#endif
3062 }
3063
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003064 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
3066 if (wp->w_buffer == curbuf)
3067 {
3068 /* Mark this window to be redrawn later. */
3069 if (wp->w_redr_type < VALID)
3070 wp->w_redr_type = VALID;
3071
3072 /* Check if a change in the buffer has invalidated the cached
3073 * values for the cursor. */
3074#ifdef FEAT_FOLDING
3075 /*
3076 * Update the folds for this window. Can't postpone this, because
3077 * a following operator might work on the whole fold: ">>dd".
3078 */
3079 foldUpdate(wp, lnum, lnume + xtra - 1);
3080
3081 /* The change may cause lines above or below the change to become
3082 * included in a fold. Set lnum/lnume to the first/last line that
3083 * might be displayed differently.
3084 * Set w_cline_folded here as an efficient way to update it when
3085 * inserting lines just above a closed fold. */
3086 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3087 if (wp->w_cursor.lnum == lnum)
3088 wp->w_cline_folded = i;
3089 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3090 if (wp->w_cursor.lnum == lnume)
3091 wp->w_cline_folded = i;
3092
3093 /* If the changed line is in a range of previously folded lines,
3094 * compare with the first line in that range. */
3095 if (wp->w_cursor.lnum <= lnum)
3096 {
3097 i = find_wl_entry(wp, lnum);
3098 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3099 changed_line_abv_curs_win(wp);
3100 }
3101#endif
3102
3103 if (wp->w_cursor.lnum > lnum)
3104 changed_line_abv_curs_win(wp);
3105 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3106 changed_cline_bef_curs_win(wp);
3107 if (wp->w_botline >= lnum)
3108 {
3109 /* Assume that botline doesn't change (inserted lines make
3110 * other lines scroll down below botline). */
3111 approximate_botline_win(wp);
3112 }
3113
3114 /* Check if any w_lines[] entries have become invalid.
3115 * For entries below the change: Correct the lnums for
3116 * inserted/deleted lines. Makes it possible to stop displaying
3117 * after the change. */
3118 for (i = 0; i < wp->w_lines_valid; ++i)
3119 if (wp->w_lines[i].wl_valid)
3120 {
3121 if (wp->w_lines[i].wl_lnum >= lnum)
3122 {
3123 if (wp->w_lines[i].wl_lnum < lnume)
3124 {
3125 /* line included in change */
3126 wp->w_lines[i].wl_valid = FALSE;
3127 }
3128 else if (xtra != 0)
3129 {
3130 /* line below change */
3131 wp->w_lines[i].wl_lnum += xtra;
3132#ifdef FEAT_FOLDING
3133 wp->w_lines[i].wl_lastlnum += xtra;
3134#endif
3135 }
3136 }
3137#ifdef FEAT_FOLDING
3138 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3139 {
3140 /* change somewhere inside this range of folded lines,
3141 * may need to be redrawn */
3142 wp->w_lines[i].wl_valid = FALSE;
3143 }
3144#endif
3145 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003146
3147#ifdef FEAT_FOLDING
3148 /* Take care of side effects for setting w_topline when folds have
3149 * changed. Esp. when the buffer was changed in another window. */
3150 if (hasAnyFolding(wp))
3151 set_topline(wp, wp->w_topline);
3152#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003153 /* relative numbering may require updating more */
3154 if (wp->w_p_rnu)
3155 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157 }
3158
3159 /* Call update_screen() later, which checks out what needs to be redrawn,
3160 * since it notices b_mod_set and then uses b_mod_*. */
3161 if (must_redraw < VALID)
3162 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003163
3164#ifdef FEAT_AUTOCMD
3165 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003166 if (lnum <= curwin->w_cursor.lnum
3167 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003168 last_cursormoved.lnum = 0;
3169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170}
3171
3172/*
3173 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3174 */
3175 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003176unchanged(
3177 buf_T *buf,
3178 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003180 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
3182 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003183 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (ff)
3185 save_file_ff(buf);
3186#ifdef FEAT_WINDOWS
3187 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003188 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189#endif
3190#ifdef FEAT_TITLE
3191 need_maketitle = TRUE; /* set window title later */
3192#endif
3193 }
3194 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#ifdef FEAT_NETBEANS_INTG
3196 netbeans_unmodified(buf);
3197#endif
3198}
3199
3200#if defined(FEAT_WINDOWS) || defined(PROTO)
3201/*
3202 * check_status: called when the status bars for the buffer 'buf'
3203 * need to be updated
3204 */
3205 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003206check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
3208 win_T *wp;
3209
3210 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3211 if (wp->w_buffer == buf && wp->w_status_height)
3212 {
3213 wp->w_redr_status = TRUE;
3214 if (must_redraw < VALID)
3215 must_redraw = VALID;
3216 }
3217}
3218#endif
3219
3220/*
3221 * If the file is readonly, give a warning message with the first change.
3222 * Don't do this for autocommands.
3223 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003224 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003226 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 */
3228 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003229change_warning(
3230 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 mode and 'showmode' is on */
3232{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003233 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (curbuf->b_did_warn == FALSE
3236 && curbufIsChanged() == 0
3237#ifdef FEAT_AUTOCMD
3238 && !autocmd_busy
3239#endif
3240 && curbuf->b_p_ro)
3241 {
3242#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003243 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003245 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (!curbuf->b_p_ro)
3247 return;
3248#endif
3249 /*
3250 * Do what msg() does, but with a column offset if the warning should
3251 * be after the mode message.
3252 */
3253 msg_start();
3254 if (msg_row == Rows - 1)
3255 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003256 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003257 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3258#ifdef FEAT_EVAL
3259 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 msg_clr_eos();
3262 (void)msg_end();
3263 if (msg_silent == 0 && !silent_mode)
3264 {
3265 out_flush();
3266 ui_delay(1000L, TRUE); /* give the user time to think about it */
3267 }
3268 curbuf->b_did_warn = TRUE;
3269 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3270 if (msg_row < Rows - 1)
3271 showmode();
3272 }
3273}
3274
3275/*
3276 * Ask for a reply from the user, a 'y' or a 'n'.
3277 * No other characters are accepted, the message is repeated until a valid
3278 * reply is entered or CTRL-C is hit.
3279 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3280 * from any buffers but directly from the user.
3281 *
3282 * return the 'y' or 'n'
3283 */
3284 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003285ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 int r = ' ';
3288 int save_State = State;
3289
3290 if (exiting) /* put terminal in raw mode for this question */
3291 settmode(TMODE_RAW);
3292 ++no_wait_return;
3293#ifdef USE_ON_FLY_SCROLL
3294 dont_scroll = TRUE; /* disallow scrolling here */
3295#endif
3296 State = CONFIRM; /* mouse behaves like with :confirm */
3297#ifdef FEAT_MOUSE
3298 setmouse(); /* disables mouse for xterm */
3299#endif
3300 ++no_mapping;
3301 ++allow_keys; /* no mapping here, but recognize keys */
3302
3303 while (r != 'y' && r != 'n')
3304 {
3305 /* same highlighting as for wait_return */
3306 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3307 if (direct)
3308 r = get_keystroke();
3309 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003310 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (r == Ctrl_C || r == ESC)
3312 r = 'n';
3313 msg_putchar(r); /* show what you typed */
3314 out_flush();
3315 }
3316 --no_wait_return;
3317 State = save_State;
3318#ifdef FEAT_MOUSE
3319 setmouse();
3320#endif
3321 --no_mapping;
3322 --allow_keys;
3323
3324 return r;
3325}
3326
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003327#if defined(FEAT_MOUSE) || defined(PROTO)
3328/*
3329 * Return TRUE if "c" is a mouse key.
3330 */
3331 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003332is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003333{
3334 return c == K_LEFTMOUSE
3335 || c == K_LEFTMOUSE_NM
3336 || c == K_LEFTDRAG
3337 || c == K_LEFTRELEASE
3338 || c == K_LEFTRELEASE_NM
3339 || c == K_MIDDLEMOUSE
3340 || c == K_MIDDLEDRAG
3341 || c == K_MIDDLERELEASE
3342 || c == K_RIGHTMOUSE
3343 || c == K_RIGHTDRAG
3344 || c == K_RIGHTRELEASE
3345 || c == K_MOUSEDOWN
3346 || c == K_MOUSEUP
3347 || c == K_MOUSELEFT
3348 || c == K_MOUSERIGHT
3349 || c == K_X1MOUSE
3350 || c == K_X1DRAG
3351 || c == K_X1RELEASE
3352 || c == K_X2MOUSE
3353 || c == K_X2DRAG
3354 || c == K_X2RELEASE;
3355}
3356#endif
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358/*
3359 * Get a key stroke directly from the user.
3360 * Ignores mouse clicks and scrollbar events, except a click for the left
3361 * button (used at the more prompt).
3362 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3363 * Disadvantage: typeahead is ignored.
3364 * Translates the interrupt character for unix to ESC.
3365 */
3366 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003367get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003369 char_u *buf = NULL;
3370 int buflen = 150;
3371 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int len = 0;
3373 int n;
3374 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003375 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
3377 mapped_ctrl_c = FALSE; /* mappings are not used here */
3378 for (;;)
3379 {
3380 cursor_on();
3381 out_flush();
3382
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003383 /* Leave some room for check_termcode() to insert a key code into (max
3384 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3385 * bytes. */
3386 maxlen = (buflen - 6 - len) / 3;
3387 if (buf == NULL)
3388 buf = alloc(buflen);
3389 else if (maxlen < 10)
3390 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003391 char_u *t_buf = buf;
3392
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003393 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003394 * escape sequence. */
3395 buflen += 100;
3396 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003397 if (buf == NULL)
3398 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003399 maxlen = (buflen - 6 - len) / 3;
3400 }
3401 if (buf == NULL)
3402 {
3403 do_outofmem_msg((long_u)buflen);
3404 return ESC; /* panic! */
3405 }
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003408 * terminal code to complete. */
3409 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (n > 0)
3411 {
3412 /* Replace zero and CSI by a special key code. */
3413 n = fix_input_buffer(buf + len, n, FALSE);
3414 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003415 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003417 else if (len > 0)
3418 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaar4395a712006-09-05 18:57:57 +00003420 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003421 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003422 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003424
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003425 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003426 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003427 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003428 {
3429 /* Redrawing was postponed, do it now. */
3430 update_screen(0);
3431 setcursor(); /* put cursor back where it belongs */
3432 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003433 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003434 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003435 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003437 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 continue;
3439
3440 /* Handle modifier and/or special key code. */
3441 n = buf[0];
3442 if (n == K_SPECIAL)
3443 {
3444 n = TO_SPECIAL(buf[1], buf[2]);
3445 if (buf[1] == KS_MODIFIER
3446 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003447#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003448 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003449#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003450#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 || n == K_VER_SCROLLBAR
3452 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#endif
3454 )
3455 {
3456 if (buf[1] == KS_MODIFIER)
3457 mod_mask = buf[2];
3458 len -= 3;
3459 if (len > 0)
3460 mch_memmove(buf, buf + 3, (size_t)len);
3461 continue;
3462 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465#ifdef FEAT_MBYTE
3466 if (has_mbyte)
3467 {
3468 if (MB_BYTE2LEN(n) > len)
3469 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003470 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 n = (*mb_ptr2char)(buf);
3472 }
3473#endif
3474#ifdef UNIX
3475 if (n == intr_char)
3476 n = ESC;
3477#endif
3478 break;
3479 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003480 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482 mapped_ctrl_c = save_mapped_ctrl_c;
3483 return n;
3484}
3485
3486/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003487 * Get a number from the user.
3488 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 */
3490 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003491get_number(
3492 int colon, /* allow colon to abort */
3493 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 int n = 0;
3496 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003497 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003499 if (mouse_used != NULL)
3500 *mouse_used = FALSE;
3501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 /* When not printing messages, the user won't know what to type, return a
3503 * zero (as if CR was hit). */
3504 if (msg_silent != 0)
3505 return 0;
3506
3507#ifdef USE_ON_FLY_SCROLL
3508 dont_scroll = TRUE; /* disallow scrolling here */
3509#endif
3510 ++no_mapping;
3511 ++allow_keys; /* no mapping here, but recognize keys */
3512 for (;;)
3513 {
3514 windgoto(msg_row, msg_col);
3515 c = safe_vgetc();
3516 if (VIM_ISDIGIT(c))
3517 {
3518 n = n * 10 + c - '0';
3519 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003520 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3523 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003524 if (typed > 0)
3525 {
3526 MSG_PUTS("\b \b");
3527 --typed;
3528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003531#ifdef FEAT_MOUSE
3532 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3533 {
3534 *mouse_used = TRUE;
3535 n = mouse_row + 1;
3536 break;
3537 }
3538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 else if (n == 0 && c == ':' && colon)
3540 {
3541 stuffcharReadbuff(':');
3542 if (!exmode_active)
3543 cmdline_row = msg_row;
3544 skip_redraw = TRUE; /* skip redraw once */
3545 do_redraw = FALSE;
3546 break;
3547 }
3548 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3549 break;
3550 }
3551 --no_mapping;
3552 --allow_keys;
3553 return n;
3554}
3555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556/*
3557 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003558 * When "mouse_used" is not NULL allow using the mouse and in that case return
3559 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560 */
3561 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003562prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003563{
3564 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003565 int save_cmdline_row;
3566 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003567
3568 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003569 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003570 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003571 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003572 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003573
Bram Moolenaar203335e2006-09-03 14:35:42 +00003574 /* Set the state such that text can be selected/copied/pasted and we still
3575 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003576 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003577 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003578 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003579 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003580
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003581 i = get_number(TRUE, mouse_used);
3582 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003583 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003584 /* don't call wait_return() now */
3585 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003586 cmdline_row = msg_row - 1;
3587 need_wait_return = FALSE;
3588 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003589 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003590 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003591 else
3592 cmdline_row = save_cmdline_row;
3593 State = save_State;
3594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003595 return i;
3596}
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003599msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
3601 long pn;
3602
3603 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3605 return;
3606
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003607 /* We don't want to overwrite another important message, but do overwrite
3608 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3609 * then "put" reports the last action. */
3610 if (keep_msg != NULL && !keep_msg_more)
3611 return;
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (n > 0)
3614 pn = n;
3615 else
3616 pn = -n;
3617
3618 if (pn > p_report)
3619 {
3620 if (pn == 1)
3621 {
3622 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003623 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3624 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003626 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3627 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 else
3630 {
3631 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003632 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3633 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003635 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3636 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003639 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (msg(msg_buf))
3641 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003642 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003643 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 }
3645 }
3646}
3647
3648/*
3649 * flush map and typeahead buffers and give a warning for an error
3650 */
3651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003652beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 if (emsg_silent == 0)
3655 {
3656 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003657 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659}
3660
3661/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003662 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 */
3664 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003665vim_beep(
3666 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 if (emsg_silent == 0)
3669 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003670 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3671 {
3672 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003674 /* While the GUI is starting up the termcap is set for the
3675 * GUI but the output still goes to a terminal. */
3676 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003678 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003680 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003683 {
3684#ifdef MSDOS
3685 /*
3686 * The number of beeps outputted is reduced to avoid having to
3687 * wait for all the beeps to finish. This is only a problem on
3688 * systems where the beeps don't overlap.
3689 */
3690 if (beep_count == 0 || beep_count == 10)
3691 {
3692 out_char(BELL);
3693 beep_count = 1;
3694 }
3695 else
3696 ++beep_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003698 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003702
3703 /* When 'verbose' is set and we are sourcing a script or executing a
3704 * function give the user a hint where the beep comes from. */
3705 if (vim_strchr(p_debug, 'e') != NULL)
3706 {
3707 msg_source(hl_attr(HLF_W));
3708 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711}
3712
3713/*
3714 * To get the "real" home directory:
3715 * - get value of $HOME
3716 * For Unix:
3717 * - go to that directory
3718 * - do mch_dirname() to get the real name of that directory.
3719 * This also works with mounts and links.
3720 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3721 */
3722static char_u *homedir = NULL;
3723
3724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003725init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
3727 char_u *var;
3728
Bram Moolenaar05159a02005-02-26 23:04:13 +00003729 /* In case we are called a second time (when 'encoding' changes). */
3730 vim_free(homedir);
3731 homedir = NULL;
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733#ifdef VMS
3734 var = mch_getenv((char_u *)"SYS$LOGIN");
3735#else
3736 var = mch_getenv((char_u *)"HOME");
3737#endif
3738
3739 if (var != NULL && *var == NUL) /* empty is same as not set */
3740 var = NULL;
3741
3742#ifdef WIN3264
3743 /*
3744 * Weird but true: $HOME may contain an indirect reference to another
3745 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3746 * when $HOME is being set.
3747 */
3748 if (var != NULL && *var == '%')
3749 {
3750 char_u *p;
3751 char_u *exp;
3752
3753 p = vim_strchr(var + 1, '%');
3754 if (p != NULL)
3755 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003756 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 exp = mch_getenv(NameBuff);
3758 if (exp != NULL && *exp != NUL
3759 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3760 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003761 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 var = NameBuff;
3763 /* Also set $HOME, it's needed for _viminfo. */
3764 vim_setenv((char_u *)"HOME", NameBuff);
3765 }
3766 }
3767 }
3768
3769 /*
3770 * Typically, $HOME is not defined on Windows, unless the user has
3771 * specifically defined it for Vim's sake. However, on Windows NT
3772 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3773 * each user. Try constructing $HOME from these.
3774 */
3775 if (var == NULL)
3776 {
3777 char_u *homedrive, *homepath;
3778
3779 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3780 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003781 if (homepath == NULL || *homepath == NUL)
3782 homepath = "\\";
3783 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3785 {
3786 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3787 if (NameBuff[0] != NUL)
3788 {
3789 var = NameBuff;
3790 /* Also set $HOME, it's needed for _viminfo. */
3791 vim_setenv((char_u *)"HOME", NameBuff);
3792 }
3793 }
3794 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003795
3796# if defined(FEAT_MBYTE)
3797 if (enc_utf8 && var != NULL)
3798 {
3799 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003800 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003801
3802 /* Convert from active codepage to UTF-8. Other conversions are
3803 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003804 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003805 if (pp != NULL)
3806 {
3807 homedir = pp;
3808 return;
3809 }
3810 }
3811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812#endif
3813
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003814#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 /*
3816 * Default home dir is C:/
3817 * Best assumption we can make in such a situation.
3818 */
3819 if (var == NULL)
3820 var = "C:/";
3821#endif
3822 if (var != NULL)
3823 {
3824#ifdef UNIX
3825 /*
3826 * Change to the directory and get the actual path. This resolves
3827 * links. Don't do it when we can't return.
3828 */
3829 if (mch_dirname(NameBuff, MAXPATHL) == OK
3830 && mch_chdir((char *)NameBuff) == 0)
3831 {
3832 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3833 var = IObuff;
3834 if (mch_chdir((char *)NameBuff) != 0)
3835 EMSG(_(e_prev_dir));
3836 }
3837#endif
3838 homedir = vim_strsave(var);
3839 }
3840}
3841
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003842#if defined(EXITFREE) || defined(PROTO)
3843 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003844free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003845{
3846 vim_free(homedir);
3847}
Bram Moolenaar24305862012-08-15 14:05:05 +02003848
3849# ifdef FEAT_CMDL_COMPL
3850 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003851free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003852{
3853 ga_clear_strings(&ga_users);
3854}
3855# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003856#endif
3857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003859 * Call expand_env() and store the result in an allocated string.
3860 * This is not very memory efficient, this expects the result to be freed
3861 * again soon.
3862 */
3863 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003864expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003865{
3866 return expand_env_save_opt(src, FALSE);
3867}
3868
3869/*
3870 * Idem, but when "one" is TRUE handle the string as one file name, only
3871 * expand "~" at the start.
3872 */
3873 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003874expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003875{
3876 char_u *p;
3877
3878 p = alloc(MAXPATHL);
3879 if (p != NULL)
3880 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3881 return p;
3882}
3883
3884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * Expand environment variable with path name.
3886 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003887 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 * If anything fails no expansion is done and dst equals src.
3889 */
3890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003891expand_env(
3892 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3893 char_u *dst, /* where to put the result */
3894 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003896 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897}
3898
3899 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003900expand_env_esc(
3901 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3902 char_u *dst, /* where to put the result */
3903 int dstlen, /* maximum length of the result */
3904 int esc, /* escape spaces in expanded variables */
3905 int one, /* "srcp" is one file name */
3906 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003908 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 char_u *tail;
3910 int c;
3911 char_u *var;
3912 int copy_char;
3913 int mustfree; /* var was allocated, need to free it later */
3914 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003915 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003917 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003918 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003919
3920 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 --dstlen; /* leave one char space for "\," */
3922 while (*src && dstlen > 0)
3923 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003924#ifdef FEAT_EVAL
3925 /* Skip over `=expr`. */
3926 if (src[0] == '`' && src[1] == '=')
3927 {
3928 size_t len;
3929
3930 var = src;
3931 src += 2;
3932 (void)skip_expr(&src);
3933 if (*src == '`')
3934 ++src;
3935 len = src - var;
3936 if (len > (size_t)dstlen)
3937 len = dstlen;
3938 vim_strncpy(dst, var, len);
3939 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003940 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003941 continue;
3942 }
3943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003945 if ((*src == '$'
3946#ifdef VMS
3947 && at_start
3948#endif
3949 )
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003950#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 || *src == '%'
3952#endif
3953 || (*src == '~' && at_start))
3954 {
3955 mustfree = FALSE;
3956
3957 /*
3958 * The variable name is copied into dst temporarily, because it may
3959 * be a string in read-only memory and a NUL needs to be appended.
3960 */
3961 if (*src != '~') /* environment var */
3962 {
3963 tail = src + 1;
3964 var = dst;
3965 c = dstlen - 1;
3966
3967#ifdef UNIX
3968 /* Unix has ${var-name} type environment vars */
3969 if (*tail == '{' && !vim_isIDc('{'))
3970 {
3971 tail++; /* ignore '{' */
3972 while (c-- > 0 && *tail && *tail != '}')
3973 *var++ = *tail++;
3974 }
3975 else
3976#endif
3977 {
3978 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003979#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 || (*src == '%' && *tail != '%')
3981#endif
3982 ))
3983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986 }
3987
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003988#if defined(MSDOS) || defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989# ifdef UNIX
3990 if (src[1] == '{' && *tail != '}')
3991# else
3992 if (*src == '%' && *tail != '%')
3993# endif
3994 var = NULL;
3995 else
3996 {
3997# ifdef UNIX
3998 if (src[1] == '{')
3999# else
4000 if (*src == '%')
4001#endif
4002 ++tail;
4003#endif
4004 *var = NUL;
4005 var = vim_getenv(dst, &mustfree);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004006#if defined(MSDOS) || defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
4008#endif
4009 }
4010 /* home directory */
4011 else if ( src[1] == NUL
4012 || vim_ispathsep(src[1])
4013 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4014 {
4015 var = homedir;
4016 tail = src + 1;
4017 }
4018 else /* user directory */
4019 {
4020#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4021 /*
4022 * Copy ~user to dst[], so we can put a NUL after it.
4023 */
4024 tail = src;
4025 var = dst;
4026 c = dstlen - 1;
4027 while ( c-- > 0
4028 && *tail
4029 && vim_isfilec(*tail)
4030 && !vim_ispathsep(*tail))
4031 *var++ = *tail++;
4032 *var = NUL;
4033# ifdef UNIX
4034 /*
4035 * If the system supports getpwnam(), use it.
4036 * Otherwise, or if getpwnam() fails, the shell is used to
4037 * expand ~user. This is slower and may fail if the shell
4038 * does not support ~user (old versions of /bin/sh).
4039 */
4040# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4041 {
4042 struct passwd *pw;
4043
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004044 /* Note: memory allocated by getpwnam() is never freed.
4045 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 pw = getpwnam((char *)dst + 1);
4047 if (pw != NULL)
4048 var = (char_u *)pw->pw_dir;
4049 else
4050 var = NULL;
4051 }
4052 if (var == NULL)
4053# endif
4054 {
4055 expand_T xpc;
4056
4057 ExpandInit(&xpc);
4058 xpc.xp_context = EXPAND_FILES;
4059 var = ExpandOne(&xpc, dst, NULL,
4060 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 mustfree = TRUE;
4062 }
4063
4064# else /* !UNIX, thus VMS */
4065 /*
4066 * USER_HOME is a comma-separated list of
4067 * directories to search for the user account in.
4068 */
4069 {
4070 char_u test[MAXPATHL], paths[MAXPATHL];
4071 char_u *path, *next_path, *ptr;
4072 struct stat st;
4073
4074 STRCPY(paths, USER_HOME);
4075 next_path = paths;
4076 while (*next_path)
4077 {
4078 for (path = next_path; *next_path && *next_path != ',';
4079 next_path++);
4080 if (*next_path)
4081 *next_path++ = NUL;
4082 STRCPY(test, path);
4083 STRCAT(test, "/");
4084 STRCAT(test, dst + 1);
4085 if (mch_stat(test, &st) == 0)
4086 {
4087 var = alloc(STRLEN(test) + 1);
4088 STRCPY(var, test);
4089 mustfree = TRUE;
4090 break;
4091 }
4092 }
4093 }
4094# endif /* UNIX */
4095#else
4096 /* cannot expand user's home directory, so don't try */
4097 var = NULL;
4098 tail = (char_u *)""; /* for gcc */
4099#endif /* UNIX || VMS */
4100 }
4101
4102#ifdef BACKSLASH_IN_FILENAME
4103 /* If 'shellslash' is set change backslashes to forward slashes.
4104 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4105 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4106 {
4107 char_u *p = vim_strsave(var);
4108
4109 if (p != NULL)
4110 {
4111 if (mustfree)
4112 vim_free(var);
4113 var = p;
4114 mustfree = TRUE;
4115 forward_slash(var);
4116 }
4117 }
4118#endif
4119
4120 /* If "var" contains white space, escape it with a backslash.
4121 * Required for ":e ~/tt" when $HOME includes a space. */
4122 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4123 {
4124 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4125
4126 if (p != NULL)
4127 {
4128 if (mustfree)
4129 vim_free(var);
4130 var = p;
4131 mustfree = TRUE;
4132 }
4133 }
4134
4135 if (var != NULL && *var != NUL
4136 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4137 {
4138 STRCPY(dst, var);
4139 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004140 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /* if var[] ends in a path separator and tail[] starts
4142 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004143 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4145 && dst[-1] != ':'
4146#endif
4147 && vim_ispathsep(*tail))
4148 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004149 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 src = tail;
4151 copy_char = FALSE;
4152 }
4153 if (mustfree)
4154 vim_free(var);
4155 }
4156
4157 if (copy_char) /* copy at least one char */
4158 {
4159 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004160 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004161 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4162 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 */
4164 at_start = FALSE;
4165 if (src[0] == '\\' && src[1] != NUL)
4166 {
4167 *dst++ = *src++;
4168 --dstlen;
4169 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004170 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 at_start = TRUE;
4172 *dst++ = *src++;
4173 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004174
4175 if (startstr != NULL && src - startstr_len >= srcp
4176 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4177 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 }
4180 *dst = NUL;
4181}
4182
4183/*
4184 * Vim's version of getenv().
4185 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004186 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004187 * "mustfree" is set to TRUE when returned is allocated, it must be
4188 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 */
4190 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004191vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 char_u *p;
4194 char_u *pend;
4195 int vimruntime;
4196
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004197#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 /* use "C:/" when $HOME is not set */
4199 if (STRCMP(name, "HOME") == 0)
4200 return homedir;
4201#endif
4202
4203 p = mch_getenv(name);
4204 if (p != NULL && *p == NUL) /* empty is the same as not set */
4205 p = NULL;
4206
4207 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004208 {
4209#if defined(FEAT_MBYTE) && defined(WIN3264)
4210 if (enc_utf8)
4211 {
4212 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004213 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004214
4215 /* Convert from active codepage to UTF-8. Other conversions are
4216 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004217 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004218 if (pp != NULL)
4219 {
4220 p = pp;
4221 *mustfree = TRUE;
4222 }
4223 }
4224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4229 if (!vimruntime && STRCMP(name, "VIM") != 0)
4230 return NULL;
4231
4232 /*
4233 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4234 * Don't do this when default_vimruntime_dir is non-empty.
4235 */
4236 if (vimruntime
4237#ifdef HAVE_PATHDEF
4238 && *default_vimruntime_dir == NUL
4239#endif
4240 )
4241 {
4242 p = mch_getenv((char_u *)"VIM");
4243 if (p != NULL && *p == NUL) /* empty is the same as not set */
4244 p = NULL;
4245 if (p != NULL)
4246 {
4247 p = vim_version_dir(p);
4248 if (p != NULL)
4249 *mustfree = TRUE;
4250 else
4251 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004252
4253#if defined(FEAT_MBYTE) && defined(WIN3264)
4254 if (enc_utf8)
4255 {
4256 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004257 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004258
4259 /* Convert from active codepage to UTF-8. Other conversions
4260 * are not done, because they would fail for non-ASCII
4261 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004262 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004263 if (pp != NULL)
4264 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004265 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004266 vim_free(p);
4267 p = pp;
4268 *mustfree = TRUE;
4269 }
4270 }
4271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 }
4274
4275 /*
4276 * When expanding $VIM or $VIMRUNTIME fails, try using:
4277 * - the directory name from 'helpfile' (unless it contains '$')
4278 * - the executable name from argv[0]
4279 */
4280 if (p == NULL)
4281 {
4282 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4283 p = p_hf;
4284#ifdef USE_EXE_NAME
4285 /*
4286 * Use the name of the executable, obtained from argv[0].
4287 */
4288 else
4289 p = exe_name;
4290#endif
4291 if (p != NULL)
4292 {
4293 /* remove the file name */
4294 pend = gettail(p);
4295
4296 /* remove "doc/" from 'helpfile', if present */
4297 if (p == p_hf)
4298 pend = remove_tail(p, pend, (char_u *)"doc");
4299
4300#ifdef USE_EXE_NAME
4301# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004302 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (p == exe_name)
4304 {
4305 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004306 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004308 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4309 if (pend1 != pend)
4310 {
4311 pnew = alloc((unsigned)(pend1 - p) + 15);
4312 if (pnew != NULL)
4313 {
4314 STRNCPY(pnew, p, (pend1 - p));
4315 STRCPY(pnew + (pend1 - p), "Resources/vim");
4316 p = pnew;
4317 pend = p + STRLEN(p);
4318 }
4319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321# endif
4322 /* remove "src/" from exe_name, if present */
4323 if (p == exe_name)
4324 pend = remove_tail(p, pend, (char_u *)"src");
4325#endif
4326
4327 /* for $VIM, remove "runtime/" or "vim54/", if present */
4328 if (!vimruntime)
4329 {
4330 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4331 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4332 }
4333
4334 /* remove trailing path separator */
4335#ifndef MACOS_CLASSIC
4336 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004337 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004338 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 --pend;
4340#endif
4341
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004342#ifdef MACOS_X
4343 if (p == exe_name || p == p_hf)
4344#endif
4345 /* check that the result is a directory name */
4346 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 if (p != NULL && !mch_isdir(p))
4349 {
4350 vim_free(p);
4351 p = NULL;
4352 }
4353 else
4354 {
4355#ifdef USE_EXE_NAME
4356 /* may add "/vim54" or "/runtime" if it exists */
4357 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4358 {
4359 vim_free(p);
4360 p = pend;
4361 }
4362#endif
4363 *mustfree = TRUE;
4364 }
4365 }
4366 }
4367
4368#ifdef HAVE_PATHDEF
4369 /* When there is a pathdef.c file we can use default_vim_dir and
4370 * default_vimruntime_dir */
4371 if (p == NULL)
4372 {
4373 /* Only use default_vimruntime_dir when it is not empty */
4374 if (vimruntime && *default_vimruntime_dir != NUL)
4375 {
4376 p = default_vimruntime_dir;
4377 *mustfree = FALSE;
4378 }
4379 else if (*default_vim_dir != NUL)
4380 {
4381 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4382 *mustfree = TRUE;
4383 else
4384 {
4385 p = default_vim_dir;
4386 *mustfree = FALSE;
4387 }
4388 }
4389 }
4390#endif
4391
4392 /*
4393 * Set the environment variable, so that the new value can be found fast
4394 * next time, and others can also use it (e.g. Perl).
4395 */
4396 if (p != NULL)
4397 {
4398 if (vimruntime)
4399 {
4400 vim_setenv((char_u *)"VIMRUNTIME", p);
4401 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403 else
4404 {
4405 vim_setenv((char_u *)"VIM", p);
4406 didset_vim = TRUE;
4407 }
4408 }
4409 return p;
4410}
4411
4412/*
4413 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4414 * Return NULL if not, return its name in allocated memory otherwise.
4415 */
4416 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004417vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418{
4419 char_u *p;
4420
4421 if (vimdir == NULL || *vimdir == NUL)
4422 return NULL;
4423 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4424 if (p != NULL && mch_isdir(p))
4425 return p;
4426 vim_free(p);
4427 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4428 if (p != NULL && mch_isdir(p))
4429 return p;
4430 vim_free(p);
4431 return NULL;
4432}
4433
4434/*
4435 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4436 * the length of "name/". Otherwise return "pend".
4437 */
4438 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004439remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440{
4441 int len = (int)STRLEN(name) + 1;
4442 char_u *newend = pend - len;
4443
4444 if (newend >= p
4445 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004446 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 return newend;
4448 return pend;
4449}
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 * Our portable version of setenv.
4453 */
4454 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004455vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
4457#ifdef HAVE_SETENV
4458 mch_setenv((char *)name, (char *)val, 1);
4459#else
4460 char_u *envbuf;
4461
4462 /*
4463 * Putenv does not copy the string, it has to remain
4464 * valid. The allocated memory will never be freed.
4465 */
4466 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4467 if (envbuf != NULL)
4468 {
4469 sprintf((char *)envbuf, "%s=%s", name, val);
4470 putenv((char *)envbuf);
4471 }
4472#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004473#ifdef FEAT_GETTEXT
4474 /*
4475 * When setting $VIMRUNTIME adjust the directory to find message
4476 * translations to $VIMRUNTIME/lang.
4477 */
4478 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4479 {
4480 char_u *buf = concat_str(val, (char_u *)"/lang");
4481
4482 if (buf != NULL)
4483 {
4484 bindtextdomain(VIMPACKAGE, (char *)buf);
4485 vim_free(buf);
4486 }
4487 }
4488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489}
4490
4491#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4492/*
4493 * Function given to ExpandGeneric() to obtain an environment variable name.
4494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004496get_env_name(
4497 expand_T *xp UNUSED,
4498 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499{
4500# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4501 /*
4502 * No environ[] on the Amiga and on the Mac (using MPW).
4503 */
4504 return NULL;
4505# else
4506# ifndef __WIN32__
4507 /* Borland C++ 5.2 has this in a header file. */
4508 extern char **environ;
4509# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004510# define ENVNAMELEN 100
4511 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 char_u *str;
4513 int n;
4514
4515 str = (char_u *)environ[idx];
4516 if (str == NULL)
4517 return NULL;
4518
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004519 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 if (str[n] == '=' || str[n] == NUL)
4522 break;
4523 name[n] = str[n];
4524 }
4525 name[n] = NUL;
4526 return name;
4527# endif
4528}
Bram Moolenaar24305862012-08-15 14:05:05 +02004529
4530/*
4531 * Find all user names for user completion.
4532 * Done only once and then cached.
4533 */
4534 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004535init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004536{
Bram Moolenaar24305862012-08-15 14:05:05 +02004537 static int lazy_init_done = FALSE;
4538
4539 if (lazy_init_done)
4540 return;
4541
4542 lazy_init_done = TRUE;
4543 ga_init2(&ga_users, sizeof(char_u *), 20);
4544
4545# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4546 {
4547 char_u* user;
4548 struct passwd* pw;
4549
4550 setpwent();
4551 while ((pw = getpwent()) != NULL)
4552 /* pw->pw_name shouldn't be NULL but just in case... */
4553 if (pw->pw_name != NULL)
4554 {
4555 if (ga_grow(&ga_users, 1) == FAIL)
4556 break;
4557 user = vim_strsave((char_u*)pw->pw_name);
4558 if (user == NULL)
4559 break;
4560 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4561 }
4562 endpwent();
4563 }
4564# endif
4565}
4566
4567/*
4568 * Function given to ExpandGeneric() to obtain an user names.
4569 */
4570 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004571get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004572{
4573 init_users();
4574 if (idx < ga_users.ga_len)
4575 return ((char_u **)ga_users.ga_data)[idx];
4576 return NULL;
4577}
4578
4579/*
4580 * Check whether name matches a user name. Return:
4581 * 0 if name does not match any user name.
4582 * 1 if name partially matches the beginning of a user name.
4583 * 2 is name fully matches a user name.
4584 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004585int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004586{
4587 int i;
4588 int n = (int)STRLEN(name);
4589 int result = 0;
4590
4591 init_users();
4592 for (i = 0; i < ga_users.ga_len; i++)
4593 {
4594 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4595 return 2; /* full match */
4596 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4597 result = 1; /* partial match */
4598 }
4599 return result;
4600}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#endif
4602
4603/*
4604 * Replace home directory by "~" in each space or comma separated file name in
4605 * 'src'.
4606 * If anything fails (except when out of space) dst equals src.
4607 */
4608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004609home_replace(
4610 buf_T *buf, /* when not NULL, check for help files */
4611 char_u *src, /* input file name */
4612 char_u *dst, /* where to put the result */
4613 int dstlen, /* maximum length of the result */
4614 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 spaces and commas in the file name. */
4616{
4617 size_t dirlen = 0, envlen = 0;
4618 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004619 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 char_u *p;
4621
4622 if (src == NULL)
4623 {
4624 *dst = NUL;
4625 return;
4626 }
4627
4628 /*
4629 * If the file is a help file, remove the path completely.
4630 */
4631 if (buf != NULL && buf->b_help)
4632 {
4633 STRCPY(dst, gettail(src));
4634 return;
4635 }
4636
4637 /*
4638 * We check both the value of the $HOME environment variable and the
4639 * "real" home directory.
4640 */
4641 if (homedir != NULL)
4642 dirlen = STRLEN(homedir);
4643
4644#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004645 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004647 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4648#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004649 /* Empty is the same as not set. */
4650 if (homedir_env != NULL && *homedir_env == NUL)
4651 homedir_env = NULL;
4652
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004653#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004654 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004655 {
4656 int usedlen = 0;
4657 int flen;
4658 char_u *fbuf = NULL;
4659
4660 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004661 (void)modify_fname((char_u *)":p", &usedlen,
4662 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004663 flen = (int)STRLEN(homedir_env);
4664 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4665 /* Remove the trailing / that is added to a directory. */
4666 homedir_env[flen - 1] = NUL;
4667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668#endif
4669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (homedir_env != NULL)
4671 envlen = STRLEN(homedir_env);
4672
4673 if (!one)
4674 src = skipwhite(src);
4675 while (*src && dstlen > 0)
4676 {
4677 /*
4678 * Here we are at the beginning of a file name.
4679 * First, check to see if the beginning of the file name matches
4680 * $HOME or the "real" home directory. Check that there is a '/'
4681 * after the match (so that if e.g. the file is "/home/pieter/bla",
4682 * and the home directory is "/home/piet", the file does not end up
4683 * as "~er/bla" (which would seem to indicate the file "bla" in user
4684 * er's home directory)).
4685 */
4686 p = homedir;
4687 len = dirlen;
4688 for (;;)
4689 {
4690 if ( len
4691 && fnamencmp(src, p, len) == 0
4692 && (vim_ispathsep(src[len])
4693 || (!one && (src[len] == ',' || src[len] == ' '))
4694 || src[len] == NUL))
4695 {
4696 src += len;
4697 if (--dstlen > 0)
4698 *dst++ = '~';
4699
4700 /*
4701 * If it's just the home directory, add "/".
4702 */
4703 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4704 *dst++ = '/';
4705 break;
4706 }
4707 if (p == homedir_env)
4708 break;
4709 p = homedir_env;
4710 len = envlen;
4711 }
4712
4713 /* if (!one) skip to separator: space or comma */
4714 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4715 *dst++ = *src++;
4716 /* skip separator */
4717 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4718 *dst++ = *src++;
4719 }
4720 /* if (dstlen == 0) out of space, what to do??? */
4721
4722 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004723
4724 if (homedir_env != homedir_env_orig)
4725 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726}
4727
4728/*
4729 * Like home_replace, store the replaced string in allocated memory.
4730 * When something fails, NULL is returned.
4731 */
4732 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004733home_replace_save(
4734 buf_T *buf, /* when not NULL, check for help files */
4735 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 char_u *dst;
4738 unsigned len;
4739
4740 len = 3; /* space for "~/" and trailing NUL */
4741 if (src != NULL) /* just in case */
4742 len += (unsigned)STRLEN(src);
4743 dst = alloc(len);
4744 if (dst != NULL)
4745 home_replace(buf, src, dst, len, TRUE);
4746 return dst;
4747}
4748
4749/*
4750 * Compare two file names and return:
4751 * FPC_SAME if they both exist and are the same file.
4752 * FPC_SAMEX if they both don't exist and have the same file name.
4753 * FPC_DIFF if they both exist and are different files.
4754 * FPC_NOTX if they both don't exist.
4755 * FPC_DIFFX if one of them doesn't exist.
4756 * For the first name environment variables are expanded
4757 */
4758 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004759fullpathcmp(
4760 char_u *s1,
4761 char_u *s2,
4762 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
4764#ifdef UNIX
4765 char_u exp1[MAXPATHL];
4766 char_u full1[MAXPATHL];
4767 char_u full2[MAXPATHL];
4768 struct stat st1, st2;
4769 int r1, r2;
4770
4771 expand_env(s1, exp1, MAXPATHL);
4772 r1 = mch_stat((char *)exp1, &st1);
4773 r2 = mch_stat((char *)s2, &st2);
4774 if (r1 != 0 && r2 != 0)
4775 {
4776 /* if mch_stat() doesn't work, may compare the names */
4777 if (checkname)
4778 {
4779 if (fnamecmp(exp1, s2) == 0)
4780 return FPC_SAMEX;
4781 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4782 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4783 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4784 return FPC_SAMEX;
4785 }
4786 return FPC_NOTX;
4787 }
4788 if (r1 != 0 || r2 != 0)
4789 return FPC_DIFFX;
4790 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4791 return FPC_SAME;
4792 return FPC_DIFF;
4793#else
4794 char_u *exp1; /* expanded s1 */
4795 char_u *full1; /* full path of s1 */
4796 char_u *full2; /* full path of s2 */
4797 int retval = FPC_DIFF;
4798 int r1, r2;
4799
4800 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4801 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4802 {
4803 full1 = exp1 + MAXPATHL;
4804 full2 = full1 + MAXPATHL;
4805
4806 expand_env(s1, exp1, MAXPATHL);
4807 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4808 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4809
4810 /* If vim_FullName() fails, the file probably doesn't exist. */
4811 if (r1 != OK && r2 != OK)
4812 {
4813 if (checkname && fnamecmp(exp1, s2) == 0)
4814 retval = FPC_SAMEX;
4815 else
4816 retval = FPC_NOTX;
4817 }
4818 else if (r1 != OK || r2 != OK)
4819 retval = FPC_DIFFX;
4820 else if (fnamecmp(full1, full2))
4821 retval = FPC_DIFF;
4822 else
4823 retval = FPC_SAME;
4824 vim_free(exp1);
4825 }
4826 return retval;
4827#endif
4828}
4829
4830/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004831 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004832 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004833 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 */
4835 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004836gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
4838 char_u *p1, *p2;
4839
4840 if (fname == NULL)
4841 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004842 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004844 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004846 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 return p1;
4849}
4850
Bram Moolenaar31710262010-08-13 13:36:15 +02004851#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004852static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004853
4854/*
4855 * Return the end of the directory name, on the first path
4856 * separator:
4857 * "/path/file", "/path/dir/", "/path//dir", "/file"
4858 * ^ ^ ^ ^
4859 */
4860 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004861gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004862{
4863 char_u *dir_end = fname;
4864 char_u *next_dir_end = fname;
4865 int look_for_sep = TRUE;
4866 char_u *p;
4867
4868 for (p = fname; *p != NUL; )
4869 {
4870 if (vim_ispathsep(*p))
4871 {
4872 if (look_for_sep)
4873 {
4874 next_dir_end = p;
4875 look_for_sep = FALSE;
4876 }
4877 }
4878 else
4879 {
4880 if (!look_for_sep)
4881 dir_end = next_dir_end;
4882 look_for_sep = TRUE;
4883 }
4884 mb_ptr_adv(p);
4885 }
4886 return dir_end;
4887}
4888#endif
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004891 * Get pointer to tail of "fname", including path separators. Putting a NUL
4892 * here leaves the directory name. Takes care of "c:/" and "//".
4893 * Always returns a valid pointer.
4894 */
4895 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004896gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004897{
4898 char_u *p;
4899 char_u *t;
4900
4901 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4902 t = gettail(fname);
4903 while (t > p && after_pathsep(fname, t))
4904 --t;
4905#ifdef VMS
4906 /* path separator is part of the path */
4907 ++t;
4908#endif
4909 return t;
4910}
4911
4912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 * get the next path component (just after the next path separator).
4914 */
4915 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004916getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004919 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (*fname)
4921 ++fname;
4922 return fname;
4923}
4924
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925/*
4926 * Get a pointer to one character past the head of a path name.
4927 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4928 * If there is no head, path is returned.
4929 */
4930 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004931get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932{
4933 char_u *retval;
4934
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004935#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 /* may skip "c:" */
4937 if (isalpha(path[0]) && path[1] == ':')
4938 retval = path + 2;
4939 else
4940 retval = path;
4941#else
4942# if defined(AMIGA)
4943 /* may skip "label:" */
4944 retval = vim_strchr(path, ':');
4945 if (retval == NULL)
4946 retval = path;
4947# else /* Unix */
4948 retval = path;
4949# endif
4950#endif
4951
4952 while (vim_ispathsep(*retval))
4953 ++retval;
4954
4955 return retval;
4956}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004959 * Return TRUE if 'c' is a path separator.
4960 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 */
4962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004963vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004965#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004967#else
4968# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004970# else
4971# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4973 return (c == ':' || c == '[' || c == ']' || c == '/'
4974 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004975# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004977# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980}
4981
Bram Moolenaar69c35002013-11-04 02:54:12 +01004982/*
4983 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4984 */
4985 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004986vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004987{
4988 return vim_ispathsep(c)
4989#ifdef BACKSLASH_IN_FILENAME
4990 && c != ':'
4991#endif
4992 ;
4993}
4994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4996/*
4997 * return TRUE if 'c' is a path list separator.
4998 */
4999 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005000vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002#ifdef UNIX
5003 return (c == ':');
5004#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005005 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#endif
5007}
5008#endif
5009
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005010#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5011 || defined(FEAT_EVAL) || defined(PROTO)
5012/*
5013 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5014 * It's done in-place.
5015 */
5016 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005017shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005018{
5019 char_u *tail, *s, *d;
5020 int skip = FALSE;
5021
5022 tail = gettail(str);
5023 d = str;
5024 for (s = str; ; ++s)
5025 {
5026 if (s >= tail) /* copy the whole tail */
5027 {
5028 *d++ = *s;
5029 if (*s == NUL)
5030 break;
5031 }
5032 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5033 {
5034 *d++ = *s;
5035 skip = FALSE;
5036 }
5037 else if (!skip)
5038 {
5039 *d++ = *s; /* copy next char */
5040 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5041 skip = TRUE;
5042# ifdef FEAT_MBYTE
5043 if (has_mbyte)
5044 {
5045 int l = mb_ptr2len(s);
5046
5047 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005048 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005049 }
5050# endif
5051 }
5052 }
5053}
5054#endif
5055
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005056/*
5057 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5058 * Also returns TRUE if there is no directory name.
5059 * "fname" must be writable!.
5060 */
5061 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005062dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005063{
5064 char_u *p;
5065 int c;
5066 int retval;
5067
5068 p = gettail_sep(fname);
5069 if (p == fname)
5070 return TRUE;
5071 c = *p;
5072 *p = NUL;
5073 retval = mch_isdir(fname);
5074 *p = c;
5075 return retval;
5076}
5077
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005079 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5080 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 */
5082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005083vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005085#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005087#else
5088 if (p_fic)
5089 return MB_STRICMP(x, y);
5090 return STRCMP(x, y);
5091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092}
5093
5094 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005095vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005097#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005098 char_u *px = x;
5099 char_u *py = y;
5100 int cx = NUL;
5101 int cy = NUL;
5102
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005103 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005105 cx = PTR2CHAR(px);
5106 cy = PTR2CHAR(py);
5107 if (cx == NUL || cy == NUL
5108 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5109 && !(cx == '/' && cy == '\\')
5110 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005112 len -= MB_PTR2LEN(px);
5113 px += MB_PTR2LEN(px);
5114 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 if (len == 0)
5117 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005118 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005119#else
5120 if (p_fic)
5121 return MB_STRNICMP(x, y, len);
5122 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125
5126/*
5127 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005128 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 */
5130 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005131concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 char_u *dest;
5134
5135 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5136 if (dest != NULL)
5137 {
5138 STRCPY(dest, fname1);
5139 if (sep)
5140 add_pathsep(dest);
5141 STRCAT(dest, fname2);
5142 }
5143 return dest;
5144}
5145
Bram Moolenaard6754642005-01-17 22:18:45 +00005146/*
5147 * Concatenate two strings and return the result in allocated memory.
5148 * Returns NULL when out of memory.
5149 */
5150 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005151concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005152{
5153 char_u *dest;
5154 size_t l = STRLEN(str1);
5155
5156 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5157 if (dest != NULL)
5158 {
5159 STRCPY(dest, str1);
5160 STRCPY(dest + l, str2);
5161 }
5162 return dest;
5163}
Bram Moolenaard6754642005-01-17 22:18:45 +00005164
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165/*
5166 * Add a path separator to a file name, unless it already ends in a path
5167 * separator.
5168 */
5169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005172 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 STRCAT(p, PATHSEPSTR);
5174}
5175
5176/*
5177 * FullName_save - Make an allocated copy of a full file name.
5178 * Returns NULL when out of memory.
5179 */
5180 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181FullName_save(
5182 char_u *fname,
5183 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005184 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 char_u *buf;
5187 char_u *new_fname = NULL;
5188
5189 if (fname == NULL)
5190 return NULL;
5191
5192 buf = alloc((unsigned)MAXPATHL);
5193 if (buf != NULL)
5194 {
5195 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5196 new_fname = vim_strsave(buf);
5197 else
5198 new_fname = vim_strsave(fname);
5199 vim_free(buf);
5200 }
5201 return new_fname;
5202}
5203
5204#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5205
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005206static char_u *skip_string(char_u *p);
5207static pos_T *ind_find_start_comment(void);
5208static pos_T *ind_find_start_CORS(void);
5209static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211/*
5212 * Find the start of a comment, not knowing if we are in a comment right now.
5213 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005214 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005216 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005217ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005218{
5219 return find_start_comment(curbuf->b_ind_maxcomment);
5220}
5221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 pos_T *pos;
5226 char_u *line;
5227 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005228 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005230 for (;;)
5231 {
5232 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5233 if (pos == NULL)
5234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005236 /*
5237 * Check if the comment start we found is inside a string.
5238 * If it is then restrict the search to below this line and try again.
5239 */
5240 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005241 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005242 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005243 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005244 break;
5245 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5246 if (cur_maxcomment <= 0)
5247 {
5248 pos = NULL;
5249 break;
5250 }
5251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 return pos;
5253}
5254
5255/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005256 * Find the start of a comment or raw string, not knowing if we are in a
5257 * comment or raw string right now.
5258 * Search starts at w_cursor.lnum and goes backwards.
5259 * Return NULL when not inside a comment or raw string.
5260 * "CORS" -> Comment Or Raw String
5261 */
5262 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005263ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005264{
Bram Moolenaar089af182015-10-07 11:41:49 +02005265 static pos_T comment_pos_copy;
5266 pos_T *comment_pos;
5267 pos_T *rs_pos;
5268
5269 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5270 if (comment_pos != NULL)
5271 {
5272 /* Need to make a copy of the static pos in findmatchlimit(),
5273 * calling find_start_rawstring() may change it. */
5274 comment_pos_copy = *comment_pos;
5275 comment_pos = &comment_pos_copy;
5276 }
5277 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005278
5279 /* If comment_pos is before rs_pos the raw string is inside the comment.
5280 * If rs_pos is before comment_pos the comment is inside the raw string. */
5281 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5282 return rs_pos;
5283 return comment_pos;
5284}
5285
5286/*
5287 * Find the start of a raw string, not knowing if we are in one right now.
5288 * Search starts at w_cursor.lnum and goes backwards.
5289 * Return NULL when not inside a raw string.
5290 */
5291 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005293{
5294 pos_T *pos;
5295 char_u *line;
5296 char_u *p;
5297 int cur_maxcomment = ind_maxcomment;
5298
5299 for (;;)
5300 {
5301 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5302 if (pos == NULL)
5303 break;
5304
5305 /*
5306 * Check if the raw string start we found is inside a string.
5307 * If it is then restrict the search to below this line and try again.
5308 */
5309 line = ml_get(pos->lnum);
5310 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5311 p = skip_string(p);
5312 if ((colnr_T)(p - line) <= pos->col)
5313 break;
5314 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5315 if (cur_maxcomment <= 0)
5316 {
5317 pos = NULL;
5318 break;
5319 }
5320 }
5321 return pos;
5322}
5323
5324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 * Skip to the end of a "string" and a 'c' character.
5326 * If there is no string or character, return argument unmodified.
5327 */
5328 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005329skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
5331 int i;
5332
5333 /*
5334 * We loop, because strings may be concatenated: "date""time".
5335 */
5336 for ( ; ; ++p)
5337 {
5338 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5339 {
5340 if (!p[1]) /* ' at end of line */
5341 break;
5342 i = 2;
5343 if (p[1] == '\\') /* '\n' or '\000' */
5344 {
5345 ++i;
5346 while (vim_isdigit(p[i - 1])) /* '\000' */
5347 ++i;
5348 }
5349 if (p[i] == '\'') /* check for trailing ' */
5350 {
5351 p += i;
5352 continue;
5353 }
5354 }
5355 else if (p[0] == '"') /* start of string */
5356 {
5357 for (++p; p[0]; ++p)
5358 {
5359 if (p[0] == '\\' && p[1] != NUL)
5360 ++p;
5361 else if (p[0] == '"') /* end of string */
5362 break;
5363 }
5364 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005365 continue; /* continue for another string */
5366 }
5367 else if (p[0] == 'R' && p[1] == '"')
5368 {
5369 /* Raw string: R"[delim](...)[delim]" */
5370 char_u *delim = p + 2;
5371 char_u *paren = vim_strchr(delim, '(');
5372
5373 if (paren != NULL)
5374 {
5375 size_t delim_len = paren - delim;
5376
5377 for (p += 3; *p; ++p)
5378 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5379 && p[delim_len + 1] == '"')
5380 {
5381 p += delim_len + 1;
5382 break;
5383 }
5384 if (p[0] == '"')
5385 continue; /* continue for another string */
5386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 break; /* no string found */
5389 }
5390 if (!*p)
5391 --p; /* backup from NUL */
5392 return p;
5393}
5394#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5395
5396#if defined(FEAT_CINDENT) || defined(PROTO)
5397
5398/*
5399 * Do C or expression indenting on the current line.
5400 */
5401 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005402do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
5404# ifdef FEAT_EVAL
5405 if (*curbuf->b_p_inde != NUL)
5406 fixthisline(get_expr_indent);
5407 else
5408# endif
5409 fixthisline(get_c_indent);
5410}
5411
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005412/* Find result cache for cpp_baseclass */
5413typedef struct {
5414 int found;
5415 lpos_T lpos;
5416} cpp_baseclass_cache_T;
5417
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418/*
5419 * Functions for C-indenting.
5420 * Most of this originally comes from Eric Fischer.
5421 */
5422/*
5423 * Below "XXX" means that this function may unlock the current line.
5424 */
5425
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005426static char_u *cin_skipcomment(char_u *);
5427static int cin_nocode(char_u *);
5428static pos_T *find_line_comment(void);
5429static int cin_has_js_key(char_u *text);
5430static int cin_islabel_skip(char_u **);
5431static int cin_isdefault(char_u *);
5432static char_u *after_label(char_u *l);
5433static int get_indent_nolabel(linenr_T lnum);
5434static int skip_label(linenr_T, char_u **pp);
5435static int cin_first_id_amount(void);
5436static int cin_get_equal_amount(linenr_T lnum);
5437static int cin_ispreproc(char_u *);
5438static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump);
5439static int cin_iscomment(char_u *);
5440static int cin_islinecomment(char_u *);
5441static int cin_isterminated(char_u *, int, int);
5442static int cin_isinit(void);
5443static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5444static int cin_isif(char_u *);
5445static int cin_iselse(char_u *);
5446static int cin_isdo(char_u *);
5447static int cin_iswhileofdo(char_u *, linenr_T);
5448static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5449static int cin_iswhileofdo_end(int terminated);
5450static int cin_isbreak(char_u *);
5451static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5452static int get_baseclass_amount(int col);
5453static int cin_ends_in(char_u *, char_u *, char_u *);
5454static int cin_starts_with(char_u *s, char *word);
5455static int cin_skip2pos(pos_T *trypos);
5456static pos_T *find_start_brace(void);
5457static pos_T *find_match_paren(int);
5458static pos_T *find_match_char(int c, int ind_maxparen);
5459static int corr_ind_maxparen(pos_T *startpos);
5460static int find_last_paren(char_u *l, int start, int end);
5461static int find_match(int lookfor, linenr_T ourscope);
5462static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
5464/*
5465 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005466 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 */
5468 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005469cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471 while (*s)
5472 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005473 char_u *prev_s = s;
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005476
5477 /* Perl/shell # comment comment continues until eol. Require a space
5478 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005479 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005480 {
5481 s += STRLEN(s);
5482 break;
5483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 if (*s != '/')
5485 break;
5486 ++s;
5487 if (*s == '/') /* slash-slash comment continues till eol */
5488 {
5489 s += STRLEN(s);
5490 break;
5491 }
5492 if (*s != '*')
5493 break;
5494 for (++s; *s; ++s) /* skip slash-star comment */
5495 if (s[0] == '*' && s[1] == '/')
5496 {
5497 s += 2;
5498 break;
5499 }
5500 }
5501 return s;
5502}
5503
5504/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005505 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * not considered code.
5507 */
5508 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005509cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511 return *cin_skipcomment(s) == NUL;
5512}
5513
5514/*
5515 * Check previous lines for a "//" line comment, skipping over blank lines.
5516 */
5517 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005518find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
5520 static pos_T pos;
5521 char_u *line;
5522 char_u *p;
5523
5524 pos = curwin->w_cursor;
5525 while (--pos.lnum > 0)
5526 {
5527 line = ml_get(pos.lnum);
5528 p = skipwhite(line);
5529 if (cin_islinecomment(p))
5530 {
5531 pos.col = (int)(p - line);
5532 return &pos;
5533 }
5534 if (*p != NUL)
5535 break;
5536 }
5537 return NULL;
5538}
5539
5540/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005541 * Return TRUE if "text" starts with "key:".
5542 */
5543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005544cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005545{
5546 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005547 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005548
5549 if (*s == '\'' || *s == '"')
5550 {
5551 /* can be 'key': or "key": */
5552 quote = *s;
5553 ++s;
5554 }
5555 if (!vim_isIDc(*s)) /* need at least one ID character */
5556 return FALSE;
5557
5558 while (vim_isIDc(*s))
5559 ++s;
5560 if (*s == quote)
5561 ++s;
5562
5563 s = cin_skipcomment(s);
5564
5565 /* "::" is not a label, it's C++ */
5566 return (*s == ':' && s[1] != ':');
5567}
5568
5569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005571 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 */
5573 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005574cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576 if (!vim_isIDc(**s)) /* need at least one ID character */
5577 return FALSE;
5578
5579 while (vim_isIDc(**s))
5580 (*s)++;
5581
5582 *s = cin_skipcomment(*s);
5583
5584 /* "::" is not a label, it's C++ */
5585 return (**s == ':' && *++*s != ':');
5586}
5587
5588/*
5589 * Recognize a label: "label:".
5590 * Note: curwin->w_cursor must be where we are looking for the label.
5591 */
5592 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005593cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 char_u *s;
5596
5597 s = cin_skipcomment(ml_get_curline());
5598
5599 /*
5600 * Exclude "default" from labels, since it should be indented
5601 * like a switch label. Same for C++ scope declarations.
5602 */
5603 if (cin_isdefault(s))
5604 return FALSE;
5605 if (cin_isscopedecl(s))
5606 return FALSE;
5607
5608 if (cin_islabel_skip(&s))
5609 {
5610 /*
5611 * Only accept a label if the previous line is terminated or is a case
5612 * label.
5613 */
5614 pos_T cursor_save;
5615 pos_T *trypos;
5616 char_u *line;
5617
5618 cursor_save = curwin->w_cursor;
5619 while (curwin->w_cursor.lnum > 1)
5620 {
5621 --curwin->w_cursor.lnum;
5622
5623 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005624 * If we're in a comment or raw string now, skip to the start of
5625 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 */
5627 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005628 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 curwin->w_cursor = *trypos;
5630
5631 line = ml_get_curline();
5632 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5633 continue;
5634 if (*(line = cin_skipcomment(line)) == NUL)
5635 continue;
5636
5637 curwin->w_cursor = cursor_save;
5638 if (cin_isterminated(line, TRUE, FALSE)
5639 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005640 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 || (cin_islabel_skip(&line) && cin_nocode(line)))
5642 return TRUE;
5643 return FALSE;
5644 }
5645 curwin->w_cursor = cursor_save;
5646 return TRUE; /* label at start of file??? */
5647 }
5648 return FALSE;
5649}
5650
5651/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005652 * Recognize structure initialization and enumerations:
5653 * "[typedef] [static|public|protected|private] enum"
5654 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 */
5656 static int
5657cin_isinit(void)
5658{
5659 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005660 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 s = cin_skipcomment(ml_get_curline());
5663
Bram Moolenaar75342212013-03-07 13:13:52 +01005664 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 s = cin_skipcomment(s + 7);
5666
Bram Moolenaar75342212013-03-07 13:13:52 +01005667 for (;;)
5668 {
5669 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005670
Bram Moolenaar75342212013-03-07 13:13:52 +01005671 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5672 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005673 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005674 if (cin_starts_with(s, skip[i]))
5675 {
5676 s = cin_skipcomment(s + l);
5677 l = 0;
5678 break;
5679 }
5680 }
5681 if (l != 0)
5682 break;
5683 }
5684
5685 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 return TRUE;
5687
5688 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5689 return TRUE;
5690
5691 return FALSE;
5692}
5693
5694/*
5695 * Recognize a switch label: "case .*:" or "default:".
5696 */
5697 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005698cin_iscase(
5699 char_u *s,
5700 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005703 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 for (s += 4; *s; ++s)
5706 {
5707 s = cin_skipcomment(s);
5708 if (*s == ':')
5709 {
5710 if (s[1] == ':') /* skip over "::" for C++ */
5711 ++s;
5712 else
5713 return TRUE;
5714 }
5715 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005716 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5718 return FALSE; /* stop at comment */
5719 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005720 {
5721 /* JS etc. */
5722 if (strict)
5723 return FALSE; /* stop at string */
5724 else
5725 return TRUE;
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728 return FALSE;
5729 }
5730
5731 if (cin_isdefault(s))
5732 return TRUE;
5733 return FALSE;
5734}
5735
5736/*
5737 * Recognize a "default" switch label.
5738 */
5739 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005740cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 return (STRNCMP(s, "default", 7) == 0
5743 && *(s = cin_skipcomment(s + 7)) == ':'
5744 && s[1] != ':');
5745}
5746
5747/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005748 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 */
5750 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005751cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
5753 int i;
5754
5755 s = cin_skipcomment(s);
5756 if (STRNCMP(s, "public", 6) == 0)
5757 i = 6;
5758 else if (STRNCMP(s, "protected", 9) == 0)
5759 i = 9;
5760 else if (STRNCMP(s, "private", 7) == 0)
5761 i = 7;
5762 else
5763 return FALSE;
5764 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5765}
5766
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005767/* Maximum number of lines to search back for a "namespace" line. */
5768#define FIND_NAMESPACE_LIM 20
5769
5770/*
5771 * Recognize a "namespace" scope declaration.
5772 */
5773 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005774cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005775{
5776 char_u *p;
5777 int has_name = FALSE;
5778
5779 s = cin_skipcomment(s);
5780 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5781 {
5782 p = cin_skipcomment(skipwhite(s + 9));
5783 while (*p != NUL)
5784 {
5785 if (vim_iswhite(*p))
5786 {
5787 has_name = TRUE; /* found end of a name */
5788 p = cin_skipcomment(skipwhite(p));
5789 }
5790 else if (*p == '{')
5791 {
5792 break;
5793 }
5794 else if (vim_iswordc(*p))
5795 {
5796 if (has_name)
5797 return FALSE; /* word character after skipping past name */
5798 ++p;
5799 }
5800 else
5801 {
5802 return FALSE;
5803 }
5804 }
5805 return TRUE;
5806 }
5807 return FALSE;
5808}
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810/*
5811 * Return a pointer to the first non-empty non-comment character after a ':'.
5812 * Return NULL if not found.
5813 * case 234: a = b;
5814 * ^
5815 */
5816 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005817after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819 for ( ; *l; ++l)
5820 {
5821 if (*l == ':')
5822 {
5823 if (l[1] == ':') /* skip over "::" for C++ */
5824 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005825 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827 }
5828 else if (*l == '\'' && l[1] && l[2] == '\'')
5829 l += 2; /* skip over 'x' */
5830 }
5831 if (*l == NUL)
5832 return NULL;
5833 l = cin_skipcomment(l + 1);
5834 if (*l == NUL)
5835 return NULL;
5836 return l;
5837}
5838
5839/*
5840 * Get indent of line "lnum", skipping a label.
5841 * Return 0 if there is nothing after the label.
5842 */
5843 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005844get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
5846 char_u *l;
5847 pos_T fp;
5848 colnr_T col;
5849 char_u *p;
5850
5851 l = ml_get(lnum);
5852 p = after_label(l);
5853 if (p == NULL)
5854 return 0;
5855
5856 fp.col = (colnr_T)(p - l);
5857 fp.lnum = lnum;
5858 getvcol(curwin, &fp, &col, NULL, NULL);
5859 return (int)col;
5860}
5861
5862/*
5863 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005864 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 * label: if (asdf && asdfasdf)
5866 * ^
5867 */
5868 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005869skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 char_u *l;
5872 int amount;
5873 pos_T cursor_save;
5874
5875 cursor_save = curwin->w_cursor;
5876 curwin->w_cursor.lnum = lnum;
5877 l = ml_get_curline();
5878 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005879 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
5881 amount = get_indent_nolabel(lnum);
5882 l = after_label(ml_get_curline());
5883 if (l == NULL) /* just in case */
5884 l = ml_get_curline();
5885 }
5886 else
5887 {
5888 amount = get_indent();
5889 l = ml_get_curline();
5890 }
5891 *pp = l;
5892
5893 curwin->w_cursor = cursor_save;
5894 return amount;
5895}
5896
5897/*
5898 * Return the indent of the first variable name after a type in a declaration.
5899 * int a, indent of "a"
5900 * static struct foo b, indent of "b"
5901 * enum bla c, indent of "c"
5902 * Returns zero when it doesn't look like a declaration.
5903 */
5904 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005905cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 char_u *line, *p, *s;
5908 int len;
5909 pos_T fp;
5910 colnr_T col;
5911
5912 line = ml_get_curline();
5913 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005914 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5916 {
5917 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005918 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 }
5920 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5921 p = skipwhite(p + 6);
5922 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5923 p = skipwhite(p + 4);
5924 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5925 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5926 {
5927 s = skipwhite(p + len);
5928 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5929 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5930 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5931 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5932 p = s;
5933 }
5934 for (len = 0; vim_isIDc(p[len]); ++len)
5935 ;
5936 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5937 return 0;
5938
5939 p = skipwhite(p + len);
5940 fp.lnum = curwin->w_cursor.lnum;
5941 fp.col = (colnr_T)(p - line);
5942 getvcol(curwin, &fp, &col, NULL, NULL);
5943 return (int)col;
5944}
5945
5946/*
5947 * Return the indent of the first non-blank after an equal sign.
5948 * char *foo = "here";
5949 * Return zero if no (useful) equal sign found.
5950 * Return -1 if the line above "lnum" ends in a backslash.
5951 * foo = "asdf\
5952 * asdf\
5953 * here";
5954 */
5955 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005956cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957{
5958 char_u *line;
5959 char_u *s;
5960 colnr_T col;
5961 pos_T fp;
5962
5963 if (lnum > 1)
5964 {
5965 line = ml_get(lnum - 1);
5966 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5967 return -1;
5968 }
5969
5970 line = s = ml_get(lnum);
5971 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5972 {
5973 if (cin_iscomment(s)) /* ignore comments */
5974 s = cin_skipcomment(s);
5975 else
5976 ++s;
5977 }
5978 if (*s != '=')
5979 return 0;
5980
5981 s = skipwhite(s + 1);
5982 if (cin_nocode(s))
5983 return 0;
5984
5985 if (*s == '"') /* nice alignment for continued strings */
5986 ++s;
5987
5988 fp.lnum = lnum;
5989 fp.col = (colnr_T)(s - line);
5990 getvcol(curwin, &fp, &col, NULL, NULL);
5991 return (int)col;
5992}
5993
5994/*
5995 * Recognize a preprocessor statement: Any line that starts with '#'.
5996 */
5997 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005998cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006000 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 return TRUE;
6002 return FALSE;
6003}
6004
6005/*
6006 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6007 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6008 * start and return the line in "*pp".
6009 */
6010 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006011cin_ispreproc_cont(char_u **pp, linenr_T *lnump)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012{
6013 char_u *line = *pp;
6014 linenr_T lnum = *lnump;
6015 int retval = FALSE;
6016
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006017 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 {
6019 if (cin_ispreproc(line))
6020 {
6021 retval = TRUE;
6022 *lnump = lnum;
6023 break;
6024 }
6025 if (lnum == 1)
6026 break;
6027 line = ml_get(--lnum);
6028 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6029 break;
6030 }
6031
6032 if (lnum != *lnump)
6033 *pp = ml_get(*lnump);
6034 return retval;
6035}
6036
6037/*
6038 * Recognize the start of a C or C++ comment.
6039 */
6040 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006041cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
6043 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6044}
6045
6046/*
6047 * Recognize the start of a "//" comment.
6048 */
6049 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006050cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 return (p[0] == '/' && p[1] == '/');
6053}
6054
6055/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006056 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6057 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006059 * If a line begins with an "else", only consider it terminated if no unmatched
6060 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 * Return the character terminating the line (ending char's have precedence if
6062 * both apply in order to determine initializations).
6063 */
6064 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006065cin_isterminated(
6066 char_u *s,
6067 int incl_open, /* include '{' at the end as terminator */
6068 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006070 char_u found_start = 0;
6071 unsigned n_open = 0;
6072 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073
6074 s = cin_skipcomment(s);
6075
6076 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6077 found_start = *s;
6078
Bram Moolenaar496f9512011-05-19 16:35:09 +02006079 if (!found_start)
6080 is_else = cin_iselse(s);
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 while (*s)
6083 {
6084 /* skip over comments, "" strings and 'c'haracters */
6085 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006086 if (*s == '}' && n_open > 0)
6087 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006088 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006089 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 && cin_nocode(s + 1))
6091 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006092 else if (*s == '{')
6093 {
6094 if (incl_open && cin_nocode(s + 1))
6095 return *s;
6096 else
6097 ++n_open;
6098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
6100 if (*s)
6101 s++;
6102 }
6103 return found_start;
6104}
6105
6106/*
6107 * Recognize the basic picture of a function declaration -- it needs to
6108 * have an open paren somewhere and a close paren at the end of the line and
6109 * no semicolons anywhere.
6110 * When a line ends in a comma we continue looking in the next line.
6111 * "sp" points to a string with the line. When looking at other lines it must
6112 * be restored to the line. When it's NULL fetch lines here.
6113 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006114 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 */
6116 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006117cin_isfuncdecl(
6118 char_u **sp,
6119 linenr_T first_lnum,
6120 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 char_u *s;
6123 linenr_T lnum = first_lnum;
6124 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006125 pos_T *trypos;
6126 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 if (sp == NULL)
6129 s = ml_get(lnum);
6130 else
6131 s = *sp;
6132
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006133 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006134 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006135 {
6136 lnum = trypos->lnum;
6137 if (lnum < min_lnum)
6138 return FALSE;
6139
6140 s = ml_get(lnum);
6141 }
6142
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006143 /* Ignore line starting with #. */
6144 if (cin_ispreproc(s))
6145 return FALSE;
6146
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6148 {
6149 if (cin_iscomment(s)) /* ignore comments */
6150 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006151 else if (*s == ':')
6152 {
6153 if (*(s + 1) == ':')
6154 s += 2;
6155 else
6156 /* To avoid a mistake in the following situation:
6157 * A::A(int a, int b)
6158 * : a(0) // <--not a function decl
6159 * , b(0)
6160 * {...
6161 */
6162 return FALSE;
6163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 else
6165 ++s;
6166 }
6167 if (*s != '(')
6168 return FALSE; /* ';', ' or " before any () or no '(' */
6169
6170 while (*s && *s != ';' && *s != '\'' && *s != '"')
6171 {
6172 if (*s == ')' && cin_nocode(s + 1))
6173 {
6174 /* ')' at the end: may have found a match
6175 * Check for he previous line not to end in a backslash:
6176 * #if defined(x) && \
6177 * defined(y)
6178 */
6179 lnum = first_lnum - 1;
6180 s = ml_get(lnum);
6181 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6182 retval = TRUE;
6183 goto done;
6184 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006185 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006187 int comma = (*s == ',');
6188
6189 /* ',' at the end: continue looking in the next line.
6190 * At the end: check for ',' in the next line, for this style:
6191 * func(arg1
6192 * , arg2) */
6193 for (;;)
6194 {
6195 if (lnum >= curbuf->b_ml.ml_line_count)
6196 break;
6197 s = ml_get(++lnum);
6198 if (!cin_ispreproc(s))
6199 break;
6200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if (lnum >= curbuf->b_ml.ml_line_count)
6202 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006203 /* Require a comma at end of the line or a comma or ')' at the
6204 * start of next line. */
6205 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006206 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006207 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006208 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210 else if (cin_iscomment(s)) /* ignore comments */
6211 s = cin_skipcomment(s);
6212 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006215 just_started = FALSE;
6216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
6218
6219done:
6220 if (lnum != first_lnum && sp != NULL)
6221 *sp = ml_get(first_lnum);
6222
6223 return retval;
6224}
6225
6226 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006227cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006229 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230}
6231
6232 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006233cin_iselse(
6234 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 if (*p == '}') /* accept "} else" */
6237 p = cin_skipcomment(p + 1);
6238 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6239}
6240
6241 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006242cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6245}
6246
6247/*
6248 * Check if this is a "while" that should have a matching "do".
6249 * We only accept a "while (condition) ;", with only white space between the
6250 * ')' and ';'. The condition may be spread over several lines.
6251 */
6252 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006253cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 pos_T cursor_save;
6256 pos_T *trypos;
6257 int retval = FALSE;
6258
6259 p = cin_skipcomment(p);
6260 if (*p == '}') /* accept "} while (cond);" */
6261 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006262 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
6264 cursor_save = curwin->w_cursor;
6265 curwin->w_cursor.lnum = lnum;
6266 curwin->w_cursor.col = 0;
6267 p = ml_get_curline();
6268 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6269 {
6270 ++p;
6271 ++curwin->w_cursor.col;
6272 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006273 if ((trypos = findmatchlimit(NULL, 0, 0,
6274 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6276 retval = TRUE;
6277 curwin->w_cursor = cursor_save;
6278 }
6279 return retval;
6280}
6281
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006282/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006283 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006284 * Return 0 if there is none.
6285 * Otherwise return !0 and update "*poffset" to point to the place where the
6286 * string was found.
6287 */
6288 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006289cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006290{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006291 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006292
6293 if (offset-- < 2)
6294 return 0;
6295 while (offset > 2 && vim_iswhite(line[offset]))
6296 --offset;
6297
6298 offset -= 1;
6299 if (!STRNCMP(line + offset, "if", 2))
6300 goto probablyFound;
6301
6302 if (offset >= 1)
6303 {
6304 offset -= 1;
6305 if (!STRNCMP(line + offset, "for", 3))
6306 goto probablyFound;
6307
6308 if (offset >= 2)
6309 {
6310 offset -= 2;
6311 if (!STRNCMP(line + offset, "while", 5))
6312 goto probablyFound;
6313 }
6314 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006315 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006316
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006317probablyFound:
6318 if (!offset || !vim_isIDc(line[offset - 1]))
6319 {
6320 *poffset = offset;
6321 return 1;
6322 }
6323 return 0;
6324}
6325
6326/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006327 * Return TRUE if we are at the end of a do-while.
6328 * do
6329 * nothing;
6330 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006331 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006332 * Adjust the cursor to the line with "while".
6333 */
6334 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006335cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006336{
6337 char_u *line;
6338 char_u *p;
6339 char_u *s;
6340 pos_T *trypos;
6341 int i;
6342
6343 if (terminated != ';') /* there must be a ';' at the end */
6344 return FALSE;
6345
6346 p = line = ml_get_curline();
6347 while (*p != NUL)
6348 {
6349 p = cin_skipcomment(p);
6350 if (*p == ')')
6351 {
6352 s = skipwhite(p + 1);
6353 if (*s == ';' && cin_nocode(s + 1))
6354 {
6355 /* Found ");" at end of the line, now check there is "while"
6356 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006357 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006358 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006359 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006360 if (trypos != NULL)
6361 {
6362 s = cin_skipcomment(ml_get(trypos->lnum));
6363 if (*s == '}') /* accept "} while (cond);" */
6364 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006365 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006366 {
6367 curwin->w_cursor.lnum = trypos->lnum;
6368 return TRUE;
6369 }
6370 }
6371
6372 /* Searching may have made "line" invalid, get it again. */
6373 line = ml_get_curline();
6374 p = line + i;
6375 }
6376 }
6377 if (*p != NUL)
6378 ++p;
6379 }
6380 return FALSE;
6381}
6382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006384cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6387}
6388
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006389/*
6390 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 * constructor-initialization. eg:
6392 *
6393 * class MyClass :
6394 * baseClass <-- here
6395 * class MyClass : public baseClass,
6396 * anotherBaseClass <-- here (should probably lineup ??)
6397 * MyClass::MyClass(...) :
6398 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006399 *
6400 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 */
6402 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006403cin_is_cpp_baseclass(
6404 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006406 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 char_u *s;
6408 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006409 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006410 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006412 if (pos->lnum <= lnum)
6413 return cached->found; /* Use the cached result */
6414
6415 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006417 s = skipwhite(line);
6418 if (*s == '#') /* skip #define FOO x ? (x) : x */
6419 return FALSE;
6420 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (*s == NUL)
6422 return FALSE;
6423
6424 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6425
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006426 /* Search for a line starting with '#', empty, ending in ';' or containing
6427 * '{' or '}' and start below it. This handles the following situations:
6428 * a = cond ?
6429 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006430 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006431 * func::foo()
6432 * : something
6433 * {}
6434 * Foo::Foo (int one, int two)
6435 * : something(4),
6436 * somethingelse(3)
6437 * {}
6438 */
6439 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006441 line = ml_get(lnum - 1);
6442 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006443 if (*s == '#' || *s == NUL)
6444 break;
6445 while (*s != NUL)
6446 {
6447 s = cin_skipcomment(s);
6448 if (*s == '{' || *s == '}'
6449 || (*s == ';' && cin_nocode(s + 1)))
6450 break;
6451 if (*s != NUL)
6452 ++s;
6453 }
6454 if (*s != NUL)
6455 break;
6456 --lnum;
6457 }
6458
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006459 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006460 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006461 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006462 for (;;)
6463 {
6464 if (*s == NUL)
6465 {
6466 if (lnum == curwin->w_cursor.lnum)
6467 break;
6468 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006469 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006470 s = line;
6471 }
6472 if (s == line)
6473 {
6474 /* don't recognize "case (foo):" as a baseclass */
6475 if (cin_iscase(s, FALSE))
6476 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006477 s = cin_skipcomment(line);
6478 if (*s == NUL)
6479 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006480 }
6481
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006482 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006483 s = skip_string(s) + 1;
6484 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 {
6486 if (s[1] == ':')
6487 {
6488 /* skip double colon. It can't be a constructor
6489 * initialization any more */
6490 lookfor_ctor_init = FALSE;
6491 s = cin_skipcomment(s + 2);
6492 }
6493 else if (lookfor_ctor_init || class_or_struct)
6494 {
6495 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006496 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 cpp_base_class = TRUE;
6498 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006499 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 s = cin_skipcomment(s + 1);
6501 }
6502 else
6503 s = cin_skipcomment(s + 1);
6504 }
6505 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6506 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6507 {
6508 class_or_struct = TRUE;
6509 lookfor_ctor_init = FALSE;
6510
6511 if (*s == 'c')
6512 s = cin_skipcomment(s + 5);
6513 else
6514 s = cin_skipcomment(s + 6);
6515 }
6516 else
6517 {
6518 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6519 {
6520 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6521 }
6522 else if (s[0] == ')')
6523 {
6524 /* Constructor-initialization is assumed if we come across
6525 * something like "):" */
6526 class_or_struct = FALSE;
6527 lookfor_ctor_init = TRUE;
6528 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006529 else if (s[0] == '?')
6530 {
6531 /* Avoid seeing '() :' after '?' as constructor init. */
6532 return FALSE;
6533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 else if (!vim_isIDc(s[0]))
6535 {
6536 /* if it is not an identifier, we are wrong */
6537 class_or_struct = FALSE;
6538 lookfor_ctor_init = FALSE;
6539 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006540 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
6542 /* it can't be a constructor-initialization any more */
6543 lookfor_ctor_init = FALSE;
6544
6545 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006546 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006547 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006550 /* When the line ends in a comma don't align with it. */
6551 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006552 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006553
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 s = cin_skipcomment(s + 1);
6555 }
6556 }
6557
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006558 cached->found = cpp_base_class;
6559 if (cpp_base_class)
6560 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 return cpp_base_class;
6562}
6563
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006564 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006565get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006566{
6567 int amount;
6568 colnr_T vcol;
6569 pos_T *trypos;
6570
6571 if (col == 0)
6572 {
6573 amount = get_indent();
6574 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006575 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006576 amount = get_indent_lnum(trypos->lnum); /* XXX */
6577 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006578 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006579 }
6580 else
6581 {
6582 curwin->w_cursor.col = col;
6583 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6584 amount = (int)vcol;
6585 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006586 if (amount < curbuf->b_ind_cpp_baseclass)
6587 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006588 return amount;
6589}
6590
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591/*
6592 * Return TRUE if string "s" ends with the string "find", possibly followed by
6593 * white space and comments. Skip strings and comments.
6594 * Ignore "ignore" after "find" if it's not NULL.
6595 */
6596 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006597cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 char_u *p = s;
6600 char_u *r;
6601 int len = (int)STRLEN(find);
6602
6603 while (*p != NUL)
6604 {
6605 p = cin_skipcomment(p);
6606 if (STRNCMP(p, find, len) == 0)
6607 {
6608 r = skipwhite(p + len);
6609 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6610 r = skipwhite(r + STRLEN(ignore));
6611 if (cin_nocode(r))
6612 return TRUE;
6613 }
6614 if (*p != NUL)
6615 ++p;
6616 }
6617 return FALSE;
6618}
6619
6620/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006621 * Return TRUE when "s" starts with "word" and then a non-ID character.
6622 */
6623 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006624cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006625{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006626 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006627
6628 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6629}
6630
6631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 * Skip strings, chars and comments until at or past "trypos".
6633 * Return the column found.
6634 */
6635 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006636cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 char_u *line;
6639 char_u *p;
6640
6641 p = line = ml_get(trypos->lnum);
6642 while (*p && (colnr_T)(p - line) < trypos->col)
6643 {
6644 if (cin_iscomment(p))
6645 p = cin_skipcomment(p);
6646 else
6647 {
6648 p = skip_string(p);
6649 ++p;
6650 }
6651 }
6652 return (int)(p - line);
6653}
6654
6655/*
6656 * Find the '{' at the start of the block we are in.
6657 * Return NULL if no match found.
6658 * Ignore a '{' that is in a comment, makes indenting the next three lines
6659 * work. */
6660/* foo() */
6661/* { */
6662/* } */
6663
6664 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006665find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 pos_T cursor_save;
6668 pos_T *trypos;
6669 pos_T *pos;
6670 static pos_T pos_copy;
6671
6672 cursor_save = curwin->w_cursor;
6673 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6674 {
6675 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6676 trypos = &pos_copy;
6677 curwin->w_cursor = *trypos;
6678 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006679 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006681 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 break;
6683 if (pos != NULL)
6684 curwin->w_cursor.lnum = pos->lnum;
6685 }
6686 curwin->w_cursor = cursor_save;
6687 return trypos;
6688}
6689
6690/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006691 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006692 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 */
6694 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006695find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006697 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006698}
6699
6700 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006702{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 pos_T cursor_save;
6704 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006705 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006706 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006709 ind_maxp_wk = ind_maxparen;
6710retry:
6711 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {
6713 /* check if the ( is in a // comment */
6714 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006715 {
6716 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6717 if (ind_maxp_wk > 0)
6718 {
6719 curwin->w_cursor = *trypos;
6720 curwin->w_cursor.col = 0; /* XXX */
6721 goto retry;
6722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 else
6726 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006727 pos_T *trypos_wk;
6728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6730 trypos = &pos_copy;
6731 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006732 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006733 {
6734 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6735 - trypos_wk->lnum);
6736 if (ind_maxp_wk > 0)
6737 {
6738 curwin->w_cursor = *trypos_wk;
6739 goto retry;
6740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744 }
6745 curwin->w_cursor = cursor_save;
6746 return trypos;
6747}
6748
6749/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006750 * Find the matching '(', ignoring it if it is in a comment or before an
6751 * unmatched {.
6752 * Return NULL if no match found.
6753 */
6754 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006755find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006756{
6757 pos_T *trypos = find_match_paren(ind_maxparen);
6758
6759 if (trypos != NULL)
6760 {
6761 pos_T *tryposBrace = find_start_brace();
6762
6763 /* If both an unmatched '(' and '{' is found. Ignore the '('
6764 * position if the '{' is further down. */
6765 if (tryposBrace != NULL
6766 && (trypos->lnum != tryposBrace->lnum
6767 ? trypos->lnum < tryposBrace->lnum
6768 : trypos->col < tryposBrace->col))
6769 trypos = NULL;
6770 }
6771 return trypos;
6772}
6773
6774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 * Return ind_maxparen corrected for the difference in line number between the
6776 * cursor position and "startpos". This makes sure that searching for a
6777 * matching paren above the cursor line doesn't find a match because of
6778 * looking a few lines further.
6779 */
6780 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006781corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
6783 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6784
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006785 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6786 return curbuf->b_ind_maxparen - (int)n;
6787 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788}
6789
6790/*
6791 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006792 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 */
6794 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006795find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 int i;
6798 int retval = FALSE;
6799 int open_count = 0;
6800
6801 curwin->w_cursor.col = 0; /* default is start of line */
6802
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006803 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
6805 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6806 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6807 if (l[i] == start)
6808 ++open_count;
6809 else if (l[i] == end)
6810 {
6811 if (open_count > 0)
6812 --open_count;
6813 else
6814 {
6815 curwin->w_cursor.col = i;
6816 retval = TRUE;
6817 }
6818 }
6819 }
6820 return retval;
6821}
6822
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006823/*
6824 * Parse 'cinoptions' and set the values in "curbuf".
6825 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6826 */
6827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006828parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006829{
6830 char_u *p;
6831 char_u *l;
6832 char_u *digits;
6833 int n;
6834 int divider;
6835 int fraction = 0;
6836 int sw = (int)get_sw_value(buf);
6837
6838 /*
6839 * Set the default values.
6840 */
6841 /* Spaces from a block's opening brace the prevailing indent for that
6842 * block should be. */
6843 buf->b_ind_level = sw;
6844
6845 /* Spaces from the edge of the line an open brace that's at the end of a
6846 * line is imagined to be. */
6847 buf->b_ind_open_imag = 0;
6848
6849 /* Spaces from the prevailing indent for a line that is not preceded by
6850 * an opening brace. */
6851 buf->b_ind_no_brace = 0;
6852
6853 /* Column where the first { of a function should be located }. */
6854 buf->b_ind_first_open = 0;
6855
6856 /* Spaces from the prevailing indent a leftmost open brace should be
6857 * located. */
6858 buf->b_ind_open_extra = 0;
6859
6860 /* Spaces from the matching open brace (real location for one at the left
6861 * edge; imaginary location from one that ends a line) the matching close
6862 * brace should be located. */
6863 buf->b_ind_close_extra = 0;
6864
6865 /* Spaces from the edge of the line an open brace sitting in the leftmost
6866 * column is imagined to be. */
6867 buf->b_ind_open_left_imag = 0;
6868
6869 /* Spaces jump labels should be shifted to the left if N is non-negative,
6870 * otherwise the jump label will be put to column 1. */
6871 buf->b_ind_jump_label = -1;
6872
6873 /* Spaces from the switch() indent a "case xx" label should be located. */
6874 buf->b_ind_case = sw;
6875
6876 /* Spaces from the "case xx:" code after a switch() should be located. */
6877 buf->b_ind_case_code = sw;
6878
6879 /* Lineup break at end of case in switch() with case label. */
6880 buf->b_ind_case_break = 0;
6881
6882 /* Spaces from the class declaration indent a scope declaration label
6883 * should be located. */
6884 buf->b_ind_scopedecl = sw;
6885
6886 /* Spaces from the scope declaration label code should be located. */
6887 buf->b_ind_scopedecl_code = sw;
6888
6889 /* Amount K&R-style parameters should be indented. */
6890 buf->b_ind_param = sw;
6891
6892 /* Amount a function type spec should be indented. */
6893 buf->b_ind_func_type = sw;
6894
6895 /* Amount a cpp base class declaration or constructor initialization
6896 * should be indented. */
6897 buf->b_ind_cpp_baseclass = sw;
6898
6899 /* additional spaces beyond the prevailing indent a continuation line
6900 * should be located. */
6901 buf->b_ind_continuation = sw;
6902
6903 /* Spaces from the indent of the line with an unclosed parentheses. */
6904 buf->b_ind_unclosed = sw * 2;
6905
6906 /* Spaces from the indent of the line with an unclosed parentheses, which
6907 * itself is also unclosed. */
6908 buf->b_ind_unclosed2 = sw;
6909
6910 /* Suppress ignoring spaces from the indent of a line starting with an
6911 * unclosed parentheses. */
6912 buf->b_ind_unclosed_noignore = 0;
6913
6914 /* If the opening paren is the last nonwhite character on the line, and
6915 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6916 * context (for very long lines). */
6917 buf->b_ind_unclosed_wrapped = 0;
6918
6919 /* Suppress ignoring white space when lining up with the character after
6920 * an unclosed parentheses. */
6921 buf->b_ind_unclosed_whiteok = 0;
6922
6923 /* Indent a closing parentheses under the line start of the matching
6924 * opening parentheses. */
6925 buf->b_ind_matching_paren = 0;
6926
6927 /* Indent a closing parentheses under the previous line. */
6928 buf->b_ind_paren_prev = 0;
6929
6930 /* Extra indent for comments. */
6931 buf->b_ind_comment = 0;
6932
6933 /* Spaces from the comment opener when there is nothing after it. */
6934 buf->b_ind_in_comment = 3;
6935
6936 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6937 * after the comment opener. */
6938 buf->b_ind_in_comment2 = 0;
6939
6940 /* Max lines to search for an open paren. */
6941 buf->b_ind_maxparen = 20;
6942
6943 /* Max lines to search for an open comment. */
6944 buf->b_ind_maxcomment = 70;
6945
6946 /* Handle braces for java code. */
6947 buf->b_ind_java = 0;
6948
6949 /* Not to confuse JS object properties with labels. */
6950 buf->b_ind_js = 0;
6951
6952 /* Handle blocked cases correctly. */
6953 buf->b_ind_keep_case_label = 0;
6954
6955 /* Handle C++ namespace. */
6956 buf->b_ind_cpp_namespace = 0;
6957
6958 /* Handle continuation lines containing conditions of if(), for() and
6959 * while(). */
6960 buf->b_ind_if_for_while = 0;
6961
6962 for (p = buf->b_p_cino; *p; )
6963 {
6964 l = p++;
6965 if (*p == '-')
6966 ++p;
6967 digits = p; /* remember where the digits start */
6968 n = getdigits(&p);
6969 divider = 0;
6970 if (*p == '.') /* ".5s" means a fraction */
6971 {
6972 fraction = atol((char *)++p);
6973 while (VIM_ISDIGIT(*p))
6974 {
6975 ++p;
6976 if (divider)
6977 divider *= 10;
6978 else
6979 divider = 10;
6980 }
6981 }
6982 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6983 {
6984 if (p == digits)
6985 n = sw; /* just "s" is one 'shiftwidth' */
6986 else
6987 {
6988 n *= sw;
6989 if (divider)
6990 n += (sw * fraction + divider / 2) / divider;
6991 }
6992 ++p;
6993 }
6994 if (l[1] == '-')
6995 n = -n;
6996
6997 /* When adding an entry here, also update the default 'cinoptions' in
6998 * doc/indent.txt, and add explanation for it! */
6999 switch (*l)
7000 {
7001 case '>': buf->b_ind_level = n; break;
7002 case 'e': buf->b_ind_open_imag = n; break;
7003 case 'n': buf->b_ind_no_brace = n; break;
7004 case 'f': buf->b_ind_first_open = n; break;
7005 case '{': buf->b_ind_open_extra = n; break;
7006 case '}': buf->b_ind_close_extra = n; break;
7007 case '^': buf->b_ind_open_left_imag = n; break;
7008 case 'L': buf->b_ind_jump_label = n; break;
7009 case ':': buf->b_ind_case = n; break;
7010 case '=': buf->b_ind_case_code = n; break;
7011 case 'b': buf->b_ind_case_break = n; break;
7012 case 'p': buf->b_ind_param = n; break;
7013 case 't': buf->b_ind_func_type = n; break;
7014 case '/': buf->b_ind_comment = n; break;
7015 case 'c': buf->b_ind_in_comment = n; break;
7016 case 'C': buf->b_ind_in_comment2 = n; break;
7017 case 'i': buf->b_ind_cpp_baseclass = n; break;
7018 case '+': buf->b_ind_continuation = n; break;
7019 case '(': buf->b_ind_unclosed = n; break;
7020 case 'u': buf->b_ind_unclosed2 = n; break;
7021 case 'U': buf->b_ind_unclosed_noignore = n; break;
7022 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7023 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7024 case 'm': buf->b_ind_matching_paren = n; break;
7025 case 'M': buf->b_ind_paren_prev = n; break;
7026 case ')': buf->b_ind_maxparen = n; break;
7027 case '*': buf->b_ind_maxcomment = n; break;
7028 case 'g': buf->b_ind_scopedecl = n; break;
7029 case 'h': buf->b_ind_scopedecl_code = n; break;
7030 case 'j': buf->b_ind_java = n; break;
7031 case 'J': buf->b_ind_js = n; break;
7032 case 'l': buf->b_ind_keep_case_label = n; break;
7033 case '#': buf->b_ind_hash_comment = n; break;
7034 case 'N': buf->b_ind_cpp_namespace = n; break;
7035 case 'k': buf->b_ind_if_for_while = n; break;
7036 }
7037 if (*p == ',')
7038 ++p;
7039 }
7040}
7041
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007042/*
7043 * Return the desired indent for C code.
7044 * Return -1 if the indent should be left alone (inside a raw string).
7045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007047get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 pos_T cur_curpos;
7050 int amount;
7051 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007052 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 colnr_T col;
7054 char_u *theline;
7055 char_u *linecopy;
7056 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007057 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007059 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 pos_T our_paren_pos;
7061 char_u *start;
7062 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007063#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064#define BRACE_AT_START 2 /* '{' is at start of line */
7065#define BRACE_AT_END 3 /* '{' is at end of line */
7066 linenr_T ourscope;
7067 char_u *l;
7068 char_u *look;
7069 char_u terminated;
7070 int lookfor;
7071#define LOOKFOR_INITIAL 0
7072#define LOOKFOR_IF 1
7073#define LOOKFOR_DO 2
7074#define LOOKFOR_CASE 3
7075#define LOOKFOR_ANY 4
7076#define LOOKFOR_TERM 5
7077#define LOOKFOR_UNTERM 6
7078#define LOOKFOR_SCOPEDECL 7
7079#define LOOKFOR_NOBREAK 8
7080#define LOOKFOR_CPP_BASECLASS 9
7081#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007082#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007083#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
7085 int whilelevel;
7086 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 int n;
7088 int iscase;
7089 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007090 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007092 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007093 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007094 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007095 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007097 /* make a copy, value is changed below */
7098 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
7100 /* remember where the cursor was when we started */
7101 cur_curpos = curwin->w_cursor;
7102
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007103 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007104 if (cur_curpos.lnum == 1)
7105 return 0;
7106
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 /* Get a copy of the current contents of the line.
7108 * This is required, because only the most recent line obtained with
7109 * ml_get is valid! */
7110 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7111 if (linecopy == NULL)
7112 return 0;
7113
7114 /*
7115 * In insert mode and the cursor is on a ')' truncate the line at the
7116 * cursor position. We don't want to line up with the matching '(' when
7117 * inserting new stuff.
7118 * For unknown reasons the cursor might be past the end of the line, thus
7119 * check for that.
7120 */
7121 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007122 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 && linecopy[curwin->w_cursor.col] == ')')
7124 linecopy[curwin->w_cursor.col] = NUL;
7125
7126 theline = skipwhite(linecopy);
7127
7128 /* move the cursor to the start of the line */
7129
7130 curwin->w_cursor.col = 0;
7131
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007132 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007135 * If we are inside a raw string don't change the indent.
7136 * Ignore a raw string inside a comment.
7137 */
7138 comment_pos = ind_find_start_comment();
7139 if (comment_pos != NULL)
7140 {
7141 /* findmatchlimit() static pos is overwritten, make a copy */
7142 tryposCopy = *comment_pos;
7143 comment_pos = &tryposCopy;
7144 }
7145 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7146 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7147 {
7148 amount = -1;
7149 goto laterend;
7150 }
7151
7152 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 * #defines and so on always go at the left when included in 'cinkeys'.
7154 */
7155 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007156 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007157 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007158 goto theend;
7159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
7161 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007162 * Is it a non-case label? Then that goes at the left margin too unless:
7163 * - JS flag is set.
7164 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007166 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007167 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {
7169 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007170 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 }
7172
7173 /*
7174 * If we're inside a "//" comment and there is a "//" comment in a
7175 * previous line, lineup with that one.
7176 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007177 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 && (trypos = find_line_comment()) != NULL) /* XXX */
7179 {
7180 /* find how indented the line beginning the comment is */
7181 getvcol(curwin, trypos, &col, NULL, NULL);
7182 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007183 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
7185
7186 /*
7187 * If we're inside a comment and not looking at the start of the
7188 * comment, try using the 'comments' option.
7189 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007190 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
7192 int lead_start_len = 2;
7193 int lead_middle_len = 1;
7194 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7195 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7196 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7197 char_u *p;
7198 int start_align = 0;
7199 int start_off = 0;
7200 int done = FALSE;
7201
7202 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007203 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007205 *lead_start = NUL;
7206 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
7208 p = curbuf->b_p_com;
7209 while (*p != NUL)
7210 {
7211 int align = 0;
7212 int off = 0;
7213 int what = 0;
7214
7215 while (*p != NUL && *p != ':')
7216 {
7217 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7218 what = *p++;
7219 else if (*p == COM_LEFT || *p == COM_RIGHT)
7220 align = *p++;
7221 else if (VIM_ISDIGIT(*p) || *p == '-')
7222 off = getdigits(&p);
7223 else
7224 ++p;
7225 }
7226
7227 if (*p == ':')
7228 ++p;
7229 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7230 if (what == COM_START)
7231 {
7232 STRCPY(lead_start, lead_end);
7233 lead_start_len = (int)STRLEN(lead_start);
7234 start_off = off;
7235 start_align = align;
7236 }
7237 else if (what == COM_MIDDLE)
7238 {
7239 STRCPY(lead_middle, lead_end);
7240 lead_middle_len = (int)STRLEN(lead_middle);
7241 }
7242 else if (what == COM_END)
7243 {
7244 /* If our line starts with the middle comment string, line it
7245 * up with the comment opener per the 'comments' option. */
7246 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7247 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7248 {
7249 done = TRUE;
7250 if (curwin->w_cursor.lnum > 1)
7251 {
7252 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007253 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 * the middle comment string matches in the previous
7255 * line, use the indent of that line. XXX */
7256 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7257 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7258 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7259 else if (STRNCMP(look, lead_middle,
7260 lead_middle_len) == 0)
7261 {
7262 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7263 break;
7264 }
7265 /* If the start comment string doesn't match with the
7266 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007267 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 lead_start, lead_start_len) != 0)
7269 continue;
7270 }
7271 if (start_off != 0)
7272 amount += start_off;
7273 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007274 amount += vim_strsize(lead_start)
7275 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 break;
7277 }
7278
7279 /* If our line starts with the end comment string, line it up
7280 * with the middle comment */
7281 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7282 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7283 {
7284 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7285 /* XXX */
7286 if (off != 0)
7287 amount += off;
7288 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007289 amount += vim_strsize(lead_start)
7290 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 done = TRUE;
7292 break;
7293 }
7294 }
7295 }
7296
7297 /* If our line starts with an asterisk, line up with the
7298 * asterisk in the comment opener; otherwise, line up
7299 * with the first character of the comment text.
7300 */
7301 if (done)
7302 ;
7303 else if (theline[0] == '*')
7304 amount += 1;
7305 else
7306 {
7307 /*
7308 * If we are more than one line away from the comment opener, take
7309 * the indent of the previous non-empty line. If 'cino' has "CO"
7310 * and we are just below the comment opener and there are any
7311 * white characters after it line up with the text after it;
7312 * otherwise, add the amount specified by "c" in 'cino'
7313 */
7314 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007315 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
7317 if (linewhite(lnum)) /* skip blank lines */
7318 continue;
7319 amount = get_indent_lnum(lnum); /* XXX */
7320 break;
7321 }
7322 if (amount == -1) /* use the comment opener */
7323 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007324 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007326 start = ml_get(comment_pos->lnum);
7327 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007329 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007331 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007333 if (curbuf->b_ind_in_comment2 || *look == NUL)
7334 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
7336 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007337 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
7339
7340 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007341 * Are we looking at a ']' that has a match?
7342 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007343 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007344 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7345 {
7346 /* align with the line containing the '['. */
7347 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007348 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007349 }
7350
7351 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 * Are we inside parentheses or braces?
7353 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007354 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007355 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007356 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 || trypos != NULL)
7358 {
7359 if (trypos != NULL && tryposBrace != NULL)
7360 {
7361 /* Both an unmatched '(' and '{' is found. Use the one which is
7362 * closer to the current cursor position, set the other to NULL. */
7363 if (trypos->lnum != tryposBrace->lnum
7364 ? trypos->lnum < tryposBrace->lnum
7365 : trypos->col < tryposBrace->col)
7366 trypos = NULL;
7367 else
7368 tryposBrace = NULL;
7369 }
7370
7371 if (trypos != NULL)
7372 {
7373 /*
7374 * If the matching paren is more than one line away, use the indent of
7375 * a previous non-empty line that matches the same paren.
7376 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007377 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007379 /* Line up with the start of the matching paren line. */
7380 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7381 }
7382 else
7383 {
7384 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007385 our_paren_pos = *trypos;
7386 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007388 l = skipwhite(ml_get(lnum));
7389 if (cin_nocode(l)) /* skip comment lines */
7390 continue;
7391 if (cin_ispreproc_cont(&l, &lnum))
7392 continue; /* ignore #define, #if, etc. */
7393 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007395 /* Skip a comment or raw string. XXX */
7396 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007397 {
7398 lnum = trypos->lnum + 1;
7399 continue;
7400 }
7401
7402 /* XXX */
7403 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007404 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007405 && trypos->lnum == our_paren_pos.lnum
7406 && trypos->col == our_paren_pos.col)
7407 {
7408 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007410 if (theline[0] == ')')
7411 {
7412 if (our_paren_pos.lnum != lnum
7413 && cur_amount > amount)
7414 cur_amount = amount;
7415 amount = -1;
7416 }
7417 break;
7418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 }
7420 }
7421
7422 /*
7423 * Line up with line where the matching paren is. XXX
7424 * If the line starts with a '(' or the indent for unclosed
7425 * parentheses is zero, line up with the unclosed parentheses.
7426 */
7427 if (amount == -1)
7428 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007429 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007430 int is_if_for_while = 0;
7431
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007432 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007433 {
7434 /* Look for the outermost opening parenthesis on this line
7435 * and check whether it belongs to an "if", "for" or "while". */
7436
7437 pos_T cursor_save = curwin->w_cursor;
7438 pos_T outermost;
7439 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007440
7441 trypos = &our_paren_pos;
7442 do {
7443 outermost = *trypos;
7444 curwin->w_cursor.lnum = outermost.lnum;
7445 curwin->w_cursor.col = outermost.col;
7446
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007447 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007448 } while (trypos && trypos->lnum == outermost.lnum);
7449
7450 curwin->w_cursor = cursor_save;
7451
7452 line = ml_get(outermost.lnum);
7453
7454 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007455 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007456 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007457
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007458 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007459 look = skipwhite(look);
7460 if (*look == '(')
7461 {
7462 linenr_T save_lnum = curwin->w_cursor.lnum;
7463 char_u *line;
7464 int look_col;
7465
7466 /* Ignore a '(' in front of the line that has a match before
7467 * our matching '('. */
7468 curwin->w_cursor.lnum = our_paren_pos.lnum;
7469 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007470 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007471 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007472 if ((trypos = findmatchlimit(NULL, ')', 0,
7473 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007474 != NULL
7475 && trypos->lnum == our_paren_pos.lnum
7476 && trypos->col < our_paren_pos.col)
7477 ignore_paren_col = trypos->col + 1;
7478
7479 curwin->w_cursor.lnum = save_lnum;
7480 look = ml_get(our_paren_pos.lnum) + look_col;
7481 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007482 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7483 && is_if_for_while == 0)
7484 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007485 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 {
7487 /*
7488 * If we're looking at a close paren, line up right there;
7489 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007490 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 * the last nonwhite character of the line, use either the
7492 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007493 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 * lines).
7495 */
7496 if (theline[0] != ')')
7497 {
7498 cur_amount = MAXCOL;
7499 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007500 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 && cin_ends_in(l, (char_u *)"(", NULL))
7502 {
7503 /* look for opening unmatched paren, indent one level
7504 * for each additional level */
7505 n = 1;
7506 for (col = 0; col < our_paren_pos.col; ++col)
7507 {
7508 switch (l[col])
7509 {
7510 case '(':
7511 case '{': ++n;
7512 break;
7513
7514 case ')':
7515 case '}': if (n > 1)
7516 --n;
7517 break;
7518 }
7519 }
7520
7521 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007522 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007524 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 our_paren_pos.col++;
7526 else
7527 {
7528 col = our_paren_pos.col + 1;
7529 while (vim_iswhite(l[col]))
7530 col++;
7531 if (l[col] != NUL) /* In case of trailing space */
7532 our_paren_pos.col = col;
7533 else
7534 our_paren_pos.col++;
7535 }
7536 }
7537
7538 /*
7539 * Find how indented the paren is, or the character after it
7540 * if we did the above "if".
7541 */
7542 if (our_paren_pos.col > 0)
7543 {
7544 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7545 if (cur_amount > (int)col)
7546 cur_amount = col;
7547 }
7548 }
7549
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007550 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {
7552 /* Line up with the start of the matching paren line. */
7553 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007554 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7555 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007556 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 {
7558 if (cur_amount != MAXCOL)
7559 amount = cur_amount;
7560 }
7561 else
7562 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007563 /* Add b_ind_unclosed2 for each '(' before our matching one,
7564 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007566 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {
7568 --our_paren_pos.col;
7569 switch (*ml_get_pos(&our_paren_pos))
7570 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007571 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 col = our_paren_pos.col;
7573 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007574 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 col = MAXCOL;
7576 break;
7577 }
7578 }
7579
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007580 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 * braces */
7582 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007583 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 else
7585 {
7586 curwin->w_cursor.lnum = our_paren_pos.lnum;
7587 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007588 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7589 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007590 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007592 {
7593 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007594 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007595 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007596 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 }
7599 /*
7600 * For a line starting with ')' use the minimum of the two
7601 * positions, to avoid giving it more indent than the previous
7602 * lines:
7603 * func_long_name( if (x
7604 * arg && yy
7605 * ) ^ not here ) ^ not here
7606 */
7607 if (cur_amount < amount)
7608 amount = cur_amount;
7609 }
7610 }
7611
7612 /* add extra indent for a comment */
7613 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007614 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 else
7617 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007618 /*
7619 * We are inside braces, there is a { before this line at the position
7620 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007621 * Make a copy of tryposBrace, it may point to pos_copy inside
7622 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007623 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007624 tryposCopy = *tryposBrace;
7625 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 ourscope = trypos->lnum;
7628 start = ml_get(ourscope);
7629
7630 /*
7631 * Now figure out how indented the line is in general.
7632 * If the brace was at the start of the line, we use that;
7633 * otherwise, check out the indentation of the line as
7634 * a whole and then add the "imaginary indent" to that.
7635 */
7636 look = skipwhite(start);
7637 if (*look == '{')
7638 {
7639 getvcol(curwin, trypos, &col, NULL, NULL);
7640 amount = col;
7641 if (*start == '{')
7642 start_brace = BRACE_IN_COL0;
7643 else
7644 start_brace = BRACE_AT_START;
7645 }
7646 else
7647 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007648 /* That opening brace might have been on a continuation
7649 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 curwin->w_cursor.lnum = ourscope;
7651
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007652 /* Position the cursor over the rightmost paren, so that
7653 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 lnum = ourscope;
7655 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007656 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7657 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 lnum = trypos->lnum;
7659
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007660 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 * case 1: if (asdf &&
7662 * ldfd) {
7663 * }
7664 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007665 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7666 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007668 else if (curbuf->b_ind_js)
7669 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007671 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
7673 start_brace = BRACE_AT_END;
7674 }
7675
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007676 /* For Javascript check if the line starts with "key:". */
7677 if (curbuf->b_ind_js)
7678 js_cur_has_key = cin_has_js_key(theline);
7679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007681 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 * we want to be. otherwise, add the amount of room
7683 * that an indent is supposed to be.
7684 */
7685 if (theline[0] == '}')
7686 {
7687 /*
7688 * they may want closing braces to line up with something
7689 * other than the open brace. indulge them, if so.
7690 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007691 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693 else
7694 {
7695 /*
7696 * If we're looking at an "else", try to find an "if"
7697 * to match it with.
7698 * If we're looking at a "while", try to find a "do"
7699 * to match it with.
7700 */
7701 lookfor = LOOKFOR_INITIAL;
7702 if (cin_iselse(theline))
7703 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007704 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 lookfor = LOOKFOR_DO;
7706 if (lookfor != LOOKFOR_INITIAL)
7707 {
7708 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007709 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {
7711 amount = get_indent(); /* XXX */
7712 goto theend;
7713 }
7714 }
7715
7716 /*
7717 * We get here if we are not on an "while-of-do" or "else" (or
7718 * failed to find a matching "if").
7719 * Search backwards for something to line up with.
7720 * First set amount for when we don't find anything.
7721 */
7722
7723 /*
7724 * if the '{' is _really_ at the left margin, use the imaginary
7725 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007726 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 */
7728
7729 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7730 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007731 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007732 lookfor_cpp_namespace = TRUE;
7733 }
7734 else if (start_brace == BRACE_AT_START &&
7735 lookfor_cpp_namespace) /* '{' is at start */
7736 {
7737
7738 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740 else
7741 {
7742 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007743 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007744 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007745
7746 l = skipwhite(ml_get_curline());
7747 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007748 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 else
7751 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007752 /* Compensate for adding b_ind_open_extra later. */
7753 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 if (amount < 0)
7755 amount = 0;
7756 }
7757 }
7758
7759 lookfor_break = FALSE;
7760
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007761 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {
7763 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007764 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
7766 else if (cin_isscopedecl(theline)) /* private:, ... */
7767 {
7768 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007769 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 }
7771 else
7772 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007773 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7774 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 lookfor_break = TRUE;
7776
7777 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007778 /* b_ind_level from start of block */
7779 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781 scope_amount = amount;
7782 whilelevel = 0;
7783
7784 /*
7785 * Search backwards. If we find something we recognize, line up
7786 * with that.
7787 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007788 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 * the usual amount relative to the conditional
7790 * that opens the block.
7791 */
7792 curwin->w_cursor = cur_curpos;
7793 for (;;)
7794 {
7795 curwin->w_cursor.lnum--;
7796 curwin->w_cursor.col = 0;
7797
7798 /*
7799 * If we went all the way back to the start of our scope, line
7800 * up with it.
7801 */
7802 if (curwin->w_cursor.lnum <= ourscope)
7803 {
7804 /* we reached end of scope:
7805 * if looking for a enum or structure initialization
7806 * go further back:
7807 * if it is an initializer (enum xxx or xxx =), then
7808 * don't add ind_continuation, otherwise it is a variable
7809 * declaration:
7810 * int x,
7811 * here; <-- add ind_continuation
7812 */
7813 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7814 {
7815 if (curwin->w_cursor.lnum == 0
7816 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007817 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007819 /* nothing found (abuse curbuf->b_ind_maxparen as
7820 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 * initialization) */
7822 if (cont_amount > 0)
7823 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007824 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 amount += ind_continuation;
7826 break;
7827 }
7828
7829 l = ml_get_curline();
7830
7831 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007832 * If we're in a comment or raw string now, skip to
7833 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007835 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 if (trypos != NULL)
7837 {
7838 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007839 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 continue;
7841 }
7842
7843 /*
7844 * Skip preprocessor directives and blank lines.
7845 */
7846 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7847 continue;
7848
7849 if (cin_nocode(l))
7850 continue;
7851
7852 terminated = cin_isterminated(l, FALSE, TRUE);
7853
7854 /*
7855 * If we are at top level and the line looks like a
7856 * function declaration, we are done
7857 * (it's a variable declaration).
7858 */
7859 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007860 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {
7862 /* if the line is terminated with another ','
7863 * it is a continued variable initialization.
7864 * don't add extra indent.
7865 * TODO: does not work, if a function
7866 * declaration is split over multiple lines:
7867 * cin_isfuncdecl returns FALSE then.
7868 */
7869 if (terminated == ',')
7870 break;
7871
7872 /* if it es a enum declaration or an assignment,
7873 * we are done.
7874 */
7875 if (terminated != ';' && cin_isinit())
7876 break;
7877
7878 /* nothing useful found */
7879 if (terminated == 0 || terminated == '{')
7880 continue;
7881 }
7882
7883 if (terminated != ';')
7884 {
7885 /* Skip parens and braces. Position the cursor
7886 * over the rightmost paren, so that matching it
7887 * will take us back to the start of the line.
7888 */ /* XXX */
7889 trypos = NULL;
7890 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007891 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007892 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893
7894 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007895 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896
7897 if (trypos != NULL)
7898 {
7899 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007900 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 continue;
7902 }
7903 }
7904
7905 /* it's a variable declaration, add indentation
7906 * like in
7907 * int a,
7908 * b;
7909 */
7910 if (cont_amount > 0)
7911 amount = cont_amount;
7912 else
7913 amount += ind_continuation;
7914 }
7915 else if (lookfor == LOOKFOR_UNTERM)
7916 {
7917 if (cont_amount > 0)
7918 amount = cont_amount;
7919 else
7920 amount += ind_continuation;
7921 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007922 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007923 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007924 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007925 && lookfor != LOOKFOR_CPP_BASECLASS
7926 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007927 {
7928 amount = scope_amount;
7929 if (theline[0] == '{')
7930 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007931 amount += curbuf->b_ind_open_extra;
7932 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007933 }
7934 }
7935
7936 if (lookfor_cpp_namespace)
7937 {
7938 /*
7939 * Looking for C++ namespace, need to look further
7940 * back.
7941 */
7942 if (curwin->w_cursor.lnum == ourscope)
7943 continue;
7944
7945 if (curwin->w_cursor.lnum == 0
7946 || curwin->w_cursor.lnum
7947 < ourscope - FIND_NAMESPACE_LIM)
7948 break;
7949
7950 l = ml_get_curline();
7951
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007952 /* If we're in a comment or raw string now, skip
7953 * to the start of it. */
7954 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007955 if (trypos != NULL)
7956 {
7957 curwin->w_cursor.lnum = trypos->lnum + 1;
7958 curwin->w_cursor.col = 0;
7959 continue;
7960 }
7961
7962 /* Skip preprocessor directives and blank lines. */
7963 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7964 continue;
7965
7966 /* Finally the actual check for "namespace". */
7967 if (cin_is_cpp_namespace(l))
7968 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007969 amount += curbuf->b_ind_cpp_namespace
7970 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007971 break;
7972 }
7973
7974 if (cin_nocode(l))
7975 continue;
7976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 }
7978 break;
7979 }
7980
7981 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007982 * If we're in a comment or raw string now, skip to the start
7983 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007985 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
7987 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007988 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 continue;
7990 }
7991
7992 l = ml_get_curline();
7993
7994 /*
7995 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007996 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007998 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (iscase || cin_isscopedecl(l))
8000 {
8001 /* we are only looking for cpp base class
8002 * declaration/initialization any longer */
8003 if (lookfor == LOOKFOR_CPP_BASECLASS)
8004 break;
8005
8006 /* When looking for a "do" we are not interested in
8007 * labels. */
8008 if (whilelevel > 0)
8009 continue;
8010
8011 /*
8012 * case xx:
8013 * c = 99 + <- this indent plus continuation
8014 *-> here;
8015 */
8016 if (lookfor == LOOKFOR_UNTERM
8017 || lookfor == LOOKFOR_ENUM_OR_INIT)
8018 {
8019 if (cont_amount > 0)
8020 amount = cont_amount;
8021 else
8022 amount += ind_continuation;
8023 break;
8024 }
8025
8026 /*
8027 * case xx: <- line up with this case
8028 * x = 333;
8029 * case yy:
8030 */
8031 if ( (iscase && lookfor == LOOKFOR_CASE)
8032 || (iscase && lookfor_break)
8033 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8034 {
8035 /*
8036 * Check that this case label is not for another
8037 * switch()
8038 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008039 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008040 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {
8042 amount = get_indent(); /* XXX */
8043 break;
8044 }
8045 continue;
8046 }
8047
8048 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8049
8050 /*
8051 * case xx: if (cond) <- line up with this if
8052 * y = y + 1;
8053 * -> s = 99;
8054 *
8055 * case xx:
8056 * if (cond) <- line up with this line
8057 * y = y + 1;
8058 * -> s = 99;
8059 */
8060 if (lookfor == LOOKFOR_TERM)
8061 {
8062 if (n)
8063 amount = n;
8064
8065 if (!lookfor_break)
8066 break;
8067 }
8068
8069 /*
8070 * case xx: x = x + 1; <- line up with this x
8071 * -> y = y + 1;
8072 *
8073 * case xx: if (cond) <- line up with this if
8074 * -> y = y + 1;
8075 */
8076 if (n)
8077 {
8078 amount = n;
8079 l = after_label(ml_get_curline());
8080 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008081 {
8082 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008083 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008084 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008085 amount += curbuf->b_ind_level
8086 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 break;
8089 }
8090
8091 /*
8092 * Try to get the indent of a statement before the switch
8093 * label. If nothing is found, line up relative to the
8094 * switch label.
8095 * break; <- may line up with this line
8096 * case xx:
8097 * -> y = 1;
8098 */
8099 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008100 ? curbuf->b_ind_case_code
8101 : curbuf->b_ind_scopedecl_code);
8102 lookfor = curbuf->b_ind_case_break
8103 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 continue;
8105 }
8106
8107 /*
8108 * Looking for a switch() label or C++ scope declaration,
8109 * ignore other lines, skip {}-blocks.
8110 */
8111 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8112 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008113 if (find_last_paren(l, '{', '}')
8114 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008117 curwin->w_cursor.col = 0;
8118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 continue;
8120 }
8121
8122 /*
8123 * Ignore jump labels with nothing after them.
8124 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008125 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {
8127 l = after_label(ml_get_curline());
8128 if (l == NULL || cin_nocode(l))
8129 continue;
8130 }
8131
8132 /*
8133 * Ignore #defines, #if, etc.
8134 * Ignore comment and empty lines.
8135 * (need to get the line again, cin_islabel() may have
8136 * unlocked it)
8137 */
8138 l = ml_get_curline();
8139 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
8140 || cin_nocode(l))
8141 continue;
8142
8143 /*
8144 * Are we at the start of a cpp base class declaration or
8145 * constructor initialization?
8146 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008147 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008148 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008149 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008150 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008151 l = ml_get_curline();
8152 }
8153 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
8155 if (lookfor == LOOKFOR_UNTERM)
8156 {
8157 if (cont_amount > 0)
8158 amount = cont_amount;
8159 else
8160 amount += ind_continuation;
8161 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008162 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008164 /* Need to find start of the declaration. */
8165 lookfor = LOOKFOR_UNTERM;
8166 ind_continuation = 0;
8167 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 }
8169 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008170 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008171 amount = get_baseclass_amount(
8172 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 break;
8174 }
8175 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8176 {
8177 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008178 * declaration or initialization before the opening brace.
8179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (cin_isterminated(l, TRUE, FALSE))
8181 break;
8182 else
8183 continue;
8184 }
8185
8186 /*
8187 * What happens next depends on the line being terminated.
8188 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008189 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 * 123,
8191 * sizeof
8192 * here
8193 * Otherwise check whether it is a enumeration or structure
8194 * initialisation (not indented) or a variable declaration
8195 * (indented).
8196 */
8197 terminated = cin_isterminated(l, FALSE, TRUE);
8198
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008199 if (js_cur_has_key)
8200 {
8201 js_cur_has_key = 0; /* only check the first line */
8202 if (curbuf->b_ind_js && terminated == ',')
8203 {
8204 /* For Javascript we might be inside an object:
8205 * key: something, <- align with this
8206 * key: something
8207 * or:
8208 * key: something + <- align with this
8209 * something,
8210 * key: something
8211 */
8212 lookfor = LOOKFOR_JS_KEY;
8213 }
8214 }
8215 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8216 {
8217 amount = get_indent();
8218 break;
8219 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008220 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008221 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008222 if (tryposBrace != NULL && tryposBrace->lnum
8223 >= curwin->w_cursor.lnum)
8224 break;
8225 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008226 /* line below current line is the one that starts a
8227 * (possibly broken) line ending in a comma. */
8228 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008229 else
8230 {
8231 amount = get_indent();
8232 if (curwin->w_cursor.lnum - 1 == ourscope)
8233 /* line above is start of the scope, thus current
8234 * line is the one that stars a (possibly broken)
8235 * line ending in a comma. */
8236 break;
8237 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008238 }
8239
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8241 && terminated == ','))
8242 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008243 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8244 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008245 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 /*
8247 * if we're in the middle of a paren thing,
8248 * go back to the line that starts it so
8249 * we can get the right prevailing indent
8250 * if ( foo &&
8251 * bar )
8252 */
8253 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008254 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008256 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 */
8258 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008259 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008260 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8261 || (trypos->lnum == tryposBrace->lnum
8262 && trypos->col < tryposBrace->col)))
8263 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264
8265 /*
8266 * If we are looking for ',', we also look for matching
8267 * braces.
8268 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008269 if (trypos == NULL && terminated == ','
8270 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008271 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
8273 if (trypos != NULL)
8274 {
8275 /*
8276 * Check if we are on a case label now. This is
8277 * handled above.
8278 * case xx: if ( asdf &&
8279 * asdf)
8280 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008281 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008283 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
8285 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008286 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 continue;
8288 }
8289 }
8290
8291 /*
8292 * Skip over continuation lines to find the one to get the
8293 * indent from
8294 * char *usethis = "bla\
8295 * bla",
8296 * here;
8297 */
8298 if (terminated == ',')
8299 {
8300 while (curwin->w_cursor.lnum > 1)
8301 {
8302 l = ml_get(curwin->w_cursor.lnum - 1);
8303 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8304 break;
8305 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008306 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 }
8309
8310 /*
8311 * Get indent and pointer to text for current line,
8312 * ignoring any jump label. XXX
8313 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008314 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008315 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008316 else
8317 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 /*
8319 * If this is just above the line we are indenting, and it
8320 * starts with a '{', line it up with this line.
8321 * while (not)
8322 * -> {
8323 * }
8324 */
8325 if (terminated != ',' && lookfor != LOOKFOR_TERM
8326 && theline[0] == '{')
8327 {
8328 amount = cur_amount;
8329 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008330 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 * doesn't start with a '{', which must have a match
8332 * in the same line (scope is the same). Probably:
8333 * { 1, 2 },
8334 * -> { 3, 4 }
8335 */
8336 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008337 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008339 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {
8341 /* have to look back, whether it is a cpp base
8342 * class declaration or initialization */
8343 lookfor = LOOKFOR_CPP_BASECLASS;
8344 continue;
8345 }
8346 break;
8347 }
8348
8349 /*
8350 * Check if we are after an "if", "while", etc.
8351 * Also allow " } else".
8352 */
8353 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8354 {
8355 /*
8356 * Found an unterminated line after an if (), line up
8357 * with the last one.
8358 * if (cond)
8359 * 100 +
8360 * -> here;
8361 */
8362 if (lookfor == LOOKFOR_UNTERM
8363 || lookfor == LOOKFOR_ENUM_OR_INIT)
8364 {
8365 if (cont_amount > 0)
8366 amount = cont_amount;
8367 else
8368 amount += ind_continuation;
8369 break;
8370 }
8371
8372 /*
8373 * If this is just above the line we are indenting, we
8374 * are finished.
8375 * while (not)
8376 * -> here;
8377 * Otherwise this indent can be used when the line
8378 * before this is terminated.
8379 * yyy;
8380 * if (stat)
8381 * while (not)
8382 * xxx;
8383 * -> here;
8384 */
8385 amount = cur_amount;
8386 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008387 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 if (lookfor != LOOKFOR_TERM)
8389 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008390 amount += curbuf->b_ind_level
8391 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 break;
8393 }
8394
8395 /*
8396 * Special trick: when expecting the while () after a
8397 * do, line up with the while()
8398 * do
8399 * x = 1;
8400 * -> here
8401 */
8402 l = skipwhite(ml_get_curline());
8403 if (cin_isdo(l))
8404 {
8405 if (whilelevel == 0)
8406 break;
8407 --whilelevel;
8408 }
8409
8410 /*
8411 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008412 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 * Need to use the scope of this "else". XXX
8414 * If whilelevel != 0 continue looking for a "do {".
8415 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008416 if (cin_iselse(l) && whilelevel == 0)
8417 {
8418 /* If we're looking at "} else", let's make sure we
8419 * find the opening brace of the enclosing scope,
8420 * not the one from "if () {". */
8421 if (*l == '}')
8422 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008423 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008424
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008425 if ((trypos = find_start_brace()) == NULL
8426 || find_match(LOOKFOR_IF, trypos->lnum)
8427 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008428 break;
8429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 }
8431
8432 /*
8433 * If we're below an unterminated line that is not an
8434 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008435 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 * the line before this one.
8437 */
8438 else
8439 {
8440 /*
8441 * Found two unterminated lines on a row, line up with
8442 * the last one.
8443 * c = 99 +
8444 * 100 +
8445 * -> here;
8446 */
8447 if (lookfor == LOOKFOR_UNTERM)
8448 {
8449 /* When line ends in a comma add extra indent */
8450 if (terminated == ',')
8451 amount += ind_continuation;
8452 break;
8453 }
8454
8455 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8456 {
8457 /* Found two lines ending in ',', lineup with the
8458 * lowest one, but check for cpp base class
8459 * declaration/initialization, if it is an
8460 * opening brace or we are looking just for
8461 * enumerations/initializations. */
8462 if (terminated == ',')
8463 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008464 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 break;
8466
8467 lookfor = LOOKFOR_CPP_BASECLASS;
8468 continue;
8469 }
8470
8471 /* Ignore unterminated lines in between, but
8472 * reduce indent. */
8473 if (amount > cur_amount)
8474 amount = cur_amount;
8475 }
8476 else
8477 {
8478 /*
8479 * Found first unterminated line on a row, may
8480 * line up with this line, remember its indent
8481 * 100 +
8482 * -> here;
8483 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008484 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008486
8487 n = (int)STRLEN(l);
8488 if (terminated == ',' && (*skipwhite(l) == ']'
8489 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008490 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
8492 /*
8493 * If previous line ends in ',', check whether we
8494 * are in an initialization or enum
8495 * struct xxx =
8496 * {
8497 * sizeof a,
8498 * 124 };
8499 * or a normal possible continuation line.
8500 * but only, of no other statement has been found
8501 * yet.
8502 */
8503 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8504 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008505 if (curbuf->b_ind_js)
8506 {
8507 /* Search for a line ending in a comma
8508 * and line up with the line below it
8509 * (could be the current line).
8510 * some = [
8511 * 1, <- line up here
8512 * 2,
8513 * some = [
8514 * 3 + <- line up here
8515 * 4 *
8516 * 5,
8517 * 6,
8518 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008519 if (cin_iscomment(skipwhite(l)))
8520 break;
8521 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008522 trypos = find_match_char('[',
8523 curbuf->b_ind_maxparen);
8524 if (trypos != NULL)
8525 {
8526 if (trypos->lnum
8527 == curwin->w_cursor.lnum - 1)
8528 {
8529 /* Current line is first inside
8530 * [], line up with it. */
8531 break;
8532 }
8533 ourscope = trypos->lnum;
8534 }
8535 }
8536 else
8537 {
8538 lookfor = LOOKFOR_ENUM_OR_INIT;
8539 cont_amount = cin_first_id_amount();
8540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 }
8542 else
8543 {
8544 if (lookfor == LOOKFOR_INITIAL
8545 && *l != NUL
8546 && l[STRLEN(l) - 1] == '\\')
8547 /* XXX */
8548 cont_amount = cin_get_equal_amount(
8549 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008550 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008551 && lookfor != LOOKFOR_JS_KEY
8552 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 lookfor = LOOKFOR_UNTERM;
8554 }
8555 }
8556 }
8557 }
8558
8559 /*
8560 * Check if we are after a while (cond);
8561 * If so: Ignore until the matching "do".
8562 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008563 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
8565 /*
8566 * Found an unterminated line after a while ();, line up
8567 * with the last one.
8568 * while (cond);
8569 * 100 + <- line up with this one
8570 * -> here;
8571 */
8572 if (lookfor == LOOKFOR_UNTERM
8573 || lookfor == LOOKFOR_ENUM_OR_INIT)
8574 {
8575 if (cont_amount > 0)
8576 amount = cont_amount;
8577 else
8578 amount += ind_continuation;
8579 break;
8580 }
8581
8582 if (whilelevel == 0)
8583 {
8584 lookfor = LOOKFOR_TERM;
8585 amount = get_indent(); /* XXX */
8586 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008587 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589 ++whilelevel;
8590 }
8591
8592 /*
8593 * We are after a "normal" statement.
8594 * If we had another statement we can stop now and use the
8595 * indent of that other statement.
8596 * Otherwise the indent of the current statement may be used,
8597 * search backwards for the next "normal" statement.
8598 */
8599 else
8600 {
8601 /*
8602 * Skip single break line, if before a switch label. It
8603 * may be lined up with the case label.
8604 */
8605 if (lookfor == LOOKFOR_NOBREAK
8606 && cin_isbreak(skipwhite(ml_get_curline())))
8607 {
8608 lookfor = LOOKFOR_ANY;
8609 continue;
8610 }
8611
8612 /*
8613 * Handle "do {" line.
8614 */
8615 if (whilelevel > 0)
8616 {
8617 l = cin_skipcomment(ml_get_curline());
8618 if (cin_isdo(l))
8619 {
8620 amount = get_indent(); /* XXX */
8621 --whilelevel;
8622 continue;
8623 }
8624 }
8625
8626 /*
8627 * Found a terminated line above an unterminated line. Add
8628 * the amount for a continuation line.
8629 * x = 1;
8630 * y = foo +
8631 * -> here;
8632 * or
8633 * int x = 1;
8634 * int foo,
8635 * -> here;
8636 */
8637 if (lookfor == LOOKFOR_UNTERM
8638 || lookfor == LOOKFOR_ENUM_OR_INIT)
8639 {
8640 if (cont_amount > 0)
8641 amount = cont_amount;
8642 else
8643 amount += ind_continuation;
8644 break;
8645 }
8646
8647 /*
8648 * Found a terminated line above a terminated line or "if"
8649 * etc. line. Use the amount of the line below us.
8650 * x = 1; x = 1;
8651 * if (asdf) y = 2;
8652 * while (asdf) ->here;
8653 * here;
8654 * ->foo;
8655 */
8656 if (lookfor == LOOKFOR_TERM)
8657 {
8658 if (!lookfor_break && whilelevel == 0)
8659 break;
8660 }
8661
8662 /*
8663 * First line above the one we're indenting is terminated.
8664 * To know what needs to be done look further backward for
8665 * a terminated line.
8666 */
8667 else
8668 {
8669 /*
8670 * position the cursor over the rightmost paren, so
8671 * that matching it will take us back to the start of
8672 * the line. Helps for:
8673 * func(asdr,
8674 * asdfasdf);
8675 * here;
8676 */
8677term_again:
8678 l = ml_get_curline();
8679 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008680 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008681 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 {
8683 /*
8684 * Check if we are on a case label now. This is
8685 * handled above.
8686 * case xx: if ( asdf &&
8687 * asdf)
8688 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008689 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008691 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 {
8693 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008694 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 continue;
8696 }
8697 }
8698
8699 /* When aligning with the case statement, don't align
8700 * with a statement after it.
8701 * case 1: { <-- don't use this { position
8702 * stat;
8703 * }
8704 * case 2:
8705 * stat;
8706 * }
8707 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008708 iscase = (curbuf->b_ind_keep_case_label
8709 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710
8711 /*
8712 * Get indent and pointer to text for current line,
8713 * ignoring any jump label.
8714 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008715 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008718 amount += curbuf->b_ind_open_extra;
8719 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008720 l = skipwhite(l);
8721 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008722 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8724
8725 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008726 * When a terminated line starts with "else" skip to
8727 * the matching "if":
8728 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008729 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008730 * Need to use the scope of this "else". XXX
8731 * If whilelevel != 0 continue looking for a "do {".
8732 */
8733 if (lookfor == LOOKFOR_TERM
8734 && *l != '}'
8735 && cin_iselse(l)
8736 && whilelevel == 0)
8737 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008738 if ((trypos = find_start_brace()) == NULL
8739 || find_match(LOOKFOR_IF, trypos->lnum)
8740 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008741 break;
8742 continue;
8743 }
8744
8745 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 * If we're at the end of a block, skip to the start of
8747 * that block.
8748 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008749 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008750 if (find_last_paren(l, '{', '}') /* XXX */
8751 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008753 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 /* if not "else {" check for terminated again */
8755 /* but skip block for "} else {" */
8756 l = cin_skipcomment(ml_get_curline());
8757 if (*l == '}' || !cin_iselse(l))
8758 goto term_again;
8759 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008760 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 }
8762 }
8763 }
8764 }
8765 }
8766 }
8767
8768 /* add extra indent for a comment */
8769 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008770 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008771
8772 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008773 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8774 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008775
8776 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008778
8779 /*
8780 * ok -- we're not inside any sort of structure at all!
8781 *
8782 * This means we're at the top level, and everything should
8783 * basically just match where the previous line is, except
8784 * for the lines immediately following a function declaration,
8785 * which are K&R-style parameters and need to be indented.
8786 *
8787 * if our line starts with an open brace, forget about any
8788 * prevailing indent and make sure it looks like the start
8789 * of a function
8790 */
8791
8792 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008794 amount = curbuf->b_ind_first_open;
8795 goto theend;
8796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008798 /*
8799 * If the NEXT line is a function declaration, the current
8800 * line needs to be indented as a function type spec.
8801 * Don't do this if the current line looks like a comment or if the
8802 * current line is terminated, ie. ends in ';', or if the current line
8803 * contains { or }: "void f() {\n if (1)"
8804 */
8805 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8806 && !cin_nocode(theline)
8807 && vim_strchr(theline, '{') == NULL
8808 && vim_strchr(theline, '}') == NULL
8809 && !cin_ends_in(theline, (char_u *)":", NULL)
8810 && !cin_ends_in(theline, (char_u *)",", NULL)
8811 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8812 cur_curpos.lnum + 1)
8813 && !cin_isterminated(theline, FALSE, TRUE))
8814 {
8815 amount = curbuf->b_ind_func_type;
8816 goto theend;
8817 }
8818
8819 /* search backwards until we find something we recognize */
8820 amount = 0;
8821 curwin->w_cursor = cur_curpos;
8822 while (curwin->w_cursor.lnum > 1)
8823 {
8824 curwin->w_cursor.lnum--;
8825 curwin->w_cursor.col = 0;
8826
8827 l = ml_get_curline();
8828
8829 /*
8830 * If we're in a comment or raw string now, skip to the start
8831 * of it.
8832 */ /* XXX */
8833 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008835 curwin->w_cursor.lnum = trypos->lnum + 1;
8836 curwin->w_cursor.col = 0;
8837 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 }
8839
8840 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008841 * Are we at the start of a cpp base class declaration or
8842 * constructor initialization?
8843 */ /* XXX */
8844 n = FALSE;
8845 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008847 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8848 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008850 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008852 /* XXX */
8853 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8854 break;
8855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008857 /*
8858 * Skip preprocessor directives and blank lines.
8859 */
8860 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8861 continue;
8862
8863 if (cin_nocode(l))
8864 continue;
8865
8866 /*
8867 * If the previous line ends in ',', use one level of
8868 * indentation:
8869 * int foo,
8870 * bar;
8871 * do this before checking for '}' in case of eg.
8872 * enum foobar
8873 * {
8874 * ...
8875 * } foo,
8876 * bar;
8877 */
8878 n = 0;
8879 if (cin_ends_in(l, (char_u *)",", NULL)
8880 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8881 {
8882 /* take us back to opening paren */
8883 if (find_last_paren(l, '(', ')')
8884 && (trypos = find_match_paren(
8885 curbuf->b_ind_maxparen)) != NULL)
8886 curwin->w_cursor = *trypos;
8887
8888 /* For a line ending in ',' that is a continuation line go
8889 * back to the first line with a backslash:
8890 * char *foo = "bla\
8891 * bla",
8892 * here;
8893 */
8894 while (n == 0 && curwin->w_cursor.lnum > 1)
8895 {
8896 l = ml_get(curwin->w_cursor.lnum - 1);
8897 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8898 break;
8899 --curwin->w_cursor.lnum;
8900 curwin->w_cursor.col = 0;
8901 }
8902
8903 amount = get_indent(); /* XXX */
8904
8905 if (amount == 0)
8906 amount = cin_first_id_amount();
8907 if (amount == 0)
8908 amount = ind_continuation;
8909 break;
8910 }
8911
8912 /*
8913 * If the line looks like a function declaration, and we're
8914 * not in a comment, put it the left margin.
8915 */
8916 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
8917 break;
8918 l = ml_get_curline();
8919
8920 /*
8921 * Finding the closing '}' of a previous function. Put
8922 * current line at the left margin. For when 'cino' has "fs".
8923 */
8924 if (*skipwhite(l) == '}')
8925 break;
8926
8927 /* (matching {)
8928 * If the previous line ends on '};' (maybe followed by
8929 * comments) align at column 0. For example:
8930 * char *string_array[] = { "foo",
8931 * / * x * / "b};ar" }; / * foobar * /
8932 */
8933 if (cin_ends_in(l, (char_u *)"};", NULL))
8934 break;
8935
8936 /*
8937 * If the previous line ends on '[' we are probably in an
8938 * array constant:
8939 * something = [
8940 * 234, <- extra indent
8941 */
8942 if (cin_ends_in(l, (char_u *)"[", NULL))
8943 {
8944 amount = get_indent() + ind_continuation;
8945 break;
8946 }
8947
8948 /*
8949 * Find a line only has a semicolon that belongs to a previous
8950 * line ending in '}', e.g. before an #endif. Don't increase
8951 * indent then.
8952 */
8953 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8954 {
8955 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
8957 while (curwin->w_cursor.lnum > 1)
8958 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008959 look = ml_get(--curwin->w_cursor.lnum);
8960 if (!(cin_nocode(look) || cin_ispreproc_cont(
8961 &look, &curwin->w_cursor.lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008963 }
8964 if (curwin->w_cursor.lnum > 0
8965 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008968 curwin->w_cursor = curpos_save;
8969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008971 /*
8972 * If the PREVIOUS line is a function declaration, the current
8973 * line (and the ones that follow) needs to be indented as
8974 * parameters.
8975 */
8976 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
8977 {
8978 amount = curbuf->b_ind_param;
8979 break;
8980 }
8981
8982 /*
8983 * If the previous line ends in ';' and the line before the
8984 * previous line ends in ',' or '\', ident to column zero:
8985 * int foo,
8986 * bar;
8987 * indent_to_0 here;
8988 */
8989 if (cin_ends_in(l, (char_u *)";", NULL))
8990 {
8991 l = ml_get(curwin->w_cursor.lnum - 1);
8992 if (cin_ends_in(l, (char_u *)",", NULL)
8993 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8994 break;
8995 l = ml_get_curline();
8996 }
8997
8998 /*
8999 * Doesn't look like anything interesting -- so just
9000 * use the indent of this line.
9001 *
9002 * Position the cursor over the rightmost paren, so that
9003 * matching it will take us back to the start of the line.
9004 */
9005 find_last_paren(l, '(', ')');
9006
9007 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9008 curwin->w_cursor = *trypos;
9009 amount = get_indent(); /* XXX */
9010 break;
9011 }
9012
9013 /* add extra indent for a comment */
9014 if (cin_iscomment(theline))
9015 amount += curbuf->b_ind_comment;
9016
9017 /* add extra indent if the previous line ended in a backslash:
9018 * "asdfasdf\
9019 * here";
9020 * char *foo = "asdf\
9021 * here";
9022 */
9023 if (cur_curpos.lnum > 1)
9024 {
9025 l = ml_get(cur_curpos.lnum - 1);
9026 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9027 {
9028 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9029 if (cur_amount > 0)
9030 amount = cur_amount;
9031 else if (cur_amount == 0)
9032 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 }
9034 }
9035
9036theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009037 if (amount < 0)
9038 amount = 0;
9039
9040laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 /* put the cursor back where it belongs */
9042 curwin->w_cursor = cur_curpos;
9043
9044 vim_free(linecopy);
9045
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 return amount;
9047}
9048
9049 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009050find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
9052 char_u *look;
9053 pos_T *theirscope;
9054 char_u *mightbeif;
9055 int elselevel;
9056 int whilelevel;
9057
9058 if (lookfor == LOOKFOR_IF)
9059 {
9060 elselevel = 1;
9061 whilelevel = 0;
9062 }
9063 else
9064 {
9065 elselevel = 0;
9066 whilelevel = 1;
9067 }
9068
9069 curwin->w_cursor.col = 0;
9070
9071 while (curwin->w_cursor.lnum > ourscope + 1)
9072 {
9073 curwin->w_cursor.lnum--;
9074 curwin->w_cursor.col = 0;
9075
9076 look = cin_skipcomment(ml_get_curline());
9077 if (cin_iselse(look)
9078 || cin_isif(look)
9079 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009080 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 {
9082 /*
9083 * if we've gone outside the braces entirely,
9084 * we must be out of scope...
9085 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009086 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (theirscope == NULL)
9088 break;
9089
9090 /*
9091 * and if the brace enclosing this is further
9092 * back than the one enclosing the else, we're
9093 * out of luck too.
9094 */
9095 if (theirscope->lnum < ourscope)
9096 break;
9097
9098 /*
9099 * and if they're enclosed in a *deeper* brace,
9100 * then we can ignore it because it's in a
9101 * different scope...
9102 */
9103 if (theirscope->lnum > ourscope)
9104 continue;
9105
9106 /*
9107 * if it was an "else" (that's not an "else if")
9108 * then we need to go back to another if, so
9109 * increment elselevel
9110 */
9111 look = cin_skipcomment(ml_get_curline());
9112 if (cin_iselse(look))
9113 {
9114 mightbeif = cin_skipcomment(look + 4);
9115 if (!cin_isif(mightbeif))
9116 ++elselevel;
9117 continue;
9118 }
9119
9120 /*
9121 * if it was a "while" then we need to go back to
9122 * another "do", so increment whilelevel. XXX
9123 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009124 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 {
9126 ++whilelevel;
9127 continue;
9128 }
9129
9130 /* If it's an "if" decrement elselevel */
9131 look = cin_skipcomment(ml_get_curline());
9132 if (cin_isif(look))
9133 {
9134 elselevel--;
9135 /*
9136 * When looking for an "if" ignore "while"s that
9137 * get in the way.
9138 */
9139 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9140 whilelevel = 0;
9141 }
9142
9143 /* If it's a "do" decrement whilelevel */
9144 if (cin_isdo(look))
9145 whilelevel--;
9146
9147 /*
9148 * if we've used up all the elses, then
9149 * this must be the if that we want!
9150 * match the indent level of that if.
9151 */
9152 if (elselevel <= 0 && whilelevel <= 0)
9153 {
9154 return OK;
9155 }
9156 }
9157 }
9158 return FAIL;
9159}
9160
9161# if defined(FEAT_EVAL) || defined(PROTO)
9162/*
9163 * Get indent level from 'indentexpr'.
9164 */
9165 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009166get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009169 pos_T save_pos;
9170 colnr_T save_curswant;
9171 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009173 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9174 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009176 /* Save and restore cursor position and curswant, in case it was changed
9177 * via :normal commands */
9178 save_pos = curwin->w_cursor;
9179 save_curswant = curwin->w_curswant;
9180 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009182 if (use_sandbox)
9183 ++sandbox;
9184 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009186 if (use_sandbox)
9187 --sandbox;
9188 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189
9190 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9191 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9192 * command. */
9193 save_State = State;
9194 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009195 curwin->w_cursor = save_pos;
9196 curwin->w_curswant = save_curswant;
9197 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 check_cursor();
9199 State = save_State;
9200
9201 /* If there is an error, just keep the current indent. */
9202 if (indent < 0)
9203 indent = get_indent();
9204
9205 return indent;
9206}
9207# endif
9208
9209#endif /* FEAT_CINDENT */
9210
9211#if defined(FEAT_LISP) || defined(PROTO)
9212
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009213static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214
9215 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009216lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217{
9218 char_u buf[LSIZE];
9219 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009220 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221
9222 while (*word != NUL)
9223 {
9224 (void)copy_option_part(&word, buf, LSIZE, ",");
9225 len = (int)STRLEN(buf);
9226 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9227 return TRUE;
9228 }
9229 return FALSE;
9230}
9231
9232/*
9233 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9234 * The incompatible newer method is quite a bit better at indenting
9235 * code in lisp-like languages than the traditional one; it's still
9236 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9237 *
9238 * TODO:
9239 * Findmatch() should be adapted for lisp, also to make showmatch
9240 * work correctly: now (v5.3) it seems all C/C++ oriented:
9241 * - it does not recognize the #\( and #\) notations as character literals
9242 * - it doesn't know about comments starting with a semicolon
9243 * - it incorrectly interprets '(' as a character literal
9244 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009245 * Update from Sergey Khorev:
9246 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 */
9248 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009249get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009251 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 int amount;
9253 char_u *that;
9254 colnr_T col;
9255 colnr_T firsttry;
9256 int parencount, quotecount;
9257 int vi_lisp;
9258
9259 /* Set vi_lisp to use the vi-compatible method */
9260 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9261
9262 realpos = curwin->w_cursor;
9263 curwin->w_cursor.col = 0;
9264
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009265 if ((pos = findmatch(NULL, '(')) == NULL)
9266 pos = findmatch(NULL, '[');
9267 else
9268 {
9269 paren = *pos;
9270 pos = findmatch(NULL, '[');
9271 if (pos == NULL || ltp(pos, &paren))
9272 pos = &paren;
9273 }
9274 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 {
9276 /* Extra trick: Take the indent of the first previous non-white
9277 * line that is at the same () level. */
9278 amount = -1;
9279 parencount = 0;
9280
9281 while (--curwin->w_cursor.lnum >= pos->lnum)
9282 {
9283 if (linewhite(curwin->w_cursor.lnum))
9284 continue;
9285 for (that = ml_get_curline(); *that != NUL; ++that)
9286 {
9287 if (*that == ';')
9288 {
9289 while (*(that + 1) != NUL)
9290 ++that;
9291 continue;
9292 }
9293 if (*that == '\\')
9294 {
9295 if (*(that + 1) != NUL)
9296 ++that;
9297 continue;
9298 }
9299 if (*that == '"' && *(that + 1) != NUL)
9300 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009301 while (*++that && *that != '"')
9302 {
9303 /* skipping escaped characters in the string */
9304 if (*that == '\\')
9305 {
9306 if (*++that == NUL)
9307 break;
9308 if (that[1] == NUL)
9309 {
9310 ++that;
9311 break;
9312 }
9313 }
9314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009316 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009318 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 --parencount;
9320 }
9321 if (parencount == 0)
9322 {
9323 amount = get_indent();
9324 break;
9325 }
9326 }
9327
9328 if (amount == -1)
9329 {
9330 curwin->w_cursor.lnum = pos->lnum;
9331 curwin->w_cursor.col = pos->col;
9332 col = pos->col;
9333
9334 that = ml_get_curline();
9335
9336 if (vi_lisp && get_indent() == 0)
9337 amount = 2;
9338 else
9339 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009340 char_u *line = that;
9341
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 amount = 0;
9343 while (*that && col)
9344 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009345 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 col--;
9347 }
9348
9349 /*
9350 * Some keywords require "body" indenting rules (the
9351 * non-standard-lisp ones are Scheme special forms):
9352 *
9353 * (let ((a 1)) instead (let ((a 1))
9354 * (...)) of (...))
9355 */
9356
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009357 if (!vi_lisp && (*that == '(' || *that == '[')
9358 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 amount += 2;
9360 else
9361 {
9362 that++;
9363 amount++;
9364 firsttry = amount;
9365
9366 while (vim_iswhite(*that))
9367 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009368 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 ++that;
9370 }
9371
9372 if (*that && *that != ';') /* not a comment line */
9373 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009374 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009376 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 firsttry++;
9378
9379 parencount = 0;
9380 quotecount = 0;
9381
9382 if (vi_lisp
9383 || (*that != '"'
9384 && *that != '\''
9385 && *that != '#'
9386 && (*that < '0' || *that > '9')))
9387 {
9388 while (*that
9389 && (!vim_iswhite(*that)
9390 || quotecount
9391 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009392 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 && !quotecount
9394 && !parencount
9395 && vi_lisp)))
9396 {
9397 if (*that == '"')
9398 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009399 if ((*that == '(' || *that == '[')
9400 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009402 if ((*that == ')' || *that == ']')
9403 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 --parencount;
9405 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009406 amount += lbr_chartabsize_adv(
9407 line, &that, (colnr_T)amount);
9408 amount += lbr_chartabsize_adv(
9409 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
9411 }
9412 while (vim_iswhite(*that))
9413 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009414 amount += lbr_chartabsize(
9415 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 that++;
9417 }
9418 if (!*that || *that == ';')
9419 amount = firsttry;
9420 }
9421 }
9422 }
9423 }
9424 }
9425 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009426 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427
9428 curwin->w_cursor = realpos;
9429
9430 return amount;
9431}
9432#endif /* FEAT_LISP */
9433
9434 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009435prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009437#if defined(SIGHUP) && defined(SIG_IGN)
9438 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9439 * makes Vim exit and then handling SIGHUP causes various reentrance
9440 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009441 signal(SIGHUP, SIG_IGN);
9442#endif
9443
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444#ifdef FEAT_GUI
9445 if (gui.in_use)
9446 {
9447 gui.dying = TRUE;
9448 out_trash(); /* trash any pending output */
9449 }
9450 else
9451#endif
9452 {
9453 windgoto((int)Rows - 1, 0);
9454
9455 /*
9456 * Switch terminal mode back now, so messages end up on the "normal"
9457 * screen (if there are two screens).
9458 */
9459 settmode(TMODE_COOK);
9460#ifdef WIN3264
9461 if (can_end_termcap_mode(FALSE) == TRUE)
9462#endif
9463 stoptermcap();
9464 out_flush();
9465 }
9466}
9467
9468/*
9469 * Preserve files and exit.
9470 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009471 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9472 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 */
9474 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009475preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 buf_T *buf;
9478
9479 prepare_to_exit();
9480
Bram Moolenaar4770d092006-01-12 23:22:24 +00009481 /* Setting this will prevent free() calls. That avoids calling free()
9482 * recursively when free() was invoked with a bad pointer. */
9483 really_exiting = TRUE;
9484
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 out_str(IObuff);
9486 screen_start(); /* don't know where cursor is now */
9487 out_flush();
9488
9489 ml_close_notmod(); /* close all not-modified buffers */
9490
9491 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9492 {
9493 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9494 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009495 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 screen_start(); /* don't know where cursor is now */
9497 out_flush();
9498 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9499 break;
9500 }
9501 }
9502
9503 ml_close_all(FALSE); /* close all memfiles, without deleting */
9504
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009505 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506
9507 getout(1);
9508}
9509
9510/*
9511 * return TRUE if "fname" exists.
9512 */
9513 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009514vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 struct stat st;
9517
9518 if (mch_stat((char *)fname, &st))
9519 return FALSE;
9520 return TRUE;
9521}
9522
9523/*
9524 * Check for CTRL-C pressed, but only once in a while.
9525 * Should be used instead of ui_breakcheck() for functions that check for
9526 * each line in the file. Calling ui_breakcheck() each time takes too much
9527 * time, because it can be a system call.
9528 */
9529
9530#ifndef BREAKCHECK_SKIP
9531# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9532# define BREAKCHECK_SKIP 200
9533# else
9534# define BREAKCHECK_SKIP 32
9535# endif
9536#endif
9537
9538static int breakcheck_count = 0;
9539
9540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009541line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542{
9543 if (++breakcheck_count >= BREAKCHECK_SKIP)
9544 {
9545 breakcheck_count = 0;
9546 ui_breakcheck();
9547 }
9548}
9549
9550/*
9551 * Like line_breakcheck() but check 10 times less often.
9552 */
9553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009554fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9557 {
9558 breakcheck_count = 0;
9559 ui_breakcheck();
9560 }
9561}
9562
9563/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009564 * Invoke expand_wildcards() for one pattern.
9565 * Expand items like "%:h" before the expansion.
9566 * Returns OK or FAIL.
9567 */
9568 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009569expand_wildcards_eval(
9570 char_u **pat, /* pointer to input pattern */
9571 int *num_file, /* resulting number of files */
9572 char_u ***file, /* array of resulting files */
9573 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009574{
9575 int ret = FAIL;
9576 char_u *eval_pat = NULL;
9577 char_u *exp_pat = *pat;
9578 char_u *ignored_msg;
9579 int usedlen;
9580
9581 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9582 {
9583 ++emsg_off;
9584 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9585 NULL, &ignored_msg, NULL);
9586 --emsg_off;
9587 if (eval_pat != NULL)
9588 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9589 }
9590
9591 if (exp_pat != NULL)
9592 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9593
9594 if (eval_pat != NULL)
9595 {
9596 vim_free(exp_pat);
9597 vim_free(eval_pat);
9598 }
9599
9600 return ret;
9601}
9602
9603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9605 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009606 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 */
9608 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009609expand_wildcards(
9610 int num_pat, /* number of input patterns */
9611 char_u **pat, /* array of input patterns */
9612 int *num_files, /* resulting number of files */
9613 char_u ***files, /* array of resulting files */
9614 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 int retval;
9617 int i, j;
9618 char_u *p;
9619 int non_suf_match; /* number without matching suffix */
9620
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009621 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622
9623 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009624 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 return retval;
9626
9627#ifdef FEAT_WILDIGN
9628 /*
9629 * Remove names that match 'wildignore'.
9630 */
9631 if (*p_wig)
9632 {
9633 char_u *ffname;
9634
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009635 /* check all files in (*files)[] */
9636 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009638 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 if (ffname == NULL) /* out of memory */
9640 break;
9641# ifdef VMS
9642 vms_remove_version(ffname);
9643# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009644 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009646 /* remove this matching files from the list */
9647 vim_free((*files)[i]);
9648 for (j = i; j + 1 < *num_files; ++j)
9649 (*files)[j] = (*files)[j + 1];
9650 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 --i;
9652 }
9653 vim_free(ffname);
9654 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009655
9656 /* If the number of matches is now zero, we fail. */
9657 if (*num_files == 0)
9658 {
9659 vim_free(*files);
9660 *files = NULL;
9661 return FAIL;
9662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 }
9664#endif
9665
9666 /*
9667 * Move the names where 'suffixes' match to the end.
9668 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009669 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 {
9671 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009672 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009674 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 {
9676 /*
9677 * Move the name without matching suffix to the front
9678 * of the list.
9679 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009680 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009682 (*files)[j] = (*files)[j - 1];
9683 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 }
9685 }
9686 }
9687
9688 return retval;
9689}
9690
9691/*
9692 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9693 */
9694 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009695match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696{
9697 int fnamelen, setsuflen;
9698 char_u *setsuf;
9699#define MAXSUFLEN 30 /* maximum length of a file suffix */
9700 char_u suf_buf[MAXSUFLEN];
9701
9702 fnamelen = (int)STRLEN(fname);
9703 setsuflen = 0;
9704 for (setsuf = p_su; *setsuf; )
9705 {
9706 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009707 if (setsuflen == 0)
9708 {
9709 char_u *tail = gettail(fname);
9710
9711 /* empty entry: match name without a '.' */
9712 if (vim_strchr(tail, '.') == NULL)
9713 {
9714 setsuflen = 1;
9715 break;
9716 }
9717 }
9718 else
9719 {
9720 if (fnamelen >= setsuflen
9721 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9722 (size_t)setsuflen) == 0)
9723 break;
9724 setsuflen = 0;
9725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 }
9727 return (setsuflen != 0);
9728}
9729
9730#if !defined(NO_EXPANDPATH) || defined(PROTO)
9731
9732# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009733static int vim_backtick(char_u *p);
9734static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735# endif
9736
9737# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9738/*
9739 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9740 * it's shared between these systems.
9741 */
9742# if defined(DJGPP) || defined(PROTO)
9743# define _cdecl /* DJGPP doesn't have this */
9744# else
9745# ifdef __BORLANDC__
9746# define _cdecl _RTLENTRYF
9747# endif
9748# endif
9749
9750/*
9751 * comparison function for qsort in dos_expandpath()
9752 */
9753 static int _cdecl
9754pstrcmp(const void *a, const void *b)
9755{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009756 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
9759# ifndef WIN3264
9760 static void
9761namelowcpy(
9762 char_u *d,
9763 char_u *s)
9764{
9765# ifdef DJGPP
9766 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9767 while (*s)
9768 *d++ = *s++;
9769 else
9770# endif
9771 while (*s)
9772 *d++ = TOLOWER_LOC(*s++);
9773 *d = NUL;
9774}
9775# endif
9776
9777/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009778 * Recursively expand one path component into all matching files and/or
9779 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 * Return the number of matches found.
9781 * "path" has backslashes before chars that are not to be expanded, starting
9782 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009783 * Return the number of matches found.
9784 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 */
9786 static int
9787dos_expandpath(
9788 garray_T *gap,
9789 char_u *path,
9790 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009791 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009792 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009794 char_u *buf;
9795 char_u *path_end;
9796 char_u *p, *s, *e;
9797 int start_len = gap->ga_len;
9798 char_u *pat;
9799 regmatch_T regmatch;
9800 int starts_with_dot;
9801 int matches;
9802 int len;
9803 int starstar = FALSE;
9804 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805#ifdef WIN3264
9806 WIN32_FIND_DATA fb;
9807 HANDLE hFind = (HANDLE)0;
9808# ifdef FEAT_MBYTE
9809 WIN32_FIND_DATAW wfb;
9810 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9811# endif
9812#else
9813 struct ffblk fb;
9814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009816 int ok;
9817
9818 /* Expanding "**" may take a long time, check for CTRL-C. */
9819 if (stardepth > 0)
9820 {
9821 ui_breakcheck();
9822 if (got_int)
9823 return 0;
9824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009826 /* Make room for file name. When doing encoding conversion the actual
9827 * length may be quite a bit longer, thus use the maximum possible length. */
9828 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 if (buf == NULL)
9830 return 0;
9831
9832 /*
9833 * Find the first part in the path name that contains a wildcard or a ~1.
9834 * Copy it into buf, including the preceding characters.
9835 */
9836 p = buf;
9837 s = buf;
9838 e = NULL;
9839 path_end = path;
9840 while (*path_end != NUL)
9841 {
9842 /* May ignore a wildcard that has a backslash before it; it will
9843 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9844 if (path_end >= path + wildoff && rem_backslash(path_end))
9845 *p++ = *path_end++;
9846 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9847 {
9848 if (e != NULL)
9849 break;
9850 s = p + 1;
9851 }
9852 else if (path_end >= path + wildoff
9853 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9854 e = p;
9855#ifdef FEAT_MBYTE
9856 if (has_mbyte)
9857 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009858 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 STRNCPY(p, path_end, len);
9860 p += len;
9861 path_end += len;
9862 }
9863 else
9864#endif
9865 *p++ = *path_end++;
9866 }
9867 e = p;
9868 *e = NUL;
9869
9870 /* now we have one wildcard component between s and e */
9871 /* Remove backslashes between "wildoff" and the start of the wildcard
9872 * component. */
9873 for (p = buf + wildoff; p < s; ++p)
9874 if (rem_backslash(p))
9875 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009876 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 --e;
9878 --s;
9879 }
9880
Bram Moolenaar231334e2005-07-25 20:46:57 +00009881 /* Check for "**" between "s" and "e". */
9882 for (p = s; p < e; ++p)
9883 if (p[0] == '*' && p[1] == '*')
9884 starstar = TRUE;
9885
Bram Moolenaard82103e2016-01-17 17:04:05 +01009886 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9888 if (pat == NULL)
9889 {
9890 vim_free(buf);
9891 return 0;
9892 }
9893
9894 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009895 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009896 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 regmatch.rm_ic = TRUE; /* Always ignore case */
9898 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009899 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009900 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 vim_free(pat);
9902
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009903 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 {
9905 vim_free(buf);
9906 return 0;
9907 }
9908
9909 /* remember the pattern or file name being looked for */
9910 matchname = vim_strsave(s);
9911
Bram Moolenaar231334e2005-07-25 20:46:57 +00009912 /* If "**" is by itself, this is the first time we encounter it and more
9913 * is following then find matches without any directory. */
9914 if (!didstar && stardepth < 100 && starstar && e - s == 2
9915 && *path_end == '/')
9916 {
9917 STRCPY(s, path_end + 1);
9918 ++stardepth;
9919 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9920 --stardepth;
9921 }
9922
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 /* Scan all files in the directory with "dir/ *.*" */
9924 STRCPY(s, "*.*");
9925#ifdef WIN3264
9926# ifdef FEAT_MBYTE
9927 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9928 {
9929 /* The active codepage differs from 'encoding'. Attempt using the
9930 * wide function. If it fails because it is not implemented fall back
9931 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009932 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 if (wn != NULL)
9934 {
9935 hFind = FindFirstFileW(wn, &wfb);
9936 if (hFind == INVALID_HANDLE_VALUE
9937 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9938 {
9939 vim_free(wn);
9940 wn = NULL;
9941 }
9942 }
9943 }
9944
9945 if (wn == NULL)
9946# endif
9947 hFind = FindFirstFile(buf, &fb);
9948 ok = (hFind != INVALID_HANDLE_VALUE);
9949#else
9950 /* If we are expanding wildcards we try both files and directories */
9951 ok = (findfirst((char *)buf, &fb,
9952 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9953#endif
9954
9955 while (ok)
9956 {
9957#ifdef WIN3264
9958# ifdef FEAT_MBYTE
9959 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009960 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 else
9962# endif
9963 p = (char_u *)fb.cFileName;
9964#else
9965 p = (char_u *)fb.ff_name;
9966#endif
9967 /* Ignore entries starting with a dot, unless when asked for. Accept
9968 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +01009969 if ((p[0] != '.' || starts_with_dot
9970 || ((flags & EW_DODOT)
9971 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009973 || (regmatch.regprog != NULL
9974 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009975 || ((flags & EW_NOTWILD)
9976 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 {
9978#ifdef WIN3264
9979 STRCPY(s, p);
9980#else
9981 namelowcpy(s, p);
9982#endif
9983 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009984
9985 if (starstar && stardepth < 100)
9986 {
9987 /* For "**" in the pattern first go deeper in the tree to
9988 * find matches. */
9989 STRCPY(buf + len, "/**");
9990 STRCPY(buf + len + 3, path_end);
9991 ++stardepth;
9992 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9993 --stardepth;
9994 }
9995
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 STRCPY(buf + len, path_end);
9997 if (mch_has_exp_wildcard(path_end))
9998 {
9999 /* need to expand another component of the path */
10000 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010001 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 }
10003 else
10004 {
10005 /* no more wildcards, check if there is a match */
10006 /* remove backslashes for the remaining components only */
10007 if (*path_end != 0)
10008 backslash_halve(buf + len + 1);
10009 if (mch_getperm(buf) >= 0) /* add existing file */
10010 addfile(gap, buf, flags);
10011 }
10012 }
10013
10014#ifdef WIN3264
10015# ifdef FEAT_MBYTE
10016 if (wn != NULL)
10017 {
10018 vim_free(p);
10019 ok = FindNextFileW(hFind, &wfb);
10020 }
10021 else
10022# endif
10023 ok = FindNextFile(hFind, &fb);
10024#else
10025 ok = (findnext(&fb) == 0);
10026#endif
10027
10028 /* If no more matches and no match was used, try expanding the name
10029 * itself. Finds the long name of a short filename. */
10030 if (!ok && matchname != NULL && gap->ga_len == start_len)
10031 {
10032 STRCPY(s, matchname);
10033#ifdef WIN3264
10034 FindClose(hFind);
10035# ifdef FEAT_MBYTE
10036 if (wn != NULL)
10037 {
10038 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010039 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 if (wn != NULL)
10041 hFind = FindFirstFileW(wn, &wfb);
10042 }
10043 if (wn == NULL)
10044# endif
10045 hFind = FindFirstFile(buf, &fb);
10046 ok = (hFind != INVALID_HANDLE_VALUE);
10047#else
10048 ok = (findfirst((char *)buf, &fb,
10049 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
10050#endif
10051 vim_free(matchname);
10052 matchname = NULL;
10053 }
10054 }
10055
10056#ifdef WIN3264
10057 FindClose(hFind);
10058# ifdef FEAT_MBYTE
10059 vim_free(wn);
10060# endif
10061#endif
10062 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010063 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 vim_free(matchname);
10065
10066 matches = gap->ga_len - start_len;
10067 if (matches > 0)
10068 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10069 sizeof(char_u *), pstrcmp);
10070 return matches;
10071}
10072
10073 int
10074mch_expandpath(
10075 garray_T *gap,
10076 char_u *path,
10077 int flags) /* EW_* flags */
10078{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010079 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080}
10081# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
10082
Bram Moolenaar231334e2005-07-25 20:46:57 +000010083#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10084 || defined(PROTO)
10085/*
10086 * Unix style wildcard expansion code.
10087 * It's here because it's used both for Unix and Mac.
10088 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010089static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010090
10091 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010092pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010093{
10094 return (pathcmp(*(char **)a, *(char **)b, -1));
10095}
10096
10097/*
10098 * Recursively expand one path component into all matching files and/or
10099 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10100 * "path" has backslashes before chars that are not to be expanded, starting
10101 * at "path + wildoff".
10102 * Return the number of matches found.
10103 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10104 */
10105 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010106unix_expandpath(
10107 garray_T *gap,
10108 char_u *path,
10109 int wildoff,
10110 int flags, /* EW_* flags */
10111 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010112{
10113 char_u *buf;
10114 char_u *path_end;
10115 char_u *p, *s, *e;
10116 int start_len = gap->ga_len;
10117 char_u *pat;
10118 regmatch_T regmatch;
10119 int starts_with_dot;
10120 int matches;
10121 int len;
10122 int starstar = FALSE;
10123 static int stardepth = 0; /* depth for "**" expansion */
10124
10125 DIR *dirp;
10126 struct dirent *dp;
10127
10128 /* Expanding "**" may take a long time, check for CTRL-C. */
10129 if (stardepth > 0)
10130 {
10131 ui_breakcheck();
10132 if (got_int)
10133 return 0;
10134 }
10135
10136 /* make room for file name */
10137 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10138 if (buf == NULL)
10139 return 0;
10140
10141 /*
10142 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010143 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010144 * Copy it into "buf", including the preceding characters.
10145 */
10146 p = buf;
10147 s = buf;
10148 e = NULL;
10149 path_end = path;
10150 while (*path_end != NUL)
10151 {
10152 /* May ignore a wildcard that has a backslash before it; it will
10153 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10154 if (path_end >= path + wildoff && rem_backslash(path_end))
10155 *p++ = *path_end++;
10156 else if (*path_end == '/')
10157 {
10158 if (e != NULL)
10159 break;
10160 s = p + 1;
10161 }
10162 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010163 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010164 || (!p_fic && (flags & EW_ICASE)
10165 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010166 e = p;
10167#ifdef FEAT_MBYTE
10168 if (has_mbyte)
10169 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010170 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010171 STRNCPY(p, path_end, len);
10172 p += len;
10173 path_end += len;
10174 }
10175 else
10176#endif
10177 *p++ = *path_end++;
10178 }
10179 e = p;
10180 *e = NUL;
10181
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010182 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010183 /* Remove backslashes between "wildoff" and the start of the wildcard
10184 * component. */
10185 for (p = buf + wildoff; p < s; ++p)
10186 if (rem_backslash(p))
10187 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010188 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010189 --e;
10190 --s;
10191 }
10192
10193 /* Check for "**" between "s" and "e". */
10194 for (p = s; p < e; ++p)
10195 if (p[0] == '*' && p[1] == '*')
10196 starstar = TRUE;
10197
10198 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010199 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010200 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10201 if (pat == NULL)
10202 {
10203 vim_free(buf);
10204 return 0;
10205 }
10206
10207 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010208 if (flags & EW_ICASE)
10209 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10210 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010211 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010212 if (flags & (EW_NOERROR | EW_NOTWILD))
10213 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010214 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010215 if (flags & (EW_NOERROR | EW_NOTWILD))
10216 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010217 vim_free(pat);
10218
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010219 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010220 {
10221 vim_free(buf);
10222 return 0;
10223 }
10224
10225 /* If "**" is by itself, this is the first time we encounter it and more
10226 * is following then find matches without any directory. */
10227 if (!didstar && stardepth < 100 && starstar && e - s == 2
10228 && *path_end == '/')
10229 {
10230 STRCPY(s, path_end + 1);
10231 ++stardepth;
10232 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10233 --stardepth;
10234 }
10235
10236 /* open the directory for scanning */
10237 *s = NUL;
10238 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10239
10240 /* Find all matching entries */
10241 if (dirp != NULL)
10242 {
10243 for (;;)
10244 {
10245 dp = readdir(dirp);
10246 if (dp == NULL)
10247 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010248 if ((dp->d_name[0] != '.' || starts_with_dot
10249 || ((flags & EW_DODOT)
10250 && dp->d_name[1] != NUL
10251 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010252 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10253 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010254 || ((flags & EW_NOTWILD)
10255 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010256 {
10257 STRCPY(s, dp->d_name);
10258 len = STRLEN(buf);
10259
10260 if (starstar && stardepth < 100)
10261 {
10262 /* For "**" in the pattern first go deeper in the tree to
10263 * find matches. */
10264 STRCPY(buf + len, "/**");
10265 STRCPY(buf + len + 3, path_end);
10266 ++stardepth;
10267 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10268 --stardepth;
10269 }
10270
10271 STRCPY(buf + len, path_end);
10272 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10273 {
10274 /* need to expand another component of the path */
10275 /* remove backslashes for the remaining components only */
10276 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10277 }
10278 else
10279 {
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010280 struct stat sb;
10281
Bram Moolenaar231334e2005-07-25 20:46:57 +000010282 /* no more wildcards, check if there is a match */
10283 /* remove backslashes for the remaining components only */
10284 if (*path_end != NUL)
10285 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010286 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010287 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010288 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010289 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010290#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010291 size_t precomp_len = STRLEN(buf)+1;
10292 char_u *precomp_buf =
10293 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010294
Bram Moolenaar231334e2005-07-25 20:46:57 +000010295 if (precomp_buf)
10296 {
10297 mch_memmove(buf, precomp_buf, precomp_len);
10298 vim_free(precomp_buf);
10299 }
10300#endif
10301 addfile(gap, buf, flags);
10302 }
10303 }
10304 }
10305 }
10306
10307 closedir(dirp);
10308 }
10309
10310 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010311 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010312
10313 matches = gap->ga_len - start_len;
10314 if (matches > 0)
10315 qsort(((char_u **)gap->ga_data) + start_len, matches,
10316 sizeof(char_u *), pstrcmp);
10317 return matches;
10318}
10319#endif
10320
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010321#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010322static int find_previous_pathsep(char_u *path, char_u **psep);
10323static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10324static void expand_path_option(char_u *curdir, garray_T *gap);
10325static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10326static void uniquefy_paths(garray_T *gap, char_u *pattern);
10327static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010328
10329/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010330 * Moves "*psep" back to the previous path separator in "path".
10331 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010332 */
10333 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010334find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010335{
10336 /* skip the current separator */
10337 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010338 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010339
10340 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010341 while (*psep > path)
10342 {
10343 if (vim_ispathsep(**psep))
10344 return OK;
10345 mb_ptr_back(path, *psep);
10346 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010347
10348 return FAIL;
10349}
10350
10351/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010352 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10353 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010354 */
10355 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010356is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010357{
10358 int j;
10359 int candidate_len;
10360 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010361 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010362 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010363
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010364 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010365 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010366 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010367 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010368
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010369 candidate_len = (int)STRLEN(maybe_unique);
10370 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010371 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010372 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010373
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010374 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010375 if (fnamecmp(maybe_unique, rival) == 0
10376 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010377 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010378 }
10379
Bram Moolenaar162bd912010-07-28 22:29:10 +020010380 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010381}
10382
10383/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010384 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010385 * paths are expanded to their equivalent fullpath. This includes the "."
10386 * (relative to current buffer directory) and empty path (relative to current
10387 * directory) notations.
10388 *
10389 * TODO: handle upward search (;) and path limiter (**N) notations by
10390 * expanding each into their equivalent path(s).
10391 */
10392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010393expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010394{
10395 char_u *path_option = *curbuf->b_p_path == NUL
10396 ? p_path : curbuf->b_p_path;
10397 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010398 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010399 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010400
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010401 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010402 return;
10403
10404 while (*path_option != NUL)
10405 {
10406 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10407
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010408 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010409 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010410 /* Relative to current buffer:
10411 * "/path/file" + "." -> "/path/"
10412 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010413 if (curbuf->b_ffname == NULL)
10414 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010415 p = gettail(curbuf->b_ffname);
10416 len = (int)(p - curbuf->b_ffname);
10417 if (len + (int)STRLEN(buf) >= MAXPATHL)
10418 continue;
10419 if (buf[1] == NUL)
10420 buf[len] = NUL;
10421 else
10422 STRMOVE(buf + len, buf + 2);
10423 mch_memmove(buf, curbuf->b_ffname, len);
10424 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010425 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010426 else if (buf[0] == NUL)
10427 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010428 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010429 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010430 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010431 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010432 else if (!mch_isFullName(buf))
10433 {
10434 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010435 len = (int)STRLEN(curdir);
10436 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010437 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010438 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010439 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010440 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010441 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010442 }
10443
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010444 if (ga_grow(gap, 1) == FAIL)
10445 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010446
10447# if defined(MSWIN) || defined(MSDOS)
10448 /* Avoid the path ending in a backslash, it fails when a comma is
10449 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010450 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010451 if (buf[len - 1] == '\\')
10452 buf[len - 1] = '/';
10453# endif
10454
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010455 p = vim_strsave(buf);
10456 if (p == NULL)
10457 break;
10458 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010459 }
10460
10461 vim_free(buf);
10462}
10463
10464/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010465 * Returns a pointer to the file or directory name in "fname" that matches the
10466 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010467 *
10468 * path: /foo/bar/baz
10469 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010470 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010471 */
10472 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010473get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010474{
10475 int i;
10476 int maxlen = 0;
10477 char_u **path_part = (char_u **)gap->ga_data;
10478 char_u *cutoff = NULL;
10479
10480 for (i = 0; i < gap->ga_len; i++)
10481 {
10482 int j = 0;
10483
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010484 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010485# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010486 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10487#endif
10488 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010489 j++;
10490 if (j > maxlen)
10491 {
10492 maxlen = j;
10493 cutoff = &fname[j];
10494 }
10495 }
10496
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010497 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010498 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010499 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010500 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010501
10502 return cutoff;
10503}
10504
10505/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010506 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10507 * that they are unique with respect to each other while conserving the part
10508 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010509 */
10510 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010511uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010512{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010513 int i;
10514 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010515 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010516 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010517 char_u *pat;
10518 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010519 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010520 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010521 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010522 char_u **in_curdir = NULL;
10523 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010524
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010525 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010526 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010527
10528 /*
10529 * We need to prepend a '*' at the beginning of file_pattern so that the
10530 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010531 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010532 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010533 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010534 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010535 if (file_pattern == NULL)
10536 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010537 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010538 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010539 STRCAT(file_pattern, pattern);
10540 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10541 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010542 if (pat == NULL)
10543 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010544
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010545 regmatch.rm_ic = TRUE; /* always ignore case */
10546 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10547 vim_free(pat);
10548 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010549 return;
10550
Bram Moolenaar162bd912010-07-28 22:29:10 +020010551 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010552 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010553 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010554 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010555
10556 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010557 if (in_curdir == NULL)
10558 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010559
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010560 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010561 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010562 char_u *path = fnames[i];
10563 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010564 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010565 char_u *pathsep_p;
10566 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010567
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010568 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010569 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010570 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010571 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010572 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010573
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010574 /* Shorten the filename while maintaining its uniqueness */
10575 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010576
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010577 /* we start at the end of the path */
10578 pathsep_p = path + len - 1;
10579
10580 while (find_previous_pathsep(path, &pathsep_p))
10581 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10582 && is_unique(pathsep_p + 1, gap, i)
10583 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10584 {
10585 sort_again = TRUE;
10586 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10587 break;
10588 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010589
10590 if (mch_isFullName(path))
10591 {
10592 /*
10593 * Last resort: shorten relative to curdir if possible.
10594 * 'possible' means:
10595 * 1. It is under the current directory.
10596 * 2. The result is actually shorter than the original.
10597 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010598 * Before curdir After
10599 * /foo/bar/file.txt /foo/bar ./file.txt
10600 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10601 * /file.txt / /file.txt
10602 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010603 */
10604 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010605 if (short_name != NULL && short_name > path + 1
10606#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010607 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010608 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010609 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010610 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010611 && !vim_ispathsep(*short_name)
10612#endif
10613 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010614 {
10615 STRCPY(path, ".");
10616 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010617 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010618 }
10619 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010620 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010621 }
10622
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010623 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010624 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010625 {
10626 char_u *rel_path;
10627 char_u *path = in_curdir[i];
10628
10629 if (path == NULL)
10630 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010631
10632 /* If the {filename} is not unique, change it to ./{filename}.
10633 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010634 short_name = shorten_fname(path, curdir);
10635 if (short_name == NULL)
10636 short_name = path;
10637 if (is_unique(short_name, gap, i))
10638 {
10639 STRCPY(fnames[i], short_name);
10640 continue;
10641 }
10642
10643 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10644 if (rel_path == NULL)
10645 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010646 STRCPY(rel_path, ".");
10647 add_pathsep(rel_path);
10648 STRCAT(rel_path, short_name);
10649
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010650 vim_free(fnames[i]);
10651 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010652 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010653 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010654 }
10655
Bram Moolenaar162bd912010-07-28 22:29:10 +020010656theend:
10657 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010658 if (in_curdir != NULL)
10659 {
10660 for (i = 0; i < gap->ga_len; i++)
10661 vim_free(in_curdir[i]);
10662 vim_free(in_curdir);
10663 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010664 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010665 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010666
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010667 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010668 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010669}
10670
10671/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010672 * Calls globpath() with 'path' values for the given pattern and stores the
10673 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010674 * Returns the total number of matches.
10675 */
10676 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010677expand_in_path(
10678 garray_T *gap,
10679 char_u *pattern,
10680 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010681{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010682 char_u *curdir;
10683 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010684 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010685
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010686 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010687 return 0;
10688 mch_dirname(curdir, MAXPATHL);
10689
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010690 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010691 expand_path_option(curdir, &path_ga);
10692 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010693 if (path_ga.ga_len == 0)
10694 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010695
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010696 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010697 ga_clear_strings(&path_ga);
10698 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010699 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010700
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010701 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010702 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010703
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010704 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010705}
10706#endif
10707
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010708#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10709/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010710 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10711 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010712 */
10713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010714remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010715{
10716 int i;
10717 int j;
10718 char_u **fnames = (char_u **)gap->ga_data;
10719
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010720 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010721 for (i = gap->ga_len - 1; i > 0; --i)
10722 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10723 {
10724 vim_free(fnames[i]);
10725 for (j = i + 1; j < gap->ga_len; ++j)
10726 fnames[j - 1] = fnames[j];
10727 --gap->ga_len;
10728 }
10729}
10730#endif
10731
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010732static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010733
10734/*
10735 * Return TRUE if "p" contains what looks like an environment variable.
10736 * Allowing for escaping.
10737 */
10738 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010739has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010740{
10741 for ( ; *p; mb_ptr_adv(p))
10742 {
10743 if (*p == '\\' && p[1] != NUL)
10744 ++p;
10745 else if (vim_strchr((char_u *)
Bram Moolenaare7fedb62015-12-31 19:07:19 +010010746#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010747 "$%"
10748#else
10749 "$"
10750#endif
10751 , *p) != NULL)
10752 return TRUE;
10753 }
10754 return FALSE;
10755}
10756
10757#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010758static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010759
10760/*
10761 * Return TRUE if "p" contains a special wildcard character.
10762 * Allowing for escaping.
10763 */
10764 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010765has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010766{
10767 for ( ; *p; mb_ptr_adv(p))
10768 {
10769 if (*p == '\\' && p[1] != NUL)
10770 ++p;
10771 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10772 return TRUE;
10773 }
10774 return FALSE;
10775}
10776#endif
10777
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778/*
10779 * Generic wildcard expansion code.
10780 *
10781 * Characters in "pat" that should not be expanded must be preceded with a
10782 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10783 *
10784 * Return FAIL when no single file was found. In this case "num_file" is not
10785 * set, and "file" may contain an error message.
10786 * Return OK when some files found. "num_file" is set to the number of
10787 * matches, "file" to the array of matches. Call FreeWild() later.
10788 */
10789 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010790gen_expand_wildcards(
10791 int num_pat, /* number of input patterns */
10792 char_u **pat, /* array of input patterns */
10793 int *num_file, /* resulting number of files */
10794 char_u ***file, /* array of resulting files */
10795 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796{
10797 int i;
10798 garray_T ga;
10799 char_u *p;
10800 static int recursive = FALSE;
10801 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010802 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010803#if defined(FEAT_SEARCHPATH)
10804 int did_expand_in_path = FALSE;
10805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806
10807 /*
10808 * expand_env() is called to expand things like "~user". If this fails,
10809 * it calls ExpandOne(), which brings us back here. In this case, always
10810 * call the machine specific expansion function, if possible. Otherwise,
10811 * return FAIL.
10812 */
10813 if (recursive)
10814#ifdef SPECIAL_WILDCHAR
10815 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10816#else
10817 return FAIL;
10818#endif
10819
10820#ifdef SPECIAL_WILDCHAR
10821 /*
10822 * If there are any special wildcard characters which we cannot handle
10823 * here, call machine specific function for all the expansion. This
10824 * avoids starting the shell for each argument separately.
10825 * For `=expr` do use the internal function.
10826 */
10827 for (i = 0; i < num_pat; i++)
10828 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010829 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830# ifdef VIM_BACKTICK
10831 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10832# endif
10833 )
10834 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10835 }
10836#endif
10837
10838 recursive = TRUE;
10839
10840 /*
10841 * The matching file names are stored in a growarray. Init it empty.
10842 */
10843 ga_init2(&ga, (int)sizeof(char_u *), 30);
10844
10845 for (i = 0; i < num_pat; ++i)
10846 {
10847 add_pat = -1;
10848 p = pat[i];
10849
10850#ifdef VIM_BACKTICK
10851 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010852 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010854 if (add_pat == -1)
10855 retval = FAIL;
10856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 else
10858#endif
10859 {
10860 /*
10861 * First expand environment variables, "~/" and "~user/".
10862 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010863 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010865 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 if (p == NULL)
10867 p = pat[i];
10868#ifdef UNIX
10869 /*
10870 * On Unix, if expand_env() can't expand an environment
10871 * variable, use the shell to do that. Discard previously
10872 * found file names and start all over again.
10873 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010874 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 {
10876 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010877 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010879 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 recursive = FALSE;
10881 return i;
10882 }
10883#endif
10884 }
10885
10886 /*
10887 * If there are wildcards: Expand file names and add each match to
10888 * the list. If there is no match, and EW_NOTFOUND is given, add
10889 * the pattern.
10890 * If there are no wildcards: Add the file name if it exists or
10891 * when EW_NOTFOUND is given.
10892 */
10893 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010894 {
10895#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010896 if ((flags & EW_PATH)
10897 && !mch_isFullName(p)
10898 && !(p[0] == '.'
10899 && (vim_ispathsep(p[1])
10900 || (p[1] == '.' && vim_ispathsep(p[2]))))
10901 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010902 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010903 /* :find completion where 'path' is used.
10904 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010905 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010906 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010907 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010908 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010909 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010910 else
10911#endif
10912 add_pat = mch_expandpath(&ga, p, flags);
10913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 }
10915
10916 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10917 {
10918 char_u *t = backslash_halve_save(p);
10919
10920#if defined(MACOS_CLASSIC)
10921 slash_to_colon(t);
10922#endif
10923 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10924 * "vim c:/" work. */
10925 if (flags & EW_NOTFOUND)
10926 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10927 else if (mch_getperm(t) >= 0)
10928 addfile(&ga, t, flags);
10929 vim_free(t);
10930 }
10931
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010932#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010933 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010934 uniquefy_paths(&ga, p);
10935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 if (p != pat[i])
10937 vim_free(p);
10938 }
10939
10940 *num_file = ga.ga_len;
10941 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10942
10943 recursive = FALSE;
10944
Bram Moolenaar336bd622016-01-17 18:23:58 +010010945 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946}
10947
10948# ifdef VIM_BACKTICK
10949
10950/*
10951 * Return TRUE if we can expand this backtick thing here.
10952 */
10953 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010954vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955{
10956 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10957}
10958
10959/*
10960 * Expand an item in `backticks` by executing it as a command.
10961 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020010962 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 */
10964 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010965expand_backtick(
10966 garray_T *gap,
10967 char_u *pat,
10968 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
10970 char_u *p;
10971 char_u *cmd;
10972 char_u *buffer;
10973 int cnt = 0;
10974 int i;
10975
10976 /* Create the command: lop off the backticks. */
10977 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10978 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010979 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980
10981#ifdef FEAT_EVAL
10982 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010983 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 else
10985#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010986 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010987 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 vim_free(cmd);
10989 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010990 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991
10992 cmd = buffer;
10993 while (*cmd != NUL)
10994 {
10995 cmd = skipwhite(cmd); /* skip over white space */
10996 p = cmd;
10997 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10998 ++p;
10999 /* add an entry if it is not empty */
11000 if (p > cmd)
11001 {
11002 i = *p;
11003 *p = NUL;
11004 addfile(gap, cmd, flags);
11005 *p = i;
11006 ++cnt;
11007 }
11008 cmd = p;
11009 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11010 ++cmd;
11011 }
11012
11013 vim_free(buffer);
11014 return cnt;
11015}
11016# endif /* VIM_BACKTICK */
11017
11018/*
11019 * Add a file to a file list. Accepted flags:
11020 * EW_DIR add directories
11021 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011022 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 * EW_NOTFOUND add even when it doesn't exist
11024 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011025 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 */
11027 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011028addfile(
11029 garray_T *gap,
11030 char_u *f, /* filename */
11031 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
11033 char_u *p;
11034 int isdir;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011035 struct stat sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011037 /* if the file/dir/link doesn't exist, may not add it */
11038 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011039 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 return;
11041
11042#ifdef FNAME_ILLEGAL
11043 /* if the file/dir contains illegal characters, don't add it */
11044 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11045 return;
11046#endif
11047
11048 isdir = mch_isdir(f);
11049 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11050 return;
11051
Bram Moolenaarb5971142015-03-21 17:32:19 +010011052 /* If the file isn't executable, may not add it. Do accept directories.
11053 * When invoked from expand_shellcmd() do not use $PATH. */
11054 if (!isdir && (flags & EW_EXEC)
11055 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011056 return;
11057
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 /* Make room for another item in the file list. */
11059 if (ga_grow(gap, 1) == FAIL)
11060 return;
11061
11062 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11063 if (p == NULL)
11064 return;
11065
11066 STRCPY(p, f);
11067#ifdef BACKSLASH_IN_FILENAME
11068 slash_adjust(p);
11069#endif
11070 /*
11071 * Append a slash or backslash after directory names if none is present.
11072 */
11073#ifndef DONT_ADD_PATHSEP_TO_DIR
11074 if (isdir && (flags & EW_ADDSLASH))
11075 add_pathsep(p);
11076#endif
11077 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078}
11079#endif /* !NO_EXPANDPATH */
11080
11081#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11082
11083#ifndef SEEK_SET
11084# define SEEK_SET 0
11085#endif
11086#ifndef SEEK_END
11087# define SEEK_END 2
11088#endif
11089
11090/*
11091 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011092 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11093 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 * Returns an allocated string, or NULL for error.
11095 */
11096 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011097get_cmd_output(
11098 char_u *cmd,
11099 char_u *infile, /* optional input file name */
11100 int flags, /* can be SHELL_SILENT */
11101 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102{
11103 char_u *tempname;
11104 char_u *command;
11105 char_u *buffer = NULL;
11106 int len;
11107 int i = 0;
11108 FILE *fd;
11109
11110 if (check_restricted() || check_secure())
11111 return NULL;
11112
11113 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011114 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 {
11116 EMSG(_(e_notmp));
11117 return NULL;
11118 }
11119
11120 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011121 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 if (command == NULL)
11123 goto done;
11124
11125 /*
11126 * Call the shell to execute the command (errors are ignored).
11127 * Don't check timestamps here.
11128 */
11129 ++no_check_timestamps;
11130 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11131 --no_check_timestamps;
11132
11133 vim_free(command);
11134
11135 /*
11136 * read the names from the file into memory
11137 */
11138# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011139 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 fd = mch_fopen((char *)tempname, "r");
11141# else
11142 fd = mch_fopen((char *)tempname, READBIN);
11143# endif
11144
11145 if (fd == NULL)
11146 {
11147 EMSG2(_(e_notopen), tempname);
11148 goto done;
11149 }
11150
11151 fseek(fd, 0L, SEEK_END);
11152 len = ftell(fd); /* get size of temp file */
11153 fseek(fd, 0L, SEEK_SET);
11154
11155 buffer = alloc(len + 1);
11156 if (buffer != NULL)
11157 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11158 fclose(fd);
11159 mch_remove(tempname);
11160 if (buffer == NULL)
11161 goto done;
11162#ifdef VMS
11163 len = i; /* VMS doesn't give us what we asked for... */
11164#endif
11165 if (i != len)
11166 {
11167 EMSG2(_(e_notread), tempname);
11168 vim_free(buffer);
11169 buffer = NULL;
11170 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011171 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011172 {
11173 /* Change NUL into SOH, otherwise the string is truncated. */
11174 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011175 if (buffer[i] == NUL)
11176 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011177
Bram Moolenaar162bd912010-07-28 22:29:10 +020011178 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011179 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011180 else
11181 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182
11183done:
11184 vim_free(tempname);
11185 return buffer;
11186}
11187#endif
11188
11189/*
11190 * Free the list of files returned by expand_wildcards() or other expansion
11191 * functions.
11192 */
11193 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011194FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011196 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 return;
11198#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
11199 /*
11200 * Is this still OK for when other functions than expand_wildcards() have
11201 * been used???
11202 */
11203 _fnexplodefree((char **)files);
11204#else
11205 while (count--)
11206 vim_free(files[count]);
11207 vim_free(files);
11208#endif
11209}
11210
11211/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011212 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 * Don't do this when still processing a command or a mapping.
11214 * Don't do this when inside a ":normal" command.
11215 */
11216 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011217goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218{
11219 return (p_im && stuff_empty() && typebuf_typed());
11220}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011221
11222/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011223 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011224 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11225 * - Remove any argument. E.g., "csh -f" -> "csh".
11226 * But don't allow a space in the path, so that this works:
11227 * "/usr/bin/csh --rcfile ~/.cshrc"
11228 * But don't do that for Windows, it's common to have a space in the path.
11229 */
11230 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011231get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011232{
11233 char_u *p;
11234
11235#ifdef WIN3264
11236 p = gettail(p_sh);
11237 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11238#else
11239 p = skiptowhite(p_sh);
11240 if (*p == NUL)
11241 {
11242 /* No white space, use the tail. */
11243 p = vim_strsave(gettail(p_sh));
11244 }
11245 else
11246 {
11247 char_u *p1, *p2;
11248
11249 /* Find the last path separator before the space. */
11250 p1 = p_sh;
11251 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11252 if (vim_ispathsep(*p2))
11253 p1 = p2 + 1;
11254 p = vim_strnsave(p1, (int)(p - p1));
11255 }
11256#endif
11257 return p;
11258}