blob: b9bd99716fa382ea43f61e1052dc33a95cd1fad6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001200 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001201 {
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
Bram Moolenaar82faa252016-06-04 20:14:07 +02001428 * with markers.
1429 * Skip mark_adjust when adding a line after the last one, there can't
1430 * be marks there. */
1431 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count)
1432 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 did_append = TRUE;
1434 }
1435#ifdef FEAT_VREPLACE
1436 else
1437 {
1438 /*
1439 * In VREPLACE mode we are starting to replace the next line.
1440 */
1441 curwin->w_cursor.lnum++;
1442 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1443 {
1444 /* In case we NL to a new line, BS to the previous one, and NL
1445 * again, we don't want to save the new line for undo twice.
1446 */
1447 (void)u_save_cursor(); /* errors are ignored! */
1448 vr_lines_changed++;
1449 }
1450 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1451 changed_bytes(curwin->w_cursor.lnum, 0);
1452 curwin->w_cursor.lnum--;
1453 did_append = FALSE;
1454 }
1455#endif
1456
1457 if (newindent
1458#ifdef FEAT_SMARTINDENT
1459 || did_si
1460#endif
1461 )
1462 {
1463 ++curwin->w_cursor.lnum;
1464#ifdef FEAT_SMARTINDENT
1465 if (did_si)
1466 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001467 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001470 newindent -= newindent % sw;
1471 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001474 /* Copy the indent */
1475 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 (void)copy_indent(newindent, saved_line);
1478
1479 /*
1480 * Set the 'preserveindent' option so that any further screwing
1481 * with the line doesn't entirely destroy our efforts to preserve
1482 * it. It gets restored at the function end.
1483 */
1484 curbuf->b_p_pi = TRUE;
1485 }
1486 else
1487 (void)set_indent(newindent, SIN_INSERT);
1488 less_cols -= curwin->w_cursor.col;
1489
1490 ai_col = curwin->w_cursor.col;
1491
1492 /*
1493 * In REPLACE mode, for each character in the new indent, there must
1494 * be a NUL on the replace stack, for when it is deleted with BS
1495 */
1496 if (REPLACE_NORMAL(State))
1497 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1498 replace_push(NUL);
1499 newcol += curwin->w_cursor.col;
1500#ifdef FEAT_SMARTINDENT
1501 if (no_si)
1502 did_si = FALSE;
1503#endif
1504 }
1505
1506#ifdef FEAT_COMMENTS
1507 /*
1508 * In REPLACE mode, for each character in the extra leader, there must be
1509 * a NUL on the replace stack, for when it is deleted with BS.
1510 */
1511 if (REPLACE_NORMAL(State))
1512 while (lead_len-- > 0)
1513 replace_push(NUL);
1514#endif
1515
1516 curwin->w_cursor = old_cursor;
1517
1518 if (dir == FORWARD)
1519 {
1520 if (trunc_line || (State & INSERT))
1521 {
1522 /* truncate current line at cursor */
1523 saved_line[curwin->w_cursor.col] = NUL;
1524 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1525 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1526 truncate_spaces(saved_line);
1527 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1528 saved_line = NULL;
1529 if (did_append)
1530 {
1531 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1532 curwin->w_cursor.lnum + 1, 1L);
1533 did_append = FALSE;
1534
1535 /* Move marks after the line break to the new line. */
1536 if (flags & OPENLINE_MARKFIX)
1537 mark_col_adjust(curwin->w_cursor.lnum,
1538 curwin->w_cursor.col + less_cols_off,
1539 1L, (long)-less_cols);
1540 }
1541 else
1542 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1543 }
1544
1545 /*
1546 * Put the cursor on the new line. Careful: the scrollup() above may
1547 * have moved w_cursor, we must use old_cursor.
1548 */
1549 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1550 }
1551 if (did_append)
1552 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1553
1554 curwin->w_cursor.col = newcol;
1555#ifdef FEAT_VIRTUALEDIT
1556 curwin->w_cursor.coladd = 0;
1557#endif
1558
1559#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1560 /*
1561 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1562 * fixthisline() from doing it (via change_indent()) by telling it we're in
1563 * normal INSERT mode.
1564 */
1565 if (State & VREPLACE_FLAG)
1566 {
1567 vreplace_mode = State; /* So we know to put things right later */
1568 State = INSERT;
1569 }
1570 else
1571 vreplace_mode = 0;
1572#endif
1573#ifdef FEAT_LISP
1574 /*
1575 * May do lisp indenting.
1576 */
1577 if (!p_paste
1578# ifdef FEAT_COMMENTS
1579 && leader == NULL
1580# endif
1581 && curbuf->b_p_lisp
1582 && curbuf->b_p_ai)
1583 {
1584 fixthisline(get_lisp_indent);
1585 p = ml_get_curline();
1586 ai_col = (colnr_T)(skipwhite(p) - p);
1587 }
1588#endif
1589#ifdef FEAT_CINDENT
1590 /*
1591 * May do indenting after opening a new line.
1592 */
1593 if (!p_paste
1594 && (curbuf->b_p_cin
1595# ifdef FEAT_EVAL
1596 || *curbuf->b_p_inde != NUL
1597# endif
1598 )
1599 && in_cinkeys(dir == FORWARD
1600 ? KEY_OPEN_FORW
1601 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1602 {
1603 do_c_expr_indent();
1604 p = ml_get_curline();
1605 ai_col = (colnr_T)(skipwhite(p) - p);
1606 }
1607#endif
1608#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1609 if (vreplace_mode != 0)
1610 State = vreplace_mode;
1611#endif
1612
1613#ifdef FEAT_VREPLACE
1614 /*
1615 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1616 * original line, and inserts the new stuff char by char, pushing old stuff
1617 * onto the replace stack (via ins_char()).
1618 */
1619 if (State & VREPLACE_FLAG)
1620 {
1621 /* Put new line in p_extra */
1622 p_extra = vim_strsave(ml_get_curline());
1623 if (p_extra == NULL)
1624 goto theend;
1625
1626 /* Put back original line */
1627 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1628
1629 /* Insert new stuff into line again */
1630 curwin->w_cursor.col = 0;
1631#ifdef FEAT_VIRTUALEDIT
1632 curwin->w_cursor.coladd = 0;
1633#endif
1634 ins_bytes(p_extra); /* will call changed_bytes() */
1635 vim_free(p_extra);
1636 next_line = NULL;
1637 }
1638#endif
1639
1640 retval = TRUE; /* success! */
1641theend:
1642 curbuf->b_p_pi = saved_pi;
1643 vim_free(saved_line);
1644 vim_free(next_line);
1645 vim_free(allocated);
1646 return retval;
1647}
1648
1649#if defined(FEAT_COMMENTS) || defined(PROTO)
1650/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001651 * get_leader_len() returns the length in bytes of the prefix of the given
1652 * string which introduces a comment. If this string is not a comment then
1653 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * When "flags" is not NULL, it is set to point to the flags of the recognized
1655 * comment leader.
1656 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001657 * If "include_space" is set, include trailing whitespace while calculating the
1658 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
1660 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001661get_leader_len(
1662 char_u *line,
1663 char_u **flags,
1664 int backward,
1665 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001668 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int got_com = FALSE;
1670 int found_one;
1671 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1672 char_u *string; /* pointer to comment string */
1673 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001674 int middle_match_len = 0;
1675 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001676 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaar81340392012-06-06 16:12:59 +02001678 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 while (vim_iswhite(line[i])) /* leading white space is ignored */
1680 ++i;
1681
1682 /*
1683 * Repeat to match several nested comment strings.
1684 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001685 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 /*
1688 * scan through the 'comments' option for a match
1689 */
1690 found_one = FALSE;
1691 for (list = curbuf->b_p_com; *list; )
1692 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001693 /* Get one option part into part_buf[]. Advance "list" to next
1694 * one. Put "string" at start of string. */
1695 if (!got_com && flags != NULL)
1696 *flags = list; /* remember where flags started */
1697 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1699 string = vim_strchr(part_buf, ':');
1700 if (string == NULL) /* missing ':', ignore this part */
1701 continue;
1702 *string++ = NUL; /* isolate flags from string */
1703
Bram Moolenaara4271d52011-05-10 13:38:27 +02001704 /* If we found a middle match previously, use that match when this
1705 * is not a middle or end. */
1706 if (middle_match_len != 0
1707 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1708 && vim_strchr(part_buf, COM_END) == NULL)
1709 break;
1710
1711 /* When we already found a nested comment, only accept further
1712 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1714 continue;
1715
Bram Moolenaara4271d52011-05-10 13:38:27 +02001716 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1718 continue;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 * When string starts with white space, must have some white space
1722 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001723 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (vim_iswhite(string[0]))
1725 {
1726 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001727 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 while (vim_iswhite(string[0]))
1729 ++string;
1730 }
1731 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1732 ;
1733 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001734 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaara4271d52011-05-10 13:38:27 +02001736 /* When 'b' flag used, there must be white space or an
1737 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (vim_strchr(part_buf, COM_BLANK) != NULL
1739 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1740 continue;
1741
Bram Moolenaara4271d52011-05-10 13:38:27 +02001742 /* We have found a match, stop searching unless this is a middle
1743 * comment. The middle comment can be a substring of the end
1744 * comment in which case it's better to return the length of the
1745 * end comment and its flags. Thus we keep searching with middle
1746 * and end matches and use an end match if it matches better. */
1747 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1748 {
1749 if (middle_match_len == 0)
1750 {
1751 middle_match_len = j;
1752 saved_flags = prev_list;
1753 }
1754 continue;
1755 }
1756 if (middle_match_len != 0 && j > middle_match_len)
1757 /* Use this match instead of the middle match, since it's a
1758 * longer thus better match. */
1759 middle_match_len = 0;
1760
1761 if (middle_match_len == 0)
1762 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 found_one = TRUE;
1764 break;
1765 }
1766
Bram Moolenaara4271d52011-05-10 13:38:27 +02001767 if (middle_match_len != 0)
1768 {
1769 /* Use the previously found middle match after failing to find a
1770 * match with an end. */
1771 if (!got_com && flags != NULL)
1772 *flags = saved_flags;
1773 i += middle_match_len;
1774 found_one = TRUE;
1775 }
1776
1777 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (!found_one)
1779 break;
1780
Bram Moolenaar81340392012-06-06 16:12:59 +02001781 result = i;
1782
Bram Moolenaara4271d52011-05-10 13:38:27 +02001783 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 while (vim_iswhite(line[i]))
1785 ++i;
1786
Bram Moolenaar81340392012-06-06 16:12:59 +02001787 if (include_space)
1788 result = i;
1789
Bram Moolenaara4271d52011-05-10 13:38:27 +02001790 /* If this comment doesn't nest, stop here. */
1791 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (vim_strchr(part_buf, COM_NEST) == NULL)
1793 break;
1794 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001795 return result;
1796}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001797
Bram Moolenaar81340392012-06-06 16:12:59 +02001798/*
1799 * Return the offset at which the last comment in line starts. If there is no
1800 * comment in the whole line, -1 is returned.
1801 *
1802 * When "flags" is not null, it is set to point to the flags describing the
1803 * recognized comment leader.
1804 */
1805 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001806get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001807{
1808 int result = -1;
1809 int i, j;
1810 int lower_check_bound = 0;
1811 char_u *string;
1812 char_u *com_leader;
1813 char_u *com_flags;
1814 char_u *list;
1815 int found_one;
1816 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1817
1818 /*
1819 * Repeat to match several nested comment strings.
1820 */
1821 i = (int)STRLEN(line);
1822 while (--i >= lower_check_bound)
1823 {
1824 /*
1825 * scan through the 'comments' option for a match
1826 */
1827 found_one = FALSE;
1828 for (list = curbuf->b_p_com; *list; )
1829 {
1830 char_u *flags_save = list;
1831
1832 /*
1833 * Get one option part into part_buf[]. Advance list to next one.
1834 * put string at start of string.
1835 */
1836 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1837 string = vim_strchr(part_buf, ':');
1838 if (string == NULL) /* If everything is fine, this cannot actually
1839 * happen. */
1840 {
1841 continue;
1842 }
1843 *string++ = NUL; /* Isolate flags from string. */
1844 com_leader = string;
1845
1846 /*
1847 * Line contents and string must match.
1848 * When string starts with white space, must have some white space
1849 * (but the amount does not need to match, there might be a mix of
1850 * TABs and spaces).
1851 */
1852 if (vim_iswhite(string[0]))
1853 {
1854 if (i == 0 || !vim_iswhite(line[i - 1]))
1855 continue;
1856 while (vim_iswhite(string[0]))
1857 ++string;
1858 }
1859 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1860 /* do nothing */;
1861 if (string[j] != NUL)
1862 continue;
1863
1864 /*
1865 * When 'b' flag used, there must be white space or an
1866 * end-of-line after the string in the line.
1867 */
1868 if (vim_strchr(part_buf, COM_BLANK) != NULL
1869 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1870 {
1871 continue;
1872 }
1873
1874 /*
1875 * We have found a match, stop searching.
1876 */
1877 found_one = TRUE;
1878
1879 if (flags)
1880 *flags = flags_save;
1881 com_flags = flags_save;
1882
1883 break;
1884 }
1885
1886 if (found_one)
1887 {
1888 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1889 int len1, len2, off;
1890
1891 result = i;
1892 /*
1893 * If this comment nests, continue searching.
1894 */
1895 if (vim_strchr(part_buf, COM_NEST) != NULL)
1896 continue;
1897
1898 lower_check_bound = i;
1899
1900 /* Let's verify whether the comment leader found is a substring
1901 * of other comment leaders. If it is, let's adjust the
1902 * lower_check_bound so that we make sure that we have determined
1903 * the comment leader correctly.
1904 */
1905
1906 while (vim_iswhite(*com_leader))
1907 ++com_leader;
1908 len1 = (int)STRLEN(com_leader);
1909
1910 for (list = curbuf->b_p_com; *list; )
1911 {
1912 char_u *flags_save = list;
1913
1914 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1915 if (flags_save == com_flags)
1916 continue;
1917 string = vim_strchr(part_buf2, ':');
1918 ++string;
1919 while (vim_iswhite(*string))
1920 ++string;
1921 len2 = (int)STRLEN(string);
1922 if (len2 == 0)
1923 continue;
1924
1925 /* Now we have to verify whether string ends with a substring
1926 * beginning the com_leader. */
1927 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1928 {
1929 --off;
1930 if (!STRNCMP(string + off, com_leader, len2 - off))
1931 {
1932 if (i - off < lower_check_bound)
1933 lower_check_bound = i - off;
1934 }
1935 }
1936 }
1937 }
1938 }
1939 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940}
1941#endif
1942
1943/*
1944 * Return the number of window lines occupied by buffer line "lnum".
1945 */
1946 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001947plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 return plines_win(curwin, lnum, TRUE);
1950}
1951
1952 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001953plines_win(
1954 win_T *wp,
1955 linenr_T lnum,
1956 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958#if defined(FEAT_DIFF) || defined(PROTO)
1959 /* Check for filler lines above this buffer line. When folded the result
1960 * is one line anyway. */
1961 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1962}
1963
1964 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001965plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 return plines_win_nofill(curwin, lnum, TRUE);
1968}
1969
1970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971plines_win_nofill(
1972 win_T *wp,
1973 linenr_T lnum,
1974 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976#endif
1977 int lines;
1978
1979 if (!wp->w_p_wrap)
1980 return 1;
1981
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001982#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (wp->w_width == 0)
1984 return 1;
1985#endif
1986
1987#ifdef FEAT_FOLDING
1988 /* A folded lines is handled just like an empty line. */
1989 /* NOTE: Caller must handle lines that are MAYBE folded. */
1990 if (lineFolded(wp, lnum) == TRUE)
1991 return 1;
1992#endif
1993
1994 lines = plines_win_nofold(wp, lnum);
1995 if (winheight > 0 && lines > wp->w_height)
1996 return (int)wp->w_height;
1997 return lines;
1998}
1999
2000/*
2001 * Return number of window lines physical line "lnum" will occupy in window
2002 * "wp". Does not care about folding, 'wrap' or 'diff'.
2003 */
2004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002005plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 char_u *s;
2008 long col;
2009 int width;
2010
2011 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2012 if (*s == NUL) /* empty line */
2013 return 1;
2014 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2015
2016 /*
2017 * If list mode is on, then the '$' at the end of the line may take up one
2018 * extra column.
2019 */
2020 if (wp->w_p_list && lcs_eol != NUL)
2021 col += 1;
2022
2023 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002024 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 */
2026 width = W_WIDTH(wp) - win_col_off(wp);
2027 if (width <= 0)
2028 return 32000;
2029 if (col <= width)
2030 return 1;
2031 col -= width;
2032 width += win_col_off2(wp);
2033 return (col + (width - 1)) / width + 1;
2034}
2035
2036/*
2037 * Like plines_win(), but only reports the number of physical screen lines
2038 * used from the start of the line to the given column number.
2039 */
2040 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002041plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
2043 long col;
2044 char_u *s;
2045 int lines = 0;
2046 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002047 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049#ifdef FEAT_DIFF
2050 /* Check for filler lines above this buffer line. When folded the result
2051 * is one line anyway. */
2052 lines = diff_check_fill(wp, lnum);
2053#endif
2054
2055 if (!wp->w_p_wrap)
2056 return lines + 1;
2057
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002058#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (wp->w_width == 0)
2060 return lines + 1;
2061#endif
2062
Bram Moolenaar597a4222014-06-25 14:39:50 +02002063 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 col = 0;
2066 while (*s != NUL && --column >= 0)
2067 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002068 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002069 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071
2072 /*
2073 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2074 * INSERT mode, then col must be adjusted so that it represents the last
2075 * screen position of the TAB. This only fixes an error when the TAB wraps
2076 * from one screen line to the next (when 'columns' is not a multiple of
2077 * 'ts') -- webb.
2078 */
2079 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002080 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002083 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 */
2085 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002086 if (width <= 0)
2087 return 9999;
2088
2089 lines += 1;
2090 if (col > width)
2091 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2092 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093}
2094
2095 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002096plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 int count = 0;
2099
2100 while (first <= last)
2101 {
2102#ifdef FEAT_FOLDING
2103 int x;
2104
2105 /* Check if there are any really folded lines, but also included lines
2106 * that are maybe folded. */
2107 x = foldedCount(wp, first, NULL);
2108 if (x > 0)
2109 {
2110 ++count; /* count 1 for "+-- folded" line */
2111 first += x;
2112 }
2113 else
2114#endif
2115 {
2116#ifdef FEAT_DIFF
2117 if (first == wp->w_topline)
2118 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2119 else
2120#endif
2121 count += plines_win(wp, first, TRUE);
2122 ++first;
2123 }
2124 }
2125 return (count);
2126}
2127
2128#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2129/*
2130 * Insert string "p" at the cursor position. Stops at a NUL byte.
2131 * Handles Replace mode and multi-byte characters.
2132 */
2133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 ins_bytes_len(p, (int)STRLEN(p));
2137}
2138#endif
2139
2140#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2141 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2142/*
2143 * Insert string "p" with length "len" at the cursor position.
2144 * Handles Replace mode and multi-byte characters.
2145 */
2146 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 int i;
2150# ifdef FEAT_MBYTE
2151 int n;
2152
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002153 if (has_mbyte)
2154 for (i = 0; i < len; i += n)
2155 {
2156 if (enc_utf8)
2157 /* avoid reading past p[len] */
2158 n = utfc_ptr2len_len(p + i, len - i);
2159 else
2160 n = (*mb_ptr2len)(p + i);
2161 ins_char_bytes(p + i, n);
2162 }
2163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002165 for (i = 0; i < len; ++i)
2166 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167}
2168#endif
2169
2170/*
2171 * Insert or replace a single character at the cursor position.
2172 * When in REPLACE or VREPLACE mode, replace any existing character.
2173 * Caller must have prepared for undo.
2174 * For multi-byte characters we get the whole character, the caller must
2175 * convert bytes to a character.
2176 */
2177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002178ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002181 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 int n;
2183
2184 n = (*mb_char2bytes)(c, buf);
2185
2186 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2187 * Happens for CTRL-Vu9900. */
2188 if (buf[0] == 0)
2189 buf[0] = '\n';
2190
2191 ins_char_bytes(buf, n);
2192}
2193
2194 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002195ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196{
2197 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199 int newlen; /* nr of bytes inserted */
2200 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2201 char_u *p;
2202 char_u *newp;
2203 char_u *oldp;
2204 int linelen; /* length of old line including NUL */
2205 colnr_T col;
2206 linenr_T lnum = curwin->w_cursor.lnum;
2207 int i;
2208
2209#ifdef FEAT_VIRTUALEDIT
2210 /* Break tabs if needed. */
2211 if (virtual_active() && curwin->w_cursor.coladd > 0)
2212 coladvance_force(getviscol());
2213#endif
2214
2215 col = curwin->w_cursor.col;
2216 oldp = ml_get(lnum);
2217 linelen = (int)STRLEN(oldp) + 1;
2218
2219 /* The lengths default to the values for when not replacing. */
2220 oldlen = 0;
2221#ifdef FEAT_MBYTE
2222 newlen = charlen;
2223#else
2224 newlen = 1;
2225#endif
2226
2227 if (State & REPLACE_FLAG)
2228 {
2229#ifdef FEAT_VREPLACE
2230 if (State & VREPLACE_FLAG)
2231 {
2232 colnr_T new_vcol = 0; /* init for GCC */
2233 colnr_T vcol;
2234 int old_list;
2235#ifndef FEAT_MBYTE
2236 char_u buf[2];
2237#endif
2238
2239 /*
2240 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2241 * Returns the old value of list, so when finished,
2242 * curwin->w_p_list should be set back to this.
2243 */
2244 old_list = curwin->w_p_list;
2245 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2246 curwin->w_p_list = FALSE;
2247
2248 /*
2249 * In virtual replace mode each character may replace one or more
2250 * characters (zero if it's a TAB). Count the number of bytes to
2251 * be deleted to make room for the new character, counting screen
2252 * cells. May result in adding spaces to fill a gap.
2253 */
2254 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2255#ifndef FEAT_MBYTE
2256 buf[0] = c;
2257 buf[1] = NUL;
2258#endif
2259 new_vcol = vcol + chartabsize(buf, vcol);
2260 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2261 {
2262 vcol += chartabsize(oldp + col + oldlen, vcol);
2263 /* Don't need to remove a TAB that takes us to the right
2264 * position. */
2265 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2266 break;
2267#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002268 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#else
2270 ++oldlen;
2271#endif
2272 /* Deleted a bit too much, insert spaces. */
2273 if (vcol > new_vcol)
2274 newlen += vcol - new_vcol;
2275 }
2276 curwin->w_p_list = old_list;
2277 }
2278 else
2279#endif
2280 if (oldp[col] != NUL)
2281 {
2282 /* normal replace */
2283#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002284 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#else
2286 oldlen = 1;
2287#endif
2288 }
2289
2290
2291 /* Push the replaced bytes onto the replace stack, so that they can be
2292 * put back when BS is used. The bytes of a multi-byte character are
2293 * done the other way around, so that the first byte is popped off
2294 * first (it tells the byte length of the character). */
2295 replace_push(NUL);
2296 for (i = 0; i < oldlen; ++i)
2297 {
2298#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002299 if (has_mbyte)
2300 i += replace_push_mb(oldp + col + i) - 1;
2301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002303 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 }
2306
2307 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2308 if (newp == NULL)
2309 return;
2310
2311 /* Copy bytes before the cursor. */
2312 if (col > 0)
2313 mch_memmove(newp, oldp, (size_t)col);
2314
2315 /* Copy bytes after the changed character(s). */
2316 p = newp + col;
2317 mch_memmove(p + newlen, oldp + col + oldlen,
2318 (size_t)(linelen - col - oldlen));
2319
2320 /* Insert or overwrite the new character. */
2321#ifdef FEAT_MBYTE
2322 mch_memmove(p, buf, charlen);
2323 i = charlen;
2324#else
2325 *p = c;
2326 i = 1;
2327#endif
2328
2329 /* Fill with spaces when necessary. */
2330 while (i < newlen)
2331 p[i++] = ' ';
2332
2333 /* Replace the line in the buffer. */
2334 ml_replace(lnum, newp, FALSE);
2335
2336 /* mark the buffer as changed and prepare for displaying */
2337 changed_bytes(lnum, col);
2338
2339 /*
2340 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2341 * show the match for right parens and braces.
2342 */
2343 if (p_sm && (State & INSERT)
2344 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002345#ifdef FEAT_INS_EXPAND
2346 && !ins_compl_active()
2347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002349 {
2350#ifdef FEAT_MBYTE
2351 if (has_mbyte)
2352 showmatch(mb_ptr2char(buf));
2353 else
2354#endif
2355 showmatch(c);
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifdef FEAT_RIGHTLEFT
2359 if (!p_ri || (State & REPLACE_FLAG))
2360#endif
2361 {
2362 /* Normal insert: move cursor right */
2363#ifdef FEAT_MBYTE
2364 curwin->w_cursor.col += charlen;
2365#else
2366 ++curwin->w_cursor.col;
2367#endif
2368 }
2369 /*
2370 * TODO: should try to update w_row here, to avoid recomputing it later.
2371 */
2372}
2373
2374/*
2375 * Insert a string at the cursor position.
2376 * Note: Does NOT handle Replace mode.
2377 * Caller must have prepared for undo.
2378 */
2379 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002380ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 char_u *oldp, *newp;
2383 int newlen = (int)STRLEN(s);
2384 int oldlen;
2385 colnr_T col;
2386 linenr_T lnum = curwin->w_cursor.lnum;
2387
2388#ifdef FEAT_VIRTUALEDIT
2389 if (virtual_active() && curwin->w_cursor.coladd > 0)
2390 coladvance_force(getviscol());
2391#endif
2392
2393 col = curwin->w_cursor.col;
2394 oldp = ml_get(lnum);
2395 oldlen = (int)STRLEN(oldp);
2396
2397 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2398 if (newp == NULL)
2399 return;
2400 if (col > 0)
2401 mch_memmove(newp, oldp, (size_t)col);
2402 mch_memmove(newp + col, s, (size_t)newlen);
2403 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2404 ml_replace(lnum, newp, FALSE);
2405 changed_bytes(lnum, col);
2406 curwin->w_cursor.col += newlen;
2407}
2408
2409/*
2410 * Delete one character under the cursor.
2411 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2412 * Caller must have prepared for undo.
2413 *
2414 * return FAIL for failure, OK otherwise
2415 */
2416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419#ifdef FEAT_MBYTE
2420 if (has_mbyte)
2421 {
2422 /* Make sure the cursor is at the start of a character. */
2423 mb_adjust_cursor();
2424 if (*ml_get_cursor() == NUL)
2425 return FAIL;
2426 return del_chars(1L, fixpos);
2427 }
2428#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002429 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430}
2431
2432#if defined(FEAT_MBYTE) || defined(PROTO)
2433/*
2434 * Like del_bytes(), but delete characters instead of bytes.
2435 */
2436 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002437del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 long bytes = 0;
2440 long i;
2441 char_u *p;
2442 int l;
2443
2444 p = ml_get_cursor();
2445 for (i = 0; i < count && *p != NUL; ++i)
2446 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 bytes += l;
2449 p += l;
2450 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002451 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452}
2453#endif
2454
2455/*
2456 * Delete "count" bytes under the cursor.
2457 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2458 * Caller must have prepared for undo.
2459 *
2460 * return FAIL for failure, OK otherwise
2461 */
2462 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463del_bytes(
2464 long count,
2465 int fixpos_arg,
2466 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 char_u *oldp, *newp;
2469 colnr_T oldlen;
2470 linenr_T lnum = curwin->w_cursor.lnum;
2471 colnr_T col = curwin->w_cursor.col;
2472 int was_alloced;
2473 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002474 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 oldp = ml_get(lnum);
2477 oldlen = (int)STRLEN(oldp);
2478
2479 /*
2480 * Can't do anything when the cursor is on the NUL after the line.
2481 */
2482 if (col >= oldlen)
2483 return FAIL;
2484
2485#ifdef FEAT_MBYTE
2486 /* If 'delcombine' is set and deleting (less than) one character, only
2487 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002488 if (p_deco && use_delcombine && enc_utf8
2489 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002491 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 int n;
2493
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002494 (void)utfc_ptr2char(oldp + col, cc);
2495 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 /* Find the last composing char, there can be several. */
2498 n = col;
2499 do
2500 {
2501 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002502 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 n += count;
2504 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2505 fixpos = 0;
2506 }
2507 }
2508#endif
2509
2510 /*
2511 * When count is too big, reduce it.
2512 */
2513 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2514 if (movelen <= 1)
2515 {
2516 /*
2517 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002518 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2519 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002521 if (col > 0 && fixpos && restart_edit == 0
2522#ifdef FEAT_VIRTUALEDIT
2523 && (ve_flags & VE_ONEMORE) == 0
2524#endif
2525 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 --curwin->w_cursor.col;
2528#ifdef FEAT_VIRTUALEDIT
2529 curwin->w_cursor.coladd = 0;
2530#endif
2531#ifdef FEAT_MBYTE
2532 if (has_mbyte)
2533 curwin->w_cursor.col -=
2534 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2535#endif
2536 }
2537 count = oldlen - col;
2538 movelen = 1;
2539 }
2540
2541 /*
2542 * If the old line has been allocated the deletion can be done in the
2543 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002544 * Can't do this when using Netbeans, because we would need to invoke
2545 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002546 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002549 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002550 was_alloced = FALSE;
2551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002553 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (was_alloced)
2555 newp = oldp; /* use same allocated memory */
2556 else
2557 { /* need to allocate a new line */
2558 newp = alloc((unsigned)(oldlen + 1 - count));
2559 if (newp == NULL)
2560 return FAIL;
2561 mch_memmove(newp, oldp, (size_t)col);
2562 }
2563 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2564 if (!was_alloced)
2565 ml_replace(lnum, newp, FALSE);
2566
2567 /* mark the buffer as changed and prepare for displaying */
2568 changed_bytes(lnum, curwin->w_cursor.col);
2569
2570 return OK;
2571}
2572
2573/*
2574 * Delete from cursor to end of line.
2575 * Caller must have prepared for undo.
2576 *
2577 * return FAIL for failure, OK otherwise
2578 */
2579 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002580truncate_line(
2581 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 char_u *newp;
2584 linenr_T lnum = curwin->w_cursor.lnum;
2585 colnr_T col = curwin->w_cursor.col;
2586
2587 if (col == 0)
2588 newp = vim_strsave((char_u *)"");
2589 else
2590 newp = vim_strnsave(ml_get(lnum), col);
2591
2592 if (newp == NULL)
2593 return FAIL;
2594
2595 ml_replace(lnum, newp, FALSE);
2596
2597 /* mark the buffer as changed and prepare for displaying */
2598 changed_bytes(lnum, curwin->w_cursor.col);
2599
2600 /*
2601 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2602 */
2603 if (fixpos && curwin->w_cursor.col > 0)
2604 --curwin->w_cursor.col;
2605
2606 return OK;
2607}
2608
2609/*
2610 * Delete "nlines" lines at the cursor.
2611 * Saves the lines for undo first if "undo" is TRUE.
2612 */
2613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002614del_lines(
2615 long nlines, /* number of lines to delete */
2616 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002619 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621 if (nlines <= 0)
2622 return;
2623
2624 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002625 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 return;
2627
2628 for (n = 0; n < nlines; )
2629 {
2630 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2631 break;
2632
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002633 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 ++n;
2635
2636 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002637 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 break;
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002641 /* Correct the cursor position before calling deleted_lines_mark(), it may
2642 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 curwin->w_cursor.col = 0;
2644 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002645
2646 /* adjust marks, mark the buffer as changed and prepare for displaying */
2647 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648}
2649
2650 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002651gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
2653 char_u *ptr = ml_get_pos(pos);
2654
2655#ifdef FEAT_MBYTE
2656 if (has_mbyte)
2657 return (*mb_ptr2char)(ptr);
2658#endif
2659 return (int)*ptr;
2660}
2661
2662 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002663gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665#ifdef FEAT_MBYTE
2666 if (has_mbyte)
2667 return (*mb_ptr2char)(ml_get_cursor());
2668#endif
2669 return (int)*ml_get_cursor();
2670}
2671
2672/*
2673 * Write a character at the current cursor position.
2674 * It is directly written into the block.
2675 */
2676 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002677pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2680 + curwin->w_cursor.col) = c;
2681}
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * When extra == 0: Return TRUE if the cursor is before or on the first
2685 * non-blank in the line.
2686 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2687 * the line.
2688 */
2689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 char_u *ptr;
2693 colnr_T col;
2694
2695 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2696 ++ptr;
2697 if (col >= curwin->w_cursor.col + extra)
2698 return TRUE;
2699 else
2700 return FALSE;
2701}
2702
2703/*
2704 * Skip to next part of an option argument: Skip space and comma.
2705 */
2706 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002707skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 if (*p == ',')
2710 ++p;
2711 while (*p == ' ')
2712 ++p;
2713 return p;
2714}
2715
2716/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002717 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 *
2719 * Most often called through changed_bytes() and changed_lines(), which also
2720 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002721 *
2722 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
2724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002725changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2728 /* The text of the preediting area is inserted, but this doesn't
2729 * mean a change of the buffer yet. That is delayed until the
2730 * text is committed. (this means preedit becomes empty) */
2731 if (im_is_preediting() && !xim_changed_while_preediting)
2732 return;
2733 xim_changed_while_preediting = FALSE;
2734#endif
2735
2736 if (!curbuf->b_changed)
2737 {
2738 int save_msg_scroll = msg_scroll;
2739
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002740 /* Give a warning about changing a read-only file. This may also
2741 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002743
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 /* Create a swap file if that is wanted.
2745 * Don't do this for "nofile" and "nowrite" buffer types. */
2746 if (curbuf->b_may_swap
2747#ifdef FEAT_QUICKFIX
2748 && !bt_dontwrite(curbuf)
2749#endif
2750 )
2751 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002752 int save_need_wait_return = need_wait_return;
2753
2754 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 ml_open_file(curbuf);
2756
2757 /* The ml_open_file() can cause an ATTENTION message.
2758 * Wait two seconds, to make sure the user reads this unexpected
2759 * message. Since we could be anywhere, call wait_return() now,
2760 * and don't let the emsg() set msg_scroll. */
2761 if (need_wait_return && emsg_silent == 0)
2762 {
2763 out_flush();
2764 ui_delay(2000L, TRUE);
2765 wait_return(TRUE);
2766 msg_scroll = save_msg_scroll;
2767 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002768 else
2769 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002771 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774}
2775
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002776/*
2777 * Internal part of changed(), no user interaction.
2778 */
2779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002780changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002781{
2782 curbuf->b_changed = TRUE;
2783 ml_setflags(curbuf);
2784#ifdef FEAT_WINDOWS
2785 check_status(curbuf);
2786 redraw_tabline = TRUE;
2787#endif
2788#ifdef FEAT_TITLE
2789 need_maketitle = TRUE; /* set window title later */
2790#endif
2791}
2792
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002793static void changedOneline(buf_T *buf, linenr_T lnum);
2794static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2795static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
2797/*
2798 * Changed bytes within a single line for the current buffer.
2799 * - marks the windows on this buffer to be redisplayed
2800 * - marks the buffer changed by calling changed()
2801 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002802 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
2804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002805changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002807 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002809
2810#ifdef FEAT_DIFF
2811 /* Diff highlighting in other diff windows may need to be updated too. */
2812 if (curwin->w_p_diff)
2813 {
2814 win_T *wp;
2815 linenr_T wlnum;
2816
Bram Moolenaar29323592016-07-24 22:04:11 +02002817 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002818 if (wp->w_p_diff && wp != curwin)
2819 {
2820 redraw_win_later(wp, VALID);
2821 wlnum = diff_lnum_win(lnum, wp);
2822 if (wlnum > 0)
2823 changedOneline(wp->w_buffer, wlnum);
2824 }
2825 }
2826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827}
2828
2829 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002830changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002832 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002835 if (lnum < buf->b_mod_top)
2836 buf->b_mod_top = lnum;
2837 else if (lnum >= buf->b_mod_bot)
2838 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840 else
2841 {
2842 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002843 buf->b_mod_set = TRUE;
2844 buf->b_mod_top = lnum;
2845 buf->b_mod_bot = lnum + 1;
2846 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848}
2849
2850/*
2851 * Appended "count" lines below line "lnum" in the current buffer.
2852 * Must be called AFTER the change and after mark_adjust().
2853 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2854 */
2855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002856appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 changed_lines(lnum + 1, 0, lnum + 1, count);
2859}
2860
2861/*
2862 * Like appended_lines(), but adjust marks first.
2863 */
2864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002865appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002867 /* Skip mark_adjust when adding a line after the last one, there can't
2868 * be marks there. */
2869 if (lnum + count < curbuf->b_ml.ml_line_count)
2870 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 changed_lines(lnum + 1, 0, lnum + 1, count);
2872}
2873
2874/*
2875 * Deleted "count" lines at line "lnum" in the current buffer.
2876 * Must be called AFTER the change and after mark_adjust().
2877 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2878 */
2879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002880deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 changed_lines(lnum, 0, lnum + count, -count);
2883}
2884
2885/*
2886 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002887 * Make sure the cursor is on a valid line before calling, a GUI callback may
2888 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 */
2890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002891deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2894 changed_lines(lnum, 0, lnum + count, -count);
2895}
2896
2897/*
2898 * Changed lines for the current buffer.
2899 * Must be called AFTER the change and after mark_adjust().
2900 * - mark the buffer changed by calling changed()
2901 * - mark the windows on this buffer to be redisplayed
2902 * - invalidate cached values
2903 * "lnum" is the first line that needs displaying, "lnume" the first line
2904 * below the changed lines (BEFORE the change).
2905 * When only inserting lines, "lnum" and "lnume" are equal.
2906 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002907 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 */
2909 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002910changed_lines(
2911 linenr_T lnum, /* first line with change */
2912 colnr_T col, /* column in first line with change */
2913 linenr_T lnume, /* line below last changed line */
2914 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002916 changed_lines_buf(curbuf, lnum, lnume, xtra);
2917
2918#ifdef FEAT_DIFF
2919 if (xtra == 0 && curwin->w_p_diff)
2920 {
2921 /* When the number of lines doesn't change then mark_adjust() isn't
2922 * called and other diff buffers still need to be marked for
2923 * displaying. */
2924 win_T *wp;
2925 linenr_T wlnum;
2926
Bram Moolenaar29323592016-07-24 22:04:11 +02002927 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002928 if (wp->w_p_diff && wp != curwin)
2929 {
2930 redraw_win_later(wp, VALID);
2931 wlnum = diff_lnum_win(lnum, wp);
2932 if (wlnum > 0)
2933 changed_lines_buf(wp->w_buffer, wlnum,
2934 lnume - lnum + wlnum, 0L);
2935 }
2936 }
2937#endif
2938
2939 changed_common(lnum, col, lnume, xtra);
2940}
2941
2942 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002943changed_lines_buf(
2944 buf_T *buf,
2945 linenr_T lnum, /* first line with change */
2946 linenr_T lnume, /* line below last changed line */
2947 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002948{
2949 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
2951 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002952 if (lnum < buf->b_mod_top)
2953 buf->b_mod_top = lnum;
2954 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
2956 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002957 buf->b_mod_bot += xtra;
2958 if (buf->b_mod_bot < lnum)
2959 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002961 if (lnume + xtra > buf->b_mod_bot)
2962 buf->b_mod_bot = lnume + xtra;
2963 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
2965 else
2966 {
2967 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002968 buf->b_mod_set = TRUE;
2969 buf->b_mod_top = lnum;
2970 buf->b_mod_bot = lnume + xtra;
2971 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973}
2974
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002975/*
2976 * Common code for when a change is was made.
2977 * See changed_lines() for the arguments.
2978 * Careful: may trigger autocommands that reload the buffer.
2979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002981changed_common(
2982 linenr_T lnum,
2983 colnr_T col,
2984 linenr_T lnume,
2985 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002988#ifdef FEAT_WINDOWS
2989 tabpage_T *tp;
2990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 int i;
2992#ifdef FEAT_JUMPLIST
2993 int cols;
2994 pos_T *p;
2995 int add;
2996#endif
2997
2998 /* mark the buffer as modified */
2999 changed();
3000
3001 /* set the '. mark */
3002 if (!cmdmod.keepjumps)
3003 {
3004 curbuf->b_last_change.lnum = lnum;
3005 curbuf->b_last_change.col = col;
3006
3007#ifdef FEAT_JUMPLIST
3008 /* Create a new entry if a new undo-able change was started or we
3009 * don't have an entry yet. */
3010 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3011 {
3012 if (curbuf->b_changelistlen == 0)
3013 add = TRUE;
3014 else
3015 {
3016 /* Don't create a new entry when the line number is the same
3017 * as the last one and the column is not too far away. Avoids
3018 * creating many entries for typing "xxxxx". */
3019 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3020 if (p->lnum != lnum)
3021 add = TRUE;
3022 else
3023 {
3024 cols = comp_textwidth(FALSE);
3025 if (cols == 0)
3026 cols = 79;
3027 add = (p->col + cols < col || col + cols < p->col);
3028 }
3029 }
3030 if (add)
3031 {
3032 /* This is the first of a new sequence of undo-able changes
3033 * and it's at some distance of the last change. Use a new
3034 * position in the changelist. */
3035 curbuf->b_new_change = FALSE;
3036
3037 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3038 {
3039 /* changelist is full: remove oldest entry */
3040 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3041 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3042 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003043 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
3045 /* Correct position in changelist for other windows on
3046 * this buffer. */
3047 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3048 --wp->w_changelistidx;
3049 }
3050 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003051 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
3053 /* For other windows, if the position in the changelist is
3054 * at the end it stays at the end. */
3055 if (wp->w_buffer == curbuf
3056 && wp->w_changelistidx == curbuf->b_changelistlen)
3057 ++wp->w_changelistidx;
3058 }
3059 ++curbuf->b_changelistlen;
3060 }
3061 }
3062 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3063 curbuf->b_last_change;
3064 /* The current window is always after the last change, so that "g,"
3065 * takes you back to it. */
3066 curwin->w_changelistidx = curbuf->b_changelistlen;
3067#endif
3068 }
3069
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003070 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 if (wp->w_buffer == curbuf)
3073 {
3074 /* Mark this window to be redrawn later. */
3075 if (wp->w_redr_type < VALID)
3076 wp->w_redr_type = VALID;
3077
3078 /* Check if a change in the buffer has invalidated the cached
3079 * values for the cursor. */
3080#ifdef FEAT_FOLDING
3081 /*
3082 * Update the folds for this window. Can't postpone this, because
3083 * a following operator might work on the whole fold: ">>dd".
3084 */
3085 foldUpdate(wp, lnum, lnume + xtra - 1);
3086
3087 /* The change may cause lines above or below the change to become
3088 * included in a fold. Set lnum/lnume to the first/last line that
3089 * might be displayed differently.
3090 * Set w_cline_folded here as an efficient way to update it when
3091 * inserting lines just above a closed fold. */
3092 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3093 if (wp->w_cursor.lnum == lnum)
3094 wp->w_cline_folded = i;
3095 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3096 if (wp->w_cursor.lnum == lnume)
3097 wp->w_cline_folded = i;
3098
3099 /* If the changed line is in a range of previously folded lines,
3100 * compare with the first line in that range. */
3101 if (wp->w_cursor.lnum <= lnum)
3102 {
3103 i = find_wl_entry(wp, lnum);
3104 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3105 changed_line_abv_curs_win(wp);
3106 }
3107#endif
3108
3109 if (wp->w_cursor.lnum > lnum)
3110 changed_line_abv_curs_win(wp);
3111 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3112 changed_cline_bef_curs_win(wp);
3113 if (wp->w_botline >= lnum)
3114 {
3115 /* Assume that botline doesn't change (inserted lines make
3116 * other lines scroll down below botline). */
3117 approximate_botline_win(wp);
3118 }
3119
3120 /* Check if any w_lines[] entries have become invalid.
3121 * For entries below the change: Correct the lnums for
3122 * inserted/deleted lines. Makes it possible to stop displaying
3123 * after the change. */
3124 for (i = 0; i < wp->w_lines_valid; ++i)
3125 if (wp->w_lines[i].wl_valid)
3126 {
3127 if (wp->w_lines[i].wl_lnum >= lnum)
3128 {
3129 if (wp->w_lines[i].wl_lnum < lnume)
3130 {
3131 /* line included in change */
3132 wp->w_lines[i].wl_valid = FALSE;
3133 }
3134 else if (xtra != 0)
3135 {
3136 /* line below change */
3137 wp->w_lines[i].wl_lnum += xtra;
3138#ifdef FEAT_FOLDING
3139 wp->w_lines[i].wl_lastlnum += xtra;
3140#endif
3141 }
3142 }
3143#ifdef FEAT_FOLDING
3144 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3145 {
3146 /* change somewhere inside this range of folded lines,
3147 * may need to be redrawn */
3148 wp->w_lines[i].wl_valid = FALSE;
3149 }
3150#endif
3151 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003152
3153#ifdef FEAT_FOLDING
3154 /* Take care of side effects for setting w_topline when folds have
3155 * changed. Esp. when the buffer was changed in another window. */
3156 if (hasAnyFolding(wp))
3157 set_topline(wp, wp->w_topline);
3158#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003159 /* relative numbering may require updating more */
3160 if (wp->w_p_rnu)
3161 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163 }
3164
3165 /* Call update_screen() later, which checks out what needs to be redrawn,
3166 * since it notices b_mod_set and then uses b_mod_*. */
3167 if (must_redraw < VALID)
3168 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003169
3170#ifdef FEAT_AUTOCMD
3171 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003172 if (lnum <= curwin->w_cursor.lnum
3173 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003174 last_cursormoved.lnum = 0;
3175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
3177
3178/*
3179 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3180 */
3181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003182unchanged(
3183 buf_T *buf,
3184 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003186 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003189 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (ff)
3191 save_file_ff(buf);
3192#ifdef FEAT_WINDOWS
3193 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003194 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
3196#ifdef FEAT_TITLE
3197 need_maketitle = TRUE; /* set window title later */
3198#endif
3199 }
3200 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#ifdef FEAT_NETBEANS_INTG
3202 netbeans_unmodified(buf);
3203#endif
3204}
3205
3206#if defined(FEAT_WINDOWS) || defined(PROTO)
3207/*
3208 * check_status: called when the status bars for the buffer 'buf'
3209 * need to be updated
3210 */
3211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003212check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
3214 win_T *wp;
3215
Bram Moolenaar29323592016-07-24 22:04:11 +02003216 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (wp->w_buffer == buf && wp->w_status_height)
3218 {
3219 wp->w_redr_status = TRUE;
3220 if (must_redraw < VALID)
3221 must_redraw = VALID;
3222 }
3223}
3224#endif
3225
3226/*
3227 * If the file is readonly, give a warning message with the first change.
3228 * Don't do this for autocommands.
3229 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003230 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003232 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 */
3234 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003235change_warning(
3236 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 mode and 'showmode' is on */
3238{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003239 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 if (curbuf->b_did_warn == FALSE
3242 && curbufIsChanged() == 0
3243#ifdef FEAT_AUTOCMD
3244 && !autocmd_busy
3245#endif
3246 && curbuf->b_p_ro)
3247 {
3248#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003249 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003251 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (!curbuf->b_p_ro)
3253 return;
3254#endif
3255 /*
3256 * Do what msg() does, but with a column offset if the warning should
3257 * be after the mode message.
3258 */
3259 msg_start();
3260 if (msg_row == Rows - 1)
3261 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003262 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003263 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3264#ifdef FEAT_EVAL
3265 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 msg_clr_eos();
3268 (void)msg_end();
3269 if (msg_silent == 0 && !silent_mode)
3270 {
3271 out_flush();
3272 ui_delay(1000L, TRUE); /* give the user time to think about it */
3273 }
3274 curbuf->b_did_warn = TRUE;
3275 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3276 if (msg_row < Rows - 1)
3277 showmode();
3278 }
3279}
3280
3281/*
3282 * Ask for a reply from the user, a 'y' or a 'n'.
3283 * No other characters are accepted, the message is repeated until a valid
3284 * reply is entered or CTRL-C is hit.
3285 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3286 * from any buffers but directly from the user.
3287 *
3288 * return the 'y' or 'n'
3289 */
3290 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003291ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 int r = ' ';
3294 int save_State = State;
3295
3296 if (exiting) /* put terminal in raw mode for this question */
3297 settmode(TMODE_RAW);
3298 ++no_wait_return;
3299#ifdef USE_ON_FLY_SCROLL
3300 dont_scroll = TRUE; /* disallow scrolling here */
3301#endif
3302 State = CONFIRM; /* mouse behaves like with :confirm */
3303#ifdef FEAT_MOUSE
3304 setmouse(); /* disables mouse for xterm */
3305#endif
3306 ++no_mapping;
3307 ++allow_keys; /* no mapping here, but recognize keys */
3308
3309 while (r != 'y' && r != 'n')
3310 {
3311 /* same highlighting as for wait_return */
3312 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3313 if (direct)
3314 r = get_keystroke();
3315 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003316 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (r == Ctrl_C || r == ESC)
3318 r = 'n';
3319 msg_putchar(r); /* show what you typed */
3320 out_flush();
3321 }
3322 --no_wait_return;
3323 State = save_State;
3324#ifdef FEAT_MOUSE
3325 setmouse();
3326#endif
3327 --no_mapping;
3328 --allow_keys;
3329
3330 return r;
3331}
3332
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003333#if defined(FEAT_MOUSE) || defined(PROTO)
3334/*
3335 * Return TRUE if "c" is a mouse key.
3336 */
3337 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003338is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003339{
3340 return c == K_LEFTMOUSE
3341 || c == K_LEFTMOUSE_NM
3342 || c == K_LEFTDRAG
3343 || c == K_LEFTRELEASE
3344 || c == K_LEFTRELEASE_NM
3345 || c == K_MIDDLEMOUSE
3346 || c == K_MIDDLEDRAG
3347 || c == K_MIDDLERELEASE
3348 || c == K_RIGHTMOUSE
3349 || c == K_RIGHTDRAG
3350 || c == K_RIGHTRELEASE
3351 || c == K_MOUSEDOWN
3352 || c == K_MOUSEUP
3353 || c == K_MOUSELEFT
3354 || c == K_MOUSERIGHT
3355 || c == K_X1MOUSE
3356 || c == K_X1DRAG
3357 || c == K_X1RELEASE
3358 || c == K_X2MOUSE
3359 || c == K_X2DRAG
3360 || c == K_X2RELEASE;
3361}
3362#endif
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364/*
3365 * Get a key stroke directly from the user.
3366 * Ignores mouse clicks and scrollbar events, except a click for the left
3367 * button (used at the more prompt).
3368 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3369 * Disadvantage: typeahead is ignored.
3370 * Translates the interrupt character for unix to ESC.
3371 */
3372 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003373get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003375 char_u *buf = NULL;
3376 int buflen = 150;
3377 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 int len = 0;
3379 int n;
3380 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003381 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
3383 mapped_ctrl_c = FALSE; /* mappings are not used here */
3384 for (;;)
3385 {
3386 cursor_on();
3387 out_flush();
3388
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003389 /* Leave some room for check_termcode() to insert a key code into (max
3390 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3391 * bytes. */
3392 maxlen = (buflen - 6 - len) / 3;
3393 if (buf == NULL)
3394 buf = alloc(buflen);
3395 else if (maxlen < 10)
3396 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003397 char_u *t_buf = buf;
3398
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003399 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003400 * escape sequence. */
3401 buflen += 100;
3402 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003403 if (buf == NULL)
3404 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003405 maxlen = (buflen - 6 - len) / 3;
3406 }
3407 if (buf == NULL)
3408 {
3409 do_outofmem_msg((long_u)buflen);
3410 return ESC; /* panic! */
3411 }
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003414 * terminal code to complete. */
3415 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (n > 0)
3417 {
3418 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003419 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003421 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003423 else if (len > 0)
3424 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaar4395a712006-09-05 18:57:57 +00003426 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003427 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003428 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003430
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003431 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003432 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003433 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003434 {
3435 /* Redrawing was postponed, do it now. */
3436 update_screen(0);
3437 setcursor(); /* put cursor back where it belongs */
3438 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003439 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003440 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003441 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003443 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 continue;
3445
3446 /* Handle modifier and/or special key code. */
3447 n = buf[0];
3448 if (n == K_SPECIAL)
3449 {
3450 n = TO_SPECIAL(buf[1], buf[2]);
3451 if (buf[1] == KS_MODIFIER
3452 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003453#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003454 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003455#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003456#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 || n == K_VER_SCROLLBAR
3458 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459#endif
3460 )
3461 {
3462 if (buf[1] == KS_MODIFIER)
3463 mod_mask = buf[2];
3464 len -= 3;
3465 if (len > 0)
3466 mch_memmove(buf, buf + 3, (size_t)len);
3467 continue;
3468 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003469 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471#ifdef FEAT_MBYTE
3472 if (has_mbyte)
3473 {
3474 if (MB_BYTE2LEN(n) > len)
3475 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003476 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 n = (*mb_ptr2char)(buf);
3478 }
3479#endif
3480#ifdef UNIX
3481 if (n == intr_char)
3482 n = ESC;
3483#endif
3484 break;
3485 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003486 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 mapped_ctrl_c = save_mapped_ctrl_c;
3489 return n;
3490}
3491
3492/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003493 * Get a number from the user.
3494 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 */
3496 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003497get_number(
3498 int colon, /* allow colon to abort */
3499 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 int n = 0;
3502 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003503 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003505 if (mouse_used != NULL)
3506 *mouse_used = FALSE;
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 /* When not printing messages, the user won't know what to type, return a
3509 * zero (as if CR was hit). */
3510 if (msg_silent != 0)
3511 return 0;
3512
3513#ifdef USE_ON_FLY_SCROLL
3514 dont_scroll = TRUE; /* disallow scrolling here */
3515#endif
3516 ++no_mapping;
3517 ++allow_keys; /* no mapping here, but recognize keys */
3518 for (;;)
3519 {
3520 windgoto(msg_row, msg_col);
3521 c = safe_vgetc();
3522 if (VIM_ISDIGIT(c))
3523 {
3524 n = n * 10 + c - '0';
3525 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003526 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3529 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003530 if (typed > 0)
3531 {
3532 MSG_PUTS("\b \b");
3533 --typed;
3534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003537#ifdef FEAT_MOUSE
3538 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3539 {
3540 *mouse_used = TRUE;
3541 n = mouse_row + 1;
3542 break;
3543 }
3544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else if (n == 0 && c == ':' && colon)
3546 {
3547 stuffcharReadbuff(':');
3548 if (!exmode_active)
3549 cmdline_row = msg_row;
3550 skip_redraw = TRUE; /* skip redraw once */
3551 do_redraw = FALSE;
3552 break;
3553 }
3554 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3555 break;
3556 }
3557 --no_mapping;
3558 --allow_keys;
3559 return n;
3560}
3561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003562/*
3563 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003564 * When "mouse_used" is not NULL allow using the mouse and in that case return
3565 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003566 */
3567 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003568prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003569{
3570 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003571 int save_cmdline_row;
3572 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003573
3574 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003575 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003576 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003577 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003578 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003579
Bram Moolenaar203335e2006-09-03 14:35:42 +00003580 /* Set the state such that text can be selected/copied/pasted and we still
3581 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003582 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003583 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003584 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003585 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003586
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003587 i = get_number(TRUE, mouse_used);
3588 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003589 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003590 /* don't call wait_return() now */
3591 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003592 cmdline_row = msg_row - 1;
3593 need_wait_return = FALSE;
3594 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003595 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003596 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003597 else
3598 cmdline_row = save_cmdline_row;
3599 State = save_State;
3600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 return i;
3602}
3603
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003605msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606{
3607 long pn;
3608
3609 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3611 return;
3612
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003613 /* We don't want to overwrite another important message, but do overwrite
3614 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3615 * then "put" reports the last action. */
3616 if (keep_msg != NULL && !keep_msg_more)
3617 return;
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (n > 0)
3620 pn = n;
3621 else
3622 pn = -n;
3623
3624 if (pn > p_report)
3625 {
3626 if (pn == 1)
3627 {
3628 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003629 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3630 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003632 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3633 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635 else
3636 {
3637 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003638 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3639 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003641 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3642 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003645 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (msg(msg_buf))
3647 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003648 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003649 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651 }
3652}
3653
3654/*
3655 * flush map and typeahead buffers and give a warning for an error
3656 */
3657 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003658beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
3660 if (emsg_silent == 0)
3661 {
3662 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003663 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665}
3666
3667/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003668 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 */
3670 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003671vim_beep(
3672 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 if (emsg_silent == 0)
3675 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003676 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3677 {
3678 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003680 /* While the GUI is starting up the termcap is set for the
3681 * GUI but the output still goes to a terminal. */
3682 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003684 )
Bram Moolenaar165bc692015-07-21 17:53:25 +02003685 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003687 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003689
3690 /* When 'verbose' is set and we are sourcing a script or executing a
3691 * function give the user a hint where the beep comes from. */
3692 if (vim_strchr(p_debug, 'e') != NULL)
3693 {
3694 msg_source(hl_attr(HLF_W));
3695 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698}
3699
3700/*
3701 * To get the "real" home directory:
3702 * - get value of $HOME
3703 * For Unix:
3704 * - go to that directory
3705 * - do mch_dirname() to get the real name of that directory.
3706 * This also works with mounts and links.
3707 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3708 */
3709static char_u *homedir = NULL;
3710
3711 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003712init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
3714 char_u *var;
3715
Bram Moolenaar05159a02005-02-26 23:04:13 +00003716 /* In case we are called a second time (when 'encoding' changes). */
3717 vim_free(homedir);
3718 homedir = NULL;
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720#ifdef VMS
3721 var = mch_getenv((char_u *)"SYS$LOGIN");
3722#else
3723 var = mch_getenv((char_u *)"HOME");
3724#endif
3725
3726 if (var != NULL && *var == NUL) /* empty is same as not set */
3727 var = NULL;
3728
3729#ifdef WIN3264
3730 /*
3731 * Weird but true: $HOME may contain an indirect reference to another
3732 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3733 * when $HOME is being set.
3734 */
3735 if (var != NULL && *var == '%')
3736 {
3737 char_u *p;
3738 char_u *exp;
3739
3740 p = vim_strchr(var + 1, '%');
3741 if (p != NULL)
3742 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003743 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 exp = mch_getenv(NameBuff);
3745 if (exp != NULL && *exp != NUL
3746 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3747 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003748 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 var = NameBuff;
3750 /* Also set $HOME, it's needed for _viminfo. */
3751 vim_setenv((char_u *)"HOME", NameBuff);
3752 }
3753 }
3754 }
3755
3756 /*
3757 * Typically, $HOME is not defined on Windows, unless the user has
3758 * specifically defined it for Vim's sake. However, on Windows NT
3759 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3760 * each user. Try constructing $HOME from these.
3761 */
3762 if (var == NULL)
3763 {
3764 char_u *homedrive, *homepath;
3765
3766 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3767 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003768 if (homepath == NULL || *homepath == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003769 homepath = (char_u *)"\\";
Bram Moolenaar6f977012010-01-06 17:53:38 +01003770 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3772 {
3773 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3774 if (NameBuff[0] != NUL)
3775 {
3776 var = NameBuff;
3777 /* Also set $HOME, it's needed for _viminfo. */
3778 vim_setenv((char_u *)"HOME", NameBuff);
3779 }
3780 }
3781 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003782
3783# if defined(FEAT_MBYTE)
3784 if (enc_utf8 && var != NULL)
3785 {
3786 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003787 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003788
3789 /* Convert from active codepage to UTF-8. Other conversions are
3790 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003791 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003792 if (pp != NULL)
3793 {
3794 homedir = pp;
3795 return;
3796 }
3797 }
3798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799#endif
3800
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003801#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 /*
3803 * Default home dir is C:/
3804 * Best assumption we can make in such a situation.
3805 */
3806 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003807 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808#endif
3809 if (var != NULL)
3810 {
3811#ifdef UNIX
3812 /*
3813 * Change to the directory and get the actual path. This resolves
3814 * links. Don't do it when we can't return.
3815 */
3816 if (mch_dirname(NameBuff, MAXPATHL) == OK
3817 && mch_chdir((char *)NameBuff) == 0)
3818 {
3819 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3820 var = IObuff;
3821 if (mch_chdir((char *)NameBuff) != 0)
3822 EMSG(_(e_prev_dir));
3823 }
3824#endif
3825 homedir = vim_strsave(var);
3826 }
3827}
3828
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003829#if defined(EXITFREE) || defined(PROTO)
3830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003831free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003832{
3833 vim_free(homedir);
3834}
Bram Moolenaar24305862012-08-15 14:05:05 +02003835
3836# ifdef FEAT_CMDL_COMPL
3837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003838free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003839{
3840 ga_clear_strings(&ga_users);
3841}
3842# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003843#endif
3844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003846 * Call expand_env() and store the result in an allocated string.
3847 * This is not very memory efficient, this expects the result to be freed
3848 * again soon.
3849 */
3850 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003851expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003852{
3853 return expand_env_save_opt(src, FALSE);
3854}
3855
3856/*
3857 * Idem, but when "one" is TRUE handle the string as one file name, only
3858 * expand "~" at the start.
3859 */
3860 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003861expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003862{
3863 char_u *p;
3864
3865 p = alloc(MAXPATHL);
3866 if (p != NULL)
3867 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3868 return p;
3869}
3870
3871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 * Expand environment variable with path name.
3873 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003874 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 * If anything fails no expansion is done and dst equals src.
3876 */
3877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003878expand_env(
3879 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3880 char_u *dst, /* where to put the result */
3881 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003883 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884}
3885
3886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003887expand_env_esc(
3888 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3889 char_u *dst, /* where to put the result */
3890 int dstlen, /* maximum length of the result */
3891 int esc, /* escape spaces in expanded variables */
3892 int one, /* "srcp" is one file name */
3893 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003895 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 char_u *tail;
3897 int c;
3898 char_u *var;
3899 int copy_char;
3900 int mustfree; /* var was allocated, need to free it later */
3901 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003902 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003904 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003905 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003906
3907 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 --dstlen; /* leave one char space for "\," */
3909 while (*src && dstlen > 0)
3910 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003911#ifdef FEAT_EVAL
3912 /* Skip over `=expr`. */
3913 if (src[0] == '`' && src[1] == '=')
3914 {
3915 size_t len;
3916
3917 var = src;
3918 src += 2;
3919 (void)skip_expr(&src);
3920 if (*src == '`')
3921 ++src;
3922 len = src - var;
3923 if (len > (size_t)dstlen)
3924 len = dstlen;
3925 vim_strncpy(dst, var, len);
3926 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003927 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003928 continue;
3929 }
3930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003932 if ((*src == '$'
3933#ifdef VMS
3934 && at_start
3935#endif
3936 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003937#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 || *src == '%'
3939#endif
3940 || (*src == '~' && at_start))
3941 {
3942 mustfree = FALSE;
3943
3944 /*
3945 * The variable name is copied into dst temporarily, because it may
3946 * be a string in read-only memory and a NUL needs to be appended.
3947 */
3948 if (*src != '~') /* environment var */
3949 {
3950 tail = src + 1;
3951 var = dst;
3952 c = dstlen - 1;
3953
3954#ifdef UNIX
3955 /* Unix has ${var-name} type environment vars */
3956 if (*tail == '{' && !vim_isIDc('{'))
3957 {
3958 tail++; /* ignore '{' */
3959 while (c-- > 0 && *tail && *tail != '}')
3960 *var++ = *tail++;
3961 }
3962 else
3963#endif
3964 {
3965 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003966#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 || (*src == '%' && *tail != '%')
3968#endif
3969 ))
3970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 }
3974
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003975#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976# ifdef UNIX
3977 if (src[1] == '{' && *tail != '}')
3978# else
3979 if (*src == '%' && *tail != '%')
3980# endif
3981 var = NULL;
3982 else
3983 {
3984# ifdef UNIX
3985 if (src[1] == '{')
3986# else
3987 if (*src == '%')
3988#endif
3989 ++tail;
3990#endif
3991 *var = NUL;
3992 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003993#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995#endif
3996 }
3997 /* home directory */
3998 else if ( src[1] == NUL
3999 || vim_ispathsep(src[1])
4000 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4001 {
4002 var = homedir;
4003 tail = src + 1;
4004 }
4005 else /* user directory */
4006 {
4007#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4008 /*
4009 * Copy ~user to dst[], so we can put a NUL after it.
4010 */
4011 tail = src;
4012 var = dst;
4013 c = dstlen - 1;
4014 while ( c-- > 0
4015 && *tail
4016 && vim_isfilec(*tail)
4017 && !vim_ispathsep(*tail))
4018 *var++ = *tail++;
4019 *var = NUL;
4020# ifdef UNIX
4021 /*
4022 * If the system supports getpwnam(), use it.
4023 * Otherwise, or if getpwnam() fails, the shell is used to
4024 * expand ~user. This is slower and may fail if the shell
4025 * does not support ~user (old versions of /bin/sh).
4026 */
4027# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4028 {
4029 struct passwd *pw;
4030
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004031 /* Note: memory allocated by getpwnam() is never freed.
4032 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 pw = getpwnam((char *)dst + 1);
4034 if (pw != NULL)
4035 var = (char_u *)pw->pw_dir;
4036 else
4037 var = NULL;
4038 }
4039 if (var == NULL)
4040# endif
4041 {
4042 expand_T xpc;
4043
4044 ExpandInit(&xpc);
4045 xpc.xp_context = EXPAND_FILES;
4046 var = ExpandOne(&xpc, dst, NULL,
4047 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 mustfree = TRUE;
4049 }
4050
4051# else /* !UNIX, thus VMS */
4052 /*
4053 * USER_HOME is a comma-separated list of
4054 * directories to search for the user account in.
4055 */
4056 {
4057 char_u test[MAXPATHL], paths[MAXPATHL];
4058 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004059 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
4061 STRCPY(paths, USER_HOME);
4062 next_path = paths;
4063 while (*next_path)
4064 {
4065 for (path = next_path; *next_path && *next_path != ',';
4066 next_path++);
4067 if (*next_path)
4068 *next_path++ = NUL;
4069 STRCPY(test, path);
4070 STRCAT(test, "/");
4071 STRCAT(test, dst + 1);
4072 if (mch_stat(test, &st) == 0)
4073 {
4074 var = alloc(STRLEN(test) + 1);
4075 STRCPY(var, test);
4076 mustfree = TRUE;
4077 break;
4078 }
4079 }
4080 }
4081# endif /* UNIX */
4082#else
4083 /* cannot expand user's home directory, so don't try */
4084 var = NULL;
4085 tail = (char_u *)""; /* for gcc */
4086#endif /* UNIX || VMS */
4087 }
4088
4089#ifdef BACKSLASH_IN_FILENAME
4090 /* If 'shellslash' is set change backslashes to forward slashes.
4091 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4092 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4093 {
4094 char_u *p = vim_strsave(var);
4095
4096 if (p != NULL)
4097 {
4098 if (mustfree)
4099 vim_free(var);
4100 var = p;
4101 mustfree = TRUE;
4102 forward_slash(var);
4103 }
4104 }
4105#endif
4106
4107 /* If "var" contains white space, escape it with a backslash.
4108 * Required for ":e ~/tt" when $HOME includes a space. */
4109 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4110 {
4111 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4112
4113 if (p != NULL)
4114 {
4115 if (mustfree)
4116 vim_free(var);
4117 var = p;
4118 mustfree = TRUE;
4119 }
4120 }
4121
4122 if (var != NULL && *var != NUL
4123 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4124 {
4125 STRCPY(dst, var);
4126 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004127 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 /* if var[] ends in a path separator and tail[] starts
4129 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004130 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4132 && dst[-1] != ':'
4133#endif
4134 && vim_ispathsep(*tail))
4135 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004136 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 src = tail;
4138 copy_char = FALSE;
4139 }
4140 if (mustfree)
4141 vim_free(var);
4142 }
4143
4144 if (copy_char) /* copy at least one char */
4145 {
4146 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004147 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004148 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4149 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 */
4151 at_start = FALSE;
4152 if (src[0] == '\\' && src[1] != NUL)
4153 {
4154 *dst++ = *src++;
4155 --dstlen;
4156 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004157 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 at_start = TRUE;
4159 *dst++ = *src++;
4160 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004161
4162 if (startstr != NULL && src - startstr_len >= srcp
4163 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4164 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
4166 }
4167 *dst = NUL;
4168}
4169
4170/*
4171 * Vim's version of getenv().
4172 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004173 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004174 * "mustfree" is set to TRUE when returned is allocated, it must be
4175 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 */
4177 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004178vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180 char_u *p;
4181 char_u *pend;
4182 int vimruntime;
4183
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004184#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /* use "C:/" when $HOME is not set */
4186 if (STRCMP(name, "HOME") == 0)
4187 return homedir;
4188#endif
4189
4190 p = mch_getenv(name);
4191 if (p != NULL && *p == NUL) /* empty is the same as not set */
4192 p = NULL;
4193
4194 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004195 {
4196#if defined(FEAT_MBYTE) && defined(WIN3264)
4197 if (enc_utf8)
4198 {
4199 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004200 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004201
4202 /* Convert from active codepage to UTF-8. Other conversions are
4203 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004204 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004205 if (pp != NULL)
4206 {
4207 p = pp;
4208 *mustfree = TRUE;
4209 }
4210 }
4211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4216 if (!vimruntime && STRCMP(name, "VIM") != 0)
4217 return NULL;
4218
4219 /*
4220 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4221 * Don't do this when default_vimruntime_dir is non-empty.
4222 */
4223 if (vimruntime
4224#ifdef HAVE_PATHDEF
4225 && *default_vimruntime_dir == NUL
4226#endif
4227 )
4228 {
4229 p = mch_getenv((char_u *)"VIM");
4230 if (p != NULL && *p == NUL) /* empty is the same as not set */
4231 p = NULL;
4232 if (p != NULL)
4233 {
4234 p = vim_version_dir(p);
4235 if (p != NULL)
4236 *mustfree = TRUE;
4237 else
4238 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004239
4240#if defined(FEAT_MBYTE) && defined(WIN3264)
4241 if (enc_utf8)
4242 {
4243 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004244 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004245
4246 /* Convert from active codepage to UTF-8. Other conversions
4247 * are not done, because they would fail for non-ASCII
4248 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004249 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004250 if (pp != NULL)
4251 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004252 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004253 vim_free(p);
4254 p = pp;
4255 *mustfree = TRUE;
4256 }
4257 }
4258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 }
4261
4262 /*
4263 * When expanding $VIM or $VIMRUNTIME fails, try using:
4264 * - the directory name from 'helpfile' (unless it contains '$')
4265 * - the executable name from argv[0]
4266 */
4267 if (p == NULL)
4268 {
4269 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4270 p = p_hf;
4271#ifdef USE_EXE_NAME
4272 /*
4273 * Use the name of the executable, obtained from argv[0].
4274 */
4275 else
4276 p = exe_name;
4277#endif
4278 if (p != NULL)
4279 {
4280 /* remove the file name */
4281 pend = gettail(p);
4282
4283 /* remove "doc/" from 'helpfile', if present */
4284 if (p == p_hf)
4285 pend = remove_tail(p, pend, (char_u *)"doc");
4286
4287#ifdef USE_EXE_NAME
4288# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004289 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 if (p == exe_name)
4291 {
4292 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004293 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004295 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4296 if (pend1 != pend)
4297 {
4298 pnew = alloc((unsigned)(pend1 - p) + 15);
4299 if (pnew != NULL)
4300 {
4301 STRNCPY(pnew, p, (pend1 - p));
4302 STRCPY(pnew + (pend1 - p), "Resources/vim");
4303 p = pnew;
4304 pend = p + STRLEN(p);
4305 }
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308# endif
4309 /* remove "src/" from exe_name, if present */
4310 if (p == exe_name)
4311 pend = remove_tail(p, pend, (char_u *)"src");
4312#endif
4313
4314 /* for $VIM, remove "runtime/" or "vim54/", if present */
4315 if (!vimruntime)
4316 {
4317 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4318 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4319 }
4320
4321 /* remove trailing path separator */
4322#ifndef MACOS_CLASSIC
4323 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004324 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004325 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 --pend;
4327#endif
4328
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004329#ifdef MACOS_X
4330 if (p == exe_name || p == p_hf)
4331#endif
4332 /* check that the result is a directory name */
4333 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 if (p != NULL && !mch_isdir(p))
4336 {
4337 vim_free(p);
4338 p = NULL;
4339 }
4340 else
4341 {
4342#ifdef USE_EXE_NAME
4343 /* may add "/vim54" or "/runtime" if it exists */
4344 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4345 {
4346 vim_free(p);
4347 p = pend;
4348 }
4349#endif
4350 *mustfree = TRUE;
4351 }
4352 }
4353 }
4354
4355#ifdef HAVE_PATHDEF
4356 /* When there is a pathdef.c file we can use default_vim_dir and
4357 * default_vimruntime_dir */
4358 if (p == NULL)
4359 {
4360 /* Only use default_vimruntime_dir when it is not empty */
4361 if (vimruntime && *default_vimruntime_dir != NUL)
4362 {
4363 p = default_vimruntime_dir;
4364 *mustfree = FALSE;
4365 }
4366 else if (*default_vim_dir != NUL)
4367 {
4368 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4369 *mustfree = TRUE;
4370 else
4371 {
4372 p = default_vim_dir;
4373 *mustfree = FALSE;
4374 }
4375 }
4376 }
4377#endif
4378
4379 /*
4380 * Set the environment variable, so that the new value can be found fast
4381 * next time, and others can also use it (e.g. Perl).
4382 */
4383 if (p != NULL)
4384 {
4385 if (vimruntime)
4386 {
4387 vim_setenv((char_u *)"VIMRUNTIME", p);
4388 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 else
4391 {
4392 vim_setenv((char_u *)"VIM", p);
4393 didset_vim = TRUE;
4394 }
4395 }
4396 return p;
4397}
4398
4399/*
4400 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4401 * Return NULL if not, return its name in allocated memory otherwise.
4402 */
4403 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004404vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
4406 char_u *p;
4407
4408 if (vimdir == NULL || *vimdir == NUL)
4409 return NULL;
4410 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4411 if (p != NULL && mch_isdir(p))
4412 return p;
4413 vim_free(p);
4414 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4415 if (p != NULL && mch_isdir(p))
4416 return p;
4417 vim_free(p);
4418 return NULL;
4419}
4420
4421/*
4422 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4423 * the length of "name/". Otherwise return "pend".
4424 */
4425 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004426remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427{
4428 int len = (int)STRLEN(name) + 1;
4429 char_u *newend = pend - len;
4430
4431 if (newend >= p
4432 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004433 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 return newend;
4435 return pend;
4436}
4437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 * Our portable version of setenv.
4440 */
4441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004442vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444#ifdef HAVE_SETENV
4445 mch_setenv((char *)name, (char *)val, 1);
4446#else
4447 char_u *envbuf;
4448
4449 /*
4450 * Putenv does not copy the string, it has to remain
4451 * valid. The allocated memory will never be freed.
4452 */
4453 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4454 if (envbuf != NULL)
4455 {
4456 sprintf((char *)envbuf, "%s=%s", name, val);
4457 putenv((char *)envbuf);
4458 }
4459#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004460#ifdef FEAT_GETTEXT
4461 /*
4462 * When setting $VIMRUNTIME adjust the directory to find message
4463 * translations to $VIMRUNTIME/lang.
4464 */
4465 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4466 {
4467 char_u *buf = concat_str(val, (char_u *)"/lang");
4468
4469 if (buf != NULL)
4470 {
4471 bindtextdomain(VIMPACKAGE, (char *)buf);
4472 vim_free(buf);
4473 }
4474 }
4475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476}
4477
4478#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4479/*
4480 * Function given to ExpandGeneric() to obtain an environment variable name.
4481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004483get_env_name(
4484 expand_T *xp UNUSED,
4485 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4488 /*
4489 * No environ[] on the Amiga and on the Mac (using MPW).
4490 */
4491 return NULL;
4492# else
4493# ifndef __WIN32__
4494 /* Borland C++ 5.2 has this in a header file. */
4495 extern char **environ;
4496# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004497# define ENVNAMELEN 100
4498 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 char_u *str;
4500 int n;
4501
4502 str = (char_u *)environ[idx];
4503 if (str == NULL)
4504 return NULL;
4505
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004506 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
4508 if (str[n] == '=' || str[n] == NUL)
4509 break;
4510 name[n] = str[n];
4511 }
4512 name[n] = NUL;
4513 return name;
4514# endif
4515}
Bram Moolenaar24305862012-08-15 14:05:05 +02004516
4517/*
4518 * Find all user names for user completion.
4519 * Done only once and then cached.
4520 */
4521 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004522init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004523{
Bram Moolenaar24305862012-08-15 14:05:05 +02004524 static int lazy_init_done = FALSE;
4525
4526 if (lazy_init_done)
4527 return;
4528
4529 lazy_init_done = TRUE;
4530 ga_init2(&ga_users, sizeof(char_u *), 20);
4531
4532# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4533 {
4534 char_u* user;
4535 struct passwd* pw;
4536
4537 setpwent();
4538 while ((pw = getpwent()) != NULL)
4539 /* pw->pw_name shouldn't be NULL but just in case... */
4540 if (pw->pw_name != NULL)
4541 {
4542 if (ga_grow(&ga_users, 1) == FAIL)
4543 break;
4544 user = vim_strsave((char_u*)pw->pw_name);
4545 if (user == NULL)
4546 break;
4547 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4548 }
4549 endpwent();
4550 }
4551# endif
4552}
4553
4554/*
4555 * Function given to ExpandGeneric() to obtain an user names.
4556 */
4557 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004558get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004559{
4560 init_users();
4561 if (idx < ga_users.ga_len)
4562 return ((char_u **)ga_users.ga_data)[idx];
4563 return NULL;
4564}
4565
4566/*
4567 * Check whether name matches a user name. Return:
4568 * 0 if name does not match any user name.
4569 * 1 if name partially matches the beginning of a user name.
4570 * 2 is name fully matches a user name.
4571 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004572int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004573{
4574 int i;
4575 int n = (int)STRLEN(name);
4576 int result = 0;
4577
4578 init_users();
4579 for (i = 0; i < ga_users.ga_len; i++)
4580 {
4581 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4582 return 2; /* full match */
4583 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4584 result = 1; /* partial match */
4585 }
4586 return result;
4587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588#endif
4589
4590/*
4591 * Replace home directory by "~" in each space or comma separated file name in
4592 * 'src'.
4593 * If anything fails (except when out of space) dst equals src.
4594 */
4595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004596home_replace(
4597 buf_T *buf, /* when not NULL, check for help files */
4598 char_u *src, /* input file name */
4599 char_u *dst, /* where to put the result */
4600 int dstlen, /* maximum length of the result */
4601 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 spaces and commas in the file name. */
4603{
4604 size_t dirlen = 0, envlen = 0;
4605 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004606 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 char_u *p;
4608
4609 if (src == NULL)
4610 {
4611 *dst = NUL;
4612 return;
4613 }
4614
4615 /*
4616 * If the file is a help file, remove the path completely.
4617 */
4618 if (buf != NULL && buf->b_help)
4619 {
4620 STRCPY(dst, gettail(src));
4621 return;
4622 }
4623
4624 /*
4625 * We check both the value of the $HOME environment variable and the
4626 * "real" home directory.
4627 */
4628 if (homedir != NULL)
4629 dirlen = STRLEN(homedir);
4630
4631#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004632 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004634 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4635#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004636 /* Empty is the same as not set. */
4637 if (homedir_env != NULL && *homedir_env == NUL)
4638 homedir_env = NULL;
4639
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004640#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004641 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004642 {
4643 int usedlen = 0;
4644 int flen;
4645 char_u *fbuf = NULL;
4646
4647 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004648 (void)modify_fname((char_u *)":p", &usedlen,
4649 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004650 flen = (int)STRLEN(homedir_env);
4651 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4652 /* Remove the trailing / that is added to a directory. */
4653 homedir_env[flen - 1] = NUL;
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655#endif
4656
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (homedir_env != NULL)
4658 envlen = STRLEN(homedir_env);
4659
4660 if (!one)
4661 src = skipwhite(src);
4662 while (*src && dstlen > 0)
4663 {
4664 /*
4665 * Here we are at the beginning of a file name.
4666 * First, check to see if the beginning of the file name matches
4667 * $HOME or the "real" home directory. Check that there is a '/'
4668 * after the match (so that if e.g. the file is "/home/pieter/bla",
4669 * and the home directory is "/home/piet", the file does not end up
4670 * as "~er/bla" (which would seem to indicate the file "bla" in user
4671 * er's home directory)).
4672 */
4673 p = homedir;
4674 len = dirlen;
4675 for (;;)
4676 {
4677 if ( len
4678 && fnamencmp(src, p, len) == 0
4679 && (vim_ispathsep(src[len])
4680 || (!one && (src[len] == ',' || src[len] == ' '))
4681 || src[len] == NUL))
4682 {
4683 src += len;
4684 if (--dstlen > 0)
4685 *dst++ = '~';
4686
4687 /*
4688 * If it's just the home directory, add "/".
4689 */
4690 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4691 *dst++ = '/';
4692 break;
4693 }
4694 if (p == homedir_env)
4695 break;
4696 p = homedir_env;
4697 len = envlen;
4698 }
4699
4700 /* if (!one) skip to separator: space or comma */
4701 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4702 *dst++ = *src++;
4703 /* skip separator */
4704 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4705 *dst++ = *src++;
4706 }
4707 /* if (dstlen == 0) out of space, what to do??? */
4708
4709 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004710
4711 if (homedir_env != homedir_env_orig)
4712 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713}
4714
4715/*
4716 * Like home_replace, store the replaced string in allocated memory.
4717 * When something fails, NULL is returned.
4718 */
4719 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004720home_replace_save(
4721 buf_T *buf, /* when not NULL, check for help files */
4722 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 char_u *dst;
4725 unsigned len;
4726
4727 len = 3; /* space for "~/" and trailing NUL */
4728 if (src != NULL) /* just in case */
4729 len += (unsigned)STRLEN(src);
4730 dst = alloc(len);
4731 if (dst != NULL)
4732 home_replace(buf, src, dst, len, TRUE);
4733 return dst;
4734}
4735
4736/*
4737 * Compare two file names and return:
4738 * FPC_SAME if they both exist and are the same file.
4739 * FPC_SAMEX if they both don't exist and have the same file name.
4740 * FPC_DIFF if they both exist and are different files.
4741 * FPC_NOTX if they both don't exist.
4742 * FPC_DIFFX if one of them doesn't exist.
4743 * For the first name environment variables are expanded
4744 */
4745 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004746fullpathcmp(
4747 char_u *s1,
4748 char_u *s2,
4749 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
4751#ifdef UNIX
4752 char_u exp1[MAXPATHL];
4753 char_u full1[MAXPATHL];
4754 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004755 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 int r1, r2;
4757
4758 expand_env(s1, exp1, MAXPATHL);
4759 r1 = mch_stat((char *)exp1, &st1);
4760 r2 = mch_stat((char *)s2, &st2);
4761 if (r1 != 0 && r2 != 0)
4762 {
4763 /* if mch_stat() doesn't work, may compare the names */
4764 if (checkname)
4765 {
4766 if (fnamecmp(exp1, s2) == 0)
4767 return FPC_SAMEX;
4768 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4769 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4770 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4771 return FPC_SAMEX;
4772 }
4773 return FPC_NOTX;
4774 }
4775 if (r1 != 0 || r2 != 0)
4776 return FPC_DIFFX;
4777 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4778 return FPC_SAME;
4779 return FPC_DIFF;
4780#else
4781 char_u *exp1; /* expanded s1 */
4782 char_u *full1; /* full path of s1 */
4783 char_u *full2; /* full path of s2 */
4784 int retval = FPC_DIFF;
4785 int r1, r2;
4786
4787 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4788 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4789 {
4790 full1 = exp1 + MAXPATHL;
4791 full2 = full1 + MAXPATHL;
4792
4793 expand_env(s1, exp1, MAXPATHL);
4794 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4795 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4796
4797 /* If vim_FullName() fails, the file probably doesn't exist. */
4798 if (r1 != OK && r2 != OK)
4799 {
4800 if (checkname && fnamecmp(exp1, s2) == 0)
4801 retval = FPC_SAMEX;
4802 else
4803 retval = FPC_NOTX;
4804 }
4805 else if (r1 != OK || r2 != OK)
4806 retval = FPC_DIFFX;
4807 else if (fnamecmp(full1, full2))
4808 retval = FPC_DIFF;
4809 else
4810 retval = FPC_SAME;
4811 vim_free(exp1);
4812 }
4813 return retval;
4814#endif
4815}
4816
4817/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004818 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004819 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004820 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 */
4822 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004823gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
4825 char_u *p1, *p2;
4826
4827 if (fname == NULL)
4828 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004829 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004831 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004833 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835 return p1;
4836}
4837
Bram Moolenaar31710262010-08-13 13:36:15 +02004838#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004839static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004840
4841/*
4842 * Return the end of the directory name, on the first path
4843 * separator:
4844 * "/path/file", "/path/dir/", "/path//dir", "/file"
4845 * ^ ^ ^ ^
4846 */
4847 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004848gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004849{
4850 char_u *dir_end = fname;
4851 char_u *next_dir_end = fname;
4852 int look_for_sep = TRUE;
4853 char_u *p;
4854
4855 for (p = fname; *p != NUL; )
4856 {
4857 if (vim_ispathsep(*p))
4858 {
4859 if (look_for_sep)
4860 {
4861 next_dir_end = p;
4862 look_for_sep = FALSE;
4863 }
4864 }
4865 else
4866 {
4867 if (!look_for_sep)
4868 dir_end = next_dir_end;
4869 look_for_sep = TRUE;
4870 }
4871 mb_ptr_adv(p);
4872 }
4873 return dir_end;
4874}
4875#endif
4876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004878 * Get pointer to tail of "fname", including path separators. Putting a NUL
4879 * here leaves the directory name. Takes care of "c:/" and "//".
4880 * Always returns a valid pointer.
4881 */
4882 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004883gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004884{
4885 char_u *p;
4886 char_u *t;
4887
4888 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4889 t = gettail(fname);
4890 while (t > p && after_pathsep(fname, t))
4891 --t;
4892#ifdef VMS
4893 /* path separator is part of the path */
4894 ++t;
4895#endif
4896 return t;
4897}
4898
4899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 * get the next path component (just after the next path separator).
4901 */
4902 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004903getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
4905 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004906 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (*fname)
4908 ++fname;
4909 return fname;
4910}
4911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912/*
4913 * Get a pointer to one character past the head of a path name.
4914 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4915 * If there is no head, path is returned.
4916 */
4917 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004918get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
4920 char_u *retval;
4921
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004922#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 /* may skip "c:" */
4924 if (isalpha(path[0]) && path[1] == ':')
4925 retval = path + 2;
4926 else
4927 retval = path;
4928#else
4929# if defined(AMIGA)
4930 /* may skip "label:" */
4931 retval = vim_strchr(path, ':');
4932 if (retval == NULL)
4933 retval = path;
4934# else /* Unix */
4935 retval = path;
4936# endif
4937#endif
4938
4939 while (vim_ispathsep(*retval))
4940 ++retval;
4941
4942 return retval;
4943}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004946 * Return TRUE if 'c' is a path separator.
4947 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 */
4949 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004950vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004952#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004954#else
4955# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004957# else
4958# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4960 return (c == ':' || c == '[' || c == ']' || c == '/'
4961 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004962# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004964# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967}
4968
Bram Moolenaar69c35002013-11-04 02:54:12 +01004969/*
4970 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4971 */
4972 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004973vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004974{
4975 return vim_ispathsep(c)
4976#ifdef BACKSLASH_IN_FILENAME
4977 && c != ':'
4978#endif
4979 ;
4980}
4981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4983/*
4984 * return TRUE if 'c' is a path list separator.
4985 */
4986 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004987vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
4989#ifdef UNIX
4990 return (c == ':');
4991#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004992 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#endif
4994}
4995#endif
4996
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004997#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4998 || defined(FEAT_EVAL) || defined(PROTO)
4999/*
5000 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5001 * It's done in-place.
5002 */
5003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005004shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005005{
5006 char_u *tail, *s, *d;
5007 int skip = FALSE;
5008
5009 tail = gettail(str);
5010 d = str;
5011 for (s = str; ; ++s)
5012 {
5013 if (s >= tail) /* copy the whole tail */
5014 {
5015 *d++ = *s;
5016 if (*s == NUL)
5017 break;
5018 }
5019 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5020 {
5021 *d++ = *s;
5022 skip = FALSE;
5023 }
5024 else if (!skip)
5025 {
5026 *d++ = *s; /* copy next char */
5027 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5028 skip = TRUE;
5029# ifdef FEAT_MBYTE
5030 if (has_mbyte)
5031 {
5032 int l = mb_ptr2len(s);
5033
5034 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005035 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005036 }
5037# endif
5038 }
5039 }
5040}
5041#endif
5042
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005043/*
5044 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5045 * Also returns TRUE if there is no directory name.
5046 * "fname" must be writable!.
5047 */
5048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005049dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005050{
5051 char_u *p;
5052 int c;
5053 int retval;
5054
5055 p = gettail_sep(fname);
5056 if (p == fname)
5057 return TRUE;
5058 c = *p;
5059 *p = NUL;
5060 retval = mch_isdir(fname);
5061 *p = c;
5062 return retval;
5063}
5064
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005066 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5067 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
5069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005070vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005072#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005074#else
5075 if (p_fic)
5076 return MB_STRICMP(x, y);
5077 return STRCMP(x, y);
5078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079}
5080
5081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005082vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005084#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005085 char_u *px = x;
5086 char_u *py = y;
5087 int cx = NUL;
5088 int cy = NUL;
5089
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005090 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005092 cx = PTR2CHAR(px);
5093 cy = PTR2CHAR(py);
5094 if (cx == NUL || cy == NUL
5095 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5096 && !(cx == '/' && cy == '\\')
5097 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005099 len -= MB_PTR2LEN(px);
5100 px += MB_PTR2LEN(px);
5101 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103 if (len == 0)
5104 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005105 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005106#else
5107 if (p_fic)
5108 return MB_STRNICMP(x, y, len);
5109 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113/*
5114 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005115 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 */
5117 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005118concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
5120 char_u *dest;
5121
5122 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5123 if (dest != NULL)
5124 {
5125 STRCPY(dest, fname1);
5126 if (sep)
5127 add_pathsep(dest);
5128 STRCAT(dest, fname2);
5129 }
5130 return dest;
5131}
5132
Bram Moolenaard6754642005-01-17 22:18:45 +00005133/*
5134 * Concatenate two strings and return the result in allocated memory.
5135 * Returns NULL when out of memory.
5136 */
5137 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005138concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005139{
5140 char_u *dest;
5141 size_t l = STRLEN(str1);
5142
5143 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5144 if (dest != NULL)
5145 {
5146 STRCPY(dest, str1);
5147 STRCPY(dest + l, str2);
5148 }
5149 return dest;
5150}
Bram Moolenaard6754642005-01-17 22:18:45 +00005151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152/*
5153 * Add a path separator to a file name, unless it already ends in a path
5154 * separator.
5155 */
5156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005157add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005159 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 STRCAT(p, PATHSEPSTR);
5161}
5162
5163/*
5164 * FullName_save - Make an allocated copy of a full file name.
5165 * Returns NULL when out of memory.
5166 */
5167 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005168FullName_save(
5169 char_u *fname,
5170 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005171 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 char_u *buf;
5174 char_u *new_fname = NULL;
5175
5176 if (fname == NULL)
5177 return NULL;
5178
5179 buf = alloc((unsigned)MAXPATHL);
5180 if (buf != NULL)
5181 {
5182 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5183 new_fname = vim_strsave(buf);
5184 else
5185 new_fname = vim_strsave(fname);
5186 vim_free(buf);
5187 }
5188 return new_fname;
5189}
5190
5191#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5192
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005193static char_u *skip_string(char_u *p);
5194static pos_T *ind_find_start_comment(void);
5195static pos_T *ind_find_start_CORS(void);
5196static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198/*
5199 * Find the start of a comment, not knowing if we are in a comment right now.
5200 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005201 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005203 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005204ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005205{
5206 return find_start_comment(curbuf->b_ind_maxcomment);
5207}
5208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005210find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
5212 pos_T *pos;
5213 char_u *line;
5214 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005215 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005217 for (;;)
5218 {
5219 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5220 if (pos == NULL)
5221 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005223 /*
5224 * Check if the comment start we found is inside a string.
5225 * If it is then restrict the search to below this line and try again.
5226 */
5227 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005228 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005229 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005230 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005231 break;
5232 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5233 if (cur_maxcomment <= 0)
5234 {
5235 pos = NULL;
5236 break;
5237 }
5238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 return pos;
5240}
5241
5242/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005243 * Find the start of a comment or raw string, not knowing if we are in a
5244 * comment or raw string right now.
5245 * Search starts at w_cursor.lnum and goes backwards.
5246 * Return NULL when not inside a comment or raw string.
5247 * "CORS" -> Comment Or Raw String
5248 */
5249 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005251{
Bram Moolenaar089af182015-10-07 11:41:49 +02005252 static pos_T comment_pos_copy;
5253 pos_T *comment_pos;
5254 pos_T *rs_pos;
5255
5256 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5257 if (comment_pos != NULL)
5258 {
5259 /* Need to make a copy of the static pos in findmatchlimit(),
5260 * calling find_start_rawstring() may change it. */
5261 comment_pos_copy = *comment_pos;
5262 comment_pos = &comment_pos_copy;
5263 }
5264 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005265
5266 /* If comment_pos is before rs_pos the raw string is inside the comment.
5267 * If rs_pos is before comment_pos the comment is inside the raw string. */
5268 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5269 return rs_pos;
5270 return comment_pos;
5271}
5272
5273/*
5274 * Find the start of a raw string, not knowing if we are in one right now.
5275 * Search starts at w_cursor.lnum and goes backwards.
5276 * Return NULL when not inside a raw string.
5277 */
5278 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005279find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005280{
5281 pos_T *pos;
5282 char_u *line;
5283 char_u *p;
5284 int cur_maxcomment = ind_maxcomment;
5285
5286 for (;;)
5287 {
5288 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5289 if (pos == NULL)
5290 break;
5291
5292 /*
5293 * Check if the raw string start we found is inside a string.
5294 * If it is then restrict the search to below this line and try again.
5295 */
5296 line = ml_get(pos->lnum);
5297 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5298 p = skip_string(p);
5299 if ((colnr_T)(p - line) <= pos->col)
5300 break;
5301 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5302 if (cur_maxcomment <= 0)
5303 {
5304 pos = NULL;
5305 break;
5306 }
5307 }
5308 return pos;
5309}
5310
5311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * Skip to the end of a "string" and a 'c' character.
5313 * If there is no string or character, return argument unmodified.
5314 */
5315 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005316skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 int i;
5319
5320 /*
5321 * We loop, because strings may be concatenated: "date""time".
5322 */
5323 for ( ; ; ++p)
5324 {
5325 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5326 {
5327 if (!p[1]) /* ' at end of line */
5328 break;
5329 i = 2;
5330 if (p[1] == '\\') /* '\n' or '\000' */
5331 {
5332 ++i;
5333 while (vim_isdigit(p[i - 1])) /* '\000' */
5334 ++i;
5335 }
5336 if (p[i] == '\'') /* check for trailing ' */
5337 {
5338 p += i;
5339 continue;
5340 }
5341 }
5342 else if (p[0] == '"') /* start of string */
5343 {
5344 for (++p; p[0]; ++p)
5345 {
5346 if (p[0] == '\\' && p[1] != NUL)
5347 ++p;
5348 else if (p[0] == '"') /* end of string */
5349 break;
5350 }
5351 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005352 continue; /* continue for another string */
5353 }
5354 else if (p[0] == 'R' && p[1] == '"')
5355 {
5356 /* Raw string: R"[delim](...)[delim]" */
5357 char_u *delim = p + 2;
5358 char_u *paren = vim_strchr(delim, '(');
5359
5360 if (paren != NULL)
5361 {
5362 size_t delim_len = paren - delim;
5363
5364 for (p += 3; *p; ++p)
5365 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5366 && p[delim_len + 1] == '"')
5367 {
5368 p += delim_len + 1;
5369 break;
5370 }
5371 if (p[0] == '"')
5372 continue; /* continue for another string */
5373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 break; /* no string found */
5376 }
5377 if (!*p)
5378 --p; /* backup from NUL */
5379 return p;
5380}
5381#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5382
5383#if defined(FEAT_CINDENT) || defined(PROTO)
5384
5385/*
5386 * Do C or expression indenting on the current line.
5387 */
5388 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005389do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391# ifdef FEAT_EVAL
5392 if (*curbuf->b_p_inde != NUL)
5393 fixthisline(get_expr_indent);
5394 else
5395# endif
5396 fixthisline(get_c_indent);
5397}
5398
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005399/* Find result cache for cpp_baseclass */
5400typedef struct {
5401 int found;
5402 lpos_T lpos;
5403} cpp_baseclass_cache_T;
5404
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405/*
5406 * Functions for C-indenting.
5407 * Most of this originally comes from Eric Fischer.
5408 */
5409/*
5410 * Below "XXX" means that this function may unlock the current line.
5411 */
5412
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005413static char_u *cin_skipcomment(char_u *);
5414static int cin_nocode(char_u *);
5415static pos_T *find_line_comment(void);
5416static int cin_has_js_key(char_u *text);
5417static int cin_islabel_skip(char_u **);
5418static int cin_isdefault(char_u *);
5419static char_u *after_label(char_u *l);
5420static int get_indent_nolabel(linenr_T lnum);
5421static int skip_label(linenr_T, char_u **pp);
5422static int cin_first_id_amount(void);
5423static int cin_get_equal_amount(linenr_T lnum);
5424static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005425static int cin_iscomment(char_u *);
5426static int cin_islinecomment(char_u *);
5427static int cin_isterminated(char_u *, int, int);
5428static int cin_isinit(void);
5429static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5430static int cin_isif(char_u *);
5431static int cin_iselse(char_u *);
5432static int cin_isdo(char_u *);
5433static int cin_iswhileofdo(char_u *, linenr_T);
5434static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5435static int cin_iswhileofdo_end(int terminated);
5436static int cin_isbreak(char_u *);
5437static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5438static int get_baseclass_amount(int col);
5439static int cin_ends_in(char_u *, char_u *, char_u *);
5440static int cin_starts_with(char_u *s, char *word);
5441static int cin_skip2pos(pos_T *trypos);
5442static pos_T *find_start_brace(void);
5443static pos_T *find_match_paren(int);
5444static pos_T *find_match_char(int c, int ind_maxparen);
5445static int corr_ind_maxparen(pos_T *startpos);
5446static int find_last_paren(char_u *l, int start, int end);
5447static int find_match(int lookfor, linenr_T ourscope);
5448static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450/*
5451 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005452 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 */
5454 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005455cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
5457 while (*s)
5458 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005459 char_u *prev_s = s;
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005462
5463 /* Perl/shell # comment comment continues until eol. Require a space
5464 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005465 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005466 {
5467 s += STRLEN(s);
5468 break;
5469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 if (*s != '/')
5471 break;
5472 ++s;
5473 if (*s == '/') /* slash-slash comment continues till eol */
5474 {
5475 s += STRLEN(s);
5476 break;
5477 }
5478 if (*s != '*')
5479 break;
5480 for (++s; *s; ++s) /* skip slash-star comment */
5481 if (s[0] == '*' && s[1] == '/')
5482 {
5483 s += 2;
5484 break;
5485 }
5486 }
5487 return s;
5488}
5489
5490/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005491 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 * not considered code.
5493 */
5494 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005495cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 return *cin_skipcomment(s) == NUL;
5498}
5499
5500/*
5501 * Check previous lines for a "//" line comment, skipping over blank lines.
5502 */
5503 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005504find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 static pos_T pos;
5507 char_u *line;
5508 char_u *p;
5509
5510 pos = curwin->w_cursor;
5511 while (--pos.lnum > 0)
5512 {
5513 line = ml_get(pos.lnum);
5514 p = skipwhite(line);
5515 if (cin_islinecomment(p))
5516 {
5517 pos.col = (int)(p - line);
5518 return &pos;
5519 }
5520 if (*p != NUL)
5521 break;
5522 }
5523 return NULL;
5524}
5525
5526/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005527 * Return TRUE if "text" starts with "key:".
5528 */
5529 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005530cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005531{
5532 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005533 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005534
5535 if (*s == '\'' || *s == '"')
5536 {
5537 /* can be 'key': or "key": */
5538 quote = *s;
5539 ++s;
5540 }
5541 if (!vim_isIDc(*s)) /* need at least one ID character */
5542 return FALSE;
5543
5544 while (vim_isIDc(*s))
5545 ++s;
5546 if (*s == quote)
5547 ++s;
5548
5549 s = cin_skipcomment(s);
5550
5551 /* "::" is not a label, it's C++ */
5552 return (*s == ':' && s[1] != ':');
5553}
5554
5555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005557 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
5559 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005560cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 if (!vim_isIDc(**s)) /* need at least one ID character */
5563 return FALSE;
5564
5565 while (vim_isIDc(**s))
5566 (*s)++;
5567
5568 *s = cin_skipcomment(*s);
5569
5570 /* "::" is not a label, it's C++ */
5571 return (**s == ':' && *++*s != ':');
5572}
5573
5574/*
5575 * Recognize a label: "label:".
5576 * Note: curwin->w_cursor must be where we are looking for the label.
5577 */
5578 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005579cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 char_u *s;
5582
5583 s = cin_skipcomment(ml_get_curline());
5584
5585 /*
5586 * Exclude "default" from labels, since it should be indented
5587 * like a switch label. Same for C++ scope declarations.
5588 */
5589 if (cin_isdefault(s))
5590 return FALSE;
5591 if (cin_isscopedecl(s))
5592 return FALSE;
5593
5594 if (cin_islabel_skip(&s))
5595 {
5596 /*
5597 * Only accept a label if the previous line is terminated or is a case
5598 * label.
5599 */
5600 pos_T cursor_save;
5601 pos_T *trypos;
5602 char_u *line;
5603
5604 cursor_save = curwin->w_cursor;
5605 while (curwin->w_cursor.lnum > 1)
5606 {
5607 --curwin->w_cursor.lnum;
5608
5609 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005610 * If we're in a comment or raw string now, skip to the start of
5611 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 */
5613 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005614 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 curwin->w_cursor = *trypos;
5616
5617 line = ml_get_curline();
5618 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5619 continue;
5620 if (*(line = cin_skipcomment(line)) == NUL)
5621 continue;
5622
5623 curwin->w_cursor = cursor_save;
5624 if (cin_isterminated(line, TRUE, FALSE)
5625 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005626 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 || (cin_islabel_skip(&line) && cin_nocode(line)))
5628 return TRUE;
5629 return FALSE;
5630 }
5631 curwin->w_cursor = cursor_save;
5632 return TRUE; /* label at start of file??? */
5633 }
5634 return FALSE;
5635}
5636
5637/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005638 * Recognize structure initialization and enumerations:
5639 * "[typedef] [static|public|protected|private] enum"
5640 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 */
5642 static int
5643cin_isinit(void)
5644{
5645 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005646 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
5648 s = cin_skipcomment(ml_get_curline());
5649
Bram Moolenaar75342212013-03-07 13:13:52 +01005650 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 s = cin_skipcomment(s + 7);
5652
Bram Moolenaar75342212013-03-07 13:13:52 +01005653 for (;;)
5654 {
5655 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005656
Bram Moolenaar75342212013-03-07 13:13:52 +01005657 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5658 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005659 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005660 if (cin_starts_with(s, skip[i]))
5661 {
5662 s = cin_skipcomment(s + l);
5663 l = 0;
5664 break;
5665 }
5666 }
5667 if (l != 0)
5668 break;
5669 }
5670
5671 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 return TRUE;
5673
5674 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5675 return TRUE;
5676
5677 return FALSE;
5678}
5679
5680/*
5681 * Recognize a switch label: "case .*:" or "default:".
5682 */
5683 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005684cin_iscase(
5685 char_u *s,
5686 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005689 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
5691 for (s += 4; *s; ++s)
5692 {
5693 s = cin_skipcomment(s);
5694 if (*s == ':')
5695 {
5696 if (s[1] == ':') /* skip over "::" for C++ */
5697 ++s;
5698 else
5699 return TRUE;
5700 }
5701 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005702 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5704 return FALSE; /* stop at comment */
5705 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005706 {
5707 /* JS etc. */
5708 if (strict)
5709 return FALSE; /* stop at string */
5710 else
5711 return TRUE;
5712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 return FALSE;
5715 }
5716
5717 if (cin_isdefault(s))
5718 return TRUE;
5719 return FALSE;
5720}
5721
5722/*
5723 * Recognize a "default" switch label.
5724 */
5725 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005726cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 return (STRNCMP(s, "default", 7) == 0
5729 && *(s = cin_skipcomment(s + 7)) == ':'
5730 && s[1] != ':');
5731}
5732
5733/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005734 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 */
5736 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005737cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 int i;
5740
5741 s = cin_skipcomment(s);
5742 if (STRNCMP(s, "public", 6) == 0)
5743 i = 6;
5744 else if (STRNCMP(s, "protected", 9) == 0)
5745 i = 9;
5746 else if (STRNCMP(s, "private", 7) == 0)
5747 i = 7;
5748 else
5749 return FALSE;
5750 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5751}
5752
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005753/* Maximum number of lines to search back for a "namespace" line. */
5754#define FIND_NAMESPACE_LIM 20
5755
5756/*
5757 * Recognize a "namespace" scope declaration.
5758 */
5759 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005760cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005761{
5762 char_u *p;
5763 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005764 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005765
5766 s = cin_skipcomment(s);
5767 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5768 {
5769 p = cin_skipcomment(skipwhite(s + 9));
5770 while (*p != NUL)
5771 {
5772 if (vim_iswhite(*p))
5773 {
5774 has_name = TRUE; /* found end of a name */
5775 p = cin_skipcomment(skipwhite(p));
5776 }
5777 else if (*p == '{')
5778 {
5779 break;
5780 }
5781 else if (vim_iswordc(*p))
5782 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005783 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005784 if (has_name)
5785 return FALSE; /* word character after skipping past name */
5786 ++p;
5787 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005788 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5789 {
5790 if (!has_name_start || has_name)
5791 return FALSE;
5792 /* C++ 17 nested namespace */
5793 p += 3;
5794 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005795 else
5796 {
5797 return FALSE;
5798 }
5799 }
5800 return TRUE;
5801 }
5802 return FALSE;
5803}
5804
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805/*
5806 * Return a pointer to the first non-empty non-comment character after a ':'.
5807 * Return NULL if not found.
5808 * case 234: a = b;
5809 * ^
5810 */
5811 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005812after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
5814 for ( ; *l; ++l)
5815 {
5816 if (*l == ':')
5817 {
5818 if (l[1] == ':') /* skip over "::" for C++ */
5819 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005820 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822 }
5823 else if (*l == '\'' && l[1] && l[2] == '\'')
5824 l += 2; /* skip over 'x' */
5825 }
5826 if (*l == NUL)
5827 return NULL;
5828 l = cin_skipcomment(l + 1);
5829 if (*l == NUL)
5830 return NULL;
5831 return l;
5832}
5833
5834/*
5835 * Get indent of line "lnum", skipping a label.
5836 * Return 0 if there is nothing after the label.
5837 */
5838 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005839get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 char_u *l;
5842 pos_T fp;
5843 colnr_T col;
5844 char_u *p;
5845
5846 l = ml_get(lnum);
5847 p = after_label(l);
5848 if (p == NULL)
5849 return 0;
5850
5851 fp.col = (colnr_T)(p - l);
5852 fp.lnum = lnum;
5853 getvcol(curwin, &fp, &col, NULL, NULL);
5854 return (int)col;
5855}
5856
5857/*
5858 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005859 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 * label: if (asdf && asdfasdf)
5861 * ^
5862 */
5863 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005864skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 char_u *l;
5867 int amount;
5868 pos_T cursor_save;
5869
5870 cursor_save = curwin->w_cursor;
5871 curwin->w_cursor.lnum = lnum;
5872 l = ml_get_curline();
5873 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005874 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 {
5876 amount = get_indent_nolabel(lnum);
5877 l = after_label(ml_get_curline());
5878 if (l == NULL) /* just in case */
5879 l = ml_get_curline();
5880 }
5881 else
5882 {
5883 amount = get_indent();
5884 l = ml_get_curline();
5885 }
5886 *pp = l;
5887
5888 curwin->w_cursor = cursor_save;
5889 return amount;
5890}
5891
5892/*
5893 * Return the indent of the first variable name after a type in a declaration.
5894 * int a, indent of "a"
5895 * static struct foo b, indent of "b"
5896 * enum bla c, indent of "c"
5897 * Returns zero when it doesn't look like a declaration.
5898 */
5899 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005900cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
5902 char_u *line, *p, *s;
5903 int len;
5904 pos_T fp;
5905 colnr_T col;
5906
5907 line = ml_get_curline();
5908 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005909 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5911 {
5912 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005913 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5916 p = skipwhite(p + 6);
5917 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5918 p = skipwhite(p + 4);
5919 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5920 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5921 {
5922 s = skipwhite(p + len);
5923 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5924 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5925 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5926 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5927 p = s;
5928 }
5929 for (len = 0; vim_isIDc(p[len]); ++len)
5930 ;
5931 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5932 return 0;
5933
5934 p = skipwhite(p + len);
5935 fp.lnum = curwin->w_cursor.lnum;
5936 fp.col = (colnr_T)(p - line);
5937 getvcol(curwin, &fp, &col, NULL, NULL);
5938 return (int)col;
5939}
5940
5941/*
5942 * Return the indent of the first non-blank after an equal sign.
5943 * char *foo = "here";
5944 * Return zero if no (useful) equal sign found.
5945 * Return -1 if the line above "lnum" ends in a backslash.
5946 * foo = "asdf\
5947 * asdf\
5948 * here";
5949 */
5950 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005951cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 char_u *line;
5954 char_u *s;
5955 colnr_T col;
5956 pos_T fp;
5957
5958 if (lnum > 1)
5959 {
5960 line = ml_get(lnum - 1);
5961 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5962 return -1;
5963 }
5964
5965 line = s = ml_get(lnum);
5966 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5967 {
5968 if (cin_iscomment(s)) /* ignore comments */
5969 s = cin_skipcomment(s);
5970 else
5971 ++s;
5972 }
5973 if (*s != '=')
5974 return 0;
5975
5976 s = skipwhite(s + 1);
5977 if (cin_nocode(s))
5978 return 0;
5979
5980 if (*s == '"') /* nice alignment for continued strings */
5981 ++s;
5982
5983 fp.lnum = lnum;
5984 fp.col = (colnr_T)(s - line);
5985 getvcol(curwin, &fp, &col, NULL, NULL);
5986 return (int)col;
5987}
5988
5989/*
5990 * Recognize a preprocessor statement: Any line that starts with '#'.
5991 */
5992 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005993cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005995 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 return TRUE;
5997 return FALSE;
5998}
5999
6000/*
6001 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6002 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6003 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006004 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 */
6006 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006007cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 char_u *line = *pp;
6010 linenr_T lnum = *lnump;
6011 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006012 int candidate_amount = *amount;
6013
6014 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6015 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006017 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 {
6019 if (cin_ispreproc(line))
6020 {
6021 retval = TRUE;
6022 *lnump = lnum;
6023 break;
6024 }
6025 if (lnum == 1)
6026 break;
6027 line = ml_get(--lnum);
6028 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6029 break;
6030 }
6031
6032 if (lnum != *lnump)
6033 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006034 if (retval)
6035 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 return retval;
6037}
6038
6039/*
6040 * Recognize the start of a C or C++ comment.
6041 */
6042 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006043cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6046}
6047
6048/*
6049 * Recognize the start of a "//" comment.
6050 */
6051 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006052cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 return (p[0] == '/' && p[1] == '/');
6055}
6056
6057/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006058 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6059 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006061 * If a line begins with an "else", only consider it terminated if no unmatched
6062 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 * Return the character terminating the line (ending char's have precedence if
6064 * both apply in order to determine initializations).
6065 */
6066 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006067cin_isterminated(
6068 char_u *s,
6069 int incl_open, /* include '{' at the end as terminator */
6070 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006072 char_u found_start = 0;
6073 unsigned n_open = 0;
6074 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076 s = cin_skipcomment(s);
6077
6078 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6079 found_start = *s;
6080
Bram Moolenaar496f9512011-05-19 16:35:09 +02006081 if (!found_start)
6082 is_else = cin_iselse(s);
6083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 while (*s)
6085 {
6086 /* skip over comments, "" strings and 'c'haracters */
6087 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006088 if (*s == '}' && n_open > 0)
6089 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006090 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006091 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 && cin_nocode(s + 1))
6093 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006094 else if (*s == '{')
6095 {
6096 if (incl_open && cin_nocode(s + 1))
6097 return *s;
6098 else
6099 ++n_open;
6100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102 if (*s)
6103 s++;
6104 }
6105 return found_start;
6106}
6107
6108/*
6109 * Recognize the basic picture of a function declaration -- it needs to
6110 * have an open paren somewhere and a close paren at the end of the line and
6111 * no semicolons anywhere.
6112 * When a line ends in a comma we continue looking in the next line.
6113 * "sp" points to a string with the line. When looking at other lines it must
6114 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006115 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006116 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 */
6118 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006119cin_isfuncdecl(
6120 char_u **sp,
6121 linenr_T first_lnum,
6122 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
6124 char_u *s;
6125 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006126 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006128 pos_T *trypos;
6129 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
6131 if (sp == NULL)
6132 s = ml_get(lnum);
6133 else
6134 s = *sp;
6135
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006136 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006137 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006138 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006139 {
6140 lnum = trypos->lnum;
6141 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006142 {
6143 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006144 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006145 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006146
6147 s = ml_get(lnum);
6148 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006149 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006150
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006151 /* Ignore line starting with #. */
6152 if (cin_ispreproc(s))
6153 return FALSE;
6154
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6156 {
6157 if (cin_iscomment(s)) /* ignore comments */
6158 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006159 else if (*s == ':')
6160 {
6161 if (*(s + 1) == ':')
6162 s += 2;
6163 else
6164 /* To avoid a mistake in the following situation:
6165 * A::A(int a, int b)
6166 * : a(0) // <--not a function decl
6167 * , b(0)
6168 * {...
6169 */
6170 return FALSE;
6171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 else
6173 ++s;
6174 }
6175 if (*s != '(')
6176 return FALSE; /* ';', ' or " before any () or no '(' */
6177
6178 while (*s && *s != ';' && *s != '\'' && *s != '"')
6179 {
6180 if (*s == ')' && cin_nocode(s + 1))
6181 {
6182 /* ')' at the end: may have found a match
6183 * Check for he previous line not to end in a backslash:
6184 * #if defined(x) && \
6185 * defined(y)
6186 */
6187 lnum = first_lnum - 1;
6188 s = ml_get(lnum);
6189 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6190 retval = TRUE;
6191 goto done;
6192 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006193 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006195 int comma = (*s == ',');
6196
6197 /* ',' at the end: continue looking in the next line.
6198 * At the end: check for ',' in the next line, for this style:
6199 * func(arg1
6200 * , arg2) */
6201 for (;;)
6202 {
6203 if (lnum >= curbuf->b_ml.ml_line_count)
6204 break;
6205 s = ml_get(++lnum);
6206 if (!cin_ispreproc(s))
6207 break;
6208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (lnum >= curbuf->b_ml.ml_line_count)
6210 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006211 /* Require a comma at end of the line or a comma or ')' at the
6212 * start of next line. */
6213 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006214 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006215 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006216 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
6218 else if (cin_iscomment(s)) /* ignore comments */
6219 s = cin_skipcomment(s);
6220 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006223 just_started = FALSE;
6224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
6226
6227done:
6228 if (lnum != first_lnum && sp != NULL)
6229 *sp = ml_get(first_lnum);
6230
6231 return retval;
6232}
6233
6234 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006235cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006237 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238}
6239
6240 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006241cin_iselse(
6242 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 if (*p == '}') /* accept "} else" */
6245 p = cin_skipcomment(p + 1);
6246 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6247}
6248
6249 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006250cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6253}
6254
6255/*
6256 * Check if this is a "while" that should have a matching "do".
6257 * We only accept a "while (condition) ;", with only white space between the
6258 * ')' and ';'. The condition may be spread over several lines.
6259 */
6260 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006261cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 pos_T cursor_save;
6264 pos_T *trypos;
6265 int retval = FALSE;
6266
6267 p = cin_skipcomment(p);
6268 if (*p == '}') /* accept "} while (cond);" */
6269 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006270 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
6272 cursor_save = curwin->w_cursor;
6273 curwin->w_cursor.lnum = lnum;
6274 curwin->w_cursor.col = 0;
6275 p = ml_get_curline();
6276 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6277 {
6278 ++p;
6279 ++curwin->w_cursor.col;
6280 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006281 if ((trypos = findmatchlimit(NULL, 0, 0,
6282 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6284 retval = TRUE;
6285 curwin->w_cursor = cursor_save;
6286 }
6287 return retval;
6288}
6289
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006290/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006291 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006292 * Return 0 if there is none.
6293 * Otherwise return !0 and update "*poffset" to point to the place where the
6294 * string was found.
6295 */
6296 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006297cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006298{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006299 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006300
6301 if (offset-- < 2)
6302 return 0;
6303 while (offset > 2 && vim_iswhite(line[offset]))
6304 --offset;
6305
6306 offset -= 1;
6307 if (!STRNCMP(line + offset, "if", 2))
6308 goto probablyFound;
6309
6310 if (offset >= 1)
6311 {
6312 offset -= 1;
6313 if (!STRNCMP(line + offset, "for", 3))
6314 goto probablyFound;
6315
6316 if (offset >= 2)
6317 {
6318 offset -= 2;
6319 if (!STRNCMP(line + offset, "while", 5))
6320 goto probablyFound;
6321 }
6322 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006323 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006324
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006325probablyFound:
6326 if (!offset || !vim_isIDc(line[offset - 1]))
6327 {
6328 *poffset = offset;
6329 return 1;
6330 }
6331 return 0;
6332}
6333
6334/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006335 * Return TRUE if we are at the end of a do-while.
6336 * do
6337 * nothing;
6338 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006339 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006340 * Adjust the cursor to the line with "while".
6341 */
6342 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006343cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006344{
6345 char_u *line;
6346 char_u *p;
6347 char_u *s;
6348 pos_T *trypos;
6349 int i;
6350
6351 if (terminated != ';') /* there must be a ';' at the end */
6352 return FALSE;
6353
6354 p = line = ml_get_curline();
6355 while (*p != NUL)
6356 {
6357 p = cin_skipcomment(p);
6358 if (*p == ')')
6359 {
6360 s = skipwhite(p + 1);
6361 if (*s == ';' && cin_nocode(s + 1))
6362 {
6363 /* Found ");" at end of the line, now check there is "while"
6364 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006365 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006366 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006367 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006368 if (trypos != NULL)
6369 {
6370 s = cin_skipcomment(ml_get(trypos->lnum));
6371 if (*s == '}') /* accept "} while (cond);" */
6372 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006373 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006374 {
6375 curwin->w_cursor.lnum = trypos->lnum;
6376 return TRUE;
6377 }
6378 }
6379
6380 /* Searching may have made "line" invalid, get it again. */
6381 line = ml_get_curline();
6382 p = line + i;
6383 }
6384 }
6385 if (*p != NUL)
6386 ++p;
6387 }
6388 return FALSE;
6389}
6390
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006392cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6395}
6396
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006397/*
6398 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 * constructor-initialization. eg:
6400 *
6401 * class MyClass :
6402 * baseClass <-- here
6403 * class MyClass : public baseClass,
6404 * anotherBaseClass <-- here (should probably lineup ??)
6405 * MyClass::MyClass(...) :
6406 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006407 *
6408 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 */
6410 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006411cin_is_cpp_baseclass(
6412 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006414 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 char_u *s;
6416 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006417 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006418 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006420 if (pos->lnum <= lnum)
6421 return cached->found; /* Use the cached result */
6422
6423 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006425 s = skipwhite(line);
6426 if (*s == '#') /* skip #define FOO x ? (x) : x */
6427 return FALSE;
6428 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (*s == NUL)
6430 return FALSE;
6431
6432 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6433
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006434 /* Search for a line starting with '#', empty, ending in ';' or containing
6435 * '{' or '}' and start below it. This handles the following situations:
6436 * a = cond ?
6437 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006438 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006439 * func::foo()
6440 * : something
6441 * {}
6442 * Foo::Foo (int one, int two)
6443 * : something(4),
6444 * somethingelse(3)
6445 * {}
6446 */
6447 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006449 line = ml_get(lnum - 1);
6450 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006451 if (*s == '#' || *s == NUL)
6452 break;
6453 while (*s != NUL)
6454 {
6455 s = cin_skipcomment(s);
6456 if (*s == '{' || *s == '}'
6457 || (*s == ';' && cin_nocode(s + 1)))
6458 break;
6459 if (*s != NUL)
6460 ++s;
6461 }
6462 if (*s != NUL)
6463 break;
6464 --lnum;
6465 }
6466
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006467 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006468 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006469 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006470 for (;;)
6471 {
6472 if (*s == NUL)
6473 {
6474 if (lnum == curwin->w_cursor.lnum)
6475 break;
6476 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006477 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006478 s = line;
6479 }
6480 if (s == line)
6481 {
6482 /* don't recognize "case (foo):" as a baseclass */
6483 if (cin_iscase(s, FALSE))
6484 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006485 s = cin_skipcomment(line);
6486 if (*s == NUL)
6487 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006488 }
6489
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006490 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006491 s = skip_string(s) + 1;
6492 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 {
6494 if (s[1] == ':')
6495 {
6496 /* skip double colon. It can't be a constructor
6497 * initialization any more */
6498 lookfor_ctor_init = FALSE;
6499 s = cin_skipcomment(s + 2);
6500 }
6501 else if (lookfor_ctor_init || class_or_struct)
6502 {
6503 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006504 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 cpp_base_class = TRUE;
6506 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006507 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 s = cin_skipcomment(s + 1);
6509 }
6510 else
6511 s = cin_skipcomment(s + 1);
6512 }
6513 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6514 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6515 {
6516 class_or_struct = TRUE;
6517 lookfor_ctor_init = FALSE;
6518
6519 if (*s == 'c')
6520 s = cin_skipcomment(s + 5);
6521 else
6522 s = cin_skipcomment(s + 6);
6523 }
6524 else
6525 {
6526 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6527 {
6528 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6529 }
6530 else if (s[0] == ')')
6531 {
6532 /* Constructor-initialization is assumed if we come across
6533 * something like "):" */
6534 class_or_struct = FALSE;
6535 lookfor_ctor_init = TRUE;
6536 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006537 else if (s[0] == '?')
6538 {
6539 /* Avoid seeing '() :' after '?' as constructor init. */
6540 return FALSE;
6541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 else if (!vim_isIDc(s[0]))
6543 {
6544 /* if it is not an identifier, we are wrong */
6545 class_or_struct = FALSE;
6546 lookfor_ctor_init = FALSE;
6547 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006548 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
6550 /* it can't be a constructor-initialization any more */
6551 lookfor_ctor_init = FALSE;
6552
6553 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006554 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006555 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006558 /* When the line ends in a comma don't align with it. */
6559 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006560 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006561
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 s = cin_skipcomment(s + 1);
6563 }
6564 }
6565
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006566 cached->found = cpp_base_class;
6567 if (cpp_base_class)
6568 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 return cpp_base_class;
6570}
6571
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006572 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006573get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006574{
6575 int amount;
6576 colnr_T vcol;
6577 pos_T *trypos;
6578
6579 if (col == 0)
6580 {
6581 amount = get_indent();
6582 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006583 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006584 amount = get_indent_lnum(trypos->lnum); /* XXX */
6585 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006586 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006587 }
6588 else
6589 {
6590 curwin->w_cursor.col = col;
6591 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6592 amount = (int)vcol;
6593 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006594 if (amount < curbuf->b_ind_cpp_baseclass)
6595 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006596 return amount;
6597}
6598
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599/*
6600 * Return TRUE if string "s" ends with the string "find", possibly followed by
6601 * white space and comments. Skip strings and comments.
6602 * Ignore "ignore" after "find" if it's not NULL.
6603 */
6604 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006605cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 char_u *p = s;
6608 char_u *r;
6609 int len = (int)STRLEN(find);
6610
6611 while (*p != NUL)
6612 {
6613 p = cin_skipcomment(p);
6614 if (STRNCMP(p, find, len) == 0)
6615 {
6616 r = skipwhite(p + len);
6617 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6618 r = skipwhite(r + STRLEN(ignore));
6619 if (cin_nocode(r))
6620 return TRUE;
6621 }
6622 if (*p != NUL)
6623 ++p;
6624 }
6625 return FALSE;
6626}
6627
6628/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006629 * Return TRUE when "s" starts with "word" and then a non-ID character.
6630 */
6631 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006632cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006633{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006634 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006635
6636 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6637}
6638
6639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 * Skip strings, chars and comments until at or past "trypos".
6641 * Return the column found.
6642 */
6643 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006644cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 char_u *line;
6647 char_u *p;
6648
6649 p = line = ml_get(trypos->lnum);
6650 while (*p && (colnr_T)(p - line) < trypos->col)
6651 {
6652 if (cin_iscomment(p))
6653 p = cin_skipcomment(p);
6654 else
6655 {
6656 p = skip_string(p);
6657 ++p;
6658 }
6659 }
6660 return (int)(p - line);
6661}
6662
6663/*
6664 * Find the '{' at the start of the block we are in.
6665 * Return NULL if no match found.
6666 * Ignore a '{' that is in a comment, makes indenting the next three lines
6667 * work. */
6668/* foo() */
6669/* { */
6670/* } */
6671
6672 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006673find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
6675 pos_T cursor_save;
6676 pos_T *trypos;
6677 pos_T *pos;
6678 static pos_T pos_copy;
6679
6680 cursor_save = curwin->w_cursor;
6681 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6682 {
6683 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6684 trypos = &pos_copy;
6685 curwin->w_cursor = *trypos;
6686 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006687 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006689 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 break;
6691 if (pos != NULL)
6692 curwin->w_cursor.lnum = pos->lnum;
6693 }
6694 curwin->w_cursor = cursor_save;
6695 return trypos;
6696}
6697
6698/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006699 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006700 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
6702 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006703find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006705 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006706}
6707
6708 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006709find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006710{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 pos_T cursor_save;
6712 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006713 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006714 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006717 ind_maxp_wk = ind_maxparen;
6718retry:
6719 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {
6721 /* check if the ( is in a // comment */
6722 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006723 {
6724 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6725 if (ind_maxp_wk > 0)
6726 {
6727 curwin->w_cursor = *trypos;
6728 curwin->w_cursor.col = 0; /* XXX */
6729 goto retry;
6730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 else
6734 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006735 pos_T *trypos_wk;
6736
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6738 trypos = &pos_copy;
6739 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006740 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006741 {
6742 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6743 - trypos_wk->lnum);
6744 if (ind_maxp_wk > 0)
6745 {
6746 curwin->w_cursor = *trypos_wk;
6747 goto retry;
6748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 }
6752 }
6753 curwin->w_cursor = cursor_save;
6754 return trypos;
6755}
6756
6757/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006758 * Find the matching '(', ignoring it if it is in a comment or before an
6759 * unmatched {.
6760 * Return NULL if no match found.
6761 */
6762 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006763find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006764{
6765 pos_T *trypos = find_match_paren(ind_maxparen);
6766
6767 if (trypos != NULL)
6768 {
6769 pos_T *tryposBrace = find_start_brace();
6770
6771 /* If both an unmatched '(' and '{' is found. Ignore the '('
6772 * position if the '{' is further down. */
6773 if (tryposBrace != NULL
6774 && (trypos->lnum != tryposBrace->lnum
6775 ? trypos->lnum < tryposBrace->lnum
6776 : trypos->col < tryposBrace->col))
6777 trypos = NULL;
6778 }
6779 return trypos;
6780}
6781
6782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 * Return ind_maxparen corrected for the difference in line number between the
6784 * cursor position and "startpos". This makes sure that searching for a
6785 * matching paren above the cursor line doesn't find a match because of
6786 * looking a few lines further.
6787 */
6788 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006789corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6792
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006793 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6794 return curbuf->b_ind_maxparen - (int)n;
6795 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796}
6797
6798/*
6799 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006800 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 */
6802 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006803find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 int i;
6806 int retval = FALSE;
6807 int open_count = 0;
6808
6809 curwin->w_cursor.col = 0; /* default is start of line */
6810
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006811 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
6813 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6814 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6815 if (l[i] == start)
6816 ++open_count;
6817 else if (l[i] == end)
6818 {
6819 if (open_count > 0)
6820 --open_count;
6821 else
6822 {
6823 curwin->w_cursor.col = i;
6824 retval = TRUE;
6825 }
6826 }
6827 }
6828 return retval;
6829}
6830
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006831/*
6832 * Parse 'cinoptions' and set the values in "curbuf".
6833 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6834 */
6835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006836parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006837{
6838 char_u *p;
6839 char_u *l;
6840 char_u *digits;
6841 int n;
6842 int divider;
6843 int fraction = 0;
6844 int sw = (int)get_sw_value(buf);
6845
6846 /*
6847 * Set the default values.
6848 */
6849 /* Spaces from a block's opening brace the prevailing indent for that
6850 * block should be. */
6851 buf->b_ind_level = sw;
6852
6853 /* Spaces from the edge of the line an open brace that's at the end of a
6854 * line is imagined to be. */
6855 buf->b_ind_open_imag = 0;
6856
6857 /* Spaces from the prevailing indent for a line that is not preceded by
6858 * an opening brace. */
6859 buf->b_ind_no_brace = 0;
6860
6861 /* Column where the first { of a function should be located }. */
6862 buf->b_ind_first_open = 0;
6863
6864 /* Spaces from the prevailing indent a leftmost open brace should be
6865 * located. */
6866 buf->b_ind_open_extra = 0;
6867
6868 /* Spaces from the matching open brace (real location for one at the left
6869 * edge; imaginary location from one that ends a line) the matching close
6870 * brace should be located. */
6871 buf->b_ind_close_extra = 0;
6872
6873 /* Spaces from the edge of the line an open brace sitting in the leftmost
6874 * column is imagined to be. */
6875 buf->b_ind_open_left_imag = 0;
6876
6877 /* Spaces jump labels should be shifted to the left if N is non-negative,
6878 * otherwise the jump label will be put to column 1. */
6879 buf->b_ind_jump_label = -1;
6880
6881 /* Spaces from the switch() indent a "case xx" label should be located. */
6882 buf->b_ind_case = sw;
6883
6884 /* Spaces from the "case xx:" code after a switch() should be located. */
6885 buf->b_ind_case_code = sw;
6886
6887 /* Lineup break at end of case in switch() with case label. */
6888 buf->b_ind_case_break = 0;
6889
6890 /* Spaces from the class declaration indent a scope declaration label
6891 * should be located. */
6892 buf->b_ind_scopedecl = sw;
6893
6894 /* Spaces from the scope declaration label code should be located. */
6895 buf->b_ind_scopedecl_code = sw;
6896
6897 /* Amount K&R-style parameters should be indented. */
6898 buf->b_ind_param = sw;
6899
6900 /* Amount a function type spec should be indented. */
6901 buf->b_ind_func_type = sw;
6902
6903 /* Amount a cpp base class declaration or constructor initialization
6904 * should be indented. */
6905 buf->b_ind_cpp_baseclass = sw;
6906
6907 /* additional spaces beyond the prevailing indent a continuation line
6908 * should be located. */
6909 buf->b_ind_continuation = sw;
6910
6911 /* Spaces from the indent of the line with an unclosed parentheses. */
6912 buf->b_ind_unclosed = sw * 2;
6913
6914 /* Spaces from the indent of the line with an unclosed parentheses, which
6915 * itself is also unclosed. */
6916 buf->b_ind_unclosed2 = sw;
6917
6918 /* Suppress ignoring spaces from the indent of a line starting with an
6919 * unclosed parentheses. */
6920 buf->b_ind_unclosed_noignore = 0;
6921
6922 /* If the opening paren is the last nonwhite character on the line, and
6923 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6924 * context (for very long lines). */
6925 buf->b_ind_unclosed_wrapped = 0;
6926
6927 /* Suppress ignoring white space when lining up with the character after
6928 * an unclosed parentheses. */
6929 buf->b_ind_unclosed_whiteok = 0;
6930
6931 /* Indent a closing parentheses under the line start of the matching
6932 * opening parentheses. */
6933 buf->b_ind_matching_paren = 0;
6934
6935 /* Indent a closing parentheses under the previous line. */
6936 buf->b_ind_paren_prev = 0;
6937
6938 /* Extra indent for comments. */
6939 buf->b_ind_comment = 0;
6940
6941 /* Spaces from the comment opener when there is nothing after it. */
6942 buf->b_ind_in_comment = 3;
6943
6944 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6945 * after the comment opener. */
6946 buf->b_ind_in_comment2 = 0;
6947
6948 /* Max lines to search for an open paren. */
6949 buf->b_ind_maxparen = 20;
6950
6951 /* Max lines to search for an open comment. */
6952 buf->b_ind_maxcomment = 70;
6953
6954 /* Handle braces for java code. */
6955 buf->b_ind_java = 0;
6956
6957 /* Not to confuse JS object properties with labels. */
6958 buf->b_ind_js = 0;
6959
6960 /* Handle blocked cases correctly. */
6961 buf->b_ind_keep_case_label = 0;
6962
6963 /* Handle C++ namespace. */
6964 buf->b_ind_cpp_namespace = 0;
6965
6966 /* Handle continuation lines containing conditions of if(), for() and
6967 * while(). */
6968 buf->b_ind_if_for_while = 0;
6969
6970 for (p = buf->b_p_cino; *p; )
6971 {
6972 l = p++;
6973 if (*p == '-')
6974 ++p;
6975 digits = p; /* remember where the digits start */
6976 n = getdigits(&p);
6977 divider = 0;
6978 if (*p == '.') /* ".5s" means a fraction */
6979 {
6980 fraction = atol((char *)++p);
6981 while (VIM_ISDIGIT(*p))
6982 {
6983 ++p;
6984 if (divider)
6985 divider *= 10;
6986 else
6987 divider = 10;
6988 }
6989 }
6990 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6991 {
6992 if (p == digits)
6993 n = sw; /* just "s" is one 'shiftwidth' */
6994 else
6995 {
6996 n *= sw;
6997 if (divider)
6998 n += (sw * fraction + divider / 2) / divider;
6999 }
7000 ++p;
7001 }
7002 if (l[1] == '-')
7003 n = -n;
7004
7005 /* When adding an entry here, also update the default 'cinoptions' in
7006 * doc/indent.txt, and add explanation for it! */
7007 switch (*l)
7008 {
7009 case '>': buf->b_ind_level = n; break;
7010 case 'e': buf->b_ind_open_imag = n; break;
7011 case 'n': buf->b_ind_no_brace = n; break;
7012 case 'f': buf->b_ind_first_open = n; break;
7013 case '{': buf->b_ind_open_extra = n; break;
7014 case '}': buf->b_ind_close_extra = n; break;
7015 case '^': buf->b_ind_open_left_imag = n; break;
7016 case 'L': buf->b_ind_jump_label = n; break;
7017 case ':': buf->b_ind_case = n; break;
7018 case '=': buf->b_ind_case_code = n; break;
7019 case 'b': buf->b_ind_case_break = n; break;
7020 case 'p': buf->b_ind_param = n; break;
7021 case 't': buf->b_ind_func_type = n; break;
7022 case '/': buf->b_ind_comment = n; break;
7023 case 'c': buf->b_ind_in_comment = n; break;
7024 case 'C': buf->b_ind_in_comment2 = n; break;
7025 case 'i': buf->b_ind_cpp_baseclass = n; break;
7026 case '+': buf->b_ind_continuation = n; break;
7027 case '(': buf->b_ind_unclosed = n; break;
7028 case 'u': buf->b_ind_unclosed2 = n; break;
7029 case 'U': buf->b_ind_unclosed_noignore = n; break;
7030 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7031 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7032 case 'm': buf->b_ind_matching_paren = n; break;
7033 case 'M': buf->b_ind_paren_prev = n; break;
7034 case ')': buf->b_ind_maxparen = n; break;
7035 case '*': buf->b_ind_maxcomment = n; break;
7036 case 'g': buf->b_ind_scopedecl = n; break;
7037 case 'h': buf->b_ind_scopedecl_code = n; break;
7038 case 'j': buf->b_ind_java = n; break;
7039 case 'J': buf->b_ind_js = n; break;
7040 case 'l': buf->b_ind_keep_case_label = n; break;
7041 case '#': buf->b_ind_hash_comment = n; break;
7042 case 'N': buf->b_ind_cpp_namespace = n; break;
7043 case 'k': buf->b_ind_if_for_while = n; break;
7044 }
7045 if (*p == ',')
7046 ++p;
7047 }
7048}
7049
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007050/*
7051 * Return the desired indent for C code.
7052 * Return -1 if the indent should be left alone (inside a raw string).
7053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007055get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 pos_T cur_curpos;
7058 int amount;
7059 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007060 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 colnr_T col;
7062 char_u *theline;
7063 char_u *linecopy;
7064 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007065 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007067 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 pos_T our_paren_pos;
7069 char_u *start;
7070 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007071#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#define BRACE_AT_START 2 /* '{' is at start of line */
7073#define BRACE_AT_END 3 /* '{' is at end of line */
7074 linenr_T ourscope;
7075 char_u *l;
7076 char_u *look;
7077 char_u terminated;
7078 int lookfor;
7079#define LOOKFOR_INITIAL 0
7080#define LOOKFOR_IF 1
7081#define LOOKFOR_DO 2
7082#define LOOKFOR_CASE 3
7083#define LOOKFOR_ANY 4
7084#define LOOKFOR_TERM 5
7085#define LOOKFOR_UNTERM 6
7086#define LOOKFOR_SCOPEDECL 7
7087#define LOOKFOR_NOBREAK 8
7088#define LOOKFOR_CPP_BASECLASS 9
7089#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007090#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007091#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
7093 int whilelevel;
7094 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 int n;
7096 int iscase;
7097 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007098 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007100 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007101 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007102 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007103 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007105 /* make a copy, value is changed below */
7106 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 /* remember where the cursor was when we started */
7109 cur_curpos = curwin->w_cursor;
7110
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007111 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007112 if (cur_curpos.lnum == 1)
7113 return 0;
7114
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 /* Get a copy of the current contents of the line.
7116 * This is required, because only the most recent line obtained with
7117 * ml_get is valid! */
7118 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7119 if (linecopy == NULL)
7120 return 0;
7121
7122 /*
7123 * In insert mode and the cursor is on a ')' truncate the line at the
7124 * cursor position. We don't want to line up with the matching '(' when
7125 * inserting new stuff.
7126 * For unknown reasons the cursor might be past the end of the line, thus
7127 * check for that.
7128 */
7129 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007130 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 && linecopy[curwin->w_cursor.col] == ')')
7132 linecopy[curwin->w_cursor.col] = NUL;
7133
7134 theline = skipwhite(linecopy);
7135
7136 /* move the cursor to the start of the line */
7137
7138 curwin->w_cursor.col = 0;
7139
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007140 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007141
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007143 * If we are inside a raw string don't change the indent.
7144 * Ignore a raw string inside a comment.
7145 */
7146 comment_pos = ind_find_start_comment();
7147 if (comment_pos != NULL)
7148 {
7149 /* findmatchlimit() static pos is overwritten, make a copy */
7150 tryposCopy = *comment_pos;
7151 comment_pos = &tryposCopy;
7152 }
7153 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7154 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7155 {
7156 amount = -1;
7157 goto laterend;
7158 }
7159
7160 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 * #defines and so on always go at the left when included in 'cinkeys'.
7162 */
7163 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007164 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007165 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007166 goto theend;
7167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
7169 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007170 * Is it a non-case label? Then that goes at the left margin too unless:
7171 * - JS flag is set.
7172 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007174 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007175 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
7177 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007178 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
7180
7181 /*
7182 * If we're inside a "//" comment and there is a "//" comment in a
7183 * previous line, lineup with that one.
7184 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007185 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 && (trypos = find_line_comment()) != NULL) /* XXX */
7187 {
7188 /* find how indented the line beginning the comment is */
7189 getvcol(curwin, trypos, &col, NULL, NULL);
7190 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007191 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193
7194 /*
7195 * If we're inside a comment and not looking at the start of the
7196 * comment, try using the 'comments' option.
7197 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007198 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
7200 int lead_start_len = 2;
7201 int lead_middle_len = 1;
7202 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7203 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7204 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7205 char_u *p;
7206 int start_align = 0;
7207 int start_off = 0;
7208 int done = FALSE;
7209
7210 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007211 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007213 *lead_start = NUL;
7214 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
7216 p = curbuf->b_p_com;
7217 while (*p != NUL)
7218 {
7219 int align = 0;
7220 int off = 0;
7221 int what = 0;
7222
7223 while (*p != NUL && *p != ':')
7224 {
7225 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7226 what = *p++;
7227 else if (*p == COM_LEFT || *p == COM_RIGHT)
7228 align = *p++;
7229 else if (VIM_ISDIGIT(*p) || *p == '-')
7230 off = getdigits(&p);
7231 else
7232 ++p;
7233 }
7234
7235 if (*p == ':')
7236 ++p;
7237 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7238 if (what == COM_START)
7239 {
7240 STRCPY(lead_start, lead_end);
7241 lead_start_len = (int)STRLEN(lead_start);
7242 start_off = off;
7243 start_align = align;
7244 }
7245 else if (what == COM_MIDDLE)
7246 {
7247 STRCPY(lead_middle, lead_end);
7248 lead_middle_len = (int)STRLEN(lead_middle);
7249 }
7250 else if (what == COM_END)
7251 {
7252 /* If our line starts with the middle comment string, line it
7253 * up with the comment opener per the 'comments' option. */
7254 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7255 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7256 {
7257 done = TRUE;
7258 if (curwin->w_cursor.lnum > 1)
7259 {
7260 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007261 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 * the middle comment string matches in the previous
7263 * line, use the indent of that line. XXX */
7264 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7265 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7266 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7267 else if (STRNCMP(look, lead_middle,
7268 lead_middle_len) == 0)
7269 {
7270 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7271 break;
7272 }
7273 /* If the start comment string doesn't match with the
7274 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007275 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 lead_start, lead_start_len) != 0)
7277 continue;
7278 }
7279 if (start_off != 0)
7280 amount += start_off;
7281 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007282 amount += vim_strsize(lead_start)
7283 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 break;
7285 }
7286
7287 /* If our line starts with the end comment string, line it up
7288 * with the middle comment */
7289 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7290 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7291 {
7292 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7293 /* XXX */
7294 if (off != 0)
7295 amount += off;
7296 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007297 amount += vim_strsize(lead_start)
7298 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 done = TRUE;
7300 break;
7301 }
7302 }
7303 }
7304
7305 /* If our line starts with an asterisk, line up with the
7306 * asterisk in the comment opener; otherwise, line up
7307 * with the first character of the comment text.
7308 */
7309 if (done)
7310 ;
7311 else if (theline[0] == '*')
7312 amount += 1;
7313 else
7314 {
7315 /*
7316 * If we are more than one line away from the comment opener, take
7317 * the indent of the previous non-empty line. If 'cino' has "CO"
7318 * and we are just below the comment opener and there are any
7319 * white characters after it line up with the text after it;
7320 * otherwise, add the amount specified by "c" in 'cino'
7321 */
7322 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007323 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 {
7325 if (linewhite(lnum)) /* skip blank lines */
7326 continue;
7327 amount = get_indent_lnum(lnum); /* XXX */
7328 break;
7329 }
7330 if (amount == -1) /* use the comment opener */
7331 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007332 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007334 start = ml_get(comment_pos->lnum);
7335 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007337 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007339 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007341 if (curbuf->b_ind_in_comment2 || *look == NUL)
7342 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 }
7344 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007345 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 }
7347
7348 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007349 * Are we looking at a ']' that has a match?
7350 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007351 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007352 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7353 {
7354 /* align with the line containing the '['. */
7355 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007356 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007357 }
7358
7359 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 * Are we inside parentheses or braces?
7361 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007362 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007363 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007364 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 || trypos != NULL)
7366 {
7367 if (trypos != NULL && tryposBrace != NULL)
7368 {
7369 /* Both an unmatched '(' and '{' is found. Use the one which is
7370 * closer to the current cursor position, set the other to NULL. */
7371 if (trypos->lnum != tryposBrace->lnum
7372 ? trypos->lnum < tryposBrace->lnum
7373 : trypos->col < tryposBrace->col)
7374 trypos = NULL;
7375 else
7376 tryposBrace = NULL;
7377 }
7378
7379 if (trypos != NULL)
7380 {
7381 /*
7382 * If the matching paren is more than one line away, use the indent of
7383 * a previous non-empty line that matches the same paren.
7384 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007385 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007387 /* Line up with the start of the matching paren line. */
7388 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7389 }
7390 else
7391 {
7392 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007393 our_paren_pos = *trypos;
7394 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007396 l = skipwhite(ml_get(lnum));
7397 if (cin_nocode(l)) /* skip comment lines */
7398 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007399 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007400 continue; /* ignore #define, #if, etc. */
7401 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007403 /* Skip a comment or raw string. XXX */
7404 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007405 {
7406 lnum = trypos->lnum + 1;
7407 continue;
7408 }
7409
7410 /* XXX */
7411 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007412 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007413 && trypos->lnum == our_paren_pos.lnum
7414 && trypos->col == our_paren_pos.col)
7415 {
7416 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007418 if (theline[0] == ')')
7419 {
7420 if (our_paren_pos.lnum != lnum
7421 && cur_amount > amount)
7422 cur_amount = amount;
7423 amount = -1;
7424 }
7425 break;
7426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 }
7429
7430 /*
7431 * Line up with line where the matching paren is. XXX
7432 * If the line starts with a '(' or the indent for unclosed
7433 * parentheses is zero, line up with the unclosed parentheses.
7434 */
7435 if (amount == -1)
7436 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007437 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007438 int is_if_for_while = 0;
7439
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007440 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007441 {
7442 /* Look for the outermost opening parenthesis on this line
7443 * and check whether it belongs to an "if", "for" or "while". */
7444
7445 pos_T cursor_save = curwin->w_cursor;
7446 pos_T outermost;
7447 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007448
7449 trypos = &our_paren_pos;
7450 do {
7451 outermost = *trypos;
7452 curwin->w_cursor.lnum = outermost.lnum;
7453 curwin->w_cursor.col = outermost.col;
7454
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007455 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007456 } while (trypos && trypos->lnum == outermost.lnum);
7457
7458 curwin->w_cursor = cursor_save;
7459
7460 line = ml_get(outermost.lnum);
7461
7462 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007463 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007464 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007465
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007466 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007467 look = skipwhite(look);
7468 if (*look == '(')
7469 {
7470 linenr_T save_lnum = curwin->w_cursor.lnum;
7471 char_u *line;
7472 int look_col;
7473
7474 /* Ignore a '(' in front of the line that has a match before
7475 * our matching '('. */
7476 curwin->w_cursor.lnum = our_paren_pos.lnum;
7477 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007478 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007479 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007480 if ((trypos = findmatchlimit(NULL, ')', 0,
7481 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007482 != NULL
7483 && trypos->lnum == our_paren_pos.lnum
7484 && trypos->col < our_paren_pos.col)
7485 ignore_paren_col = trypos->col + 1;
7486
7487 curwin->w_cursor.lnum = save_lnum;
7488 look = ml_get(our_paren_pos.lnum) + look_col;
7489 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007490 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7491 && is_if_for_while == 0)
7492 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007493 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 {
7495 /*
7496 * If we're looking at a close paren, line up right there;
7497 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007498 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 * the last nonwhite character of the line, use either the
7500 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007501 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 * lines).
7503 */
7504 if (theline[0] != ')')
7505 {
7506 cur_amount = MAXCOL;
7507 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007508 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 && cin_ends_in(l, (char_u *)"(", NULL))
7510 {
7511 /* look for opening unmatched paren, indent one level
7512 * for each additional level */
7513 n = 1;
7514 for (col = 0; col < our_paren_pos.col; ++col)
7515 {
7516 switch (l[col])
7517 {
7518 case '(':
7519 case '{': ++n;
7520 break;
7521
7522 case ')':
7523 case '}': if (n > 1)
7524 --n;
7525 break;
7526 }
7527 }
7528
7529 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007530 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007532 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 our_paren_pos.col++;
7534 else
7535 {
7536 col = our_paren_pos.col + 1;
7537 while (vim_iswhite(l[col]))
7538 col++;
7539 if (l[col] != NUL) /* In case of trailing space */
7540 our_paren_pos.col = col;
7541 else
7542 our_paren_pos.col++;
7543 }
7544 }
7545
7546 /*
7547 * Find how indented the paren is, or the character after it
7548 * if we did the above "if".
7549 */
7550 if (our_paren_pos.col > 0)
7551 {
7552 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7553 if (cur_amount > (int)col)
7554 cur_amount = col;
7555 }
7556 }
7557
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007558 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
7560 /* Line up with the start of the matching paren line. */
7561 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007562 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7563 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007564 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 {
7566 if (cur_amount != MAXCOL)
7567 amount = cur_amount;
7568 }
7569 else
7570 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007571 /* Add b_ind_unclosed2 for each '(' before our matching one,
7572 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007574 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
7576 --our_paren_pos.col;
7577 switch (*ml_get_pos(&our_paren_pos))
7578 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007579 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 col = our_paren_pos.col;
7581 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007582 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 col = MAXCOL;
7584 break;
7585 }
7586 }
7587
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007588 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 * braces */
7590 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007591 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 else
7593 {
7594 curwin->w_cursor.lnum = our_paren_pos.lnum;
7595 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007596 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7597 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007598 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007600 {
7601 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007602 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007603 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007604 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607 /*
7608 * For a line starting with ')' use the minimum of the two
7609 * positions, to avoid giving it more indent than the previous
7610 * lines:
7611 * func_long_name( if (x
7612 * arg && yy
7613 * ) ^ not here ) ^ not here
7614 */
7615 if (cur_amount < amount)
7616 amount = cur_amount;
7617 }
7618 }
7619
7620 /* add extra indent for a comment */
7621 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007622 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 else
7625 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007626 /*
7627 * We are inside braces, there is a { before this line at the position
7628 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007629 * Make a copy of tryposBrace, it may point to pos_copy inside
7630 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007631 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007632 tryposCopy = *tryposBrace;
7633 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 ourscope = trypos->lnum;
7636 start = ml_get(ourscope);
7637
7638 /*
7639 * Now figure out how indented the line is in general.
7640 * If the brace was at the start of the line, we use that;
7641 * otherwise, check out the indentation of the line as
7642 * a whole and then add the "imaginary indent" to that.
7643 */
7644 look = skipwhite(start);
7645 if (*look == '{')
7646 {
7647 getvcol(curwin, trypos, &col, NULL, NULL);
7648 amount = col;
7649 if (*start == '{')
7650 start_brace = BRACE_IN_COL0;
7651 else
7652 start_brace = BRACE_AT_START;
7653 }
7654 else
7655 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007656 /* That opening brace might have been on a continuation
7657 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 curwin->w_cursor.lnum = ourscope;
7659
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007660 /* Position the cursor over the rightmost paren, so that
7661 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 lnum = ourscope;
7663 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007664 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7665 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 lnum = trypos->lnum;
7667
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007668 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 * case 1: if (asdf &&
7670 * ldfd) {
7671 * }
7672 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007673 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7674 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007676 else if (curbuf->b_ind_js)
7677 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007679 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681 start_brace = BRACE_AT_END;
7682 }
7683
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007684 /* For Javascript check if the line starts with "key:". */
7685 if (curbuf->b_ind_js)
7686 js_cur_has_key = cin_has_js_key(theline);
7687
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007689 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 * we want to be. otherwise, add the amount of room
7691 * that an indent is supposed to be.
7692 */
7693 if (theline[0] == '}')
7694 {
7695 /*
7696 * they may want closing braces to line up with something
7697 * other than the open brace. indulge them, if so.
7698 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007699 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701 else
7702 {
7703 /*
7704 * If we're looking at an "else", try to find an "if"
7705 * to match it with.
7706 * If we're looking at a "while", try to find a "do"
7707 * to match it with.
7708 */
7709 lookfor = LOOKFOR_INITIAL;
7710 if (cin_iselse(theline))
7711 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007712 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 lookfor = LOOKFOR_DO;
7714 if (lookfor != LOOKFOR_INITIAL)
7715 {
7716 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007717 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 {
7719 amount = get_indent(); /* XXX */
7720 goto theend;
7721 }
7722 }
7723
7724 /*
7725 * We get here if we are not on an "while-of-do" or "else" (or
7726 * failed to find a matching "if").
7727 * Search backwards for something to line up with.
7728 * First set amount for when we don't find anything.
7729 */
7730
7731 /*
7732 * if the '{' is _really_ at the left margin, use the imaginary
7733 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007734 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 */
7736
7737 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7738 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007739 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007740 lookfor_cpp_namespace = TRUE;
7741 }
7742 else if (start_brace == BRACE_AT_START &&
7743 lookfor_cpp_namespace) /* '{' is at start */
7744 {
7745
7746 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 }
7748 else
7749 {
7750 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007751 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007752 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007753
7754 l = skipwhite(ml_get_curline());
7755 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007756 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 else
7759 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007760 /* Compensate for adding b_ind_open_extra later. */
7761 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 if (amount < 0)
7763 amount = 0;
7764 }
7765 }
7766
7767 lookfor_break = FALSE;
7768
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007769 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {
7771 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007772 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 }
7774 else if (cin_isscopedecl(theline)) /* private:, ... */
7775 {
7776 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007777 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
7779 else
7780 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007781 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7782 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 lookfor_break = TRUE;
7784
7785 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007786 /* b_ind_level from start of block */
7787 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
7789 scope_amount = amount;
7790 whilelevel = 0;
7791
7792 /*
7793 * Search backwards. If we find something we recognize, line up
7794 * with that.
7795 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007796 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 * the usual amount relative to the conditional
7798 * that opens the block.
7799 */
7800 curwin->w_cursor = cur_curpos;
7801 for (;;)
7802 {
7803 curwin->w_cursor.lnum--;
7804 curwin->w_cursor.col = 0;
7805
7806 /*
7807 * If we went all the way back to the start of our scope, line
7808 * up with it.
7809 */
7810 if (curwin->w_cursor.lnum <= ourscope)
7811 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007812 /* We reached end of scope:
7813 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007815 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 * don't add ind_continuation, otherwise it is a variable
7817 * declaration:
7818 * int x,
7819 * here; <-- add ind_continuation
7820 */
7821 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7822 {
7823 if (curwin->w_cursor.lnum == 0
7824 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007825 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007827 /* nothing found (abuse curbuf->b_ind_maxparen as
7828 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 * initialization) */
7830 if (cont_amount > 0)
7831 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007832 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 amount += ind_continuation;
7834 break;
7835 }
7836
7837 l = ml_get_curline();
7838
7839 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007840 * If we're in a comment or raw string now, skip to
7841 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007843 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 if (trypos != NULL)
7845 {
7846 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007847 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 continue;
7849 }
7850
7851 /*
7852 * Skip preprocessor directives and blank lines.
7853 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007854 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7855 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 continue;
7857
7858 if (cin_nocode(l))
7859 continue;
7860
7861 terminated = cin_isterminated(l, FALSE, TRUE);
7862
7863 /*
7864 * If we are at top level and the line looks like a
7865 * function declaration, we are done
7866 * (it's a variable declaration).
7867 */
7868 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007869 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
7871 /* if the line is terminated with another ','
7872 * it is a continued variable initialization.
7873 * don't add extra indent.
7874 * TODO: does not work, if a function
7875 * declaration is split over multiple lines:
7876 * cin_isfuncdecl returns FALSE then.
7877 */
7878 if (terminated == ',')
7879 break;
7880
7881 /* if it es a enum declaration or an assignment,
7882 * we are done.
7883 */
7884 if (terminated != ';' && cin_isinit())
7885 break;
7886
7887 /* nothing useful found */
7888 if (terminated == 0 || terminated == '{')
7889 continue;
7890 }
7891
7892 if (terminated != ';')
7893 {
7894 /* Skip parens and braces. Position the cursor
7895 * over the rightmost paren, so that matching it
7896 * will take us back to the start of the line.
7897 */ /* XXX */
7898 trypos = NULL;
7899 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007900 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007901 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902
7903 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007904 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
7906 if (trypos != NULL)
7907 {
7908 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007909 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 continue;
7911 }
7912 }
7913
7914 /* it's a variable declaration, add indentation
7915 * like in
7916 * int a,
7917 * b;
7918 */
7919 if (cont_amount > 0)
7920 amount = cont_amount;
7921 else
7922 amount += ind_continuation;
7923 }
7924 else if (lookfor == LOOKFOR_UNTERM)
7925 {
7926 if (cont_amount > 0)
7927 amount = cont_amount;
7928 else
7929 amount += ind_continuation;
7930 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007931 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007932 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007933 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007934 && lookfor != LOOKFOR_CPP_BASECLASS
7935 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007936 {
7937 amount = scope_amount;
7938 if (theline[0] == '{')
7939 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007940 amount += curbuf->b_ind_open_extra;
7941 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007942 }
7943 }
7944
7945 if (lookfor_cpp_namespace)
7946 {
7947 /*
7948 * Looking for C++ namespace, need to look further
7949 * back.
7950 */
7951 if (curwin->w_cursor.lnum == ourscope)
7952 continue;
7953
7954 if (curwin->w_cursor.lnum == 0
7955 || curwin->w_cursor.lnum
7956 < ourscope - FIND_NAMESPACE_LIM)
7957 break;
7958
7959 l = ml_get_curline();
7960
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007961 /* If we're in a comment or raw string now, skip
7962 * to the start of it. */
7963 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007964 if (trypos != NULL)
7965 {
7966 curwin->w_cursor.lnum = trypos->lnum + 1;
7967 curwin->w_cursor.col = 0;
7968 continue;
7969 }
7970
7971 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007972 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7973 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02007974 continue;
7975
7976 /* Finally the actual check for "namespace". */
7977 if (cin_is_cpp_namespace(l))
7978 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007979 amount += curbuf->b_ind_cpp_namespace
7980 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007981 break;
7982 }
7983
7984 if (cin_nocode(l))
7985 continue;
7986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 }
7988 break;
7989 }
7990
7991 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007992 * If we're in a comment or raw string now, skip to the start
7993 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007995 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 {
7997 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007998 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 continue;
8000 }
8001
8002 l = ml_get_curline();
8003
8004 /*
8005 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008006 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008008 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (iscase || cin_isscopedecl(l))
8010 {
8011 /* we are only looking for cpp base class
8012 * declaration/initialization any longer */
8013 if (lookfor == LOOKFOR_CPP_BASECLASS)
8014 break;
8015
8016 /* When looking for a "do" we are not interested in
8017 * labels. */
8018 if (whilelevel > 0)
8019 continue;
8020
8021 /*
8022 * case xx:
8023 * c = 99 + <- this indent plus continuation
8024 *-> here;
8025 */
8026 if (lookfor == LOOKFOR_UNTERM
8027 || lookfor == LOOKFOR_ENUM_OR_INIT)
8028 {
8029 if (cont_amount > 0)
8030 amount = cont_amount;
8031 else
8032 amount += ind_continuation;
8033 break;
8034 }
8035
8036 /*
8037 * case xx: <- line up with this case
8038 * x = 333;
8039 * case yy:
8040 */
8041 if ( (iscase && lookfor == LOOKFOR_CASE)
8042 || (iscase && lookfor_break)
8043 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8044 {
8045 /*
8046 * Check that this case label is not for another
8047 * switch()
8048 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008049 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008050 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {
8052 amount = get_indent(); /* XXX */
8053 break;
8054 }
8055 continue;
8056 }
8057
8058 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8059
8060 /*
8061 * case xx: if (cond) <- line up with this if
8062 * y = y + 1;
8063 * -> s = 99;
8064 *
8065 * case xx:
8066 * if (cond) <- line up with this line
8067 * y = y + 1;
8068 * -> s = 99;
8069 */
8070 if (lookfor == LOOKFOR_TERM)
8071 {
8072 if (n)
8073 amount = n;
8074
8075 if (!lookfor_break)
8076 break;
8077 }
8078
8079 /*
8080 * case xx: x = x + 1; <- line up with this x
8081 * -> y = y + 1;
8082 *
8083 * case xx: if (cond) <- line up with this if
8084 * -> y = y + 1;
8085 */
8086 if (n)
8087 {
8088 amount = n;
8089 l = after_label(ml_get_curline());
8090 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008091 {
8092 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008093 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008094 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008095 amount += curbuf->b_ind_level
8096 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 break;
8099 }
8100
8101 /*
8102 * Try to get the indent of a statement before the switch
8103 * label. If nothing is found, line up relative to the
8104 * switch label.
8105 * break; <- may line up with this line
8106 * case xx:
8107 * -> y = 1;
8108 */
8109 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008110 ? curbuf->b_ind_case_code
8111 : curbuf->b_ind_scopedecl_code);
8112 lookfor = curbuf->b_ind_case_break
8113 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 continue;
8115 }
8116
8117 /*
8118 * Looking for a switch() label or C++ scope declaration,
8119 * ignore other lines, skip {}-blocks.
8120 */
8121 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8122 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008123 if (find_last_paren(l, '{', '}')
8124 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008127 curwin->w_cursor.col = 0;
8128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 continue;
8130 }
8131
8132 /*
8133 * Ignore jump labels with nothing after them.
8134 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008135 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {
8137 l = after_label(ml_get_curline());
8138 if (l == NULL || cin_nocode(l))
8139 continue;
8140 }
8141
8142 /*
8143 * Ignore #defines, #if, etc.
8144 * Ignore comment and empty lines.
8145 * (need to get the line again, cin_islabel() may have
8146 * unlocked it)
8147 */
8148 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008149 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 || cin_nocode(l))
8151 continue;
8152
8153 /*
8154 * Are we at the start of a cpp base class declaration or
8155 * constructor initialization?
8156 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008157 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008158 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008159 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008160 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008161 l = ml_get_curline();
8162 }
8163 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {
8165 if (lookfor == LOOKFOR_UNTERM)
8166 {
8167 if (cont_amount > 0)
8168 amount = cont_amount;
8169 else
8170 amount += ind_continuation;
8171 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008172 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008174 /* Need to find start of the declaration. */
8175 lookfor = LOOKFOR_UNTERM;
8176 ind_continuation = 0;
8177 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008180 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008181 amount = get_baseclass_amount(
8182 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 break;
8184 }
8185 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8186 {
8187 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008188 * declaration or initialization before the opening brace.
8189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 if (cin_isterminated(l, TRUE, FALSE))
8191 break;
8192 else
8193 continue;
8194 }
8195
8196 /*
8197 * What happens next depends on the line being terminated.
8198 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008199 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 * 123,
8201 * sizeof
8202 * here
8203 * Otherwise check whether it is a enumeration or structure
8204 * initialisation (not indented) or a variable declaration
8205 * (indented).
8206 */
8207 terminated = cin_isterminated(l, FALSE, TRUE);
8208
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008209 if (js_cur_has_key)
8210 {
8211 js_cur_has_key = 0; /* only check the first line */
8212 if (curbuf->b_ind_js && terminated == ',')
8213 {
8214 /* For Javascript we might be inside an object:
8215 * key: something, <- align with this
8216 * key: something
8217 * or:
8218 * key: something + <- align with this
8219 * something,
8220 * key: something
8221 */
8222 lookfor = LOOKFOR_JS_KEY;
8223 }
8224 }
8225 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8226 {
8227 amount = get_indent();
8228 break;
8229 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008230 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008231 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008232 if (tryposBrace != NULL && tryposBrace->lnum
8233 >= curwin->w_cursor.lnum)
8234 break;
8235 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008236 /* line below current line is the one that starts a
8237 * (possibly broken) line ending in a comma. */
8238 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008239 else
8240 {
8241 amount = get_indent();
8242 if (curwin->w_cursor.lnum - 1 == ourscope)
8243 /* line above is start of the scope, thus current
8244 * line is the one that stars a (possibly broken)
8245 * line ending in a comma. */
8246 break;
8247 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008248 }
8249
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8251 && terminated == ','))
8252 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008253 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8254 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008255 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 /*
8257 * if we're in the middle of a paren thing,
8258 * go back to the line that starts it so
8259 * we can get the right prevailing indent
8260 * if ( foo &&
8261 * bar )
8262 */
8263 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008264 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008266 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 */
8268 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008269 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008270 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8271 || (trypos->lnum == tryposBrace->lnum
8272 && trypos->col < tryposBrace->col)))
8273 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274
8275 /*
8276 * If we are looking for ',', we also look for matching
8277 * braces.
8278 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008279 if (trypos == NULL && terminated == ','
8280 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008281 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282
8283 if (trypos != NULL)
8284 {
8285 /*
8286 * Check if we are on a case label now. This is
8287 * handled above.
8288 * case xx: if ( asdf &&
8289 * asdf)
8290 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008291 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008293 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {
8295 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008296 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 continue;
8298 }
8299 }
8300
8301 /*
8302 * Skip over continuation lines to find the one to get the
8303 * indent from
8304 * char *usethis = "bla\
8305 * bla",
8306 * here;
8307 */
8308 if (terminated == ',')
8309 {
8310 while (curwin->w_cursor.lnum > 1)
8311 {
8312 l = ml_get(curwin->w_cursor.lnum - 1);
8313 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8314 break;
8315 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008316 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 }
8318 }
8319
8320 /*
8321 * Get indent and pointer to text for current line,
8322 * ignoring any jump label. XXX
8323 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008324 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008325 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008326 else
8327 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 /*
8329 * If this is just above the line we are indenting, and it
8330 * starts with a '{', line it up with this line.
8331 * while (not)
8332 * -> {
8333 * }
8334 */
8335 if (terminated != ',' && lookfor != LOOKFOR_TERM
8336 && theline[0] == '{')
8337 {
8338 amount = cur_amount;
8339 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008340 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 * doesn't start with a '{', which must have a match
8342 * in the same line (scope is the same). Probably:
8343 * { 1, 2 },
8344 * -> { 3, 4 }
8345 */
8346 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008347 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008349 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {
8351 /* have to look back, whether it is a cpp base
8352 * class declaration or initialization */
8353 lookfor = LOOKFOR_CPP_BASECLASS;
8354 continue;
8355 }
8356 break;
8357 }
8358
8359 /*
8360 * Check if we are after an "if", "while", etc.
8361 * Also allow " } else".
8362 */
8363 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8364 {
8365 /*
8366 * Found an unterminated line after an if (), line up
8367 * with the last one.
8368 * if (cond)
8369 * 100 +
8370 * -> here;
8371 */
8372 if (lookfor == LOOKFOR_UNTERM
8373 || lookfor == LOOKFOR_ENUM_OR_INIT)
8374 {
8375 if (cont_amount > 0)
8376 amount = cont_amount;
8377 else
8378 amount += ind_continuation;
8379 break;
8380 }
8381
8382 /*
8383 * If this is just above the line we are indenting, we
8384 * are finished.
8385 * while (not)
8386 * -> here;
8387 * Otherwise this indent can be used when the line
8388 * before this is terminated.
8389 * yyy;
8390 * if (stat)
8391 * while (not)
8392 * xxx;
8393 * -> here;
8394 */
8395 amount = cur_amount;
8396 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008397 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 if (lookfor != LOOKFOR_TERM)
8399 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008400 amount += curbuf->b_ind_level
8401 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 break;
8403 }
8404
8405 /*
8406 * Special trick: when expecting the while () after a
8407 * do, line up with the while()
8408 * do
8409 * x = 1;
8410 * -> here
8411 */
8412 l = skipwhite(ml_get_curline());
8413 if (cin_isdo(l))
8414 {
8415 if (whilelevel == 0)
8416 break;
8417 --whilelevel;
8418 }
8419
8420 /*
8421 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008422 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 * Need to use the scope of this "else". XXX
8424 * If whilelevel != 0 continue looking for a "do {".
8425 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008426 if (cin_iselse(l) && whilelevel == 0)
8427 {
8428 /* If we're looking at "} else", let's make sure we
8429 * find the opening brace of the enclosing scope,
8430 * not the one from "if () {". */
8431 if (*l == '}')
8432 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008433 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008434
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008435 if ((trypos = find_start_brace()) == NULL
8436 || find_match(LOOKFOR_IF, trypos->lnum)
8437 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008438 break;
8439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 }
8441
8442 /*
8443 * If we're below an unterminated line that is not an
8444 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008445 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 * the line before this one.
8447 */
8448 else
8449 {
8450 /*
8451 * Found two unterminated lines on a row, line up with
8452 * the last one.
8453 * c = 99 +
8454 * 100 +
8455 * -> here;
8456 */
8457 if (lookfor == LOOKFOR_UNTERM)
8458 {
8459 /* When line ends in a comma add extra indent */
8460 if (terminated == ',')
8461 amount += ind_continuation;
8462 break;
8463 }
8464
8465 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8466 {
8467 /* Found two lines ending in ',', lineup with the
8468 * lowest one, but check for cpp base class
8469 * declaration/initialization, if it is an
8470 * opening brace or we are looking just for
8471 * enumerations/initializations. */
8472 if (terminated == ',')
8473 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008474 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 break;
8476
8477 lookfor = LOOKFOR_CPP_BASECLASS;
8478 continue;
8479 }
8480
8481 /* Ignore unterminated lines in between, but
8482 * reduce indent. */
8483 if (amount > cur_amount)
8484 amount = cur_amount;
8485 }
8486 else
8487 {
8488 /*
8489 * Found first unterminated line on a row, may
8490 * line up with this line, remember its indent
8491 * 100 +
8492 * -> here;
8493 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008494 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008496
8497 n = (int)STRLEN(l);
8498 if (terminated == ',' && (*skipwhite(l) == ']'
8499 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501
8502 /*
8503 * If previous line ends in ',', check whether we
8504 * are in an initialization or enum
8505 * struct xxx =
8506 * {
8507 * sizeof a,
8508 * 124 };
8509 * or a normal possible continuation line.
8510 * but only, of no other statement has been found
8511 * yet.
8512 */
8513 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8514 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008515 if (curbuf->b_ind_js)
8516 {
8517 /* Search for a line ending in a comma
8518 * and line up with the line below it
8519 * (could be the current line).
8520 * some = [
8521 * 1, <- line up here
8522 * 2,
8523 * some = [
8524 * 3 + <- line up here
8525 * 4 *
8526 * 5,
8527 * 6,
8528 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008529 if (cin_iscomment(skipwhite(l)))
8530 break;
8531 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008532 trypos = find_match_char('[',
8533 curbuf->b_ind_maxparen);
8534 if (trypos != NULL)
8535 {
8536 if (trypos->lnum
8537 == curwin->w_cursor.lnum - 1)
8538 {
8539 /* Current line is first inside
8540 * [], line up with it. */
8541 break;
8542 }
8543 ourscope = trypos->lnum;
8544 }
8545 }
8546 else
8547 {
8548 lookfor = LOOKFOR_ENUM_OR_INIT;
8549 cont_amount = cin_first_id_amount();
8550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
8552 else
8553 {
8554 if (lookfor == LOOKFOR_INITIAL
8555 && *l != NUL
8556 && l[STRLEN(l) - 1] == '\\')
8557 /* XXX */
8558 cont_amount = cin_get_equal_amount(
8559 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008560 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008561 && lookfor != LOOKFOR_JS_KEY
8562 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 lookfor = LOOKFOR_UNTERM;
8564 }
8565 }
8566 }
8567 }
8568
8569 /*
8570 * Check if we are after a while (cond);
8571 * If so: Ignore until the matching "do".
8572 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008573 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {
8575 /*
8576 * Found an unterminated line after a while ();, line up
8577 * with the last one.
8578 * while (cond);
8579 * 100 + <- line up with this one
8580 * -> here;
8581 */
8582 if (lookfor == LOOKFOR_UNTERM
8583 || lookfor == LOOKFOR_ENUM_OR_INIT)
8584 {
8585 if (cont_amount > 0)
8586 amount = cont_amount;
8587 else
8588 amount += ind_continuation;
8589 break;
8590 }
8591
8592 if (whilelevel == 0)
8593 {
8594 lookfor = LOOKFOR_TERM;
8595 amount = get_indent(); /* XXX */
8596 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008597 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 }
8599 ++whilelevel;
8600 }
8601
8602 /*
8603 * We are after a "normal" statement.
8604 * If we had another statement we can stop now and use the
8605 * indent of that other statement.
8606 * Otherwise the indent of the current statement may be used,
8607 * search backwards for the next "normal" statement.
8608 */
8609 else
8610 {
8611 /*
8612 * Skip single break line, if before a switch label. It
8613 * may be lined up with the case label.
8614 */
8615 if (lookfor == LOOKFOR_NOBREAK
8616 && cin_isbreak(skipwhite(ml_get_curline())))
8617 {
8618 lookfor = LOOKFOR_ANY;
8619 continue;
8620 }
8621
8622 /*
8623 * Handle "do {" line.
8624 */
8625 if (whilelevel > 0)
8626 {
8627 l = cin_skipcomment(ml_get_curline());
8628 if (cin_isdo(l))
8629 {
8630 amount = get_indent(); /* XXX */
8631 --whilelevel;
8632 continue;
8633 }
8634 }
8635
8636 /*
8637 * Found a terminated line above an unterminated line. Add
8638 * the amount for a continuation line.
8639 * x = 1;
8640 * y = foo +
8641 * -> here;
8642 * or
8643 * int x = 1;
8644 * int foo,
8645 * -> here;
8646 */
8647 if (lookfor == LOOKFOR_UNTERM
8648 || lookfor == LOOKFOR_ENUM_OR_INIT)
8649 {
8650 if (cont_amount > 0)
8651 amount = cont_amount;
8652 else
8653 amount += ind_continuation;
8654 break;
8655 }
8656
8657 /*
8658 * Found a terminated line above a terminated line or "if"
8659 * etc. line. Use the amount of the line below us.
8660 * x = 1; x = 1;
8661 * if (asdf) y = 2;
8662 * while (asdf) ->here;
8663 * here;
8664 * ->foo;
8665 */
8666 if (lookfor == LOOKFOR_TERM)
8667 {
8668 if (!lookfor_break && whilelevel == 0)
8669 break;
8670 }
8671
8672 /*
8673 * First line above the one we're indenting is terminated.
8674 * To know what needs to be done look further backward for
8675 * a terminated line.
8676 */
8677 else
8678 {
8679 /*
8680 * position the cursor over the rightmost paren, so
8681 * that matching it will take us back to the start of
8682 * the line. Helps for:
8683 * func(asdr,
8684 * asdfasdf);
8685 * here;
8686 */
8687term_again:
8688 l = ml_get_curline();
8689 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008690 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008691 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 {
8693 /*
8694 * Check if we are on a case label now. This is
8695 * handled above.
8696 * case xx: if ( asdf &&
8697 * asdf)
8698 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008699 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008701 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {
8703 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008704 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 continue;
8706 }
8707 }
8708
8709 /* When aligning with the case statement, don't align
8710 * with a statement after it.
8711 * case 1: { <-- don't use this { position
8712 * stat;
8713 * }
8714 * case 2:
8715 * stat;
8716 * }
8717 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008718 iscase = (curbuf->b_ind_keep_case_label
8719 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720
8721 /*
8722 * Get indent and pointer to text for current line,
8723 * ignoring any jump label.
8724 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008725 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726
8727 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008728 amount += curbuf->b_ind_open_extra;
8729 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008730 l = skipwhite(l);
8731 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008732 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8734
8735 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008736 * When a terminated line starts with "else" skip to
8737 * the matching "if":
8738 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008739 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008740 * Need to use the scope of this "else". XXX
8741 * If whilelevel != 0 continue looking for a "do {".
8742 */
8743 if (lookfor == LOOKFOR_TERM
8744 && *l != '}'
8745 && cin_iselse(l)
8746 && whilelevel == 0)
8747 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008748 if ((trypos = find_start_brace()) == NULL
8749 || find_match(LOOKFOR_IF, trypos->lnum)
8750 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008751 break;
8752 continue;
8753 }
8754
8755 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 * If we're at the end of a block, skip to the start of
8757 * that block.
8758 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008759 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008760 if (find_last_paren(l, '{', '}') /* XXX */
8761 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008763 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 /* if not "else {" check for terminated again */
8765 /* but skip block for "} else {" */
8766 l = cin_skipcomment(ml_get_curline());
8767 if (*l == '}' || !cin_iselse(l))
8768 goto term_again;
8769 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008770 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 }
8772 }
8773 }
8774 }
8775 }
8776 }
8777
8778 /* add extra indent for a comment */
8779 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008780 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008781
8782 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008783 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8784 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008785
8786 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008788
8789 /*
8790 * ok -- we're not inside any sort of structure at all!
8791 *
8792 * This means we're at the top level, and everything should
8793 * basically just match where the previous line is, except
8794 * for the lines immediately following a function declaration,
8795 * which are K&R-style parameters and need to be indented.
8796 *
8797 * if our line starts with an open brace, forget about any
8798 * prevailing indent and make sure it looks like the start
8799 * of a function
8800 */
8801
8802 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008804 amount = curbuf->b_ind_first_open;
8805 goto theend;
8806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008808 /*
8809 * If the NEXT line is a function declaration, the current
8810 * line needs to be indented as a function type spec.
8811 * Don't do this if the current line looks like a comment or if the
8812 * current line is terminated, ie. ends in ';', or if the current line
8813 * contains { or }: "void f() {\n if (1)"
8814 */
8815 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8816 && !cin_nocode(theline)
8817 && vim_strchr(theline, '{') == NULL
8818 && vim_strchr(theline, '}') == NULL
8819 && !cin_ends_in(theline, (char_u *)":", NULL)
8820 && !cin_ends_in(theline, (char_u *)",", NULL)
8821 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8822 cur_curpos.lnum + 1)
8823 && !cin_isterminated(theline, FALSE, TRUE))
8824 {
8825 amount = curbuf->b_ind_func_type;
8826 goto theend;
8827 }
8828
8829 /* search backwards until we find something we recognize */
8830 amount = 0;
8831 curwin->w_cursor = cur_curpos;
8832 while (curwin->w_cursor.lnum > 1)
8833 {
8834 curwin->w_cursor.lnum--;
8835 curwin->w_cursor.col = 0;
8836
8837 l = ml_get_curline();
8838
8839 /*
8840 * If we're in a comment or raw string now, skip to the start
8841 * of it.
8842 */ /* XXX */
8843 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008845 curwin->w_cursor.lnum = trypos->lnum + 1;
8846 curwin->w_cursor.col = 0;
8847 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 }
8849
8850 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008851 * Are we at the start of a cpp base class declaration or
8852 * constructor initialization?
8853 */ /* XXX */
8854 n = FALSE;
8855 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008857 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8858 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008860 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008862 /* XXX */
8863 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8864 break;
8865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008867 /*
8868 * Skip preprocessor directives and blank lines.
8869 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008870 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008871 continue;
8872
8873 if (cin_nocode(l))
8874 continue;
8875
8876 /*
8877 * If the previous line ends in ',', use one level of
8878 * indentation:
8879 * int foo,
8880 * bar;
8881 * do this before checking for '}' in case of eg.
8882 * enum foobar
8883 * {
8884 * ...
8885 * } foo,
8886 * bar;
8887 */
8888 n = 0;
8889 if (cin_ends_in(l, (char_u *)",", NULL)
8890 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8891 {
8892 /* take us back to opening paren */
8893 if (find_last_paren(l, '(', ')')
8894 && (trypos = find_match_paren(
8895 curbuf->b_ind_maxparen)) != NULL)
8896 curwin->w_cursor = *trypos;
8897
8898 /* For a line ending in ',' that is a continuation line go
8899 * back to the first line with a backslash:
8900 * char *foo = "bla\
8901 * bla",
8902 * here;
8903 */
8904 while (n == 0 && curwin->w_cursor.lnum > 1)
8905 {
8906 l = ml_get(curwin->w_cursor.lnum - 1);
8907 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8908 break;
8909 --curwin->w_cursor.lnum;
8910 curwin->w_cursor.col = 0;
8911 }
8912
8913 amount = get_indent(); /* XXX */
8914
8915 if (amount == 0)
8916 amount = cin_first_id_amount();
8917 if (amount == 0)
8918 amount = ind_continuation;
8919 break;
8920 }
8921
8922 /*
8923 * If the line looks like a function declaration, and we're
8924 * not in a comment, put it the left margin.
8925 */
8926 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
8927 break;
8928 l = ml_get_curline();
8929
8930 /*
8931 * Finding the closing '}' of a previous function. Put
8932 * current line at the left margin. For when 'cino' has "fs".
8933 */
8934 if (*skipwhite(l) == '}')
8935 break;
8936
8937 /* (matching {)
8938 * If the previous line ends on '};' (maybe followed by
8939 * comments) align at column 0. For example:
8940 * char *string_array[] = { "foo",
8941 * / * x * / "b};ar" }; / * foobar * /
8942 */
8943 if (cin_ends_in(l, (char_u *)"};", NULL))
8944 break;
8945
8946 /*
8947 * If the previous line ends on '[' we are probably in an
8948 * array constant:
8949 * something = [
8950 * 234, <- extra indent
8951 */
8952 if (cin_ends_in(l, (char_u *)"[", NULL))
8953 {
8954 amount = get_indent() + ind_continuation;
8955 break;
8956 }
8957
8958 /*
8959 * Find a line only has a semicolon that belongs to a previous
8960 * line ending in '}', e.g. before an #endif. Don't increase
8961 * indent then.
8962 */
8963 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8964 {
8965 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966
8967 while (curwin->w_cursor.lnum > 1)
8968 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008969 look = ml_get(--curwin->w_cursor.lnum);
8970 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008971 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008973 }
8974 if (curwin->w_cursor.lnum > 0
8975 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008978 curwin->w_cursor = curpos_save;
8979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008981 /*
8982 * If the PREVIOUS line is a function declaration, the current
8983 * line (and the ones that follow) needs to be indented as
8984 * parameters.
8985 */
8986 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
8987 {
8988 amount = curbuf->b_ind_param;
8989 break;
8990 }
8991
8992 /*
8993 * If the previous line ends in ';' and the line before the
8994 * previous line ends in ',' or '\', ident to column zero:
8995 * int foo,
8996 * bar;
8997 * indent_to_0 here;
8998 */
8999 if (cin_ends_in(l, (char_u *)";", NULL))
9000 {
9001 l = ml_get(curwin->w_cursor.lnum - 1);
9002 if (cin_ends_in(l, (char_u *)",", NULL)
9003 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9004 break;
9005 l = ml_get_curline();
9006 }
9007
9008 /*
9009 * Doesn't look like anything interesting -- so just
9010 * use the indent of this line.
9011 *
9012 * Position the cursor over the rightmost paren, so that
9013 * matching it will take us back to the start of the line.
9014 */
9015 find_last_paren(l, '(', ')');
9016
9017 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9018 curwin->w_cursor = *trypos;
9019 amount = get_indent(); /* XXX */
9020 break;
9021 }
9022
9023 /* add extra indent for a comment */
9024 if (cin_iscomment(theline))
9025 amount += curbuf->b_ind_comment;
9026
9027 /* add extra indent if the previous line ended in a backslash:
9028 * "asdfasdf\
9029 * here";
9030 * char *foo = "asdf\
9031 * here";
9032 */
9033 if (cur_curpos.lnum > 1)
9034 {
9035 l = ml_get(cur_curpos.lnum - 1);
9036 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9037 {
9038 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9039 if (cur_amount > 0)
9040 amount = cur_amount;
9041 else if (cur_amount == 0)
9042 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 }
9044 }
9045
9046theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009047 if (amount < 0)
9048 amount = 0;
9049
9050laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 /* put the cursor back where it belongs */
9052 curwin->w_cursor = cur_curpos;
9053
9054 vim_free(linecopy);
9055
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 return amount;
9057}
9058
9059 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009060find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 char_u *look;
9063 pos_T *theirscope;
9064 char_u *mightbeif;
9065 int elselevel;
9066 int whilelevel;
9067
9068 if (lookfor == LOOKFOR_IF)
9069 {
9070 elselevel = 1;
9071 whilelevel = 0;
9072 }
9073 else
9074 {
9075 elselevel = 0;
9076 whilelevel = 1;
9077 }
9078
9079 curwin->w_cursor.col = 0;
9080
9081 while (curwin->w_cursor.lnum > ourscope + 1)
9082 {
9083 curwin->w_cursor.lnum--;
9084 curwin->w_cursor.col = 0;
9085
9086 look = cin_skipcomment(ml_get_curline());
9087 if (cin_iselse(look)
9088 || cin_isif(look)
9089 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009090 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 {
9092 /*
9093 * if we've gone outside the braces entirely,
9094 * we must be out of scope...
9095 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009096 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 if (theirscope == NULL)
9098 break;
9099
9100 /*
9101 * and if the brace enclosing this is further
9102 * back than the one enclosing the else, we're
9103 * out of luck too.
9104 */
9105 if (theirscope->lnum < ourscope)
9106 break;
9107
9108 /*
9109 * and if they're enclosed in a *deeper* brace,
9110 * then we can ignore it because it's in a
9111 * different scope...
9112 */
9113 if (theirscope->lnum > ourscope)
9114 continue;
9115
9116 /*
9117 * if it was an "else" (that's not an "else if")
9118 * then we need to go back to another if, so
9119 * increment elselevel
9120 */
9121 look = cin_skipcomment(ml_get_curline());
9122 if (cin_iselse(look))
9123 {
9124 mightbeif = cin_skipcomment(look + 4);
9125 if (!cin_isif(mightbeif))
9126 ++elselevel;
9127 continue;
9128 }
9129
9130 /*
9131 * if it was a "while" then we need to go back to
9132 * another "do", so increment whilelevel. XXX
9133 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009134 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 {
9136 ++whilelevel;
9137 continue;
9138 }
9139
9140 /* If it's an "if" decrement elselevel */
9141 look = cin_skipcomment(ml_get_curline());
9142 if (cin_isif(look))
9143 {
9144 elselevel--;
9145 /*
9146 * When looking for an "if" ignore "while"s that
9147 * get in the way.
9148 */
9149 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9150 whilelevel = 0;
9151 }
9152
9153 /* If it's a "do" decrement whilelevel */
9154 if (cin_isdo(look))
9155 whilelevel--;
9156
9157 /*
9158 * if we've used up all the elses, then
9159 * this must be the if that we want!
9160 * match the indent level of that if.
9161 */
9162 if (elselevel <= 0 && whilelevel <= 0)
9163 {
9164 return OK;
9165 }
9166 }
9167 }
9168 return FAIL;
9169}
9170
9171# if defined(FEAT_EVAL) || defined(PROTO)
9172/*
9173 * Get indent level from 'indentexpr'.
9174 */
9175 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009176get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009179 pos_T save_pos;
9180 colnr_T save_curswant;
9181 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009183 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9184 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009186 /* Save and restore cursor position and curswant, in case it was changed
9187 * via :normal commands */
9188 save_pos = curwin->w_cursor;
9189 save_curswant = curwin->w_curswant;
9190 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009192 if (use_sandbox)
9193 ++sandbox;
9194 ++textlock;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009195 indent = (int)eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009196 if (use_sandbox)
9197 --sandbox;
9198 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
9200 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9201 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9202 * command. */
9203 save_State = State;
9204 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009205 curwin->w_cursor = save_pos;
9206 curwin->w_curswant = save_curswant;
9207 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 check_cursor();
9209 State = save_State;
9210
9211 /* If there is an error, just keep the current indent. */
9212 if (indent < 0)
9213 indent = get_indent();
9214
9215 return indent;
9216}
9217# endif
9218
9219#endif /* FEAT_CINDENT */
9220
9221#if defined(FEAT_LISP) || defined(PROTO)
9222
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009223static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
9225 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009226lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227{
9228 char_u buf[LSIZE];
9229 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009230 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231
9232 while (*word != NUL)
9233 {
9234 (void)copy_option_part(&word, buf, LSIZE, ",");
9235 len = (int)STRLEN(buf);
9236 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9237 return TRUE;
9238 }
9239 return FALSE;
9240}
9241
9242/*
9243 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9244 * The incompatible newer method is quite a bit better at indenting
9245 * code in lisp-like languages than the traditional one; it's still
9246 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9247 *
9248 * TODO:
9249 * Findmatch() should be adapted for lisp, also to make showmatch
9250 * work correctly: now (v5.3) it seems all C/C++ oriented:
9251 * - it does not recognize the #\( and #\) notations as character literals
9252 * - it doesn't know about comments starting with a semicolon
9253 * - it incorrectly interprets '(' as a character literal
9254 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009255 * Update from Sergey Khorev:
9256 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 */
9258 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009259get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009261 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 int amount;
9263 char_u *that;
9264 colnr_T col;
9265 colnr_T firsttry;
9266 int parencount, quotecount;
9267 int vi_lisp;
9268
9269 /* Set vi_lisp to use the vi-compatible method */
9270 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9271
9272 realpos = curwin->w_cursor;
9273 curwin->w_cursor.col = 0;
9274
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009275 if ((pos = findmatch(NULL, '(')) == NULL)
9276 pos = findmatch(NULL, '[');
9277 else
9278 {
9279 paren = *pos;
9280 pos = findmatch(NULL, '[');
9281 if (pos == NULL || ltp(pos, &paren))
9282 pos = &paren;
9283 }
9284 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 {
9286 /* Extra trick: Take the indent of the first previous non-white
9287 * line that is at the same () level. */
9288 amount = -1;
9289 parencount = 0;
9290
9291 while (--curwin->w_cursor.lnum >= pos->lnum)
9292 {
9293 if (linewhite(curwin->w_cursor.lnum))
9294 continue;
9295 for (that = ml_get_curline(); *that != NUL; ++that)
9296 {
9297 if (*that == ';')
9298 {
9299 while (*(that + 1) != NUL)
9300 ++that;
9301 continue;
9302 }
9303 if (*that == '\\')
9304 {
9305 if (*(that + 1) != NUL)
9306 ++that;
9307 continue;
9308 }
9309 if (*that == '"' && *(that + 1) != NUL)
9310 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009311 while (*++that && *that != '"')
9312 {
9313 /* skipping escaped characters in the string */
9314 if (*that == '\\')
9315 {
9316 if (*++that == NUL)
9317 break;
9318 if (that[1] == NUL)
9319 {
9320 ++that;
9321 break;
9322 }
9323 }
9324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009326 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009328 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 --parencount;
9330 }
9331 if (parencount == 0)
9332 {
9333 amount = get_indent();
9334 break;
9335 }
9336 }
9337
9338 if (amount == -1)
9339 {
9340 curwin->w_cursor.lnum = pos->lnum;
9341 curwin->w_cursor.col = pos->col;
9342 col = pos->col;
9343
9344 that = ml_get_curline();
9345
9346 if (vi_lisp && get_indent() == 0)
9347 amount = 2;
9348 else
9349 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009350 char_u *line = that;
9351
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 amount = 0;
9353 while (*that && col)
9354 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009355 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 col--;
9357 }
9358
9359 /*
9360 * Some keywords require "body" indenting rules (the
9361 * non-standard-lisp ones are Scheme special forms):
9362 *
9363 * (let ((a 1)) instead (let ((a 1))
9364 * (...)) of (...))
9365 */
9366
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009367 if (!vi_lisp && (*that == '(' || *that == '[')
9368 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 amount += 2;
9370 else
9371 {
9372 that++;
9373 amount++;
9374 firsttry = amount;
9375
9376 while (vim_iswhite(*that))
9377 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009378 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 ++that;
9380 }
9381
9382 if (*that && *that != ';') /* not a comment line */
9383 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009384 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009386 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 firsttry++;
9388
9389 parencount = 0;
9390 quotecount = 0;
9391
9392 if (vi_lisp
9393 || (*that != '"'
9394 && *that != '\''
9395 && *that != '#'
9396 && (*that < '0' || *that > '9')))
9397 {
9398 while (*that
9399 && (!vim_iswhite(*that)
9400 || quotecount
9401 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009402 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 && !quotecount
9404 && !parencount
9405 && vi_lisp)))
9406 {
9407 if (*that == '"')
9408 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009409 if ((*that == '(' || *that == '[')
9410 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009412 if ((*that == ')' || *that == ']')
9413 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 --parencount;
9415 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009416 amount += lbr_chartabsize_adv(
9417 line, &that, (colnr_T)amount);
9418 amount += lbr_chartabsize_adv(
9419 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 }
9421 }
9422 while (vim_iswhite(*that))
9423 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009424 amount += lbr_chartabsize(
9425 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 that++;
9427 }
9428 if (!*that || *that == ';')
9429 amount = firsttry;
9430 }
9431 }
9432 }
9433 }
9434 }
9435 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009436 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437
9438 curwin->w_cursor = realpos;
9439
9440 return amount;
9441}
9442#endif /* FEAT_LISP */
9443
9444 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009445prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009447#if defined(SIGHUP) && defined(SIG_IGN)
9448 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9449 * makes Vim exit and then handling SIGHUP causes various reentrance
9450 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009451 signal(SIGHUP, SIG_IGN);
9452#endif
9453
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454#ifdef FEAT_GUI
9455 if (gui.in_use)
9456 {
9457 gui.dying = TRUE;
9458 out_trash(); /* trash any pending output */
9459 }
9460 else
9461#endif
9462 {
9463 windgoto((int)Rows - 1, 0);
9464
9465 /*
9466 * Switch terminal mode back now, so messages end up on the "normal"
9467 * screen (if there are two screens).
9468 */
9469 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009470 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 out_flush();
9472 }
9473}
9474
9475/*
9476 * Preserve files and exit.
9477 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009478 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9479 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 */
9481 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009482preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484 buf_T *buf;
9485
9486 prepare_to_exit();
9487
Bram Moolenaar4770d092006-01-12 23:22:24 +00009488 /* Setting this will prevent free() calls. That avoids calling free()
9489 * recursively when free() was invoked with a bad pointer. */
9490 really_exiting = TRUE;
9491
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 out_str(IObuff);
9493 screen_start(); /* don't know where cursor is now */
9494 out_flush();
9495
9496 ml_close_notmod(); /* close all not-modified buffers */
9497
Bram Moolenaar29323592016-07-24 22:04:11 +02009498 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 {
9500 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9501 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009502 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 screen_start(); /* don't know where cursor is now */
9504 out_flush();
9505 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9506 break;
9507 }
9508 }
9509
9510 ml_close_all(FALSE); /* close all memfiles, without deleting */
9511
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009512 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513
9514 getout(1);
9515}
9516
9517/*
9518 * return TRUE if "fname" exists.
9519 */
9520 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009521vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009523 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524
9525 if (mch_stat((char *)fname, &st))
9526 return FALSE;
9527 return TRUE;
9528}
9529
9530/*
9531 * Check for CTRL-C pressed, but only once in a while.
9532 * Should be used instead of ui_breakcheck() for functions that check for
9533 * each line in the file. Calling ui_breakcheck() each time takes too much
9534 * time, because it can be a system call.
9535 */
9536
9537#ifndef BREAKCHECK_SKIP
9538# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9539# define BREAKCHECK_SKIP 200
9540# else
9541# define BREAKCHECK_SKIP 32
9542# endif
9543#endif
9544
9545static int breakcheck_count = 0;
9546
9547 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009548line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550 if (++breakcheck_count >= BREAKCHECK_SKIP)
9551 {
9552 breakcheck_count = 0;
9553 ui_breakcheck();
9554 }
9555}
9556
9557/*
9558 * Like line_breakcheck() but check 10 times less often.
9559 */
9560 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009561fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9564 {
9565 breakcheck_count = 0;
9566 ui_breakcheck();
9567 }
9568}
9569
9570/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009571 * Invoke expand_wildcards() for one pattern.
9572 * Expand items like "%:h" before the expansion.
9573 * Returns OK or FAIL.
9574 */
9575 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009576expand_wildcards_eval(
9577 char_u **pat, /* pointer to input pattern */
9578 int *num_file, /* resulting number of files */
9579 char_u ***file, /* array of resulting files */
9580 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009581{
9582 int ret = FAIL;
9583 char_u *eval_pat = NULL;
9584 char_u *exp_pat = *pat;
9585 char_u *ignored_msg;
9586 int usedlen;
9587
9588 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9589 {
9590 ++emsg_off;
9591 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9592 NULL, &ignored_msg, NULL);
9593 --emsg_off;
9594 if (eval_pat != NULL)
9595 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9596 }
9597
9598 if (exp_pat != NULL)
9599 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9600
9601 if (eval_pat != NULL)
9602 {
9603 vim_free(exp_pat);
9604 vim_free(eval_pat);
9605 }
9606
9607 return ret;
9608}
9609
9610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9612 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009613 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 */
9615 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009616expand_wildcards(
9617 int num_pat, /* number of input patterns */
9618 char_u **pat, /* array of input patterns */
9619 int *num_files, /* resulting number of files */
9620 char_u ***files, /* array of resulting files */
9621 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 int retval;
9624 int i, j;
9625 char_u *p;
9626 int non_suf_match; /* number without matching suffix */
9627
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009628 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
9630 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009631 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 return retval;
9633
9634#ifdef FEAT_WILDIGN
9635 /*
9636 * Remove names that match 'wildignore'.
9637 */
9638 if (*p_wig)
9639 {
9640 char_u *ffname;
9641
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009642 /* check all files in (*files)[] */
9643 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009645 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 if (ffname == NULL) /* out of memory */
9647 break;
9648# ifdef VMS
9649 vms_remove_version(ffname);
9650# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009651 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009653 /* remove this matching files from the list */
9654 vim_free((*files)[i]);
9655 for (j = i; j + 1 < *num_files; ++j)
9656 (*files)[j] = (*files)[j + 1];
9657 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 --i;
9659 }
9660 vim_free(ffname);
9661 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009662
9663 /* If the number of matches is now zero, we fail. */
9664 if (*num_files == 0)
9665 {
9666 vim_free(*files);
9667 *files = NULL;
9668 return FAIL;
9669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 }
9671#endif
9672
9673 /*
9674 * Move the names where 'suffixes' match to the end.
9675 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009676 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
9678 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009679 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009681 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 {
9683 /*
9684 * Move the name without matching suffix to the front
9685 * of the list.
9686 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009687 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009689 (*files)[j] = (*files)[j - 1];
9690 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 }
9692 }
9693 }
9694
9695 return retval;
9696}
9697
9698/*
9699 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9700 */
9701 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009702match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703{
9704 int fnamelen, setsuflen;
9705 char_u *setsuf;
9706#define MAXSUFLEN 30 /* maximum length of a file suffix */
9707 char_u suf_buf[MAXSUFLEN];
9708
9709 fnamelen = (int)STRLEN(fname);
9710 setsuflen = 0;
9711 for (setsuf = p_su; *setsuf; )
9712 {
9713 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009714 if (setsuflen == 0)
9715 {
9716 char_u *tail = gettail(fname);
9717
9718 /* empty entry: match name without a '.' */
9719 if (vim_strchr(tail, '.') == NULL)
9720 {
9721 setsuflen = 1;
9722 break;
9723 }
9724 }
9725 else
9726 {
9727 if (fnamelen >= setsuflen
9728 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9729 (size_t)setsuflen) == 0)
9730 break;
9731 setsuflen = 0;
9732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 }
9734 return (setsuflen != 0);
9735}
9736
9737#if !defined(NO_EXPANDPATH) || defined(PROTO)
9738
9739# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009740static int vim_backtick(char_u *p);
9741static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742# endif
9743
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009744# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745/*
9746 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9747 * it's shared between these systems.
9748 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009749# if defined(PROTO)
9750# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751# else
9752# ifdef __BORLANDC__
9753# define _cdecl _RTLENTRYF
9754# endif
9755# endif
9756
9757/*
9758 * comparison function for qsort in dos_expandpath()
9759 */
9760 static int _cdecl
9761pstrcmp(const void *a, const void *b)
9762{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009763 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764}
9765
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009767 * Recursively expand one path component into all matching files and/or
9768 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 * Return the number of matches found.
9770 * "path" has backslashes before chars that are not to be expanded, starting
9771 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009772 * Return the number of matches found.
9773 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 */
9775 static int
9776dos_expandpath(
9777 garray_T *gap,
9778 char_u *path,
9779 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009780 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009781 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009783 char_u *buf;
9784 char_u *path_end;
9785 char_u *p, *s, *e;
9786 int start_len = gap->ga_len;
9787 char_u *pat;
9788 regmatch_T regmatch;
9789 int starts_with_dot;
9790 int matches;
9791 int len;
9792 int starstar = FALSE;
9793 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 WIN32_FIND_DATA fb;
9795 HANDLE hFind = (HANDLE)0;
9796# ifdef FEAT_MBYTE
9797 WIN32_FIND_DATAW wfb;
9798 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009801 int ok;
9802
9803 /* Expanding "**" may take a long time, check for CTRL-C. */
9804 if (stardepth > 0)
9805 {
9806 ui_breakcheck();
9807 if (got_int)
9808 return 0;
9809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009811 /* Make room for file name. When doing encoding conversion the actual
9812 * length may be quite a bit longer, thus use the maximum possible length. */
9813 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 if (buf == NULL)
9815 return 0;
9816
9817 /*
9818 * Find the first part in the path name that contains a wildcard or a ~1.
9819 * Copy it into buf, including the preceding characters.
9820 */
9821 p = buf;
9822 s = buf;
9823 e = NULL;
9824 path_end = path;
9825 while (*path_end != NUL)
9826 {
9827 /* May ignore a wildcard that has a backslash before it; it will
9828 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9829 if (path_end >= path + wildoff && rem_backslash(path_end))
9830 *p++ = *path_end++;
9831 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9832 {
9833 if (e != NULL)
9834 break;
9835 s = p + 1;
9836 }
9837 else if (path_end >= path + wildoff
9838 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9839 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009840# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 if (has_mbyte)
9842 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009843 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 STRNCPY(p, path_end, len);
9845 p += len;
9846 path_end += len;
9847 }
9848 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 *p++ = *path_end++;
9851 }
9852 e = p;
9853 *e = NUL;
9854
9855 /* now we have one wildcard component between s and e */
9856 /* Remove backslashes between "wildoff" and the start of the wildcard
9857 * component. */
9858 for (p = buf + wildoff; p < s; ++p)
9859 if (rem_backslash(p))
9860 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009861 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 --e;
9863 --s;
9864 }
9865
Bram Moolenaar231334e2005-07-25 20:46:57 +00009866 /* Check for "**" between "s" and "e". */
9867 for (p = s; p < e; ++p)
9868 if (p[0] == '*' && p[1] == '*')
9869 starstar = TRUE;
9870
Bram Moolenaard82103e2016-01-17 17:04:05 +01009871 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9873 if (pat == NULL)
9874 {
9875 vim_free(buf);
9876 return 0;
9877 }
9878
9879 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009880 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009881 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 regmatch.rm_ic = TRUE; /* Always ignore case */
9883 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009884 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009885 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 vim_free(pat);
9887
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009888 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 {
9890 vim_free(buf);
9891 return 0;
9892 }
9893
9894 /* remember the pattern or file name being looked for */
9895 matchname = vim_strsave(s);
9896
Bram Moolenaar231334e2005-07-25 20:46:57 +00009897 /* If "**" is by itself, this is the first time we encounter it and more
9898 * is following then find matches without any directory. */
9899 if (!didstar && stardepth < 100 && starstar && e - s == 2
9900 && *path_end == '/')
9901 {
9902 STRCPY(s, path_end + 1);
9903 ++stardepth;
9904 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9905 --stardepth;
9906 }
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 /* Scan all files in the directory with "dir/ *.*" */
9909 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910# ifdef FEAT_MBYTE
9911 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9912 {
9913 /* The active codepage differs from 'encoding'. Attempt using the
9914 * wide function. If it fails because it is not implemented fall back
9915 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009916 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 if (wn != NULL)
9918 {
9919 hFind = FindFirstFileW(wn, &wfb);
9920 if (hFind == INVALID_HANDLE_VALUE
9921 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9922 {
9923 vim_free(wn);
9924 wn = NULL;
9925 }
9926 }
9927 }
9928
9929 if (wn == NULL)
9930# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009931 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933
9934 while (ok)
9935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936# ifdef FEAT_MBYTE
9937 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009938 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 else
9940# endif
9941 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 /* 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 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009955
9956 if (starstar && stardepth < 100)
9957 {
9958 /* For "**" in the pattern first go deeper in the tree to
9959 * find matches. */
9960 STRCPY(buf + len, "/**");
9961 STRCPY(buf + len + 3, path_end);
9962 ++stardepth;
9963 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9964 --stardepth;
9965 }
9966
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 STRCPY(buf + len, path_end);
9968 if (mch_has_exp_wildcard(path_end))
9969 {
9970 /* need to expand another component of the path */
9971 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009972 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 }
9974 else
9975 {
9976 /* no more wildcards, check if there is a match */
9977 /* remove backslashes for the remaining components only */
9978 if (*path_end != 0)
9979 backslash_halve(buf + len + 1);
9980 if (mch_getperm(buf) >= 0) /* add existing file */
9981 addfile(gap, buf, flags);
9982 }
9983 }
9984
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985# ifdef FEAT_MBYTE
9986 if (wn != NULL)
9987 {
9988 vim_free(p);
9989 ok = FindNextFileW(hFind, &wfb);
9990 }
9991 else
9992# endif
9993 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994
9995 /* If no more matches and no match was used, try expanding the name
9996 * itself. Finds the long name of a short filename. */
9997 if (!ok && matchname != NULL && gap->ga_len == start_len)
9998 {
9999 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 FindClose(hFind);
10001# ifdef FEAT_MBYTE
10002 if (wn != NULL)
10003 {
10004 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010005 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if (wn != NULL)
10007 hFind = FindFirstFileW(wn, &wfb);
10008 }
10009 if (wn == NULL)
10010# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010011 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 vim_free(matchname);
10014 matchname = NULL;
10015 }
10016 }
10017
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 FindClose(hFind);
10019# ifdef FEAT_MBYTE
10020 vim_free(wn);
10021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010023 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 vim_free(matchname);
10025
10026 matches = gap->ga_len - start_len;
10027 if (matches > 0)
10028 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10029 sizeof(char_u *), pstrcmp);
10030 return matches;
10031}
10032
10033 int
10034mch_expandpath(
10035 garray_T *gap,
10036 char_u *path,
10037 int flags) /* EW_* flags */
10038{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010039 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010041# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaar231334e2005-07-25 20:46:57 +000010043#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10044 || defined(PROTO)
10045/*
10046 * Unix style wildcard expansion code.
10047 * It's here because it's used both for Unix and Mac.
10048 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010049static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010050
10051 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010052pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010053{
10054 return (pathcmp(*(char **)a, *(char **)b, -1));
10055}
10056
10057/*
10058 * Recursively expand one path component into all matching files and/or
10059 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10060 * "path" has backslashes before chars that are not to be expanded, starting
10061 * at "path + wildoff".
10062 * Return the number of matches found.
10063 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10064 */
10065 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010066unix_expandpath(
10067 garray_T *gap,
10068 char_u *path,
10069 int wildoff,
10070 int flags, /* EW_* flags */
10071 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010072{
10073 char_u *buf;
10074 char_u *path_end;
10075 char_u *p, *s, *e;
10076 int start_len = gap->ga_len;
10077 char_u *pat;
10078 regmatch_T regmatch;
10079 int starts_with_dot;
10080 int matches;
10081 int len;
10082 int starstar = FALSE;
10083 static int stardepth = 0; /* depth for "**" expansion */
10084
10085 DIR *dirp;
10086 struct dirent *dp;
10087
10088 /* Expanding "**" may take a long time, check for CTRL-C. */
10089 if (stardepth > 0)
10090 {
10091 ui_breakcheck();
10092 if (got_int)
10093 return 0;
10094 }
10095
10096 /* make room for file name */
10097 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10098 if (buf == NULL)
10099 return 0;
10100
10101 /*
10102 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010103 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010104 * Copy it into "buf", including the preceding characters.
10105 */
10106 p = buf;
10107 s = buf;
10108 e = NULL;
10109 path_end = path;
10110 while (*path_end != NUL)
10111 {
10112 /* May ignore a wildcard that has a backslash before it; it will
10113 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10114 if (path_end >= path + wildoff && rem_backslash(path_end))
10115 *p++ = *path_end++;
10116 else if (*path_end == '/')
10117 {
10118 if (e != NULL)
10119 break;
10120 s = p + 1;
10121 }
10122 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010123 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010124 || (!p_fic && (flags & EW_ICASE)
10125 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010126 e = p;
10127#ifdef FEAT_MBYTE
10128 if (has_mbyte)
10129 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010130 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010131 STRNCPY(p, path_end, len);
10132 p += len;
10133 path_end += len;
10134 }
10135 else
10136#endif
10137 *p++ = *path_end++;
10138 }
10139 e = p;
10140 *e = NUL;
10141
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010142 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010143 /* Remove backslashes between "wildoff" and the start of the wildcard
10144 * component. */
10145 for (p = buf + wildoff; p < s; ++p)
10146 if (rem_backslash(p))
10147 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010148 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010149 --e;
10150 --s;
10151 }
10152
10153 /* Check for "**" between "s" and "e". */
10154 for (p = s; p < e; ++p)
10155 if (p[0] == '*' && p[1] == '*')
10156 starstar = TRUE;
10157
10158 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010159 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010160 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10161 if (pat == NULL)
10162 {
10163 vim_free(buf);
10164 return 0;
10165 }
10166
10167 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010168 if (flags & EW_ICASE)
10169 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10170 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010171 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010172 if (flags & (EW_NOERROR | EW_NOTWILD))
10173 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010174 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010175 if (flags & (EW_NOERROR | EW_NOTWILD))
10176 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010177 vim_free(pat);
10178
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010179 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010180 {
10181 vim_free(buf);
10182 return 0;
10183 }
10184
10185 /* If "**" is by itself, this is the first time we encounter it and more
10186 * is following then find matches without any directory. */
10187 if (!didstar && stardepth < 100 && starstar && e - s == 2
10188 && *path_end == '/')
10189 {
10190 STRCPY(s, path_end + 1);
10191 ++stardepth;
10192 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10193 --stardepth;
10194 }
10195
10196 /* open the directory for scanning */
10197 *s = NUL;
10198 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10199
10200 /* Find all matching entries */
10201 if (dirp != NULL)
10202 {
10203 for (;;)
10204 {
10205 dp = readdir(dirp);
10206 if (dp == NULL)
10207 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010208 if ((dp->d_name[0] != '.' || starts_with_dot
10209 || ((flags & EW_DODOT)
10210 && dp->d_name[1] != NUL
10211 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010212 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10213 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010214 || ((flags & EW_NOTWILD)
10215 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010216 {
10217 STRCPY(s, dp->d_name);
10218 len = STRLEN(buf);
10219
10220 if (starstar && stardepth < 100)
10221 {
10222 /* For "**" in the pattern first go deeper in the tree to
10223 * find matches. */
10224 STRCPY(buf + len, "/**");
10225 STRCPY(buf + len + 3, path_end);
10226 ++stardepth;
10227 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10228 --stardepth;
10229 }
10230
10231 STRCPY(buf + len, path_end);
10232 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10233 {
10234 /* need to expand another component of the path */
10235 /* remove backslashes for the remaining components only */
10236 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10237 }
10238 else
10239 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010240 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010241
Bram Moolenaar231334e2005-07-25 20:46:57 +000010242 /* no more wildcards, check if there is a match */
10243 /* remove backslashes for the remaining components only */
10244 if (*path_end != NUL)
10245 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010246 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010247 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010248 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010249 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010250#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010251 size_t precomp_len = STRLEN(buf)+1;
10252 char_u *precomp_buf =
10253 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010254
Bram Moolenaar231334e2005-07-25 20:46:57 +000010255 if (precomp_buf)
10256 {
10257 mch_memmove(buf, precomp_buf, precomp_len);
10258 vim_free(precomp_buf);
10259 }
10260#endif
10261 addfile(gap, buf, flags);
10262 }
10263 }
10264 }
10265 }
10266
10267 closedir(dirp);
10268 }
10269
10270 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010271 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010272
10273 matches = gap->ga_len - start_len;
10274 if (matches > 0)
10275 qsort(((char_u **)gap->ga_data) + start_len, matches,
10276 sizeof(char_u *), pstrcmp);
10277 return matches;
10278}
10279#endif
10280
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010281#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010282static int find_previous_pathsep(char_u *path, char_u **psep);
10283static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10284static void expand_path_option(char_u *curdir, garray_T *gap);
10285static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10286static void uniquefy_paths(garray_T *gap, char_u *pattern);
10287static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010288
10289/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010290 * Moves "*psep" back to the previous path separator in "path".
10291 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010292 */
10293 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010294find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010295{
10296 /* skip the current separator */
10297 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010298 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010299
10300 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010301 while (*psep > path)
10302 {
10303 if (vim_ispathsep(**psep))
10304 return OK;
10305 mb_ptr_back(path, *psep);
10306 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010307
10308 return FAIL;
10309}
10310
10311/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010312 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10313 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010314 */
10315 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010316is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010317{
10318 int j;
10319 int candidate_len;
10320 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010321 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010322 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010323
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010324 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010325 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010326 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010327 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010328
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010329 candidate_len = (int)STRLEN(maybe_unique);
10330 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010331 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010332 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010333
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010334 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010335 if (fnamecmp(maybe_unique, rival) == 0
10336 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010337 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010338 }
10339
Bram Moolenaar162bd912010-07-28 22:29:10 +020010340 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010341}
10342
10343/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010344 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010345 * paths are expanded to their equivalent fullpath. This includes the "."
10346 * (relative to current buffer directory) and empty path (relative to current
10347 * directory) notations.
10348 *
10349 * TODO: handle upward search (;) and path limiter (**N) notations by
10350 * expanding each into their equivalent path(s).
10351 */
10352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010353expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010354{
10355 char_u *path_option = *curbuf->b_p_path == NUL
10356 ? p_path : curbuf->b_p_path;
10357 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010358 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010359 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010360
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010361 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010362 return;
10363
10364 while (*path_option != NUL)
10365 {
10366 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10367
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010368 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010369 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010370 /* Relative to current buffer:
10371 * "/path/file" + "." -> "/path/"
10372 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010373 if (curbuf->b_ffname == NULL)
10374 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010375 p = gettail(curbuf->b_ffname);
10376 len = (int)(p - curbuf->b_ffname);
10377 if (len + (int)STRLEN(buf) >= MAXPATHL)
10378 continue;
10379 if (buf[1] == NUL)
10380 buf[len] = NUL;
10381 else
10382 STRMOVE(buf + len, buf + 2);
10383 mch_memmove(buf, curbuf->b_ffname, len);
10384 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010385 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010386 else if (buf[0] == NUL)
10387 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010388 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010389 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010390 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010391 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010392 else if (!mch_isFullName(buf))
10393 {
10394 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010395 len = (int)STRLEN(curdir);
10396 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010397 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010398 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010399 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010400 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010401 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010402 }
10403
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010404 if (ga_grow(gap, 1) == FAIL)
10405 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010406
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010407# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010408 /* Avoid the path ending in a backslash, it fails when a comma is
10409 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010410 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010411 if (buf[len - 1] == '\\')
10412 buf[len - 1] = '/';
10413# endif
10414
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010415 p = vim_strsave(buf);
10416 if (p == NULL)
10417 break;
10418 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010419 }
10420
10421 vim_free(buf);
10422}
10423
10424/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010425 * Returns a pointer to the file or directory name in "fname" that matches the
10426 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010427 *
10428 * path: /foo/bar/baz
10429 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010430 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010431 */
10432 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010433get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010434{
10435 int i;
10436 int maxlen = 0;
10437 char_u **path_part = (char_u **)gap->ga_data;
10438 char_u *cutoff = NULL;
10439
10440 for (i = 0; i < gap->ga_len; i++)
10441 {
10442 int j = 0;
10443
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010444 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010445# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010446 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10447#endif
10448 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010449 j++;
10450 if (j > maxlen)
10451 {
10452 maxlen = j;
10453 cutoff = &fname[j];
10454 }
10455 }
10456
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010457 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010458 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010459 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010460 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010461
10462 return cutoff;
10463}
10464
10465/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010466 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10467 * that they are unique with respect to each other while conserving the part
10468 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010469 */
10470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010471uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010472{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010473 int i;
10474 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010475 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010476 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010477 char_u *pat;
10478 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010479 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010480 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010481 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010482 char_u **in_curdir = NULL;
10483 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010484
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010485 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010486 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010487
10488 /*
10489 * We need to prepend a '*' at the beginning of file_pattern so that the
10490 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010491 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010492 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010493 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010494 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010495 if (file_pattern == NULL)
10496 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010497 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010498 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010499 STRCAT(file_pattern, pattern);
10500 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10501 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010502 if (pat == NULL)
10503 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010504
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010505 regmatch.rm_ic = TRUE; /* always ignore case */
10506 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10507 vim_free(pat);
10508 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010509 return;
10510
Bram Moolenaar162bd912010-07-28 22:29:10 +020010511 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010512 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010513 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010514 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010515
10516 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010517 if (in_curdir == NULL)
10518 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010519
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010520 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010521 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010522 char_u *path = fnames[i];
10523 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010524 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010525 char_u *pathsep_p;
10526 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010527
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010528 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010529 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010530 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010531 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010532 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010533
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010534 /* Shorten the filename while maintaining its uniqueness */
10535 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010536
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010537 /* Don't assume all files can be reached without path when search
10538 * pattern starts with star star slash, so only remove path_cutoff
10539 * when possible. */
10540 if (pattern[0] == '*' && pattern[1] == '*'
10541 && vim_ispathsep_nocolon(pattern[2])
10542 && path_cutoff != NULL
10543 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10544 && is_unique(path_cutoff, gap, i))
10545 {
10546 sort_again = TRUE;
10547 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10548 }
10549 else
10550 {
10551 /* Here all files can be reached without path, so get shortest
10552 * unique path. We start at the end of the path. */
10553 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010554
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010555 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 }
10564 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010565
10566 if (mch_isFullName(path))
10567 {
10568 /*
10569 * Last resort: shorten relative to curdir if possible.
10570 * 'possible' means:
10571 * 1. It is under the current directory.
10572 * 2. The result is actually shorter than the original.
10573 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010574 * Before curdir After
10575 * /foo/bar/file.txt /foo/bar ./file.txt
10576 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10577 * /file.txt / /file.txt
10578 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010579 */
10580 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010581 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010582#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010583 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010584 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010585 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010586 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010587 && !vim_ispathsep(*short_name)
10588#endif
10589 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010590 {
10591 STRCPY(path, ".");
10592 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010593 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010594 }
10595 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010596 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010597 }
10598
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010599 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010600 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010601 {
10602 char_u *rel_path;
10603 char_u *path = in_curdir[i];
10604
10605 if (path == NULL)
10606 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010607
10608 /* If the {filename} is not unique, change it to ./{filename}.
10609 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010610 short_name = shorten_fname(path, curdir);
10611 if (short_name == NULL)
10612 short_name = path;
10613 if (is_unique(short_name, gap, i))
10614 {
10615 STRCPY(fnames[i], short_name);
10616 continue;
10617 }
10618
10619 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10620 if (rel_path == NULL)
10621 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010622 STRCPY(rel_path, ".");
10623 add_pathsep(rel_path);
10624 STRCAT(rel_path, short_name);
10625
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010626 vim_free(fnames[i]);
10627 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010628 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010629 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010630 }
10631
Bram Moolenaar162bd912010-07-28 22:29:10 +020010632theend:
10633 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010634 if (in_curdir != NULL)
10635 {
10636 for (i = 0; i < gap->ga_len; i++)
10637 vim_free(in_curdir[i]);
10638 vim_free(in_curdir);
10639 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010640 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010641 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010642
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010643 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010644 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010645}
10646
10647/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010648 * Calls globpath() with 'path' values for the given pattern and stores the
10649 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010650 * Returns the total number of matches.
10651 */
10652 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010653expand_in_path(
10654 garray_T *gap,
10655 char_u *pattern,
10656 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010657{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010658 char_u *curdir;
10659 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010660 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010661
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010662 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010663 return 0;
10664 mch_dirname(curdir, MAXPATHL);
10665
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010666 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010667 expand_path_option(curdir, &path_ga);
10668 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010669 if (path_ga.ga_len == 0)
10670 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010671
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010672 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010673 ga_clear_strings(&path_ga);
10674 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010675 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010676
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010677 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010678 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010679
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010680 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010681}
10682#endif
10683
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010684#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10685/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010686 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10687 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010688 */
10689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010690remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010691{
10692 int i;
10693 int j;
10694 char_u **fnames = (char_u **)gap->ga_data;
10695
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010696 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010697 for (i = gap->ga_len - 1; i > 0; --i)
10698 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10699 {
10700 vim_free(fnames[i]);
10701 for (j = i + 1; j < gap->ga_len; ++j)
10702 fnames[j - 1] = fnames[j];
10703 --gap->ga_len;
10704 }
10705}
10706#endif
10707
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010708static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010709
10710/*
10711 * Return TRUE if "p" contains what looks like an environment variable.
10712 * Allowing for escaping.
10713 */
10714 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010715has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010716{
10717 for ( ; *p; mb_ptr_adv(p))
10718 {
10719 if (*p == '\\' && p[1] != NUL)
10720 ++p;
10721 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010722#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010723 "$%"
10724#else
10725 "$"
10726#endif
10727 , *p) != NULL)
10728 return TRUE;
10729 }
10730 return FALSE;
10731}
10732
10733#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010734static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010735
10736/*
10737 * Return TRUE if "p" contains a special wildcard character.
10738 * Allowing for escaping.
10739 */
10740 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010741has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010742{
10743 for ( ; *p; mb_ptr_adv(p))
10744 {
10745 if (*p == '\\' && p[1] != NUL)
10746 ++p;
10747 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10748 return TRUE;
10749 }
10750 return FALSE;
10751}
10752#endif
10753
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754/*
10755 * Generic wildcard expansion code.
10756 *
10757 * Characters in "pat" that should not be expanded must be preceded with a
10758 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10759 *
10760 * Return FAIL when no single file was found. In this case "num_file" is not
10761 * set, and "file" may contain an error message.
10762 * Return OK when some files found. "num_file" is set to the number of
10763 * matches, "file" to the array of matches. Call FreeWild() later.
10764 */
10765 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010766gen_expand_wildcards(
10767 int num_pat, /* number of input patterns */
10768 char_u **pat, /* array of input patterns */
10769 int *num_file, /* resulting number of files */
10770 char_u ***file, /* array of resulting files */
10771 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772{
10773 int i;
10774 garray_T ga;
10775 char_u *p;
10776 static int recursive = FALSE;
10777 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010778 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010779#if defined(FEAT_SEARCHPATH)
10780 int did_expand_in_path = FALSE;
10781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782
10783 /*
10784 * expand_env() is called to expand things like "~user". If this fails,
10785 * it calls ExpandOne(), which brings us back here. In this case, always
10786 * call the machine specific expansion function, if possible. Otherwise,
10787 * return FAIL.
10788 */
10789 if (recursive)
10790#ifdef SPECIAL_WILDCHAR
10791 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10792#else
10793 return FAIL;
10794#endif
10795
10796#ifdef SPECIAL_WILDCHAR
10797 /*
10798 * If there are any special wildcard characters which we cannot handle
10799 * here, call machine specific function for all the expansion. This
10800 * avoids starting the shell for each argument separately.
10801 * For `=expr` do use the internal function.
10802 */
10803 for (i = 0; i < num_pat; i++)
10804 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010805 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806# ifdef VIM_BACKTICK
10807 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10808# endif
10809 )
10810 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10811 }
10812#endif
10813
10814 recursive = TRUE;
10815
10816 /*
10817 * The matching file names are stored in a growarray. Init it empty.
10818 */
10819 ga_init2(&ga, (int)sizeof(char_u *), 30);
10820
10821 for (i = 0; i < num_pat; ++i)
10822 {
10823 add_pat = -1;
10824 p = pat[i];
10825
10826#ifdef VIM_BACKTICK
10827 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010830 if (add_pat == -1)
10831 retval = FAIL;
10832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 else
10834#endif
10835 {
10836 /*
10837 * First expand environment variables, "~/" and "~user/".
10838 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010839 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010841 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 if (p == NULL)
10843 p = pat[i];
10844#ifdef UNIX
10845 /*
10846 * On Unix, if expand_env() can't expand an environment
10847 * variable, use the shell to do that. Discard previously
10848 * found file names and start all over again.
10849 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010850 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 {
10852 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010853 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010855 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 recursive = FALSE;
10857 return i;
10858 }
10859#endif
10860 }
10861
10862 /*
10863 * If there are wildcards: Expand file names and add each match to
10864 * the list. If there is no match, and EW_NOTFOUND is given, add
10865 * the pattern.
10866 * If there are no wildcards: Add the file name if it exists or
10867 * when EW_NOTFOUND is given.
10868 */
10869 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010870 {
10871#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010872 if ((flags & EW_PATH)
10873 && !mch_isFullName(p)
10874 && !(p[0] == '.'
10875 && (vim_ispathsep(p[1])
10876 || (p[1] == '.' && vim_ispathsep(p[2]))))
10877 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010878 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010879 /* :find completion where 'path' is used.
10880 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010881 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010882 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010883 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010884 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010885 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010886 else
10887#endif
10888 add_pat = mch_expandpath(&ga, p, flags);
10889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 }
10891
10892 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10893 {
10894 char_u *t = backslash_halve_save(p);
10895
10896#if defined(MACOS_CLASSIC)
10897 slash_to_colon(t);
10898#endif
10899 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10900 * "vim c:/" work. */
10901 if (flags & EW_NOTFOUND)
10902 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020010903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 addfile(&ga, t, flags);
10905 vim_free(t);
10906 }
10907
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010908#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010909 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010910 uniquefy_paths(&ga, p);
10911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (p != pat[i])
10913 vim_free(p);
10914 }
10915
10916 *num_file = ga.ga_len;
10917 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10918
10919 recursive = FALSE;
10920
Bram Moolenaar336bd622016-01-17 18:23:58 +010010921 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922}
10923
10924# ifdef VIM_BACKTICK
10925
10926/*
10927 * Return TRUE if we can expand this backtick thing here.
10928 */
10929 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010930vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931{
10932 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10933}
10934
10935/*
10936 * Expand an item in `backticks` by executing it as a command.
10937 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020010938 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 */
10940 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010941expand_backtick(
10942 garray_T *gap,
10943 char_u *pat,
10944 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
10946 char_u *p;
10947 char_u *cmd;
10948 char_u *buffer;
10949 int cnt = 0;
10950 int i;
10951
10952 /* Create the command: lop off the backticks. */
10953 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10954 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010955 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
10957#ifdef FEAT_EVAL
10958 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010959 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 else
10961#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010962 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010963 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 vim_free(cmd);
10965 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010966 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967
10968 cmd = buffer;
10969 while (*cmd != NUL)
10970 {
10971 cmd = skipwhite(cmd); /* skip over white space */
10972 p = cmd;
10973 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10974 ++p;
10975 /* add an entry if it is not empty */
10976 if (p > cmd)
10977 {
10978 i = *p;
10979 *p = NUL;
10980 addfile(gap, cmd, flags);
10981 *p = i;
10982 ++cnt;
10983 }
10984 cmd = p;
10985 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10986 ++cmd;
10987 }
10988
10989 vim_free(buffer);
10990 return cnt;
10991}
10992# endif /* VIM_BACKTICK */
10993
10994/*
10995 * Add a file to a file list. Accepted flags:
10996 * EW_DIR add directories
10997 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010998 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 * EW_NOTFOUND add even when it doesn't exist
11000 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011001 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 */
11003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011004addfile(
11005 garray_T *gap,
11006 char_u *f, /* filename */
11007 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008{
11009 char_u *p;
11010 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011011 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011013 /* if the file/dir/link doesn't exist, may not add it */
11014 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011015 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 return;
11017
11018#ifdef FNAME_ILLEGAL
11019 /* if the file/dir contains illegal characters, don't add it */
11020 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11021 return;
11022#endif
11023
11024 isdir = mch_isdir(f);
11025 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11026 return;
11027
Bram Moolenaarb5971142015-03-21 17:32:19 +010011028 /* If the file isn't executable, may not add it. Do accept directories.
11029 * When invoked from expand_shellcmd() do not use $PATH. */
11030 if (!isdir && (flags & EW_EXEC)
11031 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011032 return;
11033
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 /* Make room for another item in the file list. */
11035 if (ga_grow(gap, 1) == FAIL)
11036 return;
11037
11038 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11039 if (p == NULL)
11040 return;
11041
11042 STRCPY(p, f);
11043#ifdef BACKSLASH_IN_FILENAME
11044 slash_adjust(p);
11045#endif
11046 /*
11047 * Append a slash or backslash after directory names if none is present.
11048 */
11049#ifndef DONT_ADD_PATHSEP_TO_DIR
11050 if (isdir && (flags & EW_ADDSLASH))
11051 add_pathsep(p);
11052#endif
11053 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054}
11055#endif /* !NO_EXPANDPATH */
11056
11057#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11058
11059#ifndef SEEK_SET
11060# define SEEK_SET 0
11061#endif
11062#ifndef SEEK_END
11063# define SEEK_END 2
11064#endif
11065
11066/*
11067 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011068 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11069 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 * Returns an allocated string, or NULL for error.
11071 */
11072 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011073get_cmd_output(
11074 char_u *cmd,
11075 char_u *infile, /* optional input file name */
11076 int flags, /* can be SHELL_SILENT */
11077 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 char_u *tempname;
11080 char_u *command;
11081 char_u *buffer = NULL;
11082 int len;
11083 int i = 0;
11084 FILE *fd;
11085
11086 if (check_restricted() || check_secure())
11087 return NULL;
11088
11089 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011090 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 {
11092 EMSG(_(e_notmp));
11093 return NULL;
11094 }
11095
11096 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011097 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 if (command == NULL)
11099 goto done;
11100
11101 /*
11102 * Call the shell to execute the command (errors are ignored).
11103 * Don't check timestamps here.
11104 */
11105 ++no_check_timestamps;
11106 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11107 --no_check_timestamps;
11108
11109 vim_free(command);
11110
11111 /*
11112 * read the names from the file into memory
11113 */
11114# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011115 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 fd = mch_fopen((char *)tempname, "r");
11117# else
11118 fd = mch_fopen((char *)tempname, READBIN);
11119# endif
11120
11121 if (fd == NULL)
11122 {
11123 EMSG2(_(e_notopen), tempname);
11124 goto done;
11125 }
11126
11127 fseek(fd, 0L, SEEK_END);
11128 len = ftell(fd); /* get size of temp file */
11129 fseek(fd, 0L, SEEK_SET);
11130
11131 buffer = alloc(len + 1);
11132 if (buffer != NULL)
11133 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11134 fclose(fd);
11135 mch_remove(tempname);
11136 if (buffer == NULL)
11137 goto done;
11138#ifdef VMS
11139 len = i; /* VMS doesn't give us what we asked for... */
11140#endif
11141 if (i != len)
11142 {
11143 EMSG2(_(e_notread), tempname);
11144 vim_free(buffer);
11145 buffer = NULL;
11146 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011147 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011148 {
11149 /* Change NUL into SOH, otherwise the string is truncated. */
11150 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011151 if (buffer[i] == NUL)
11152 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011153
Bram Moolenaar162bd912010-07-28 22:29:10 +020011154 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011155 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011156 else
11157 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158
11159done:
11160 vim_free(tempname);
11161 return buffer;
11162}
11163#endif
11164
11165/*
11166 * Free the list of files returned by expand_wildcards() or other expansion
11167 * functions.
11168 */
11169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011170FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011172 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 while (count--)
11175 vim_free(files[count]);
11176 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177}
11178
11179/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011180 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 * Don't do this when still processing a command or a mapping.
11182 * Don't do this when inside a ":normal" command.
11183 */
11184 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011185goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186{
11187 return (p_im && stuff_empty() && typebuf_typed());
11188}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011189
11190/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011191 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011192 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11193 * - Remove any argument. E.g., "csh -f" -> "csh".
11194 * But don't allow a space in the path, so that this works:
11195 * "/usr/bin/csh --rcfile ~/.cshrc"
11196 * But don't do that for Windows, it's common to have a space in the path.
11197 */
11198 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011199get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011200{
11201 char_u *p;
11202
11203#ifdef WIN3264
11204 p = gettail(p_sh);
11205 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11206#else
11207 p = skiptowhite(p_sh);
11208 if (*p == NUL)
11209 {
11210 /* No white space, use the tail. */
11211 p = vim_strsave(gettail(p_sh));
11212 }
11213 else
11214 {
11215 char_u *p1, *p2;
11216
11217 /* Find the last path separator before the space. */
11218 p1 = p_sh;
11219 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11220 if (vim_ispathsep(*p2))
11221 p1 = p2 + 1;
11222 p = vim_strnsave(p1, (int)(p - p1));
11223 }
11224#endif
11225 return p;
11226}