blob: d8e7e1accd2a67cb5673b4ea5e3a89efc9c96fc1 [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 Moolenaar828c3d72018-06-19 18:58:07 +020017#if defined(FEAT_CMDL_COMPL) && defined(WIN3264)
18# include <lm.h>
19#endif
20
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static char_u *vim_version_dir(char_u *vimdir);
22static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
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 Moolenaar04958cb2018-06-23 19:23:02 +020035#ifdef FEAT_VARTABS
36 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
37 curbuf->b_p_vts_array, FALSE);
38#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020039 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041}
42
43/*
44 * Count the size (in window cells) of the indent in line "lnum".
45 */
46 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010047get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020049#ifdef FEAT_VARTABS
50 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
51 curbuf->b_p_vts_array, FALSE);
52#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020053 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000055}
56
57#if defined(FEAT_FOLDING) || defined(PROTO)
58/*
59 * Count the size (in window cells) of the indent in line "lnum" of buffer
60 * "buf".
61 */
62 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010063get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000064{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020065#ifdef FEAT_VARTABS
66 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
67 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
68#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020069 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000071}
72#endif
73
74/*
75 * count the size (in window cells) of the indent in line "ptr", with
76 * 'tabstop' at "ts"
77 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000078 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010079get_indent_str(
80 char_u *ptr,
81 int ts,
82 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 int count = 0;
85
86 for ( ; *ptr; ++ptr)
87 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020088 if (*ptr == TAB)
89 {
90 if (!list || lcs_tab1) /* count a tab for what it is worth */
91 count += ts - (count % ts);
92 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020093 /* In list mode, when tab is not set, count screen char width
94 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020095 count += ptr2cells(ptr);
96 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 else if (*ptr == ' ')
98 ++count; /* count a space for one */
99 else
100 break;
101 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000102 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103}
104
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200105#ifdef FEAT_VARTABS
106/*
107 * Count the size (in window cells) of the indent in line "ptr", using
108 * variable tabstops.
109 * if "list" is TRUE, count only screen size for tabs.
110 */
111 int
112get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
113{
114 int count = 0;
115
116 for ( ; *ptr; ++ptr)
117 {
118 if (*ptr == TAB) /* count a tab for what it is worth */
119 {
120 if (!list || lcs_tab1)
121 count += tabstop_padding(count, ts, vts);
122 else
123 /* In list mode, when tab is not set, count screen char width
124 * for Tab, displays: ^I */
125 count += ptr2cells(ptr);
126 }
127 else if (*ptr == ' ')
128 ++count; /* count a space for one */
129 else
130 break;
131 }
132 return count;
133}
134#endif
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Set the indent of the current line.
138 * Leaves the cursor on the first non-blank in the line.
139 * Caller must take care of undo.
140 * "flags":
141 * SIN_CHANGED: call changed_bytes() if the line was changed.
142 * SIN_INSERT: insert the indent in front of the line.
143 * SIN_UNDO: save line for undo before changing it.
144 * Returns TRUE if the line was changed.
145 */
146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100147set_indent(
148 int size, /* measured in spaces */
149 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150{
151 char_u *p;
152 char_u *newline;
153 char_u *oldline;
154 char_u *s;
155 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000156 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 int line_len;
158 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000159 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200160#ifdef FEAT_VARTABS
161 int ind_col = 0;
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000164 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000165 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000166 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168 /*
169 * First check if there is anything to do and compute the number of
170 * characters needed for the indent.
171 */
172 todo = size;
173 ind_len = 0;
174 p = oldline = ml_get_curline();
175
176 /* Calculate the buffer size for the new indent, and check to see if it
177 * isn't already set */
178
Bram Moolenaar5002c292007-07-24 13:26:15 +0000179 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
180 * 'preserveindent' are set count the number of characters at the
181 * beginning of the line to be copied */
182 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {
184 /* If 'preserveindent' is set then reuse as much as possible of
185 * the existing indent structure for the new indent */
186 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
187 {
188 ind_done = 0;
189
190 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100191 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
193 if (*p == TAB)
194 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200195#ifdef FEAT_VARTABS
196 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
197 curbuf->b_p_vts_array);
198#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 tab_pad = (int)curbuf->b_p_ts
200 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 /* stop if this tab will overshoot the target */
203 if (todo < tab_pad)
204 break;
205 todo -= tab_pad;
206 ++ind_len;
207 ind_done += tab_pad;
208 }
209 else
210 {
211 --todo;
212 ++ind_len;
213 ++ind_done;
214 }
215 ++p;
216 }
217
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200218#ifdef FEAT_VARTABS
219 /* These diverge from this point. */
220 ind_col = ind_done;
221#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000222 /* Set initial number of whitespace chars to copy if we are
223 * preserving indent but expandtab is set */
224 if (curbuf->b_p_et)
225 orig_char_len = ind_len;
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200228#ifdef FEAT_VARTABS
229 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
230 curbuf->b_p_vts_array);
231#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200233#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000234 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {
236 doit = TRUE;
237 todo -= tab_pad;
238 ++ind_len;
239 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200240#ifdef FEAT_VARTABS
241 ind_col += tab_pad;
242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 }
244 }
245
246 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200247#ifdef FEAT_VARTABS
248 for (;;)
249 {
250 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
251 curbuf->b_p_vts_array);
252 if (todo < tab_pad)
253 break;
254 if (*p != TAB)
255 doit = TRUE;
256 else
257 ++p;
258 todo -= tab_pad;
259 ++ind_len;
260 ind_col += tab_pad;
261 }
262#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 while (todo >= (int)curbuf->b_p_ts)
264 {
265 if (*p != TAB)
266 doit = TRUE;
267 else
268 ++p;
269 todo -= (int)curbuf->b_p_ts;
270 ++ind_len;
271 /* ind_done += (int)curbuf->b_p_ts; */
272 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 }
275 /* count spaces required for indent */
276 while (todo > 0)
277 {
278 if (*p != ' ')
279 doit = TRUE;
280 else
281 ++p;
282 --todo;
283 ++ind_len;
284 /* ++ind_done; */
285 }
286
287 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100288 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 return FALSE;
290
291 /* Allocate memory for the new line. */
292 if (flags & SIN_INSERT)
293 p = oldline;
294 else
295 p = skipwhite(p);
296 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000297
298 /* If 'preserveindent' and 'expandtab' are both set keep the original
299 * characters and allocate accordingly. We will fill the rest with spaces
300 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000301 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000302 {
303 newline = alloc(orig_char_len + size - ind_done + line_len);
304 if (newline == NULL)
305 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000306 todo = size - ind_done;
307 ind_len = orig_char_len + todo; /* Set total length of indent in
308 * characters, which may have been
309 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000310 p = oldline;
311 s = newline;
312 while (orig_char_len > 0)
313 {
314 *s++ = *p++;
315 orig_char_len--;
316 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000317
Bram Moolenaar5002c292007-07-24 13:26:15 +0000318 /* Skip over any additional white space (useful when newindent is less
319 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100320 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000321 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000322
Bram Moolenaar5002c292007-07-24 13:26:15 +0000323 }
324 else
325 {
326 todo = size;
327 newline = alloc(ind_len + line_len);
328 if (newline == NULL)
329 return FALSE;
330 s = newline;
331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332
333 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 /* if 'expandtab' isn't set: use TABs */
335 if (!curbuf->b_p_et)
336 {
337 /* If 'preserveindent' is set then reuse as much as possible of
338 * the existing indent structure for the new indent */
339 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
340 {
341 p = oldline;
342 ind_done = 0;
343
Bram Moolenaar1c465442017-03-12 20:10:05 +0100344 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
346 if (*p == TAB)
347 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200348#ifdef FEAT_VARTABS
349 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
350 curbuf->b_p_vts_array);
351#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 tab_pad = (int)curbuf->b_p_ts
353 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 /* stop if this tab will overshoot the target */
356 if (todo < tab_pad)
357 break;
358 todo -= tab_pad;
359 ind_done += tab_pad;
360 }
361 else
362 {
363 --todo;
364 ++ind_done;
365 }
366 *s++ = *p++;
367 }
368
369 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200370#ifdef FEAT_VARTABS
371 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
372 curbuf->b_p_vts_array);
373#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (todo >= tab_pad)
377 {
378 *s++ = TAB;
379 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200380#ifdef FEAT_VARTABS
381 ind_done += tab_pad;
382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384
385 p = skipwhite(p);
386 }
387
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200388#ifdef FEAT_VARTABS
389 for (;;)
390 {
391 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
392 curbuf->b_p_vts_array);
393 if (todo < tab_pad)
394 break;
395 *s++ = TAB;
396 todo -= tab_pad;
397 ind_done += tab_pad;
398 }
399#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 while (todo >= (int)curbuf->b_p_ts)
401 {
402 *s++ = TAB;
403 todo -= (int)curbuf->b_p_ts;
404 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 while (todo > 0)
408 {
409 *s++ = ' ';
410 --todo;
411 }
412 mch_memmove(s, p, (size_t)line_len);
413
414 /* Replace the line (unless undo fails). */
415 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
416 {
417 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
418 if (flags & SIN_CHANGED)
419 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200420 /* Correct saved cursor position if it is in this line. */
421 if (saved_cursor.lnum == curwin->w_cursor.lnum)
422 {
423 if (saved_cursor.col >= (colnr_T)(p - oldline))
424 /* cursor was after the indent, adjust for the number of
425 * bytes added/removed */
426 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
427 else if (saved_cursor.col >= (colnr_T)(s - newline))
428 /* cursor was in the indent, and is now after it, put it back
429 * at the start of the indent (replacing spaces with TAB) */
430 saved_cursor.col = (colnr_T)(s - newline);
431 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000432 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 }
434 else
435 vim_free(newline);
436
437 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000438 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439}
440
441/*
442 * Copy the indent from ptr to the current line (and fill to size)
443 * Leaves the cursor on the first non-blank in the line.
444 * Returns TRUE if the line was changed.
445 */
446 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100447copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 char_u *p = NULL;
450 char_u *line = NULL;
451 char_u *s;
452 int todo;
453 int ind_len;
454 int line_len = 0;
455 int tab_pad;
456 int ind_done;
457 int round;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200458#ifdef FEAT_VARTABS
459 int ind_col;
460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
462 /* Round 1: compute the number of characters needed for the indent
463 * Round 2: copy the characters. */
464 for (round = 1; round <= 2; ++round)
465 {
466 todo = size;
467 ind_len = 0;
468 ind_done = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200469#ifdef FEAT_VARTABS
470 ind_col = 0;
471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 s = src;
473
474 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100475 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {
477 if (*s == TAB)
478 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200479#ifdef FEAT_VARTABS
480 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
481 curbuf->b_p_vts_array);
482#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 tab_pad = (int)curbuf->b_p_ts
484 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 /* Stop if this tab will overshoot the target */
487 if (todo < tab_pad)
488 break;
489 todo -= tab_pad;
490 ind_done += tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200491#ifdef FEAT_VARTABS
492 ind_col += tab_pad;
493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 }
495 else
496 {
497 --todo;
498 ++ind_done;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200499#ifdef FEAT_VARTABS
500 ++ind_col;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000504 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 *p++ = *s;
506 ++s;
507 }
508
509 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200510#ifdef FEAT_VARTABS
511 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
512 curbuf->b_p_vts_array);
513#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200515#endif
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200516 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
518 todo -= tab_pad;
519 ++ind_len;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200520#ifdef FEAT_VARTABS
521 ind_col += tab_pad;
522#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000523 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 *p++ = TAB;
525 }
526
527 /* Add tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200528 if (!curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200530#ifdef FEAT_VARTABS
531 for (;;)
532 {
533 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
534 curbuf->b_p_vts_array);
535 if (todo < tab_pad)
536 break;
537 todo -= tab_pad;
538 ++ind_len;
539 ind_col += tab_pad;
540 if (p != NULL)
541 *p++ = TAB;
542 }
543#else
544 while (todo >= (int)curbuf->b_p_ts)
545 {
546 todo -= (int)curbuf->b_p_ts;
547 ++ind_len;
548 if (p != NULL)
549 *p++ = TAB;
550 }
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553
554 /* Count/add spaces required for indent */
555 while (todo > 0)
556 {
557 --todo;
558 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000559 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 *p++ = ' ';
561 }
562
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000563 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
565 /* Allocate memory for the result: the copied indent, new indent
566 * and the rest of the line. */
567 line_len = (int)STRLEN(ml_get_curline()) + 1;
568 line = alloc(ind_len + line_len);
569 if (line == NULL)
570 return FALSE;
571 p = line;
572 }
573 }
574
575 /* Append the original line */
576 mch_memmove(p, ml_get_curline(), (size_t)line_len);
577
578 /* Replace the line */
579 ml_replace(curwin->w_cursor.lnum, line, FALSE);
580
581 /* Put the cursor after the indent. */
582 curwin->w_cursor.col = ind_len;
583 return TRUE;
584}
585
586/*
587 * Return the indent of the current line after a number. Return -1 if no
588 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000589 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 */
591 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100592get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 colnr_T col;
595 pos_T pos;
596
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200597 regmatch_T regmatch;
598 int lead_len = 0; /* length of comment leader */
599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (lnum > curbuf->b_ml.ml_line_count)
601 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000602 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200603
604#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200605 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
606 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200607 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000608#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200609 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
610 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200611 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200612 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200613
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200614 /* vim_regexec() expects a pointer to a line. This lets us
615 * start matching for the flp beyond any comment leader... */
616 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200617 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200618 pos.lnum = lnum;
619 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200620#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200621 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200622#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200623 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200624 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200625 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000626
627 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 getvcol(curwin, &pos, &col, NULL, NULL);
630 return (int)col;
631}
632
Bram Moolenaar597a4222014-06-25 14:39:50 +0200633#if defined(FEAT_LINEBREAK) || defined(PROTO)
634/*
635 * Return appropriate space number for breakindent, taking influencing
636 * parameters into account. Window must be specified, since it is not
637 * necessarily always the current one.
638 */
639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640get_breakindent_win(
641 win_T *wp,
642 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200643{
644 static int prev_indent = 0; /* cached indent value */
645 static long prev_ts = 0L; /* cached tabstop value */
646 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100647 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200648#ifdef FEAT_VARTABS
649 static int *prev_vts = NULL; /* cached vartabs values */
650#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200651 int bri = 0;
652 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200653 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200654 - ((wp->w_p_nu || wp->w_p_rnu)
655 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
656 ? number_width(wp) + 1 : 0);
657
658 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200659 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200660 || prev_tick != CHANGEDTICK(wp->w_buffer)
661#ifdef FEAT_VARTABS
662 || prev_vts != wp->w_buffer->b_p_vts_array
663#endif
664 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200665 {
666 prev_line = line;
667 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100668 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200669#ifdef FEAT_VARTABS
670 prev_vts = wp->w_buffer->b_p_vts_array;
671 prev_indent = get_indent_str_vtab(line,
672 (int)wp->w_buffer->b_p_ts,
673 wp->w_buffer->b_p_vts_array, wp->w_p_list);
674#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200675 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200676 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200677#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200678 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200679 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200680
681 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200682 if (wp->w_p_brisbr)
683 bri -= vim_strsize(p_sbr);
684
685 /* Add offset for number column, if 'n' is in 'cpoptions' */
686 bri += win_col_off2(wp);
687
688 /* never indent past left window margin */
689 if (bri < 0)
690 bri = 0;
691 /* always leave at least bri_min characters on the left,
692 * if text width is sufficient */
693 else if (bri > eff_wwidth - wp->w_p_brimin)
694 bri = (eff_wwidth - wp->w_p_brimin < 0)
695 ? 0 : eff_wwidth - wp->w_p_brimin;
696
697 return bri;
698}
699#endif
700
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704/*
705 * Return TRUE if the string "line" starts with a word from 'cinwords'.
706 */
707 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100708cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 char_u *cinw;
711 char_u *cinw_buf;
712 int cinw_len;
713 int retval = FALSE;
714 int len;
715
716 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
717 cinw_buf = alloc((unsigned)cinw_len);
718 if (cinw_buf != NULL)
719 {
720 line = skipwhite(line);
721 for (cinw = curbuf->b_p_cinw; *cinw; )
722 {
723 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
724 if (STRNCMP(line, cinw_buf, len) == 0
725 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
726 {
727 retval = TRUE;
728 break;
729 }
730 }
731 vim_free(cinw_buf);
732 }
733 return retval;
734}
735#endif
736
737/*
738 * open_line: Add a new line below or above the current line.
739 *
740 * For VREPLACE mode, we only add a new line when we get to the end of the
741 * file, otherwise we just start replacing the next line.
742 *
743 * Caller must take care of undo. Since VREPLACE may affect any number of
744 * lines however, it may call u_save_cursor() again when starting to change a
745 * new line.
746 * "flags": OPENLINE_DELSPACES delete spaces after cursor
747 * OPENLINE_DO_COM format comments
748 * OPENLINE_KEEPTRAIL keep trailing spaces
749 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200750 * OPENLINE_COM_LIST format comments with list or 2nd line indent
751 *
752 * "second_line_indent": indent for after ^^D in Insert mode or if flag
753 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200755 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100758open_line(
759 int dir, /* FORWARD or BACKWARD */
760 int flags,
761 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
763 char_u *saved_line; /* copy of the original line */
764 char_u *next_line = NULL; /* copy of the next line */
765 char_u *p_extra = NULL; /* what goes to next line */
766 int less_cols = 0; /* less columns for mark in new line */
767 int less_cols_off = 0; /* columns to skip for mark adjust */
768 pos_T old_cursor; /* old cursor position */
769 int newcol = 0; /* new cursor column */
770 int newindent = 0; /* auto-indent of the new line */
771 int n;
772 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200773 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#ifdef FEAT_COMMENTS
775 int extra_len = 0; /* length of p_extra string */
776 int lead_len; /* length of comment leader */
777 char_u *lead_flags; /* position in 'comments' for comment leader */
778 char_u *leader = NULL; /* copy of comment leader */
779#endif
780 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 int saved_char = NUL; /* init for GCC */
783#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
784 pos_T *pos;
785#endif
786#ifdef FEAT_SMARTINDENT
787 int do_si = (!p_paste && curbuf->b_p_si
788# ifdef FEAT_CINDENT
789 && !curbuf->b_p_cin
790# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200791# ifdef FEAT_EVAL
792 && *curbuf->b_p_inde == NUL
793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 );
795 int no_si = FALSE; /* reset did_si afterwards */
796 int first_char = NUL; /* init for GCC */
797#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200798#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 int vreplace_mode;
800#endif
801 int did_append; /* appended a new line */
802 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
803
804 /*
805 * make a copy of the current line so we can mess with it
806 */
807 saved_line = vim_strsave(ml_get_curline());
808 if (saved_line == NULL) /* out of memory! */
809 return FALSE;
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (State & VREPLACE_FLAG)
812 {
813 /*
814 * With VREPLACE we make a copy of the next line, which we will be
815 * starting to replace. First make the new line empty and let vim play
816 * with the indenting and comment leader to its heart's content. Then
817 * we grab what it ended up putting on the new line, put back the
818 * original line, and call ins_char() to put each new character onto
819 * the line, replacing what was there before and pushing the right
820 * stuff onto the replace stack. -- webb.
821 */
822 if (curwin->w_cursor.lnum < orig_line_count)
823 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
824 else
825 next_line = vim_strsave((char_u *)"");
826 if (next_line == NULL) /* out of memory! */
827 goto theend;
828
829 /*
830 * In VREPLACE mode, a NL replaces the rest of the line, and starts
831 * replacing the next line, so push all of the characters left on the
832 * line onto the replace stack. We'll push any other characters that
833 * might be replaced at the start of the next line (due to autoindent
834 * etc) a bit later.
835 */
836 replace_push(NUL); /* Call twice because BS over NL expects it */
837 replace_push(NUL);
838 p = saved_line + curwin->w_cursor.col;
839 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000840 {
841#ifdef FEAT_MBYTE
842 if (has_mbyte)
843 p += replace_push_mb(p);
844 else
845#endif
846 replace_push(*p++);
847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 saved_line[curwin->w_cursor.col] = NUL;
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200851 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
853 p_extra = saved_line + curwin->w_cursor.col;
854#ifdef FEAT_SMARTINDENT
855 if (do_si) /* need first char after new line break */
856 {
857 p = skipwhite(p_extra);
858 first_char = *p;
859 }
860#endif
861#ifdef FEAT_COMMENTS
862 extra_len = (int)STRLEN(p_extra);
863#endif
864 saved_char = *p_extra;
865 *p_extra = NUL;
866 }
867
868 u_clearline(); /* cannot do "U" command when adding lines */
869#ifdef FEAT_SMARTINDENT
870 did_si = FALSE;
871#endif
872 ai_col = 0;
873
874 /*
875 * If we just did an auto-indent, then we didn't type anything on
876 * the prior line, and it should be truncated. Do this even if 'ai' is not
877 * set because automatically inserting a comment leader also sets did_ai.
878 */
879 if (dir == FORWARD && did_ai)
880 trunc_line = TRUE;
881
882 /*
883 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
884 * indent to use for the new line.
885 */
886 if (curbuf->b_p_ai
887#ifdef FEAT_SMARTINDENT
888 || do_si
889#endif
890 )
891 {
892 /*
893 * count white space on current line
894 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200895#ifdef FEAT_VARTABS
896 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
897 curbuf->b_p_vts_array, FALSE);
898#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200899 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200900#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200901 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
902 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
904#ifdef FEAT_SMARTINDENT
905 /*
906 * Do smart indenting.
907 * In insert/replace mode (only when dir == FORWARD)
908 * we may move some text to the next line. If it starts with '{'
909 * don't add an indent. Fixes inserting a NL before '{' in line
910 * "if (condition) {"
911 */
912 if (!trunc_line && do_si && *saved_line != NUL
913 && (p_extra == NULL || first_char != '{'))
914 {
915 char_u *ptr;
916 char_u last_char;
917
918 old_cursor = curwin->w_cursor;
919 ptr = saved_line;
920# ifdef FEAT_COMMENTS
921 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200922 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 else
924 lead_len = 0;
925# endif
926 if (dir == FORWARD)
927 {
928 /*
929 * Skip preprocessor directives, unless they are
930 * recognised as comments.
931 */
932 if (
933# ifdef FEAT_COMMENTS
934 lead_len == 0 &&
935# endif
936 ptr[0] == '#')
937 {
938 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
939 ptr = ml_get(--curwin->w_cursor.lnum);
940 newindent = get_indent();
941 }
942# ifdef FEAT_COMMENTS
943 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200944 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 else
946 lead_len = 0;
947 if (lead_len > 0)
948 {
949 /*
950 * This case gets the following right:
951 * \*
952 * * A comment (read '\' as '/').
953 * *\
954 * #define IN_THE_WAY
955 * This should line up here;
956 */
957 p = skipwhite(ptr);
958 if (p[0] == '/' && p[1] == '*')
959 p++;
960 if (p[0] == '*')
961 {
962 for (p++; *p; p++)
963 {
964 if (p[0] == '/' && p[-1] == '*')
965 {
966 /*
967 * End of C comment, indent should line up
968 * with the line containing the start of
969 * the comment
970 */
971 curwin->w_cursor.col = (colnr_T)(p - ptr);
972 if ((pos = findmatch(NULL, NUL)) != NULL)
973 {
974 curwin->w_cursor.lnum = pos->lnum;
975 newindent = get_indent();
976 }
977 }
978 }
979 }
980 }
981 else /* Not a comment line */
982# endif
983 {
984 /* Find last non-blank in line */
985 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100986 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 --p;
988 last_char = *p;
989
990 /*
991 * find the character just before the '{' or ';'
992 */
993 if (last_char == '{' || last_char == ';')
994 {
995 if (p > ptr)
996 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100997 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 --p;
999 }
1000 /*
1001 * Try to catch lines that are split over multiple
1002 * lines. eg:
1003 * if (condition &&
1004 * condition) {
1005 * Should line up here!
1006 * }
1007 */
1008 if (*p == ')')
1009 {
1010 curwin->w_cursor.col = (colnr_T)(p - ptr);
1011 if ((pos = findmatch(NULL, '(')) != NULL)
1012 {
1013 curwin->w_cursor.lnum = pos->lnum;
1014 newindent = get_indent();
1015 ptr = ml_get_curline();
1016 }
1017 }
1018 /*
1019 * If last character is '{' do indent, without
1020 * checking for "if" and the like.
1021 */
1022 if (last_char == '{')
1023 {
1024 did_si = TRUE; /* do indent */
1025 no_si = TRUE; /* don't delete it when '{' typed */
1026 }
1027 /*
1028 * Look for "if" and the like, use 'cinwords'.
1029 * Don't do this if the previous line ended in ';' or
1030 * '}'.
1031 */
1032 else if (last_char != ';' && last_char != '}'
1033 && cin_is_cinword(ptr))
1034 did_si = TRUE;
1035 }
1036 }
1037 else /* dir == BACKWARD */
1038 {
1039 /*
1040 * Skip preprocessor directives, unless they are
1041 * recognised as comments.
1042 */
1043 if (
1044# ifdef FEAT_COMMENTS
1045 lead_len == 0 &&
1046# endif
1047 ptr[0] == '#')
1048 {
1049 int was_backslashed = FALSE;
1050
1051 while ((ptr[0] == '#' || was_backslashed) &&
1052 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1053 {
1054 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1055 was_backslashed = TRUE;
1056 else
1057 was_backslashed = FALSE;
1058 ptr = ml_get(++curwin->w_cursor.lnum);
1059 }
1060 if (was_backslashed)
1061 newindent = 0; /* Got to end of file */
1062 else
1063 newindent = get_indent();
1064 }
1065 p = skipwhite(ptr);
1066 if (*p == '}') /* if line starts with '}': do indent */
1067 did_si = TRUE;
1068 else /* can delete indent when '{' typed */
1069 can_si_back = TRUE;
1070 }
1071 curwin->w_cursor = old_cursor;
1072 }
1073 if (do_si)
1074 can_si = TRUE;
1075#endif /* FEAT_SMARTINDENT */
1076
1077 did_ai = TRUE;
1078 }
1079
1080#ifdef FEAT_COMMENTS
1081 /*
1082 * Find out if the current line starts with a comment leader.
1083 * This may then be inserted in front of the new line.
1084 */
1085 end_comment_pending = NUL;
1086 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001087 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 else
1089 lead_len = 0;
1090 if (lead_len > 0)
1091 {
1092 char_u *lead_repl = NULL; /* replaces comment leader */
1093 int lead_repl_len = 0; /* length of *lead_repl */
1094 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1095 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1096 char_u *comment_end = NULL; /* where lead_end has been found */
1097 int extra_space = FALSE; /* append extra space */
1098 int current_flag;
1099 int require_blank = FALSE; /* requires blank after middle */
1100 char_u *p2;
1101
1102 /*
1103 * If the comment leader has the start, middle or end flag, it may not
1104 * be used or may be replaced with the middle leader.
1105 */
1106 for (p = lead_flags; *p && *p != ':'; ++p)
1107 {
1108 if (*p == COM_BLANK)
1109 {
1110 require_blank = TRUE;
1111 continue;
1112 }
1113 if (*p == COM_START || *p == COM_MIDDLE)
1114 {
1115 current_flag = *p;
1116 if (*p == COM_START)
1117 {
1118 /*
1119 * Doing "O" on a start of comment does not insert leader.
1120 */
1121 if (dir == BACKWARD)
1122 {
1123 lead_len = 0;
1124 break;
1125 }
1126
1127 /* find start of middle part */
1128 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1129 require_blank = FALSE;
1130 }
1131
1132 /*
1133 * Isolate the strings of the middle and end leader.
1134 */
1135 while (*p && p[-1] != ':') /* find end of middle flags */
1136 {
1137 if (*p == COM_BLANK)
1138 require_blank = TRUE;
1139 ++p;
1140 }
1141 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1142
1143 while (*p && p[-1] != ':') /* find end of end flags */
1144 {
1145 /* Check whether we allow automatic ending of comments */
1146 if (*p == COM_AUTO_END)
1147 end_comment_pending = -1; /* means we want to set it */
1148 ++p;
1149 }
1150 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1151
1152 if (end_comment_pending == -1) /* we can set it now */
1153 end_comment_pending = lead_end[n - 1];
1154
1155 /*
1156 * If the end of the comment is in the same line, don't use
1157 * the comment leader.
1158 */
1159 if (dir == FORWARD)
1160 {
1161 for (p = saved_line + lead_len; *p; ++p)
1162 if (STRNCMP(p, lead_end, n) == 0)
1163 {
1164 comment_end = p;
1165 lead_len = 0;
1166 break;
1167 }
1168 }
1169
1170 /*
1171 * Doing "o" on a start of comment inserts the middle leader.
1172 */
1173 if (lead_len > 0)
1174 {
1175 if (current_flag == COM_START)
1176 {
1177 lead_repl = lead_middle;
1178 lead_repl_len = (int)STRLEN(lead_middle);
1179 }
1180
1181 /*
1182 * If we have hit RETURN immediately after the start
1183 * comment leader, then put a space after the middle
1184 * comment leader on the next line.
1185 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001186 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && ((p_extra != NULL
1188 && (int)curwin->w_cursor.col == lead_len)
1189 || (p_extra == NULL
1190 && saved_line[lead_len] == NUL)
1191 || require_blank))
1192 extra_space = TRUE;
1193 }
1194 break;
1195 }
1196 if (*p == COM_END)
1197 {
1198 /*
1199 * Doing "o" on the end of a comment does not insert leader.
1200 * Remember where the end is, might want to use it to find the
1201 * start (for C-comments).
1202 */
1203 if (dir == FORWARD)
1204 {
1205 comment_end = skipwhite(saved_line);
1206 lead_len = 0;
1207 break;
1208 }
1209
1210 /*
1211 * Doing "O" on the end of a comment inserts the middle leader.
1212 * Find the string for the middle leader, searching backwards.
1213 */
1214 while (p > curbuf->b_p_com && *p != ',')
1215 --p;
1216 for (lead_repl = p; lead_repl > curbuf->b_p_com
1217 && lead_repl[-1] != ':'; --lead_repl)
1218 ;
1219 lead_repl_len = (int)(p - lead_repl);
1220
1221 /* We can probably always add an extra space when doing "O" on
1222 * the comment-end */
1223 extra_space = TRUE;
1224
1225 /* Check whether we allow automatic ending of comments */
1226 for (p2 = p; *p2 && *p2 != ':'; p2++)
1227 {
1228 if (*p2 == COM_AUTO_END)
1229 end_comment_pending = -1; /* means we want to set it */
1230 }
1231 if (end_comment_pending == -1)
1232 {
1233 /* Find last character in end-comment string */
1234 while (*p2 && *p2 != ',')
1235 p2++;
1236 end_comment_pending = p2[-1];
1237 }
1238 break;
1239 }
1240 if (*p == COM_FIRST)
1241 {
1242 /*
1243 * Comment leader for first line only: Don't repeat leader
1244 * when using "O", blank out leader when using "o".
1245 */
1246 if (dir == BACKWARD)
1247 lead_len = 0;
1248 else
1249 {
1250 lead_repl = (char_u *)"";
1251 lead_repl_len = 0;
1252 }
1253 break;
1254 }
1255 }
1256 if (lead_len)
1257 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001258 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001259 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001260 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 allocated = leader; /* remember to free it later */
1262
1263 if (leader == NULL)
1264 lead_len = 0;
1265 else
1266 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001267 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 /*
1270 * Replace leader with lead_repl, right or left adjusted
1271 */
1272 if (lead_repl != NULL)
1273 {
1274 int c = 0;
1275 int off = 0;
1276
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001277 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
1279 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001280 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 else if (VIM_ISDIGIT(*p) || *p == '-')
1282 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001283 else
1284 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 if (c == COM_RIGHT) /* right adjusted leader */
1287 {
1288 /* find last non-white in the leader to line up with */
1289 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001290 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001293
1294#ifdef FEAT_MBYTE
1295 /* Compute the length of the replaced characters in
1296 * screen characters, not bytes. */
1297 {
1298 int repl_size = vim_strnsize(lead_repl,
1299 lead_repl_len);
1300 int old_size = 0;
1301 char_u *endp = p;
1302 int l;
1303
1304 while (old_size < repl_size && p > leader)
1305 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001306 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001307 old_size += ptr2cells(p);
1308 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001309 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001310 if (l != 0)
1311 mch_memmove(endp + l, endp,
1312 (size_t)((leader + lead_len) - endp));
1313 lead_len += l;
1314 }
1315#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (p < leader + lead_repl_len)
1317 p = leader;
1318 else
1319 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1322 if (p + lead_repl_len > leader + lead_len)
1323 p[lead_repl_len] = NUL;
1324
1325 /* blank-out any other chars from the old leader. */
1326 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001327 {
1328#ifdef FEAT_MBYTE
1329 int l = mb_head_off(leader, p);
1330
1331 if (l > 1)
1332 {
1333 p -= l;
1334 if (ptr2cells(p) > 1)
1335 {
1336 p[1] = ' ';
1337 --l;
1338 }
1339 mch_memmove(p + 1, p + l + 1,
1340 (size_t)((leader + lead_len) - (p + l + 1)));
1341 lead_len -= l;
1342 *p = ' ';
1343 }
1344 else
1345#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001346 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 else /* left adjusted leader */
1351 {
1352 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001353#ifdef FEAT_MBYTE
1354 /* Compute the length of the replaced characters in
1355 * screen characters, not bytes. Move the part that is
1356 * not to be overwritten. */
1357 {
1358 int repl_size = vim_strnsize(lead_repl,
1359 lead_repl_len);
1360 int i;
1361 int l;
1362
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001363 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001365 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001366 if (vim_strnsize(p, i + l) > repl_size)
1367 break;
1368 }
1369 if (i != lead_repl_len)
1370 {
1371 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001372 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001373 lead_len += lead_repl_len - i;
1374 }
1375 }
1376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1378
1379 /* Replace any remaining non-white chars in the old
1380 * leader by spaces. Keep Tabs, the indent must
1381 * remain the same. */
1382 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001383 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
1385 /* Don't put a space before a TAB. */
1386 if (p + 1 < leader + lead_len && p[1] == TAB)
1387 {
1388 --lead_len;
1389 mch_memmove(p, p + 1,
1390 (leader + lead_len) - p);
1391 }
1392 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001393 {
1394#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001395 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001396
1397 if (l > 1)
1398 {
1399 if (ptr2cells(p) > 1)
1400 {
1401 /* Replace a double-wide char with
1402 * two spaces */
1403 --l;
1404 *p++ = ' ';
1405 }
1406 mch_memmove(p + 1, p + l,
1407 (leader + lead_len) - p);
1408 lead_len -= l - 1;
1409 }
1410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414 *p = NUL;
1415 }
1416
1417 /* Recompute the indent, it may have changed. */
1418 if (curbuf->b_p_ai
1419#ifdef FEAT_SMARTINDENT
1420 || do_si
1421#endif
1422 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001423#ifdef FEAT_VARTABS
1424 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1425 curbuf->b_p_vts_array, FALSE);
1426#else
1427 newindent = get_indent_str(leader,
1428 (int)curbuf->b_p_ts, FALSE);
1429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 /* Add the indent offset */
1432 if (newindent + off < 0)
1433 {
1434 off = -newindent;
1435 newindent = 0;
1436 }
1437 else
1438 newindent += off;
1439
1440 /* Correct trailing spaces for the shift, so that
1441 * alignment remains equal. */
1442 while (off > 0 && lead_len > 0
1443 && leader[lead_len - 1] == ' ')
1444 {
1445 /* Don't do it when there is a tab before the space */
1446 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1447 break;
1448 --lead_len;
1449 --off;
1450 }
1451
1452 /* If the leader ends in white space, don't add an
1453 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001454 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 extra_space = FALSE;
1456 leader[lead_len] = NUL;
1457 }
1458
1459 if (extra_space)
1460 {
1461 leader[lead_len++] = ' ';
1462 leader[lead_len] = NUL;
1463 }
1464
1465 newcol = lead_len;
1466
1467 /*
1468 * if a new indent will be set below, remove the indent that
1469 * is in the comment leader
1470 */
1471 if (newindent
1472#ifdef FEAT_SMARTINDENT
1473 || did_si
1474#endif
1475 )
1476 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001477 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 --lead_len;
1480 --newcol;
1481 ++leader;
1482 }
1483 }
1484
1485 }
1486#ifdef FEAT_SMARTINDENT
1487 did_si = can_si = FALSE;
1488#endif
1489 }
1490 else if (comment_end != NULL)
1491 {
1492 /*
1493 * We have finished a comment, so we don't use the leader.
1494 * If this was a C-comment and 'ai' or 'si' is set do a normal
1495 * indent to align with the line containing the start of the
1496 * comment.
1497 */
1498 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1499 (curbuf->b_p_ai
1500#ifdef FEAT_SMARTINDENT
1501 || do_si
1502#endif
1503 ))
1504 {
1505 old_cursor = curwin->w_cursor;
1506 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1507 if ((pos = findmatch(NULL, NUL)) != NULL)
1508 {
1509 curwin->w_cursor.lnum = pos->lnum;
1510 newindent = get_indent();
1511 }
1512 curwin->w_cursor = old_cursor;
1513 }
1514 }
1515 }
1516#endif
1517
1518 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1519 if (p_extra != NULL)
1520 {
1521 *p_extra = saved_char; /* restore char that NUL replaced */
1522
1523 /*
1524 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1525 * non-blank.
1526 *
1527 * When in REPLACE mode, put the deleted blanks on the replace stack,
1528 * preceded by a NUL, so they can be put back when a BS is entered.
1529 */
1530 if (REPLACE_NORMAL(State))
1531 replace_push(NUL); /* end of extra blanks */
1532 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1533 {
1534 while ((*p_extra == ' ' || *p_extra == '\t')
1535#ifdef FEAT_MBYTE
1536 && (!enc_utf8
1537 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1538#endif
1539 )
1540 {
1541 if (REPLACE_NORMAL(State))
1542 replace_push(*p_extra);
1543 ++p_extra;
1544 ++less_cols_off;
1545 }
1546 }
1547 if (*p_extra != NUL)
1548 did_ai = FALSE; /* append some text, don't truncate now */
1549
1550 /* columns for marks adjusted for removed columns */
1551 less_cols = (int)(p_extra - saved_line);
1552 }
1553
1554 if (p_extra == NULL)
1555 p_extra = (char_u *)""; /* append empty line */
1556
1557#ifdef FEAT_COMMENTS
1558 /* concatenate leader and p_extra, if there is a leader */
1559 if (lead_len)
1560 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001561 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1562 {
1563 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001564 int padding = second_line_indent
1565 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001566
1567 /* Here whitespace is inserted after the comment char.
1568 * Below, set_indent(newindent, SIN_INSERT) will insert the
1569 * whitespace needed before the comment char. */
1570 for (i = 0; i < padding; i++)
1571 {
1572 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001573 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001574 newcol++;
1575 }
1576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 STRCAT(leader, p_extra);
1578 p_extra = leader;
1579 did_ai = TRUE; /* So truncating blanks works with comments */
1580 less_cols -= lead_len;
1581 }
1582 else
1583 end_comment_pending = NUL; /* turns out there was no leader */
1584#endif
1585
1586 old_cursor = curwin->w_cursor;
1587 if (dir == BACKWARD)
1588 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1592 == FAIL)
1593 goto theend;
1594 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001595 * with markers.
1596 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001597 * be marks there. But still needed in diff mode. */
1598 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1599#ifdef FEAT_DIFF
1600 || curwin->w_p_diff
1601#endif
1602 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001603 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 did_append = TRUE;
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 else
1607 {
1608 /*
1609 * In VREPLACE mode we are starting to replace the next line.
1610 */
1611 curwin->w_cursor.lnum++;
1612 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1613 {
1614 /* In case we NL to a new line, BS to the previous one, and NL
1615 * again, we don't want to save the new line for undo twice.
1616 */
1617 (void)u_save_cursor(); /* errors are ignored! */
1618 vr_lines_changed++;
1619 }
1620 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1621 changed_bytes(curwin->w_cursor.lnum, 0);
1622 curwin->w_cursor.lnum--;
1623 did_append = FALSE;
1624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 if (newindent
1627#ifdef FEAT_SMARTINDENT
1628 || did_si
1629#endif
1630 )
1631 {
1632 ++curwin->w_cursor.lnum;
1633#ifdef FEAT_SMARTINDENT
1634 if (did_si)
1635 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001636 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001639 newindent -= newindent % sw;
1640 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001643 /* Copy the indent */
1644 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 (void)copy_indent(newindent, saved_line);
1647
1648 /*
1649 * Set the 'preserveindent' option so that any further screwing
1650 * with the line doesn't entirely destroy our efforts to preserve
1651 * it. It gets restored at the function end.
1652 */
1653 curbuf->b_p_pi = TRUE;
1654 }
1655 else
1656 (void)set_indent(newindent, SIN_INSERT);
1657 less_cols -= curwin->w_cursor.col;
1658
1659 ai_col = curwin->w_cursor.col;
1660
1661 /*
1662 * In REPLACE mode, for each character in the new indent, there must
1663 * be a NUL on the replace stack, for when it is deleted with BS
1664 */
1665 if (REPLACE_NORMAL(State))
1666 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1667 replace_push(NUL);
1668 newcol += curwin->w_cursor.col;
1669#ifdef FEAT_SMARTINDENT
1670 if (no_si)
1671 did_si = FALSE;
1672#endif
1673 }
1674
1675#ifdef FEAT_COMMENTS
1676 /*
1677 * In REPLACE mode, for each character in the extra leader, there must be
1678 * a NUL on the replace stack, for when it is deleted with BS.
1679 */
1680 if (REPLACE_NORMAL(State))
1681 while (lead_len-- > 0)
1682 replace_push(NUL);
1683#endif
1684
1685 curwin->w_cursor = old_cursor;
1686
1687 if (dir == FORWARD)
1688 {
1689 if (trunc_line || (State & INSERT))
1690 {
1691 /* truncate current line at cursor */
1692 saved_line[curwin->w_cursor.col] = NUL;
1693 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1694 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1695 truncate_spaces(saved_line);
1696 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1697 saved_line = NULL;
1698 if (did_append)
1699 {
1700 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1701 curwin->w_cursor.lnum + 1, 1L);
1702 did_append = FALSE;
1703
1704 /* Move marks after the line break to the new line. */
1705 if (flags & OPENLINE_MARKFIX)
1706 mark_col_adjust(curwin->w_cursor.lnum,
1707 curwin->w_cursor.col + less_cols_off,
1708 1L, (long)-less_cols);
1709 }
1710 else
1711 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1712 }
1713
1714 /*
1715 * Put the cursor on the new line. Careful: the scrollup() above may
1716 * have moved w_cursor, we must use old_cursor.
1717 */
1718 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1719 }
1720 if (did_append)
1721 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1722
1723 curwin->w_cursor.col = newcol;
1724#ifdef FEAT_VIRTUALEDIT
1725 curwin->w_cursor.coladd = 0;
1726#endif
1727
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001728#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /*
1730 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1731 * fixthisline() from doing it (via change_indent()) by telling it we're in
1732 * normal INSERT mode.
1733 */
1734 if (State & VREPLACE_FLAG)
1735 {
1736 vreplace_mode = State; /* So we know to put things right later */
1737 State = INSERT;
1738 }
1739 else
1740 vreplace_mode = 0;
1741#endif
1742#ifdef FEAT_LISP
1743 /*
1744 * May do lisp indenting.
1745 */
1746 if (!p_paste
1747# ifdef FEAT_COMMENTS
1748 && leader == NULL
1749# endif
1750 && curbuf->b_p_lisp
1751 && curbuf->b_p_ai)
1752 {
1753 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001754 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756#endif
1757#ifdef FEAT_CINDENT
1758 /*
1759 * May do indenting after opening a new line.
1760 */
1761 if (!p_paste
1762 && (curbuf->b_p_cin
1763# ifdef FEAT_EVAL
1764 || *curbuf->b_p_inde != NUL
1765# endif
1766 )
1767 && in_cinkeys(dir == FORWARD
1768 ? KEY_OPEN_FORW
1769 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1770 {
1771 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001772 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 }
1774#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001775#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (vreplace_mode != 0)
1777 State = vreplace_mode;
1778#endif
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 /*
1781 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1782 * original line, and inserts the new stuff char by char, pushing old stuff
1783 * onto the replace stack (via ins_char()).
1784 */
1785 if (State & VREPLACE_FLAG)
1786 {
1787 /* Put new line in p_extra */
1788 p_extra = vim_strsave(ml_get_curline());
1789 if (p_extra == NULL)
1790 goto theend;
1791
1792 /* Put back original line */
1793 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1794
1795 /* Insert new stuff into line again */
1796 curwin->w_cursor.col = 0;
1797#ifdef FEAT_VIRTUALEDIT
1798 curwin->w_cursor.coladd = 0;
1799#endif
1800 ins_bytes(p_extra); /* will call changed_bytes() */
1801 vim_free(p_extra);
1802 next_line = NULL;
1803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001805 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806theend:
1807 curbuf->b_p_pi = saved_pi;
1808 vim_free(saved_line);
1809 vim_free(next_line);
1810 vim_free(allocated);
1811 return retval;
1812}
1813
1814#if defined(FEAT_COMMENTS) || defined(PROTO)
1815/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001816 * get_leader_len() returns the length in bytes of the prefix of the given
1817 * string which introduces a comment. If this string is not a comment then
1818 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 * When "flags" is not NULL, it is set to point to the flags of the recognized
1820 * comment leader.
1821 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001822 * If "include_space" is set, include trailing whitespace while calculating the
1823 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 */
1825 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001826get_leader_len(
1827 char_u *line,
1828 char_u **flags,
1829 int backward,
1830 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001833 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int got_com = FALSE;
1835 int found_one;
1836 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1837 char_u *string; /* pointer to comment string */
1838 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001839 int middle_match_len = 0;
1840 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001841 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar81340392012-06-06 16:12:59 +02001843 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001844 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 ++i;
1846
1847 /*
1848 * Repeat to match several nested comment strings.
1849 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001850 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 /*
1853 * scan through the 'comments' option for a match
1854 */
1855 found_one = FALSE;
1856 for (list = curbuf->b_p_com; *list; )
1857 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001858 /* Get one option part into part_buf[]. Advance "list" to next
1859 * one. Put "string" at start of string. */
1860 if (!got_com && flags != NULL)
1861 *flags = list; /* remember where flags started */
1862 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1864 string = vim_strchr(part_buf, ':');
1865 if (string == NULL) /* missing ':', ignore this part */
1866 continue;
1867 *string++ = NUL; /* isolate flags from string */
1868
Bram Moolenaara4271d52011-05-10 13:38:27 +02001869 /* If we found a middle match previously, use that match when this
1870 * is not a middle or end. */
1871 if (middle_match_len != 0
1872 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1873 && vim_strchr(part_buf, COM_END) == NULL)
1874 break;
1875
1876 /* When we already found a nested comment, only accept further
1877 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1879 continue;
1880
Bram Moolenaara4271d52011-05-10 13:38:27 +02001881 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1883 continue;
1884
Bram Moolenaara4271d52011-05-10 13:38:27 +02001885 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * When string starts with white space, must have some white space
1887 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001888 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001889 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001891 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001892 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001893 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 ++string;
1895 }
1896 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1897 ;
1898 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001899 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaara4271d52011-05-10 13:38:27 +02001901 /* When 'b' flag used, there must be white space or an
1902 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001904 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 continue;
1906
Bram Moolenaara4271d52011-05-10 13:38:27 +02001907 /* We have found a match, stop searching unless this is a middle
1908 * comment. The middle comment can be a substring of the end
1909 * comment in which case it's better to return the length of the
1910 * end comment and its flags. Thus we keep searching with middle
1911 * and end matches and use an end match if it matches better. */
1912 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1913 {
1914 if (middle_match_len == 0)
1915 {
1916 middle_match_len = j;
1917 saved_flags = prev_list;
1918 }
1919 continue;
1920 }
1921 if (middle_match_len != 0 && j > middle_match_len)
1922 /* Use this match instead of the middle match, since it's a
1923 * longer thus better match. */
1924 middle_match_len = 0;
1925
1926 if (middle_match_len == 0)
1927 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 found_one = TRUE;
1929 break;
1930 }
1931
Bram Moolenaara4271d52011-05-10 13:38:27 +02001932 if (middle_match_len != 0)
1933 {
1934 /* Use the previously found middle match after failing to find a
1935 * match with an end. */
1936 if (!got_com && flags != NULL)
1937 *flags = saved_flags;
1938 i += middle_match_len;
1939 found_one = TRUE;
1940 }
1941
1942 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (!found_one)
1944 break;
1945
Bram Moolenaar81340392012-06-06 16:12:59 +02001946 result = i;
1947
Bram Moolenaara4271d52011-05-10 13:38:27 +02001948 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001949 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 ++i;
1951
Bram Moolenaar81340392012-06-06 16:12:59 +02001952 if (include_space)
1953 result = i;
1954
Bram Moolenaara4271d52011-05-10 13:38:27 +02001955 /* If this comment doesn't nest, stop here. */
1956 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (vim_strchr(part_buf, COM_NEST) == NULL)
1958 break;
1959 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001960 return result;
1961}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001962
Bram Moolenaar81340392012-06-06 16:12:59 +02001963/*
1964 * Return the offset at which the last comment in line starts. If there is no
1965 * comment in the whole line, -1 is returned.
1966 *
1967 * When "flags" is not null, it is set to point to the flags describing the
1968 * recognized comment leader.
1969 */
1970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001972{
1973 int result = -1;
1974 int i, j;
1975 int lower_check_bound = 0;
1976 char_u *string;
1977 char_u *com_leader;
1978 char_u *com_flags;
1979 char_u *list;
1980 int found_one;
1981 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1982
1983 /*
1984 * Repeat to match several nested comment strings.
1985 */
1986 i = (int)STRLEN(line);
1987 while (--i >= lower_check_bound)
1988 {
1989 /*
1990 * scan through the 'comments' option for a match
1991 */
1992 found_one = FALSE;
1993 for (list = curbuf->b_p_com; *list; )
1994 {
1995 char_u *flags_save = list;
Bram Moolenaar53932812018-12-07 21:08:49 +01001996 int is_only_whitespace = FALSE;
Bram Moolenaar81340392012-06-06 16:12:59 +02001997
1998 /*
1999 * Get one option part into part_buf[]. Advance list to next one.
2000 * put string at start of string.
2001 */
2002 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2003 string = vim_strchr(part_buf, ':');
2004 if (string == NULL) /* If everything is fine, this cannot actually
2005 * happen. */
2006 {
2007 continue;
2008 }
2009 *string++ = NUL; /* Isolate flags from string. */
2010 com_leader = string;
2011
2012 /*
2013 * Line contents and string must match.
2014 * When string starts with white space, must have some white space
2015 * (but the amount does not need to match, there might be a mix of
2016 * TABs and spaces).
2017 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002018 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002019 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002020 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002021 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002022 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002023 ++string;
Bram Moolenaar53932812018-12-07 21:08:49 +01002024 if (*string == NUL)
2025 is_only_whitespace = TRUE;
Bram Moolenaar81340392012-06-06 16:12:59 +02002026 }
2027 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2028 /* do nothing */;
2029 if (string[j] != NUL)
2030 continue;
2031
2032 /*
2033 * When 'b' flag used, there must be white space or an
2034 * end-of-line after the string in the line.
2035 */
2036 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002037 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002038 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002039
2040 // For a middlepart comment that is only white space, only consider
2041 // it to match if everything before the current position in the
2042 // line is also whitespace.
2043 if (is_only_whitespace && vim_strchr(part_buf, COM_MIDDLE) != NULL)
2044 {
2045 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
2046 ;
2047 if (j < i)
2048 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +02002049 }
2050
2051 /*
2052 * We have found a match, stop searching.
2053 */
2054 found_one = TRUE;
2055
2056 if (flags)
2057 *flags = flags_save;
2058 com_flags = flags_save;
2059
2060 break;
2061 }
2062
2063 if (found_one)
2064 {
2065 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2066 int len1, len2, off;
2067
2068 result = i;
2069 /*
2070 * If this comment nests, continue searching.
2071 */
2072 if (vim_strchr(part_buf, COM_NEST) != NULL)
2073 continue;
2074
2075 lower_check_bound = i;
2076
2077 /* Let's verify whether the comment leader found is a substring
2078 * of other comment leaders. If it is, let's adjust the
2079 * lower_check_bound so that we make sure that we have determined
2080 * the comment leader correctly.
2081 */
2082
Bram Moolenaar1c465442017-03-12 20:10:05 +01002083 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002084 ++com_leader;
2085 len1 = (int)STRLEN(com_leader);
2086
2087 for (list = curbuf->b_p_com; *list; )
2088 {
2089 char_u *flags_save = list;
2090
2091 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2092 if (flags_save == com_flags)
2093 continue;
2094 string = vim_strchr(part_buf2, ':');
2095 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002096 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002097 ++string;
2098 len2 = (int)STRLEN(string);
2099 if (len2 == 0)
2100 continue;
2101
2102 /* Now we have to verify whether string ends with a substring
2103 * beginning the com_leader. */
2104 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2105 {
2106 --off;
2107 if (!STRNCMP(string + off, com_leader, len2 - off))
2108 {
2109 if (i - off < lower_check_bound)
2110 lower_check_bound = i - off;
2111 }
2112 }
2113 }
2114 }
2115 }
2116 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117}
2118#endif
2119
2120/*
2121 * Return the number of window lines occupied by buffer line "lnum".
2122 */
2123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126 return plines_win(curwin, lnum, TRUE);
2127}
2128
2129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002130plines_win(
2131 win_T *wp,
2132 linenr_T lnum,
2133 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135#if defined(FEAT_DIFF) || defined(PROTO)
2136 /* Check for filler lines above this buffer line. When folded the result
2137 * is one line anyway. */
2138 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2139}
2140
2141 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002142plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 return plines_win_nofill(curwin, lnum, TRUE);
2145}
2146
2147 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002148plines_win_nofill(
2149 win_T *wp,
2150 linenr_T lnum,
2151 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153#endif
2154 int lines;
2155
2156 if (!wp->w_p_wrap)
2157 return 1;
2158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 if (wp->w_width == 0)
2160 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161
2162#ifdef FEAT_FOLDING
2163 /* A folded lines is handled just like an empty line. */
2164 /* NOTE: Caller must handle lines that are MAYBE folded. */
2165 if (lineFolded(wp, lnum) == TRUE)
2166 return 1;
2167#endif
2168
2169 lines = plines_win_nofold(wp, lnum);
2170 if (winheight > 0 && lines > wp->w_height)
2171 return (int)wp->w_height;
2172 return lines;
2173}
2174
2175/*
2176 * Return number of window lines physical line "lnum" will occupy in window
2177 * "wp". Does not care about folding, 'wrap' or 'diff'.
2178 */
2179 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002180plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 char_u *s;
2183 long col;
2184 int width;
2185
2186 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2187 if (*s == NUL) /* empty line */
2188 return 1;
2189 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2190
2191 /*
2192 * If list mode is on, then the '$' at the end of the line may take up one
2193 * extra column.
2194 */
2195 if (wp->w_p_list && lcs_eol != NUL)
2196 col += 1;
2197
2198 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002199 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002201 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (width <= 0)
2203 return 32000;
2204 if (col <= width)
2205 return 1;
2206 col -= width;
2207 width += win_col_off2(wp);
2208 return (col + (width - 1)) / width + 1;
2209}
2210
2211/*
2212 * Like plines_win(), but only reports the number of physical screen lines
2213 * used from the start of the line to the given column number.
2214 */
2215 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002216plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217{
2218 long col;
2219 char_u *s;
2220 int lines = 0;
2221 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002222 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224#ifdef FEAT_DIFF
2225 /* Check for filler lines above this buffer line. When folded the result
2226 * is one line anyway. */
2227 lines = diff_check_fill(wp, lnum);
2228#endif
2229
2230 if (!wp->w_p_wrap)
2231 return lines + 1;
2232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (wp->w_width == 0)
2234 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaar597a4222014-06-25 14:39:50 +02002236 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 col = 0;
2239 while (*s != NUL && --column >= 0)
2240 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002241 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002242 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
2244
2245 /*
2246 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2247 * INSERT mode, then col must be adjusted so that it represents the last
2248 * screen position of the TAB. This only fixes an error when the TAB wraps
2249 * from one screen line to the next (when 'columns' is not a multiple of
2250 * 'ts') -- webb.
2251 */
2252 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002253 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
2255 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002256 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002258 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002259 if (width <= 0)
2260 return 9999;
2261
2262 lines += 1;
2263 if (col > width)
2264 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2265 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266}
2267
2268 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002269plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
2271 int count = 0;
2272
2273 while (first <= last)
2274 {
2275#ifdef FEAT_FOLDING
2276 int x;
2277
2278 /* Check if there are any really folded lines, but also included lines
2279 * that are maybe folded. */
2280 x = foldedCount(wp, first, NULL);
2281 if (x > 0)
2282 {
2283 ++count; /* count 1 for "+-- folded" line */
2284 first += x;
2285 }
2286 else
2287#endif
2288 {
2289#ifdef FEAT_DIFF
2290 if (first == wp->w_topline)
2291 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2292 else
2293#endif
2294 count += plines_win(wp, first, TRUE);
2295 ++first;
2296 }
2297 }
2298 return (count);
2299}
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301/*
2302 * Insert string "p" at the cursor position. Stops at a NUL byte.
2303 * Handles Replace mode and multi-byte characters.
2304 */
2305 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002306ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307{
2308 ins_bytes_len(p, (int)STRLEN(p));
2309}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311/*
2312 * Insert string "p" with length "len" at the cursor position.
2313 * Handles Replace mode and multi-byte characters.
2314 */
2315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002316ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002319#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 int n;
2321
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002322 if (has_mbyte)
2323 for (i = 0; i < len; i += n)
2324 {
2325 if (enc_utf8)
2326 /* avoid reading past p[len] */
2327 n = utfc_ptr2len_len(p + i, len - i);
2328 else
2329 n = (*mb_ptr2len)(p + i);
2330 ins_char_bytes(p + i, n);
2331 }
2332 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002333#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002334 for (i = 0; i < len; ++i)
2335 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338/*
2339 * Insert or replace a single character at the cursor position.
2340 * When in REPLACE or VREPLACE mode, replace any existing character.
2341 * Caller must have prepared for undo.
2342 * For multi-byte characters we get the whole character, the caller must
2343 * convert bytes to a character.
2344 */
2345 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002346ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002348 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002349 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002351#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 n = (*mb_char2bytes)(c, buf);
2353
2354 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2355 * Happens for CTRL-Vu9900. */
2356 if (buf[0] == 0)
2357 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002358#else
2359 buf[0] = c;
2360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 ins_char_bytes(buf, n);
2363}
2364
2365 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002366ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 int newlen; /* nr of bytes inserted */
2370 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2371 char_u *p;
2372 char_u *newp;
2373 char_u *oldp;
2374 int linelen; /* length of old line including NUL */
2375 colnr_T col;
2376 linenr_T lnum = curwin->w_cursor.lnum;
2377 int i;
2378
2379#ifdef FEAT_VIRTUALEDIT
2380 /* Break tabs if needed. */
2381 if (virtual_active() && curwin->w_cursor.coladd > 0)
2382 coladvance_force(getviscol());
2383#endif
2384
2385 col = curwin->w_cursor.col;
2386 oldp = ml_get(lnum);
2387 linelen = (int)STRLEN(oldp) + 1;
2388
2389 /* The lengths default to the values for when not replacing. */
2390 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
2393 if (State & REPLACE_FLAG)
2394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 if (State & VREPLACE_FLAG)
2396 {
2397 colnr_T new_vcol = 0; /* init for GCC */
2398 colnr_T vcol;
2399 int old_list;
2400#ifndef FEAT_MBYTE
2401 char_u buf[2];
2402#endif
2403
2404 /*
2405 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2406 * Returns the old value of list, so when finished,
2407 * curwin->w_p_list should be set back to this.
2408 */
2409 old_list = curwin->w_p_list;
2410 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2411 curwin->w_p_list = FALSE;
2412
2413 /*
2414 * In virtual replace mode each character may replace one or more
2415 * characters (zero if it's a TAB). Count the number of bytes to
2416 * be deleted to make room for the new character, counting screen
2417 * cells. May result in adding spaces to fill a gap.
2418 */
2419 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2420#ifndef FEAT_MBYTE
2421 buf[0] = c;
2422 buf[1] = NUL;
2423#endif
2424 new_vcol = vcol + chartabsize(buf, vcol);
2425 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2426 {
2427 vcol += chartabsize(oldp + col + oldlen, vcol);
2428 /* Don't need to remove a TAB that takes us to the right
2429 * position. */
2430 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2431 break;
2432#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002433 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#else
2435 ++oldlen;
2436#endif
2437 /* Deleted a bit too much, insert spaces. */
2438 if (vcol > new_vcol)
2439 newlen += vcol - new_vcol;
2440 }
2441 curwin->w_p_list = old_list;
2442 }
2443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (oldp[col] != NUL)
2445 {
2446 /* normal replace */
2447#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002448 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449#else
2450 oldlen = 1;
2451#endif
2452 }
2453
2454
2455 /* Push the replaced bytes onto the replace stack, so that they can be
2456 * put back when BS is used. The bytes of a multi-byte character are
2457 * done the other way around, so that the first byte is popped off
2458 * first (it tells the byte length of the character). */
2459 replace_push(NUL);
2460 for (i = 0; i < oldlen; ++i)
2461 {
2462#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002463 if (has_mbyte)
2464 i += replace_push_mb(oldp + col + i) - 1;
2465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002467 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469 }
2470
2471 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2472 if (newp == NULL)
2473 return;
2474
2475 /* Copy bytes before the cursor. */
2476 if (col > 0)
2477 mch_memmove(newp, oldp, (size_t)col);
2478
2479 /* Copy bytes after the changed character(s). */
2480 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002481 if (linelen > col + oldlen)
2482 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 (size_t)(linelen - col - oldlen));
2484
2485 /* Insert or overwrite the new character. */
2486#ifdef FEAT_MBYTE
2487 mch_memmove(p, buf, charlen);
2488 i = charlen;
2489#else
2490 *p = c;
2491 i = 1;
2492#endif
2493
2494 /* Fill with spaces when necessary. */
2495 while (i < newlen)
2496 p[i++] = ' ';
2497
2498 /* Replace the line in the buffer. */
2499 ml_replace(lnum, newp, FALSE);
2500
2501 /* mark the buffer as changed and prepare for displaying */
2502 changed_bytes(lnum, col);
2503
2504 /*
2505 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2506 * show the match for right parens and braces.
2507 */
2508 if (p_sm && (State & INSERT)
2509 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002510#ifdef FEAT_INS_EXPAND
2511 && !ins_compl_active()
2512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002514 {
2515#ifdef FEAT_MBYTE
2516 if (has_mbyte)
2517 showmatch(mb_ptr2char(buf));
2518 else
2519#endif
2520 showmatch(c);
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523#ifdef FEAT_RIGHTLEFT
2524 if (!p_ri || (State & REPLACE_FLAG))
2525#endif
2526 {
2527 /* Normal insert: move cursor right */
2528#ifdef FEAT_MBYTE
2529 curwin->w_cursor.col += charlen;
2530#else
2531 ++curwin->w_cursor.col;
2532#endif
2533 }
2534 /*
2535 * TODO: should try to update w_row here, to avoid recomputing it later.
2536 */
2537}
2538
2539/*
2540 * Insert a string at the cursor position.
2541 * Note: Does NOT handle Replace mode.
2542 * Caller must have prepared for undo.
2543 */
2544 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002545ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 char_u *oldp, *newp;
2548 int newlen = (int)STRLEN(s);
2549 int oldlen;
2550 colnr_T col;
2551 linenr_T lnum = curwin->w_cursor.lnum;
2552
2553#ifdef FEAT_VIRTUALEDIT
2554 if (virtual_active() && curwin->w_cursor.coladd > 0)
2555 coladvance_force(getviscol());
2556#endif
2557
2558 col = curwin->w_cursor.col;
2559 oldp = ml_get(lnum);
2560 oldlen = (int)STRLEN(oldp);
2561
2562 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2563 if (newp == NULL)
2564 return;
2565 if (col > 0)
2566 mch_memmove(newp, oldp, (size_t)col);
2567 mch_memmove(newp + col, s, (size_t)newlen);
2568 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2569 ml_replace(lnum, newp, FALSE);
2570 changed_bytes(lnum, col);
2571 curwin->w_cursor.col += newlen;
2572}
2573
2574/*
2575 * Delete one character under the cursor.
2576 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2577 * Caller must have prepared for undo.
2578 *
2579 * return FAIL for failure, OK otherwise
2580 */
2581 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002582del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583{
2584#ifdef FEAT_MBYTE
2585 if (has_mbyte)
2586 {
2587 /* Make sure the cursor is at the start of a character. */
2588 mb_adjust_cursor();
2589 if (*ml_get_cursor() == NUL)
2590 return FAIL;
2591 return del_chars(1L, fixpos);
2592 }
2593#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002594 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595}
2596
2597#if defined(FEAT_MBYTE) || defined(PROTO)
2598/*
2599 * Like del_bytes(), but delete characters instead of bytes.
2600 */
2601 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002602del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604 long bytes = 0;
2605 long i;
2606 char_u *p;
2607 int l;
2608
2609 p = ml_get_cursor();
2610 for (i = 0; i < count && *p != NUL; ++i)
2611 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002612 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 bytes += l;
2614 p += l;
2615 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002616 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617}
2618#endif
2619
2620/*
2621 * Delete "count" bytes under the cursor.
2622 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2623 * Caller must have prepared for undo.
2624 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002625 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 */
2627 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002628del_bytes(
2629 long count,
2630 int fixpos_arg,
2631 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
2633 char_u *oldp, *newp;
2634 colnr_T oldlen;
2635 linenr_T lnum = curwin->w_cursor.lnum;
2636 colnr_T col = curwin->w_cursor.col;
2637 int was_alloced;
2638 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002639 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 oldp = ml_get(lnum);
2642 oldlen = (int)STRLEN(oldp);
2643
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002644 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (col >= oldlen)
2646 return FAIL;
2647
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002648 /* If "count" is zero there is nothing to do. */
2649 if (count == 0)
2650 return OK;
2651
2652 /* If "count" is negative the caller must be doing something wrong. */
2653 if (count < 1)
2654 {
2655 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2656 return FAIL;
2657 }
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#ifdef FEAT_MBYTE
2660 /* If 'delcombine' is set and deleting (less than) one character, only
2661 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002662 if (p_deco && use_delcombine && enc_utf8
2663 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002665 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 int n;
2667
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002668 (void)utfc_ptr2char(oldp + col, cc);
2669 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
2671 /* Find the last composing char, there can be several. */
2672 n = col;
2673 do
2674 {
2675 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002676 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 n += count;
2678 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2679 fixpos = 0;
2680 }
2681 }
2682#endif
2683
2684 /*
2685 * When count is too big, reduce it.
2686 */
2687 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2688 if (movelen <= 1)
2689 {
2690 /*
2691 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002692 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2693 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002695 if (col > 0 && fixpos && restart_edit == 0
2696#ifdef FEAT_VIRTUALEDIT
2697 && (ve_flags & VE_ONEMORE) == 0
2698#endif
2699 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 --curwin->w_cursor.col;
2702#ifdef FEAT_VIRTUALEDIT
2703 curwin->w_cursor.coladd = 0;
2704#endif
2705#ifdef FEAT_MBYTE
2706 if (has_mbyte)
2707 curwin->w_cursor.col -=
2708 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2709#endif
2710 }
2711 count = oldlen - col;
2712 movelen = 1;
2713 }
2714
2715 /*
2716 * If the old line has been allocated the deletion can be done in the
2717 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002718 * Can't do this when using Netbeans, because we would need to invoke
2719 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002720 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002723 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002724 was_alloced = FALSE;
2725 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002727 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (was_alloced)
2729 newp = oldp; /* use same allocated memory */
2730 else
2731 { /* need to allocate a new line */
2732 newp = alloc((unsigned)(oldlen + 1 - count));
2733 if (newp == NULL)
2734 return FAIL;
2735 mch_memmove(newp, oldp, (size_t)col);
2736 }
2737 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2738 if (!was_alloced)
2739 ml_replace(lnum, newp, FALSE);
2740
2741 /* mark the buffer as changed and prepare for displaying */
2742 changed_bytes(lnum, curwin->w_cursor.col);
2743
2744 return OK;
2745}
2746
2747/*
2748 * Delete from cursor to end of line.
2749 * Caller must have prepared for undo.
2750 *
2751 * return FAIL for failure, OK otherwise
2752 */
2753 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002754truncate_line(
2755 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 char_u *newp;
2758 linenr_T lnum = curwin->w_cursor.lnum;
2759 colnr_T col = curwin->w_cursor.col;
2760
2761 if (col == 0)
2762 newp = vim_strsave((char_u *)"");
2763 else
2764 newp = vim_strnsave(ml_get(lnum), col);
2765
2766 if (newp == NULL)
2767 return FAIL;
2768
2769 ml_replace(lnum, newp, FALSE);
2770
2771 /* mark the buffer as changed and prepare for displaying */
2772 changed_bytes(lnum, curwin->w_cursor.col);
2773
2774 /*
2775 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2776 */
2777 if (fixpos && curwin->w_cursor.col > 0)
2778 --curwin->w_cursor.col;
2779
2780 return OK;
2781}
2782
2783/*
2784 * Delete "nlines" lines at the cursor.
2785 * Saves the lines for undo first if "undo" is TRUE.
2786 */
2787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002788del_lines(
2789 long nlines, /* number of lines to delete */
2790 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002793 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 if (nlines <= 0)
2796 return;
2797
2798 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002799 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 return;
2801
2802 for (n = 0; n < nlines; )
2803 {
2804 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2805 break;
2806
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002807 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 ++n;
2809
2810 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002811 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 break;
2813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002815 /* Correct the cursor position before calling deleted_lines_mark(), it may
2816 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 curwin->w_cursor.col = 0;
2818 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002819
2820 /* adjust marks, mark the buffer as changed and prepare for displaying */
2821 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822}
2823
2824 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002825gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002827 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002829 /* When searching columns is sometimes put at the end of a line. */
2830 if (pos->col == MAXCOL)
2831 return NUL;
2832 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833#ifdef FEAT_MBYTE
2834 if (has_mbyte)
2835 return (*mb_ptr2char)(ptr);
2836#endif
2837 return (int)*ptr;
2838}
2839
2840 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002841gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843#ifdef FEAT_MBYTE
2844 if (has_mbyte)
2845 return (*mb_ptr2char)(ml_get_cursor());
2846#endif
2847 return (int)*ml_get_cursor();
2848}
2849
2850/*
2851 * Write a character at the current cursor position.
2852 * It is directly written into the block.
2853 */
2854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002855pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
2857 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2858 + curwin->w_cursor.col) = c;
2859}
2860
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861/*
2862 * When extra == 0: Return TRUE if the cursor is before or on the first
2863 * non-blank in the line.
2864 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2865 * the line.
2866 */
2867 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002868inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 char_u *ptr;
2871 colnr_T col;
2872
Bram Moolenaar1c465442017-03-12 20:10:05 +01002873 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 ++ptr;
2875 if (col >= curwin->w_cursor.col + extra)
2876 return TRUE;
2877 else
2878 return FALSE;
2879}
2880
2881/*
2882 * Skip to next part of an option argument: Skip space and comma.
2883 */
2884 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002885skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 if (*p == ',')
2888 ++p;
2889 while (*p == ' ')
2890 ++p;
2891 return p;
2892}
2893
2894/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002895 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 *
2897 * Most often called through changed_bytes() and changed_lines(), which also
2898 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002899 *
2900 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
2902 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002903changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
2905#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002906 if (p_imst == IM_ON_THE_SPOT)
2907 {
2908 /* The text of the preediting area is inserted, but this doesn't
2909 * mean a change of the buffer yet. That is delayed until the
2910 * text is committed. (this means preedit becomes empty) */
2911 if (im_is_preediting() && !xim_changed_while_preediting)
2912 return;
2913 xim_changed_while_preediting = FALSE;
2914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915#endif
2916
2917 if (!curbuf->b_changed)
2918 {
2919 int save_msg_scroll = msg_scroll;
2920
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002921 /* Give a warning about changing a read-only file. This may also
2922 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002924
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 /* Create a swap file if that is wanted.
2926 * Don't do this for "nofile" and "nowrite" buffer types. */
2927 if (curbuf->b_may_swap
2928#ifdef FEAT_QUICKFIX
2929 && !bt_dontwrite(curbuf)
2930#endif
2931 )
2932 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002933 int save_need_wait_return = need_wait_return;
2934
2935 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 ml_open_file(curbuf);
2937
2938 /* The ml_open_file() can cause an ATTENTION message.
2939 * Wait two seconds, to make sure the user reads this unexpected
2940 * message. Since we could be anywhere, call wait_return() now,
2941 * and don't let the emsg() set msg_scroll. */
2942 if (need_wait_return && emsg_silent == 0)
2943 {
2944 out_flush();
2945 ui_delay(2000L, TRUE);
2946 wait_return(TRUE);
2947 msg_scroll = save_msg_scroll;
2948 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002949 else
2950 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002952 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002954 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955}
2956
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002957/*
2958 * Internal part of changed(), no user interaction.
2959 */
2960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002961changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002962{
2963 curbuf->b_changed = TRUE;
2964 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002965 check_status(curbuf);
2966 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002967#ifdef FEAT_TITLE
2968 need_maketitle = TRUE; /* set window title later */
2969#endif
2970}
2971
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002972static void changedOneline(buf_T *buf, linenr_T lnum);
2973static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2974static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976/*
2977 * Changed bytes within a single line for the current buffer.
2978 * - marks the windows on this buffer to be redisplayed
2979 * - marks the buffer changed by calling changed()
2980 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002981 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 */
2983 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002984changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002986 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002988
2989#ifdef FEAT_DIFF
2990 /* Diff highlighting in other diff windows may need to be updated too. */
2991 if (curwin->w_p_diff)
2992 {
2993 win_T *wp;
2994 linenr_T wlnum;
2995
Bram Moolenaar29323592016-07-24 22:04:11 +02002996 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002997 if (wp->w_p_diff && wp != curwin)
2998 {
2999 redraw_win_later(wp, VALID);
3000 wlnum = diff_lnum_win(lnum, wp);
3001 if (wlnum > 0)
3002 changedOneline(wp->w_buffer, wlnum);
3003 }
3004 }
3005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
3008 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003009changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003011 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
3013 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003014 if (lnum < buf->b_mod_top)
3015 buf->b_mod_top = lnum;
3016 else if (lnum >= buf->b_mod_bot)
3017 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
3019 else
3020 {
3021 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003022 buf->b_mod_set = TRUE;
3023 buf->b_mod_top = lnum;
3024 buf->b_mod_bot = lnum + 1;
3025 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
3027}
3028
3029/*
3030 * Appended "count" lines below line "lnum" in the current buffer.
3031 * Must be called AFTER the change and after mark_adjust().
3032 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3033 */
3034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003035appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
3037 changed_lines(lnum + 1, 0, lnum + 1, count);
3038}
3039
3040/*
3041 * Like appended_lines(), but adjust marks first.
3042 */
3043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003044appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003046 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003047 * be marks there. But it's still needed in diff mode. */
3048 if (lnum + count < curbuf->b_ml.ml_line_count
3049#ifdef FEAT_DIFF
3050 || curwin->w_p_diff
3051#endif
3052 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003053 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 changed_lines(lnum + 1, 0, lnum + 1, count);
3055}
3056
3057/*
3058 * Deleted "count" lines at line "lnum" in the current buffer.
3059 * Must be called AFTER the change and after mark_adjust().
3060 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3061 */
3062 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003063deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 changed_lines(lnum, 0, lnum + count, -count);
3066}
3067
3068/*
3069 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003070 * Make sure the cursor is on a valid line before calling, a GUI callback may
3071 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 */
3073 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003074deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
3076 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3077 changed_lines(lnum, 0, lnum + count, -count);
3078}
3079
3080/*
3081 * Changed lines for the current buffer.
3082 * Must be called AFTER the change and after mark_adjust().
3083 * - mark the buffer changed by calling changed()
3084 * - mark the windows on this buffer to be redisplayed
3085 * - invalidate cached values
3086 * "lnum" is the first line that needs displaying, "lnume" the first line
3087 * below the changed lines (BEFORE the change).
3088 * When only inserting lines, "lnum" and "lnume" are equal.
3089 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003090 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 */
3092 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003093changed_lines(
3094 linenr_T lnum, /* first line with change */
3095 colnr_T col, /* column in first line with change */
3096 linenr_T lnume, /* line below last changed line */
3097 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003099 changed_lines_buf(curbuf, lnum, lnume, xtra);
3100
3101#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003102 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003103 {
3104 /* When the number of lines doesn't change then mark_adjust() isn't
3105 * called and other diff buffers still need to be marked for
3106 * displaying. */
3107 win_T *wp;
3108 linenr_T wlnum;
3109
Bram Moolenaar29323592016-07-24 22:04:11 +02003110 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003111 if (wp->w_p_diff && wp != curwin)
3112 {
3113 redraw_win_later(wp, VALID);
3114 wlnum = diff_lnum_win(lnum, wp);
3115 if (wlnum > 0)
3116 changed_lines_buf(wp->w_buffer, wlnum,
3117 lnume - lnum + wlnum, 0L);
3118 }
3119 }
3120#endif
3121
3122 changed_common(lnum, col, lnume, xtra);
3123}
3124
3125 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003126changed_lines_buf(
3127 buf_T *buf,
3128 linenr_T lnum, /* first line with change */
3129 linenr_T lnume, /* line below last changed line */
3130 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003131{
3132 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
3134 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003135 if (lnum < buf->b_mod_top)
3136 buf->b_mod_top = lnum;
3137 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003140 buf->b_mod_bot += xtra;
3141 if (buf->b_mod_bot < lnum)
3142 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003144 if (lnume + xtra > buf->b_mod_bot)
3145 buf->b_mod_bot = lnume + xtra;
3146 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
3148 else
3149 {
3150 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003151 buf->b_mod_set = TRUE;
3152 buf->b_mod_top = lnum;
3153 buf->b_mod_bot = lnume + xtra;
3154 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156}
3157
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003158/*
3159 * Common code for when a change is was made.
3160 * See changed_lines() for the arguments.
3161 * Careful: may trigger autocommands that reload the buffer.
3162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003164changed_common(
3165 linenr_T lnum,
3166 colnr_T col,
3167 linenr_T lnume,
3168 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
3170 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003171 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 int i;
3173#ifdef FEAT_JUMPLIST
3174 int cols;
3175 pos_T *p;
3176 int add;
3177#endif
3178
3179 /* mark the buffer as modified */
3180 changed();
3181
Bram Moolenaare3521d92018-09-16 14:10:31 +02003182#ifdef FEAT_DIFF
3183 if (curwin->w_p_diff && diff_internal())
3184 curtab->tp_diff_update = TRUE;
3185#endif
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 /* set the '. mark */
3188 if (!cmdmod.keepjumps)
3189 {
3190 curbuf->b_last_change.lnum = lnum;
3191 curbuf->b_last_change.col = col;
3192
3193#ifdef FEAT_JUMPLIST
3194 /* Create a new entry if a new undo-able change was started or we
3195 * don't have an entry yet. */
3196 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3197 {
3198 if (curbuf->b_changelistlen == 0)
3199 add = TRUE;
3200 else
3201 {
3202 /* Don't create a new entry when the line number is the same
3203 * as the last one and the column is not too far away. Avoids
3204 * creating many entries for typing "xxxxx". */
3205 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3206 if (p->lnum != lnum)
3207 add = TRUE;
3208 else
3209 {
3210 cols = comp_textwidth(FALSE);
3211 if (cols == 0)
3212 cols = 79;
3213 add = (p->col + cols < col || col + cols < p->col);
3214 }
3215 }
3216 if (add)
3217 {
3218 /* This is the first of a new sequence of undo-able changes
3219 * and it's at some distance of the last change. Use a new
3220 * position in the changelist. */
3221 curbuf->b_new_change = FALSE;
3222
3223 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3224 {
3225 /* changelist is full: remove oldest entry */
3226 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3227 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3228 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003229 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 /* Correct position in changelist for other windows on
3232 * this buffer. */
3233 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3234 --wp->w_changelistidx;
3235 }
3236 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003237 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 /* For other windows, if the position in the changelist is
3240 * at the end it stays at the end. */
3241 if (wp->w_buffer == curbuf
3242 && wp->w_changelistidx == curbuf->b_changelistlen)
3243 ++wp->w_changelistidx;
3244 }
3245 ++curbuf->b_changelistlen;
3246 }
3247 }
3248 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3249 curbuf->b_last_change;
3250 /* The current window is always after the last change, so that "g,"
3251 * takes you back to it. */
3252 curwin->w_changelistidx = curbuf->b_changelistlen;
3253#endif
3254 }
3255
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003256 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
3258 if (wp->w_buffer == curbuf)
3259 {
3260 /* Mark this window to be redrawn later. */
3261 if (wp->w_redr_type < VALID)
3262 wp->w_redr_type = VALID;
3263
3264 /* Check if a change in the buffer has invalidated the cached
3265 * values for the cursor. */
3266#ifdef FEAT_FOLDING
3267 /*
3268 * Update the folds for this window. Can't postpone this, because
3269 * a following operator might work on the whole fold: ">>dd".
3270 */
3271 foldUpdate(wp, lnum, lnume + xtra - 1);
3272
3273 /* The change may cause lines above or below the change to become
3274 * included in a fold. Set lnum/lnume to the first/last line that
3275 * might be displayed differently.
3276 * Set w_cline_folded here as an efficient way to update it when
3277 * inserting lines just above a closed fold. */
3278 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3279 if (wp->w_cursor.lnum == lnum)
3280 wp->w_cline_folded = i;
3281 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3282 if (wp->w_cursor.lnum == lnume)
3283 wp->w_cline_folded = i;
3284
3285 /* If the changed line is in a range of previously folded lines,
3286 * compare with the first line in that range. */
3287 if (wp->w_cursor.lnum <= lnum)
3288 {
3289 i = find_wl_entry(wp, lnum);
3290 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3291 changed_line_abv_curs_win(wp);
3292 }
3293#endif
3294
3295 if (wp->w_cursor.lnum > lnum)
3296 changed_line_abv_curs_win(wp);
3297 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3298 changed_cline_bef_curs_win(wp);
3299 if (wp->w_botline >= lnum)
3300 {
3301 /* Assume that botline doesn't change (inserted lines make
3302 * other lines scroll down below botline). */
3303 approximate_botline_win(wp);
3304 }
3305
3306 /* Check if any w_lines[] entries have become invalid.
3307 * For entries below the change: Correct the lnums for
3308 * inserted/deleted lines. Makes it possible to stop displaying
3309 * after the change. */
3310 for (i = 0; i < wp->w_lines_valid; ++i)
3311 if (wp->w_lines[i].wl_valid)
3312 {
3313 if (wp->w_lines[i].wl_lnum >= lnum)
3314 {
3315 if (wp->w_lines[i].wl_lnum < lnume)
3316 {
3317 /* line included in change */
3318 wp->w_lines[i].wl_valid = FALSE;
3319 }
3320 else if (xtra != 0)
3321 {
3322 /* line below change */
3323 wp->w_lines[i].wl_lnum += xtra;
3324#ifdef FEAT_FOLDING
3325 wp->w_lines[i].wl_lastlnum += xtra;
3326#endif
3327 }
3328 }
3329#ifdef FEAT_FOLDING
3330 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3331 {
3332 /* change somewhere inside this range of folded lines,
3333 * may need to be redrawn */
3334 wp->w_lines[i].wl_valid = FALSE;
3335 }
3336#endif
3337 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003338
3339#ifdef FEAT_FOLDING
3340 /* Take care of side effects for setting w_topline when folds have
3341 * changed. Esp. when the buffer was changed in another window. */
3342 if (hasAnyFolding(wp))
3343 set_topline(wp, wp->w_topline);
3344#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003345 /* relative numbering may require updating more */
3346 if (wp->w_p_rnu)
3347 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
3349 }
3350
3351 /* Call update_screen() later, which checks out what needs to be redrawn,
3352 * since it notices b_mod_set and then uses b_mod_*. */
3353 if (must_redraw < VALID)
3354 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003355
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003356 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003357 if (lnum <= curwin->w_cursor.lnum
3358 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003359 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360}
3361
3362/*
3363 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3364 */
3365 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003366unchanged(
3367 buf_T *buf,
3368 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003370 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
3372 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003373 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (ff)
3375 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003377 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#ifdef FEAT_TITLE
3379 need_maketitle = TRUE; /* set window title later */
3380#endif
3381 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003382 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383#ifdef FEAT_NETBEANS_INTG
3384 netbeans_unmodified(buf);
3385#endif
3386}
3387
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388/*
3389 * check_status: called when the status bars for the buffer 'buf'
3390 * need to be updated
3391 */
3392 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003393check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 win_T *wp;
3396
Bram Moolenaar29323592016-07-24 22:04:11 +02003397 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (wp->w_buffer == buf && wp->w_status_height)
3399 {
3400 wp->w_redr_status = TRUE;
3401 if (must_redraw < VALID)
3402 must_redraw = VALID;
3403 }
3404}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406/*
3407 * If the file is readonly, give a warning message with the first change.
3408 * Don't do this for autocommands.
3409 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003410 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003412 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 */
3414 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003415change_warning(
3416 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 mode and 'showmode' is on */
3418{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003419 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3420
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (curbuf->b_did_warn == FALSE
3422 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 && curbuf->b_p_ro)
3425 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003426 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003428 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (!curbuf->b_p_ro)
3430 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 /*
3432 * Do what msg() does, but with a column offset if the warning should
3433 * be after the mode message.
3434 */
3435 msg_start();
3436 if (msg_row == Rows - 1)
3437 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003438 msg_source(HL_ATTR(HLF_W));
3439 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003440#ifdef FEAT_EVAL
3441 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 msg_clr_eos();
3444 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003445 if (msg_silent == 0 && !silent_mode
3446#ifdef FEAT_EVAL
3447 && time_for_testing != 1
3448#endif
3449 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
3451 out_flush();
3452 ui_delay(1000L, TRUE); /* give the user time to think about it */
3453 }
3454 curbuf->b_did_warn = TRUE;
3455 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3456 if (msg_row < Rows - 1)
3457 showmode();
3458 }
3459}
3460
3461/*
3462 * Ask for a reply from the user, a 'y' or a 'n'.
3463 * No other characters are accepted, the message is repeated until a valid
3464 * reply is entered or CTRL-C is hit.
3465 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3466 * from any buffers but directly from the user.
3467 *
3468 * return the 'y' or 'n'
3469 */
3470 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003471ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 int r = ' ';
3474 int save_State = State;
3475
3476 if (exiting) /* put terminal in raw mode for this question */
3477 settmode(TMODE_RAW);
3478 ++no_wait_return;
3479#ifdef USE_ON_FLY_SCROLL
3480 dont_scroll = TRUE; /* disallow scrolling here */
3481#endif
3482 State = CONFIRM; /* mouse behaves like with :confirm */
3483#ifdef FEAT_MOUSE
3484 setmouse(); /* disables mouse for xterm */
3485#endif
3486 ++no_mapping;
3487 ++allow_keys; /* no mapping here, but recognize keys */
3488
3489 while (r != 'y' && r != 'n')
3490 {
3491 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003492 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (direct)
3494 r = get_keystroke();
3495 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003496 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (r == Ctrl_C || r == ESC)
3498 r = 'n';
3499 msg_putchar(r); /* show what you typed */
3500 out_flush();
3501 }
3502 --no_wait_return;
3503 State = save_State;
3504#ifdef FEAT_MOUSE
3505 setmouse();
3506#endif
3507 --no_mapping;
3508 --allow_keys;
3509
3510 return r;
3511}
3512
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003513#if defined(FEAT_MOUSE) || defined(PROTO)
3514/*
3515 * Return TRUE if "c" is a mouse key.
3516 */
3517 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003518is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003519{
3520 return c == K_LEFTMOUSE
3521 || c == K_LEFTMOUSE_NM
3522 || c == K_LEFTDRAG
3523 || c == K_LEFTRELEASE
3524 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003525 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003526 || c == K_MIDDLEMOUSE
3527 || c == K_MIDDLEDRAG
3528 || c == K_MIDDLERELEASE
3529 || c == K_RIGHTMOUSE
3530 || c == K_RIGHTDRAG
3531 || c == K_RIGHTRELEASE
3532 || c == K_MOUSEDOWN
3533 || c == K_MOUSEUP
3534 || c == K_MOUSELEFT
3535 || c == K_MOUSERIGHT
3536 || c == K_X1MOUSE
3537 || c == K_X1DRAG
3538 || c == K_X1RELEASE
3539 || c == K_X2MOUSE
3540 || c == K_X2DRAG
3541 || c == K_X2RELEASE;
3542}
3543#endif
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545/*
3546 * Get a key stroke directly from the user.
3547 * Ignores mouse clicks and scrollbar events, except a click for the left
3548 * button (used at the more prompt).
3549 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3550 * Disadvantage: typeahead is ignored.
3551 * Translates the interrupt character for unix to ESC.
3552 */
3553 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003554get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003556 char_u *buf = NULL;
3557 int buflen = 150;
3558 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int len = 0;
3560 int n;
3561 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003562 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 mapped_ctrl_c = FALSE; /* mappings are not used here */
3565 for (;;)
3566 {
3567 cursor_on();
3568 out_flush();
3569
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003570 /* Leave some room for check_termcode() to insert a key code into (max
3571 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3572 * bytes. */
3573 maxlen = (buflen - 6 - len) / 3;
3574 if (buf == NULL)
3575 buf = alloc(buflen);
3576 else if (maxlen < 10)
3577 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003578 char_u *t_buf = buf;
3579
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003580 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003581 * escape sequence. */
3582 buflen += 100;
3583 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003584 if (buf == NULL)
3585 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003586 maxlen = (buflen - 6 - len) / 3;
3587 }
3588 if (buf == NULL)
3589 {
3590 do_outofmem_msg((long_u)buflen);
3591 return ESC; /* panic! */
3592 }
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003595 * terminal code to complete. */
3596 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (n > 0)
3598 {
3599 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003600 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003602 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003604 else if (len > 0)
3605 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar4395a712006-09-05 18:57:57 +00003607 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003608 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003609 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003611
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003612 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003613 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003614 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003615 {
3616 /* Redrawing was postponed, do it now. */
3617 update_screen(0);
3618 setcursor(); /* put cursor back where it belongs */
3619 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003620 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003621 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003622 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003624 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 continue;
3626
3627 /* Handle modifier and/or special key code. */
3628 n = buf[0];
3629 if (n == K_SPECIAL)
3630 {
3631 n = TO_SPECIAL(buf[1], buf[2]);
3632 if (buf[1] == KS_MODIFIER
3633 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003634#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003635 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003636#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003637#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 || n == K_VER_SCROLLBAR
3639 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640#endif
3641 )
3642 {
3643 if (buf[1] == KS_MODIFIER)
3644 mod_mask = buf[2];
3645 len -= 3;
3646 if (len > 0)
3647 mch_memmove(buf, buf + 3, (size_t)len);
3648 continue;
3649 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003650 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652#ifdef FEAT_MBYTE
3653 if (has_mbyte)
3654 {
3655 if (MB_BYTE2LEN(n) > len)
3656 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003657 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 n = (*mb_ptr2char)(buf);
3659 }
3660#endif
3661#ifdef UNIX
3662 if (n == intr_char)
3663 n = ESC;
3664#endif
3665 break;
3666 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003667 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
3669 mapped_ctrl_c = save_mapped_ctrl_c;
3670 return n;
3671}
3672
3673/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003674 * Get a number from the user.
3675 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 */
3677 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003678get_number(
3679 int colon, /* allow colon to abort */
3680 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682 int n = 0;
3683 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003684 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003686 if (mouse_used != NULL)
3687 *mouse_used = FALSE;
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 /* When not printing messages, the user won't know what to type, return a
3690 * zero (as if CR was hit). */
3691 if (msg_silent != 0)
3692 return 0;
3693
3694#ifdef USE_ON_FLY_SCROLL
3695 dont_scroll = TRUE; /* disallow scrolling here */
3696#endif
3697 ++no_mapping;
3698 ++allow_keys; /* no mapping here, but recognize keys */
3699 for (;;)
3700 {
3701 windgoto(msg_row, msg_col);
3702 c = safe_vgetc();
3703 if (VIM_ISDIGIT(c))
3704 {
3705 n = n * 10 + c - '0';
3706 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003707 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3710 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003711 if (typed > 0)
3712 {
3713 MSG_PUTS("\b \b");
3714 --typed;
3715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003718#ifdef FEAT_MOUSE
3719 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3720 {
3721 *mouse_used = TRUE;
3722 n = mouse_row + 1;
3723 break;
3724 }
3725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 else if (n == 0 && c == ':' && colon)
3727 {
3728 stuffcharReadbuff(':');
3729 if (!exmode_active)
3730 cmdline_row = msg_row;
3731 skip_redraw = TRUE; /* skip redraw once */
3732 do_redraw = FALSE;
3733 break;
3734 }
3735 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3736 break;
3737 }
3738 --no_mapping;
3739 --allow_keys;
3740 return n;
3741}
3742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743/*
3744 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003745 * When "mouse_used" is not NULL allow using the mouse and in that case return
3746 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003747 */
3748 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003749prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003750{
3751 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003752 int save_cmdline_row;
3753 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754
3755 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003756 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003757 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003758 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003759 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003760
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003761 // Set the state such that text can be selected/copied/pasted and we still
3762 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3763 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003764 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003765 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003766 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003767 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003768#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003769 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003770 setmouse();
3771#endif
3772
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003773 i = get_number(TRUE, mouse_used);
3774 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003775 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003776 /* don't call wait_return() now */
3777 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778 cmdline_row = msg_row - 1;
3779 need_wait_return = FALSE;
3780 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003781 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003783 else
3784 cmdline_row = save_cmdline_row;
3785 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003786#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003787 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003788 setmouse();
3789#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003791 return i;
3792}
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003795msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
3797 long pn;
3798
3799 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3801 return;
3802
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003803 /* We don't want to overwrite another important message, but do overwrite
3804 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3805 * then "put" reports the last action. */
3806 if (keep_msg != NULL && !keep_msg_more)
3807 return;
3808
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (n > 0)
3810 pn = n;
3811 else
3812 pn = -n;
3813
3814 if (pn > p_report)
3815 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003816 if (n > 0)
3817 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3818 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003820 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3821 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003823 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if (msg(msg_buf))
3825 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003826 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003827 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829 }
3830}
3831
3832/*
3833 * flush map and typeahead buffers and give a warning for an error
3834 */
3835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003836beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837{
3838 if (emsg_silent == 0)
3839 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003840 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003841 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843}
3844
3845/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003846 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 */
3848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003849vim_beep(
3850 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003852#ifdef FEAT_EVAL
3853 called_vim_beep = TRUE;
3854#endif
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (emsg_silent == 0)
3857 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003858 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3859 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003860#ifdef ELAPSED_FUNC
3861 static int did_init = FALSE;
3862 static ELAPSED_TYPE start_tv;
3863
3864 /* Only beep once per half a second, otherwise a sequence of beeps
3865 * would freeze Vim. */
3866 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3867 {
3868 did_init = TRUE;
3869 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003871 if (p_vb
3872#ifdef FEAT_GUI
3873 /* While the GUI is starting up the termcap is set for
3874 * the GUI but the output still goes to a terminal. */
3875 && !(gui.in_use && gui.starting)
3876#endif
3877 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003878 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003879 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003880#ifdef FEAT_VTP
3881 /* No restore color information, refresh the screen. */
3882 if (has_vtp_working() != 0
3883# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003884 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003885# endif
3886 )
3887 {
3888 redraw_later(CLEAR);
3889 update_screen(0);
3890 redrawcmd();
3891 }
3892#endif
3893 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003894 else
3895 out_char(BELL);
3896#ifdef ELAPSED_FUNC
3897 }
3898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003900
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003901 /* When 'debug' contains "beep" produce a message. If we are sourcing
3902 * a script or executing a function give the user a hint where the beep
3903 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003904 if (vim_strchr(p_debug, 'e') != NULL)
3905 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003906 msg_source(HL_ATTR(HLF_W));
3907 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910}
3911
3912/*
3913 * To get the "real" home directory:
3914 * - get value of $HOME
3915 * For Unix:
3916 * - go to that directory
3917 * - do mch_dirname() to get the real name of that directory.
3918 * This also works with mounts and links.
3919 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003920 * For Windows:
3921 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 */
3923static char_u *homedir = NULL;
3924
3925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003926init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
3928 char_u *var;
3929
Bram Moolenaar05159a02005-02-26 23:04:13 +00003930 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003931 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933#ifdef VMS
3934 var = mch_getenv((char_u *)"SYS$LOGIN");
3935#else
3936 var = mch_getenv((char_u *)"HOME");
3937#endif
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939#ifdef WIN3264
3940 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003941 * Typically, $HOME is not defined on Windows, unless the user has
3942 * specifically defined it for Vim's sake. However, on Windows NT
3943 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3944 * each user. Try constructing $HOME from these.
3945 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003946 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003947 {
3948 char_u *homedrive, *homepath;
3949
3950 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3951 homepath = mch_getenv((char_u *)"HOMEPATH");
3952 if (homepath == NULL || *homepath == NUL)
3953 homepath = (char_u *)"\\";
3954 if (homedrive != NULL
3955 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3956 {
3957 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3958 if (NameBuff[0] != NUL)
3959 var = NameBuff;
3960 }
3961 }
3962
3963 if (var == NULL)
3964 var = mch_getenv((char_u *)"USERPROFILE");
3965
3966 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * Weird but true: $HOME may contain an indirect reference to another
3968 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3969 * when $HOME is being set.
3970 */
3971 if (var != NULL && *var == '%')
3972 {
3973 char_u *p;
3974 char_u *exp;
3975
3976 p = vim_strchr(var + 1, '%');
3977 if (p != NULL)
3978 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003979 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 exp = mch_getenv(NameBuff);
3981 if (exp != NULL && *exp != NUL
3982 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3983 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003984 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 }
3988 }
3989
Bram Moolenaar48340b62017-08-29 22:08:53 +02003990 if (var != NULL && *var == NUL) /* empty is same as not set */
3991 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
Bram Moolenaar48340b62017-08-29 22:08:53 +02003993# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003994 if (enc_utf8 && var != NULL)
3995 {
3996 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003997 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003998
3999 /* Convert from active codepage to UTF-8. Other conversions are
4000 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004001 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004002 if (pp != NULL)
4003 {
4004 homedir = pp;
4005 return;
4006 }
4007 }
4008# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 /*
4011 * Default home dir is C:/
4012 * Best assumption we can make in such a situation.
4013 */
4014 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004015 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (var != NULL)
4019 {
4020#ifdef UNIX
4021 /*
4022 * Change to the directory and get the actual path. This resolves
4023 * links. Don't do it when we can't return.
4024 */
4025 if (mch_dirname(NameBuff, MAXPATHL) == OK
4026 && mch_chdir((char *)NameBuff) == 0)
4027 {
4028 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4029 var = IObuff;
4030 if (mch_chdir((char *)NameBuff) != 0)
4031 EMSG(_(e_prev_dir));
4032 }
4033#endif
4034 homedir = vim_strsave(var);
4035 }
4036}
4037
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004038#if defined(EXITFREE) || defined(PROTO)
4039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004040free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004041{
4042 vim_free(homedir);
4043}
Bram Moolenaar24305862012-08-15 14:05:05 +02004044
4045# ifdef FEAT_CMDL_COMPL
4046 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004047free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004048{
4049 ga_clear_strings(&ga_users);
4050}
4051# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004052#endif
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004055 * Call expand_env() and store the result in an allocated string.
4056 * This is not very memory efficient, this expects the result to be freed
4057 * again soon.
4058 */
4059 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004060expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004061{
4062 return expand_env_save_opt(src, FALSE);
4063}
4064
4065/*
4066 * Idem, but when "one" is TRUE handle the string as one file name, only
4067 * expand "~" at the start.
4068 */
4069 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004070expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004071{
4072 char_u *p;
4073
4074 p = alloc(MAXPATHL);
4075 if (p != NULL)
4076 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4077 return p;
4078}
4079
4080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 * Expand environment variable with path name.
4082 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004083 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * If anything fails no expansion is done and dst equals src.
4085 */
4086 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004087expand_env(
4088 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4089 char_u *dst, /* where to put the result */
4090 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004092 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093}
4094
4095 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004096expand_env_esc(
4097 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4098 char_u *dst, /* where to put the result */
4099 int dstlen, /* maximum length of the result */
4100 int esc, /* escape spaces in expanded variables */
4101 int one, /* "srcp" is one file name */
4102 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004104 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 char_u *tail;
4106 int c;
4107 char_u *var;
4108 int copy_char;
4109 int mustfree; /* var was allocated, need to free it later */
4110 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004111 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004113 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004114 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004115
4116 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 --dstlen; /* leave one char space for "\," */
4118 while (*src && dstlen > 0)
4119 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004120#ifdef FEAT_EVAL
4121 /* Skip over `=expr`. */
4122 if (src[0] == '`' && src[1] == '=')
4123 {
4124 size_t len;
4125
4126 var = src;
4127 src += 2;
4128 (void)skip_expr(&src);
4129 if (*src == '`')
4130 ++src;
4131 len = src - var;
4132 if (len > (size_t)dstlen)
4133 len = dstlen;
4134 vim_strncpy(dst, var, len);
4135 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004136 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004137 continue;
4138 }
4139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004141 if ((*src == '$'
4142#ifdef VMS
4143 && at_start
4144#endif
4145 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004146#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 || *src == '%'
4148#endif
4149 || (*src == '~' && at_start))
4150 {
4151 mustfree = FALSE;
4152
4153 /*
4154 * The variable name is copied into dst temporarily, because it may
4155 * be a string in read-only memory and a NUL needs to be appended.
4156 */
4157 if (*src != '~') /* environment var */
4158 {
4159 tail = src + 1;
4160 var = dst;
4161 c = dstlen - 1;
4162
4163#ifdef UNIX
4164 /* Unix has ${var-name} type environment vars */
4165 if (*tail == '{' && !vim_isIDc('{'))
4166 {
4167 tail++; /* ignore '{' */
4168 while (c-- > 0 && *tail && *tail != '}')
4169 *var++ = *tail++;
4170 }
4171 else
4172#endif
4173 {
4174 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004175#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 || (*src == '%' && *tail != '%')
4177#endif
4178 ))
4179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 }
4183
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004184#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185# ifdef UNIX
4186 if (src[1] == '{' && *tail != '}')
4187# else
4188 if (*src == '%' && *tail != '%')
4189# endif
4190 var = NULL;
4191 else
4192 {
4193# ifdef UNIX
4194 if (src[1] == '{')
4195# else
4196 if (*src == '%')
4197#endif
4198 ++tail;
4199#endif
4200 *var = NUL;
4201 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004202#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204#endif
4205 }
4206 /* home directory */
4207 else if ( src[1] == NUL
4208 || vim_ispathsep(src[1])
4209 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4210 {
4211 var = homedir;
4212 tail = src + 1;
4213 }
4214 else /* user directory */
4215 {
4216#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4217 /*
4218 * Copy ~user to dst[], so we can put a NUL after it.
4219 */
4220 tail = src;
4221 var = dst;
4222 c = dstlen - 1;
4223 while ( c-- > 0
4224 && *tail
4225 && vim_isfilec(*tail)
4226 && !vim_ispathsep(*tail))
4227 *var++ = *tail++;
4228 *var = NUL;
4229# ifdef UNIX
4230 /*
4231 * If the system supports getpwnam(), use it.
4232 * Otherwise, or if getpwnam() fails, the shell is used to
4233 * expand ~user. This is slower and may fail if the shell
4234 * does not support ~user (old versions of /bin/sh).
4235 */
4236# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4237 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004238 /* Note: memory allocated by getpwnam() is never freed.
4239 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004240 struct passwd *pw = (*dst == NUL)
4241 ? NULL : getpwnam((char *)dst + 1);
4242
4243 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245 if (var == NULL)
4246# endif
4247 {
4248 expand_T xpc;
4249
4250 ExpandInit(&xpc);
4251 xpc.xp_context = EXPAND_FILES;
4252 var = ExpandOne(&xpc, dst, NULL,
4253 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 mustfree = TRUE;
4255 }
4256
4257# else /* !UNIX, thus VMS */
4258 /*
4259 * USER_HOME is a comma-separated list of
4260 * directories to search for the user account in.
4261 */
4262 {
4263 char_u test[MAXPATHL], paths[MAXPATHL];
4264 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004265 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 STRCPY(paths, USER_HOME);
4268 next_path = paths;
4269 while (*next_path)
4270 {
4271 for (path = next_path; *next_path && *next_path != ',';
4272 next_path++);
4273 if (*next_path)
4274 *next_path++ = NUL;
4275 STRCPY(test, path);
4276 STRCAT(test, "/");
4277 STRCAT(test, dst + 1);
4278 if (mch_stat(test, &st) == 0)
4279 {
4280 var = alloc(STRLEN(test) + 1);
4281 STRCPY(var, test);
4282 mustfree = TRUE;
4283 break;
4284 }
4285 }
4286 }
4287# endif /* UNIX */
4288#else
4289 /* cannot expand user's home directory, so don't try */
4290 var = NULL;
4291 tail = (char_u *)""; /* for gcc */
4292#endif /* UNIX || VMS */
4293 }
4294
4295#ifdef BACKSLASH_IN_FILENAME
4296 /* If 'shellslash' is set change backslashes to forward slashes.
4297 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4298 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4299 {
4300 char_u *p = vim_strsave(var);
4301
4302 if (p != NULL)
4303 {
4304 if (mustfree)
4305 vim_free(var);
4306 var = p;
4307 mustfree = TRUE;
4308 forward_slash(var);
4309 }
4310 }
4311#endif
4312
4313 /* If "var" contains white space, escape it with a backslash.
4314 * Required for ":e ~/tt" when $HOME includes a space. */
4315 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4316 {
4317 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4318
4319 if (p != NULL)
4320 {
4321 if (mustfree)
4322 vim_free(var);
4323 var = p;
4324 mustfree = TRUE;
4325 }
4326 }
4327
4328 if (var != NULL && *var != NUL
4329 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4330 {
4331 STRCPY(dst, var);
4332 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004333 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /* if var[] ends in a path separator and tail[] starts
4335 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004336 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4338 && dst[-1] != ':'
4339#endif
4340 && vim_ispathsep(*tail))
4341 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004342 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 src = tail;
4344 copy_char = FALSE;
4345 }
4346 if (mustfree)
4347 vim_free(var);
4348 }
4349
4350 if (copy_char) /* copy at least one char */
4351 {
4352 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004353 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004354 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4355 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 */
4357 at_start = FALSE;
4358 if (src[0] == '\\' && src[1] != NUL)
4359 {
4360 *dst++ = *src++;
4361 --dstlen;
4362 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004363 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004365 if (dstlen > 0)
4366 {
4367 *dst++ = *src++;
4368 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004369
Bram Moolenaar1c864092017-08-06 18:15:45 +02004370 if (startstr != NULL && src - startstr_len >= srcp
4371 && STRNCMP(src - startstr_len, startstr,
4372 startstr_len) == 0)
4373 at_start = TRUE;
4374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004376
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
4378 *dst = NUL;
4379}
4380
4381/*
4382 * Vim's version of getenv().
4383 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004384 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004385 * "mustfree" is set to TRUE when returned is allocated, it must be
4386 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 */
4388 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004389vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
4391 char_u *p;
4392 char_u *pend;
4393 int vimruntime;
4394
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004395#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 /* use "C:/" when $HOME is not set */
4397 if (STRCMP(name, "HOME") == 0)
4398 return homedir;
4399#endif
4400
4401 p = mch_getenv(name);
4402 if (p != NULL && *p == NUL) /* empty is the same as not set */
4403 p = NULL;
4404
4405 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004406 {
4407#if defined(FEAT_MBYTE) && defined(WIN3264)
4408 if (enc_utf8)
4409 {
4410 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004411 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004412
4413 /* Convert from active codepage to UTF-8. Other conversions are
4414 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004415 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004416 if (pp != NULL)
4417 {
4418 p = pp;
4419 *mustfree = TRUE;
4420 }
4421 }
4422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4427 if (!vimruntime && STRCMP(name, "VIM") != 0)
4428 return NULL;
4429
4430 /*
4431 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4432 * Don't do this when default_vimruntime_dir is non-empty.
4433 */
4434 if (vimruntime
4435#ifdef HAVE_PATHDEF
4436 && *default_vimruntime_dir == NUL
4437#endif
4438 )
4439 {
4440 p = mch_getenv((char_u *)"VIM");
4441 if (p != NULL && *p == NUL) /* empty is the same as not set */
4442 p = NULL;
4443 if (p != NULL)
4444 {
4445 p = vim_version_dir(p);
4446 if (p != NULL)
4447 *mustfree = TRUE;
4448 else
4449 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004450
4451#if defined(FEAT_MBYTE) && defined(WIN3264)
4452 if (enc_utf8)
4453 {
4454 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004455 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004456
4457 /* Convert from active codepage to UTF-8. Other conversions
4458 * are not done, because they would fail for non-ASCII
4459 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004460 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004461 if (pp != NULL)
4462 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004463 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004464 vim_free(p);
4465 p = pp;
4466 *mustfree = TRUE;
4467 }
4468 }
4469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 }
4472
4473 /*
4474 * When expanding $VIM or $VIMRUNTIME fails, try using:
4475 * - the directory name from 'helpfile' (unless it contains '$')
4476 * - the executable name from argv[0]
4477 */
4478 if (p == NULL)
4479 {
4480 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4481 p = p_hf;
4482#ifdef USE_EXE_NAME
4483 /*
4484 * Use the name of the executable, obtained from argv[0].
4485 */
4486 else
4487 p = exe_name;
4488#endif
4489 if (p != NULL)
4490 {
4491 /* remove the file name */
4492 pend = gettail(p);
4493
4494 /* remove "doc/" from 'helpfile', if present */
4495 if (p == p_hf)
4496 pend = remove_tail(p, pend, (char_u *)"doc");
4497
4498#ifdef USE_EXE_NAME
4499# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004500 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (p == exe_name)
4502 {
4503 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004504 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004506 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4507 if (pend1 != pend)
4508 {
4509 pnew = alloc((unsigned)(pend1 - p) + 15);
4510 if (pnew != NULL)
4511 {
4512 STRNCPY(pnew, p, (pend1 - p));
4513 STRCPY(pnew + (pend1 - p), "Resources/vim");
4514 p = pnew;
4515 pend = p + STRLEN(p);
4516 }
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519# endif
4520 /* remove "src/" from exe_name, if present */
4521 if (p == exe_name)
4522 pend = remove_tail(p, pend, (char_u *)"src");
4523#endif
4524
4525 /* for $VIM, remove "runtime/" or "vim54/", if present */
4526 if (!vimruntime)
4527 {
4528 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4529 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4530 }
4531
4532 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004533 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004536#ifdef MACOS_X
4537 if (p == exe_name || p == p_hf)
4538#endif
4539 /* check that the result is a directory name */
4540 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
4542 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004543 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 else
4545 {
4546#ifdef USE_EXE_NAME
4547 /* may add "/vim54" or "/runtime" if it exists */
4548 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4549 {
4550 vim_free(p);
4551 p = pend;
4552 }
4553#endif
4554 *mustfree = TRUE;
4555 }
4556 }
4557 }
4558
4559#ifdef HAVE_PATHDEF
4560 /* When there is a pathdef.c file we can use default_vim_dir and
4561 * default_vimruntime_dir */
4562 if (p == NULL)
4563 {
4564 /* Only use default_vimruntime_dir when it is not empty */
4565 if (vimruntime && *default_vimruntime_dir != NUL)
4566 {
4567 p = default_vimruntime_dir;
4568 *mustfree = FALSE;
4569 }
4570 else if (*default_vim_dir != NUL)
4571 {
4572 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4573 *mustfree = TRUE;
4574 else
4575 {
4576 p = default_vim_dir;
4577 *mustfree = FALSE;
4578 }
4579 }
4580 }
4581#endif
4582
4583 /*
4584 * Set the environment variable, so that the new value can be found fast
4585 * next time, and others can also use it (e.g. Perl).
4586 */
4587 if (p != NULL)
4588 {
4589 if (vimruntime)
4590 {
4591 vim_setenv((char_u *)"VIMRUNTIME", p);
4592 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594 else
4595 {
4596 vim_setenv((char_u *)"VIM", p);
4597 didset_vim = TRUE;
4598 }
4599 }
4600 return p;
4601}
4602
4603/*
4604 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4605 * Return NULL if not, return its name in allocated memory otherwise.
4606 */
4607 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004608vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 char_u *p;
4611
4612 if (vimdir == NULL || *vimdir == NUL)
4613 return NULL;
4614 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4615 if (p != NULL && mch_isdir(p))
4616 return p;
4617 vim_free(p);
4618 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4619 if (p != NULL && mch_isdir(p))
4620 return p;
4621 vim_free(p);
4622 return NULL;
4623}
4624
4625/*
4626 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4627 * the length of "name/". Otherwise return "pend".
4628 */
4629 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004630remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
4632 int len = (int)STRLEN(name) + 1;
4633 char_u *newend = pend - len;
4634
4635 if (newend >= p
4636 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004637 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 return newend;
4639 return pend;
4640}
4641
Bram Moolenaar137374f2018-05-13 15:59:50 +02004642 void
4643vim_unsetenv(char_u *var)
4644{
4645#ifdef HAVE_UNSETENV
4646 unsetenv((char *)var);
4647#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004648 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004649#endif
4650}
4651
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 * Our portable version of setenv.
4655 */
4656 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004657vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
4659#ifdef HAVE_SETENV
4660 mch_setenv((char *)name, (char *)val, 1);
4661#else
4662 char_u *envbuf;
4663
4664 /*
4665 * Putenv does not copy the string, it has to remain
4666 * valid. The allocated memory will never be freed.
4667 */
4668 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4669 if (envbuf != NULL)
4670 {
4671 sprintf((char *)envbuf, "%s=%s", name, val);
4672 putenv((char *)envbuf);
4673 }
4674#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004675#ifdef FEAT_GETTEXT
4676 /*
4677 * When setting $VIMRUNTIME adjust the directory to find message
4678 * translations to $VIMRUNTIME/lang.
4679 */
4680 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4681 {
4682 char_u *buf = concat_str(val, (char_u *)"/lang");
4683
4684 if (buf != NULL)
4685 {
4686 bindtextdomain(VIMPACKAGE, (char *)buf);
4687 vim_free(buf);
4688 }
4689 }
4690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691}
4692
4693#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4694/*
4695 * Function given to ExpandGeneric() to obtain an environment variable name.
4696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004698get_env_name(
4699 expand_T *xp UNUSED,
4700 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
Bram Moolenaard0573012017-10-28 21:11:06 +02004702# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004704 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 */
4706 return NULL;
4707# else
4708# ifndef __WIN32__
4709 /* Borland C++ 5.2 has this in a header file. */
4710 extern char **environ;
4711# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004712# define ENVNAMELEN 100
4713 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 char_u *str;
4715 int n;
4716
4717 str = (char_u *)environ[idx];
4718 if (str == NULL)
4719 return NULL;
4720
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004721 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
4723 if (str[n] == '=' || str[n] == NUL)
4724 break;
4725 name[n] = str[n];
4726 }
4727 name[n] = NUL;
4728 return name;
4729# endif
4730}
Bram Moolenaar24305862012-08-15 14:05:05 +02004731
4732/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004733 * Add a user name to the list of users in ga_users.
4734 * Do nothing if user name is NULL or empty.
4735 */
4736 static void
4737add_user(char_u *user, int need_copy)
4738{
4739 char_u *user_copy = (user != NULL && need_copy)
4740 ? vim_strsave(user) : user;
4741
4742 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4743 {
4744 if (need_copy)
4745 vim_free(user);
4746 return;
4747 }
4748 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4749}
4750
4751/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004752 * Find all user names for user completion.
4753 * Done only once and then cached.
4754 */
4755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004756init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004757{
Bram Moolenaar24305862012-08-15 14:05:05 +02004758 static int lazy_init_done = FALSE;
4759
4760 if (lazy_init_done)
4761 return;
4762
4763 lazy_init_done = TRUE;
4764 ga_init2(&ga_users, sizeof(char_u *), 20);
4765
4766# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4767 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004768 struct passwd* pw;
4769
4770 setpwent();
4771 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004772 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004773 endpwent();
4774 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004775# elif defined(WIN3264)
4776 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004777 DWORD nusers = 0, ntotal = 0, i;
4778 PUSER_INFO_0 uinfo;
4779
4780 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4781 &nusers, &ntotal, NULL) == NERR_Success)
4782 {
4783 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004784 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004785
4786 NetApiBufferFree(uinfo);
4787 }
4788 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004789# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004790# if defined(HAVE_GETPWNAM)
4791 {
4792 char_u *user_env = mch_getenv((char_u *)"USER");
4793
4794 // The $USER environment variable may be a valid remote user name (NIS,
4795 // LDAP) not already listed by getpwent(), as getpwent() only lists
4796 // local user names. If $USER is not already listed, check whether it
4797 // is a valid remote user name using getpwnam() and if it is, add it to
4798 // the list of user names.
4799
4800 if (user_env != NULL && *user_env != NUL)
4801 {
4802 int i;
4803
4804 for (i = 0; i < ga_users.ga_len; i++)
4805 {
4806 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4807
4808 if (STRCMP(local_user, user_env) == 0)
4809 break;
4810 }
4811
4812 if (i == ga_users.ga_len)
4813 {
4814 struct passwd *pw = getpwnam((char *)user_env);
4815
4816 if (pw != NULL)
4817 add_user((char_u *)pw->pw_name, TRUE);
4818 }
4819 }
4820 }
4821# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004822}
4823
4824/*
4825 * Function given to ExpandGeneric() to obtain an user names.
4826 */
4827 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004828get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004829{
4830 init_users();
4831 if (idx < ga_users.ga_len)
4832 return ((char_u **)ga_users.ga_data)[idx];
4833 return NULL;
4834}
4835
4836/*
4837 * Check whether name matches a user name. Return:
4838 * 0 if name does not match any user name.
4839 * 1 if name partially matches the beginning of a user name.
4840 * 2 is name fully matches a user name.
4841 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004842 int
4843match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004844{
4845 int i;
4846 int n = (int)STRLEN(name);
4847 int result = 0;
4848
4849 init_users();
4850 for (i = 0; i < ga_users.ga_len; i++)
4851 {
4852 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4853 return 2; /* full match */
4854 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4855 result = 1; /* partial match */
4856 }
4857 return result;
4858}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859#endif
4860
4861/*
4862 * Replace home directory by "~" in each space or comma separated file name in
4863 * 'src'.
4864 * If anything fails (except when out of space) dst equals src.
4865 */
4866 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004867home_replace(
4868 buf_T *buf, /* when not NULL, check for help files */
4869 char_u *src, /* input file name */
4870 char_u *dst, /* where to put the result */
4871 int dstlen, /* maximum length of the result */
4872 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 spaces and commas in the file name. */
4874{
4875 size_t dirlen = 0, envlen = 0;
4876 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004877 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 char_u *p;
4879
4880 if (src == NULL)
4881 {
4882 *dst = NUL;
4883 return;
4884 }
4885
4886 /*
4887 * If the file is a help file, remove the path completely.
4888 */
4889 if (buf != NULL && buf->b_help)
4890 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004891 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 return;
4893 }
4894
4895 /*
4896 * We check both the value of the $HOME environment variable and the
4897 * "real" home directory.
4898 */
4899 if (homedir != NULL)
4900 dirlen = STRLEN(homedir);
4901
4902#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004903 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004905 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4906#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004907#ifdef WIN3264
4908 if (homedir_env == NULL)
4909 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4910#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004911 /* Empty is the same as not set. */
4912 if (homedir_env != NULL && *homedir_env == NUL)
4913 homedir_env = NULL;
4914
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004915#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004916 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004917 {
4918 int usedlen = 0;
4919 int flen;
4920 char_u *fbuf = NULL;
4921
4922 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004923 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004924 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004925 flen = (int)STRLEN(homedir_env);
4926 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4927 /* Remove the trailing / that is added to a directory. */
4928 homedir_env[flen - 1] = NUL;
4929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930#endif
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (homedir_env != NULL)
4933 envlen = STRLEN(homedir_env);
4934
4935 if (!one)
4936 src = skipwhite(src);
4937 while (*src && dstlen > 0)
4938 {
4939 /*
4940 * Here we are at the beginning of a file name.
4941 * First, check to see if the beginning of the file name matches
4942 * $HOME or the "real" home directory. Check that there is a '/'
4943 * after the match (so that if e.g. the file is "/home/pieter/bla",
4944 * and the home directory is "/home/piet", the file does not end up
4945 * as "~er/bla" (which would seem to indicate the file "bla" in user
4946 * er's home directory)).
4947 */
4948 p = homedir;
4949 len = dirlen;
4950 for (;;)
4951 {
4952 if ( len
4953 && fnamencmp(src, p, len) == 0
4954 && (vim_ispathsep(src[len])
4955 || (!one && (src[len] == ',' || src[len] == ' '))
4956 || src[len] == NUL))
4957 {
4958 src += len;
4959 if (--dstlen > 0)
4960 *dst++ = '~';
4961
4962 /*
4963 * If it's just the home directory, add "/".
4964 */
4965 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4966 *dst++ = '/';
4967 break;
4968 }
4969 if (p == homedir_env)
4970 break;
4971 p = homedir_env;
4972 len = envlen;
4973 }
4974
4975 /* if (!one) skip to separator: space or comma */
4976 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4977 *dst++ = *src++;
4978 /* skip separator */
4979 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4980 *dst++ = *src++;
4981 }
4982 /* if (dstlen == 0) out of space, what to do??? */
4983
4984 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004985
4986 if (homedir_env != homedir_env_orig)
4987 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988}
4989
4990/*
4991 * Like home_replace, store the replaced string in allocated memory.
4992 * When something fails, NULL is returned.
4993 */
4994 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004995home_replace_save(
4996 buf_T *buf, /* when not NULL, check for help files */
4997 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
4999 char_u *dst;
5000 unsigned len;
5001
5002 len = 3; /* space for "~/" and trailing NUL */
5003 if (src != NULL) /* just in case */
5004 len += (unsigned)STRLEN(src);
5005 dst = alloc(len);
5006 if (dst != NULL)
5007 home_replace(buf, src, dst, len, TRUE);
5008 return dst;
5009}
5010
5011/*
5012 * Compare two file names and return:
5013 * FPC_SAME if they both exist and are the same file.
5014 * FPC_SAMEX if they both don't exist and have the same file name.
5015 * FPC_DIFF if they both exist and are different files.
5016 * FPC_NOTX if they both don't exist.
5017 * FPC_DIFFX if one of them doesn't exist.
5018 * For the first name environment variables are expanded
5019 */
5020 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005021fullpathcmp(
5022 char_u *s1,
5023 char_u *s2,
5024 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
5026#ifdef UNIX
5027 char_u exp1[MAXPATHL];
5028 char_u full1[MAXPATHL];
5029 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005030 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 int r1, r2;
5032
5033 expand_env(s1, exp1, MAXPATHL);
5034 r1 = mch_stat((char *)exp1, &st1);
5035 r2 = mch_stat((char *)s2, &st2);
5036 if (r1 != 0 && r2 != 0)
5037 {
5038 /* if mch_stat() doesn't work, may compare the names */
5039 if (checkname)
5040 {
5041 if (fnamecmp(exp1, s2) == 0)
5042 return FPC_SAMEX;
5043 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5044 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5045 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5046 return FPC_SAMEX;
5047 }
5048 return FPC_NOTX;
5049 }
5050 if (r1 != 0 || r2 != 0)
5051 return FPC_DIFFX;
5052 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5053 return FPC_SAME;
5054 return FPC_DIFF;
5055#else
5056 char_u *exp1; /* expanded s1 */
5057 char_u *full1; /* full path of s1 */
5058 char_u *full2; /* full path of s2 */
5059 int retval = FPC_DIFF;
5060 int r1, r2;
5061
5062 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5063 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5064 {
5065 full1 = exp1 + MAXPATHL;
5066 full2 = full1 + MAXPATHL;
5067
5068 expand_env(s1, exp1, MAXPATHL);
5069 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5070 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5071
5072 /* If vim_FullName() fails, the file probably doesn't exist. */
5073 if (r1 != OK && r2 != OK)
5074 {
5075 if (checkname && fnamecmp(exp1, s2) == 0)
5076 retval = FPC_SAMEX;
5077 else
5078 retval = FPC_NOTX;
5079 }
5080 else if (r1 != OK || r2 != OK)
5081 retval = FPC_DIFFX;
5082 else if (fnamecmp(full1, full2))
5083 retval = FPC_DIFF;
5084 else
5085 retval = FPC_SAME;
5086 vim_free(exp1);
5087 }
5088 return retval;
5089#endif
5090}
5091
5092/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005093 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005094 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005095 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 */
5097 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005098gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
5100 char_u *p1, *p2;
5101
5102 if (fname == NULL)
5103 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005104 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005106 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005108 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110 return p1;
5111}
5112
Bram Moolenaar31710262010-08-13 13:36:15 +02005113#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005114/*
5115 * Return the end of the directory name, on the first path
5116 * separator:
5117 * "/path/file", "/path/dir/", "/path//dir", "/file"
5118 * ^ ^ ^ ^
5119 */
5120 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005121gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005122{
5123 char_u *dir_end = fname;
5124 char_u *next_dir_end = fname;
5125 int look_for_sep = TRUE;
5126 char_u *p;
5127
5128 for (p = fname; *p != NUL; )
5129 {
5130 if (vim_ispathsep(*p))
5131 {
5132 if (look_for_sep)
5133 {
5134 next_dir_end = p;
5135 look_for_sep = FALSE;
5136 }
5137 }
5138 else
5139 {
5140 if (!look_for_sep)
5141 dir_end = next_dir_end;
5142 look_for_sep = TRUE;
5143 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005144 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005145 }
5146 return dir_end;
5147}
5148#endif
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005151 * Get pointer to tail of "fname", including path separators. Putting a NUL
5152 * here leaves the directory name. Takes care of "c:/" and "//".
5153 * Always returns a valid pointer.
5154 */
5155 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005156gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005157{
5158 char_u *p;
5159 char_u *t;
5160
5161 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5162 t = gettail(fname);
5163 while (t > p && after_pathsep(fname, t))
5164 --t;
5165#ifdef VMS
5166 /* path separator is part of the path */
5167 ++t;
5168#endif
5169 return t;
5170}
5171
5172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 * get the next path component (just after the next path separator).
5174 */
5175 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005176getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177{
5178 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005179 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (*fname)
5181 ++fname;
5182 return fname;
5183}
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185/*
5186 * Get a pointer to one character past the head of a path name.
5187 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5188 * If there is no head, path is returned.
5189 */
5190 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
5193 char_u *retval;
5194
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005195#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 /* may skip "c:" */
5197 if (isalpha(path[0]) && path[1] == ':')
5198 retval = path + 2;
5199 else
5200 retval = path;
5201#else
5202# if defined(AMIGA)
5203 /* may skip "label:" */
5204 retval = vim_strchr(path, ':');
5205 if (retval == NULL)
5206 retval = path;
5207# else /* Unix */
5208 retval = path;
5209# endif
5210#endif
5211
5212 while (vim_ispathsep(*retval))
5213 ++retval;
5214
5215 return retval;
5216}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005219 * Return TRUE if 'c' is a path separator.
5220 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 */
5222 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005225#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005227#else
5228# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005230# else
5231# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5233 return (c == ':' || c == '[' || c == ']' || c == '/'
5234 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005235# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005237# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240}
5241
Bram Moolenaar69c35002013-11-04 02:54:12 +01005242/*
5243 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5244 */
5245 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005246vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005247{
5248 return vim_ispathsep(c)
5249#ifdef BACKSLASH_IN_FILENAME
5250 && c != ':'
5251#endif
5252 ;
5253}
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5256/*
5257 * return TRUE if 'c' is a path list separator.
5258 */
5259 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005260vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262#ifdef UNIX
5263 return (c == ':');
5264#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005265 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#endif
5267}
5268#endif
5269
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005270/*
5271 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5272 * It's done in-place.
5273 */
5274 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005275shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005276{
5277 char_u *tail, *s, *d;
5278 int skip = FALSE;
5279
5280 tail = gettail(str);
5281 d = str;
5282 for (s = str; ; ++s)
5283 {
5284 if (s >= tail) /* copy the whole tail */
5285 {
5286 *d++ = *s;
5287 if (*s == NUL)
5288 break;
5289 }
5290 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5291 {
5292 *d++ = *s;
5293 skip = FALSE;
5294 }
5295 else if (!skip)
5296 {
5297 *d++ = *s; /* copy next char */
5298 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5299 skip = TRUE;
5300# ifdef FEAT_MBYTE
5301 if (has_mbyte)
5302 {
5303 int l = mb_ptr2len(s);
5304
5305 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005306 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005307 }
5308# endif
5309 }
5310 }
5311}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005312
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005313/*
5314 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5315 * Also returns TRUE if there is no directory name.
5316 * "fname" must be writable!.
5317 */
5318 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005319dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005320{
5321 char_u *p;
5322 int c;
5323 int retval;
5324
5325 p = gettail_sep(fname);
5326 if (p == fname)
5327 return TRUE;
5328 c = *p;
5329 *p = NUL;
5330 retval = mch_isdir(fname);
5331 *p = c;
5332 return retval;
5333}
5334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005336 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5337 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 */
5339 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005340vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005342#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005344#else
5345 if (p_fic)
5346 return MB_STRICMP(x, y);
5347 return STRCMP(x, y);
5348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349}
5350
5351 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005352vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005354#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005355 char_u *px = x;
5356 char_u *py = y;
5357 int cx = NUL;
5358 int cy = NUL;
5359
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005360 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005362 cx = PTR2CHAR(px);
5363 cy = PTR2CHAR(py);
5364 if (cx == NUL || cy == NUL
5365 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5366 && !(cx == '/' && cy == '\\')
5367 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005369 len -= MB_PTR2LEN(px);
5370 px += MB_PTR2LEN(px);
5371 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373 if (len == 0)
5374 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005375 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005376#else
5377 if (p_fic)
5378 return MB_STRNICMP(x, y, len);
5379 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383/*
5384 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005385 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 */
5387 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005388concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
5390 char_u *dest;
5391
5392 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5393 if (dest != NULL)
5394 {
5395 STRCPY(dest, fname1);
5396 if (sep)
5397 add_pathsep(dest);
5398 STRCAT(dest, fname2);
5399 }
5400 return dest;
5401}
5402
Bram Moolenaard6754642005-01-17 22:18:45 +00005403/*
5404 * Concatenate two strings and return the result in allocated memory.
5405 * Returns NULL when out of memory.
5406 */
5407 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005408concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005409{
5410 char_u *dest;
5411 size_t l = STRLEN(str1);
5412
5413 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5414 if (dest != NULL)
5415 {
5416 STRCPY(dest, str1);
5417 STRCPY(dest + l, str2);
5418 }
5419 return dest;
5420}
Bram Moolenaard6754642005-01-17 22:18:45 +00005421
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422/*
5423 * Add a path separator to a file name, unless it already ends in a path
5424 * separator.
5425 */
5426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005427add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005429 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 STRCAT(p, PATHSEPSTR);
5431}
5432
5433/*
5434 * FullName_save - Make an allocated copy of a full file name.
5435 * Returns NULL when out of memory.
5436 */
5437 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005438FullName_save(
5439 char_u *fname,
5440 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005441 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 char_u *buf;
5444 char_u *new_fname = NULL;
5445
5446 if (fname == NULL)
5447 return NULL;
5448
5449 buf = alloc((unsigned)MAXPATHL);
5450 if (buf != NULL)
5451 {
5452 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5453 new_fname = vim_strsave(buf);
5454 else
5455 new_fname = vim_strsave(fname);
5456 vim_free(buf);
5457 }
5458 return new_fname;
5459}
5460
5461#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5462
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005463static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005464static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
5466/*
5467 * Find the start of a comment, not knowing if we are in a comment right now.
5468 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005469 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005471 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005472ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005473{
5474 return find_start_comment(curbuf->b_ind_maxcomment);
5475}
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005478find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 pos_T *pos;
5481 char_u *line;
5482 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005483 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005485 for (;;)
5486 {
5487 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5488 if (pos == NULL)
5489 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005491 /*
5492 * Check if the comment start we found is inside a string.
5493 * If it is then restrict the search to below this line and try again.
5494 */
5495 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005496 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005497 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005498 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005499 break;
5500 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5501 if (cur_maxcomment <= 0)
5502 {
5503 pos = NULL;
5504 break;
5505 }
5506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 return pos;
5508}
5509
5510/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005511 * Find the start of a comment or raw string, not knowing if we are in a
5512 * comment or raw string right now.
5513 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005514 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005515 * Return NULL when not inside a comment or raw string.
5516 * "CORS" -> Comment Or Raw String
5517 */
5518 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005519ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005520{
Bram Moolenaar089af182015-10-07 11:41:49 +02005521 static pos_T comment_pos_copy;
5522 pos_T *comment_pos;
5523 pos_T *rs_pos;
5524
5525 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5526 if (comment_pos != NULL)
5527 {
5528 /* Need to make a copy of the static pos in findmatchlimit(),
5529 * calling find_start_rawstring() may change it. */
5530 comment_pos_copy = *comment_pos;
5531 comment_pos = &comment_pos_copy;
5532 }
5533 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005534
5535 /* If comment_pos is before rs_pos the raw string is inside the comment.
5536 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005537 if (comment_pos == NULL || (rs_pos != NULL
5538 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005539 {
5540 if (is_raw != NULL && rs_pos != NULL)
5541 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005542 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005543 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005544 return comment_pos;
5545}
5546
5547/*
5548 * Find the start of a raw string, not knowing if we are in one right now.
5549 * Search starts at w_cursor.lnum and goes backwards.
5550 * Return NULL when not inside a raw string.
5551 */
5552 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005553find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005554{
5555 pos_T *pos;
5556 char_u *line;
5557 char_u *p;
5558 int cur_maxcomment = ind_maxcomment;
5559
5560 for (;;)
5561 {
5562 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5563 if (pos == NULL)
5564 break;
5565
5566 /*
5567 * Check if the raw string start we found is inside a string.
5568 * If it is then restrict the search to below this line and try again.
5569 */
5570 line = ml_get(pos->lnum);
5571 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5572 p = skip_string(p);
5573 if ((colnr_T)(p - line) <= pos->col)
5574 break;
5575 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5576 if (cur_maxcomment <= 0)
5577 {
5578 pos = NULL;
5579 break;
5580 }
5581 }
5582 return pos;
5583}
5584
5585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 * Skip to the end of a "string" and a 'c' character.
5587 * If there is no string or character, return argument unmodified.
5588 */
5589 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005590skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 int i;
5593
5594 /*
5595 * We loop, because strings may be concatenated: "date""time".
5596 */
5597 for ( ; ; ++p)
5598 {
5599 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5600 {
5601 if (!p[1]) /* ' at end of line */
5602 break;
5603 i = 2;
5604 if (p[1] == '\\') /* '\n' or '\000' */
5605 {
5606 ++i;
5607 while (vim_isdigit(p[i - 1])) /* '\000' */
5608 ++i;
5609 }
5610 if (p[i] == '\'') /* check for trailing ' */
5611 {
5612 p += i;
5613 continue;
5614 }
5615 }
5616 else if (p[0] == '"') /* start of string */
5617 {
5618 for (++p; p[0]; ++p)
5619 {
5620 if (p[0] == '\\' && p[1] != NUL)
5621 ++p;
5622 else if (p[0] == '"') /* end of string */
5623 break;
5624 }
5625 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005626 continue; /* continue for another string */
5627 }
5628 else if (p[0] == 'R' && p[1] == '"')
5629 {
5630 /* Raw string: R"[delim](...)[delim]" */
5631 char_u *delim = p + 2;
5632 char_u *paren = vim_strchr(delim, '(');
5633
5634 if (paren != NULL)
5635 {
5636 size_t delim_len = paren - delim;
5637
5638 for (p += 3; *p; ++p)
5639 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5640 && p[delim_len + 1] == '"')
5641 {
5642 p += delim_len + 1;
5643 break;
5644 }
5645 if (p[0] == '"')
5646 continue; /* continue for another string */
5647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649 break; /* no string found */
5650 }
5651 if (!*p)
5652 --p; /* backup from NUL */
5653 return p;
5654}
5655#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5656
5657#if defined(FEAT_CINDENT) || defined(PROTO)
5658
5659/*
5660 * Do C or expression indenting on the current line.
5661 */
5662 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005663do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
5665# ifdef FEAT_EVAL
5666 if (*curbuf->b_p_inde != NUL)
5667 fixthisline(get_expr_indent);
5668 else
5669# endif
5670 fixthisline(get_c_indent);
5671}
5672
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005673/* Find result cache for cpp_baseclass */
5674typedef struct {
5675 int found;
5676 lpos_T lpos;
5677} cpp_baseclass_cache_T;
5678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679/*
5680 * Functions for C-indenting.
5681 * Most of this originally comes from Eric Fischer.
5682 */
5683/*
5684 * Below "XXX" means that this function may unlock the current line.
5685 */
5686
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005687static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005688static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005689static int cin_iscomment(char_u *);
5690static int cin_islinecomment(char_u *);
5691static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005692static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005693static int cin_ends_in(char_u *, char_u *, char_u *);
5694static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005695static pos_T *find_match_paren(int);
5696static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005697static int find_last_paren(char_u *l, int start, int end);
5698static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700/*
5701 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005702 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
5704 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005705cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
5707 while (*s)
5708 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005709 char_u *prev_s = s;
5710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005712
5713 /* Perl/shell # comment comment continues until eol. Require a space
5714 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005715 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005716 {
5717 s += STRLEN(s);
5718 break;
5719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (*s != '/')
5721 break;
5722 ++s;
5723 if (*s == '/') /* slash-slash comment continues till eol */
5724 {
5725 s += STRLEN(s);
5726 break;
5727 }
5728 if (*s != '*')
5729 break;
5730 for (++s; *s; ++s) /* skip slash-star comment */
5731 if (s[0] == '*' && s[1] == '/')
5732 {
5733 s += 2;
5734 break;
5735 }
5736 }
5737 return s;
5738}
5739
5740/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005741 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 * not considered code.
5743 */
5744 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005745cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
5747 return *cin_skipcomment(s) == NUL;
5748}
5749
5750/*
5751 * Check previous lines for a "//" line comment, skipping over blank lines.
5752 */
5753 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005754find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 static pos_T pos;
5757 char_u *line;
5758 char_u *p;
5759
5760 pos = curwin->w_cursor;
5761 while (--pos.lnum > 0)
5762 {
5763 line = ml_get(pos.lnum);
5764 p = skipwhite(line);
5765 if (cin_islinecomment(p))
5766 {
5767 pos.col = (int)(p - line);
5768 return &pos;
5769 }
5770 if (*p != NUL)
5771 break;
5772 }
5773 return NULL;
5774}
5775
5776/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005777 * Return TRUE if "text" starts with "key:".
5778 */
5779 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005780cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005781{
5782 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005783 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005784
5785 if (*s == '\'' || *s == '"')
5786 {
5787 /* can be 'key': or "key": */
5788 quote = *s;
5789 ++s;
5790 }
5791 if (!vim_isIDc(*s)) /* need at least one ID character */
5792 return FALSE;
5793
5794 while (vim_isIDc(*s))
5795 ++s;
5796 if (*s == quote)
5797 ++s;
5798
5799 s = cin_skipcomment(s);
5800
5801 /* "::" is not a label, it's C++ */
5802 return (*s == ':' && s[1] != ':');
5803}
5804
5805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005807 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 */
5809 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005810cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 if (!vim_isIDc(**s)) /* need at least one ID character */
5813 return FALSE;
5814
5815 while (vim_isIDc(**s))
5816 (*s)++;
5817
5818 *s = cin_skipcomment(*s);
5819
5820 /* "::" is not a label, it's C++ */
5821 return (**s == ':' && *++*s != ':');
5822}
5823
5824/*
5825 * Recognize a label: "label:".
5826 * Note: curwin->w_cursor must be where we are looking for the label.
5827 */
5828 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005829cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 char_u *s;
5832
5833 s = cin_skipcomment(ml_get_curline());
5834
5835 /*
5836 * Exclude "default" from labels, since it should be indented
5837 * like a switch label. Same for C++ scope declarations.
5838 */
5839 if (cin_isdefault(s))
5840 return FALSE;
5841 if (cin_isscopedecl(s))
5842 return FALSE;
5843
5844 if (cin_islabel_skip(&s))
5845 {
5846 /*
5847 * Only accept a label if the previous line is terminated or is a case
5848 * label.
5849 */
5850 pos_T cursor_save;
5851 pos_T *trypos;
5852 char_u *line;
5853
5854 cursor_save = curwin->w_cursor;
5855 while (curwin->w_cursor.lnum > 1)
5856 {
5857 --curwin->w_cursor.lnum;
5858
5859 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005860 * If we're in a comment or raw string now, skip to the start of
5861 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 */
5863 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005864 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 curwin->w_cursor = *trypos;
5866
5867 line = ml_get_curline();
5868 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5869 continue;
5870 if (*(line = cin_skipcomment(line)) == NUL)
5871 continue;
5872
5873 curwin->w_cursor = cursor_save;
5874 if (cin_isterminated(line, TRUE, FALSE)
5875 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005876 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 || (cin_islabel_skip(&line) && cin_nocode(line)))
5878 return TRUE;
5879 return FALSE;
5880 }
5881 curwin->w_cursor = cursor_save;
5882 return TRUE; /* label at start of file??? */
5883 }
5884 return FALSE;
5885}
5886
5887/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005888 * Recognize structure initialization and enumerations:
5889 * "[typedef] [static|public|protected|private] enum"
5890 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 */
5892 static int
5893cin_isinit(void)
5894{
5895 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005896 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898 s = cin_skipcomment(ml_get_curline());
5899
Bram Moolenaar75342212013-03-07 13:13:52 +01005900 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 s = cin_skipcomment(s + 7);
5902
Bram Moolenaar75342212013-03-07 13:13:52 +01005903 for (;;)
5904 {
5905 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005906
Bram Moolenaar75342212013-03-07 13:13:52 +01005907 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5908 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005909 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005910 if (cin_starts_with(s, skip[i]))
5911 {
5912 s = cin_skipcomment(s + l);
5913 l = 0;
5914 break;
5915 }
5916 }
5917 if (l != 0)
5918 break;
5919 }
5920
5921 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 return TRUE;
5923
5924 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5925 return TRUE;
5926
5927 return FALSE;
5928}
5929
5930/*
5931 * Recognize a switch label: "case .*:" or "default:".
5932 */
5933 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005934cin_iscase(
5935 char_u *s,
5936 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005939 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {
5941 for (s += 4; *s; ++s)
5942 {
5943 s = cin_skipcomment(s);
5944 if (*s == ':')
5945 {
5946 if (s[1] == ':') /* skip over "::" for C++ */
5947 ++s;
5948 else
5949 return TRUE;
5950 }
5951 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005952 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5954 return FALSE; /* stop at comment */
5955 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005956 {
5957 /* JS etc. */
5958 if (strict)
5959 return FALSE; /* stop at string */
5960 else
5961 return TRUE;
5962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 }
5964 return FALSE;
5965 }
5966
5967 if (cin_isdefault(s))
5968 return TRUE;
5969 return FALSE;
5970}
5971
5972/*
5973 * Recognize a "default" switch label.
5974 */
5975 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005976cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 return (STRNCMP(s, "default", 7) == 0
5979 && *(s = cin_skipcomment(s + 7)) == ':'
5980 && s[1] != ':');
5981}
5982
5983/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005984 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 */
5986 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005987cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 int i;
5990
5991 s = cin_skipcomment(s);
5992 if (STRNCMP(s, "public", 6) == 0)
5993 i = 6;
5994 else if (STRNCMP(s, "protected", 9) == 0)
5995 i = 9;
5996 else if (STRNCMP(s, "private", 7) == 0)
5997 i = 7;
5998 else
5999 return FALSE;
6000 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6001}
6002
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006003/* Maximum number of lines to search back for a "namespace" line. */
6004#define FIND_NAMESPACE_LIM 20
6005
6006/*
6007 * Recognize a "namespace" scope declaration.
6008 */
6009 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006010cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006011{
6012 char_u *p;
6013 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006014 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006015
6016 s = cin_skipcomment(s);
6017 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6018 {
6019 p = cin_skipcomment(skipwhite(s + 9));
6020 while (*p != NUL)
6021 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006022 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006023 {
6024 has_name = TRUE; /* found end of a name */
6025 p = cin_skipcomment(skipwhite(p));
6026 }
6027 else if (*p == '{')
6028 {
6029 break;
6030 }
6031 else if (vim_iswordc(*p))
6032 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006033 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006034 if (has_name)
6035 return FALSE; /* word character after skipping past name */
6036 ++p;
6037 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006038 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6039 {
6040 if (!has_name_start || has_name)
6041 return FALSE;
6042 /* C++ 17 nested namespace */
6043 p += 3;
6044 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006045 else
6046 {
6047 return FALSE;
6048 }
6049 }
6050 return TRUE;
6051 }
6052 return FALSE;
6053}
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006056 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6057 */
6058 static int
6059cin_is_cpp_extern_c(char_u *s)
6060{
6061 char_u *p;
6062 int has_string_literal = FALSE;
6063
6064 s = cin_skipcomment(s);
6065 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6066 {
6067 p = cin_skipcomment(skipwhite(s + 6));
6068 while (*p != NUL)
6069 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006070 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006071 {
6072 p = cin_skipcomment(skipwhite(p));
6073 }
6074 else if (*p == '{')
6075 {
6076 break;
6077 }
6078 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6079 {
6080 if (has_string_literal)
6081 return FALSE;
6082 has_string_literal = TRUE;
6083 p += 3;
6084 }
6085 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6086 && p[4] == '"')
6087 {
6088 if (has_string_literal)
6089 return FALSE;
6090 has_string_literal = TRUE;
6091 p += 5;
6092 }
6093 else
6094 {
6095 return FALSE;
6096 }
6097 }
6098 return has_string_literal ? TRUE : FALSE;
6099 }
6100 return FALSE;
6101}
6102
6103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 * Return a pointer to the first non-empty non-comment character after a ':'.
6105 * Return NULL if not found.
6106 * case 234: a = b;
6107 * ^
6108 */
6109 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006110after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111{
6112 for ( ; *l; ++l)
6113 {
6114 if (*l == ':')
6115 {
6116 if (l[1] == ':') /* skip over "::" for C++ */
6117 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006118 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 break;
6120 }
6121 else if (*l == '\'' && l[1] && l[2] == '\'')
6122 l += 2; /* skip over 'x' */
6123 }
6124 if (*l == NUL)
6125 return NULL;
6126 l = cin_skipcomment(l + 1);
6127 if (*l == NUL)
6128 return NULL;
6129 return l;
6130}
6131
6132/*
6133 * Get indent of line "lnum", skipping a label.
6134 * Return 0 if there is nothing after the label.
6135 */
6136 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006137get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 char_u *l;
6140 pos_T fp;
6141 colnr_T col;
6142 char_u *p;
6143
6144 l = ml_get(lnum);
6145 p = after_label(l);
6146 if (p == NULL)
6147 return 0;
6148
6149 fp.col = (colnr_T)(p - l);
6150 fp.lnum = lnum;
6151 getvcol(curwin, &fp, &col, NULL, NULL);
6152 return (int)col;
6153}
6154
6155/*
6156 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006157 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 * label: if (asdf && asdfasdf)
6159 * ^
6160 */
6161 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006162skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 char_u *l;
6165 int amount;
6166 pos_T cursor_save;
6167
6168 cursor_save = curwin->w_cursor;
6169 curwin->w_cursor.lnum = lnum;
6170 l = ml_get_curline();
6171 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006172 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 {
6174 amount = get_indent_nolabel(lnum);
6175 l = after_label(ml_get_curline());
6176 if (l == NULL) /* just in case */
6177 l = ml_get_curline();
6178 }
6179 else
6180 {
6181 amount = get_indent();
6182 l = ml_get_curline();
6183 }
6184 *pp = l;
6185
6186 curwin->w_cursor = cursor_save;
6187 return amount;
6188}
6189
6190/*
6191 * Return the indent of the first variable name after a type in a declaration.
6192 * int a, indent of "a"
6193 * static struct foo b, indent of "b"
6194 * enum bla c, indent of "c"
6195 * Returns zero when it doesn't look like a declaration.
6196 */
6197 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006198cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 char_u *line, *p, *s;
6201 int len;
6202 pos_T fp;
6203 colnr_T col;
6204
6205 line = ml_get_curline();
6206 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006207 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6209 {
6210 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006211 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
6213 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6214 p = skipwhite(p + 6);
6215 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6216 p = skipwhite(p + 4);
6217 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6218 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6219 {
6220 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006221 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6222 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6223 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6224 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 p = s;
6226 }
6227 for (len = 0; vim_isIDc(p[len]); ++len)
6228 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006229 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 return 0;
6231
6232 p = skipwhite(p + len);
6233 fp.lnum = curwin->w_cursor.lnum;
6234 fp.col = (colnr_T)(p - line);
6235 getvcol(curwin, &fp, &col, NULL, NULL);
6236 return (int)col;
6237}
6238
6239/*
6240 * Return the indent of the first non-blank after an equal sign.
6241 * char *foo = "here";
6242 * Return zero if no (useful) equal sign found.
6243 * Return -1 if the line above "lnum" ends in a backslash.
6244 * foo = "asdf\
6245 * asdf\
6246 * here";
6247 */
6248 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006249cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 char_u *line;
6252 char_u *s;
6253 colnr_T col;
6254 pos_T fp;
6255
6256 if (lnum > 1)
6257 {
6258 line = ml_get(lnum - 1);
6259 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6260 return -1;
6261 }
6262
6263 line = s = ml_get(lnum);
6264 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6265 {
6266 if (cin_iscomment(s)) /* ignore comments */
6267 s = cin_skipcomment(s);
6268 else
6269 ++s;
6270 }
6271 if (*s != '=')
6272 return 0;
6273
6274 s = skipwhite(s + 1);
6275 if (cin_nocode(s))
6276 return 0;
6277
6278 if (*s == '"') /* nice alignment for continued strings */
6279 ++s;
6280
6281 fp.lnum = lnum;
6282 fp.col = (colnr_T)(s - line);
6283 getvcol(curwin, &fp, &col, NULL, NULL);
6284 return (int)col;
6285}
6286
6287/*
6288 * Recognize a preprocessor statement: Any line that starts with '#'.
6289 */
6290 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006291cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006293 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 return TRUE;
6295 return FALSE;
6296}
6297
6298/*
6299 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6300 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6301 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006302 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 */
6304 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006305cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306{
6307 char_u *line = *pp;
6308 linenr_T lnum = *lnump;
6309 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006310 int candidate_amount = *amount;
6311
6312 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6313 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006315 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 {
6317 if (cin_ispreproc(line))
6318 {
6319 retval = TRUE;
6320 *lnump = lnum;
6321 break;
6322 }
6323 if (lnum == 1)
6324 break;
6325 line = ml_get(--lnum);
6326 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6327 break;
6328 }
6329
6330 if (lnum != *lnump)
6331 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006332 if (retval)
6333 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 return retval;
6335}
6336
6337/*
6338 * Recognize the start of a C or C++ comment.
6339 */
6340 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006341cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6344}
6345
6346/*
6347 * Recognize the start of a "//" comment.
6348 */
6349 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006350cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 return (p[0] == '/' && p[1] == '/');
6353}
6354
6355/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006356 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6357 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006359 * If a line begins with an "else", only consider it terminated if no unmatched
6360 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 * Return the character terminating the line (ending char's have precedence if
6362 * both apply in order to determine initializations).
6363 */
6364 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006365cin_isterminated(
6366 char_u *s,
6367 int incl_open, /* include '{' at the end as terminator */
6368 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006370 char_u found_start = 0;
6371 unsigned n_open = 0;
6372 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
6374 s = cin_skipcomment(s);
6375
6376 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6377 found_start = *s;
6378
Bram Moolenaar496f9512011-05-19 16:35:09 +02006379 if (!found_start)
6380 is_else = cin_iselse(s);
6381
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 while (*s)
6383 {
6384 /* skip over comments, "" strings and 'c'haracters */
6385 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006386 if (*s == '}' && n_open > 0)
6387 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006388 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006389 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 && cin_nocode(s + 1))
6391 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006392 else if (*s == '{')
6393 {
6394 if (incl_open && cin_nocode(s + 1))
6395 return *s;
6396 else
6397 ++n_open;
6398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400 if (*s)
6401 s++;
6402 }
6403 return found_start;
6404}
6405
6406/*
6407 * Recognize the basic picture of a function declaration -- it needs to
6408 * have an open paren somewhere and a close paren at the end of the line and
6409 * no semicolons anywhere.
6410 * When a line ends in a comma we continue looking in the next line.
6411 * "sp" points to a string with the line. When looking at other lines it must
6412 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006413 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006414 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 */
6416 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006417cin_isfuncdecl(
6418 char_u **sp,
6419 linenr_T first_lnum,
6420 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
6422 char_u *s;
6423 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006424 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006426 pos_T *trypos;
6427 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
6429 if (sp == NULL)
6430 s = ml_get(lnum);
6431 else
6432 s = *sp;
6433
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006434 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006435 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006436 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006437 {
6438 lnum = trypos->lnum;
6439 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006440 {
6441 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006442 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006443 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006444
6445 s = ml_get(lnum);
6446 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006447 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006448
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006449 /* Ignore line starting with #. */
6450 if (cin_ispreproc(s))
6451 return FALSE;
6452
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6454 {
6455 if (cin_iscomment(s)) /* ignore comments */
6456 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006457 else if (*s == ':')
6458 {
6459 if (*(s + 1) == ':')
6460 s += 2;
6461 else
6462 /* To avoid a mistake in the following situation:
6463 * A::A(int a, int b)
6464 * : a(0) // <--not a function decl
6465 * , b(0)
6466 * {...
6467 */
6468 return FALSE;
6469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 else
6471 ++s;
6472 }
6473 if (*s != '(')
6474 return FALSE; /* ';', ' or " before any () or no '(' */
6475
6476 while (*s && *s != ';' && *s != '\'' && *s != '"')
6477 {
6478 if (*s == ')' && cin_nocode(s + 1))
6479 {
6480 /* ')' at the end: may have found a match
6481 * Check for he previous line not to end in a backslash:
6482 * #if defined(x) && \
6483 * defined(y)
6484 */
6485 lnum = first_lnum - 1;
6486 s = ml_get(lnum);
6487 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6488 retval = TRUE;
6489 goto done;
6490 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006491 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006493 int comma = (*s == ',');
6494
6495 /* ',' at the end: continue looking in the next line.
6496 * At the end: check for ',' in the next line, for this style:
6497 * func(arg1
6498 * , arg2) */
6499 for (;;)
6500 {
6501 if (lnum >= curbuf->b_ml.ml_line_count)
6502 break;
6503 s = ml_get(++lnum);
6504 if (!cin_ispreproc(s))
6505 break;
6506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (lnum >= curbuf->b_ml.ml_line_count)
6508 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006509 /* Require a comma at end of the line or a comma or ')' at the
6510 * start of next line. */
6511 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006512 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006513 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006514 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 }
6516 else if (cin_iscomment(s)) /* ignore comments */
6517 s = cin_skipcomment(s);
6518 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006521 just_started = FALSE;
6522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 }
6524
6525done:
6526 if (lnum != first_lnum && sp != NULL)
6527 *sp = ml_get(first_lnum);
6528
6529 return retval;
6530}
6531
6532 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006533cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006535 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536}
6537
6538 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006539cin_iselse(
6540 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
6542 if (*p == '}') /* accept "} else" */
6543 p = cin_skipcomment(p + 1);
6544 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6545}
6546
6547 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006548cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
6550 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6551}
6552
6553/*
6554 * Check if this is a "while" that should have a matching "do".
6555 * We only accept a "while (condition) ;", with only white space between the
6556 * ')' and ';'. The condition may be spread over several lines.
6557 */
6558 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006559cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 pos_T cursor_save;
6562 pos_T *trypos;
6563 int retval = FALSE;
6564
6565 p = cin_skipcomment(p);
6566 if (*p == '}') /* accept "} while (cond);" */
6567 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006568 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
6570 cursor_save = curwin->w_cursor;
6571 curwin->w_cursor.lnum = lnum;
6572 curwin->w_cursor.col = 0;
6573 p = ml_get_curline();
6574 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6575 {
6576 ++p;
6577 ++curwin->w_cursor.col;
6578 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006579 if ((trypos = findmatchlimit(NULL, 0, 0,
6580 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6582 retval = TRUE;
6583 curwin->w_cursor = cursor_save;
6584 }
6585 return retval;
6586}
6587
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006588/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006589 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006590 * Return 0 if there is none.
6591 * Otherwise return !0 and update "*poffset" to point to the place where the
6592 * string was found.
6593 */
6594 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006595cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006596{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006597 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006598
6599 if (offset-- < 2)
6600 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006601 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006602 --offset;
6603
6604 offset -= 1;
6605 if (!STRNCMP(line + offset, "if", 2))
6606 goto probablyFound;
6607
6608 if (offset >= 1)
6609 {
6610 offset -= 1;
6611 if (!STRNCMP(line + offset, "for", 3))
6612 goto probablyFound;
6613
6614 if (offset >= 2)
6615 {
6616 offset -= 2;
6617 if (!STRNCMP(line + offset, "while", 5))
6618 goto probablyFound;
6619 }
6620 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006621 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006622
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006623probablyFound:
6624 if (!offset || !vim_isIDc(line[offset - 1]))
6625 {
6626 *poffset = offset;
6627 return 1;
6628 }
6629 return 0;
6630}
6631
6632/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006633 * Return TRUE if we are at the end of a do-while.
6634 * do
6635 * nothing;
6636 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006637 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006638 * Adjust the cursor to the line with "while".
6639 */
6640 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006641cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006642{
6643 char_u *line;
6644 char_u *p;
6645 char_u *s;
6646 pos_T *trypos;
6647 int i;
6648
6649 if (terminated != ';') /* there must be a ';' at the end */
6650 return FALSE;
6651
6652 p = line = ml_get_curline();
6653 while (*p != NUL)
6654 {
6655 p = cin_skipcomment(p);
6656 if (*p == ')')
6657 {
6658 s = skipwhite(p + 1);
6659 if (*s == ';' && cin_nocode(s + 1))
6660 {
6661 /* Found ");" at end of the line, now check there is "while"
6662 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006663 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006664 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006665 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006666 if (trypos != NULL)
6667 {
6668 s = cin_skipcomment(ml_get(trypos->lnum));
6669 if (*s == '}') /* accept "} while (cond);" */
6670 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006671 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006672 {
6673 curwin->w_cursor.lnum = trypos->lnum;
6674 return TRUE;
6675 }
6676 }
6677
6678 /* Searching may have made "line" invalid, get it again. */
6679 line = ml_get_curline();
6680 p = line + i;
6681 }
6682 }
6683 if (*p != NUL)
6684 ++p;
6685 }
6686 return FALSE;
6687}
6688
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006690cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6693}
6694
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006695/*
6696 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 * constructor-initialization. eg:
6698 *
6699 * class MyClass :
6700 * baseClass <-- here
6701 * class MyClass : public baseClass,
6702 * anotherBaseClass <-- here (should probably lineup ??)
6703 * MyClass::MyClass(...) :
6704 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006705 *
6706 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 */
6708 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006709cin_is_cpp_baseclass(
6710 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006712 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 char_u *s;
6714 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006715 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006716 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006718 if (pos->lnum <= lnum)
6719 return cached->found; /* Use the cached result */
6720
6721 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006723 s = skipwhite(line);
6724 if (*s == '#') /* skip #define FOO x ? (x) : x */
6725 return FALSE;
6726 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 if (*s == NUL)
6728 return FALSE;
6729
6730 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6731
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006732 /* Search for a line starting with '#', empty, ending in ';' or containing
6733 * '{' or '}' and start below it. This handles the following situations:
6734 * a = cond ?
6735 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006736 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006737 * func::foo()
6738 * : something
6739 * {}
6740 * Foo::Foo (int one, int two)
6741 * : something(4),
6742 * somethingelse(3)
6743 * {}
6744 */
6745 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006747 line = ml_get(lnum - 1);
6748 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006749 if (*s == '#' || *s == NUL)
6750 break;
6751 while (*s != NUL)
6752 {
6753 s = cin_skipcomment(s);
6754 if (*s == '{' || *s == '}'
6755 || (*s == ';' && cin_nocode(s + 1)))
6756 break;
6757 if (*s != NUL)
6758 ++s;
6759 }
6760 if (*s != NUL)
6761 break;
6762 --lnum;
6763 }
6764
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006765 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006766 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006767 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006768 for (;;)
6769 {
6770 if (*s == NUL)
6771 {
6772 if (lnum == curwin->w_cursor.lnum)
6773 break;
6774 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006775 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006776 s = line;
6777 }
6778 if (s == line)
6779 {
6780 /* don't recognize "case (foo):" as a baseclass */
6781 if (cin_iscase(s, FALSE))
6782 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006783 s = cin_skipcomment(line);
6784 if (*s == NUL)
6785 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006786 }
6787
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006788 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006789 s = skip_string(s) + 1;
6790 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 if (s[1] == ':')
6793 {
6794 /* skip double colon. It can't be a constructor
6795 * initialization any more */
6796 lookfor_ctor_init = FALSE;
6797 s = cin_skipcomment(s + 2);
6798 }
6799 else if (lookfor_ctor_init || class_or_struct)
6800 {
6801 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006802 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 cpp_base_class = TRUE;
6804 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006805 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 s = cin_skipcomment(s + 1);
6807 }
6808 else
6809 s = cin_skipcomment(s + 1);
6810 }
6811 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6812 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6813 {
6814 class_or_struct = TRUE;
6815 lookfor_ctor_init = FALSE;
6816
6817 if (*s == 'c')
6818 s = cin_skipcomment(s + 5);
6819 else
6820 s = cin_skipcomment(s + 6);
6821 }
6822 else
6823 {
6824 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6825 {
6826 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6827 }
6828 else if (s[0] == ')')
6829 {
6830 /* Constructor-initialization is assumed if we come across
6831 * something like "):" */
6832 class_or_struct = FALSE;
6833 lookfor_ctor_init = TRUE;
6834 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006835 else if (s[0] == '?')
6836 {
6837 /* Avoid seeing '() :' after '?' as constructor init. */
6838 return FALSE;
6839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 else if (!vim_isIDc(s[0]))
6841 {
6842 /* if it is not an identifier, we are wrong */
6843 class_or_struct = FALSE;
6844 lookfor_ctor_init = FALSE;
6845 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006846 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {
6848 /* it can't be a constructor-initialization any more */
6849 lookfor_ctor_init = FALSE;
6850
6851 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006852 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006853 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006856 /* When the line ends in a comma don't align with it. */
6857 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006858 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006859
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 s = cin_skipcomment(s + 1);
6861 }
6862 }
6863
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006864 cached->found = cpp_base_class;
6865 if (cpp_base_class)
6866 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 return cpp_base_class;
6868}
6869
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006870 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006871get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006872{
6873 int amount;
6874 colnr_T vcol;
6875 pos_T *trypos;
6876
6877 if (col == 0)
6878 {
6879 amount = get_indent();
6880 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006881 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006882 amount = get_indent_lnum(trypos->lnum); /* XXX */
6883 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006884 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006885 }
6886 else
6887 {
6888 curwin->w_cursor.col = col;
6889 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6890 amount = (int)vcol;
6891 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006892 if (amount < curbuf->b_ind_cpp_baseclass)
6893 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006894 return amount;
6895}
6896
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897/*
6898 * Return TRUE if string "s" ends with the string "find", possibly followed by
6899 * white space and comments. Skip strings and comments.
6900 * Ignore "ignore" after "find" if it's not NULL.
6901 */
6902 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 char_u *p = s;
6906 char_u *r;
6907 int len = (int)STRLEN(find);
6908
6909 while (*p != NUL)
6910 {
6911 p = cin_skipcomment(p);
6912 if (STRNCMP(p, find, len) == 0)
6913 {
6914 r = skipwhite(p + len);
6915 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6916 r = skipwhite(r + STRLEN(ignore));
6917 if (cin_nocode(r))
6918 return TRUE;
6919 }
6920 if (*p != NUL)
6921 ++p;
6922 }
6923 return FALSE;
6924}
6925
6926/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006927 * Return TRUE when "s" starts with "word" and then a non-ID character.
6928 */
6929 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006930cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006931{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006932 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006933
6934 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6935}
6936
6937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 * Skip strings, chars and comments until at or past "trypos".
6939 * Return the column found.
6940 */
6941 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006942cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944 char_u *line;
6945 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006946 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
6948 p = line = ml_get(trypos->lnum);
6949 while (*p && (colnr_T)(p - line) < trypos->col)
6950 {
6951 if (cin_iscomment(p))
6952 p = cin_skipcomment(p);
6953 else
6954 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006955 new_p = skip_string(p);
6956 if (new_p == p)
6957 ++p;
6958 else
6959 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
6961 }
6962 return (int)(p - line);
6963}
6964
6965/*
6966 * Find the '{' at the start of the block we are in.
6967 * Return NULL if no match found.
6968 * Ignore a '{' that is in a comment, makes indenting the next three lines
6969 * work. */
6970/* foo() */
6971/* { */
6972/* } */
6973
6974 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006975find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977 pos_T cursor_save;
6978 pos_T *trypos;
6979 pos_T *pos;
6980 static pos_T pos_copy;
6981
6982 cursor_save = curwin->w_cursor;
6983 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6984 {
6985 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6986 trypos = &pos_copy;
6987 curwin->w_cursor = *trypos;
6988 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006989 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006991 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 break;
6993 if (pos != NULL)
6994 curwin->w_cursor.lnum = pos->lnum;
6995 }
6996 curwin->w_cursor = cursor_save;
6997 return trypos;
6998}
6999
7000/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007001 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007002 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 */
7004 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007005find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007007 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007008}
7009
7010 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007011find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007012{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 pos_T cursor_save;
7014 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007015 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007016 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007019 ind_maxp_wk = ind_maxparen;
7020retry:
7021 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {
7023 /* check if the ( is in a // comment */
7024 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007025 {
7026 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7027 if (ind_maxp_wk > 0)
7028 {
7029 curwin->w_cursor = *trypos;
7030 curwin->w_cursor.col = 0; /* XXX */
7031 goto retry;
7032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 else
7036 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007037 pos_T *trypos_wk;
7038
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7040 trypos = &pos_copy;
7041 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007042 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007043 {
7044 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7045 - trypos_wk->lnum);
7046 if (ind_maxp_wk > 0)
7047 {
7048 curwin->w_cursor = *trypos_wk;
7049 goto retry;
7050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
7054 }
7055 curwin->w_cursor = cursor_save;
7056 return trypos;
7057}
7058
7059/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007060 * Find the matching '(', ignoring it if it is in a comment or before an
7061 * unmatched {.
7062 * Return NULL if no match found.
7063 */
7064 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007065find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007066{
7067 pos_T *trypos = find_match_paren(ind_maxparen);
7068
7069 if (trypos != NULL)
7070 {
7071 pos_T *tryposBrace = find_start_brace();
7072
7073 /* If both an unmatched '(' and '{' is found. Ignore the '('
7074 * position if the '{' is further down. */
7075 if (tryposBrace != NULL
7076 && (trypos->lnum != tryposBrace->lnum
7077 ? trypos->lnum < tryposBrace->lnum
7078 : trypos->col < tryposBrace->col))
7079 trypos = NULL;
7080 }
7081 return trypos;
7082}
7083
7084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 * Return ind_maxparen corrected for the difference in line number between the
7086 * cursor position and "startpos". This makes sure that searching for a
7087 * matching paren above the cursor line doesn't find a match because of
7088 * looking a few lines further.
7089 */
7090 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007091corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
7093 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7094
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007095 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7096 return curbuf->b_ind_maxparen - (int)n;
7097 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098}
7099
7100/*
7101 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007102 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 */
7104 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007105find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
7107 int i;
7108 int retval = FALSE;
7109 int open_count = 0;
7110
7111 curwin->w_cursor.col = 0; /* default is start of line */
7112
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007113 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {
7115 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7116 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7117 if (l[i] == start)
7118 ++open_count;
7119 else if (l[i] == end)
7120 {
7121 if (open_count > 0)
7122 --open_count;
7123 else
7124 {
7125 curwin->w_cursor.col = i;
7126 retval = TRUE;
7127 }
7128 }
7129 }
7130 return retval;
7131}
7132
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007133/*
7134 * Parse 'cinoptions' and set the values in "curbuf".
7135 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7136 */
7137 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007138parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007139{
7140 char_u *p;
7141 char_u *l;
7142 char_u *digits;
7143 int n;
7144 int divider;
7145 int fraction = 0;
7146 int sw = (int)get_sw_value(buf);
7147
7148 /*
7149 * Set the default values.
7150 */
7151 /* Spaces from a block's opening brace the prevailing indent for that
7152 * block should be. */
7153 buf->b_ind_level = sw;
7154
7155 /* Spaces from the edge of the line an open brace that's at the end of a
7156 * line is imagined to be. */
7157 buf->b_ind_open_imag = 0;
7158
7159 /* Spaces from the prevailing indent for a line that is not preceded by
7160 * an opening brace. */
7161 buf->b_ind_no_brace = 0;
7162
7163 /* Column where the first { of a function should be located }. */
7164 buf->b_ind_first_open = 0;
7165
7166 /* Spaces from the prevailing indent a leftmost open brace should be
7167 * located. */
7168 buf->b_ind_open_extra = 0;
7169
7170 /* Spaces from the matching open brace (real location for one at the left
7171 * edge; imaginary location from one that ends a line) the matching close
7172 * brace should be located. */
7173 buf->b_ind_close_extra = 0;
7174
7175 /* Spaces from the edge of the line an open brace sitting in the leftmost
7176 * column is imagined to be. */
7177 buf->b_ind_open_left_imag = 0;
7178
7179 /* Spaces jump labels should be shifted to the left if N is non-negative,
7180 * otherwise the jump label will be put to column 1. */
7181 buf->b_ind_jump_label = -1;
7182
7183 /* Spaces from the switch() indent a "case xx" label should be located. */
7184 buf->b_ind_case = sw;
7185
7186 /* Spaces from the "case xx:" code after a switch() should be located. */
7187 buf->b_ind_case_code = sw;
7188
7189 /* Lineup break at end of case in switch() with case label. */
7190 buf->b_ind_case_break = 0;
7191
7192 /* Spaces from the class declaration indent a scope declaration label
7193 * should be located. */
7194 buf->b_ind_scopedecl = sw;
7195
7196 /* Spaces from the scope declaration label code should be located. */
7197 buf->b_ind_scopedecl_code = sw;
7198
7199 /* Amount K&R-style parameters should be indented. */
7200 buf->b_ind_param = sw;
7201
7202 /* Amount a function type spec should be indented. */
7203 buf->b_ind_func_type = sw;
7204
7205 /* Amount a cpp base class declaration or constructor initialization
7206 * should be indented. */
7207 buf->b_ind_cpp_baseclass = sw;
7208
7209 /* additional spaces beyond the prevailing indent a continuation line
7210 * should be located. */
7211 buf->b_ind_continuation = sw;
7212
7213 /* Spaces from the indent of the line with an unclosed parentheses. */
7214 buf->b_ind_unclosed = sw * 2;
7215
7216 /* Spaces from the indent of the line with an unclosed parentheses, which
7217 * itself is also unclosed. */
7218 buf->b_ind_unclosed2 = sw;
7219
7220 /* Suppress ignoring spaces from the indent of a line starting with an
7221 * unclosed parentheses. */
7222 buf->b_ind_unclosed_noignore = 0;
7223
7224 /* If the opening paren is the last nonwhite character on the line, and
7225 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7226 * context (for very long lines). */
7227 buf->b_ind_unclosed_wrapped = 0;
7228
7229 /* Suppress ignoring white space when lining up with the character after
7230 * an unclosed parentheses. */
7231 buf->b_ind_unclosed_whiteok = 0;
7232
7233 /* Indent a closing parentheses under the line start of the matching
7234 * opening parentheses. */
7235 buf->b_ind_matching_paren = 0;
7236
7237 /* Indent a closing parentheses under the previous line. */
7238 buf->b_ind_paren_prev = 0;
7239
7240 /* Extra indent for comments. */
7241 buf->b_ind_comment = 0;
7242
7243 /* Spaces from the comment opener when there is nothing after it. */
7244 buf->b_ind_in_comment = 3;
7245
7246 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7247 * after the comment opener. */
7248 buf->b_ind_in_comment2 = 0;
7249
7250 /* Max lines to search for an open paren. */
7251 buf->b_ind_maxparen = 20;
7252
7253 /* Max lines to search for an open comment. */
7254 buf->b_ind_maxcomment = 70;
7255
7256 /* Handle braces for java code. */
7257 buf->b_ind_java = 0;
7258
7259 /* Not to confuse JS object properties with labels. */
7260 buf->b_ind_js = 0;
7261
7262 /* Handle blocked cases correctly. */
7263 buf->b_ind_keep_case_label = 0;
7264
7265 /* Handle C++ namespace. */
7266 buf->b_ind_cpp_namespace = 0;
7267
7268 /* Handle continuation lines containing conditions of if(), for() and
7269 * while(). */
7270 buf->b_ind_if_for_while = 0;
7271
Bram Moolenaar6b643942017-03-05 19:44:06 +01007272 /* indentation for # comments */
7273 buf->b_ind_hash_comment = 0;
7274
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007275 /* Handle C++ extern "C" or "C++" */
7276 buf->b_ind_cpp_extern_c = 0;
7277
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007278 for (p = buf->b_p_cino; *p; )
7279 {
7280 l = p++;
7281 if (*p == '-')
7282 ++p;
7283 digits = p; /* remember where the digits start */
7284 n = getdigits(&p);
7285 divider = 0;
7286 if (*p == '.') /* ".5s" means a fraction */
7287 {
7288 fraction = atol((char *)++p);
7289 while (VIM_ISDIGIT(*p))
7290 {
7291 ++p;
7292 if (divider)
7293 divider *= 10;
7294 else
7295 divider = 10;
7296 }
7297 }
7298 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7299 {
7300 if (p == digits)
7301 n = sw; /* just "s" is one 'shiftwidth' */
7302 else
7303 {
7304 n *= sw;
7305 if (divider)
7306 n += (sw * fraction + divider / 2) / divider;
7307 }
7308 ++p;
7309 }
7310 if (l[1] == '-')
7311 n = -n;
7312
7313 /* When adding an entry here, also update the default 'cinoptions' in
7314 * doc/indent.txt, and add explanation for it! */
7315 switch (*l)
7316 {
7317 case '>': buf->b_ind_level = n; break;
7318 case 'e': buf->b_ind_open_imag = n; break;
7319 case 'n': buf->b_ind_no_brace = n; break;
7320 case 'f': buf->b_ind_first_open = n; break;
7321 case '{': buf->b_ind_open_extra = n; break;
7322 case '}': buf->b_ind_close_extra = n; break;
7323 case '^': buf->b_ind_open_left_imag = n; break;
7324 case 'L': buf->b_ind_jump_label = n; break;
7325 case ':': buf->b_ind_case = n; break;
7326 case '=': buf->b_ind_case_code = n; break;
7327 case 'b': buf->b_ind_case_break = n; break;
7328 case 'p': buf->b_ind_param = n; break;
7329 case 't': buf->b_ind_func_type = n; break;
7330 case '/': buf->b_ind_comment = n; break;
7331 case 'c': buf->b_ind_in_comment = n; break;
7332 case 'C': buf->b_ind_in_comment2 = n; break;
7333 case 'i': buf->b_ind_cpp_baseclass = n; break;
7334 case '+': buf->b_ind_continuation = n; break;
7335 case '(': buf->b_ind_unclosed = n; break;
7336 case 'u': buf->b_ind_unclosed2 = n; break;
7337 case 'U': buf->b_ind_unclosed_noignore = n; break;
7338 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7339 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7340 case 'm': buf->b_ind_matching_paren = n; break;
7341 case 'M': buf->b_ind_paren_prev = n; break;
7342 case ')': buf->b_ind_maxparen = n; break;
7343 case '*': buf->b_ind_maxcomment = n; break;
7344 case 'g': buf->b_ind_scopedecl = n; break;
7345 case 'h': buf->b_ind_scopedecl_code = n; break;
7346 case 'j': buf->b_ind_java = n; break;
7347 case 'J': buf->b_ind_js = n; break;
7348 case 'l': buf->b_ind_keep_case_label = n; break;
7349 case '#': buf->b_ind_hash_comment = n; break;
7350 case 'N': buf->b_ind_cpp_namespace = n; break;
7351 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007352 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007353 }
7354 if (*p == ',')
7355 ++p;
7356 }
7357}
7358
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007359/*
7360 * Return the desired indent for C code.
7361 * Return -1 if the indent should be left alone (inside a raw string).
7362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007364get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 pos_T cur_curpos;
7367 int amount;
7368 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007369 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 colnr_T col;
7371 char_u *theline;
7372 char_u *linecopy;
7373 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007374 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007376 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 pos_T our_paren_pos;
7378 char_u *start;
7379 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007380#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381#define BRACE_AT_START 2 /* '{' is at start of line */
7382#define BRACE_AT_END 3 /* '{' is at end of line */
7383 linenr_T ourscope;
7384 char_u *l;
7385 char_u *look;
7386 char_u terminated;
7387 int lookfor;
7388#define LOOKFOR_INITIAL 0
7389#define LOOKFOR_IF 1
7390#define LOOKFOR_DO 2
7391#define LOOKFOR_CASE 3
7392#define LOOKFOR_ANY 4
7393#define LOOKFOR_TERM 5
7394#define LOOKFOR_UNTERM 6
7395#define LOOKFOR_SCOPEDECL 7
7396#define LOOKFOR_NOBREAK 8
7397#define LOOKFOR_CPP_BASECLASS 9
7398#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007399#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007400#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
7402 int whilelevel;
7403 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int n;
7405 int iscase;
7406 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007407 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007409 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007410 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007411 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007412 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007413 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007415 /* make a copy, value is changed below */
7416 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 /* remember where the cursor was when we started */
7419 cur_curpos = curwin->w_cursor;
7420
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007421 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007422 if (cur_curpos.lnum == 1)
7423 return 0;
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 /* Get a copy of the current contents of the line.
7426 * This is required, because only the most recent line obtained with
7427 * ml_get is valid! */
7428 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7429 if (linecopy == NULL)
7430 return 0;
7431
7432 /*
7433 * In insert mode and the cursor is on a ')' truncate the line at the
7434 * cursor position. We don't want to line up with the matching '(' when
7435 * inserting new stuff.
7436 * For unknown reasons the cursor might be past the end of the line, thus
7437 * check for that.
7438 */
7439 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007440 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 && linecopy[curwin->w_cursor.col] == ')')
7442 linecopy[curwin->w_cursor.col] = NUL;
7443
7444 theline = skipwhite(linecopy);
7445
7446 /* move the cursor to the start of the line */
7447
7448 curwin->w_cursor.col = 0;
7449
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007450 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007451
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007453 * If we are inside a raw string don't change the indent.
7454 * Ignore a raw string inside a comment.
7455 */
7456 comment_pos = ind_find_start_comment();
7457 if (comment_pos != NULL)
7458 {
7459 /* findmatchlimit() static pos is overwritten, make a copy */
7460 tryposCopy = *comment_pos;
7461 comment_pos = &tryposCopy;
7462 }
7463 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007464 if (trypos != NULL && (comment_pos == NULL
7465 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007466 {
7467 amount = -1;
7468 goto laterend;
7469 }
7470
7471 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 * #defines and so on always go at the left when included in 'cinkeys'.
7473 */
7474 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007475 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007476 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007477 goto theend;
7478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479
7480 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007481 * Is it a non-case label? Then that goes at the left margin too unless:
7482 * - JS flag is set.
7483 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007485 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007486 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 {
7488 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007489 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 }
7491
7492 /*
7493 * If we're inside a "//" comment and there is a "//" comment in a
7494 * previous line, lineup with that one.
7495 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007496 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 && (trypos = find_line_comment()) != NULL) /* XXX */
7498 {
7499 /* find how indented the line beginning the comment is */
7500 getvcol(curwin, trypos, &col, NULL, NULL);
7501 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007502 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504
7505 /*
7506 * If we're inside a comment and not looking at the start of the
7507 * comment, try using the 'comments' option.
7508 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007509 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 {
7511 int lead_start_len = 2;
7512 int lead_middle_len = 1;
7513 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7514 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7515 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7516 char_u *p;
7517 int start_align = 0;
7518 int start_off = 0;
7519 int done = FALSE;
7520
7521 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007522 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007524 *lead_start = NUL;
7525 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526
7527 p = curbuf->b_p_com;
7528 while (*p != NUL)
7529 {
7530 int align = 0;
7531 int off = 0;
7532 int what = 0;
7533
7534 while (*p != NUL && *p != ':')
7535 {
7536 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7537 what = *p++;
7538 else if (*p == COM_LEFT || *p == COM_RIGHT)
7539 align = *p++;
7540 else if (VIM_ISDIGIT(*p) || *p == '-')
7541 off = getdigits(&p);
7542 else
7543 ++p;
7544 }
7545
7546 if (*p == ':')
7547 ++p;
7548 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7549 if (what == COM_START)
7550 {
7551 STRCPY(lead_start, lead_end);
7552 lead_start_len = (int)STRLEN(lead_start);
7553 start_off = off;
7554 start_align = align;
7555 }
7556 else if (what == COM_MIDDLE)
7557 {
7558 STRCPY(lead_middle, lead_end);
7559 lead_middle_len = (int)STRLEN(lead_middle);
7560 }
7561 else if (what == COM_END)
7562 {
7563 /* If our line starts with the middle comment string, line it
7564 * up with the comment opener per the 'comments' option. */
7565 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7566 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7567 {
7568 done = TRUE;
7569 if (curwin->w_cursor.lnum > 1)
7570 {
7571 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007572 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 * the middle comment string matches in the previous
7574 * line, use the indent of that line. XXX */
7575 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7576 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7577 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7578 else if (STRNCMP(look, lead_middle,
7579 lead_middle_len) == 0)
7580 {
7581 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7582 break;
7583 }
7584 /* If the start comment string doesn't match with the
7585 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007586 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 lead_start, lead_start_len) != 0)
7588 continue;
7589 }
7590 if (start_off != 0)
7591 amount += start_off;
7592 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007593 amount += vim_strsize(lead_start)
7594 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 break;
7596 }
7597
7598 /* If our line starts with the end comment string, line it up
7599 * with the middle comment */
7600 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7601 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7602 {
7603 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7604 /* XXX */
7605 if (off != 0)
7606 amount += off;
7607 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007608 amount += vim_strsize(lead_start)
7609 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 done = TRUE;
7611 break;
7612 }
7613 }
7614 }
7615
7616 /* If our line starts with an asterisk, line up with the
7617 * asterisk in the comment opener; otherwise, line up
7618 * with the first character of the comment text.
7619 */
7620 if (done)
7621 ;
7622 else if (theline[0] == '*')
7623 amount += 1;
7624 else
7625 {
7626 /*
7627 * If we are more than one line away from the comment opener, take
7628 * the indent of the previous non-empty line. If 'cino' has "CO"
7629 * and we are just below the comment opener and there are any
7630 * white characters after it line up with the text after it;
7631 * otherwise, add the amount specified by "c" in 'cino'
7632 */
7633 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007634 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 {
7636 if (linewhite(lnum)) /* skip blank lines */
7637 continue;
7638 amount = get_indent_lnum(lnum); /* XXX */
7639 break;
7640 }
7641 if (amount == -1) /* use the comment opener */
7642 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007643 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007645 start = ml_get(comment_pos->lnum);
7646 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007648 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007650 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007652 if (curbuf->b_ind_in_comment2 || *look == NUL)
7653 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007656 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
7658
7659 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007660 * Are we looking at a ']' that has a match?
7661 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007662 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007663 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7664 {
7665 /* align with the line containing the '['. */
7666 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007667 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007668 }
7669
7670 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 * Are we inside parentheses or braces?
7672 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007673 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007674 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007675 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 || trypos != NULL)
7677 {
7678 if (trypos != NULL && tryposBrace != NULL)
7679 {
7680 /* Both an unmatched '(' and '{' is found. Use the one which is
7681 * closer to the current cursor position, set the other to NULL. */
7682 if (trypos->lnum != tryposBrace->lnum
7683 ? trypos->lnum < tryposBrace->lnum
7684 : trypos->col < tryposBrace->col)
7685 trypos = NULL;
7686 else
7687 tryposBrace = NULL;
7688 }
7689
7690 if (trypos != NULL)
7691 {
7692 /*
7693 * If the matching paren is more than one line away, use the indent of
7694 * a previous non-empty line that matches the same paren.
7695 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007696 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007698 /* Line up with the start of the matching paren line. */
7699 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7700 }
7701 else
7702 {
7703 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007704 our_paren_pos = *trypos;
7705 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007707 l = skipwhite(ml_get(lnum));
7708 if (cin_nocode(l)) /* skip comment lines */
7709 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007710 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007711 continue; /* ignore #define, #if, etc. */
7712 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007714 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007715 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007716 {
7717 lnum = trypos->lnum + 1;
7718 continue;
7719 }
7720
7721 /* XXX */
7722 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007723 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007724 && trypos->lnum == our_paren_pos.lnum
7725 && trypos->col == our_paren_pos.col)
7726 {
7727 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007729 if (theline[0] == ')')
7730 {
7731 if (our_paren_pos.lnum != lnum
7732 && cur_amount > amount)
7733 cur_amount = amount;
7734 amount = -1;
7735 }
7736 break;
7737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739 }
7740
7741 /*
7742 * Line up with line where the matching paren is. XXX
7743 * If the line starts with a '(' or the indent for unclosed
7744 * parentheses is zero, line up with the unclosed parentheses.
7745 */
7746 if (amount == -1)
7747 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007748 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007749 int is_if_for_while = 0;
7750
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007751 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007752 {
7753 /* Look for the outermost opening parenthesis on this line
7754 * and check whether it belongs to an "if", "for" or "while". */
7755
7756 pos_T cursor_save = curwin->w_cursor;
7757 pos_T outermost;
7758 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007759
7760 trypos = &our_paren_pos;
7761 do {
7762 outermost = *trypos;
7763 curwin->w_cursor.lnum = outermost.lnum;
7764 curwin->w_cursor.col = outermost.col;
7765
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007766 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007767 } while (trypos && trypos->lnum == outermost.lnum);
7768
7769 curwin->w_cursor = cursor_save;
7770
7771 line = ml_get(outermost.lnum);
7772
7773 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007774 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007775 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007776
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007777 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007778 look = skipwhite(look);
7779 if (*look == '(')
7780 {
7781 linenr_T save_lnum = curwin->w_cursor.lnum;
7782 char_u *line;
7783 int look_col;
7784
7785 /* Ignore a '(' in front of the line that has a match before
7786 * our matching '('. */
7787 curwin->w_cursor.lnum = our_paren_pos.lnum;
7788 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007789 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007790 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007791 if ((trypos = findmatchlimit(NULL, ')', 0,
7792 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007793 != NULL
7794 && trypos->lnum == our_paren_pos.lnum
7795 && trypos->col < our_paren_pos.col)
7796 ignore_paren_col = trypos->col + 1;
7797
7798 curwin->w_cursor.lnum = save_lnum;
7799 look = ml_get(our_paren_pos.lnum) + look_col;
7800 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007801 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7802 && is_if_for_while == 0)
7803 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007804 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
7806 /*
7807 * If we're looking at a close paren, line up right there;
7808 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007809 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 * the last nonwhite character of the line, use either the
7811 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007812 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 * lines).
7814 */
7815 if (theline[0] != ')')
7816 {
7817 cur_amount = MAXCOL;
7818 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007819 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 && cin_ends_in(l, (char_u *)"(", NULL))
7821 {
7822 /* look for opening unmatched paren, indent one level
7823 * for each additional level */
7824 n = 1;
7825 for (col = 0; col < our_paren_pos.col; ++col)
7826 {
7827 switch (l[col])
7828 {
7829 case '(':
7830 case '{': ++n;
7831 break;
7832
7833 case ')':
7834 case '}': if (n > 1)
7835 --n;
7836 break;
7837 }
7838 }
7839
7840 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007841 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007843 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 our_paren_pos.col++;
7845 else
7846 {
7847 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007848 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 col++;
7850 if (l[col] != NUL) /* In case of trailing space */
7851 our_paren_pos.col = col;
7852 else
7853 our_paren_pos.col++;
7854 }
7855 }
7856
7857 /*
7858 * Find how indented the paren is, or the character after it
7859 * if we did the above "if".
7860 */
7861 if (our_paren_pos.col > 0)
7862 {
7863 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7864 if (cur_amount > (int)col)
7865 cur_amount = col;
7866 }
7867 }
7868
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007869 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
7871 /* Line up with the start of the matching paren line. */
7872 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007873 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7874 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007875 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
7877 if (cur_amount != MAXCOL)
7878 amount = cur_amount;
7879 }
7880 else
7881 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007882 /* Add b_ind_unclosed2 for each '(' before our matching one,
7883 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007885 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 --our_paren_pos.col;
7888 switch (*ml_get_pos(&our_paren_pos))
7889 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007890 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 col = our_paren_pos.col;
7892 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007893 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 col = MAXCOL;
7895 break;
7896 }
7897 }
7898
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007899 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 * braces */
7901 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007902 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 else
7904 {
7905 curwin->w_cursor.lnum = our_paren_pos.lnum;
7906 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007907 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7908 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007909 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007911 {
7912 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007913 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007914 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007915 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 }
7918 /*
7919 * For a line starting with ')' use the minimum of the two
7920 * positions, to avoid giving it more indent than the previous
7921 * lines:
7922 * func_long_name( if (x
7923 * arg && yy
7924 * ) ^ not here ) ^ not here
7925 */
7926 if (cur_amount < amount)
7927 amount = cur_amount;
7928 }
7929 }
7930
7931 /* add extra indent for a comment */
7932 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007933 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 else
7936 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007937 /*
7938 * We are inside braces, there is a { before this line at the position
7939 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007940 * Make a copy of tryposBrace, it may point to pos_copy inside
7941 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007942 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007943 tryposCopy = *tryposBrace;
7944 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 ourscope = trypos->lnum;
7947 start = ml_get(ourscope);
7948
7949 /*
7950 * Now figure out how indented the line is in general.
7951 * If the brace was at the start of the line, we use that;
7952 * otherwise, check out the indentation of the line as
7953 * a whole and then add the "imaginary indent" to that.
7954 */
7955 look = skipwhite(start);
7956 if (*look == '{')
7957 {
7958 getvcol(curwin, trypos, &col, NULL, NULL);
7959 amount = col;
7960 if (*start == '{')
7961 start_brace = BRACE_IN_COL0;
7962 else
7963 start_brace = BRACE_AT_START;
7964 }
7965 else
7966 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007967 /* That opening brace might have been on a continuation
7968 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 curwin->w_cursor.lnum = ourscope;
7970
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007971 /* Position the cursor over the rightmost paren, so that
7972 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 lnum = ourscope;
7974 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007975 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7976 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 lnum = trypos->lnum;
7978
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007979 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 * case 1: if (asdf &&
7981 * ldfd) {
7982 * }
7983 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007984 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7985 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007987 else if (curbuf->b_ind_js)
7988 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007990 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991
7992 start_brace = BRACE_AT_END;
7993 }
7994
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007995 /* For Javascript check if the line starts with "key:". */
7996 if (curbuf->b_ind_js)
7997 js_cur_has_key = cin_has_js_key(theline);
7998
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008000 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 * we want to be. otherwise, add the amount of room
8002 * that an indent is supposed to be.
8003 */
8004 if (theline[0] == '}')
8005 {
8006 /*
8007 * they may want closing braces to line up with something
8008 * other than the open brace. indulge them, if so.
8009 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008010 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 }
8012 else
8013 {
8014 /*
8015 * If we're looking at an "else", try to find an "if"
8016 * to match it with.
8017 * If we're looking at a "while", try to find a "do"
8018 * to match it with.
8019 */
8020 lookfor = LOOKFOR_INITIAL;
8021 if (cin_iselse(theline))
8022 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008023 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 lookfor = LOOKFOR_DO;
8025 if (lookfor != LOOKFOR_INITIAL)
8026 {
8027 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008028 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {
8030 amount = get_indent(); /* XXX */
8031 goto theend;
8032 }
8033 }
8034
8035 /*
8036 * We get here if we are not on an "while-of-do" or "else" (or
8037 * failed to find a matching "if").
8038 * Search backwards for something to line up with.
8039 * First set amount for when we don't find anything.
8040 */
8041
8042 /*
8043 * if the '{' is _really_ at the left margin, use the imaginary
8044 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008045 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 */
8047
8048 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8049 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008050 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008051 lookfor_cpp_namespace = TRUE;
8052 }
8053 else if (start_brace == BRACE_AT_START &&
8054 lookfor_cpp_namespace) /* '{' is at start */
8055 {
8056
8057 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059 else
8060 {
8061 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008062 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008063 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008064
8065 l = skipwhite(ml_get_curline());
8066 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008067 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008068 else if (cin_is_cpp_extern_c(l))
8069 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 else
8072 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008073 /* Compensate for adding b_ind_open_extra later. */
8074 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 if (amount < 0)
8076 amount = 0;
8077 }
8078 }
8079
8080 lookfor_break = FALSE;
8081
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008082 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
8084 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008085 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 }
8087 else if (cin_isscopedecl(theline)) /* private:, ... */
8088 {
8089 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008090 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092 else
8093 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008094 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8095 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 lookfor_break = TRUE;
8097
8098 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008099 /* b_ind_level from start of block */
8100 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
8102 scope_amount = amount;
8103 whilelevel = 0;
8104
8105 /*
8106 * Search backwards. If we find something we recognize, line up
8107 * with that.
8108 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008109 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 * the usual amount relative to the conditional
8111 * that opens the block.
8112 */
8113 curwin->w_cursor = cur_curpos;
8114 for (;;)
8115 {
8116 curwin->w_cursor.lnum--;
8117 curwin->w_cursor.col = 0;
8118
8119 /*
8120 * If we went all the way back to the start of our scope, line
8121 * up with it.
8122 */
8123 if (curwin->w_cursor.lnum <= ourscope)
8124 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008125 /* We reached end of scope:
8126 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008128 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 * don't add ind_continuation, otherwise it is a variable
8130 * declaration:
8131 * int x,
8132 * here; <-- add ind_continuation
8133 */
8134 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8135 {
8136 if (curwin->w_cursor.lnum == 0
8137 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008138 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008140 /* nothing found (abuse curbuf->b_ind_maxparen as
8141 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 * initialization) */
8143 if (cont_amount > 0)
8144 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008145 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 amount += ind_continuation;
8147 break;
8148 }
8149
8150 l = ml_get_curline();
8151
8152 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008153 * If we're in a comment or raw string now, skip to
8154 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008156 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 if (trypos != NULL)
8158 {
8159 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008160 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 continue;
8162 }
8163
8164 /*
8165 * Skip preprocessor directives and blank lines.
8166 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008167 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8168 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 continue;
8170
8171 if (cin_nocode(l))
8172 continue;
8173
8174 terminated = cin_isterminated(l, FALSE, TRUE);
8175
8176 /*
8177 * If we are at top level and the line looks like a
8178 * function declaration, we are done
8179 * (it's a variable declaration).
8180 */
8181 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008182 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {
8184 /* if the line is terminated with another ','
8185 * it is a continued variable initialization.
8186 * don't add extra indent.
8187 * TODO: does not work, if a function
8188 * declaration is split over multiple lines:
8189 * cin_isfuncdecl returns FALSE then.
8190 */
8191 if (terminated == ',')
8192 break;
8193
8194 /* if it es a enum declaration or an assignment,
8195 * we are done.
8196 */
8197 if (terminated != ';' && cin_isinit())
8198 break;
8199
8200 /* nothing useful found */
8201 if (terminated == 0 || terminated == '{')
8202 continue;
8203 }
8204
8205 if (terminated != ';')
8206 {
8207 /* Skip parens and braces. Position the cursor
8208 * over the rightmost paren, so that matching it
8209 * will take us back to the start of the line.
8210 */ /* XXX */
8211 trypos = NULL;
8212 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008213 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008214 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215
8216 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008217 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218
8219 if (trypos != NULL)
8220 {
8221 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008222 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 continue;
8224 }
8225 }
8226
8227 /* it's a variable declaration, add indentation
8228 * like in
8229 * int a,
8230 * b;
8231 */
8232 if (cont_amount > 0)
8233 amount = cont_amount;
8234 else
8235 amount += ind_continuation;
8236 }
8237 else if (lookfor == LOOKFOR_UNTERM)
8238 {
8239 if (cont_amount > 0)
8240 amount = cont_amount;
8241 else
8242 amount += ind_continuation;
8243 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008244 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008245 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008246 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008247 && lookfor != LOOKFOR_CPP_BASECLASS
8248 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008249 {
8250 amount = scope_amount;
8251 if (theline[0] == '{')
8252 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008253 amount += curbuf->b_ind_open_extra;
8254 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008255 }
8256 }
8257
8258 if (lookfor_cpp_namespace)
8259 {
8260 /*
8261 * Looking for C++ namespace, need to look further
8262 * back.
8263 */
8264 if (curwin->w_cursor.lnum == ourscope)
8265 continue;
8266
8267 if (curwin->w_cursor.lnum == 0
8268 || curwin->w_cursor.lnum
8269 < ourscope - FIND_NAMESPACE_LIM)
8270 break;
8271
8272 l = ml_get_curline();
8273
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008274 /* If we're in a comment or raw string now, skip
8275 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008276 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008277 if (trypos != NULL)
8278 {
8279 curwin->w_cursor.lnum = trypos->lnum + 1;
8280 curwin->w_cursor.col = 0;
8281 continue;
8282 }
8283
8284 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008285 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8286 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008287 continue;
8288
8289 /* Finally the actual check for "namespace". */
8290 if (cin_is_cpp_namespace(l))
8291 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008292 amount += curbuf->b_ind_cpp_namespace
8293 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008294 break;
8295 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008296 else if (cin_is_cpp_extern_c(l))
8297 {
8298 amount += curbuf->b_ind_cpp_extern_c
8299 - added_to_amount;
8300 break;
8301 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008302
8303 if (cin_nocode(l))
8304 continue;
8305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307 break;
8308 }
8309
8310 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008311 * If we're in a comment or raw string now, skip to the start
8312 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008314 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {
8316 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008317 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 continue;
8319 }
8320
8321 l = ml_get_curline();
8322
8323 /*
8324 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008325 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008327 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (iscase || cin_isscopedecl(l))
8329 {
8330 /* we are only looking for cpp base class
8331 * declaration/initialization any longer */
8332 if (lookfor == LOOKFOR_CPP_BASECLASS)
8333 break;
8334
8335 /* When looking for a "do" we are not interested in
8336 * labels. */
8337 if (whilelevel > 0)
8338 continue;
8339
8340 /*
8341 * case xx:
8342 * c = 99 + <- this indent plus continuation
8343 *-> here;
8344 */
8345 if (lookfor == LOOKFOR_UNTERM
8346 || lookfor == LOOKFOR_ENUM_OR_INIT)
8347 {
8348 if (cont_amount > 0)
8349 amount = cont_amount;
8350 else
8351 amount += ind_continuation;
8352 break;
8353 }
8354
8355 /*
8356 * case xx: <- line up with this case
8357 * x = 333;
8358 * case yy:
8359 */
8360 if ( (iscase && lookfor == LOOKFOR_CASE)
8361 || (iscase && lookfor_break)
8362 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8363 {
8364 /*
8365 * Check that this case label is not for another
8366 * switch()
8367 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008368 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008369 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {
8371 amount = get_indent(); /* XXX */
8372 break;
8373 }
8374 continue;
8375 }
8376
8377 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8378
8379 /*
8380 * case xx: if (cond) <- line up with this if
8381 * y = y + 1;
8382 * -> s = 99;
8383 *
8384 * case xx:
8385 * if (cond) <- line up with this line
8386 * y = y + 1;
8387 * -> s = 99;
8388 */
8389 if (lookfor == LOOKFOR_TERM)
8390 {
8391 if (n)
8392 amount = n;
8393
8394 if (!lookfor_break)
8395 break;
8396 }
8397
8398 /*
8399 * case xx: x = x + 1; <- line up with this x
8400 * -> y = y + 1;
8401 *
8402 * case xx: if (cond) <- line up with this if
8403 * -> y = y + 1;
8404 */
8405 if (n)
8406 {
8407 amount = n;
8408 l = after_label(ml_get_curline());
8409 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008410 {
8411 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008412 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008413 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008414 amount += curbuf->b_ind_level
8415 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 break;
8418 }
8419
8420 /*
8421 * Try to get the indent of a statement before the switch
8422 * label. If nothing is found, line up relative to the
8423 * switch label.
8424 * break; <- may line up with this line
8425 * case xx:
8426 * -> y = 1;
8427 */
8428 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008429 ? curbuf->b_ind_case_code
8430 : curbuf->b_ind_scopedecl_code);
8431 lookfor = curbuf->b_ind_case_break
8432 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 continue;
8434 }
8435
8436 /*
8437 * Looking for a switch() label or C++ scope declaration,
8438 * ignore other lines, skip {}-blocks.
8439 */
8440 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8441 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008442 if (find_last_paren(l, '{', '}')
8443 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008446 curwin->w_cursor.col = 0;
8447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 continue;
8449 }
8450
8451 /*
8452 * Ignore jump labels with nothing after them.
8453 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008454 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {
8456 l = after_label(ml_get_curline());
8457 if (l == NULL || cin_nocode(l))
8458 continue;
8459 }
8460
8461 /*
8462 * Ignore #defines, #if, etc.
8463 * Ignore comment and empty lines.
8464 * (need to get the line again, cin_islabel() may have
8465 * unlocked it)
8466 */
8467 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008468 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 || cin_nocode(l))
8470 continue;
8471
8472 /*
8473 * Are we at the start of a cpp base class declaration or
8474 * constructor initialization?
8475 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008476 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008477 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008478 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008479 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008480 l = ml_get_curline();
8481 }
8482 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {
8484 if (lookfor == LOOKFOR_UNTERM)
8485 {
8486 if (cont_amount > 0)
8487 amount = cont_amount;
8488 else
8489 amount += ind_continuation;
8490 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008491 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008493 /* Need to find start of the declaration. */
8494 lookfor = LOOKFOR_UNTERM;
8495 ind_continuation = 0;
8496 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 }
8498 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008499 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008500 amount = get_baseclass_amount(
8501 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 break;
8503 }
8504 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8505 {
8506 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008507 * declaration or initialization before the opening brace.
8508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 if (cin_isterminated(l, TRUE, FALSE))
8510 break;
8511 else
8512 continue;
8513 }
8514
8515 /*
8516 * What happens next depends on the line being terminated.
8517 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008518 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 * 123,
8520 * sizeof
8521 * here
8522 * Otherwise check whether it is a enumeration or structure
8523 * initialisation (not indented) or a variable declaration
8524 * (indented).
8525 */
8526 terminated = cin_isterminated(l, FALSE, TRUE);
8527
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008528 if (js_cur_has_key)
8529 {
8530 js_cur_has_key = 0; /* only check the first line */
8531 if (curbuf->b_ind_js && terminated == ',')
8532 {
8533 /* For Javascript we might be inside an object:
8534 * key: something, <- align with this
8535 * key: something
8536 * or:
8537 * key: something + <- align with this
8538 * something,
8539 * key: something
8540 */
8541 lookfor = LOOKFOR_JS_KEY;
8542 }
8543 }
8544 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8545 {
8546 amount = get_indent();
8547 break;
8548 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008549 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008550 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008551 if (tryposBrace != NULL && tryposBrace->lnum
8552 >= curwin->w_cursor.lnum)
8553 break;
8554 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008555 /* line below current line is the one that starts a
8556 * (possibly broken) line ending in a comma. */
8557 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008558 else
8559 {
8560 amount = get_indent();
8561 if (curwin->w_cursor.lnum - 1 == ourscope)
8562 /* line above is start of the scope, thus current
8563 * line is the one that stars a (possibly broken)
8564 * line ending in a comma. */
8565 break;
8566 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008567 }
8568
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8570 && terminated == ','))
8571 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008572 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8573 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008574 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 /*
8576 * if we're in the middle of a paren thing,
8577 * go back to the line that starts it so
8578 * we can get the right prevailing indent
8579 * if ( foo &&
8580 * bar )
8581 */
8582 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008583 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008585 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 */
8587 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008588 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008589 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8590 || (trypos->lnum == tryposBrace->lnum
8591 && trypos->col < tryposBrace->col)))
8592 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
8594 /*
8595 * If we are looking for ',', we also look for matching
8596 * braces.
8597 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008598 if (trypos == NULL && terminated == ','
8599 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008600 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
8602 if (trypos != NULL)
8603 {
8604 /*
8605 * Check if we are on a case label now. This is
8606 * handled above.
8607 * case xx: if ( asdf &&
8608 * asdf)
8609 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008610 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008612 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {
8614 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008615 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 continue;
8617 }
8618 }
8619
8620 /*
8621 * Skip over continuation lines to find the one to get the
8622 * indent from
8623 * char *usethis = "bla\
8624 * bla",
8625 * here;
8626 */
8627 if (terminated == ',')
8628 {
8629 while (curwin->w_cursor.lnum > 1)
8630 {
8631 l = ml_get(curwin->w_cursor.lnum - 1);
8632 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8633 break;
8634 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008635 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 }
8637 }
8638
8639 /*
8640 * Get indent and pointer to text for current line,
8641 * ignoring any jump label. XXX
8642 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008643 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008644 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008645 else
8646 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 /*
8648 * If this is just above the line we are indenting, and it
8649 * starts with a '{', line it up with this line.
8650 * while (not)
8651 * -> {
8652 * }
8653 */
8654 if (terminated != ',' && lookfor != LOOKFOR_TERM
8655 && theline[0] == '{')
8656 {
8657 amount = cur_amount;
8658 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008659 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 * doesn't start with a '{', which must have a match
8661 * in the same line (scope is the same). Probably:
8662 * { 1, 2 },
8663 * -> { 3, 4 }
8664 */
8665 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008666 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008668 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 {
8670 /* have to look back, whether it is a cpp base
8671 * class declaration or initialization */
8672 lookfor = LOOKFOR_CPP_BASECLASS;
8673 continue;
8674 }
8675 break;
8676 }
8677
8678 /*
8679 * Check if we are after an "if", "while", etc.
8680 * Also allow " } else".
8681 */
8682 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8683 {
8684 /*
8685 * Found an unterminated line after an if (), line up
8686 * with the last one.
8687 * if (cond)
8688 * 100 +
8689 * -> here;
8690 */
8691 if (lookfor == LOOKFOR_UNTERM
8692 || lookfor == LOOKFOR_ENUM_OR_INIT)
8693 {
8694 if (cont_amount > 0)
8695 amount = cont_amount;
8696 else
8697 amount += ind_continuation;
8698 break;
8699 }
8700
8701 /*
8702 * If this is just above the line we are indenting, we
8703 * are finished.
8704 * while (not)
8705 * -> here;
8706 * Otherwise this indent can be used when the line
8707 * before this is terminated.
8708 * yyy;
8709 * if (stat)
8710 * while (not)
8711 * xxx;
8712 * -> here;
8713 */
8714 amount = cur_amount;
8715 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008716 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 if (lookfor != LOOKFOR_TERM)
8718 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008719 amount += curbuf->b_ind_level
8720 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 break;
8722 }
8723
8724 /*
8725 * Special trick: when expecting the while () after a
8726 * do, line up with the while()
8727 * do
8728 * x = 1;
8729 * -> here
8730 */
8731 l = skipwhite(ml_get_curline());
8732 if (cin_isdo(l))
8733 {
8734 if (whilelevel == 0)
8735 break;
8736 --whilelevel;
8737 }
8738
8739 /*
8740 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008741 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 * Need to use the scope of this "else". XXX
8743 * If whilelevel != 0 continue looking for a "do {".
8744 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008745 if (cin_iselse(l) && whilelevel == 0)
8746 {
8747 /* If we're looking at "} else", let's make sure we
8748 * find the opening brace of the enclosing scope,
8749 * not the one from "if () {". */
8750 if (*l == '}')
8751 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008752 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008753
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008754 if ((trypos = find_start_brace()) == NULL
8755 || find_match(LOOKFOR_IF, trypos->lnum)
8756 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008757 break;
8758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 }
8760
8761 /*
8762 * If we're below an unterminated line that is not an
8763 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008764 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 * the line before this one.
8766 */
8767 else
8768 {
8769 /*
8770 * Found two unterminated lines on a row, line up with
8771 * the last one.
8772 * c = 99 +
8773 * 100 +
8774 * -> here;
8775 */
8776 if (lookfor == LOOKFOR_UNTERM)
8777 {
8778 /* When line ends in a comma add extra indent */
8779 if (terminated == ',')
8780 amount += ind_continuation;
8781 break;
8782 }
8783
8784 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8785 {
8786 /* Found two lines ending in ',', lineup with the
8787 * lowest one, but check for cpp base class
8788 * declaration/initialization, if it is an
8789 * opening brace or we are looking just for
8790 * enumerations/initializations. */
8791 if (terminated == ',')
8792 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008793 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 break;
8795
8796 lookfor = LOOKFOR_CPP_BASECLASS;
8797 continue;
8798 }
8799
8800 /* Ignore unterminated lines in between, but
8801 * reduce indent. */
8802 if (amount > cur_amount)
8803 amount = cur_amount;
8804 }
8805 else
8806 {
8807 /*
8808 * Found first unterminated line on a row, may
8809 * line up with this line, remember its indent
8810 * 100 +
8811 * -> here;
8812 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008813 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008815
8816 n = (int)STRLEN(l);
8817 if (terminated == ',' && (*skipwhite(l) == ']'
8818 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820
8821 /*
8822 * If previous line ends in ',', check whether we
8823 * are in an initialization or enum
8824 * struct xxx =
8825 * {
8826 * sizeof a,
8827 * 124 };
8828 * or a normal possible continuation line.
8829 * but only, of no other statement has been found
8830 * yet.
8831 */
8832 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8833 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008834 if (curbuf->b_ind_js)
8835 {
8836 /* Search for a line ending in a comma
8837 * and line up with the line below it
8838 * (could be the current line).
8839 * some = [
8840 * 1, <- line up here
8841 * 2,
8842 * some = [
8843 * 3 + <- line up here
8844 * 4 *
8845 * 5,
8846 * 6,
8847 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008848 if (cin_iscomment(skipwhite(l)))
8849 break;
8850 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008851 trypos = find_match_char('[',
8852 curbuf->b_ind_maxparen);
8853 if (trypos != NULL)
8854 {
8855 if (trypos->lnum
8856 == curwin->w_cursor.lnum - 1)
8857 {
8858 /* Current line is first inside
8859 * [], line up with it. */
8860 break;
8861 }
8862 ourscope = trypos->lnum;
8863 }
8864 }
8865 else
8866 {
8867 lookfor = LOOKFOR_ENUM_OR_INIT;
8868 cont_amount = cin_first_id_amount();
8869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 }
8871 else
8872 {
8873 if (lookfor == LOOKFOR_INITIAL
8874 && *l != NUL
8875 && l[STRLEN(l) - 1] == '\\')
8876 /* XXX */
8877 cont_amount = cin_get_equal_amount(
8878 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008879 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008880 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008881 && lookfor != LOOKFOR_COMMA
8882 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 lookfor = LOOKFOR_UNTERM;
8884 }
8885 }
8886 }
8887 }
8888
8889 /*
8890 * Check if we are after a while (cond);
8891 * If so: Ignore until the matching "do".
8892 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008893 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 {
8895 /*
8896 * Found an unterminated line after a while ();, line up
8897 * with the last one.
8898 * while (cond);
8899 * 100 + <- line up with this one
8900 * -> here;
8901 */
8902 if (lookfor == LOOKFOR_UNTERM
8903 || lookfor == LOOKFOR_ENUM_OR_INIT)
8904 {
8905 if (cont_amount > 0)
8906 amount = cont_amount;
8907 else
8908 amount += ind_continuation;
8909 break;
8910 }
8911
8912 if (whilelevel == 0)
8913 {
8914 lookfor = LOOKFOR_TERM;
8915 amount = get_indent(); /* XXX */
8916 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008917 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919 ++whilelevel;
8920 }
8921
8922 /*
8923 * We are after a "normal" statement.
8924 * If we had another statement we can stop now and use the
8925 * indent of that other statement.
8926 * Otherwise the indent of the current statement may be used,
8927 * search backwards for the next "normal" statement.
8928 */
8929 else
8930 {
8931 /*
8932 * Skip single break line, if before a switch label. It
8933 * may be lined up with the case label.
8934 */
8935 if (lookfor == LOOKFOR_NOBREAK
8936 && cin_isbreak(skipwhite(ml_get_curline())))
8937 {
8938 lookfor = LOOKFOR_ANY;
8939 continue;
8940 }
8941
8942 /*
8943 * Handle "do {" line.
8944 */
8945 if (whilelevel > 0)
8946 {
8947 l = cin_skipcomment(ml_get_curline());
8948 if (cin_isdo(l))
8949 {
8950 amount = get_indent(); /* XXX */
8951 --whilelevel;
8952 continue;
8953 }
8954 }
8955
8956 /*
8957 * Found a terminated line above an unterminated line. Add
8958 * the amount for a continuation line.
8959 * x = 1;
8960 * y = foo +
8961 * -> here;
8962 * or
8963 * int x = 1;
8964 * int foo,
8965 * -> here;
8966 */
8967 if (lookfor == LOOKFOR_UNTERM
8968 || lookfor == LOOKFOR_ENUM_OR_INIT)
8969 {
8970 if (cont_amount > 0)
8971 amount = cont_amount;
8972 else
8973 amount += ind_continuation;
8974 break;
8975 }
8976
8977 /*
8978 * Found a terminated line above a terminated line or "if"
8979 * etc. line. Use the amount of the line below us.
8980 * x = 1; x = 1;
8981 * if (asdf) y = 2;
8982 * while (asdf) ->here;
8983 * here;
8984 * ->foo;
8985 */
8986 if (lookfor == LOOKFOR_TERM)
8987 {
8988 if (!lookfor_break && whilelevel == 0)
8989 break;
8990 }
8991
8992 /*
8993 * First line above the one we're indenting is terminated.
8994 * To know what needs to be done look further backward for
8995 * a terminated line.
8996 */
8997 else
8998 {
8999 /*
9000 * position the cursor over the rightmost paren, so
9001 * that matching it will take us back to the start of
9002 * the line. Helps for:
9003 * func(asdr,
9004 * asdfasdf);
9005 * here;
9006 */
9007term_again:
9008 l = ml_get_curline();
9009 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009010 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009011 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 {
9013 /*
9014 * Check if we are on a case label now. This is
9015 * handled above.
9016 * case xx: if ( asdf &&
9017 * asdf)
9018 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009019 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009021 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 {
9023 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009024 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 continue;
9026 }
9027 }
9028
9029 /* When aligning with the case statement, don't align
9030 * with a statement after it.
9031 * case 1: { <-- don't use this { position
9032 * stat;
9033 * }
9034 * case 2:
9035 * stat;
9036 * }
9037 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009038 iscase = (curbuf->b_ind_keep_case_label
9039 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
9041 /*
9042 * Get indent and pointer to text for current line,
9043 * ignoring any jump label.
9044 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009045 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
9047 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009048 amount += curbuf->b_ind_open_extra;
9049 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009050 l = skipwhite(l);
9051 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009052 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9054
9055 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009056 * When a terminated line starts with "else" skip to
9057 * the matching "if":
9058 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009059 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009060 * Need to use the scope of this "else". XXX
9061 * If whilelevel != 0 continue looking for a "do {".
9062 */
9063 if (lookfor == LOOKFOR_TERM
9064 && *l != '}'
9065 && cin_iselse(l)
9066 && whilelevel == 0)
9067 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009068 if ((trypos = find_start_brace()) == NULL
9069 || find_match(LOOKFOR_IF, trypos->lnum)
9070 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009071 break;
9072 continue;
9073 }
9074
9075 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 * If we're at the end of a block, skip to the start of
9077 * that block.
9078 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009079 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009080 if (find_last_paren(l, '{', '}') /* XXX */
9081 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009083 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 /* if not "else {" check for terminated again */
9085 /* but skip block for "} else {" */
9086 l = cin_skipcomment(ml_get_curline());
9087 if (*l == '}' || !cin_iselse(l))
9088 goto term_again;
9089 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009090 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 }
9092 }
9093 }
9094 }
9095 }
9096 }
9097
9098 /* add extra indent for a comment */
9099 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009100 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009101
9102 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009103 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9104 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009105
9106 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009108
9109 /*
9110 * ok -- we're not inside any sort of structure at all!
9111 *
9112 * This means we're at the top level, and everything should
9113 * basically just match where the previous line is, except
9114 * for the lines immediately following a function declaration,
9115 * which are K&R-style parameters and need to be indented.
9116 *
9117 * if our line starts with an open brace, forget about any
9118 * prevailing indent and make sure it looks like the start
9119 * of a function
9120 */
9121
9122 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009124 amount = curbuf->b_ind_first_open;
9125 goto theend;
9126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009128 /*
9129 * If the NEXT line is a function declaration, the current
9130 * line needs to be indented as a function type spec.
9131 * Don't do this if the current line looks like a comment or if the
9132 * current line is terminated, ie. ends in ';', or if the current line
9133 * contains { or }: "void f() {\n if (1)"
9134 */
9135 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9136 && !cin_nocode(theline)
9137 && vim_strchr(theline, '{') == NULL
9138 && vim_strchr(theline, '}') == NULL
9139 && !cin_ends_in(theline, (char_u *)":", NULL)
9140 && !cin_ends_in(theline, (char_u *)",", NULL)
9141 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9142 cur_curpos.lnum + 1)
9143 && !cin_isterminated(theline, FALSE, TRUE))
9144 {
9145 amount = curbuf->b_ind_func_type;
9146 goto theend;
9147 }
9148
9149 /* search backwards until we find something we recognize */
9150 amount = 0;
9151 curwin->w_cursor = cur_curpos;
9152 while (curwin->w_cursor.lnum > 1)
9153 {
9154 curwin->w_cursor.lnum--;
9155 curwin->w_cursor.col = 0;
9156
9157 l = ml_get_curline();
9158
9159 /*
9160 * If we're in a comment or raw string now, skip to the start
9161 * of it.
9162 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009163 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009165 curwin->w_cursor.lnum = trypos->lnum + 1;
9166 curwin->w_cursor.col = 0;
9167 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 }
9169
9170 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009171 * Are we at the start of a cpp base class declaration or
9172 * constructor initialization?
9173 */ /* XXX */
9174 n = FALSE;
9175 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009177 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9178 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009180 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009182 /* XXX */
9183 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9184 break;
9185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009187 /*
9188 * Skip preprocessor directives and blank lines.
9189 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009190 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009191 continue;
9192
9193 if (cin_nocode(l))
9194 continue;
9195
9196 /*
9197 * If the previous line ends in ',', use one level of
9198 * indentation:
9199 * int foo,
9200 * bar;
9201 * do this before checking for '}' in case of eg.
9202 * enum foobar
9203 * {
9204 * ...
9205 * } foo,
9206 * bar;
9207 */
9208 n = 0;
9209 if (cin_ends_in(l, (char_u *)",", NULL)
9210 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9211 {
9212 /* take us back to opening paren */
9213 if (find_last_paren(l, '(', ')')
9214 && (trypos = find_match_paren(
9215 curbuf->b_ind_maxparen)) != NULL)
9216 curwin->w_cursor = *trypos;
9217
9218 /* For a line ending in ',' that is a continuation line go
9219 * back to the first line with a backslash:
9220 * char *foo = "bla\
9221 * bla",
9222 * here;
9223 */
9224 while (n == 0 && curwin->w_cursor.lnum > 1)
9225 {
9226 l = ml_get(curwin->w_cursor.lnum - 1);
9227 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9228 break;
9229 --curwin->w_cursor.lnum;
9230 curwin->w_cursor.col = 0;
9231 }
9232
9233 amount = get_indent(); /* XXX */
9234
9235 if (amount == 0)
9236 amount = cin_first_id_amount();
9237 if (amount == 0)
9238 amount = ind_continuation;
9239 break;
9240 }
9241
9242 /*
9243 * If the line looks like a function declaration, and we're
9244 * not in a comment, put it the left margin.
9245 */
9246 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9247 break;
9248 l = ml_get_curline();
9249
9250 /*
9251 * Finding the closing '}' of a previous function. Put
9252 * current line at the left margin. For when 'cino' has "fs".
9253 */
9254 if (*skipwhite(l) == '}')
9255 break;
9256
9257 /* (matching {)
9258 * If the previous line ends on '};' (maybe followed by
9259 * comments) align at column 0. For example:
9260 * char *string_array[] = { "foo",
9261 * / * x * / "b};ar" }; / * foobar * /
9262 */
9263 if (cin_ends_in(l, (char_u *)"};", NULL))
9264 break;
9265
9266 /*
9267 * If the previous line ends on '[' we are probably in an
9268 * array constant:
9269 * something = [
9270 * 234, <- extra indent
9271 */
9272 if (cin_ends_in(l, (char_u *)"[", NULL))
9273 {
9274 amount = get_indent() + ind_continuation;
9275 break;
9276 }
9277
9278 /*
9279 * Find a line only has a semicolon that belongs to a previous
9280 * line ending in '}', e.g. before an #endif. Don't increase
9281 * indent then.
9282 */
9283 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9284 {
9285 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286
9287 while (curwin->w_cursor.lnum > 1)
9288 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009289 look = ml_get(--curwin->w_cursor.lnum);
9290 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009291 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009293 }
9294 if (curwin->w_cursor.lnum > 0
9295 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009298 curwin->w_cursor = curpos_save;
9299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009301 /*
9302 * If the PREVIOUS line is a function declaration, the current
9303 * line (and the ones that follow) needs to be indented as
9304 * parameters.
9305 */
9306 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9307 {
9308 amount = curbuf->b_ind_param;
9309 break;
9310 }
9311
9312 /*
9313 * If the previous line ends in ';' and the line before the
9314 * previous line ends in ',' or '\', ident to column zero:
9315 * int foo,
9316 * bar;
9317 * indent_to_0 here;
9318 */
9319 if (cin_ends_in(l, (char_u *)";", NULL))
9320 {
9321 l = ml_get(curwin->w_cursor.lnum - 1);
9322 if (cin_ends_in(l, (char_u *)",", NULL)
9323 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9324 break;
9325 l = ml_get_curline();
9326 }
9327
9328 /*
9329 * Doesn't look like anything interesting -- so just
9330 * use the indent of this line.
9331 *
9332 * Position the cursor over the rightmost paren, so that
9333 * matching it will take us back to the start of the line.
9334 */
9335 find_last_paren(l, '(', ')');
9336
9337 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9338 curwin->w_cursor = *trypos;
9339 amount = get_indent(); /* XXX */
9340 break;
9341 }
9342
9343 /* add extra indent for a comment */
9344 if (cin_iscomment(theline))
9345 amount += curbuf->b_ind_comment;
9346
9347 /* add extra indent if the previous line ended in a backslash:
9348 * "asdfasdf\
9349 * here";
9350 * char *foo = "asdf\
9351 * here";
9352 */
9353 if (cur_curpos.lnum > 1)
9354 {
9355 l = ml_get(cur_curpos.lnum - 1);
9356 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9357 {
9358 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9359 if (cur_amount > 0)
9360 amount = cur_amount;
9361 else if (cur_amount == 0)
9362 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 }
9364 }
9365
9366theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009367 if (amount < 0)
9368 amount = 0;
9369
9370laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 /* put the cursor back where it belongs */
9372 curwin->w_cursor = cur_curpos;
9373
9374 vim_free(linecopy);
9375
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 return amount;
9377}
9378
9379 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009380find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 char_u *look;
9383 pos_T *theirscope;
9384 char_u *mightbeif;
9385 int elselevel;
9386 int whilelevel;
9387
9388 if (lookfor == LOOKFOR_IF)
9389 {
9390 elselevel = 1;
9391 whilelevel = 0;
9392 }
9393 else
9394 {
9395 elselevel = 0;
9396 whilelevel = 1;
9397 }
9398
9399 curwin->w_cursor.col = 0;
9400
9401 while (curwin->w_cursor.lnum > ourscope + 1)
9402 {
9403 curwin->w_cursor.lnum--;
9404 curwin->w_cursor.col = 0;
9405
9406 look = cin_skipcomment(ml_get_curline());
9407 if (cin_iselse(look)
9408 || cin_isif(look)
9409 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009410 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 {
9412 /*
9413 * if we've gone outside the braces entirely,
9414 * we must be out of scope...
9415 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009416 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (theirscope == NULL)
9418 break;
9419
9420 /*
9421 * and if the brace enclosing this is further
9422 * back than the one enclosing the else, we're
9423 * out of luck too.
9424 */
9425 if (theirscope->lnum < ourscope)
9426 break;
9427
9428 /*
9429 * and if they're enclosed in a *deeper* brace,
9430 * then we can ignore it because it's in a
9431 * different scope...
9432 */
9433 if (theirscope->lnum > ourscope)
9434 continue;
9435
9436 /*
9437 * if it was an "else" (that's not an "else if")
9438 * then we need to go back to another if, so
9439 * increment elselevel
9440 */
9441 look = cin_skipcomment(ml_get_curline());
9442 if (cin_iselse(look))
9443 {
9444 mightbeif = cin_skipcomment(look + 4);
9445 if (!cin_isif(mightbeif))
9446 ++elselevel;
9447 continue;
9448 }
9449
9450 /*
9451 * if it was a "while" then we need to go back to
9452 * another "do", so increment whilelevel. XXX
9453 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009454 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 {
9456 ++whilelevel;
9457 continue;
9458 }
9459
9460 /* If it's an "if" decrement elselevel */
9461 look = cin_skipcomment(ml_get_curline());
9462 if (cin_isif(look))
9463 {
9464 elselevel--;
9465 /*
9466 * When looking for an "if" ignore "while"s that
9467 * get in the way.
9468 */
9469 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9470 whilelevel = 0;
9471 }
9472
9473 /* If it's a "do" decrement whilelevel */
9474 if (cin_isdo(look))
9475 whilelevel--;
9476
9477 /*
9478 * if we've used up all the elses, then
9479 * this must be the if that we want!
9480 * match the indent level of that if.
9481 */
9482 if (elselevel <= 0 && whilelevel <= 0)
9483 {
9484 return OK;
9485 }
9486 }
9487 }
9488 return FAIL;
9489}
9490
9491# if defined(FEAT_EVAL) || defined(PROTO)
9492/*
9493 * Get indent level from 'indentexpr'.
9494 */
9495 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009496get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009498 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009499 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009500 pos_T save_pos;
9501 colnr_T save_curswant;
9502 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009504 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9505 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009507 /* Save and restore cursor position and curswant, in case it was changed
9508 * via :normal commands */
9509 save_pos = curwin->w_cursor;
9510 save_curswant = curwin->w_curswant;
9511 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009513 if (use_sandbox)
9514 ++sandbox;
9515 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009516
9517 /* Need to make a copy, the 'indentexpr' option could be changed while
9518 * evaluating it. */
9519 inde_copy = vim_strsave(curbuf->b_p_inde);
9520 if (inde_copy != NULL)
9521 {
9522 indent = (int)eval_to_number(inde_copy);
9523 vim_free(inde_copy);
9524 }
9525
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009526 if (use_sandbox)
9527 --sandbox;
9528 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529
9530 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9531 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9532 * command. */
9533 save_State = State;
9534 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009535 curwin->w_cursor = save_pos;
9536 curwin->w_curswant = save_curswant;
9537 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 check_cursor();
9539 State = save_State;
9540
9541 /* If there is an error, just keep the current indent. */
9542 if (indent < 0)
9543 indent = get_indent();
9544
9545 return indent;
9546}
9547# endif
9548
9549#endif /* FEAT_CINDENT */
9550
9551#if defined(FEAT_LISP) || defined(PROTO)
9552
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009554lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556 char_u buf[LSIZE];
9557 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009558 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559
9560 while (*word != NUL)
9561 {
9562 (void)copy_option_part(&word, buf, LSIZE, ",");
9563 len = (int)STRLEN(buf);
9564 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9565 return TRUE;
9566 }
9567 return FALSE;
9568}
9569
9570/*
9571 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9572 * The incompatible newer method is quite a bit better at indenting
9573 * code in lisp-like languages than the traditional one; it's still
9574 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9575 *
9576 * TODO:
9577 * Findmatch() should be adapted for lisp, also to make showmatch
9578 * work correctly: now (v5.3) it seems all C/C++ oriented:
9579 * - it does not recognize the #\( and #\) notations as character literals
9580 * - it doesn't know about comments starting with a semicolon
9581 * - it incorrectly interprets '(' as a character literal
9582 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009583 * Update from Sergey Khorev:
9584 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 */
9586 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009587get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009589 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 int amount;
9591 char_u *that;
9592 colnr_T col;
9593 colnr_T firsttry;
9594 int parencount, quotecount;
9595 int vi_lisp;
9596
9597 /* Set vi_lisp to use the vi-compatible method */
9598 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9599
9600 realpos = curwin->w_cursor;
9601 curwin->w_cursor.col = 0;
9602
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009603 if ((pos = findmatch(NULL, '(')) == NULL)
9604 pos = findmatch(NULL, '[');
9605 else
9606 {
9607 paren = *pos;
9608 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009609 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009610 pos = &paren;
9611 }
9612 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 {
9614 /* Extra trick: Take the indent of the first previous non-white
9615 * line that is at the same () level. */
9616 amount = -1;
9617 parencount = 0;
9618
9619 while (--curwin->w_cursor.lnum >= pos->lnum)
9620 {
9621 if (linewhite(curwin->w_cursor.lnum))
9622 continue;
9623 for (that = ml_get_curline(); *that != NUL; ++that)
9624 {
9625 if (*that == ';')
9626 {
9627 while (*(that + 1) != NUL)
9628 ++that;
9629 continue;
9630 }
9631 if (*that == '\\')
9632 {
9633 if (*(that + 1) != NUL)
9634 ++that;
9635 continue;
9636 }
9637 if (*that == '"' && *(that + 1) != NUL)
9638 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009639 while (*++that && *that != '"')
9640 {
9641 /* skipping escaped characters in the string */
9642 if (*that == '\\')
9643 {
9644 if (*++that == NUL)
9645 break;
9646 if (that[1] == NUL)
9647 {
9648 ++that;
9649 break;
9650 }
9651 }
9652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009654 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009656 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 --parencount;
9658 }
9659 if (parencount == 0)
9660 {
9661 amount = get_indent();
9662 break;
9663 }
9664 }
9665
9666 if (amount == -1)
9667 {
9668 curwin->w_cursor.lnum = pos->lnum;
9669 curwin->w_cursor.col = pos->col;
9670 col = pos->col;
9671
9672 that = ml_get_curline();
9673
9674 if (vi_lisp && get_indent() == 0)
9675 amount = 2;
9676 else
9677 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009678 char_u *line = that;
9679
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 amount = 0;
9681 while (*that && col)
9682 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009683 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 col--;
9685 }
9686
9687 /*
9688 * Some keywords require "body" indenting rules (the
9689 * non-standard-lisp ones are Scheme special forms):
9690 *
9691 * (let ((a 1)) instead (let ((a 1))
9692 * (...)) of (...))
9693 */
9694
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009695 if (!vi_lisp && (*that == '(' || *that == '[')
9696 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 amount += 2;
9698 else
9699 {
9700 that++;
9701 amount++;
9702 firsttry = amount;
9703
Bram Moolenaar1c465442017-03-12 20:10:05 +01009704 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009706 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 ++that;
9708 }
9709
9710 if (*that && *that != ';') /* not a comment line */
9711 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009712 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009714 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 firsttry++;
9716
9717 parencount = 0;
9718 quotecount = 0;
9719
9720 if (vi_lisp
9721 || (*that != '"'
9722 && *that != '\''
9723 && *that != '#'
9724 && (*that < '0' || *that > '9')))
9725 {
9726 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009727 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 || quotecount
9729 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009730 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 && !quotecount
9732 && !parencount
9733 && vi_lisp)))
9734 {
9735 if (*that == '"')
9736 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009737 if ((*that == '(' || *that == '[')
9738 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009740 if ((*that == ')' || *that == ']')
9741 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 --parencount;
9743 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009744 amount += lbr_chartabsize_adv(
9745 line, &that, (colnr_T)amount);
9746 amount += lbr_chartabsize_adv(
9747 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 }
9749 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009750 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009752 amount += lbr_chartabsize(
9753 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 that++;
9755 }
9756 if (!*that || *that == ';')
9757 amount = firsttry;
9758 }
9759 }
9760 }
9761 }
9762 }
9763 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009764 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765
9766 curwin->w_cursor = realpos;
9767
9768 return amount;
9769}
9770#endif /* FEAT_LISP */
9771
9772 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009773prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009775#if defined(SIGHUP) && defined(SIG_IGN)
9776 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9777 * makes Vim exit and then handling SIGHUP causes various reentrance
9778 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009779 signal(SIGHUP, SIG_IGN);
9780#endif
9781
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782#ifdef FEAT_GUI
9783 if (gui.in_use)
9784 {
9785 gui.dying = TRUE;
9786 out_trash(); /* trash any pending output */
9787 }
9788 else
9789#endif
9790 {
9791 windgoto((int)Rows - 1, 0);
9792
9793 /*
9794 * Switch terminal mode back now, so messages end up on the "normal"
9795 * screen (if there are two screens).
9796 */
9797 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009798 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 out_flush();
9800 }
9801}
9802
9803/*
9804 * Preserve files and exit.
9805 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009806 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9807 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 */
9809 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009810preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812 buf_T *buf;
9813
9814 prepare_to_exit();
9815
Bram Moolenaar4770d092006-01-12 23:22:24 +00009816 /* Setting this will prevent free() calls. That avoids calling free()
9817 * recursively when free() was invoked with a bad pointer. */
9818 really_exiting = TRUE;
9819
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 out_str(IObuff);
9821 screen_start(); /* don't know where cursor is now */
9822 out_flush();
9823
9824 ml_close_notmod(); /* close all not-modified buffers */
9825
Bram Moolenaar29323592016-07-24 22:04:11 +02009826 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 {
9828 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9829 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009830 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 screen_start(); /* don't know where cursor is now */
9832 out_flush();
9833 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9834 break;
9835 }
9836 }
9837
9838 ml_close_all(FALSE); /* close all memfiles, without deleting */
9839
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009840 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841
9842 getout(1);
9843}
9844
9845/*
9846 * return TRUE if "fname" exists.
9847 */
9848 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009849vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009851 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852
9853 if (mch_stat((char *)fname, &st))
9854 return FALSE;
9855 return TRUE;
9856}
9857
9858/*
9859 * Check for CTRL-C pressed, but only once in a while.
9860 * Should be used instead of ui_breakcheck() for functions that check for
9861 * each line in the file. Calling ui_breakcheck() each time takes too much
9862 * time, because it can be a system call.
9863 */
9864
9865#ifndef BREAKCHECK_SKIP
9866# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9867# define BREAKCHECK_SKIP 200
9868# else
9869# define BREAKCHECK_SKIP 32
9870# endif
9871#endif
9872
9873static int breakcheck_count = 0;
9874
9875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009876line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877{
9878 if (++breakcheck_count >= BREAKCHECK_SKIP)
9879 {
9880 breakcheck_count = 0;
9881 ui_breakcheck();
9882 }
9883}
9884
9885/*
9886 * Like line_breakcheck() but check 10 times less often.
9887 */
9888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009889fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890{
9891 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9892 {
9893 breakcheck_count = 0;
9894 ui_breakcheck();
9895 }
9896}
9897
9898/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009899 * Invoke expand_wildcards() for one pattern.
9900 * Expand items like "%:h" before the expansion.
9901 * Returns OK or FAIL.
9902 */
9903 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009904expand_wildcards_eval(
9905 char_u **pat, /* pointer to input pattern */
9906 int *num_file, /* resulting number of files */
9907 char_u ***file, /* array of resulting files */
9908 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009909{
9910 int ret = FAIL;
9911 char_u *eval_pat = NULL;
9912 char_u *exp_pat = *pat;
9913 char_u *ignored_msg;
9914 int usedlen;
9915
9916 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9917 {
9918 ++emsg_off;
9919 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9920 NULL, &ignored_msg, NULL);
9921 --emsg_off;
9922 if (eval_pat != NULL)
9923 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9924 }
9925
9926 if (exp_pat != NULL)
9927 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9928
9929 if (eval_pat != NULL)
9930 {
9931 vim_free(exp_pat);
9932 vim_free(eval_pat);
9933 }
9934
9935 return ret;
9936}
9937
9938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9940 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009941 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 */
9943 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009944expand_wildcards(
9945 int num_pat, /* number of input patterns */
9946 char_u **pat, /* array of input patterns */
9947 int *num_files, /* resulting number of files */
9948 char_u ***files, /* array of resulting files */
9949 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950{
9951 int retval;
9952 int i, j;
9953 char_u *p;
9954 int non_suf_match; /* number without matching suffix */
9955
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009956 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957
9958 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009959 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 return retval;
9961
9962#ifdef FEAT_WILDIGN
9963 /*
9964 * Remove names that match 'wildignore'.
9965 */
9966 if (*p_wig)
9967 {
9968 char_u *ffname;
9969
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009970 /* check all files in (*files)[] */
9971 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009973 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 if (ffname == NULL) /* out of memory */
9975 break;
9976# ifdef VMS
9977 vms_remove_version(ffname);
9978# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009979 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009981 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009982 vim_free((*files)[i]);
9983 for (j = i; j + 1 < *num_files; ++j)
9984 (*files)[j] = (*files)[j + 1];
9985 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 --i;
9987 }
9988 vim_free(ffname);
9989 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009990
9991 /* If the number of matches is now zero, we fail. */
9992 if (*num_files == 0)
9993 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009994 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009995 return FAIL;
9996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 }
9998#endif
9999
10000 /*
10001 * Move the names where 'suffixes' match to the end.
10002 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010003 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 {
10005 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010006 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010008 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 {
10010 /*
10011 * Move the name without matching suffix to the front
10012 * of the list.
10013 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010014 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010016 (*files)[j] = (*files)[j - 1];
10017 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 }
10019 }
10020 }
10021
10022 return retval;
10023}
10024
10025/*
10026 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10027 */
10028 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010029match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 int fnamelen, setsuflen;
10032 char_u *setsuf;
10033#define MAXSUFLEN 30 /* maximum length of a file suffix */
10034 char_u suf_buf[MAXSUFLEN];
10035
10036 fnamelen = (int)STRLEN(fname);
10037 setsuflen = 0;
10038 for (setsuf = p_su; *setsuf; )
10039 {
10040 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010041 if (setsuflen == 0)
10042 {
10043 char_u *tail = gettail(fname);
10044
10045 /* empty entry: match name without a '.' */
10046 if (vim_strchr(tail, '.') == NULL)
10047 {
10048 setsuflen = 1;
10049 break;
10050 }
10051 }
10052 else
10053 {
10054 if (fnamelen >= setsuflen
10055 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10056 (size_t)setsuflen) == 0)
10057 break;
10058 setsuflen = 0;
10059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 }
10061 return (setsuflen != 0);
10062}
10063
10064#if !defined(NO_EXPANDPATH) || defined(PROTO)
10065
10066# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010067static int vim_backtick(char_u *p);
10068static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069# endif
10070
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010071# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072/*
10073 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10074 * it's shared between these systems.
10075 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010076# if defined(PROTO)
10077# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078# else
10079# ifdef __BORLANDC__
10080# define _cdecl _RTLENTRYF
10081# endif
10082# endif
10083
10084/*
10085 * comparison function for qsort in dos_expandpath()
10086 */
10087 static int _cdecl
10088pstrcmp(const void *a, const void *b)
10089{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010090 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091}
10092
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010094 * Recursively expand one path component into all matching files and/or
10095 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 * Return the number of matches found.
10097 * "path" has backslashes before chars that are not to be expanded, starting
10098 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010099 * Return the number of matches found.
10100 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 */
10102 static int
10103dos_expandpath(
10104 garray_T *gap,
10105 char_u *path,
10106 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010107 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010108 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010110 char_u *buf;
10111 char_u *path_end;
10112 char_u *p, *s, *e;
10113 int start_len = gap->ga_len;
10114 char_u *pat;
10115 regmatch_T regmatch;
10116 int starts_with_dot;
10117 int matches;
10118 int len;
10119 int starstar = FALSE;
10120 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 WIN32_FIND_DATA fb;
10122 HANDLE hFind = (HANDLE)0;
10123# ifdef FEAT_MBYTE
10124 WIN32_FIND_DATAW wfb;
10125 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010128 int ok;
10129
10130 /* Expanding "**" may take a long time, check for CTRL-C. */
10131 if (stardepth > 0)
10132 {
10133 ui_breakcheck();
10134 if (got_int)
10135 return 0;
10136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010138 /* Make room for file name. When doing encoding conversion the actual
10139 * length may be quite a bit longer, thus use the maximum possible length. */
10140 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 if (buf == NULL)
10142 return 0;
10143
10144 /*
10145 * Find the first part in the path name that contains a wildcard or a ~1.
10146 * Copy it into buf, including the preceding characters.
10147 */
10148 p = buf;
10149 s = buf;
10150 e = NULL;
10151 path_end = path;
10152 while (*path_end != NUL)
10153 {
10154 /* May ignore a wildcard that has a backslash before it; it will
10155 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10156 if (path_end >= path + wildoff && rem_backslash(path_end))
10157 *p++ = *path_end++;
10158 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10159 {
10160 if (e != NULL)
10161 break;
10162 s = p + 1;
10163 }
10164 else if (path_end >= path + wildoff
10165 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10166 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010167# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 if (has_mbyte)
10169 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010170 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 STRNCPY(p, path_end, len);
10172 p += len;
10173 path_end += len;
10174 }
10175 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 *p++ = *path_end++;
10178 }
10179 e = p;
10180 *e = NUL;
10181
10182 /* now we have one wildcard component between s and e */
10183 /* Remove backslashes between "wildoff" and the start of the wildcard
10184 * component. */
10185 for (p = buf + wildoff; p < s; ++p)
10186 if (rem_backslash(p))
10187 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010188 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 --e;
10190 --s;
10191 }
10192
Bram Moolenaar231334e2005-07-25 20:46:57 +000010193 /* Check for "**" between "s" and "e". */
10194 for (p = s; p < e; ++p)
10195 if (p[0] == '*' && p[1] == '*')
10196 starstar = TRUE;
10197
Bram Moolenaard82103e2016-01-17 17:04:05 +010010198 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10200 if (pat == NULL)
10201 {
10202 vim_free(buf);
10203 return 0;
10204 }
10205
10206 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010207 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010208 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 regmatch.rm_ic = TRUE; /* Always ignore case */
10210 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010211 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010212 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 vim_free(pat);
10214
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010215 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 {
10217 vim_free(buf);
10218 return 0;
10219 }
10220
10221 /* remember the pattern or file name being looked for */
10222 matchname = vim_strsave(s);
10223
Bram Moolenaar231334e2005-07-25 20:46:57 +000010224 /* If "**" is by itself, this is the first time we encounter it and more
10225 * is following then find matches without any directory. */
10226 if (!didstar && stardepth < 100 && starstar && e - s == 2
10227 && *path_end == '/')
10228 {
10229 STRCPY(s, path_end + 1);
10230 ++stardepth;
10231 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10232 --stardepth;
10233 }
10234
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 /* Scan all files in the directory with "dir/ *.*" */
10236 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237# ifdef FEAT_MBYTE
10238 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10239 {
10240 /* The active codepage differs from 'encoding'. Attempt using the
10241 * wide function. If it fails because it is not implemented fall back
10242 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010243 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 if (wn != NULL)
10245 {
10246 hFind = FindFirstFileW(wn, &wfb);
10247 if (hFind == INVALID_HANDLE_VALUE
10248 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010249 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 }
10251 }
10252
10253 if (wn == NULL)
10254# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010255 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257
10258 while (ok)
10259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260# ifdef FEAT_MBYTE
10261 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010262 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 else
10264# endif
10265 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 /* Ignore entries starting with a dot, unless when asked for. Accept
10267 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010268 if ((p[0] != '.' || starts_with_dot
10269 || ((flags & EW_DODOT)
10270 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010272 || (regmatch.regprog != NULL
10273 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010274 || ((flags & EW_NOTWILD)
10275 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010279
10280 if (starstar && stardepth < 100)
10281 {
10282 /* For "**" in the pattern first go deeper in the tree to
10283 * find matches. */
10284 STRCPY(buf + len, "/**");
10285 STRCPY(buf + len + 3, path_end);
10286 ++stardepth;
10287 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10288 --stardepth;
10289 }
10290
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 STRCPY(buf + len, path_end);
10292 if (mch_has_exp_wildcard(path_end))
10293 {
10294 /* need to expand another component of the path */
10295 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010296 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 }
10298 else
10299 {
10300 /* no more wildcards, check if there is a match */
10301 /* remove backslashes for the remaining components only */
10302 if (*path_end != 0)
10303 backslash_halve(buf + len + 1);
10304 if (mch_getperm(buf) >= 0) /* add existing file */
10305 addfile(gap, buf, flags);
10306 }
10307 }
10308
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309# ifdef FEAT_MBYTE
10310 if (wn != NULL)
10311 {
10312 vim_free(p);
10313 ok = FindNextFileW(hFind, &wfb);
10314 }
10315 else
10316# endif
10317 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318
10319 /* If no more matches and no match was used, try expanding the name
10320 * itself. Finds the long name of a short filename. */
10321 if (!ok && matchname != NULL && gap->ga_len == start_len)
10322 {
10323 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 FindClose(hFind);
10325# ifdef FEAT_MBYTE
10326 if (wn != NULL)
10327 {
10328 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010329 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 if (wn != NULL)
10331 hFind = FindFirstFileW(wn, &wfb);
10332 }
10333 if (wn == NULL)
10334# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010335 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010337 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 }
10339 }
10340
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 FindClose(hFind);
10342# ifdef FEAT_MBYTE
10343 vim_free(wn);
10344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010346 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 vim_free(matchname);
10348
10349 matches = gap->ga_len - start_len;
10350 if (matches > 0)
10351 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10352 sizeof(char_u *), pstrcmp);
10353 return matches;
10354}
10355
10356 int
10357mch_expandpath(
10358 garray_T *gap,
10359 char_u *path,
10360 int flags) /* EW_* flags */
10361{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010362 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010364# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365
Bram Moolenaar231334e2005-07-25 20:46:57 +000010366#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10367 || defined(PROTO)
10368/*
10369 * Unix style wildcard expansion code.
10370 * It's here because it's used both for Unix and Mac.
10371 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010372 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010373pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010374{
10375 return (pathcmp(*(char **)a, *(char **)b, -1));
10376}
10377
10378/*
10379 * Recursively expand one path component into all matching files and/or
10380 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10381 * "path" has backslashes before chars that are not to be expanded, starting
10382 * at "path + wildoff".
10383 * Return the number of matches found.
10384 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10385 */
10386 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010387unix_expandpath(
10388 garray_T *gap,
10389 char_u *path,
10390 int wildoff,
10391 int flags, /* EW_* flags */
10392 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010393{
10394 char_u *buf;
10395 char_u *path_end;
10396 char_u *p, *s, *e;
10397 int start_len = gap->ga_len;
10398 char_u *pat;
10399 regmatch_T regmatch;
10400 int starts_with_dot;
10401 int matches;
10402 int len;
10403 int starstar = FALSE;
10404 static int stardepth = 0; /* depth for "**" expansion */
10405
10406 DIR *dirp;
10407 struct dirent *dp;
10408
10409 /* Expanding "**" may take a long time, check for CTRL-C. */
10410 if (stardepth > 0)
10411 {
10412 ui_breakcheck();
10413 if (got_int)
10414 return 0;
10415 }
10416
10417 /* make room for file name */
10418 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10419 if (buf == NULL)
10420 return 0;
10421
10422 /*
10423 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010424 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010425 * Copy it into "buf", including the preceding characters.
10426 */
10427 p = buf;
10428 s = buf;
10429 e = NULL;
10430 path_end = path;
10431 while (*path_end != NUL)
10432 {
10433 /* May ignore a wildcard that has a backslash before it; it will
10434 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10435 if (path_end >= path + wildoff && rem_backslash(path_end))
10436 *p++ = *path_end++;
10437 else if (*path_end == '/')
10438 {
10439 if (e != NULL)
10440 break;
10441 s = p + 1;
10442 }
10443 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010444 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010445 || (!p_fic && (flags & EW_ICASE)
10446 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010447 e = p;
10448#ifdef FEAT_MBYTE
10449 if (has_mbyte)
10450 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010451 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010452 STRNCPY(p, path_end, len);
10453 p += len;
10454 path_end += len;
10455 }
10456 else
10457#endif
10458 *p++ = *path_end++;
10459 }
10460 e = p;
10461 *e = NUL;
10462
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010463 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010464 /* Remove backslashes between "wildoff" and the start of the wildcard
10465 * component. */
10466 for (p = buf + wildoff; p < s; ++p)
10467 if (rem_backslash(p))
10468 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010469 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010470 --e;
10471 --s;
10472 }
10473
10474 /* Check for "**" between "s" and "e". */
10475 for (p = s; p < e; ++p)
10476 if (p[0] == '*' && p[1] == '*')
10477 starstar = TRUE;
10478
10479 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010480 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010481 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10482 if (pat == NULL)
10483 {
10484 vim_free(buf);
10485 return 0;
10486 }
10487
10488 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010489 if (flags & EW_ICASE)
10490 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10491 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010492 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010493 if (flags & (EW_NOERROR | EW_NOTWILD))
10494 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010495 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010496 if (flags & (EW_NOERROR | EW_NOTWILD))
10497 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010498 vim_free(pat);
10499
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010500 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010501 {
10502 vim_free(buf);
10503 return 0;
10504 }
10505
10506 /* If "**" is by itself, this is the first time we encounter it and more
10507 * is following then find matches without any directory. */
10508 if (!didstar && stardepth < 100 && starstar && e - s == 2
10509 && *path_end == '/')
10510 {
10511 STRCPY(s, path_end + 1);
10512 ++stardepth;
10513 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10514 --stardepth;
10515 }
10516
10517 /* open the directory for scanning */
10518 *s = NUL;
10519 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10520
10521 /* Find all matching entries */
10522 if (dirp != NULL)
10523 {
10524 for (;;)
10525 {
10526 dp = readdir(dirp);
10527 if (dp == NULL)
10528 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010529 if ((dp->d_name[0] != '.' || starts_with_dot
10530 || ((flags & EW_DODOT)
10531 && dp->d_name[1] != NUL
10532 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010533 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10534 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010535 || ((flags & EW_NOTWILD)
10536 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010537 {
10538 STRCPY(s, dp->d_name);
10539 len = STRLEN(buf);
10540
10541 if (starstar && stardepth < 100)
10542 {
10543 /* For "**" in the pattern first go deeper in the tree to
10544 * find matches. */
10545 STRCPY(buf + len, "/**");
10546 STRCPY(buf + len + 3, path_end);
10547 ++stardepth;
10548 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10549 --stardepth;
10550 }
10551
10552 STRCPY(buf + len, path_end);
10553 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10554 {
10555 /* need to expand another component of the path */
10556 /* remove backslashes for the remaining components only */
10557 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10558 }
10559 else
10560 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010561 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010562
Bram Moolenaar231334e2005-07-25 20:46:57 +000010563 /* no more wildcards, check if there is a match */
10564 /* remove backslashes for the remaining components only */
10565 if (*path_end != NUL)
10566 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010567 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010568 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010569 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010570 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010571#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010572 size_t precomp_len = STRLEN(buf)+1;
10573 char_u *precomp_buf =
10574 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010575
Bram Moolenaar231334e2005-07-25 20:46:57 +000010576 if (precomp_buf)
10577 {
10578 mch_memmove(buf, precomp_buf, precomp_len);
10579 vim_free(precomp_buf);
10580 }
10581#endif
10582 addfile(gap, buf, flags);
10583 }
10584 }
10585 }
10586 }
10587
10588 closedir(dirp);
10589 }
10590
10591 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010592 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010593
10594 matches = gap->ga_len - start_len;
10595 if (matches > 0)
10596 qsort(((char_u **)gap->ga_data) + start_len, matches,
10597 sizeof(char_u *), pstrcmp);
10598 return matches;
10599}
10600#endif
10601
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010602#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010603/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010604 * Moves "*psep" back to the previous path separator in "path".
10605 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010606 */
10607 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010608find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010609{
10610 /* skip the current separator */
10611 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010612 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010613
10614 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010615 while (*psep > path)
10616 {
10617 if (vim_ispathsep(**psep))
10618 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010619 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010620 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010621
10622 return FAIL;
10623}
10624
10625/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010626 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10627 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010628 */
10629 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010630is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010631{
10632 int j;
10633 int candidate_len;
10634 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010635 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010636 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010637
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010638 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010639 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010640 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010641 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010642
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010643 candidate_len = (int)STRLEN(maybe_unique);
10644 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010645 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010646 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010647
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010648 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010649 if (fnamecmp(maybe_unique, rival) == 0
10650 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010651 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010652 }
10653
Bram Moolenaar162bd912010-07-28 22:29:10 +020010654 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010655}
10656
10657/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010658 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010659 * paths are expanded to their equivalent fullpath. This includes the "."
10660 * (relative to current buffer directory) and empty path (relative to current
10661 * directory) notations.
10662 *
10663 * TODO: handle upward search (;) and path limiter (**N) notations by
10664 * expanding each into their equivalent path(s).
10665 */
10666 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010667expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010668{
10669 char_u *path_option = *curbuf->b_p_path == NUL
10670 ? p_path : curbuf->b_p_path;
10671 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010672 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010673 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010674
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010675 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010676 return;
10677
10678 while (*path_option != NUL)
10679 {
10680 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10681
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010682 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010683 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010684 /* Relative to current buffer:
10685 * "/path/file" + "." -> "/path/"
10686 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010687 if (curbuf->b_ffname == NULL)
10688 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010689 p = gettail(curbuf->b_ffname);
10690 len = (int)(p - curbuf->b_ffname);
10691 if (len + (int)STRLEN(buf) >= MAXPATHL)
10692 continue;
10693 if (buf[1] == NUL)
10694 buf[len] = NUL;
10695 else
10696 STRMOVE(buf + len, buf + 2);
10697 mch_memmove(buf, curbuf->b_ffname, len);
10698 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010699 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010700 else if (buf[0] == NUL)
10701 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010702 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010703 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010704 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010705 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010706 else if (!mch_isFullName(buf))
10707 {
10708 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010709 len = (int)STRLEN(curdir);
10710 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010711 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010712 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010713 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010714 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010715 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010716 }
10717
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010718 if (ga_grow(gap, 1) == FAIL)
10719 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010720
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010721# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010722 /* Avoid the path ending in a backslash, it fails when a comma is
10723 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010724 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010725 if (buf[len - 1] == '\\')
10726 buf[len - 1] = '/';
10727# endif
10728
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010729 p = vim_strsave(buf);
10730 if (p == NULL)
10731 break;
10732 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010733 }
10734
10735 vim_free(buf);
10736}
10737
10738/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010739 * Returns a pointer to the file or directory name in "fname" that matches the
10740 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010741 *
10742 * path: /foo/bar/baz
10743 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010744 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010745 */
10746 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010747get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010748{
10749 int i;
10750 int maxlen = 0;
10751 char_u **path_part = (char_u **)gap->ga_data;
10752 char_u *cutoff = NULL;
10753
10754 for (i = 0; i < gap->ga_len; i++)
10755 {
10756 int j = 0;
10757
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010758 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010759# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010760 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10761#endif
10762 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010763 j++;
10764 if (j > maxlen)
10765 {
10766 maxlen = j;
10767 cutoff = &fname[j];
10768 }
10769 }
10770
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010771 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010772 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010773 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010774 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010775
10776 return cutoff;
10777}
10778
10779/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010780 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10781 * that they are unique with respect to each other while conserving the part
10782 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010783 */
10784 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010785uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010786{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010787 int i;
10788 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010789 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010790 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010791 char_u *pat;
10792 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010793 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010794 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010795 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010796 char_u **in_curdir = NULL;
10797 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010798
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010799 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010800 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010801
10802 /*
10803 * We need to prepend a '*' at the beginning of file_pattern so that the
10804 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010805 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010806 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010807 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010808 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010809 if (file_pattern == NULL)
10810 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010811 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010812 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010813 STRCAT(file_pattern, pattern);
10814 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10815 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010816 if (pat == NULL)
10817 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010818
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010819 regmatch.rm_ic = TRUE; /* always ignore case */
10820 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10821 vim_free(pat);
10822 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010823 return;
10824
Bram Moolenaar162bd912010-07-28 22:29:10 +020010825 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010826 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010827 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010828 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010829
10830 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010831 if (in_curdir == NULL)
10832 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010833
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010834 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010835 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010836 char_u *path = fnames[i];
10837 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010838 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010839 char_u *pathsep_p;
10840 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010841
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010842 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010843 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010844 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010845 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010846 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010847
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010848 /* Shorten the filename while maintaining its uniqueness */
10849 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010850
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010851 /* Don't assume all files can be reached without path when search
10852 * pattern starts with star star slash, so only remove path_cutoff
10853 * when possible. */
10854 if (pattern[0] == '*' && pattern[1] == '*'
10855 && vim_ispathsep_nocolon(pattern[2])
10856 && path_cutoff != NULL
10857 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10858 && is_unique(path_cutoff, gap, i))
10859 {
10860 sort_again = TRUE;
10861 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10862 }
10863 else
10864 {
10865 /* Here all files can be reached without path, so get shortest
10866 * unique path. We start at the end of the path. */
10867 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010868
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010869 while (find_previous_pathsep(path, &pathsep_p))
10870 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10871 && is_unique(pathsep_p + 1, gap, i)
10872 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10873 {
10874 sort_again = TRUE;
10875 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10876 break;
10877 }
10878 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010879
10880 if (mch_isFullName(path))
10881 {
10882 /*
10883 * Last resort: shorten relative to curdir if possible.
10884 * 'possible' means:
10885 * 1. It is under the current directory.
10886 * 2. The result is actually shorter than the original.
10887 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010888 * Before curdir After
10889 * /foo/bar/file.txt /foo/bar ./file.txt
10890 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10891 * /file.txt / /file.txt
10892 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010893 */
10894 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010895 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010896#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010897 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010898 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010899 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010900 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010901 && !vim_ispathsep(*short_name)
10902#endif
10903 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010904 {
10905 STRCPY(path, ".");
10906 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010907 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010908 }
10909 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010910 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010911 }
10912
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010913 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010914 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010915 {
10916 char_u *rel_path;
10917 char_u *path = in_curdir[i];
10918
10919 if (path == NULL)
10920 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010921
10922 /* If the {filename} is not unique, change it to ./{filename}.
10923 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010924 short_name = shorten_fname(path, curdir);
10925 if (short_name == NULL)
10926 short_name = path;
10927 if (is_unique(short_name, gap, i))
10928 {
10929 STRCPY(fnames[i], short_name);
10930 continue;
10931 }
10932
10933 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10934 if (rel_path == NULL)
10935 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010936 STRCPY(rel_path, ".");
10937 add_pathsep(rel_path);
10938 STRCAT(rel_path, short_name);
10939
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010940 vim_free(fnames[i]);
10941 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010942 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010943 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010944 }
10945
Bram Moolenaar162bd912010-07-28 22:29:10 +020010946theend:
10947 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010948 if (in_curdir != NULL)
10949 {
10950 for (i = 0; i < gap->ga_len; i++)
10951 vim_free(in_curdir[i]);
10952 vim_free(in_curdir);
10953 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010954 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010955 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010956
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010957 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010958 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010959}
10960
10961/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010962 * Calls globpath() with 'path' values for the given pattern and stores the
10963 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010964 * Returns the total number of matches.
10965 */
10966 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010967expand_in_path(
10968 garray_T *gap,
10969 char_u *pattern,
10970 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010971{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010972 char_u *curdir;
10973 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010974 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010975 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010976
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010977 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010978 return 0;
10979 mch_dirname(curdir, MAXPATHL);
10980
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010981 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010982 expand_path_option(curdir, &path_ga);
10983 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010984 if (path_ga.ga_len == 0)
10985 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010986
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010987 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010988 ga_clear_strings(&path_ga);
10989 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010990 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010991
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010992 if (flags & EW_ICASE)
10993 glob_flags |= WILD_ICASE;
10994 if (flags & EW_ADDSLASH)
10995 glob_flags |= WILD_ADD_SLASH;
10996 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010997 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010998
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010999 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011000}
11001#endif
11002
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011003#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11004/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011005 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11006 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011007 */
11008 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011009remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011010{
11011 int i;
11012 int j;
11013 char_u **fnames = (char_u **)gap->ga_data;
11014
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011015 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011016 for (i = gap->ga_len - 1; i > 0; --i)
11017 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11018 {
11019 vim_free(fnames[i]);
11020 for (j = i + 1; j < gap->ga_len; ++j)
11021 fnames[j - 1] = fnames[j];
11022 --gap->ga_len;
11023 }
11024}
11025#endif
11026
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011027/*
11028 * Return TRUE if "p" contains what looks like an environment variable.
11029 * Allowing for escaping.
11030 */
11031 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011032has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011033{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011034 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011035 {
11036 if (*p == '\\' && p[1] != NUL)
11037 ++p;
11038 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011039#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011040 "$%"
11041#else
11042 "$"
11043#endif
11044 , *p) != NULL)
11045 return TRUE;
11046 }
11047 return FALSE;
11048}
11049
11050#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011051/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011052 * Return TRUE if "p" contains a special wildcard character, one that Vim
11053 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011054 */
11055 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011056has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011057{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011058 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011059 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011060 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011061 if (*p == '\\' && p[1] != NUL)
11062 ++p;
11063 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11064 return TRUE;
11065 }
11066 return FALSE;
11067}
11068#endif
11069
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070/*
11071 * Generic wildcard expansion code.
11072 *
11073 * Characters in "pat" that should not be expanded must be preceded with a
11074 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11075 *
11076 * Return FAIL when no single file was found. In this case "num_file" is not
11077 * set, and "file" may contain an error message.
11078 * Return OK when some files found. "num_file" is set to the number of
11079 * matches, "file" to the array of matches. Call FreeWild() later.
11080 */
11081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011082gen_expand_wildcards(
11083 int num_pat, /* number of input patterns */
11084 char_u **pat, /* array of input patterns */
11085 int *num_file, /* resulting number of files */
11086 char_u ***file, /* array of resulting files */
11087 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089 int i;
11090 garray_T ga;
11091 char_u *p;
11092 static int recursive = FALSE;
11093 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011094 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011095#if defined(FEAT_SEARCHPATH)
11096 int did_expand_in_path = FALSE;
11097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098
11099 /*
11100 * expand_env() is called to expand things like "~user". If this fails,
11101 * it calls ExpandOne(), which brings us back here. In this case, always
11102 * call the machine specific expansion function, if possible. Otherwise,
11103 * return FAIL.
11104 */
11105 if (recursive)
11106#ifdef SPECIAL_WILDCHAR
11107 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11108#else
11109 return FAIL;
11110#endif
11111
11112#ifdef SPECIAL_WILDCHAR
11113 /*
11114 * If there are any special wildcard characters which we cannot handle
11115 * here, call machine specific function for all the expansion. This
11116 * avoids starting the shell for each argument separately.
11117 * For `=expr` do use the internal function.
11118 */
11119 for (i = 0; i < num_pat; i++)
11120 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011121 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122# ifdef VIM_BACKTICK
11123 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11124# endif
11125 )
11126 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11127 }
11128#endif
11129
11130 recursive = TRUE;
11131
11132 /*
11133 * The matching file names are stored in a growarray. Init it empty.
11134 */
11135 ga_init2(&ga, (int)sizeof(char_u *), 30);
11136
11137 for (i = 0; i < num_pat; ++i)
11138 {
11139 add_pat = -1;
11140 p = pat[i];
11141
11142#ifdef VIM_BACKTICK
11143 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011146 if (add_pat == -1)
11147 retval = FAIL;
11148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 else
11150#endif
11151 {
11152 /*
11153 * First expand environment variables, "~/" and "~user/".
11154 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011155 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011157 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 if (p == NULL)
11159 p = pat[i];
11160#ifdef UNIX
11161 /*
11162 * On Unix, if expand_env() can't expand an environment
11163 * variable, use the shell to do that. Discard previously
11164 * found file names and start all over again.
11165 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011166 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 {
11168 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011169 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011171 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 recursive = FALSE;
11173 return i;
11174 }
11175#endif
11176 }
11177
11178 /*
11179 * If there are wildcards: Expand file names and add each match to
11180 * the list. If there is no match, and EW_NOTFOUND is given, add
11181 * the pattern.
11182 * If there are no wildcards: Add the file name if it exists or
11183 * when EW_NOTFOUND is given.
11184 */
11185 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011186 {
11187#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011188 if ((flags & EW_PATH)
11189 && !mch_isFullName(p)
11190 && !(p[0] == '.'
11191 && (vim_ispathsep(p[1])
11192 || (p[1] == '.' && vim_ispathsep(p[2]))))
11193 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011194 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011195 /* :find completion where 'path' is used.
11196 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011197 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011198 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011199 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011200 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011201 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011202 else
11203#endif
11204 add_pat = mch_expandpath(&ga, p, flags);
11205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 }
11207
11208 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11209 {
11210 char_u *t = backslash_halve_save(p);
11211
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11213 * "vim c:/" work. */
11214 if (flags & EW_NOTFOUND)
11215 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 addfile(&ga, t, flags);
11218 vim_free(t);
11219 }
11220
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011221#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011222 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011223 uniquefy_paths(&ga, p);
11224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 if (p != pat[i])
11226 vim_free(p);
11227 }
11228
11229 *num_file = ga.ga_len;
11230 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11231
11232 recursive = FALSE;
11233
Bram Moolenaar336bd622016-01-17 18:23:58 +010011234 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235}
11236
11237# ifdef VIM_BACKTICK
11238
11239/*
11240 * Return TRUE if we can expand this backtick thing here.
11241 */
11242 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011243vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244{
11245 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11246}
11247
11248/*
11249 * Expand an item in `backticks` by executing it as a command.
11250 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011251 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 */
11253 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011254expand_backtick(
11255 garray_T *gap,
11256 char_u *pat,
11257 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258{
11259 char_u *p;
11260 char_u *cmd;
11261 char_u *buffer;
11262 int cnt = 0;
11263 int i;
11264
11265 /* Create the command: lop off the backticks. */
11266 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11267 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011268 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269
11270#ifdef FEAT_EVAL
11271 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011272 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 else
11274#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011275 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011276 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 vim_free(cmd);
11278 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011279 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280
11281 cmd = buffer;
11282 while (*cmd != NUL)
11283 {
11284 cmd = skipwhite(cmd); /* skip over white space */
11285 p = cmd;
11286 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11287 ++p;
11288 /* add an entry if it is not empty */
11289 if (p > cmd)
11290 {
11291 i = *p;
11292 *p = NUL;
11293 addfile(gap, cmd, flags);
11294 *p = i;
11295 ++cnt;
11296 }
11297 cmd = p;
11298 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11299 ++cmd;
11300 }
11301
11302 vim_free(buffer);
11303 return cnt;
11304}
11305# endif /* VIM_BACKTICK */
11306
11307/*
11308 * Add a file to a file list. Accepted flags:
11309 * EW_DIR add directories
11310 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011311 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 * EW_NOTFOUND add even when it doesn't exist
11313 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011314 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 */
11316 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011317addfile(
11318 garray_T *gap,
11319 char_u *f, /* filename */
11320 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321{
11322 char_u *p;
11323 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011324 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011326 /* if the file/dir/link doesn't exist, may not add it */
11327 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011328 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 return;
11330
11331#ifdef FNAME_ILLEGAL
11332 /* if the file/dir contains illegal characters, don't add it */
11333 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11334 return;
11335#endif
11336
11337 isdir = mch_isdir(f);
11338 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11339 return;
11340
Bram Moolenaarb5971142015-03-21 17:32:19 +010011341 /* If the file isn't executable, may not add it. Do accept directories.
11342 * When invoked from expand_shellcmd() do not use $PATH. */
11343 if (!isdir && (flags & EW_EXEC)
11344 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011345 return;
11346
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 /* Make room for another item in the file list. */
11348 if (ga_grow(gap, 1) == FAIL)
11349 return;
11350
11351 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11352 if (p == NULL)
11353 return;
11354
11355 STRCPY(p, f);
11356#ifdef BACKSLASH_IN_FILENAME
11357 slash_adjust(p);
11358#endif
11359 /*
11360 * Append a slash or backslash after directory names if none is present.
11361 */
11362#ifndef DONT_ADD_PATHSEP_TO_DIR
11363 if (isdir && (flags & EW_ADDSLASH))
11364 add_pathsep(p);
11365#endif
11366 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367}
11368#endif /* !NO_EXPANDPATH */
11369
11370#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11371
11372#ifndef SEEK_SET
11373# define SEEK_SET 0
11374#endif
11375#ifndef SEEK_END
11376# define SEEK_END 2
11377#endif
11378
11379/*
11380 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011381 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11382 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 * Returns an allocated string, or NULL for error.
11384 */
11385 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011386get_cmd_output(
11387 char_u *cmd,
11388 char_u *infile, /* optional input file name */
11389 int flags, /* can be SHELL_SILENT */
11390 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391{
11392 char_u *tempname;
11393 char_u *command;
11394 char_u *buffer = NULL;
11395 int len;
11396 int i = 0;
11397 FILE *fd;
11398
11399 if (check_restricted() || check_secure())
11400 return NULL;
11401
11402 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011403 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 {
11405 EMSG(_(e_notmp));
11406 return NULL;
11407 }
11408
11409 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011410 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 if (command == NULL)
11412 goto done;
11413
11414 /*
11415 * Call the shell to execute the command (errors are ignored).
11416 * Don't check timestamps here.
11417 */
11418 ++no_check_timestamps;
11419 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11420 --no_check_timestamps;
11421
11422 vim_free(command);
11423
11424 /*
11425 * read the names from the file into memory
11426 */
11427# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011428 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 fd = mch_fopen((char *)tempname, "r");
11430# else
11431 fd = mch_fopen((char *)tempname, READBIN);
11432# endif
11433
11434 if (fd == NULL)
11435 {
11436 EMSG2(_(e_notopen), tempname);
11437 goto done;
11438 }
11439
11440 fseek(fd, 0L, SEEK_END);
11441 len = ftell(fd); /* get size of temp file */
11442 fseek(fd, 0L, SEEK_SET);
11443
11444 buffer = alloc(len + 1);
11445 if (buffer != NULL)
11446 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11447 fclose(fd);
11448 mch_remove(tempname);
11449 if (buffer == NULL)
11450 goto done;
11451#ifdef VMS
11452 len = i; /* VMS doesn't give us what we asked for... */
11453#endif
11454 if (i != len)
11455 {
11456 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011457 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011459 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011460 {
11461 /* Change NUL into SOH, otherwise the string is truncated. */
11462 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011463 if (buffer[i] == NUL)
11464 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011465
Bram Moolenaar162bd912010-07-28 22:29:10 +020011466 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011467 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011468 else
11469 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470
11471done:
11472 vim_free(tempname);
11473 return buffer;
11474}
11475#endif
11476
11477/*
11478 * Free the list of files returned by expand_wildcards() or other expansion
11479 * functions.
11480 */
11481 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011482FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011484 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 while (count--)
11487 vim_free(files[count]);
11488 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489}
11490
11491/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011492 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 * Don't do this when still processing a command or a mapping.
11494 * Don't do this when inside a ":normal" command.
11495 */
11496 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011497goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498{
11499 return (p_im && stuff_empty() && typebuf_typed());
11500}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011501
11502/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011503 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011504 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11505 * - Remove any argument. E.g., "csh -f" -> "csh".
11506 * But don't allow a space in the path, so that this works:
11507 * "/usr/bin/csh --rcfile ~/.cshrc"
11508 * But don't do that for Windows, it's common to have a space in the path.
11509 */
11510 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011511get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011512{
11513 char_u *p;
11514
11515#ifdef WIN3264
11516 p = gettail(p_sh);
11517 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11518#else
11519 p = skiptowhite(p_sh);
11520 if (*p == NUL)
11521 {
11522 /* No white space, use the tail. */
11523 p = vim_strsave(gettail(p_sh));
11524 }
11525 else
11526 {
11527 char_u *p1, *p2;
11528
11529 /* Find the last path separator before the space. */
11530 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011531 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011532 if (vim_ispathsep(*p2))
11533 p1 = p2 + 1;
11534 p = vim_strnsave(p1, (int)(p - p1));
11535 }
11536#endif
11537 return p;
11538}