blob: a764a7b07ec1f8379f3c9cf9f64993ad6fd27080 [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
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001979#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 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
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002055#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 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 Moolenaar165bc692015-07-21 17:53:25 +02003679 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003681 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003683
3684 /* When 'verbose' is set and we are sourcing a script or executing a
3685 * function give the user a hint where the beep comes from. */
3686 if (vim_strchr(p_debug, 'e') != NULL)
3687 {
3688 msg_source(hl_attr(HLF_W));
3689 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692}
3693
3694/*
3695 * To get the "real" home directory:
3696 * - get value of $HOME
3697 * For Unix:
3698 * - go to that directory
3699 * - do mch_dirname() to get the real name of that directory.
3700 * This also works with mounts and links.
3701 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3702 */
3703static char_u *homedir = NULL;
3704
3705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003706init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 char_u *var;
3709
Bram Moolenaar05159a02005-02-26 23:04:13 +00003710 /* In case we are called a second time (when 'encoding' changes). */
3711 vim_free(homedir);
3712 homedir = NULL;
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714#ifdef VMS
3715 var = mch_getenv((char_u *)"SYS$LOGIN");
3716#else
3717 var = mch_getenv((char_u *)"HOME");
3718#endif
3719
3720 if (var != NULL && *var == NUL) /* empty is same as not set */
3721 var = NULL;
3722
3723#ifdef WIN3264
3724 /*
3725 * Weird but true: $HOME may contain an indirect reference to another
3726 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3727 * when $HOME is being set.
3728 */
3729 if (var != NULL && *var == '%')
3730 {
3731 char_u *p;
3732 char_u *exp;
3733
3734 p = vim_strchr(var + 1, '%');
3735 if (p != NULL)
3736 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003737 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 exp = mch_getenv(NameBuff);
3739 if (exp != NULL && *exp != NUL
3740 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3741 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003742 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 var = NameBuff;
3744 /* Also set $HOME, it's needed for _viminfo. */
3745 vim_setenv((char_u *)"HOME", NameBuff);
3746 }
3747 }
3748 }
3749
3750 /*
3751 * Typically, $HOME is not defined on Windows, unless the user has
3752 * specifically defined it for Vim's sake. However, on Windows NT
3753 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3754 * each user. Try constructing $HOME from these.
3755 */
3756 if (var == NULL)
3757 {
3758 char_u *homedrive, *homepath;
3759
3760 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3761 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003762 if (homepath == NULL || *homepath == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003763 homepath = (char_u *)"\\";
Bram Moolenaar6f977012010-01-06 17:53:38 +01003764 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3766 {
3767 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3768 if (NameBuff[0] != NUL)
3769 {
3770 var = NameBuff;
3771 /* Also set $HOME, it's needed for _viminfo. */
3772 vim_setenv((char_u *)"HOME", NameBuff);
3773 }
3774 }
3775 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003776
3777# if defined(FEAT_MBYTE)
3778 if (enc_utf8 && var != NULL)
3779 {
3780 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003781 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003782
3783 /* Convert from active codepage to UTF-8. Other conversions are
3784 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003785 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003786 if (pp != NULL)
3787 {
3788 homedir = pp;
3789 return;
3790 }
3791 }
3792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#endif
3794
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003795#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 /*
3797 * Default home dir is C:/
3798 * Best assumption we can make in such a situation.
3799 */
3800 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003801 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802#endif
3803 if (var != NULL)
3804 {
3805#ifdef UNIX
3806 /*
3807 * Change to the directory and get the actual path. This resolves
3808 * links. Don't do it when we can't return.
3809 */
3810 if (mch_dirname(NameBuff, MAXPATHL) == OK
3811 && mch_chdir((char *)NameBuff) == 0)
3812 {
3813 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3814 var = IObuff;
3815 if (mch_chdir((char *)NameBuff) != 0)
3816 EMSG(_(e_prev_dir));
3817 }
3818#endif
3819 homedir = vim_strsave(var);
3820 }
3821}
3822
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003823#if defined(EXITFREE) || defined(PROTO)
3824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003825free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003826{
3827 vim_free(homedir);
3828}
Bram Moolenaar24305862012-08-15 14:05:05 +02003829
3830# ifdef FEAT_CMDL_COMPL
3831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003832free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003833{
3834 ga_clear_strings(&ga_users);
3835}
3836# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003837#endif
3838
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003840 * Call expand_env() and store the result in an allocated string.
3841 * This is not very memory efficient, this expects the result to be freed
3842 * again soon.
3843 */
3844 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003845expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003846{
3847 return expand_env_save_opt(src, FALSE);
3848}
3849
3850/*
3851 * Idem, but when "one" is TRUE handle the string as one file name, only
3852 * expand "~" at the start.
3853 */
3854 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003855expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003856{
3857 char_u *p;
3858
3859 p = alloc(MAXPATHL);
3860 if (p != NULL)
3861 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3862 return p;
3863}
3864
3865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * Expand environment variable with path name.
3867 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003868 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 * If anything fails no expansion is done and dst equals src.
3870 */
3871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003872expand_env(
3873 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3874 char_u *dst, /* where to put the result */
3875 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003877 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878}
3879
3880 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003881expand_env_esc(
3882 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3883 char_u *dst, /* where to put the result */
3884 int dstlen, /* maximum length of the result */
3885 int esc, /* escape spaces in expanded variables */
3886 int one, /* "srcp" is one file name */
3887 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003889 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 char_u *tail;
3891 int c;
3892 char_u *var;
3893 int copy_char;
3894 int mustfree; /* var was allocated, need to free it later */
3895 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003896 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003898 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003899 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003900
3901 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 --dstlen; /* leave one char space for "\," */
3903 while (*src && dstlen > 0)
3904 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003905#ifdef FEAT_EVAL
3906 /* Skip over `=expr`. */
3907 if (src[0] == '`' && src[1] == '=')
3908 {
3909 size_t len;
3910
3911 var = src;
3912 src += 2;
3913 (void)skip_expr(&src);
3914 if (*src == '`')
3915 ++src;
3916 len = src - var;
3917 if (len > (size_t)dstlen)
3918 len = dstlen;
3919 vim_strncpy(dst, var, len);
3920 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003921 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003922 continue;
3923 }
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003926 if ((*src == '$'
3927#ifdef VMS
3928 && at_start
3929#endif
3930 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003931#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 || *src == '%'
3933#endif
3934 || (*src == '~' && at_start))
3935 {
3936 mustfree = FALSE;
3937
3938 /*
3939 * The variable name is copied into dst temporarily, because it may
3940 * be a string in read-only memory and a NUL needs to be appended.
3941 */
3942 if (*src != '~') /* environment var */
3943 {
3944 tail = src + 1;
3945 var = dst;
3946 c = dstlen - 1;
3947
3948#ifdef UNIX
3949 /* Unix has ${var-name} type environment vars */
3950 if (*tail == '{' && !vim_isIDc('{'))
3951 {
3952 tail++; /* ignore '{' */
3953 while (c-- > 0 && *tail && *tail != '}')
3954 *var++ = *tail++;
3955 }
3956 else
3957#endif
3958 {
3959 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003960#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 || (*src == '%' && *tail != '%')
3962#endif
3963 ))
3964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 }
3968
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003969#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970# ifdef UNIX
3971 if (src[1] == '{' && *tail != '}')
3972# else
3973 if (*src == '%' && *tail != '%')
3974# endif
3975 var = NULL;
3976 else
3977 {
3978# ifdef UNIX
3979 if (src[1] == '{')
3980# else
3981 if (*src == '%')
3982#endif
3983 ++tail;
3984#endif
3985 *var = NUL;
3986 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003987#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989#endif
3990 }
3991 /* home directory */
3992 else if ( src[1] == NUL
3993 || vim_ispathsep(src[1])
3994 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3995 {
3996 var = homedir;
3997 tail = src + 1;
3998 }
3999 else /* user directory */
4000 {
4001#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4002 /*
4003 * Copy ~user to dst[], so we can put a NUL after it.
4004 */
4005 tail = src;
4006 var = dst;
4007 c = dstlen - 1;
4008 while ( c-- > 0
4009 && *tail
4010 && vim_isfilec(*tail)
4011 && !vim_ispathsep(*tail))
4012 *var++ = *tail++;
4013 *var = NUL;
4014# ifdef UNIX
4015 /*
4016 * If the system supports getpwnam(), use it.
4017 * Otherwise, or if getpwnam() fails, the shell is used to
4018 * expand ~user. This is slower and may fail if the shell
4019 * does not support ~user (old versions of /bin/sh).
4020 */
4021# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4022 {
4023 struct passwd *pw;
4024
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004025 /* Note: memory allocated by getpwnam() is never freed.
4026 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 pw = getpwnam((char *)dst + 1);
4028 if (pw != NULL)
4029 var = (char_u *)pw->pw_dir;
4030 else
4031 var = NULL;
4032 }
4033 if (var == NULL)
4034# endif
4035 {
4036 expand_T xpc;
4037
4038 ExpandInit(&xpc);
4039 xpc.xp_context = EXPAND_FILES;
4040 var = ExpandOne(&xpc, dst, NULL,
4041 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 mustfree = TRUE;
4043 }
4044
4045# else /* !UNIX, thus VMS */
4046 /*
4047 * USER_HOME is a comma-separated list of
4048 * directories to search for the user account in.
4049 */
4050 {
4051 char_u test[MAXPATHL], paths[MAXPATHL];
4052 char_u *path, *next_path, *ptr;
4053 struct stat st;
4054
4055 STRCPY(paths, USER_HOME);
4056 next_path = paths;
4057 while (*next_path)
4058 {
4059 for (path = next_path; *next_path && *next_path != ',';
4060 next_path++);
4061 if (*next_path)
4062 *next_path++ = NUL;
4063 STRCPY(test, path);
4064 STRCAT(test, "/");
4065 STRCAT(test, dst + 1);
4066 if (mch_stat(test, &st) == 0)
4067 {
4068 var = alloc(STRLEN(test) + 1);
4069 STRCPY(var, test);
4070 mustfree = TRUE;
4071 break;
4072 }
4073 }
4074 }
4075# endif /* UNIX */
4076#else
4077 /* cannot expand user's home directory, so don't try */
4078 var = NULL;
4079 tail = (char_u *)""; /* for gcc */
4080#endif /* UNIX || VMS */
4081 }
4082
4083#ifdef BACKSLASH_IN_FILENAME
4084 /* If 'shellslash' is set change backslashes to forward slashes.
4085 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4086 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4087 {
4088 char_u *p = vim_strsave(var);
4089
4090 if (p != NULL)
4091 {
4092 if (mustfree)
4093 vim_free(var);
4094 var = p;
4095 mustfree = TRUE;
4096 forward_slash(var);
4097 }
4098 }
4099#endif
4100
4101 /* If "var" contains white space, escape it with a backslash.
4102 * Required for ":e ~/tt" when $HOME includes a space. */
4103 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4104 {
4105 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4106
4107 if (p != NULL)
4108 {
4109 if (mustfree)
4110 vim_free(var);
4111 var = p;
4112 mustfree = TRUE;
4113 }
4114 }
4115
4116 if (var != NULL && *var != NUL
4117 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4118 {
4119 STRCPY(dst, var);
4120 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004121 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 /* if var[] ends in a path separator and tail[] starts
4123 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004124 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4126 && dst[-1] != ':'
4127#endif
4128 && vim_ispathsep(*tail))
4129 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004130 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 src = tail;
4132 copy_char = FALSE;
4133 }
4134 if (mustfree)
4135 vim_free(var);
4136 }
4137
4138 if (copy_char) /* copy at least one char */
4139 {
4140 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004141 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004142 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4143 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 */
4145 at_start = FALSE;
4146 if (src[0] == '\\' && src[1] != NUL)
4147 {
4148 *dst++ = *src++;
4149 --dstlen;
4150 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004151 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 at_start = TRUE;
4153 *dst++ = *src++;
4154 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004155
4156 if (startstr != NULL && src - startstr_len >= srcp
4157 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4158 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 }
4160 }
4161 *dst = NUL;
4162}
4163
4164/*
4165 * Vim's version of getenv().
4166 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004167 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004168 * "mustfree" is set to TRUE when returned is allocated, it must be
4169 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 */
4171 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004172vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173{
4174 char_u *p;
4175 char_u *pend;
4176 int vimruntime;
4177
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004178#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 /* use "C:/" when $HOME is not set */
4180 if (STRCMP(name, "HOME") == 0)
4181 return homedir;
4182#endif
4183
4184 p = mch_getenv(name);
4185 if (p != NULL && *p == NUL) /* empty is the same as not set */
4186 p = NULL;
4187
4188 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004189 {
4190#if defined(FEAT_MBYTE) && defined(WIN3264)
4191 if (enc_utf8)
4192 {
4193 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004194 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004195
4196 /* Convert from active codepage to UTF-8. Other conversions are
4197 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004198 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004199 if (pp != NULL)
4200 {
4201 p = pp;
4202 *mustfree = TRUE;
4203 }
4204 }
4205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4210 if (!vimruntime && STRCMP(name, "VIM") != 0)
4211 return NULL;
4212
4213 /*
4214 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4215 * Don't do this when default_vimruntime_dir is non-empty.
4216 */
4217 if (vimruntime
4218#ifdef HAVE_PATHDEF
4219 && *default_vimruntime_dir == NUL
4220#endif
4221 )
4222 {
4223 p = mch_getenv((char_u *)"VIM");
4224 if (p != NULL && *p == NUL) /* empty is the same as not set */
4225 p = NULL;
4226 if (p != NULL)
4227 {
4228 p = vim_version_dir(p);
4229 if (p != NULL)
4230 *mustfree = TRUE;
4231 else
4232 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004233
4234#if defined(FEAT_MBYTE) && defined(WIN3264)
4235 if (enc_utf8)
4236 {
4237 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004238 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004239
4240 /* Convert from active codepage to UTF-8. Other conversions
4241 * are not done, because they would fail for non-ASCII
4242 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004243 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004244 if (pp != NULL)
4245 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004246 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004247 vim_free(p);
4248 p = pp;
4249 *mustfree = TRUE;
4250 }
4251 }
4252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 /*
4257 * When expanding $VIM or $VIMRUNTIME fails, try using:
4258 * - the directory name from 'helpfile' (unless it contains '$')
4259 * - the executable name from argv[0]
4260 */
4261 if (p == NULL)
4262 {
4263 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4264 p = p_hf;
4265#ifdef USE_EXE_NAME
4266 /*
4267 * Use the name of the executable, obtained from argv[0].
4268 */
4269 else
4270 p = exe_name;
4271#endif
4272 if (p != NULL)
4273 {
4274 /* remove the file name */
4275 pend = gettail(p);
4276
4277 /* remove "doc/" from 'helpfile', if present */
4278 if (p == p_hf)
4279 pend = remove_tail(p, pend, (char_u *)"doc");
4280
4281#ifdef USE_EXE_NAME
4282# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004283 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (p == exe_name)
4285 {
4286 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004287 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004289 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4290 if (pend1 != pend)
4291 {
4292 pnew = alloc((unsigned)(pend1 - p) + 15);
4293 if (pnew != NULL)
4294 {
4295 STRNCPY(pnew, p, (pend1 - p));
4296 STRCPY(pnew + (pend1 - p), "Resources/vim");
4297 p = pnew;
4298 pend = p + STRLEN(p);
4299 }
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302# endif
4303 /* remove "src/" from exe_name, if present */
4304 if (p == exe_name)
4305 pend = remove_tail(p, pend, (char_u *)"src");
4306#endif
4307
4308 /* for $VIM, remove "runtime/" or "vim54/", if present */
4309 if (!vimruntime)
4310 {
4311 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4312 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4313 }
4314
4315 /* remove trailing path separator */
4316#ifndef MACOS_CLASSIC
4317 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004318 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004319 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 --pend;
4321#endif
4322
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004323#ifdef MACOS_X
4324 if (p == exe_name || p == p_hf)
4325#endif
4326 /* check that the result is a directory name */
4327 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 if (p != NULL && !mch_isdir(p))
4330 {
4331 vim_free(p);
4332 p = NULL;
4333 }
4334 else
4335 {
4336#ifdef USE_EXE_NAME
4337 /* may add "/vim54" or "/runtime" if it exists */
4338 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4339 {
4340 vim_free(p);
4341 p = pend;
4342 }
4343#endif
4344 *mustfree = TRUE;
4345 }
4346 }
4347 }
4348
4349#ifdef HAVE_PATHDEF
4350 /* When there is a pathdef.c file we can use default_vim_dir and
4351 * default_vimruntime_dir */
4352 if (p == NULL)
4353 {
4354 /* Only use default_vimruntime_dir when it is not empty */
4355 if (vimruntime && *default_vimruntime_dir != NUL)
4356 {
4357 p = default_vimruntime_dir;
4358 *mustfree = FALSE;
4359 }
4360 else if (*default_vim_dir != NUL)
4361 {
4362 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4363 *mustfree = TRUE;
4364 else
4365 {
4366 p = default_vim_dir;
4367 *mustfree = FALSE;
4368 }
4369 }
4370 }
4371#endif
4372
4373 /*
4374 * Set the environment variable, so that the new value can be found fast
4375 * next time, and others can also use it (e.g. Perl).
4376 */
4377 if (p != NULL)
4378 {
4379 if (vimruntime)
4380 {
4381 vim_setenv((char_u *)"VIMRUNTIME", p);
4382 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 else
4385 {
4386 vim_setenv((char_u *)"VIM", p);
4387 didset_vim = TRUE;
4388 }
4389 }
4390 return p;
4391}
4392
4393/*
4394 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4395 * Return NULL if not, return its name in allocated memory otherwise.
4396 */
4397 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004398vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
4400 char_u *p;
4401
4402 if (vimdir == NULL || *vimdir == NUL)
4403 return NULL;
4404 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4405 if (p != NULL && mch_isdir(p))
4406 return p;
4407 vim_free(p);
4408 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4409 if (p != NULL && mch_isdir(p))
4410 return p;
4411 vim_free(p);
4412 return NULL;
4413}
4414
4415/*
4416 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4417 * the length of "name/". Otherwise return "pend".
4418 */
4419 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004420remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421{
4422 int len = (int)STRLEN(name) + 1;
4423 char_u *newend = pend - len;
4424
4425 if (newend >= p
4426 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004427 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 return newend;
4429 return pend;
4430}
4431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * Our portable version of setenv.
4434 */
4435 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004436vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
4438#ifdef HAVE_SETENV
4439 mch_setenv((char *)name, (char *)val, 1);
4440#else
4441 char_u *envbuf;
4442
4443 /*
4444 * Putenv does not copy the string, it has to remain
4445 * valid. The allocated memory will never be freed.
4446 */
4447 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4448 if (envbuf != NULL)
4449 {
4450 sprintf((char *)envbuf, "%s=%s", name, val);
4451 putenv((char *)envbuf);
4452 }
4453#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004454#ifdef FEAT_GETTEXT
4455 /*
4456 * When setting $VIMRUNTIME adjust the directory to find message
4457 * translations to $VIMRUNTIME/lang.
4458 */
4459 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4460 {
4461 char_u *buf = concat_str(val, (char_u *)"/lang");
4462
4463 if (buf != NULL)
4464 {
4465 bindtextdomain(VIMPACKAGE, (char *)buf);
4466 vim_free(buf);
4467 }
4468 }
4469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470}
4471
4472#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4473/*
4474 * Function given to ExpandGeneric() to obtain an environment variable name.
4475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004477get_env_name(
4478 expand_T *xp UNUSED,
4479 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480{
4481# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4482 /*
4483 * No environ[] on the Amiga and on the Mac (using MPW).
4484 */
4485 return NULL;
4486# else
4487# ifndef __WIN32__
4488 /* Borland C++ 5.2 has this in a header file. */
4489 extern char **environ;
4490# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004491# define ENVNAMELEN 100
4492 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 char_u *str;
4494 int n;
4495
4496 str = (char_u *)environ[idx];
4497 if (str == NULL)
4498 return NULL;
4499
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004500 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
4502 if (str[n] == '=' || str[n] == NUL)
4503 break;
4504 name[n] = str[n];
4505 }
4506 name[n] = NUL;
4507 return name;
4508# endif
4509}
Bram Moolenaar24305862012-08-15 14:05:05 +02004510
4511/*
4512 * Find all user names for user completion.
4513 * Done only once and then cached.
4514 */
4515 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004516init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004517{
Bram Moolenaar24305862012-08-15 14:05:05 +02004518 static int lazy_init_done = FALSE;
4519
4520 if (lazy_init_done)
4521 return;
4522
4523 lazy_init_done = TRUE;
4524 ga_init2(&ga_users, sizeof(char_u *), 20);
4525
4526# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4527 {
4528 char_u* user;
4529 struct passwd* pw;
4530
4531 setpwent();
4532 while ((pw = getpwent()) != NULL)
4533 /* pw->pw_name shouldn't be NULL but just in case... */
4534 if (pw->pw_name != NULL)
4535 {
4536 if (ga_grow(&ga_users, 1) == FAIL)
4537 break;
4538 user = vim_strsave((char_u*)pw->pw_name);
4539 if (user == NULL)
4540 break;
4541 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4542 }
4543 endpwent();
4544 }
4545# endif
4546}
4547
4548/*
4549 * Function given to ExpandGeneric() to obtain an user names.
4550 */
4551 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004552get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004553{
4554 init_users();
4555 if (idx < ga_users.ga_len)
4556 return ((char_u **)ga_users.ga_data)[idx];
4557 return NULL;
4558}
4559
4560/*
4561 * Check whether name matches a user name. Return:
4562 * 0 if name does not match any user name.
4563 * 1 if name partially matches the beginning of a user name.
4564 * 2 is name fully matches a user name.
4565 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004566int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004567{
4568 int i;
4569 int n = (int)STRLEN(name);
4570 int result = 0;
4571
4572 init_users();
4573 for (i = 0; i < ga_users.ga_len; i++)
4574 {
4575 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4576 return 2; /* full match */
4577 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4578 result = 1; /* partial match */
4579 }
4580 return result;
4581}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#endif
4583
4584/*
4585 * Replace home directory by "~" in each space or comma separated file name in
4586 * 'src'.
4587 * If anything fails (except when out of space) dst equals src.
4588 */
4589 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004590home_replace(
4591 buf_T *buf, /* when not NULL, check for help files */
4592 char_u *src, /* input file name */
4593 char_u *dst, /* where to put the result */
4594 int dstlen, /* maximum length of the result */
4595 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 spaces and commas in the file name. */
4597{
4598 size_t dirlen = 0, envlen = 0;
4599 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004600 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 char_u *p;
4602
4603 if (src == NULL)
4604 {
4605 *dst = NUL;
4606 return;
4607 }
4608
4609 /*
4610 * If the file is a help file, remove the path completely.
4611 */
4612 if (buf != NULL && buf->b_help)
4613 {
4614 STRCPY(dst, gettail(src));
4615 return;
4616 }
4617
4618 /*
4619 * We check both the value of the $HOME environment variable and the
4620 * "real" home directory.
4621 */
4622 if (homedir != NULL)
4623 dirlen = STRLEN(homedir);
4624
4625#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004626 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004628 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4629#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004630 /* Empty is the same as not set. */
4631 if (homedir_env != NULL && *homedir_env == NUL)
4632 homedir_env = NULL;
4633
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004634#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004635 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004636 {
4637 int usedlen = 0;
4638 int flen;
4639 char_u *fbuf = NULL;
4640
4641 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004642 (void)modify_fname((char_u *)":p", &usedlen,
4643 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004644 flen = (int)STRLEN(homedir_env);
4645 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4646 /* Remove the trailing / that is added to a directory. */
4647 homedir_env[flen - 1] = NUL;
4648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#endif
4650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (homedir_env != NULL)
4652 envlen = STRLEN(homedir_env);
4653
4654 if (!one)
4655 src = skipwhite(src);
4656 while (*src && dstlen > 0)
4657 {
4658 /*
4659 * Here we are at the beginning of a file name.
4660 * First, check to see if the beginning of the file name matches
4661 * $HOME or the "real" home directory. Check that there is a '/'
4662 * after the match (so that if e.g. the file is "/home/pieter/bla",
4663 * and the home directory is "/home/piet", the file does not end up
4664 * as "~er/bla" (which would seem to indicate the file "bla" in user
4665 * er's home directory)).
4666 */
4667 p = homedir;
4668 len = dirlen;
4669 for (;;)
4670 {
4671 if ( len
4672 && fnamencmp(src, p, len) == 0
4673 && (vim_ispathsep(src[len])
4674 || (!one && (src[len] == ',' || src[len] == ' '))
4675 || src[len] == NUL))
4676 {
4677 src += len;
4678 if (--dstlen > 0)
4679 *dst++ = '~';
4680
4681 /*
4682 * If it's just the home directory, add "/".
4683 */
4684 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4685 *dst++ = '/';
4686 break;
4687 }
4688 if (p == homedir_env)
4689 break;
4690 p = homedir_env;
4691 len = envlen;
4692 }
4693
4694 /* if (!one) skip to separator: space or comma */
4695 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4696 *dst++ = *src++;
4697 /* skip separator */
4698 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4699 *dst++ = *src++;
4700 }
4701 /* if (dstlen == 0) out of space, what to do??? */
4702
4703 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004704
4705 if (homedir_env != homedir_env_orig)
4706 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707}
4708
4709/*
4710 * Like home_replace, store the replaced string in allocated memory.
4711 * When something fails, NULL is returned.
4712 */
4713 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004714home_replace_save(
4715 buf_T *buf, /* when not NULL, check for help files */
4716 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
4718 char_u *dst;
4719 unsigned len;
4720
4721 len = 3; /* space for "~/" and trailing NUL */
4722 if (src != NULL) /* just in case */
4723 len += (unsigned)STRLEN(src);
4724 dst = alloc(len);
4725 if (dst != NULL)
4726 home_replace(buf, src, dst, len, TRUE);
4727 return dst;
4728}
4729
4730/*
4731 * Compare two file names and return:
4732 * FPC_SAME if they both exist and are the same file.
4733 * FPC_SAMEX if they both don't exist and have the same file name.
4734 * FPC_DIFF if they both exist and are different files.
4735 * FPC_NOTX if they both don't exist.
4736 * FPC_DIFFX if one of them doesn't exist.
4737 * For the first name environment variables are expanded
4738 */
4739 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004740fullpathcmp(
4741 char_u *s1,
4742 char_u *s2,
4743 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
4745#ifdef UNIX
4746 char_u exp1[MAXPATHL];
4747 char_u full1[MAXPATHL];
4748 char_u full2[MAXPATHL];
4749 struct stat st1, st2;
4750 int r1, r2;
4751
4752 expand_env(s1, exp1, MAXPATHL);
4753 r1 = mch_stat((char *)exp1, &st1);
4754 r2 = mch_stat((char *)s2, &st2);
4755 if (r1 != 0 && r2 != 0)
4756 {
4757 /* if mch_stat() doesn't work, may compare the names */
4758 if (checkname)
4759 {
4760 if (fnamecmp(exp1, s2) == 0)
4761 return FPC_SAMEX;
4762 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4763 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4764 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4765 return FPC_SAMEX;
4766 }
4767 return FPC_NOTX;
4768 }
4769 if (r1 != 0 || r2 != 0)
4770 return FPC_DIFFX;
4771 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4772 return FPC_SAME;
4773 return FPC_DIFF;
4774#else
4775 char_u *exp1; /* expanded s1 */
4776 char_u *full1; /* full path of s1 */
4777 char_u *full2; /* full path of s2 */
4778 int retval = FPC_DIFF;
4779 int r1, r2;
4780
4781 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4782 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4783 {
4784 full1 = exp1 + MAXPATHL;
4785 full2 = full1 + MAXPATHL;
4786
4787 expand_env(s1, exp1, MAXPATHL);
4788 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4789 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4790
4791 /* If vim_FullName() fails, the file probably doesn't exist. */
4792 if (r1 != OK && r2 != OK)
4793 {
4794 if (checkname && fnamecmp(exp1, s2) == 0)
4795 retval = FPC_SAMEX;
4796 else
4797 retval = FPC_NOTX;
4798 }
4799 else if (r1 != OK || r2 != OK)
4800 retval = FPC_DIFFX;
4801 else if (fnamecmp(full1, full2))
4802 retval = FPC_DIFF;
4803 else
4804 retval = FPC_SAME;
4805 vim_free(exp1);
4806 }
4807 return retval;
4808#endif
4809}
4810
4811/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004812 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004813 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004814 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 */
4816 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004817gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
4819 char_u *p1, *p2;
4820
4821 if (fname == NULL)
4822 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004823 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004825 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004827 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 return p1;
4830}
4831
Bram Moolenaar31710262010-08-13 13:36:15 +02004832#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004833static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004834
4835/*
4836 * Return the end of the directory name, on the first path
4837 * separator:
4838 * "/path/file", "/path/dir/", "/path//dir", "/file"
4839 * ^ ^ ^ ^
4840 */
4841 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004842gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004843{
4844 char_u *dir_end = fname;
4845 char_u *next_dir_end = fname;
4846 int look_for_sep = TRUE;
4847 char_u *p;
4848
4849 for (p = fname; *p != NUL; )
4850 {
4851 if (vim_ispathsep(*p))
4852 {
4853 if (look_for_sep)
4854 {
4855 next_dir_end = p;
4856 look_for_sep = FALSE;
4857 }
4858 }
4859 else
4860 {
4861 if (!look_for_sep)
4862 dir_end = next_dir_end;
4863 look_for_sep = TRUE;
4864 }
4865 mb_ptr_adv(p);
4866 }
4867 return dir_end;
4868}
4869#endif
4870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004872 * Get pointer to tail of "fname", including path separators. Putting a NUL
4873 * here leaves the directory name. Takes care of "c:/" and "//".
4874 * Always returns a valid pointer.
4875 */
4876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004877gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004878{
4879 char_u *p;
4880 char_u *t;
4881
4882 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4883 t = gettail(fname);
4884 while (t > p && after_pathsep(fname, t))
4885 --t;
4886#ifdef VMS
4887 /* path separator is part of the path */
4888 ++t;
4889#endif
4890 return t;
4891}
4892
4893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 * get the next path component (just after the next path separator).
4895 */
4896 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004897getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004900 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (*fname)
4902 ++fname;
4903 return fname;
4904}
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906/*
4907 * Get a pointer to one character past the head of a path name.
4908 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4909 * If there is no head, path is returned.
4910 */
4911 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004912get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
4914 char_u *retval;
4915
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004916#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 /* may skip "c:" */
4918 if (isalpha(path[0]) && path[1] == ':')
4919 retval = path + 2;
4920 else
4921 retval = path;
4922#else
4923# if defined(AMIGA)
4924 /* may skip "label:" */
4925 retval = vim_strchr(path, ':');
4926 if (retval == NULL)
4927 retval = path;
4928# else /* Unix */
4929 retval = path;
4930# endif
4931#endif
4932
4933 while (vim_ispathsep(*retval))
4934 ++retval;
4935
4936 return retval;
4937}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004940 * Return TRUE if 'c' is a path separator.
4941 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 */
4943 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004944vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004946#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004948#else
4949# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004951# else
4952# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4954 return (c == ':' || c == '[' || c == ']' || c == '/'
4955 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004956# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004958# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961}
4962
Bram Moolenaar69c35002013-11-04 02:54:12 +01004963/*
4964 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4965 */
4966 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004967vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004968{
4969 return vim_ispathsep(c)
4970#ifdef BACKSLASH_IN_FILENAME
4971 && c != ':'
4972#endif
4973 ;
4974}
4975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4977/*
4978 * return TRUE if 'c' is a path list separator.
4979 */
4980 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004981vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
4983#ifdef UNIX
4984 return (c == ':');
4985#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004986 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987#endif
4988}
4989#endif
4990
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004991#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4992 || defined(FEAT_EVAL) || defined(PROTO)
4993/*
4994 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4995 * It's done in-place.
4996 */
4997 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004998shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004999{
5000 char_u *tail, *s, *d;
5001 int skip = FALSE;
5002
5003 tail = gettail(str);
5004 d = str;
5005 for (s = str; ; ++s)
5006 {
5007 if (s >= tail) /* copy the whole tail */
5008 {
5009 *d++ = *s;
5010 if (*s == NUL)
5011 break;
5012 }
5013 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5014 {
5015 *d++ = *s;
5016 skip = FALSE;
5017 }
5018 else if (!skip)
5019 {
5020 *d++ = *s; /* copy next char */
5021 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5022 skip = TRUE;
5023# ifdef FEAT_MBYTE
5024 if (has_mbyte)
5025 {
5026 int l = mb_ptr2len(s);
5027
5028 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005029 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005030 }
5031# endif
5032 }
5033 }
5034}
5035#endif
5036
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005037/*
5038 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5039 * Also returns TRUE if there is no directory name.
5040 * "fname" must be writable!.
5041 */
5042 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005043dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005044{
5045 char_u *p;
5046 int c;
5047 int retval;
5048
5049 p = gettail_sep(fname);
5050 if (p == fname)
5051 return TRUE;
5052 c = *p;
5053 *p = NUL;
5054 retval = mch_isdir(fname);
5055 *p = c;
5056 return retval;
5057}
5058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005060 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5061 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 */
5063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005064vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005066#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005068#else
5069 if (p_fic)
5070 return MB_STRICMP(x, y);
5071 return STRCMP(x, y);
5072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073}
5074
5075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005076vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005078#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005079 char_u *px = x;
5080 char_u *py = y;
5081 int cx = NUL;
5082 int cy = NUL;
5083
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005084 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005086 cx = PTR2CHAR(px);
5087 cy = PTR2CHAR(py);
5088 if (cx == NUL || cy == NUL
5089 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5090 && !(cx == '/' && cy == '\\')
5091 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005093 len -= MB_PTR2LEN(px);
5094 px += MB_PTR2LEN(px);
5095 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 if (len == 0)
5098 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005099 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005100#else
5101 if (p_fic)
5102 return MB_STRNICMP(x, y, len);
5103 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107/*
5108 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005109 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
5111 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005112concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
5114 char_u *dest;
5115
5116 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5117 if (dest != NULL)
5118 {
5119 STRCPY(dest, fname1);
5120 if (sep)
5121 add_pathsep(dest);
5122 STRCAT(dest, fname2);
5123 }
5124 return dest;
5125}
5126
Bram Moolenaard6754642005-01-17 22:18:45 +00005127/*
5128 * Concatenate two strings and return the result in allocated memory.
5129 * Returns NULL when out of memory.
5130 */
5131 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005132concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005133{
5134 char_u *dest;
5135 size_t l = STRLEN(str1);
5136
5137 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5138 if (dest != NULL)
5139 {
5140 STRCPY(dest, str1);
5141 STRCPY(dest + l, str2);
5142 }
5143 return dest;
5144}
Bram Moolenaard6754642005-01-17 22:18:45 +00005145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146/*
5147 * Add a path separator to a file name, unless it already ends in a path
5148 * separator.
5149 */
5150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005151add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005153 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 STRCAT(p, PATHSEPSTR);
5155}
5156
5157/*
5158 * FullName_save - Make an allocated copy of a full file name.
5159 * Returns NULL when out of memory.
5160 */
5161 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005162FullName_save(
5163 char_u *fname,
5164 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005165 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 char_u *buf;
5168 char_u *new_fname = NULL;
5169
5170 if (fname == NULL)
5171 return NULL;
5172
5173 buf = alloc((unsigned)MAXPATHL);
5174 if (buf != NULL)
5175 {
5176 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5177 new_fname = vim_strsave(buf);
5178 else
5179 new_fname = vim_strsave(fname);
5180 vim_free(buf);
5181 }
5182 return new_fname;
5183}
5184
5185#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5186
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005187static char_u *skip_string(char_u *p);
5188static pos_T *ind_find_start_comment(void);
5189static pos_T *ind_find_start_CORS(void);
5190static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192/*
5193 * Find the start of a comment, not knowing if we are in a comment right now.
5194 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005195 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005197 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005198ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005199{
5200 return find_start_comment(curbuf->b_ind_maxcomment);
5201}
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005204find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
5206 pos_T *pos;
5207 char_u *line;
5208 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005209 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005211 for (;;)
5212 {
5213 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5214 if (pos == NULL)
5215 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005217 /*
5218 * Check if the comment start we found is inside a string.
5219 * If it is then restrict the search to below this line and try again.
5220 */
5221 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005222 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005223 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005224 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005225 break;
5226 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5227 if (cur_maxcomment <= 0)
5228 {
5229 pos = NULL;
5230 break;
5231 }
5232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 return pos;
5234}
5235
5236/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005237 * Find the start of a comment or raw string, not knowing if we are in a
5238 * comment or raw string right now.
5239 * Search starts at w_cursor.lnum and goes backwards.
5240 * Return NULL when not inside a comment or raw string.
5241 * "CORS" -> Comment Or Raw String
5242 */
5243 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005244ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005245{
Bram Moolenaar089af182015-10-07 11:41:49 +02005246 static pos_T comment_pos_copy;
5247 pos_T *comment_pos;
5248 pos_T *rs_pos;
5249
5250 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5251 if (comment_pos != NULL)
5252 {
5253 /* Need to make a copy of the static pos in findmatchlimit(),
5254 * calling find_start_rawstring() may change it. */
5255 comment_pos_copy = *comment_pos;
5256 comment_pos = &comment_pos_copy;
5257 }
5258 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005259
5260 /* If comment_pos is before rs_pos the raw string is inside the comment.
5261 * If rs_pos is before comment_pos the comment is inside the raw string. */
5262 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5263 return rs_pos;
5264 return comment_pos;
5265}
5266
5267/*
5268 * Find the start of a raw string, not knowing if we are in one right now.
5269 * Search starts at w_cursor.lnum and goes backwards.
5270 * Return NULL when not inside a raw string.
5271 */
5272 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005273find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005274{
5275 pos_T *pos;
5276 char_u *line;
5277 char_u *p;
5278 int cur_maxcomment = ind_maxcomment;
5279
5280 for (;;)
5281 {
5282 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5283 if (pos == NULL)
5284 break;
5285
5286 /*
5287 * Check if the raw string start we found is inside a string.
5288 * If it is then restrict the search to below this line and try again.
5289 */
5290 line = ml_get(pos->lnum);
5291 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5292 p = skip_string(p);
5293 if ((colnr_T)(p - line) <= pos->col)
5294 break;
5295 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5296 if (cur_maxcomment <= 0)
5297 {
5298 pos = NULL;
5299 break;
5300 }
5301 }
5302 return pos;
5303}
5304
5305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 * Skip to the end of a "string" and a 'c' character.
5307 * If there is no string or character, return argument unmodified.
5308 */
5309 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005310skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311{
5312 int i;
5313
5314 /*
5315 * We loop, because strings may be concatenated: "date""time".
5316 */
5317 for ( ; ; ++p)
5318 {
5319 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5320 {
5321 if (!p[1]) /* ' at end of line */
5322 break;
5323 i = 2;
5324 if (p[1] == '\\') /* '\n' or '\000' */
5325 {
5326 ++i;
5327 while (vim_isdigit(p[i - 1])) /* '\000' */
5328 ++i;
5329 }
5330 if (p[i] == '\'') /* check for trailing ' */
5331 {
5332 p += i;
5333 continue;
5334 }
5335 }
5336 else if (p[0] == '"') /* start of string */
5337 {
5338 for (++p; p[0]; ++p)
5339 {
5340 if (p[0] == '\\' && p[1] != NUL)
5341 ++p;
5342 else if (p[0] == '"') /* end of string */
5343 break;
5344 }
5345 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005346 continue; /* continue for another string */
5347 }
5348 else if (p[0] == 'R' && p[1] == '"')
5349 {
5350 /* Raw string: R"[delim](...)[delim]" */
5351 char_u *delim = p + 2;
5352 char_u *paren = vim_strchr(delim, '(');
5353
5354 if (paren != NULL)
5355 {
5356 size_t delim_len = paren - delim;
5357
5358 for (p += 3; *p; ++p)
5359 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5360 && p[delim_len + 1] == '"')
5361 {
5362 p += delim_len + 1;
5363 break;
5364 }
5365 if (p[0] == '"')
5366 continue; /* continue for another string */
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
5369 break; /* no string found */
5370 }
5371 if (!*p)
5372 --p; /* backup from NUL */
5373 return p;
5374}
5375#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5376
5377#if defined(FEAT_CINDENT) || defined(PROTO)
5378
5379/*
5380 * Do C or expression indenting on the current line.
5381 */
5382 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005383do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
5385# ifdef FEAT_EVAL
5386 if (*curbuf->b_p_inde != NUL)
5387 fixthisline(get_expr_indent);
5388 else
5389# endif
5390 fixthisline(get_c_indent);
5391}
5392
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005393/* Find result cache for cpp_baseclass */
5394typedef struct {
5395 int found;
5396 lpos_T lpos;
5397} cpp_baseclass_cache_T;
5398
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399/*
5400 * Functions for C-indenting.
5401 * Most of this originally comes from Eric Fischer.
5402 */
5403/*
5404 * Below "XXX" means that this function may unlock the current line.
5405 */
5406
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005407static char_u *cin_skipcomment(char_u *);
5408static int cin_nocode(char_u *);
5409static pos_T *find_line_comment(void);
5410static int cin_has_js_key(char_u *text);
5411static int cin_islabel_skip(char_u **);
5412static int cin_isdefault(char_u *);
5413static char_u *after_label(char_u *l);
5414static int get_indent_nolabel(linenr_T lnum);
5415static int skip_label(linenr_T, char_u **pp);
5416static int cin_first_id_amount(void);
5417static int cin_get_equal_amount(linenr_T lnum);
5418static int cin_ispreproc(char_u *);
5419static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump);
5420static int cin_iscomment(char_u *);
5421static int cin_islinecomment(char_u *);
5422static int cin_isterminated(char_u *, int, int);
5423static int cin_isinit(void);
5424static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5425static int cin_isif(char_u *);
5426static int cin_iselse(char_u *);
5427static int cin_isdo(char_u *);
5428static int cin_iswhileofdo(char_u *, linenr_T);
5429static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5430static int cin_iswhileofdo_end(int terminated);
5431static int cin_isbreak(char_u *);
5432static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5433static int get_baseclass_amount(int col);
5434static int cin_ends_in(char_u *, char_u *, char_u *);
5435static int cin_starts_with(char_u *s, char *word);
5436static int cin_skip2pos(pos_T *trypos);
5437static pos_T *find_start_brace(void);
5438static pos_T *find_match_paren(int);
5439static pos_T *find_match_char(int c, int ind_maxparen);
5440static int corr_ind_maxparen(pos_T *startpos);
5441static int find_last_paren(char_u *l, int start, int end);
5442static int find_match(int lookfor, linenr_T ourscope);
5443static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445/*
5446 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005447 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 */
5449 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005450cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 while (*s)
5453 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005454 char_u *prev_s = s;
5455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005457
5458 /* Perl/shell # comment comment continues until eol. Require a space
5459 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005460 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005461 {
5462 s += STRLEN(s);
5463 break;
5464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 if (*s != '/')
5466 break;
5467 ++s;
5468 if (*s == '/') /* slash-slash comment continues till eol */
5469 {
5470 s += STRLEN(s);
5471 break;
5472 }
5473 if (*s != '*')
5474 break;
5475 for (++s; *s; ++s) /* skip slash-star comment */
5476 if (s[0] == '*' && s[1] == '/')
5477 {
5478 s += 2;
5479 break;
5480 }
5481 }
5482 return s;
5483}
5484
5485/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005486 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 * not considered code.
5488 */
5489 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005490cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 return *cin_skipcomment(s) == NUL;
5493}
5494
5495/*
5496 * Check previous lines for a "//" line comment, skipping over blank lines.
5497 */
5498 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005499find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501 static pos_T pos;
5502 char_u *line;
5503 char_u *p;
5504
5505 pos = curwin->w_cursor;
5506 while (--pos.lnum > 0)
5507 {
5508 line = ml_get(pos.lnum);
5509 p = skipwhite(line);
5510 if (cin_islinecomment(p))
5511 {
5512 pos.col = (int)(p - line);
5513 return &pos;
5514 }
5515 if (*p != NUL)
5516 break;
5517 }
5518 return NULL;
5519}
5520
5521/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005522 * Return TRUE if "text" starts with "key:".
5523 */
5524 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005525cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005526{
5527 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005528 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005529
5530 if (*s == '\'' || *s == '"')
5531 {
5532 /* can be 'key': or "key": */
5533 quote = *s;
5534 ++s;
5535 }
5536 if (!vim_isIDc(*s)) /* need at least one ID character */
5537 return FALSE;
5538
5539 while (vim_isIDc(*s))
5540 ++s;
5541 if (*s == quote)
5542 ++s;
5543
5544 s = cin_skipcomment(s);
5545
5546 /* "::" is not a label, it's C++ */
5547 return (*s == ':' && s[1] != ':');
5548}
5549
5550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005552 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 */
5554 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005555cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 if (!vim_isIDc(**s)) /* need at least one ID character */
5558 return FALSE;
5559
5560 while (vim_isIDc(**s))
5561 (*s)++;
5562
5563 *s = cin_skipcomment(*s);
5564
5565 /* "::" is not a label, it's C++ */
5566 return (**s == ':' && *++*s != ':');
5567}
5568
5569/*
5570 * Recognize a label: "label:".
5571 * Note: curwin->w_cursor must be where we are looking for the label.
5572 */
5573 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005574cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576 char_u *s;
5577
5578 s = cin_skipcomment(ml_get_curline());
5579
5580 /*
5581 * Exclude "default" from labels, since it should be indented
5582 * like a switch label. Same for C++ scope declarations.
5583 */
5584 if (cin_isdefault(s))
5585 return FALSE;
5586 if (cin_isscopedecl(s))
5587 return FALSE;
5588
5589 if (cin_islabel_skip(&s))
5590 {
5591 /*
5592 * Only accept a label if the previous line is terminated or is a case
5593 * label.
5594 */
5595 pos_T cursor_save;
5596 pos_T *trypos;
5597 char_u *line;
5598
5599 cursor_save = curwin->w_cursor;
5600 while (curwin->w_cursor.lnum > 1)
5601 {
5602 --curwin->w_cursor.lnum;
5603
5604 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005605 * If we're in a comment or raw string now, skip to the start of
5606 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 */
5608 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005609 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 curwin->w_cursor = *trypos;
5611
5612 line = ml_get_curline();
5613 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5614 continue;
5615 if (*(line = cin_skipcomment(line)) == NUL)
5616 continue;
5617
5618 curwin->w_cursor = cursor_save;
5619 if (cin_isterminated(line, TRUE, FALSE)
5620 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005621 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 || (cin_islabel_skip(&line) && cin_nocode(line)))
5623 return TRUE;
5624 return FALSE;
5625 }
5626 curwin->w_cursor = cursor_save;
5627 return TRUE; /* label at start of file??? */
5628 }
5629 return FALSE;
5630}
5631
5632/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005633 * Recognize structure initialization and enumerations:
5634 * "[typedef] [static|public|protected|private] enum"
5635 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 */
5637 static int
5638cin_isinit(void)
5639{
5640 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005641 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
5643 s = cin_skipcomment(ml_get_curline());
5644
Bram Moolenaar75342212013-03-07 13:13:52 +01005645 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 s = cin_skipcomment(s + 7);
5647
Bram Moolenaar75342212013-03-07 13:13:52 +01005648 for (;;)
5649 {
5650 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005651
Bram Moolenaar75342212013-03-07 13:13:52 +01005652 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5653 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005654 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005655 if (cin_starts_with(s, skip[i]))
5656 {
5657 s = cin_skipcomment(s + l);
5658 l = 0;
5659 break;
5660 }
5661 }
5662 if (l != 0)
5663 break;
5664 }
5665
5666 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 return TRUE;
5668
5669 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5670 return TRUE;
5671
5672 return FALSE;
5673}
5674
5675/*
5676 * Recognize a switch label: "case .*:" or "default:".
5677 */
5678 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005679cin_iscase(
5680 char_u *s,
5681 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005684 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
5686 for (s += 4; *s; ++s)
5687 {
5688 s = cin_skipcomment(s);
5689 if (*s == ':')
5690 {
5691 if (s[1] == ':') /* skip over "::" for C++ */
5692 ++s;
5693 else
5694 return TRUE;
5695 }
5696 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005697 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5699 return FALSE; /* stop at comment */
5700 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005701 {
5702 /* JS etc. */
5703 if (strict)
5704 return FALSE; /* stop at string */
5705 else
5706 return TRUE;
5707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
5709 return FALSE;
5710 }
5711
5712 if (cin_isdefault(s))
5713 return TRUE;
5714 return FALSE;
5715}
5716
5717/*
5718 * Recognize a "default" switch label.
5719 */
5720 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005721cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 return (STRNCMP(s, "default", 7) == 0
5724 && *(s = cin_skipcomment(s + 7)) == ':'
5725 && s[1] != ':');
5726}
5727
5728/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005729 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 */
5731 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005732cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734 int i;
5735
5736 s = cin_skipcomment(s);
5737 if (STRNCMP(s, "public", 6) == 0)
5738 i = 6;
5739 else if (STRNCMP(s, "protected", 9) == 0)
5740 i = 9;
5741 else if (STRNCMP(s, "private", 7) == 0)
5742 i = 7;
5743 else
5744 return FALSE;
5745 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5746}
5747
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005748/* Maximum number of lines to search back for a "namespace" line. */
5749#define FIND_NAMESPACE_LIM 20
5750
5751/*
5752 * Recognize a "namespace" scope declaration.
5753 */
5754 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005755cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005756{
5757 char_u *p;
5758 int has_name = FALSE;
5759
5760 s = cin_skipcomment(s);
5761 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5762 {
5763 p = cin_skipcomment(skipwhite(s + 9));
5764 while (*p != NUL)
5765 {
5766 if (vim_iswhite(*p))
5767 {
5768 has_name = TRUE; /* found end of a name */
5769 p = cin_skipcomment(skipwhite(p));
5770 }
5771 else if (*p == '{')
5772 {
5773 break;
5774 }
5775 else if (vim_iswordc(*p))
5776 {
5777 if (has_name)
5778 return FALSE; /* word character after skipping past name */
5779 ++p;
5780 }
5781 else
5782 {
5783 return FALSE;
5784 }
5785 }
5786 return TRUE;
5787 }
5788 return FALSE;
5789}
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791/*
5792 * Return a pointer to the first non-empty non-comment character after a ':'.
5793 * Return NULL if not found.
5794 * case 234: a = b;
5795 * ^
5796 */
5797 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005798after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 for ( ; *l; ++l)
5801 {
5802 if (*l == ':')
5803 {
5804 if (l[1] == ':') /* skip over "::" for C++ */
5805 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005806 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 break;
5808 }
5809 else if (*l == '\'' && l[1] && l[2] == '\'')
5810 l += 2; /* skip over 'x' */
5811 }
5812 if (*l == NUL)
5813 return NULL;
5814 l = cin_skipcomment(l + 1);
5815 if (*l == NUL)
5816 return NULL;
5817 return l;
5818}
5819
5820/*
5821 * Get indent of line "lnum", skipping a label.
5822 * Return 0 if there is nothing after the label.
5823 */
5824 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005825get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 char_u *l;
5828 pos_T fp;
5829 colnr_T col;
5830 char_u *p;
5831
5832 l = ml_get(lnum);
5833 p = after_label(l);
5834 if (p == NULL)
5835 return 0;
5836
5837 fp.col = (colnr_T)(p - l);
5838 fp.lnum = lnum;
5839 getvcol(curwin, &fp, &col, NULL, NULL);
5840 return (int)col;
5841}
5842
5843/*
5844 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005845 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 * label: if (asdf && asdfasdf)
5847 * ^
5848 */
5849 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005850skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 char_u *l;
5853 int amount;
5854 pos_T cursor_save;
5855
5856 cursor_save = curwin->w_cursor;
5857 curwin->w_cursor.lnum = lnum;
5858 l = ml_get_curline();
5859 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005860 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
5862 amount = get_indent_nolabel(lnum);
5863 l = after_label(ml_get_curline());
5864 if (l == NULL) /* just in case */
5865 l = ml_get_curline();
5866 }
5867 else
5868 {
5869 amount = get_indent();
5870 l = ml_get_curline();
5871 }
5872 *pp = l;
5873
5874 curwin->w_cursor = cursor_save;
5875 return amount;
5876}
5877
5878/*
5879 * Return the indent of the first variable name after a type in a declaration.
5880 * int a, indent of "a"
5881 * static struct foo b, indent of "b"
5882 * enum bla c, indent of "c"
5883 * Returns zero when it doesn't look like a declaration.
5884 */
5885 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005886cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887{
5888 char_u *line, *p, *s;
5889 int len;
5890 pos_T fp;
5891 colnr_T col;
5892
5893 line = ml_get_curline();
5894 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005895 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5897 {
5898 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005899 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 }
5901 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5902 p = skipwhite(p + 6);
5903 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5904 p = skipwhite(p + 4);
5905 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5906 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5907 {
5908 s = skipwhite(p + len);
5909 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5910 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5911 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5912 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5913 p = s;
5914 }
5915 for (len = 0; vim_isIDc(p[len]); ++len)
5916 ;
5917 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5918 return 0;
5919
5920 p = skipwhite(p + len);
5921 fp.lnum = curwin->w_cursor.lnum;
5922 fp.col = (colnr_T)(p - line);
5923 getvcol(curwin, &fp, &col, NULL, NULL);
5924 return (int)col;
5925}
5926
5927/*
5928 * Return the indent of the first non-blank after an equal sign.
5929 * char *foo = "here";
5930 * Return zero if no (useful) equal sign found.
5931 * Return -1 if the line above "lnum" ends in a backslash.
5932 * foo = "asdf\
5933 * asdf\
5934 * here";
5935 */
5936 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005937cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 char_u *line;
5940 char_u *s;
5941 colnr_T col;
5942 pos_T fp;
5943
5944 if (lnum > 1)
5945 {
5946 line = ml_get(lnum - 1);
5947 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5948 return -1;
5949 }
5950
5951 line = s = ml_get(lnum);
5952 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5953 {
5954 if (cin_iscomment(s)) /* ignore comments */
5955 s = cin_skipcomment(s);
5956 else
5957 ++s;
5958 }
5959 if (*s != '=')
5960 return 0;
5961
5962 s = skipwhite(s + 1);
5963 if (cin_nocode(s))
5964 return 0;
5965
5966 if (*s == '"') /* nice alignment for continued strings */
5967 ++s;
5968
5969 fp.lnum = lnum;
5970 fp.col = (colnr_T)(s - line);
5971 getvcol(curwin, &fp, &col, NULL, NULL);
5972 return (int)col;
5973}
5974
5975/*
5976 * Recognize a preprocessor statement: Any line that starts with '#'.
5977 */
5978 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005979cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005981 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 return TRUE;
5983 return FALSE;
5984}
5985
5986/*
5987 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5988 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5989 * start and return the line in "*pp".
5990 */
5991 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005992cin_ispreproc_cont(char_u **pp, linenr_T *lnump)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 char_u *line = *pp;
5995 linenr_T lnum = *lnump;
5996 int retval = FALSE;
5997
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005998 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 {
6000 if (cin_ispreproc(line))
6001 {
6002 retval = TRUE;
6003 *lnump = lnum;
6004 break;
6005 }
6006 if (lnum == 1)
6007 break;
6008 line = ml_get(--lnum);
6009 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6010 break;
6011 }
6012
6013 if (lnum != *lnump)
6014 *pp = ml_get(*lnump);
6015 return retval;
6016}
6017
6018/*
6019 * Recognize the start of a C or C++ comment.
6020 */
6021 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006022cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6025}
6026
6027/*
6028 * Recognize the start of a "//" comment.
6029 */
6030 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006031cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033 return (p[0] == '/' && p[1] == '/');
6034}
6035
6036/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006037 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6038 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006040 * If a line begins with an "else", only consider it terminated if no unmatched
6041 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 * Return the character terminating the line (ending char's have precedence if
6043 * both apply in order to determine initializations).
6044 */
6045 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006046cin_isterminated(
6047 char_u *s,
6048 int incl_open, /* include '{' at the end as terminator */
6049 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006051 char_u found_start = 0;
6052 unsigned n_open = 0;
6053 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
6055 s = cin_skipcomment(s);
6056
6057 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6058 found_start = *s;
6059
Bram Moolenaar496f9512011-05-19 16:35:09 +02006060 if (!found_start)
6061 is_else = cin_iselse(s);
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 while (*s)
6064 {
6065 /* skip over comments, "" strings and 'c'haracters */
6066 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006067 if (*s == '}' && n_open > 0)
6068 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006069 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006070 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 && cin_nocode(s + 1))
6072 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006073 else if (*s == '{')
6074 {
6075 if (incl_open && cin_nocode(s + 1))
6076 return *s;
6077 else
6078 ++n_open;
6079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
6081 if (*s)
6082 s++;
6083 }
6084 return found_start;
6085}
6086
6087/*
6088 * Recognize the basic picture of a function declaration -- it needs to
6089 * have an open paren somewhere and a close paren at the end of the line and
6090 * no semicolons anywhere.
6091 * When a line ends in a comma we continue looking in the next line.
6092 * "sp" points to a string with the line. When looking at other lines it must
6093 * be restored to the line. When it's NULL fetch lines here.
6094 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006095 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 */
6097 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006098cin_isfuncdecl(
6099 char_u **sp,
6100 linenr_T first_lnum,
6101 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 char_u *s;
6104 linenr_T lnum = first_lnum;
6105 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006106 pos_T *trypos;
6107 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109 if (sp == NULL)
6110 s = ml_get(lnum);
6111 else
6112 s = *sp;
6113
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006114 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006115 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006116 {
6117 lnum = trypos->lnum;
6118 if (lnum < min_lnum)
6119 return FALSE;
6120
6121 s = ml_get(lnum);
6122 }
6123
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006124 /* Ignore line starting with #. */
6125 if (cin_ispreproc(s))
6126 return FALSE;
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6129 {
6130 if (cin_iscomment(s)) /* ignore comments */
6131 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006132 else if (*s == ':')
6133 {
6134 if (*(s + 1) == ':')
6135 s += 2;
6136 else
6137 /* To avoid a mistake in the following situation:
6138 * A::A(int a, int b)
6139 * : a(0) // <--not a function decl
6140 * , b(0)
6141 * {...
6142 */
6143 return FALSE;
6144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 else
6146 ++s;
6147 }
6148 if (*s != '(')
6149 return FALSE; /* ';', ' or " before any () or no '(' */
6150
6151 while (*s && *s != ';' && *s != '\'' && *s != '"')
6152 {
6153 if (*s == ')' && cin_nocode(s + 1))
6154 {
6155 /* ')' at the end: may have found a match
6156 * Check for he previous line not to end in a backslash:
6157 * #if defined(x) && \
6158 * defined(y)
6159 */
6160 lnum = first_lnum - 1;
6161 s = ml_get(lnum);
6162 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6163 retval = TRUE;
6164 goto done;
6165 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006166 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006168 int comma = (*s == ',');
6169
6170 /* ',' at the end: continue looking in the next line.
6171 * At the end: check for ',' in the next line, for this style:
6172 * func(arg1
6173 * , arg2) */
6174 for (;;)
6175 {
6176 if (lnum >= curbuf->b_ml.ml_line_count)
6177 break;
6178 s = ml_get(++lnum);
6179 if (!cin_ispreproc(s))
6180 break;
6181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (lnum >= curbuf->b_ml.ml_line_count)
6183 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006184 /* Require a comma at end of the line or a comma or ')' at the
6185 * start of next line. */
6186 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006187 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006188 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006189 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 }
6191 else if (cin_iscomment(s)) /* ignore comments */
6192 s = cin_skipcomment(s);
6193 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006196 just_started = FALSE;
6197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
6199
6200done:
6201 if (lnum != first_lnum && sp != NULL)
6202 *sp = ml_get(first_lnum);
6203
6204 return retval;
6205}
6206
6207 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006208cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006210 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211}
6212
6213 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006214cin_iselse(
6215 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 if (*p == '}') /* accept "} else" */
6218 p = cin_skipcomment(p + 1);
6219 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6220}
6221
6222 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006223cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
6225 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6226}
6227
6228/*
6229 * Check if this is a "while" that should have a matching "do".
6230 * We only accept a "while (condition) ;", with only white space between the
6231 * ')' and ';'. The condition may be spread over several lines.
6232 */
6233 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 pos_T cursor_save;
6237 pos_T *trypos;
6238 int retval = FALSE;
6239
6240 p = cin_skipcomment(p);
6241 if (*p == '}') /* accept "} while (cond);" */
6242 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006243 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
6245 cursor_save = curwin->w_cursor;
6246 curwin->w_cursor.lnum = lnum;
6247 curwin->w_cursor.col = 0;
6248 p = ml_get_curline();
6249 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6250 {
6251 ++p;
6252 ++curwin->w_cursor.col;
6253 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006254 if ((trypos = findmatchlimit(NULL, 0, 0,
6255 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6257 retval = TRUE;
6258 curwin->w_cursor = cursor_save;
6259 }
6260 return retval;
6261}
6262
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006263/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006264 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006265 * Return 0 if there is none.
6266 * Otherwise return !0 and update "*poffset" to point to the place where the
6267 * string was found.
6268 */
6269 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006270cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006271{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006272 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006273
6274 if (offset-- < 2)
6275 return 0;
6276 while (offset > 2 && vim_iswhite(line[offset]))
6277 --offset;
6278
6279 offset -= 1;
6280 if (!STRNCMP(line + offset, "if", 2))
6281 goto probablyFound;
6282
6283 if (offset >= 1)
6284 {
6285 offset -= 1;
6286 if (!STRNCMP(line + offset, "for", 3))
6287 goto probablyFound;
6288
6289 if (offset >= 2)
6290 {
6291 offset -= 2;
6292 if (!STRNCMP(line + offset, "while", 5))
6293 goto probablyFound;
6294 }
6295 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006296 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006297
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006298probablyFound:
6299 if (!offset || !vim_isIDc(line[offset - 1]))
6300 {
6301 *poffset = offset;
6302 return 1;
6303 }
6304 return 0;
6305}
6306
6307/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006308 * Return TRUE if we are at the end of a do-while.
6309 * do
6310 * nothing;
6311 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006312 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006313 * Adjust the cursor to the line with "while".
6314 */
6315 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006316cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006317{
6318 char_u *line;
6319 char_u *p;
6320 char_u *s;
6321 pos_T *trypos;
6322 int i;
6323
6324 if (terminated != ';') /* there must be a ';' at the end */
6325 return FALSE;
6326
6327 p = line = ml_get_curline();
6328 while (*p != NUL)
6329 {
6330 p = cin_skipcomment(p);
6331 if (*p == ')')
6332 {
6333 s = skipwhite(p + 1);
6334 if (*s == ';' && cin_nocode(s + 1))
6335 {
6336 /* Found ");" at end of the line, now check there is "while"
6337 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006338 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006339 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006340 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006341 if (trypos != NULL)
6342 {
6343 s = cin_skipcomment(ml_get(trypos->lnum));
6344 if (*s == '}') /* accept "} while (cond);" */
6345 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006346 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006347 {
6348 curwin->w_cursor.lnum = trypos->lnum;
6349 return TRUE;
6350 }
6351 }
6352
6353 /* Searching may have made "line" invalid, get it again. */
6354 line = ml_get_curline();
6355 p = line + i;
6356 }
6357 }
6358 if (*p != NUL)
6359 ++p;
6360 }
6361 return FALSE;
6362}
6363
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006365cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
6367 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6368}
6369
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006370/*
6371 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 * constructor-initialization. eg:
6373 *
6374 * class MyClass :
6375 * baseClass <-- here
6376 * class MyClass : public baseClass,
6377 * anotherBaseClass <-- here (should probably lineup ??)
6378 * MyClass::MyClass(...) :
6379 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006380 *
6381 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 */
6383 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006384cin_is_cpp_baseclass(
6385 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006387 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 char_u *s;
6389 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006390 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006391 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006393 if (pos->lnum <= lnum)
6394 return cached->found; /* Use the cached result */
6395
6396 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006398 s = skipwhite(line);
6399 if (*s == '#') /* skip #define FOO x ? (x) : x */
6400 return FALSE;
6401 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 if (*s == NUL)
6403 return FALSE;
6404
6405 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6406
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006407 /* Search for a line starting with '#', empty, ending in ';' or containing
6408 * '{' or '}' and start below it. This handles the following situations:
6409 * a = cond ?
6410 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006411 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006412 * func::foo()
6413 * : something
6414 * {}
6415 * Foo::Foo (int one, int two)
6416 * : something(4),
6417 * somethingelse(3)
6418 * {}
6419 */
6420 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006422 line = ml_get(lnum - 1);
6423 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006424 if (*s == '#' || *s == NUL)
6425 break;
6426 while (*s != NUL)
6427 {
6428 s = cin_skipcomment(s);
6429 if (*s == '{' || *s == '}'
6430 || (*s == ';' && cin_nocode(s + 1)))
6431 break;
6432 if (*s != NUL)
6433 ++s;
6434 }
6435 if (*s != NUL)
6436 break;
6437 --lnum;
6438 }
6439
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006440 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006441 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006442 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006443 for (;;)
6444 {
6445 if (*s == NUL)
6446 {
6447 if (lnum == curwin->w_cursor.lnum)
6448 break;
6449 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006450 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006451 s = line;
6452 }
6453 if (s == line)
6454 {
6455 /* don't recognize "case (foo):" as a baseclass */
6456 if (cin_iscase(s, FALSE))
6457 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006458 s = cin_skipcomment(line);
6459 if (*s == NUL)
6460 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006461 }
6462
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006463 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006464 s = skip_string(s) + 1;
6465 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
6467 if (s[1] == ':')
6468 {
6469 /* skip double colon. It can't be a constructor
6470 * initialization any more */
6471 lookfor_ctor_init = FALSE;
6472 s = cin_skipcomment(s + 2);
6473 }
6474 else if (lookfor_ctor_init || class_or_struct)
6475 {
6476 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006477 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 cpp_base_class = TRUE;
6479 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006480 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 s = cin_skipcomment(s + 1);
6482 }
6483 else
6484 s = cin_skipcomment(s + 1);
6485 }
6486 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6487 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6488 {
6489 class_or_struct = TRUE;
6490 lookfor_ctor_init = FALSE;
6491
6492 if (*s == 'c')
6493 s = cin_skipcomment(s + 5);
6494 else
6495 s = cin_skipcomment(s + 6);
6496 }
6497 else
6498 {
6499 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6500 {
6501 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6502 }
6503 else if (s[0] == ')')
6504 {
6505 /* Constructor-initialization is assumed if we come across
6506 * something like "):" */
6507 class_or_struct = FALSE;
6508 lookfor_ctor_init = TRUE;
6509 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006510 else if (s[0] == '?')
6511 {
6512 /* Avoid seeing '() :' after '?' as constructor init. */
6513 return FALSE;
6514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 else if (!vim_isIDc(s[0]))
6516 {
6517 /* if it is not an identifier, we are wrong */
6518 class_or_struct = FALSE;
6519 lookfor_ctor_init = FALSE;
6520 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006521 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
6523 /* it can't be a constructor-initialization any more */
6524 lookfor_ctor_init = FALSE;
6525
6526 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006527 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006528 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 }
6530
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006531 /* When the line ends in a comma don't align with it. */
6532 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006533 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 s = cin_skipcomment(s + 1);
6536 }
6537 }
6538
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006539 cached->found = cpp_base_class;
6540 if (cpp_base_class)
6541 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 return cpp_base_class;
6543}
6544
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006545 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006546get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006547{
6548 int amount;
6549 colnr_T vcol;
6550 pos_T *trypos;
6551
6552 if (col == 0)
6553 {
6554 amount = get_indent();
6555 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006556 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006557 amount = get_indent_lnum(trypos->lnum); /* XXX */
6558 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006559 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006560 }
6561 else
6562 {
6563 curwin->w_cursor.col = col;
6564 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6565 amount = (int)vcol;
6566 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006567 if (amount < curbuf->b_ind_cpp_baseclass)
6568 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006569 return amount;
6570}
6571
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572/*
6573 * Return TRUE if string "s" ends with the string "find", possibly followed by
6574 * white space and comments. Skip strings and comments.
6575 * Ignore "ignore" after "find" if it's not NULL.
6576 */
6577 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006578cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
6580 char_u *p = s;
6581 char_u *r;
6582 int len = (int)STRLEN(find);
6583
6584 while (*p != NUL)
6585 {
6586 p = cin_skipcomment(p);
6587 if (STRNCMP(p, find, len) == 0)
6588 {
6589 r = skipwhite(p + len);
6590 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6591 r = skipwhite(r + STRLEN(ignore));
6592 if (cin_nocode(r))
6593 return TRUE;
6594 }
6595 if (*p != NUL)
6596 ++p;
6597 }
6598 return FALSE;
6599}
6600
6601/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006602 * Return TRUE when "s" starts with "word" and then a non-ID character.
6603 */
6604 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006605cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006606{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006607 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006608
6609 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6610}
6611
6612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 * Skip strings, chars and comments until at or past "trypos".
6614 * Return the column found.
6615 */
6616 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006617cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619 char_u *line;
6620 char_u *p;
6621
6622 p = line = ml_get(trypos->lnum);
6623 while (*p && (colnr_T)(p - line) < trypos->col)
6624 {
6625 if (cin_iscomment(p))
6626 p = cin_skipcomment(p);
6627 else
6628 {
6629 p = skip_string(p);
6630 ++p;
6631 }
6632 }
6633 return (int)(p - line);
6634}
6635
6636/*
6637 * Find the '{' at the start of the block we are in.
6638 * Return NULL if no match found.
6639 * Ignore a '{' that is in a comment, makes indenting the next three lines
6640 * work. */
6641/* foo() */
6642/* { */
6643/* } */
6644
6645 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006646find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
6648 pos_T cursor_save;
6649 pos_T *trypos;
6650 pos_T *pos;
6651 static pos_T pos_copy;
6652
6653 cursor_save = curwin->w_cursor;
6654 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6655 {
6656 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6657 trypos = &pos_copy;
6658 curwin->w_cursor = *trypos;
6659 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006660 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006662 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 break;
6664 if (pos != NULL)
6665 curwin->w_cursor.lnum = pos->lnum;
6666 }
6667 curwin->w_cursor = cursor_save;
6668 return trypos;
6669}
6670
6671/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006672 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006673 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 */
6675 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006676find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006678 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006679}
6680
6681 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006682find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006683{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 pos_T cursor_save;
6685 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006686 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006687 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006690 ind_maxp_wk = ind_maxparen;
6691retry:
6692 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {
6694 /* check if the ( is in a // comment */
6695 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006696 {
6697 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6698 if (ind_maxp_wk > 0)
6699 {
6700 curwin->w_cursor = *trypos;
6701 curwin->w_cursor.col = 0; /* XXX */
6702 goto retry;
6703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 else
6707 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006708 pos_T *trypos_wk;
6709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6711 trypos = &pos_copy;
6712 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006713 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006714 {
6715 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6716 - trypos_wk->lnum);
6717 if (ind_maxp_wk > 0)
6718 {
6719 curwin->w_cursor = *trypos_wk;
6720 goto retry;
6721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
6725 }
6726 curwin->w_cursor = cursor_save;
6727 return trypos;
6728}
6729
6730/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006731 * Find the matching '(', ignoring it if it is in a comment or before an
6732 * unmatched {.
6733 * Return NULL if no match found.
6734 */
6735 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006736find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006737{
6738 pos_T *trypos = find_match_paren(ind_maxparen);
6739
6740 if (trypos != NULL)
6741 {
6742 pos_T *tryposBrace = find_start_brace();
6743
6744 /* If both an unmatched '(' and '{' is found. Ignore the '('
6745 * position if the '{' is further down. */
6746 if (tryposBrace != NULL
6747 && (trypos->lnum != tryposBrace->lnum
6748 ? trypos->lnum < tryposBrace->lnum
6749 : trypos->col < tryposBrace->col))
6750 trypos = NULL;
6751 }
6752 return trypos;
6753}
6754
6755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 * Return ind_maxparen corrected for the difference in line number between the
6757 * cursor position and "startpos". This makes sure that searching for a
6758 * matching paren above the cursor line doesn't find a match because of
6759 * looking a few lines further.
6760 */
6761 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006762corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
6764 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6765
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006766 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6767 return curbuf->b_ind_maxparen - (int)n;
6768 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769}
6770
6771/*
6772 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006773 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 */
6775 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006776find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 int i;
6779 int retval = FALSE;
6780 int open_count = 0;
6781
6782 curwin->w_cursor.col = 0; /* default is start of line */
6783
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006784 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {
6786 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6787 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6788 if (l[i] == start)
6789 ++open_count;
6790 else if (l[i] == end)
6791 {
6792 if (open_count > 0)
6793 --open_count;
6794 else
6795 {
6796 curwin->w_cursor.col = i;
6797 retval = TRUE;
6798 }
6799 }
6800 }
6801 return retval;
6802}
6803
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006804/*
6805 * Parse 'cinoptions' and set the values in "curbuf".
6806 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6807 */
6808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006809parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006810{
6811 char_u *p;
6812 char_u *l;
6813 char_u *digits;
6814 int n;
6815 int divider;
6816 int fraction = 0;
6817 int sw = (int)get_sw_value(buf);
6818
6819 /*
6820 * Set the default values.
6821 */
6822 /* Spaces from a block's opening brace the prevailing indent for that
6823 * block should be. */
6824 buf->b_ind_level = sw;
6825
6826 /* Spaces from the edge of the line an open brace that's at the end of a
6827 * line is imagined to be. */
6828 buf->b_ind_open_imag = 0;
6829
6830 /* Spaces from the prevailing indent for a line that is not preceded by
6831 * an opening brace. */
6832 buf->b_ind_no_brace = 0;
6833
6834 /* Column where the first { of a function should be located }. */
6835 buf->b_ind_first_open = 0;
6836
6837 /* Spaces from the prevailing indent a leftmost open brace should be
6838 * located. */
6839 buf->b_ind_open_extra = 0;
6840
6841 /* Spaces from the matching open brace (real location for one at the left
6842 * edge; imaginary location from one that ends a line) the matching close
6843 * brace should be located. */
6844 buf->b_ind_close_extra = 0;
6845
6846 /* Spaces from the edge of the line an open brace sitting in the leftmost
6847 * column is imagined to be. */
6848 buf->b_ind_open_left_imag = 0;
6849
6850 /* Spaces jump labels should be shifted to the left if N is non-negative,
6851 * otherwise the jump label will be put to column 1. */
6852 buf->b_ind_jump_label = -1;
6853
6854 /* Spaces from the switch() indent a "case xx" label should be located. */
6855 buf->b_ind_case = sw;
6856
6857 /* Spaces from the "case xx:" code after a switch() should be located. */
6858 buf->b_ind_case_code = sw;
6859
6860 /* Lineup break at end of case in switch() with case label. */
6861 buf->b_ind_case_break = 0;
6862
6863 /* Spaces from the class declaration indent a scope declaration label
6864 * should be located. */
6865 buf->b_ind_scopedecl = sw;
6866
6867 /* Spaces from the scope declaration label code should be located. */
6868 buf->b_ind_scopedecl_code = sw;
6869
6870 /* Amount K&R-style parameters should be indented. */
6871 buf->b_ind_param = sw;
6872
6873 /* Amount a function type spec should be indented. */
6874 buf->b_ind_func_type = sw;
6875
6876 /* Amount a cpp base class declaration or constructor initialization
6877 * should be indented. */
6878 buf->b_ind_cpp_baseclass = sw;
6879
6880 /* additional spaces beyond the prevailing indent a continuation line
6881 * should be located. */
6882 buf->b_ind_continuation = sw;
6883
6884 /* Spaces from the indent of the line with an unclosed parentheses. */
6885 buf->b_ind_unclosed = sw * 2;
6886
6887 /* Spaces from the indent of the line with an unclosed parentheses, which
6888 * itself is also unclosed. */
6889 buf->b_ind_unclosed2 = sw;
6890
6891 /* Suppress ignoring spaces from the indent of a line starting with an
6892 * unclosed parentheses. */
6893 buf->b_ind_unclosed_noignore = 0;
6894
6895 /* If the opening paren is the last nonwhite character on the line, and
6896 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6897 * context (for very long lines). */
6898 buf->b_ind_unclosed_wrapped = 0;
6899
6900 /* Suppress ignoring white space when lining up with the character after
6901 * an unclosed parentheses. */
6902 buf->b_ind_unclosed_whiteok = 0;
6903
6904 /* Indent a closing parentheses under the line start of the matching
6905 * opening parentheses. */
6906 buf->b_ind_matching_paren = 0;
6907
6908 /* Indent a closing parentheses under the previous line. */
6909 buf->b_ind_paren_prev = 0;
6910
6911 /* Extra indent for comments. */
6912 buf->b_ind_comment = 0;
6913
6914 /* Spaces from the comment opener when there is nothing after it. */
6915 buf->b_ind_in_comment = 3;
6916
6917 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6918 * after the comment opener. */
6919 buf->b_ind_in_comment2 = 0;
6920
6921 /* Max lines to search for an open paren. */
6922 buf->b_ind_maxparen = 20;
6923
6924 /* Max lines to search for an open comment. */
6925 buf->b_ind_maxcomment = 70;
6926
6927 /* Handle braces for java code. */
6928 buf->b_ind_java = 0;
6929
6930 /* Not to confuse JS object properties with labels. */
6931 buf->b_ind_js = 0;
6932
6933 /* Handle blocked cases correctly. */
6934 buf->b_ind_keep_case_label = 0;
6935
6936 /* Handle C++ namespace. */
6937 buf->b_ind_cpp_namespace = 0;
6938
6939 /* Handle continuation lines containing conditions of if(), for() and
6940 * while(). */
6941 buf->b_ind_if_for_while = 0;
6942
6943 for (p = buf->b_p_cino; *p; )
6944 {
6945 l = p++;
6946 if (*p == '-')
6947 ++p;
6948 digits = p; /* remember where the digits start */
6949 n = getdigits(&p);
6950 divider = 0;
6951 if (*p == '.') /* ".5s" means a fraction */
6952 {
6953 fraction = atol((char *)++p);
6954 while (VIM_ISDIGIT(*p))
6955 {
6956 ++p;
6957 if (divider)
6958 divider *= 10;
6959 else
6960 divider = 10;
6961 }
6962 }
6963 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6964 {
6965 if (p == digits)
6966 n = sw; /* just "s" is one 'shiftwidth' */
6967 else
6968 {
6969 n *= sw;
6970 if (divider)
6971 n += (sw * fraction + divider / 2) / divider;
6972 }
6973 ++p;
6974 }
6975 if (l[1] == '-')
6976 n = -n;
6977
6978 /* When adding an entry here, also update the default 'cinoptions' in
6979 * doc/indent.txt, and add explanation for it! */
6980 switch (*l)
6981 {
6982 case '>': buf->b_ind_level = n; break;
6983 case 'e': buf->b_ind_open_imag = n; break;
6984 case 'n': buf->b_ind_no_brace = n; break;
6985 case 'f': buf->b_ind_first_open = n; break;
6986 case '{': buf->b_ind_open_extra = n; break;
6987 case '}': buf->b_ind_close_extra = n; break;
6988 case '^': buf->b_ind_open_left_imag = n; break;
6989 case 'L': buf->b_ind_jump_label = n; break;
6990 case ':': buf->b_ind_case = n; break;
6991 case '=': buf->b_ind_case_code = n; break;
6992 case 'b': buf->b_ind_case_break = n; break;
6993 case 'p': buf->b_ind_param = n; break;
6994 case 't': buf->b_ind_func_type = n; break;
6995 case '/': buf->b_ind_comment = n; break;
6996 case 'c': buf->b_ind_in_comment = n; break;
6997 case 'C': buf->b_ind_in_comment2 = n; break;
6998 case 'i': buf->b_ind_cpp_baseclass = n; break;
6999 case '+': buf->b_ind_continuation = n; break;
7000 case '(': buf->b_ind_unclosed = n; break;
7001 case 'u': buf->b_ind_unclosed2 = n; break;
7002 case 'U': buf->b_ind_unclosed_noignore = n; break;
7003 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7004 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7005 case 'm': buf->b_ind_matching_paren = n; break;
7006 case 'M': buf->b_ind_paren_prev = n; break;
7007 case ')': buf->b_ind_maxparen = n; break;
7008 case '*': buf->b_ind_maxcomment = n; break;
7009 case 'g': buf->b_ind_scopedecl = n; break;
7010 case 'h': buf->b_ind_scopedecl_code = n; break;
7011 case 'j': buf->b_ind_java = n; break;
7012 case 'J': buf->b_ind_js = n; break;
7013 case 'l': buf->b_ind_keep_case_label = n; break;
7014 case '#': buf->b_ind_hash_comment = n; break;
7015 case 'N': buf->b_ind_cpp_namespace = n; break;
7016 case 'k': buf->b_ind_if_for_while = n; break;
7017 }
7018 if (*p == ',')
7019 ++p;
7020 }
7021}
7022
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007023/*
7024 * Return the desired indent for C code.
7025 * Return -1 if the indent should be left alone (inside a raw string).
7026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007028get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 pos_T cur_curpos;
7031 int amount;
7032 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007033 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 colnr_T col;
7035 char_u *theline;
7036 char_u *linecopy;
7037 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007038 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007040 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 pos_T our_paren_pos;
7042 char_u *start;
7043 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007044#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045#define BRACE_AT_START 2 /* '{' is at start of line */
7046#define BRACE_AT_END 3 /* '{' is at end of line */
7047 linenr_T ourscope;
7048 char_u *l;
7049 char_u *look;
7050 char_u terminated;
7051 int lookfor;
7052#define LOOKFOR_INITIAL 0
7053#define LOOKFOR_IF 1
7054#define LOOKFOR_DO 2
7055#define LOOKFOR_CASE 3
7056#define LOOKFOR_ANY 4
7057#define LOOKFOR_TERM 5
7058#define LOOKFOR_UNTERM 6
7059#define LOOKFOR_SCOPEDECL 7
7060#define LOOKFOR_NOBREAK 8
7061#define LOOKFOR_CPP_BASECLASS 9
7062#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007063#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007064#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
7066 int whilelevel;
7067 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 int n;
7069 int iscase;
7070 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007071 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007073 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007074 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007075 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007076 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007078 /* make a copy, value is changed below */
7079 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081 /* remember where the cursor was when we started */
7082 cur_curpos = curwin->w_cursor;
7083
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007084 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007085 if (cur_curpos.lnum == 1)
7086 return 0;
7087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 /* Get a copy of the current contents of the line.
7089 * This is required, because only the most recent line obtained with
7090 * ml_get is valid! */
7091 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7092 if (linecopy == NULL)
7093 return 0;
7094
7095 /*
7096 * In insert mode and the cursor is on a ')' truncate the line at the
7097 * cursor position. We don't want to line up with the matching '(' when
7098 * inserting new stuff.
7099 * For unknown reasons the cursor might be past the end of the line, thus
7100 * check for that.
7101 */
7102 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007103 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 && linecopy[curwin->w_cursor.col] == ')')
7105 linecopy[curwin->w_cursor.col] = NUL;
7106
7107 theline = skipwhite(linecopy);
7108
7109 /* move the cursor to the start of the line */
7110
7111 curwin->w_cursor.col = 0;
7112
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007113 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007114
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007116 * If we are inside a raw string don't change the indent.
7117 * Ignore a raw string inside a comment.
7118 */
7119 comment_pos = ind_find_start_comment();
7120 if (comment_pos != NULL)
7121 {
7122 /* findmatchlimit() static pos is overwritten, make a copy */
7123 tryposCopy = *comment_pos;
7124 comment_pos = &tryposCopy;
7125 }
7126 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7127 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7128 {
7129 amount = -1;
7130 goto laterend;
7131 }
7132
7133 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 * #defines and so on always go at the left when included in 'cinkeys'.
7135 */
7136 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007137 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007138 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007139 goto theend;
7140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141
7142 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007143 * Is it a non-case label? Then that goes at the left margin too unless:
7144 * - JS flag is set.
7145 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007147 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007148 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {
7150 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007151 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
7153
7154 /*
7155 * If we're inside a "//" comment and there is a "//" comment in a
7156 * previous line, lineup with that one.
7157 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007158 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 && (trypos = find_line_comment()) != NULL) /* XXX */
7160 {
7161 /* find how indented the line beginning the comment is */
7162 getvcol(curwin, trypos, &col, NULL, NULL);
7163 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007164 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 }
7166
7167 /*
7168 * If we're inside a comment and not looking at the start of the
7169 * comment, try using the 'comments' option.
7170 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007171 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {
7173 int lead_start_len = 2;
7174 int lead_middle_len = 1;
7175 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7176 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7177 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7178 char_u *p;
7179 int start_align = 0;
7180 int start_off = 0;
7181 int done = FALSE;
7182
7183 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007184 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007186 *lead_start = NUL;
7187 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
7189 p = curbuf->b_p_com;
7190 while (*p != NUL)
7191 {
7192 int align = 0;
7193 int off = 0;
7194 int what = 0;
7195
7196 while (*p != NUL && *p != ':')
7197 {
7198 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7199 what = *p++;
7200 else if (*p == COM_LEFT || *p == COM_RIGHT)
7201 align = *p++;
7202 else if (VIM_ISDIGIT(*p) || *p == '-')
7203 off = getdigits(&p);
7204 else
7205 ++p;
7206 }
7207
7208 if (*p == ':')
7209 ++p;
7210 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7211 if (what == COM_START)
7212 {
7213 STRCPY(lead_start, lead_end);
7214 lead_start_len = (int)STRLEN(lead_start);
7215 start_off = off;
7216 start_align = align;
7217 }
7218 else if (what == COM_MIDDLE)
7219 {
7220 STRCPY(lead_middle, lead_end);
7221 lead_middle_len = (int)STRLEN(lead_middle);
7222 }
7223 else if (what == COM_END)
7224 {
7225 /* If our line starts with the middle comment string, line it
7226 * up with the comment opener per the 'comments' option. */
7227 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7228 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7229 {
7230 done = TRUE;
7231 if (curwin->w_cursor.lnum > 1)
7232 {
7233 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007234 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 * the middle comment string matches in the previous
7236 * line, use the indent of that line. XXX */
7237 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7238 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7239 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7240 else if (STRNCMP(look, lead_middle,
7241 lead_middle_len) == 0)
7242 {
7243 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7244 break;
7245 }
7246 /* If the start comment string doesn't match with the
7247 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007248 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 lead_start, lead_start_len) != 0)
7250 continue;
7251 }
7252 if (start_off != 0)
7253 amount += start_off;
7254 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007255 amount += vim_strsize(lead_start)
7256 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 break;
7258 }
7259
7260 /* If our line starts with the end comment string, line it up
7261 * with the middle comment */
7262 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7263 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7264 {
7265 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7266 /* XXX */
7267 if (off != 0)
7268 amount += off;
7269 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007270 amount += vim_strsize(lead_start)
7271 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 done = TRUE;
7273 break;
7274 }
7275 }
7276 }
7277
7278 /* If our line starts with an asterisk, line up with the
7279 * asterisk in the comment opener; otherwise, line up
7280 * with the first character of the comment text.
7281 */
7282 if (done)
7283 ;
7284 else if (theline[0] == '*')
7285 amount += 1;
7286 else
7287 {
7288 /*
7289 * If we are more than one line away from the comment opener, take
7290 * the indent of the previous non-empty line. If 'cino' has "CO"
7291 * and we are just below the comment opener and there are any
7292 * white characters after it line up with the text after it;
7293 * otherwise, add the amount specified by "c" in 'cino'
7294 */
7295 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007296 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
7298 if (linewhite(lnum)) /* skip blank lines */
7299 continue;
7300 amount = get_indent_lnum(lnum); /* XXX */
7301 break;
7302 }
7303 if (amount == -1) /* use the comment opener */
7304 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007305 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007307 start = ml_get(comment_pos->lnum);
7308 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007310 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007312 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007314 if (curbuf->b_ind_in_comment2 || *look == NUL)
7315 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
7317 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007318 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
7320
7321 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007322 * Are we looking at a ']' that has a match?
7323 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007324 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007325 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7326 {
7327 /* align with the line containing the '['. */
7328 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007329 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007330 }
7331
7332 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 * Are we inside parentheses or braces?
7334 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007335 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007336 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007337 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 || trypos != NULL)
7339 {
7340 if (trypos != NULL && tryposBrace != NULL)
7341 {
7342 /* Both an unmatched '(' and '{' is found. Use the one which is
7343 * closer to the current cursor position, set the other to NULL. */
7344 if (trypos->lnum != tryposBrace->lnum
7345 ? trypos->lnum < tryposBrace->lnum
7346 : trypos->col < tryposBrace->col)
7347 trypos = NULL;
7348 else
7349 tryposBrace = NULL;
7350 }
7351
7352 if (trypos != NULL)
7353 {
7354 /*
7355 * If the matching paren is more than one line away, use the indent of
7356 * a previous non-empty line that matches the same paren.
7357 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007358 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007360 /* Line up with the start of the matching paren line. */
7361 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7362 }
7363 else
7364 {
7365 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007366 our_paren_pos = *trypos;
7367 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007369 l = skipwhite(ml_get(lnum));
7370 if (cin_nocode(l)) /* skip comment lines */
7371 continue;
7372 if (cin_ispreproc_cont(&l, &lnum))
7373 continue; /* ignore #define, #if, etc. */
7374 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007376 /* Skip a comment or raw string. XXX */
7377 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007378 {
7379 lnum = trypos->lnum + 1;
7380 continue;
7381 }
7382
7383 /* XXX */
7384 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007385 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007386 && trypos->lnum == our_paren_pos.lnum
7387 && trypos->col == our_paren_pos.col)
7388 {
7389 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007391 if (theline[0] == ')')
7392 {
7393 if (our_paren_pos.lnum != lnum
7394 && cur_amount > amount)
7395 cur_amount = amount;
7396 amount = -1;
7397 }
7398 break;
7399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 }
7401 }
7402
7403 /*
7404 * Line up with line where the matching paren is. XXX
7405 * If the line starts with a '(' or the indent for unclosed
7406 * parentheses is zero, line up with the unclosed parentheses.
7407 */
7408 if (amount == -1)
7409 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007410 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007411 int is_if_for_while = 0;
7412
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007413 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007414 {
7415 /* Look for the outermost opening parenthesis on this line
7416 * and check whether it belongs to an "if", "for" or "while". */
7417
7418 pos_T cursor_save = curwin->w_cursor;
7419 pos_T outermost;
7420 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007421
7422 trypos = &our_paren_pos;
7423 do {
7424 outermost = *trypos;
7425 curwin->w_cursor.lnum = outermost.lnum;
7426 curwin->w_cursor.col = outermost.col;
7427
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007428 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007429 } while (trypos && trypos->lnum == outermost.lnum);
7430
7431 curwin->w_cursor = cursor_save;
7432
7433 line = ml_get(outermost.lnum);
7434
7435 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007436 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007437 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007438
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007439 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007440 look = skipwhite(look);
7441 if (*look == '(')
7442 {
7443 linenr_T save_lnum = curwin->w_cursor.lnum;
7444 char_u *line;
7445 int look_col;
7446
7447 /* Ignore a '(' in front of the line that has a match before
7448 * our matching '('. */
7449 curwin->w_cursor.lnum = our_paren_pos.lnum;
7450 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007451 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007452 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007453 if ((trypos = findmatchlimit(NULL, ')', 0,
7454 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007455 != NULL
7456 && trypos->lnum == our_paren_pos.lnum
7457 && trypos->col < our_paren_pos.col)
7458 ignore_paren_col = trypos->col + 1;
7459
7460 curwin->w_cursor.lnum = save_lnum;
7461 look = ml_get(our_paren_pos.lnum) + look_col;
7462 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007463 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7464 && is_if_for_while == 0)
7465 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007466 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
7468 /*
7469 * If we're looking at a close paren, line up right there;
7470 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007471 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 * the last nonwhite character of the line, use either the
7473 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007474 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 * lines).
7476 */
7477 if (theline[0] != ')')
7478 {
7479 cur_amount = MAXCOL;
7480 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007481 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 && cin_ends_in(l, (char_u *)"(", NULL))
7483 {
7484 /* look for opening unmatched paren, indent one level
7485 * for each additional level */
7486 n = 1;
7487 for (col = 0; col < our_paren_pos.col; ++col)
7488 {
7489 switch (l[col])
7490 {
7491 case '(':
7492 case '{': ++n;
7493 break;
7494
7495 case ')':
7496 case '}': if (n > 1)
7497 --n;
7498 break;
7499 }
7500 }
7501
7502 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007503 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007505 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 our_paren_pos.col++;
7507 else
7508 {
7509 col = our_paren_pos.col + 1;
7510 while (vim_iswhite(l[col]))
7511 col++;
7512 if (l[col] != NUL) /* In case of trailing space */
7513 our_paren_pos.col = col;
7514 else
7515 our_paren_pos.col++;
7516 }
7517 }
7518
7519 /*
7520 * Find how indented the paren is, or the character after it
7521 * if we did the above "if".
7522 */
7523 if (our_paren_pos.col > 0)
7524 {
7525 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7526 if (cur_amount > (int)col)
7527 cur_amount = col;
7528 }
7529 }
7530
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007531 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 {
7533 /* Line up with the start of the matching paren line. */
7534 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007535 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7536 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007537 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 {
7539 if (cur_amount != MAXCOL)
7540 amount = cur_amount;
7541 }
7542 else
7543 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007544 /* Add b_ind_unclosed2 for each '(' before our matching one,
7545 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007547 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 {
7549 --our_paren_pos.col;
7550 switch (*ml_get_pos(&our_paren_pos))
7551 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007552 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 col = our_paren_pos.col;
7554 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007555 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 col = MAXCOL;
7557 break;
7558 }
7559 }
7560
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007561 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 * braces */
7563 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007564 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 else
7566 {
7567 curwin->w_cursor.lnum = our_paren_pos.lnum;
7568 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007569 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7570 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007571 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007573 {
7574 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007575 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007576 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007577 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 }
7580 /*
7581 * For a line starting with ')' use the minimum of the two
7582 * positions, to avoid giving it more indent than the previous
7583 * lines:
7584 * func_long_name( if (x
7585 * arg && yy
7586 * ) ^ not here ) ^ not here
7587 */
7588 if (cur_amount < amount)
7589 amount = cur_amount;
7590 }
7591 }
7592
7593 /* add extra indent for a comment */
7594 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007595 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 else
7598 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007599 /*
7600 * We are inside braces, there is a { before this line at the position
7601 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007602 * Make a copy of tryposBrace, it may point to pos_copy inside
7603 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007604 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007605 tryposCopy = *tryposBrace;
7606 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 ourscope = trypos->lnum;
7609 start = ml_get(ourscope);
7610
7611 /*
7612 * Now figure out how indented the line is in general.
7613 * If the brace was at the start of the line, we use that;
7614 * otherwise, check out the indentation of the line as
7615 * a whole and then add the "imaginary indent" to that.
7616 */
7617 look = skipwhite(start);
7618 if (*look == '{')
7619 {
7620 getvcol(curwin, trypos, &col, NULL, NULL);
7621 amount = col;
7622 if (*start == '{')
7623 start_brace = BRACE_IN_COL0;
7624 else
7625 start_brace = BRACE_AT_START;
7626 }
7627 else
7628 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007629 /* That opening brace might have been on a continuation
7630 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 curwin->w_cursor.lnum = ourscope;
7632
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007633 /* Position the cursor over the rightmost paren, so that
7634 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 lnum = ourscope;
7636 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007637 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7638 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 lnum = trypos->lnum;
7640
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007641 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 * case 1: if (asdf &&
7643 * ldfd) {
7644 * }
7645 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007646 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7647 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007649 else if (curbuf->b_ind_js)
7650 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007652 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
7654 start_brace = BRACE_AT_END;
7655 }
7656
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007657 /* For Javascript check if the line starts with "key:". */
7658 if (curbuf->b_ind_js)
7659 js_cur_has_key = cin_has_js_key(theline);
7660
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007662 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 * we want to be. otherwise, add the amount of room
7664 * that an indent is supposed to be.
7665 */
7666 if (theline[0] == '}')
7667 {
7668 /*
7669 * they may want closing braces to line up with something
7670 * other than the open brace. indulge them, if so.
7671 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007672 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 }
7674 else
7675 {
7676 /*
7677 * If we're looking at an "else", try to find an "if"
7678 * to match it with.
7679 * If we're looking at a "while", try to find a "do"
7680 * to match it with.
7681 */
7682 lookfor = LOOKFOR_INITIAL;
7683 if (cin_iselse(theline))
7684 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007685 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 lookfor = LOOKFOR_DO;
7687 if (lookfor != LOOKFOR_INITIAL)
7688 {
7689 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007690 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {
7692 amount = get_indent(); /* XXX */
7693 goto theend;
7694 }
7695 }
7696
7697 /*
7698 * We get here if we are not on an "while-of-do" or "else" (or
7699 * failed to find a matching "if").
7700 * Search backwards for something to line up with.
7701 * First set amount for when we don't find anything.
7702 */
7703
7704 /*
7705 * if the '{' is _really_ at the left margin, use the imaginary
7706 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007707 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 */
7709
7710 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7711 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007712 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007713 lookfor_cpp_namespace = TRUE;
7714 }
7715 else if (start_brace == BRACE_AT_START &&
7716 lookfor_cpp_namespace) /* '{' is at start */
7717 {
7718
7719 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721 else
7722 {
7723 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007724 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007725 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007726
7727 l = skipwhite(ml_get_curline());
7728 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007729 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 else
7732 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007733 /* Compensate for adding b_ind_open_extra later. */
7734 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 if (amount < 0)
7736 amount = 0;
7737 }
7738 }
7739
7740 lookfor_break = FALSE;
7741
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007742 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {
7744 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007745 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
7747 else if (cin_isscopedecl(theline)) /* private:, ... */
7748 {
7749 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007750 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 }
7752 else
7753 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007754 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7755 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 lookfor_break = TRUE;
7757
7758 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007759 /* b_ind_level from start of block */
7760 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
7762 scope_amount = amount;
7763 whilelevel = 0;
7764
7765 /*
7766 * Search backwards. If we find something we recognize, line up
7767 * with that.
7768 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007769 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 * the usual amount relative to the conditional
7771 * that opens the block.
7772 */
7773 curwin->w_cursor = cur_curpos;
7774 for (;;)
7775 {
7776 curwin->w_cursor.lnum--;
7777 curwin->w_cursor.col = 0;
7778
7779 /*
7780 * If we went all the way back to the start of our scope, line
7781 * up with it.
7782 */
7783 if (curwin->w_cursor.lnum <= ourscope)
7784 {
7785 /* we reached end of scope:
7786 * if looking for a enum or structure initialization
7787 * go further back:
7788 * if it is an initializer (enum xxx or xxx =), then
7789 * don't add ind_continuation, otherwise it is a variable
7790 * declaration:
7791 * int x,
7792 * here; <-- add ind_continuation
7793 */
7794 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7795 {
7796 if (curwin->w_cursor.lnum == 0
7797 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007798 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007800 /* nothing found (abuse curbuf->b_ind_maxparen as
7801 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 * initialization) */
7803 if (cont_amount > 0)
7804 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007805 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 amount += ind_continuation;
7807 break;
7808 }
7809
7810 l = ml_get_curline();
7811
7812 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007813 * If we're in a comment or raw string now, skip to
7814 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007816 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 if (trypos != NULL)
7818 {
7819 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007820 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 continue;
7822 }
7823
7824 /*
7825 * Skip preprocessor directives and blank lines.
7826 */
7827 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7828 continue;
7829
7830 if (cin_nocode(l))
7831 continue;
7832
7833 terminated = cin_isterminated(l, FALSE, TRUE);
7834
7835 /*
7836 * If we are at top level and the line looks like a
7837 * function declaration, we are done
7838 * (it's a variable declaration).
7839 */
7840 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007841 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {
7843 /* if the line is terminated with another ','
7844 * it is a continued variable initialization.
7845 * don't add extra indent.
7846 * TODO: does not work, if a function
7847 * declaration is split over multiple lines:
7848 * cin_isfuncdecl returns FALSE then.
7849 */
7850 if (terminated == ',')
7851 break;
7852
7853 /* if it es a enum declaration or an assignment,
7854 * we are done.
7855 */
7856 if (terminated != ';' && cin_isinit())
7857 break;
7858
7859 /* nothing useful found */
7860 if (terminated == 0 || terminated == '{')
7861 continue;
7862 }
7863
7864 if (terminated != ';')
7865 {
7866 /* Skip parens and braces. Position the cursor
7867 * over the rightmost paren, so that matching it
7868 * will take us back to the start of the line.
7869 */ /* XXX */
7870 trypos = NULL;
7871 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007872 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007873 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874
7875 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007876 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877
7878 if (trypos != NULL)
7879 {
7880 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007881 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 continue;
7883 }
7884 }
7885
7886 /* it's a variable declaration, add indentation
7887 * like in
7888 * int a,
7889 * b;
7890 */
7891 if (cont_amount > 0)
7892 amount = cont_amount;
7893 else
7894 amount += ind_continuation;
7895 }
7896 else if (lookfor == LOOKFOR_UNTERM)
7897 {
7898 if (cont_amount > 0)
7899 amount = cont_amount;
7900 else
7901 amount += ind_continuation;
7902 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007903 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007904 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007905 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007906 && lookfor != LOOKFOR_CPP_BASECLASS
7907 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007908 {
7909 amount = scope_amount;
7910 if (theline[0] == '{')
7911 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007912 amount += curbuf->b_ind_open_extra;
7913 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007914 }
7915 }
7916
7917 if (lookfor_cpp_namespace)
7918 {
7919 /*
7920 * Looking for C++ namespace, need to look further
7921 * back.
7922 */
7923 if (curwin->w_cursor.lnum == ourscope)
7924 continue;
7925
7926 if (curwin->w_cursor.lnum == 0
7927 || curwin->w_cursor.lnum
7928 < ourscope - FIND_NAMESPACE_LIM)
7929 break;
7930
7931 l = ml_get_curline();
7932
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007933 /* If we're in a comment or raw string now, skip
7934 * to the start of it. */
7935 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007936 if (trypos != NULL)
7937 {
7938 curwin->w_cursor.lnum = trypos->lnum + 1;
7939 curwin->w_cursor.col = 0;
7940 continue;
7941 }
7942
7943 /* Skip preprocessor directives and blank lines. */
7944 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7945 continue;
7946
7947 /* Finally the actual check for "namespace". */
7948 if (cin_is_cpp_namespace(l))
7949 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007950 amount += curbuf->b_ind_cpp_namespace
7951 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007952 break;
7953 }
7954
7955 if (cin_nocode(l))
7956 continue;
7957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959 break;
7960 }
7961
7962 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007963 * If we're in a comment or raw string now, skip to the start
7964 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007966 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {
7968 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007969 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 continue;
7971 }
7972
7973 l = ml_get_curline();
7974
7975 /*
7976 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007977 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007979 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (iscase || cin_isscopedecl(l))
7981 {
7982 /* we are only looking for cpp base class
7983 * declaration/initialization any longer */
7984 if (lookfor == LOOKFOR_CPP_BASECLASS)
7985 break;
7986
7987 /* When looking for a "do" we are not interested in
7988 * labels. */
7989 if (whilelevel > 0)
7990 continue;
7991
7992 /*
7993 * case xx:
7994 * c = 99 + <- this indent plus continuation
7995 *-> here;
7996 */
7997 if (lookfor == LOOKFOR_UNTERM
7998 || lookfor == LOOKFOR_ENUM_OR_INIT)
7999 {
8000 if (cont_amount > 0)
8001 amount = cont_amount;
8002 else
8003 amount += ind_continuation;
8004 break;
8005 }
8006
8007 /*
8008 * case xx: <- line up with this case
8009 * x = 333;
8010 * case yy:
8011 */
8012 if ( (iscase && lookfor == LOOKFOR_CASE)
8013 || (iscase && lookfor_break)
8014 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8015 {
8016 /*
8017 * Check that this case label is not for another
8018 * switch()
8019 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008020 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008021 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {
8023 amount = get_indent(); /* XXX */
8024 break;
8025 }
8026 continue;
8027 }
8028
8029 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8030
8031 /*
8032 * case xx: if (cond) <- line up with this if
8033 * y = y + 1;
8034 * -> s = 99;
8035 *
8036 * case xx:
8037 * if (cond) <- line up with this line
8038 * y = y + 1;
8039 * -> s = 99;
8040 */
8041 if (lookfor == LOOKFOR_TERM)
8042 {
8043 if (n)
8044 amount = n;
8045
8046 if (!lookfor_break)
8047 break;
8048 }
8049
8050 /*
8051 * case xx: x = x + 1; <- line up with this x
8052 * -> y = y + 1;
8053 *
8054 * case xx: if (cond) <- line up with this if
8055 * -> y = y + 1;
8056 */
8057 if (n)
8058 {
8059 amount = n;
8060 l = after_label(ml_get_curline());
8061 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008062 {
8063 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008064 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008065 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008066 amount += curbuf->b_ind_level
8067 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 break;
8070 }
8071
8072 /*
8073 * Try to get the indent of a statement before the switch
8074 * label. If nothing is found, line up relative to the
8075 * switch label.
8076 * break; <- may line up with this line
8077 * case xx:
8078 * -> y = 1;
8079 */
8080 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008081 ? curbuf->b_ind_case_code
8082 : curbuf->b_ind_scopedecl_code);
8083 lookfor = curbuf->b_ind_case_break
8084 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 continue;
8086 }
8087
8088 /*
8089 * Looking for a switch() label or C++ scope declaration,
8090 * ignore other lines, skip {}-blocks.
8091 */
8092 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8093 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008094 if (find_last_paren(l, '{', '}')
8095 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008096 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008098 curwin->w_cursor.col = 0;
8099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 continue;
8101 }
8102
8103 /*
8104 * Ignore jump labels with nothing after them.
8105 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008106 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {
8108 l = after_label(ml_get_curline());
8109 if (l == NULL || cin_nocode(l))
8110 continue;
8111 }
8112
8113 /*
8114 * Ignore #defines, #if, etc.
8115 * Ignore comment and empty lines.
8116 * (need to get the line again, cin_islabel() may have
8117 * unlocked it)
8118 */
8119 l = ml_get_curline();
8120 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
8121 || cin_nocode(l))
8122 continue;
8123
8124 /*
8125 * Are we at the start of a cpp base class declaration or
8126 * constructor initialization?
8127 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008128 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008129 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008130 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008131 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008132 l = ml_get_curline();
8133 }
8134 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
8136 if (lookfor == LOOKFOR_UNTERM)
8137 {
8138 if (cont_amount > 0)
8139 amount = cont_amount;
8140 else
8141 amount += ind_continuation;
8142 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008143 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008145 /* Need to find start of the declaration. */
8146 lookfor = LOOKFOR_UNTERM;
8147 ind_continuation = 0;
8148 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 }
8150 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008151 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008152 amount = get_baseclass_amount(
8153 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 break;
8155 }
8156 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8157 {
8158 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008159 * declaration or initialization before the opening brace.
8160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 if (cin_isterminated(l, TRUE, FALSE))
8162 break;
8163 else
8164 continue;
8165 }
8166
8167 /*
8168 * What happens next depends on the line being terminated.
8169 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008170 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 * 123,
8172 * sizeof
8173 * here
8174 * Otherwise check whether it is a enumeration or structure
8175 * initialisation (not indented) or a variable declaration
8176 * (indented).
8177 */
8178 terminated = cin_isterminated(l, FALSE, TRUE);
8179
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008180 if (js_cur_has_key)
8181 {
8182 js_cur_has_key = 0; /* only check the first line */
8183 if (curbuf->b_ind_js && terminated == ',')
8184 {
8185 /* For Javascript we might be inside an object:
8186 * key: something, <- align with this
8187 * key: something
8188 * or:
8189 * key: something + <- align with this
8190 * something,
8191 * key: something
8192 */
8193 lookfor = LOOKFOR_JS_KEY;
8194 }
8195 }
8196 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8197 {
8198 amount = get_indent();
8199 break;
8200 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008201 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008202 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008203 if (tryposBrace != NULL && tryposBrace->lnum
8204 >= curwin->w_cursor.lnum)
8205 break;
8206 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008207 /* line below current line is the one that starts a
8208 * (possibly broken) line ending in a comma. */
8209 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008210 else
8211 {
8212 amount = get_indent();
8213 if (curwin->w_cursor.lnum - 1 == ourscope)
8214 /* line above is start of the scope, thus current
8215 * line is the one that stars a (possibly broken)
8216 * line ending in a comma. */
8217 break;
8218 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008219 }
8220
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8222 && terminated == ','))
8223 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008224 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8225 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008226 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 /*
8228 * if we're in the middle of a paren thing,
8229 * go back to the line that starts it so
8230 * we can get the right prevailing indent
8231 * if ( foo &&
8232 * bar )
8233 */
8234 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008235 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008237 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 */
8239 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008240 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008241 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8242 || (trypos->lnum == tryposBrace->lnum
8243 && trypos->col < tryposBrace->col)))
8244 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245
8246 /*
8247 * If we are looking for ',', we also look for matching
8248 * braces.
8249 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008250 if (trypos == NULL && terminated == ','
8251 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008252 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
8254 if (trypos != NULL)
8255 {
8256 /*
8257 * Check if we are on a case label now. This is
8258 * handled above.
8259 * case xx: if ( asdf &&
8260 * asdf)
8261 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008262 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008264 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {
8266 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008267 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 continue;
8269 }
8270 }
8271
8272 /*
8273 * Skip over continuation lines to find the one to get the
8274 * indent from
8275 * char *usethis = "bla\
8276 * bla",
8277 * here;
8278 */
8279 if (terminated == ',')
8280 {
8281 while (curwin->w_cursor.lnum > 1)
8282 {
8283 l = ml_get(curwin->w_cursor.lnum - 1);
8284 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8285 break;
8286 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008287 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 }
8289 }
8290
8291 /*
8292 * Get indent and pointer to text for current line,
8293 * ignoring any jump label. XXX
8294 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008295 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008296 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008297 else
8298 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 /*
8300 * If this is just above the line we are indenting, and it
8301 * starts with a '{', line it up with this line.
8302 * while (not)
8303 * -> {
8304 * }
8305 */
8306 if (terminated != ',' && lookfor != LOOKFOR_TERM
8307 && theline[0] == '{')
8308 {
8309 amount = cur_amount;
8310 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008311 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 * doesn't start with a '{', which must have a match
8313 * in the same line (scope is the same). Probably:
8314 * { 1, 2 },
8315 * -> { 3, 4 }
8316 */
8317 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008318 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008320 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
8322 /* have to look back, whether it is a cpp base
8323 * class declaration or initialization */
8324 lookfor = LOOKFOR_CPP_BASECLASS;
8325 continue;
8326 }
8327 break;
8328 }
8329
8330 /*
8331 * Check if we are after an "if", "while", etc.
8332 * Also allow " } else".
8333 */
8334 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8335 {
8336 /*
8337 * Found an unterminated line after an if (), line up
8338 * with the last one.
8339 * if (cond)
8340 * 100 +
8341 * -> here;
8342 */
8343 if (lookfor == LOOKFOR_UNTERM
8344 || lookfor == LOOKFOR_ENUM_OR_INIT)
8345 {
8346 if (cont_amount > 0)
8347 amount = cont_amount;
8348 else
8349 amount += ind_continuation;
8350 break;
8351 }
8352
8353 /*
8354 * If this is just above the line we are indenting, we
8355 * are finished.
8356 * while (not)
8357 * -> here;
8358 * Otherwise this indent can be used when the line
8359 * before this is terminated.
8360 * yyy;
8361 * if (stat)
8362 * while (not)
8363 * xxx;
8364 * -> here;
8365 */
8366 amount = cur_amount;
8367 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008368 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 if (lookfor != LOOKFOR_TERM)
8370 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008371 amount += curbuf->b_ind_level
8372 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 break;
8374 }
8375
8376 /*
8377 * Special trick: when expecting the while () after a
8378 * do, line up with the while()
8379 * do
8380 * x = 1;
8381 * -> here
8382 */
8383 l = skipwhite(ml_get_curline());
8384 if (cin_isdo(l))
8385 {
8386 if (whilelevel == 0)
8387 break;
8388 --whilelevel;
8389 }
8390
8391 /*
8392 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008393 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 * Need to use the scope of this "else". XXX
8395 * If whilelevel != 0 continue looking for a "do {".
8396 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008397 if (cin_iselse(l) && whilelevel == 0)
8398 {
8399 /* If we're looking at "} else", let's make sure we
8400 * find the opening brace of the enclosing scope,
8401 * not the one from "if () {". */
8402 if (*l == '}')
8403 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008404 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008405
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008406 if ((trypos = find_start_brace()) == NULL
8407 || find_match(LOOKFOR_IF, trypos->lnum)
8408 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008409 break;
8410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 }
8412
8413 /*
8414 * If we're below an unterminated line that is not an
8415 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008416 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 * the line before this one.
8418 */
8419 else
8420 {
8421 /*
8422 * Found two unterminated lines on a row, line up with
8423 * the last one.
8424 * c = 99 +
8425 * 100 +
8426 * -> here;
8427 */
8428 if (lookfor == LOOKFOR_UNTERM)
8429 {
8430 /* When line ends in a comma add extra indent */
8431 if (terminated == ',')
8432 amount += ind_continuation;
8433 break;
8434 }
8435
8436 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8437 {
8438 /* Found two lines ending in ',', lineup with the
8439 * lowest one, but check for cpp base class
8440 * declaration/initialization, if it is an
8441 * opening brace or we are looking just for
8442 * enumerations/initializations. */
8443 if (terminated == ',')
8444 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008445 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 break;
8447
8448 lookfor = LOOKFOR_CPP_BASECLASS;
8449 continue;
8450 }
8451
8452 /* Ignore unterminated lines in between, but
8453 * reduce indent. */
8454 if (amount > cur_amount)
8455 amount = cur_amount;
8456 }
8457 else
8458 {
8459 /*
8460 * Found first unterminated line on a row, may
8461 * line up with this line, remember its indent
8462 * 100 +
8463 * -> here;
8464 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008465 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008467
8468 n = (int)STRLEN(l);
8469 if (terminated == ',' && (*skipwhite(l) == ']'
8470 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
8473 /*
8474 * If previous line ends in ',', check whether we
8475 * are in an initialization or enum
8476 * struct xxx =
8477 * {
8478 * sizeof a,
8479 * 124 };
8480 * or a normal possible continuation line.
8481 * but only, of no other statement has been found
8482 * yet.
8483 */
8484 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8485 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008486 if (curbuf->b_ind_js)
8487 {
8488 /* Search for a line ending in a comma
8489 * and line up with the line below it
8490 * (could be the current line).
8491 * some = [
8492 * 1, <- line up here
8493 * 2,
8494 * some = [
8495 * 3 + <- line up here
8496 * 4 *
8497 * 5,
8498 * 6,
8499 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008500 if (cin_iscomment(skipwhite(l)))
8501 break;
8502 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008503 trypos = find_match_char('[',
8504 curbuf->b_ind_maxparen);
8505 if (trypos != NULL)
8506 {
8507 if (trypos->lnum
8508 == curwin->w_cursor.lnum - 1)
8509 {
8510 /* Current line is first inside
8511 * [], line up with it. */
8512 break;
8513 }
8514 ourscope = trypos->lnum;
8515 }
8516 }
8517 else
8518 {
8519 lookfor = LOOKFOR_ENUM_OR_INIT;
8520 cont_amount = cin_first_id_amount();
8521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 }
8523 else
8524 {
8525 if (lookfor == LOOKFOR_INITIAL
8526 && *l != NUL
8527 && l[STRLEN(l) - 1] == '\\')
8528 /* XXX */
8529 cont_amount = cin_get_equal_amount(
8530 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008531 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008532 && lookfor != LOOKFOR_JS_KEY
8533 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 lookfor = LOOKFOR_UNTERM;
8535 }
8536 }
8537 }
8538 }
8539
8540 /*
8541 * Check if we are after a while (cond);
8542 * If so: Ignore until the matching "do".
8543 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008544 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {
8546 /*
8547 * Found an unterminated line after a while ();, line up
8548 * with the last one.
8549 * while (cond);
8550 * 100 + <- line up with this one
8551 * -> here;
8552 */
8553 if (lookfor == LOOKFOR_UNTERM
8554 || lookfor == LOOKFOR_ENUM_OR_INIT)
8555 {
8556 if (cont_amount > 0)
8557 amount = cont_amount;
8558 else
8559 amount += ind_continuation;
8560 break;
8561 }
8562
8563 if (whilelevel == 0)
8564 {
8565 lookfor = LOOKFOR_TERM;
8566 amount = get_indent(); /* XXX */
8567 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008568 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
8570 ++whilelevel;
8571 }
8572
8573 /*
8574 * We are after a "normal" statement.
8575 * If we had another statement we can stop now and use the
8576 * indent of that other statement.
8577 * Otherwise the indent of the current statement may be used,
8578 * search backwards for the next "normal" statement.
8579 */
8580 else
8581 {
8582 /*
8583 * Skip single break line, if before a switch label. It
8584 * may be lined up with the case label.
8585 */
8586 if (lookfor == LOOKFOR_NOBREAK
8587 && cin_isbreak(skipwhite(ml_get_curline())))
8588 {
8589 lookfor = LOOKFOR_ANY;
8590 continue;
8591 }
8592
8593 /*
8594 * Handle "do {" line.
8595 */
8596 if (whilelevel > 0)
8597 {
8598 l = cin_skipcomment(ml_get_curline());
8599 if (cin_isdo(l))
8600 {
8601 amount = get_indent(); /* XXX */
8602 --whilelevel;
8603 continue;
8604 }
8605 }
8606
8607 /*
8608 * Found a terminated line above an unterminated line. Add
8609 * the amount for a continuation line.
8610 * x = 1;
8611 * y = foo +
8612 * -> here;
8613 * or
8614 * int x = 1;
8615 * int foo,
8616 * -> here;
8617 */
8618 if (lookfor == LOOKFOR_UNTERM
8619 || lookfor == LOOKFOR_ENUM_OR_INIT)
8620 {
8621 if (cont_amount > 0)
8622 amount = cont_amount;
8623 else
8624 amount += ind_continuation;
8625 break;
8626 }
8627
8628 /*
8629 * Found a terminated line above a terminated line or "if"
8630 * etc. line. Use the amount of the line below us.
8631 * x = 1; x = 1;
8632 * if (asdf) y = 2;
8633 * while (asdf) ->here;
8634 * here;
8635 * ->foo;
8636 */
8637 if (lookfor == LOOKFOR_TERM)
8638 {
8639 if (!lookfor_break && whilelevel == 0)
8640 break;
8641 }
8642
8643 /*
8644 * First line above the one we're indenting is terminated.
8645 * To know what needs to be done look further backward for
8646 * a terminated line.
8647 */
8648 else
8649 {
8650 /*
8651 * position the cursor over the rightmost paren, so
8652 * that matching it will take us back to the start of
8653 * the line. Helps for:
8654 * func(asdr,
8655 * asdfasdf);
8656 * here;
8657 */
8658term_again:
8659 l = ml_get_curline();
8660 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008661 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008662 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {
8664 /*
8665 * Check if we are on a case label now. This is
8666 * handled above.
8667 * case xx: if ( asdf &&
8668 * asdf)
8669 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008670 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008672 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 {
8674 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008675 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 continue;
8677 }
8678 }
8679
8680 /* When aligning with the case statement, don't align
8681 * with a statement after it.
8682 * case 1: { <-- don't use this { position
8683 * stat;
8684 * }
8685 * case 2:
8686 * stat;
8687 * }
8688 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008689 iscase = (curbuf->b_ind_keep_case_label
8690 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 /*
8693 * Get indent and pointer to text for current line,
8694 * ignoring any jump label.
8695 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008696 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697
8698 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008699 amount += curbuf->b_ind_open_extra;
8700 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008701 l = skipwhite(l);
8702 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008703 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8705
8706 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008707 * When a terminated line starts with "else" skip to
8708 * the matching "if":
8709 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008710 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008711 * Need to use the scope of this "else". XXX
8712 * If whilelevel != 0 continue looking for a "do {".
8713 */
8714 if (lookfor == LOOKFOR_TERM
8715 && *l != '}'
8716 && cin_iselse(l)
8717 && whilelevel == 0)
8718 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008719 if ((trypos = find_start_brace()) == NULL
8720 || find_match(LOOKFOR_IF, trypos->lnum)
8721 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008722 break;
8723 continue;
8724 }
8725
8726 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 * If we're at the end of a block, skip to the start of
8728 * that block.
8729 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008730 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008731 if (find_last_paren(l, '{', '}') /* XXX */
8732 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008734 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 /* if not "else {" check for terminated again */
8736 /* but skip block for "} else {" */
8737 l = cin_skipcomment(ml_get_curline());
8738 if (*l == '}' || !cin_iselse(l))
8739 goto term_again;
8740 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008741 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 }
8743 }
8744 }
8745 }
8746 }
8747 }
8748
8749 /* add extra indent for a comment */
8750 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008751 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008752
8753 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008754 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8755 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008756
8757 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008759
8760 /*
8761 * ok -- we're not inside any sort of structure at all!
8762 *
8763 * This means we're at the top level, and everything should
8764 * basically just match where the previous line is, except
8765 * for the lines immediately following a function declaration,
8766 * which are K&R-style parameters and need to be indented.
8767 *
8768 * if our line starts with an open brace, forget about any
8769 * prevailing indent and make sure it looks like the start
8770 * of a function
8771 */
8772
8773 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008775 amount = curbuf->b_ind_first_open;
8776 goto theend;
8777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008779 /*
8780 * If the NEXT line is a function declaration, the current
8781 * line needs to be indented as a function type spec.
8782 * Don't do this if the current line looks like a comment or if the
8783 * current line is terminated, ie. ends in ';', or if the current line
8784 * contains { or }: "void f() {\n if (1)"
8785 */
8786 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8787 && !cin_nocode(theline)
8788 && vim_strchr(theline, '{') == NULL
8789 && vim_strchr(theline, '}') == NULL
8790 && !cin_ends_in(theline, (char_u *)":", NULL)
8791 && !cin_ends_in(theline, (char_u *)",", NULL)
8792 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8793 cur_curpos.lnum + 1)
8794 && !cin_isterminated(theline, FALSE, TRUE))
8795 {
8796 amount = curbuf->b_ind_func_type;
8797 goto theend;
8798 }
8799
8800 /* search backwards until we find something we recognize */
8801 amount = 0;
8802 curwin->w_cursor = cur_curpos;
8803 while (curwin->w_cursor.lnum > 1)
8804 {
8805 curwin->w_cursor.lnum--;
8806 curwin->w_cursor.col = 0;
8807
8808 l = ml_get_curline();
8809
8810 /*
8811 * If we're in a comment or raw string now, skip to the start
8812 * of it.
8813 */ /* XXX */
8814 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008816 curwin->w_cursor.lnum = trypos->lnum + 1;
8817 curwin->w_cursor.col = 0;
8818 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
8820
8821 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008822 * Are we at the start of a cpp base class declaration or
8823 * constructor initialization?
8824 */ /* XXX */
8825 n = FALSE;
8826 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008828 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8829 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008831 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008833 /* XXX */
8834 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8835 break;
8836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008838 /*
8839 * Skip preprocessor directives and blank lines.
8840 */
8841 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8842 continue;
8843
8844 if (cin_nocode(l))
8845 continue;
8846
8847 /*
8848 * If the previous line ends in ',', use one level of
8849 * indentation:
8850 * int foo,
8851 * bar;
8852 * do this before checking for '}' in case of eg.
8853 * enum foobar
8854 * {
8855 * ...
8856 * } foo,
8857 * bar;
8858 */
8859 n = 0;
8860 if (cin_ends_in(l, (char_u *)",", NULL)
8861 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8862 {
8863 /* take us back to opening paren */
8864 if (find_last_paren(l, '(', ')')
8865 && (trypos = find_match_paren(
8866 curbuf->b_ind_maxparen)) != NULL)
8867 curwin->w_cursor = *trypos;
8868
8869 /* For a line ending in ',' that is a continuation line go
8870 * back to the first line with a backslash:
8871 * char *foo = "bla\
8872 * bla",
8873 * here;
8874 */
8875 while (n == 0 && curwin->w_cursor.lnum > 1)
8876 {
8877 l = ml_get(curwin->w_cursor.lnum - 1);
8878 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8879 break;
8880 --curwin->w_cursor.lnum;
8881 curwin->w_cursor.col = 0;
8882 }
8883
8884 amount = get_indent(); /* XXX */
8885
8886 if (amount == 0)
8887 amount = cin_first_id_amount();
8888 if (amount == 0)
8889 amount = ind_continuation;
8890 break;
8891 }
8892
8893 /*
8894 * If the line looks like a function declaration, and we're
8895 * not in a comment, put it the left margin.
8896 */
8897 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
8898 break;
8899 l = ml_get_curline();
8900
8901 /*
8902 * Finding the closing '}' of a previous function. Put
8903 * current line at the left margin. For when 'cino' has "fs".
8904 */
8905 if (*skipwhite(l) == '}')
8906 break;
8907
8908 /* (matching {)
8909 * If the previous line ends on '};' (maybe followed by
8910 * comments) align at column 0. For example:
8911 * char *string_array[] = { "foo",
8912 * / * x * / "b};ar" }; / * foobar * /
8913 */
8914 if (cin_ends_in(l, (char_u *)"};", NULL))
8915 break;
8916
8917 /*
8918 * If the previous line ends on '[' we are probably in an
8919 * array constant:
8920 * something = [
8921 * 234, <- extra indent
8922 */
8923 if (cin_ends_in(l, (char_u *)"[", NULL))
8924 {
8925 amount = get_indent() + ind_continuation;
8926 break;
8927 }
8928
8929 /*
8930 * Find a line only has a semicolon that belongs to a previous
8931 * line ending in '}', e.g. before an #endif. Don't increase
8932 * indent then.
8933 */
8934 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8935 {
8936 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937
8938 while (curwin->w_cursor.lnum > 1)
8939 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008940 look = ml_get(--curwin->w_cursor.lnum);
8941 if (!(cin_nocode(look) || cin_ispreproc_cont(
8942 &look, &curwin->w_cursor.lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008944 }
8945 if (curwin->w_cursor.lnum > 0
8946 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008949 curwin->w_cursor = curpos_save;
8950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008952 /*
8953 * If the PREVIOUS line is a function declaration, the current
8954 * line (and the ones that follow) needs to be indented as
8955 * parameters.
8956 */
8957 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
8958 {
8959 amount = curbuf->b_ind_param;
8960 break;
8961 }
8962
8963 /*
8964 * If the previous line ends in ';' and the line before the
8965 * previous line ends in ',' or '\', ident to column zero:
8966 * int foo,
8967 * bar;
8968 * indent_to_0 here;
8969 */
8970 if (cin_ends_in(l, (char_u *)";", NULL))
8971 {
8972 l = ml_get(curwin->w_cursor.lnum - 1);
8973 if (cin_ends_in(l, (char_u *)",", NULL)
8974 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8975 break;
8976 l = ml_get_curline();
8977 }
8978
8979 /*
8980 * Doesn't look like anything interesting -- so just
8981 * use the indent of this line.
8982 *
8983 * Position the cursor over the rightmost paren, so that
8984 * matching it will take us back to the start of the line.
8985 */
8986 find_last_paren(l, '(', ')');
8987
8988 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
8989 curwin->w_cursor = *trypos;
8990 amount = get_indent(); /* XXX */
8991 break;
8992 }
8993
8994 /* add extra indent for a comment */
8995 if (cin_iscomment(theline))
8996 amount += curbuf->b_ind_comment;
8997
8998 /* add extra indent if the previous line ended in a backslash:
8999 * "asdfasdf\
9000 * here";
9001 * char *foo = "asdf\
9002 * here";
9003 */
9004 if (cur_curpos.lnum > 1)
9005 {
9006 l = ml_get(cur_curpos.lnum - 1);
9007 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9008 {
9009 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9010 if (cur_amount > 0)
9011 amount = cur_amount;
9012 else if (cur_amount == 0)
9013 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 }
9015 }
9016
9017theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009018 if (amount < 0)
9019 amount = 0;
9020
9021laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 /* put the cursor back where it belongs */
9023 curwin->w_cursor = cur_curpos;
9024
9025 vim_free(linecopy);
9026
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 return amount;
9028}
9029
9030 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009031find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033 char_u *look;
9034 pos_T *theirscope;
9035 char_u *mightbeif;
9036 int elselevel;
9037 int whilelevel;
9038
9039 if (lookfor == LOOKFOR_IF)
9040 {
9041 elselevel = 1;
9042 whilelevel = 0;
9043 }
9044 else
9045 {
9046 elselevel = 0;
9047 whilelevel = 1;
9048 }
9049
9050 curwin->w_cursor.col = 0;
9051
9052 while (curwin->w_cursor.lnum > ourscope + 1)
9053 {
9054 curwin->w_cursor.lnum--;
9055 curwin->w_cursor.col = 0;
9056
9057 look = cin_skipcomment(ml_get_curline());
9058 if (cin_iselse(look)
9059 || cin_isif(look)
9060 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009061 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 {
9063 /*
9064 * if we've gone outside the braces entirely,
9065 * we must be out of scope...
9066 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009067 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 if (theirscope == NULL)
9069 break;
9070
9071 /*
9072 * and if the brace enclosing this is further
9073 * back than the one enclosing the else, we're
9074 * out of luck too.
9075 */
9076 if (theirscope->lnum < ourscope)
9077 break;
9078
9079 /*
9080 * and if they're enclosed in a *deeper* brace,
9081 * then we can ignore it because it's in a
9082 * different scope...
9083 */
9084 if (theirscope->lnum > ourscope)
9085 continue;
9086
9087 /*
9088 * if it was an "else" (that's not an "else if")
9089 * then we need to go back to another if, so
9090 * increment elselevel
9091 */
9092 look = cin_skipcomment(ml_get_curline());
9093 if (cin_iselse(look))
9094 {
9095 mightbeif = cin_skipcomment(look + 4);
9096 if (!cin_isif(mightbeif))
9097 ++elselevel;
9098 continue;
9099 }
9100
9101 /*
9102 * if it was a "while" then we need to go back to
9103 * another "do", so increment whilelevel. XXX
9104 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009105 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 {
9107 ++whilelevel;
9108 continue;
9109 }
9110
9111 /* If it's an "if" decrement elselevel */
9112 look = cin_skipcomment(ml_get_curline());
9113 if (cin_isif(look))
9114 {
9115 elselevel--;
9116 /*
9117 * When looking for an "if" ignore "while"s that
9118 * get in the way.
9119 */
9120 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9121 whilelevel = 0;
9122 }
9123
9124 /* If it's a "do" decrement whilelevel */
9125 if (cin_isdo(look))
9126 whilelevel--;
9127
9128 /*
9129 * if we've used up all the elses, then
9130 * this must be the if that we want!
9131 * match the indent level of that if.
9132 */
9133 if (elselevel <= 0 && whilelevel <= 0)
9134 {
9135 return OK;
9136 }
9137 }
9138 }
9139 return FAIL;
9140}
9141
9142# if defined(FEAT_EVAL) || defined(PROTO)
9143/*
9144 * Get indent level from 'indentexpr'.
9145 */
9146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009147get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148{
9149 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009150 pos_T save_pos;
9151 colnr_T save_curswant;
9152 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009154 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9155 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009157 /* Save and restore cursor position and curswant, in case it was changed
9158 * via :normal commands */
9159 save_pos = curwin->w_cursor;
9160 save_curswant = curwin->w_curswant;
9161 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009163 if (use_sandbox)
9164 ++sandbox;
9165 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009167 if (use_sandbox)
9168 --sandbox;
9169 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170
9171 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9172 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9173 * command. */
9174 save_State = State;
9175 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009176 curwin->w_cursor = save_pos;
9177 curwin->w_curswant = save_curswant;
9178 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 check_cursor();
9180 State = save_State;
9181
9182 /* If there is an error, just keep the current indent. */
9183 if (indent < 0)
9184 indent = get_indent();
9185
9186 return indent;
9187}
9188# endif
9189
9190#endif /* FEAT_CINDENT */
9191
9192#if defined(FEAT_LISP) || defined(PROTO)
9193
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009194static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
9196 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009197lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 char_u buf[LSIZE];
9200 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009201 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
9203 while (*word != NUL)
9204 {
9205 (void)copy_option_part(&word, buf, LSIZE, ",");
9206 len = (int)STRLEN(buf);
9207 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9208 return TRUE;
9209 }
9210 return FALSE;
9211}
9212
9213/*
9214 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9215 * The incompatible newer method is quite a bit better at indenting
9216 * code in lisp-like languages than the traditional one; it's still
9217 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9218 *
9219 * TODO:
9220 * Findmatch() should be adapted for lisp, also to make showmatch
9221 * work correctly: now (v5.3) it seems all C/C++ oriented:
9222 * - it does not recognize the #\( and #\) notations as character literals
9223 * - it doesn't know about comments starting with a semicolon
9224 * - it incorrectly interprets '(' as a character literal
9225 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009226 * Update from Sergey Khorev:
9227 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 */
9229 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009230get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009232 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 int amount;
9234 char_u *that;
9235 colnr_T col;
9236 colnr_T firsttry;
9237 int parencount, quotecount;
9238 int vi_lisp;
9239
9240 /* Set vi_lisp to use the vi-compatible method */
9241 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9242
9243 realpos = curwin->w_cursor;
9244 curwin->w_cursor.col = 0;
9245
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009246 if ((pos = findmatch(NULL, '(')) == NULL)
9247 pos = findmatch(NULL, '[');
9248 else
9249 {
9250 paren = *pos;
9251 pos = findmatch(NULL, '[');
9252 if (pos == NULL || ltp(pos, &paren))
9253 pos = &paren;
9254 }
9255 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 {
9257 /* Extra trick: Take the indent of the first previous non-white
9258 * line that is at the same () level. */
9259 amount = -1;
9260 parencount = 0;
9261
9262 while (--curwin->w_cursor.lnum >= pos->lnum)
9263 {
9264 if (linewhite(curwin->w_cursor.lnum))
9265 continue;
9266 for (that = ml_get_curline(); *that != NUL; ++that)
9267 {
9268 if (*that == ';')
9269 {
9270 while (*(that + 1) != NUL)
9271 ++that;
9272 continue;
9273 }
9274 if (*that == '\\')
9275 {
9276 if (*(that + 1) != NUL)
9277 ++that;
9278 continue;
9279 }
9280 if (*that == '"' && *(that + 1) != NUL)
9281 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009282 while (*++that && *that != '"')
9283 {
9284 /* skipping escaped characters in the string */
9285 if (*that == '\\')
9286 {
9287 if (*++that == NUL)
9288 break;
9289 if (that[1] == NUL)
9290 {
9291 ++that;
9292 break;
9293 }
9294 }
9295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009297 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009299 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 --parencount;
9301 }
9302 if (parencount == 0)
9303 {
9304 amount = get_indent();
9305 break;
9306 }
9307 }
9308
9309 if (amount == -1)
9310 {
9311 curwin->w_cursor.lnum = pos->lnum;
9312 curwin->w_cursor.col = pos->col;
9313 col = pos->col;
9314
9315 that = ml_get_curline();
9316
9317 if (vi_lisp && get_indent() == 0)
9318 amount = 2;
9319 else
9320 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009321 char_u *line = that;
9322
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 amount = 0;
9324 while (*that && col)
9325 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009326 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 col--;
9328 }
9329
9330 /*
9331 * Some keywords require "body" indenting rules (the
9332 * non-standard-lisp ones are Scheme special forms):
9333 *
9334 * (let ((a 1)) instead (let ((a 1))
9335 * (...)) of (...))
9336 */
9337
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009338 if (!vi_lisp && (*that == '(' || *that == '[')
9339 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 amount += 2;
9341 else
9342 {
9343 that++;
9344 amount++;
9345 firsttry = amount;
9346
9347 while (vim_iswhite(*that))
9348 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009349 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 ++that;
9351 }
9352
9353 if (*that && *that != ';') /* not a comment line */
9354 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009355 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009357 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 firsttry++;
9359
9360 parencount = 0;
9361 quotecount = 0;
9362
9363 if (vi_lisp
9364 || (*that != '"'
9365 && *that != '\''
9366 && *that != '#'
9367 && (*that < '0' || *that > '9')))
9368 {
9369 while (*that
9370 && (!vim_iswhite(*that)
9371 || quotecount
9372 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009373 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 && !quotecount
9375 && !parencount
9376 && vi_lisp)))
9377 {
9378 if (*that == '"')
9379 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009380 if ((*that == '(' || *that == '[')
9381 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009383 if ((*that == ')' || *that == ']')
9384 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 --parencount;
9386 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009387 amount += lbr_chartabsize_adv(
9388 line, &that, (colnr_T)amount);
9389 amount += lbr_chartabsize_adv(
9390 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 }
9392 }
9393 while (vim_iswhite(*that))
9394 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009395 amount += lbr_chartabsize(
9396 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 that++;
9398 }
9399 if (!*that || *that == ';')
9400 amount = firsttry;
9401 }
9402 }
9403 }
9404 }
9405 }
9406 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009407 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408
9409 curwin->w_cursor = realpos;
9410
9411 return amount;
9412}
9413#endif /* FEAT_LISP */
9414
9415 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009416prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009418#if defined(SIGHUP) && defined(SIG_IGN)
9419 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9420 * makes Vim exit and then handling SIGHUP causes various reentrance
9421 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009422 signal(SIGHUP, SIG_IGN);
9423#endif
9424
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425#ifdef FEAT_GUI
9426 if (gui.in_use)
9427 {
9428 gui.dying = TRUE;
9429 out_trash(); /* trash any pending output */
9430 }
9431 else
9432#endif
9433 {
9434 windgoto((int)Rows - 1, 0);
9435
9436 /*
9437 * Switch terminal mode back now, so messages end up on the "normal"
9438 * screen (if there are two screens).
9439 */
9440 settmode(TMODE_COOK);
9441#ifdef WIN3264
9442 if (can_end_termcap_mode(FALSE) == TRUE)
9443#endif
9444 stoptermcap();
9445 out_flush();
9446 }
9447}
9448
9449/*
9450 * Preserve files and exit.
9451 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009452 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9453 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 */
9455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009456preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457{
9458 buf_T *buf;
9459
9460 prepare_to_exit();
9461
Bram Moolenaar4770d092006-01-12 23:22:24 +00009462 /* Setting this will prevent free() calls. That avoids calling free()
9463 * recursively when free() was invoked with a bad pointer. */
9464 really_exiting = TRUE;
9465
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 out_str(IObuff);
9467 screen_start(); /* don't know where cursor is now */
9468 out_flush();
9469
9470 ml_close_notmod(); /* close all not-modified buffers */
9471
9472 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9473 {
9474 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9475 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009476 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 screen_start(); /* don't know where cursor is now */
9478 out_flush();
9479 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9480 break;
9481 }
9482 }
9483
9484 ml_close_all(FALSE); /* close all memfiles, without deleting */
9485
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009486 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487
9488 getout(1);
9489}
9490
9491/*
9492 * return TRUE if "fname" exists.
9493 */
9494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009495vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497 struct stat st;
9498
9499 if (mch_stat((char *)fname, &st))
9500 return FALSE;
9501 return TRUE;
9502}
9503
9504/*
9505 * Check for CTRL-C pressed, but only once in a while.
9506 * Should be used instead of ui_breakcheck() for functions that check for
9507 * each line in the file. Calling ui_breakcheck() each time takes too much
9508 * time, because it can be a system call.
9509 */
9510
9511#ifndef BREAKCHECK_SKIP
9512# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9513# define BREAKCHECK_SKIP 200
9514# else
9515# define BREAKCHECK_SKIP 32
9516# endif
9517#endif
9518
9519static int breakcheck_count = 0;
9520
9521 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009522line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524 if (++breakcheck_count >= BREAKCHECK_SKIP)
9525 {
9526 breakcheck_count = 0;
9527 ui_breakcheck();
9528 }
9529}
9530
9531/*
9532 * Like line_breakcheck() but check 10 times less often.
9533 */
9534 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009535fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
9537 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9538 {
9539 breakcheck_count = 0;
9540 ui_breakcheck();
9541 }
9542}
9543
9544/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009545 * Invoke expand_wildcards() for one pattern.
9546 * Expand items like "%:h" before the expansion.
9547 * Returns OK or FAIL.
9548 */
9549 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009550expand_wildcards_eval(
9551 char_u **pat, /* pointer to input pattern */
9552 int *num_file, /* resulting number of files */
9553 char_u ***file, /* array of resulting files */
9554 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009555{
9556 int ret = FAIL;
9557 char_u *eval_pat = NULL;
9558 char_u *exp_pat = *pat;
9559 char_u *ignored_msg;
9560 int usedlen;
9561
9562 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9563 {
9564 ++emsg_off;
9565 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9566 NULL, &ignored_msg, NULL);
9567 --emsg_off;
9568 if (eval_pat != NULL)
9569 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9570 }
9571
9572 if (exp_pat != NULL)
9573 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9574
9575 if (eval_pat != NULL)
9576 {
9577 vim_free(exp_pat);
9578 vim_free(eval_pat);
9579 }
9580
9581 return ret;
9582}
9583
9584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9586 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009587 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 */
9589 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009590expand_wildcards(
9591 int num_pat, /* number of input patterns */
9592 char_u **pat, /* array of input patterns */
9593 int *num_files, /* resulting number of files */
9594 char_u ***files, /* array of resulting files */
9595 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 int retval;
9598 int i, j;
9599 char_u *p;
9600 int non_suf_match; /* number without matching suffix */
9601
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009602 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603
9604 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009605 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 return retval;
9607
9608#ifdef FEAT_WILDIGN
9609 /*
9610 * Remove names that match 'wildignore'.
9611 */
9612 if (*p_wig)
9613 {
9614 char_u *ffname;
9615
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009616 /* check all files in (*files)[] */
9617 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009619 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 if (ffname == NULL) /* out of memory */
9621 break;
9622# ifdef VMS
9623 vms_remove_version(ffname);
9624# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009625 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009627 /* remove this matching files from the list */
9628 vim_free((*files)[i]);
9629 for (j = i; j + 1 < *num_files; ++j)
9630 (*files)[j] = (*files)[j + 1];
9631 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 --i;
9633 }
9634 vim_free(ffname);
9635 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009636
9637 /* If the number of matches is now zero, we fail. */
9638 if (*num_files == 0)
9639 {
9640 vim_free(*files);
9641 *files = NULL;
9642 return FAIL;
9643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 }
9645#endif
9646
9647 /*
9648 * Move the names where 'suffixes' match to the end.
9649 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009650 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 {
9652 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009653 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009655 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 {
9657 /*
9658 * Move the name without matching suffix to the front
9659 * of the list.
9660 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009661 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009663 (*files)[j] = (*files)[j - 1];
9664 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 }
9666 }
9667 }
9668
9669 return retval;
9670}
9671
9672/*
9673 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9674 */
9675 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009676match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
9678 int fnamelen, setsuflen;
9679 char_u *setsuf;
9680#define MAXSUFLEN 30 /* maximum length of a file suffix */
9681 char_u suf_buf[MAXSUFLEN];
9682
9683 fnamelen = (int)STRLEN(fname);
9684 setsuflen = 0;
9685 for (setsuf = p_su; *setsuf; )
9686 {
9687 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009688 if (setsuflen == 0)
9689 {
9690 char_u *tail = gettail(fname);
9691
9692 /* empty entry: match name without a '.' */
9693 if (vim_strchr(tail, '.') == NULL)
9694 {
9695 setsuflen = 1;
9696 break;
9697 }
9698 }
9699 else
9700 {
9701 if (fnamelen >= setsuflen
9702 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9703 (size_t)setsuflen) == 0)
9704 break;
9705 setsuflen = 0;
9706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 }
9708 return (setsuflen != 0);
9709}
9710
9711#if !defined(NO_EXPANDPATH) || defined(PROTO)
9712
9713# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009714static int vim_backtick(char_u *p);
9715static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716# endif
9717
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009718# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719/*
9720 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9721 * it's shared between these systems.
9722 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009723# if defined(PROTO)
9724# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725# else
9726# ifdef __BORLANDC__
9727# define _cdecl _RTLENTRYF
9728# endif
9729# endif
9730
9731/*
9732 * comparison function for qsort in dos_expandpath()
9733 */
9734 static int _cdecl
9735pstrcmp(const void *a, const void *b)
9736{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009737 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738}
9739
9740# ifndef WIN3264
9741 static void
9742namelowcpy(
9743 char_u *d,
9744 char_u *s)
9745{
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009746 while (*s)
9747 *d++ = TOLOWER_LOC(*s++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 *d = NUL;
9749}
9750# endif
9751
9752/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009753 * Recursively expand one path component into all matching files and/or
9754 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 * Return the number of matches found.
9756 * "path" has backslashes before chars that are not to be expanded, starting
9757 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009758 * Return the number of matches found.
9759 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 */
9761 static int
9762dos_expandpath(
9763 garray_T *gap,
9764 char_u *path,
9765 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009766 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009767 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009769 char_u *buf;
9770 char_u *path_end;
9771 char_u *p, *s, *e;
9772 int start_len = gap->ga_len;
9773 char_u *pat;
9774 regmatch_T regmatch;
9775 int starts_with_dot;
9776 int matches;
9777 int len;
9778 int starstar = FALSE;
9779 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#ifdef WIN3264
9781 WIN32_FIND_DATA fb;
9782 HANDLE hFind = (HANDLE)0;
9783# ifdef FEAT_MBYTE
9784 WIN32_FIND_DATAW wfb;
9785 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9786# endif
9787#else
9788 struct ffblk fb;
9789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009791 int ok;
9792
9793 /* Expanding "**" may take a long time, check for CTRL-C. */
9794 if (stardepth > 0)
9795 {
9796 ui_breakcheck();
9797 if (got_int)
9798 return 0;
9799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009801 /* Make room for file name. When doing encoding conversion the actual
9802 * length may be quite a bit longer, thus use the maximum possible length. */
9803 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (buf == NULL)
9805 return 0;
9806
9807 /*
9808 * Find the first part in the path name that contains a wildcard or a ~1.
9809 * Copy it into buf, including the preceding characters.
9810 */
9811 p = buf;
9812 s = buf;
9813 e = NULL;
9814 path_end = path;
9815 while (*path_end != NUL)
9816 {
9817 /* May ignore a wildcard that has a backslash before it; it will
9818 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9819 if (path_end >= path + wildoff && rem_backslash(path_end))
9820 *p++ = *path_end++;
9821 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9822 {
9823 if (e != NULL)
9824 break;
9825 s = p + 1;
9826 }
9827 else if (path_end >= path + wildoff
9828 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9829 e = p;
9830#ifdef FEAT_MBYTE
9831 if (has_mbyte)
9832 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009833 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 STRNCPY(p, path_end, len);
9835 p += len;
9836 path_end += len;
9837 }
9838 else
9839#endif
9840 *p++ = *path_end++;
9841 }
9842 e = p;
9843 *e = NUL;
9844
9845 /* now we have one wildcard component between s and e */
9846 /* Remove backslashes between "wildoff" and the start of the wildcard
9847 * component. */
9848 for (p = buf + wildoff; p < s; ++p)
9849 if (rem_backslash(p))
9850 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009851 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 --e;
9853 --s;
9854 }
9855
Bram Moolenaar231334e2005-07-25 20:46:57 +00009856 /* Check for "**" between "s" and "e". */
9857 for (p = s; p < e; ++p)
9858 if (p[0] == '*' && p[1] == '*')
9859 starstar = TRUE;
9860
Bram Moolenaard82103e2016-01-17 17:04:05 +01009861 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9863 if (pat == NULL)
9864 {
9865 vim_free(buf);
9866 return 0;
9867 }
9868
9869 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009870 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009871 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 regmatch.rm_ic = TRUE; /* Always ignore case */
9873 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009874 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009875 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 vim_free(pat);
9877
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009878 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 {
9880 vim_free(buf);
9881 return 0;
9882 }
9883
9884 /* remember the pattern or file name being looked for */
9885 matchname = vim_strsave(s);
9886
Bram Moolenaar231334e2005-07-25 20:46:57 +00009887 /* If "**" is by itself, this is the first time we encounter it and more
9888 * is following then find matches without any directory. */
9889 if (!didstar && stardepth < 100 && starstar && e - s == 2
9890 && *path_end == '/')
9891 {
9892 STRCPY(s, path_end + 1);
9893 ++stardepth;
9894 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9895 --stardepth;
9896 }
9897
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 /* Scan all files in the directory with "dir/ *.*" */
9899 STRCPY(s, "*.*");
9900#ifdef WIN3264
9901# ifdef FEAT_MBYTE
9902 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9903 {
9904 /* The active codepage differs from 'encoding'. Attempt using the
9905 * wide function. If it fails because it is not implemented fall back
9906 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009907 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 if (wn != NULL)
9909 {
9910 hFind = FindFirstFileW(wn, &wfb);
9911 if (hFind == INVALID_HANDLE_VALUE
9912 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9913 {
9914 vim_free(wn);
9915 wn = NULL;
9916 }
9917 }
9918 }
9919
9920 if (wn == NULL)
9921# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009922 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 ok = (hFind != INVALID_HANDLE_VALUE);
9924#else
9925 /* If we are expanding wildcards we try both files and directories */
9926 ok = (findfirst((char *)buf, &fb,
9927 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9928#endif
9929
9930 while (ok)
9931 {
9932#ifdef WIN3264
9933# ifdef FEAT_MBYTE
9934 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009935 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 else
9937# endif
9938 p = (char_u *)fb.cFileName;
9939#else
9940 p = (char_u *)fb.ff_name;
9941#endif
9942 /* Ignore entries starting with a dot, unless when asked for. Accept
9943 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +01009944 if ((p[0] != '.' || starts_with_dot
9945 || ((flags & EW_DODOT)
9946 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009948 || (regmatch.regprog != NULL
9949 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009950 || ((flags & EW_NOTWILD)
9951 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 {
9953#ifdef WIN3264
9954 STRCPY(s, p);
9955#else
9956 namelowcpy(s, p);
9957#endif
9958 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009959
9960 if (starstar && stardepth < 100)
9961 {
9962 /* For "**" in the pattern first go deeper in the tree to
9963 * find matches. */
9964 STRCPY(buf + len, "/**");
9965 STRCPY(buf + len + 3, path_end);
9966 ++stardepth;
9967 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9968 --stardepth;
9969 }
9970
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 STRCPY(buf + len, path_end);
9972 if (mch_has_exp_wildcard(path_end))
9973 {
9974 /* need to expand another component of the path */
9975 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009976 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 }
9978 else
9979 {
9980 /* no more wildcards, check if there is a match */
9981 /* remove backslashes for the remaining components only */
9982 if (*path_end != 0)
9983 backslash_halve(buf + len + 1);
9984 if (mch_getperm(buf) >= 0) /* add existing file */
9985 addfile(gap, buf, flags);
9986 }
9987 }
9988
9989#ifdef WIN3264
9990# ifdef FEAT_MBYTE
9991 if (wn != NULL)
9992 {
9993 vim_free(p);
9994 ok = FindNextFileW(hFind, &wfb);
9995 }
9996 else
9997# endif
9998 ok = FindNextFile(hFind, &fb);
9999#else
10000 ok = (findnext(&fb) == 0);
10001#endif
10002
10003 /* If no more matches and no match was used, try expanding the name
10004 * itself. Finds the long name of a short filename. */
10005 if (!ok && matchname != NULL && gap->ga_len == start_len)
10006 {
10007 STRCPY(s, matchname);
10008#ifdef WIN3264
10009 FindClose(hFind);
10010# ifdef FEAT_MBYTE
10011 if (wn != NULL)
10012 {
10013 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010014 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 if (wn != NULL)
10016 hFind = FindFirstFileW(wn, &wfb);
10017 }
10018 if (wn == NULL)
10019# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010020 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 ok = (hFind != INVALID_HANDLE_VALUE);
10022#else
10023 ok = (findfirst((char *)buf, &fb,
10024 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
10025#endif
10026 vim_free(matchname);
10027 matchname = NULL;
10028 }
10029 }
10030
10031#ifdef WIN3264
10032 FindClose(hFind);
10033# ifdef FEAT_MBYTE
10034 vim_free(wn);
10035# endif
10036#endif
10037 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010038 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 vim_free(matchname);
10040
10041 matches = gap->ga_len - start_len;
10042 if (matches > 0)
10043 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10044 sizeof(char_u *), pstrcmp);
10045 return matches;
10046}
10047
10048 int
10049mch_expandpath(
10050 garray_T *gap,
10051 char_u *path,
10052 int flags) /* EW_* flags */
10053{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010054 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010056# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
Bram Moolenaar231334e2005-07-25 20:46:57 +000010058#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10059 || defined(PROTO)
10060/*
10061 * Unix style wildcard expansion code.
10062 * It's here because it's used both for Unix and Mac.
10063 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010064static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010065
10066 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010067pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010068{
10069 return (pathcmp(*(char **)a, *(char **)b, -1));
10070}
10071
10072/*
10073 * Recursively expand one path component into all matching files and/or
10074 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10075 * "path" has backslashes before chars that are not to be expanded, starting
10076 * at "path + wildoff".
10077 * Return the number of matches found.
10078 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10079 */
10080 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010081unix_expandpath(
10082 garray_T *gap,
10083 char_u *path,
10084 int wildoff,
10085 int flags, /* EW_* flags */
10086 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010087{
10088 char_u *buf;
10089 char_u *path_end;
10090 char_u *p, *s, *e;
10091 int start_len = gap->ga_len;
10092 char_u *pat;
10093 regmatch_T regmatch;
10094 int starts_with_dot;
10095 int matches;
10096 int len;
10097 int starstar = FALSE;
10098 static int stardepth = 0; /* depth for "**" expansion */
10099
10100 DIR *dirp;
10101 struct dirent *dp;
10102
10103 /* Expanding "**" may take a long time, check for CTRL-C. */
10104 if (stardepth > 0)
10105 {
10106 ui_breakcheck();
10107 if (got_int)
10108 return 0;
10109 }
10110
10111 /* make room for file name */
10112 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10113 if (buf == NULL)
10114 return 0;
10115
10116 /*
10117 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010118 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010119 * Copy it into "buf", including the preceding characters.
10120 */
10121 p = buf;
10122 s = buf;
10123 e = NULL;
10124 path_end = path;
10125 while (*path_end != NUL)
10126 {
10127 /* May ignore a wildcard that has a backslash before it; it will
10128 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10129 if (path_end >= path + wildoff && rem_backslash(path_end))
10130 *p++ = *path_end++;
10131 else if (*path_end == '/')
10132 {
10133 if (e != NULL)
10134 break;
10135 s = p + 1;
10136 }
10137 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010138 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010139 || (!p_fic && (flags & EW_ICASE)
10140 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010141 e = p;
10142#ifdef FEAT_MBYTE
10143 if (has_mbyte)
10144 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010145 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010146 STRNCPY(p, path_end, len);
10147 p += len;
10148 path_end += len;
10149 }
10150 else
10151#endif
10152 *p++ = *path_end++;
10153 }
10154 e = p;
10155 *e = NUL;
10156
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010157 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010158 /* Remove backslashes between "wildoff" and the start of the wildcard
10159 * component. */
10160 for (p = buf + wildoff; p < s; ++p)
10161 if (rem_backslash(p))
10162 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010163 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010164 --e;
10165 --s;
10166 }
10167
10168 /* Check for "**" between "s" and "e". */
10169 for (p = s; p < e; ++p)
10170 if (p[0] == '*' && p[1] == '*')
10171 starstar = TRUE;
10172
10173 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010174 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010175 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10176 if (pat == NULL)
10177 {
10178 vim_free(buf);
10179 return 0;
10180 }
10181
10182 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010183 if (flags & EW_ICASE)
10184 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10185 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010186 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010187 if (flags & (EW_NOERROR | EW_NOTWILD))
10188 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010189 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010190 if (flags & (EW_NOERROR | EW_NOTWILD))
10191 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010192 vim_free(pat);
10193
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010194 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010195 {
10196 vim_free(buf);
10197 return 0;
10198 }
10199
10200 /* If "**" is by itself, this is the first time we encounter it and more
10201 * is following then find matches without any directory. */
10202 if (!didstar && stardepth < 100 && starstar && e - s == 2
10203 && *path_end == '/')
10204 {
10205 STRCPY(s, path_end + 1);
10206 ++stardepth;
10207 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10208 --stardepth;
10209 }
10210
10211 /* open the directory for scanning */
10212 *s = NUL;
10213 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10214
10215 /* Find all matching entries */
10216 if (dirp != NULL)
10217 {
10218 for (;;)
10219 {
10220 dp = readdir(dirp);
10221 if (dp == NULL)
10222 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010223 if ((dp->d_name[0] != '.' || starts_with_dot
10224 || ((flags & EW_DODOT)
10225 && dp->d_name[1] != NUL
10226 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010227 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10228 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010229 || ((flags & EW_NOTWILD)
10230 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010231 {
10232 STRCPY(s, dp->d_name);
10233 len = STRLEN(buf);
10234
10235 if (starstar && stardepth < 100)
10236 {
10237 /* For "**" in the pattern first go deeper in the tree to
10238 * find matches. */
10239 STRCPY(buf + len, "/**");
10240 STRCPY(buf + len + 3, path_end);
10241 ++stardepth;
10242 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10243 --stardepth;
10244 }
10245
10246 STRCPY(buf + len, path_end);
10247 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10248 {
10249 /* need to expand another component of the path */
10250 /* remove backslashes for the remaining components only */
10251 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10252 }
10253 else
10254 {
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010255 struct stat sb;
10256
Bram Moolenaar231334e2005-07-25 20:46:57 +000010257 /* no more wildcards, check if there is a match */
10258 /* remove backslashes for the remaining components only */
10259 if (*path_end != NUL)
10260 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010261 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010262 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010263 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010264 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010265#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010266 size_t precomp_len = STRLEN(buf)+1;
10267 char_u *precomp_buf =
10268 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010269
Bram Moolenaar231334e2005-07-25 20:46:57 +000010270 if (precomp_buf)
10271 {
10272 mch_memmove(buf, precomp_buf, precomp_len);
10273 vim_free(precomp_buf);
10274 }
10275#endif
10276 addfile(gap, buf, flags);
10277 }
10278 }
10279 }
10280 }
10281
10282 closedir(dirp);
10283 }
10284
10285 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010286 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010287
10288 matches = gap->ga_len - start_len;
10289 if (matches > 0)
10290 qsort(((char_u **)gap->ga_data) + start_len, matches,
10291 sizeof(char_u *), pstrcmp);
10292 return matches;
10293}
10294#endif
10295
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010296#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010297static int find_previous_pathsep(char_u *path, char_u **psep);
10298static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10299static void expand_path_option(char_u *curdir, garray_T *gap);
10300static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10301static void uniquefy_paths(garray_T *gap, char_u *pattern);
10302static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010303
10304/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010305 * Moves "*psep" back to the previous path separator in "path".
10306 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010307 */
10308 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010309find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010310{
10311 /* skip the current separator */
10312 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010313 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010314
10315 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010316 while (*psep > path)
10317 {
10318 if (vim_ispathsep(**psep))
10319 return OK;
10320 mb_ptr_back(path, *psep);
10321 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010322
10323 return FAIL;
10324}
10325
10326/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010327 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10328 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010329 */
10330 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010331is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010332{
10333 int j;
10334 int candidate_len;
10335 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010336 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010337 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010338
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010339 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010340 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010341 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010342 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010343
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010344 candidate_len = (int)STRLEN(maybe_unique);
10345 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010346 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010347 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010348
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010349 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010350 if (fnamecmp(maybe_unique, rival) == 0
10351 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010352 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010353 }
10354
Bram Moolenaar162bd912010-07-28 22:29:10 +020010355 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010356}
10357
10358/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010359 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010360 * paths are expanded to their equivalent fullpath. This includes the "."
10361 * (relative to current buffer directory) and empty path (relative to current
10362 * directory) notations.
10363 *
10364 * TODO: handle upward search (;) and path limiter (**N) notations by
10365 * expanding each into their equivalent path(s).
10366 */
10367 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010368expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010369{
10370 char_u *path_option = *curbuf->b_p_path == NUL
10371 ? p_path : curbuf->b_p_path;
10372 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010373 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010374 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010375
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010376 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010377 return;
10378
10379 while (*path_option != NUL)
10380 {
10381 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10382
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010383 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010384 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010385 /* Relative to current buffer:
10386 * "/path/file" + "." -> "/path/"
10387 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010388 if (curbuf->b_ffname == NULL)
10389 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010390 p = gettail(curbuf->b_ffname);
10391 len = (int)(p - curbuf->b_ffname);
10392 if (len + (int)STRLEN(buf) >= MAXPATHL)
10393 continue;
10394 if (buf[1] == NUL)
10395 buf[len] = NUL;
10396 else
10397 STRMOVE(buf + len, buf + 2);
10398 mch_memmove(buf, curbuf->b_ffname, len);
10399 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010400 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010401 else if (buf[0] == NUL)
10402 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010403 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010404 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010405 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010406 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010407 else if (!mch_isFullName(buf))
10408 {
10409 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010410 len = (int)STRLEN(curdir);
10411 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010412 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010413 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010414 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010415 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010416 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010417 }
10418
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010419 if (ga_grow(gap, 1) == FAIL)
10420 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010421
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010422# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010423 /* Avoid the path ending in a backslash, it fails when a comma is
10424 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010425 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010426 if (buf[len - 1] == '\\')
10427 buf[len - 1] = '/';
10428# endif
10429
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010430 p = vim_strsave(buf);
10431 if (p == NULL)
10432 break;
10433 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010434 }
10435
10436 vim_free(buf);
10437}
10438
10439/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010440 * Returns a pointer to the file or directory name in "fname" that matches the
10441 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010442 *
10443 * path: /foo/bar/baz
10444 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010445 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010446 */
10447 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010448get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010449{
10450 int i;
10451 int maxlen = 0;
10452 char_u **path_part = (char_u **)gap->ga_data;
10453 char_u *cutoff = NULL;
10454
10455 for (i = 0; i < gap->ga_len; i++)
10456 {
10457 int j = 0;
10458
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010459 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010460# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010461 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10462#endif
10463 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010464 j++;
10465 if (j > maxlen)
10466 {
10467 maxlen = j;
10468 cutoff = &fname[j];
10469 }
10470 }
10471
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010472 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010473 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010474 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010475 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010476
10477 return cutoff;
10478}
10479
10480/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010481 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10482 * that they are unique with respect to each other while conserving the part
10483 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010484 */
10485 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010486uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010487{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010488 int i;
10489 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010490 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010491 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010492 char_u *pat;
10493 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010494 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010495 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010496 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010497 char_u **in_curdir = NULL;
10498 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010499
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010500 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010501 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010502
10503 /*
10504 * We need to prepend a '*' at the beginning of file_pattern so that the
10505 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010506 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010507 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010508 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010509 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010510 if (file_pattern == NULL)
10511 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010512 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010513 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010514 STRCAT(file_pattern, pattern);
10515 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10516 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010517 if (pat == NULL)
10518 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010519
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010520 regmatch.rm_ic = TRUE; /* always ignore case */
10521 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10522 vim_free(pat);
10523 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010524 return;
10525
Bram Moolenaar162bd912010-07-28 22:29:10 +020010526 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010527 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010528 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010529 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010530
10531 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010532 if (in_curdir == NULL)
10533 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010534
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010535 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010536 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010537 char_u *path = fnames[i];
10538 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010539 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010540 char_u *pathsep_p;
10541 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010542
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010543 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010544 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010545 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010546 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010547 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010548
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010549 /* Shorten the filename while maintaining its uniqueness */
10550 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010551
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010552 /* we start at the end of the path */
10553 pathsep_p = path + len - 1;
10554
10555 while (find_previous_pathsep(path, &pathsep_p))
10556 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10557 && is_unique(pathsep_p + 1, gap, i)
10558 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10559 {
10560 sort_again = TRUE;
10561 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10562 break;
10563 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010564
10565 if (mch_isFullName(path))
10566 {
10567 /*
10568 * Last resort: shorten relative to curdir if possible.
10569 * 'possible' means:
10570 * 1. It is under the current directory.
10571 * 2. The result is actually shorter than the original.
10572 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010573 * Before curdir After
10574 * /foo/bar/file.txt /foo/bar ./file.txt
10575 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10576 * /file.txt / /file.txt
10577 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010578 */
10579 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010580 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010581#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010582 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010583 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010584 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010585 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010586 && !vim_ispathsep(*short_name)
10587#endif
10588 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010589 {
10590 STRCPY(path, ".");
10591 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010592 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010593 }
10594 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010595 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010596 }
10597
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010598 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010599 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010600 {
10601 char_u *rel_path;
10602 char_u *path = in_curdir[i];
10603
10604 if (path == NULL)
10605 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010606
10607 /* If the {filename} is not unique, change it to ./{filename}.
10608 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010609 short_name = shorten_fname(path, curdir);
10610 if (short_name == NULL)
10611 short_name = path;
10612 if (is_unique(short_name, gap, i))
10613 {
10614 STRCPY(fnames[i], short_name);
10615 continue;
10616 }
10617
10618 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10619 if (rel_path == NULL)
10620 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010621 STRCPY(rel_path, ".");
10622 add_pathsep(rel_path);
10623 STRCAT(rel_path, short_name);
10624
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010625 vim_free(fnames[i]);
10626 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010627 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010628 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010629 }
10630
Bram Moolenaar162bd912010-07-28 22:29:10 +020010631theend:
10632 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010633 if (in_curdir != NULL)
10634 {
10635 for (i = 0; i < gap->ga_len; i++)
10636 vim_free(in_curdir[i]);
10637 vim_free(in_curdir);
10638 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010640 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010641
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010642 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010643 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644}
10645
10646/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010647 * Calls globpath() with 'path' values for the given pattern and stores the
10648 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010649 * Returns the total number of matches.
10650 */
10651 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010652expand_in_path(
10653 garray_T *gap,
10654 char_u *pattern,
10655 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010656{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010657 char_u *curdir;
10658 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010659 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010660
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010661 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010662 return 0;
10663 mch_dirname(curdir, MAXPATHL);
10664
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010665 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010666 expand_path_option(curdir, &path_ga);
10667 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010668 if (path_ga.ga_len == 0)
10669 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010670
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010671 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010672 ga_clear_strings(&path_ga);
10673 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010674 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010675
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010676 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010677 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010678
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010679 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010680}
10681#endif
10682
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010683#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10684/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010685 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10686 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010687 */
10688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010689remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010690{
10691 int i;
10692 int j;
10693 char_u **fnames = (char_u **)gap->ga_data;
10694
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010695 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010696 for (i = gap->ga_len - 1; i > 0; --i)
10697 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10698 {
10699 vim_free(fnames[i]);
10700 for (j = i + 1; j < gap->ga_len; ++j)
10701 fnames[j - 1] = fnames[j];
10702 --gap->ga_len;
10703 }
10704}
10705#endif
10706
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010707static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010708
10709/*
10710 * Return TRUE if "p" contains what looks like an environment variable.
10711 * Allowing for escaping.
10712 */
10713 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010714has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010715{
10716 for ( ; *p; mb_ptr_adv(p))
10717 {
10718 if (*p == '\\' && p[1] != NUL)
10719 ++p;
10720 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010721#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010722 "$%"
10723#else
10724 "$"
10725#endif
10726 , *p) != NULL)
10727 return TRUE;
10728 }
10729 return FALSE;
10730}
10731
10732#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010733static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010734
10735/*
10736 * Return TRUE if "p" contains a special wildcard character.
10737 * Allowing for escaping.
10738 */
10739 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010740has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010741{
10742 for ( ; *p; mb_ptr_adv(p))
10743 {
10744 if (*p == '\\' && p[1] != NUL)
10745 ++p;
10746 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10747 return TRUE;
10748 }
10749 return FALSE;
10750}
10751#endif
10752
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753/*
10754 * Generic wildcard expansion code.
10755 *
10756 * Characters in "pat" that should not be expanded must be preceded with a
10757 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10758 *
10759 * Return FAIL when no single file was found. In this case "num_file" is not
10760 * set, and "file" may contain an error message.
10761 * Return OK when some files found. "num_file" is set to the number of
10762 * matches, "file" to the array of matches. Call FreeWild() later.
10763 */
10764 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010765gen_expand_wildcards(
10766 int num_pat, /* number of input patterns */
10767 char_u **pat, /* array of input patterns */
10768 int *num_file, /* resulting number of files */
10769 char_u ***file, /* array of resulting files */
10770 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771{
10772 int i;
10773 garray_T ga;
10774 char_u *p;
10775 static int recursive = FALSE;
10776 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010777 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010778#if defined(FEAT_SEARCHPATH)
10779 int did_expand_in_path = FALSE;
10780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781
10782 /*
10783 * expand_env() is called to expand things like "~user". If this fails,
10784 * it calls ExpandOne(), which brings us back here. In this case, always
10785 * call the machine specific expansion function, if possible. Otherwise,
10786 * return FAIL.
10787 */
10788 if (recursive)
10789#ifdef SPECIAL_WILDCHAR
10790 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10791#else
10792 return FAIL;
10793#endif
10794
10795#ifdef SPECIAL_WILDCHAR
10796 /*
10797 * If there are any special wildcard characters which we cannot handle
10798 * here, call machine specific function for all the expansion. This
10799 * avoids starting the shell for each argument separately.
10800 * For `=expr` do use the internal function.
10801 */
10802 for (i = 0; i < num_pat; i++)
10803 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010804 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805# ifdef VIM_BACKTICK
10806 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10807# endif
10808 )
10809 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10810 }
10811#endif
10812
10813 recursive = TRUE;
10814
10815 /*
10816 * The matching file names are stored in a growarray. Init it empty.
10817 */
10818 ga_init2(&ga, (int)sizeof(char_u *), 30);
10819
10820 for (i = 0; i < num_pat; ++i)
10821 {
10822 add_pat = -1;
10823 p = pat[i];
10824
10825#ifdef VIM_BACKTICK
10826 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010829 if (add_pat == -1)
10830 retval = FAIL;
10831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 else
10833#endif
10834 {
10835 /*
10836 * First expand environment variables, "~/" and "~user/".
10837 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010838 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010840 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 if (p == NULL)
10842 p = pat[i];
10843#ifdef UNIX
10844 /*
10845 * On Unix, if expand_env() can't expand an environment
10846 * variable, use the shell to do that. Discard previously
10847 * found file names and start all over again.
10848 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010849 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 {
10851 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010852 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010854 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 recursive = FALSE;
10856 return i;
10857 }
10858#endif
10859 }
10860
10861 /*
10862 * If there are wildcards: Expand file names and add each match to
10863 * the list. If there is no match, and EW_NOTFOUND is given, add
10864 * the pattern.
10865 * If there are no wildcards: Add the file name if it exists or
10866 * when EW_NOTFOUND is given.
10867 */
10868 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010869 {
10870#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010871 if ((flags & EW_PATH)
10872 && !mch_isFullName(p)
10873 && !(p[0] == '.'
10874 && (vim_ispathsep(p[1])
10875 || (p[1] == '.' && vim_ispathsep(p[2]))))
10876 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010877 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010878 /* :find completion where 'path' is used.
10879 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010880 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010881 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010882 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010883 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010884 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010885 else
10886#endif
10887 add_pat = mch_expandpath(&ga, p, flags);
10888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 }
10890
10891 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10892 {
10893 char_u *t = backslash_halve_save(p);
10894
10895#if defined(MACOS_CLASSIC)
10896 slash_to_colon(t);
10897#endif
10898 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10899 * "vim c:/" work. */
10900 if (flags & EW_NOTFOUND)
10901 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10902 else if (mch_getperm(t) >= 0)
10903 addfile(&ga, t, flags);
10904 vim_free(t);
10905 }
10906
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010907#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010908 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010909 uniquefy_paths(&ga, p);
10910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 if (p != pat[i])
10912 vim_free(p);
10913 }
10914
10915 *num_file = ga.ga_len;
10916 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10917
10918 recursive = FALSE;
10919
Bram Moolenaar336bd622016-01-17 18:23:58 +010010920 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921}
10922
10923# ifdef VIM_BACKTICK
10924
10925/*
10926 * Return TRUE if we can expand this backtick thing here.
10927 */
10928 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010929vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930{
10931 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10932}
10933
10934/*
10935 * Expand an item in `backticks` by executing it as a command.
10936 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020010937 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 */
10939 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010940expand_backtick(
10941 garray_T *gap,
10942 char_u *pat,
10943 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944{
10945 char_u *p;
10946 char_u *cmd;
10947 char_u *buffer;
10948 int cnt = 0;
10949 int i;
10950
10951 /* Create the command: lop off the backticks. */
10952 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10953 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010954 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955
10956#ifdef FEAT_EVAL
10957 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010958 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 else
10960#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010961 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010962 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 vim_free(cmd);
10964 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010965 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966
10967 cmd = buffer;
10968 while (*cmd != NUL)
10969 {
10970 cmd = skipwhite(cmd); /* skip over white space */
10971 p = cmd;
10972 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10973 ++p;
10974 /* add an entry if it is not empty */
10975 if (p > cmd)
10976 {
10977 i = *p;
10978 *p = NUL;
10979 addfile(gap, cmd, flags);
10980 *p = i;
10981 ++cnt;
10982 }
10983 cmd = p;
10984 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10985 ++cmd;
10986 }
10987
10988 vim_free(buffer);
10989 return cnt;
10990}
10991# endif /* VIM_BACKTICK */
10992
10993/*
10994 * Add a file to a file list. Accepted flags:
10995 * EW_DIR add directories
10996 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010997 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 * EW_NOTFOUND add even when it doesn't exist
10999 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011000 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 */
11002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011003addfile(
11004 garray_T *gap,
11005 char_u *f, /* filename */
11006 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007{
11008 char_u *p;
11009 int isdir;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011010 struct stat sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011012 /* if the file/dir/link doesn't exist, may not add it */
11013 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011014 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 return;
11016
11017#ifdef FNAME_ILLEGAL
11018 /* if the file/dir contains illegal characters, don't add it */
11019 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11020 return;
11021#endif
11022
11023 isdir = mch_isdir(f);
11024 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11025 return;
11026
Bram Moolenaarb5971142015-03-21 17:32:19 +010011027 /* If the file isn't executable, may not add it. Do accept directories.
11028 * When invoked from expand_shellcmd() do not use $PATH. */
11029 if (!isdir && (flags & EW_EXEC)
11030 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011031 return;
11032
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 /* Make room for another item in the file list. */
11034 if (ga_grow(gap, 1) == FAIL)
11035 return;
11036
11037 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11038 if (p == NULL)
11039 return;
11040
11041 STRCPY(p, f);
11042#ifdef BACKSLASH_IN_FILENAME
11043 slash_adjust(p);
11044#endif
11045 /*
11046 * Append a slash or backslash after directory names if none is present.
11047 */
11048#ifndef DONT_ADD_PATHSEP_TO_DIR
11049 if (isdir && (flags & EW_ADDSLASH))
11050 add_pathsep(p);
11051#endif
11052 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053}
11054#endif /* !NO_EXPANDPATH */
11055
11056#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11057
11058#ifndef SEEK_SET
11059# define SEEK_SET 0
11060#endif
11061#ifndef SEEK_END
11062# define SEEK_END 2
11063#endif
11064
11065/*
11066 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011067 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11068 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 * Returns an allocated string, or NULL for error.
11070 */
11071 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011072get_cmd_output(
11073 char_u *cmd,
11074 char_u *infile, /* optional input file name */
11075 int flags, /* can be SHELL_SILENT */
11076 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077{
11078 char_u *tempname;
11079 char_u *command;
11080 char_u *buffer = NULL;
11081 int len;
11082 int i = 0;
11083 FILE *fd;
11084
11085 if (check_restricted() || check_secure())
11086 return NULL;
11087
11088 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011089 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 {
11091 EMSG(_(e_notmp));
11092 return NULL;
11093 }
11094
11095 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011096 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 if (command == NULL)
11098 goto done;
11099
11100 /*
11101 * Call the shell to execute the command (errors are ignored).
11102 * Don't check timestamps here.
11103 */
11104 ++no_check_timestamps;
11105 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11106 --no_check_timestamps;
11107
11108 vim_free(command);
11109
11110 /*
11111 * read the names from the file into memory
11112 */
11113# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011114 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 fd = mch_fopen((char *)tempname, "r");
11116# else
11117 fd = mch_fopen((char *)tempname, READBIN);
11118# endif
11119
11120 if (fd == NULL)
11121 {
11122 EMSG2(_(e_notopen), tempname);
11123 goto done;
11124 }
11125
11126 fseek(fd, 0L, SEEK_END);
11127 len = ftell(fd); /* get size of temp file */
11128 fseek(fd, 0L, SEEK_SET);
11129
11130 buffer = alloc(len + 1);
11131 if (buffer != NULL)
11132 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11133 fclose(fd);
11134 mch_remove(tempname);
11135 if (buffer == NULL)
11136 goto done;
11137#ifdef VMS
11138 len = i; /* VMS doesn't give us what we asked for... */
11139#endif
11140 if (i != len)
11141 {
11142 EMSG2(_(e_notread), tempname);
11143 vim_free(buffer);
11144 buffer = NULL;
11145 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011146 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011147 {
11148 /* Change NUL into SOH, otherwise the string is truncated. */
11149 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011150 if (buffer[i] == NUL)
11151 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011152
Bram Moolenaar162bd912010-07-28 22:29:10 +020011153 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011154 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011155 else
11156 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157
11158done:
11159 vim_free(tempname);
11160 return buffer;
11161}
11162#endif
11163
11164/*
11165 * Free the list of files returned by expand_wildcards() or other expansion
11166 * functions.
11167 */
11168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011169FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011171 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 return;
11173#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
11174 /*
11175 * Is this still OK for when other functions than expand_wildcards() have
11176 * been used???
11177 */
11178 _fnexplodefree((char **)files);
11179#else
11180 while (count--)
11181 vim_free(files[count]);
11182 vim_free(files);
11183#endif
11184}
11185
11186/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011187 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 * Don't do this when still processing a command or a mapping.
11189 * Don't do this when inside a ":normal" command.
11190 */
11191 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011192goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
11194 return (p_im && stuff_empty() && typebuf_typed());
11195}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011196
11197/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011198 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011199 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11200 * - Remove any argument. E.g., "csh -f" -> "csh".
11201 * But don't allow a space in the path, so that this works:
11202 * "/usr/bin/csh --rcfile ~/.cshrc"
11203 * But don't do that for Windows, it's common to have a space in the path.
11204 */
11205 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011206get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011207{
11208 char_u *p;
11209
11210#ifdef WIN3264
11211 p = gettail(p_sh);
11212 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11213#else
11214 p = skiptowhite(p_sh);
11215 if (*p == NUL)
11216 {
11217 /* No white space, use the tail. */
11218 p = vim_strsave(gettail(p_sh));
11219 }
11220 else
11221 {
11222 char_u *p1, *p2;
11223
11224 /* Find the last path separator before the space. */
11225 p1 = p_sh;
11226 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11227 if (vim_ispathsep(*p2))
11228 p1 = p2 + 1;
11229 p = vim_strnsave(p1, (int)(p - p1));
11230 }
11231#endif
11232 return p;
11233}