blob: b1cc2154922c9a191becce4aa8fe5e0b86f03da1 [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;
1996
1997 /*
1998 * Get one option part into part_buf[]. Advance list to next one.
1999 * put string at start of string.
2000 */
2001 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2002 string = vim_strchr(part_buf, ':');
2003 if (string == NULL) /* If everything is fine, this cannot actually
2004 * happen. */
2005 {
2006 continue;
2007 }
2008 *string++ = NUL; /* Isolate flags from string. */
2009 com_leader = string;
2010
2011 /*
2012 * Line contents and string must match.
2013 * When string starts with white space, must have some white space
2014 * (but the amount does not need to match, there might be a mix of
2015 * TABs and spaces).
2016 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002017 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002018 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002019 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002020 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002021 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002022 ++string;
2023 }
2024 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2025 /* do nothing */;
2026 if (string[j] != NUL)
2027 continue;
2028
2029 /*
2030 * When 'b' flag used, there must be white space or an
2031 * end-of-line after the string in the line.
2032 */
2033 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002034 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002035 {
2036 continue;
2037 }
2038
2039 /*
2040 * We have found a match, stop searching.
2041 */
2042 found_one = TRUE;
2043
2044 if (flags)
2045 *flags = flags_save;
2046 com_flags = flags_save;
2047
2048 break;
2049 }
2050
2051 if (found_one)
2052 {
2053 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2054 int len1, len2, off;
2055
2056 result = i;
2057 /*
2058 * If this comment nests, continue searching.
2059 */
2060 if (vim_strchr(part_buf, COM_NEST) != NULL)
2061 continue;
2062
2063 lower_check_bound = i;
2064
2065 /* Let's verify whether the comment leader found is a substring
2066 * of other comment leaders. If it is, let's adjust the
2067 * lower_check_bound so that we make sure that we have determined
2068 * the comment leader correctly.
2069 */
2070
Bram Moolenaar1c465442017-03-12 20:10:05 +01002071 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002072 ++com_leader;
2073 len1 = (int)STRLEN(com_leader);
2074
2075 for (list = curbuf->b_p_com; *list; )
2076 {
2077 char_u *flags_save = list;
2078
2079 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2080 if (flags_save == com_flags)
2081 continue;
2082 string = vim_strchr(part_buf2, ':');
2083 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002084 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002085 ++string;
2086 len2 = (int)STRLEN(string);
2087 if (len2 == 0)
2088 continue;
2089
2090 /* Now we have to verify whether string ends with a substring
2091 * beginning the com_leader. */
2092 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2093 {
2094 --off;
2095 if (!STRNCMP(string + off, com_leader, len2 - off))
2096 {
2097 if (i - off < lower_check_bound)
2098 lower_check_bound = i - off;
2099 }
2100 }
2101 }
2102 }
2103 }
2104 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105}
2106#endif
2107
2108/*
2109 * Return the number of window lines occupied by buffer line "lnum".
2110 */
2111 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002112plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 return plines_win(curwin, lnum, TRUE);
2115}
2116
2117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118plines_win(
2119 win_T *wp,
2120 linenr_T lnum,
2121 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123#if defined(FEAT_DIFF) || defined(PROTO)
2124 /* Check for filler lines above this buffer line. When folded the result
2125 * is one line anyway. */
2126 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2127}
2128
2129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002130plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
2132 return plines_win_nofill(curwin, lnum, TRUE);
2133}
2134
2135 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002136plines_win_nofill(
2137 win_T *wp,
2138 linenr_T lnum,
2139 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141#endif
2142 int lines;
2143
2144 if (!wp->w_p_wrap)
2145 return 1;
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (wp->w_width == 0)
2148 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150#ifdef FEAT_FOLDING
2151 /* A folded lines is handled just like an empty line. */
2152 /* NOTE: Caller must handle lines that are MAYBE folded. */
2153 if (lineFolded(wp, lnum) == TRUE)
2154 return 1;
2155#endif
2156
2157 lines = plines_win_nofold(wp, lnum);
2158 if (winheight > 0 && lines > wp->w_height)
2159 return (int)wp->w_height;
2160 return lines;
2161}
2162
2163/*
2164 * Return number of window lines physical line "lnum" will occupy in window
2165 * "wp". Does not care about folding, 'wrap' or 'diff'.
2166 */
2167 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002168plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170 char_u *s;
2171 long col;
2172 int width;
2173
2174 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2175 if (*s == NUL) /* empty line */
2176 return 1;
2177 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2178
2179 /*
2180 * If list mode is on, then the '$' at the end of the line may take up one
2181 * extra column.
2182 */
2183 if (wp->w_p_list && lcs_eol != NUL)
2184 col += 1;
2185
2186 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002187 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002189 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (width <= 0)
2191 return 32000;
2192 if (col <= width)
2193 return 1;
2194 col -= width;
2195 width += win_col_off2(wp);
2196 return (col + (width - 1)) / width + 1;
2197}
2198
2199/*
2200 * Like plines_win(), but only reports the number of physical screen lines
2201 * used from the start of the line to the given column number.
2202 */
2203 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002204plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206 long col;
2207 char_u *s;
2208 int lines = 0;
2209 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002210 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212#ifdef FEAT_DIFF
2213 /* Check for filler lines above this buffer line. When folded the result
2214 * is one line anyway. */
2215 lines = diff_check_fill(wp, lnum);
2216#endif
2217
2218 if (!wp->w_p_wrap)
2219 return lines + 1;
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (wp->w_width == 0)
2222 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
Bram Moolenaar597a4222014-06-25 14:39:50 +02002224 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 col = 0;
2227 while (*s != NUL && --column >= 0)
2228 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002229 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002230 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232
2233 /*
2234 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2235 * INSERT mode, then col must be adjusted so that it represents the last
2236 * screen position of the TAB. This only fixes an error when the TAB wraps
2237 * from one screen line to the next (when 'columns' is not a multiple of
2238 * 'ts') -- webb.
2239 */
2240 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002241 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
2243 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002244 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002246 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002247 if (width <= 0)
2248 return 9999;
2249
2250 lines += 1;
2251 if (col > width)
2252 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2253 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254}
2255
2256 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002257plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258{
2259 int count = 0;
2260
2261 while (first <= last)
2262 {
2263#ifdef FEAT_FOLDING
2264 int x;
2265
2266 /* Check if there are any really folded lines, but also included lines
2267 * that are maybe folded. */
2268 x = foldedCount(wp, first, NULL);
2269 if (x > 0)
2270 {
2271 ++count; /* count 1 for "+-- folded" line */
2272 first += x;
2273 }
2274 else
2275#endif
2276 {
2277#ifdef FEAT_DIFF
2278 if (first == wp->w_topline)
2279 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2280 else
2281#endif
2282 count += plines_win(wp, first, TRUE);
2283 ++first;
2284 }
2285 }
2286 return (count);
2287}
2288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289/*
2290 * Insert string "p" at the cursor position. Stops at a NUL byte.
2291 * Handles Replace mode and multi-byte characters.
2292 */
2293 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002294ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 ins_bytes_len(p, (int)STRLEN(p));
2297}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299/*
2300 * Insert string "p" with length "len" at the cursor position.
2301 * Handles Replace mode and multi-byte characters.
2302 */
2303 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002304ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002307#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 int n;
2309
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002310 if (has_mbyte)
2311 for (i = 0; i < len; i += n)
2312 {
2313 if (enc_utf8)
2314 /* avoid reading past p[len] */
2315 n = utfc_ptr2len_len(p + i, len - i);
2316 else
2317 n = (*mb_ptr2len)(p + i);
2318 ins_char_bytes(p + i, n);
2319 }
2320 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002321#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002322 for (i = 0; i < len; ++i)
2323 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326/*
2327 * Insert or replace a single character at the cursor position.
2328 * When in REPLACE or VREPLACE mode, replace any existing character.
2329 * Caller must have prepared for undo.
2330 * For multi-byte characters we get the whole character, the caller must
2331 * convert bytes to a character.
2332 */
2333 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002334ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002336 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002337 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002339#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 n = (*mb_char2bytes)(c, buf);
2341
2342 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2343 * Happens for CTRL-Vu9900. */
2344 if (buf[0] == 0)
2345 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002346#else
2347 buf[0] = c;
2348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 ins_char_bytes(buf, n);
2351}
2352
2353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002354ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
2356 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int newlen; /* nr of bytes inserted */
2358 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2359 char_u *p;
2360 char_u *newp;
2361 char_u *oldp;
2362 int linelen; /* length of old line including NUL */
2363 colnr_T col;
2364 linenr_T lnum = curwin->w_cursor.lnum;
2365 int i;
2366
2367#ifdef FEAT_VIRTUALEDIT
2368 /* Break tabs if needed. */
2369 if (virtual_active() && curwin->w_cursor.coladd > 0)
2370 coladvance_force(getviscol());
2371#endif
2372
2373 col = curwin->w_cursor.col;
2374 oldp = ml_get(lnum);
2375 linelen = (int)STRLEN(oldp) + 1;
2376
2377 /* The lengths default to the values for when not replacing. */
2378 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381 if (State & REPLACE_FLAG)
2382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (State & VREPLACE_FLAG)
2384 {
2385 colnr_T new_vcol = 0; /* init for GCC */
2386 colnr_T vcol;
2387 int old_list;
2388#ifndef FEAT_MBYTE
2389 char_u buf[2];
2390#endif
2391
2392 /*
2393 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2394 * Returns the old value of list, so when finished,
2395 * curwin->w_p_list should be set back to this.
2396 */
2397 old_list = curwin->w_p_list;
2398 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2399 curwin->w_p_list = FALSE;
2400
2401 /*
2402 * In virtual replace mode each character may replace one or more
2403 * characters (zero if it's a TAB). Count the number of bytes to
2404 * be deleted to make room for the new character, counting screen
2405 * cells. May result in adding spaces to fill a gap.
2406 */
2407 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2408#ifndef FEAT_MBYTE
2409 buf[0] = c;
2410 buf[1] = NUL;
2411#endif
2412 new_vcol = vcol + chartabsize(buf, vcol);
2413 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2414 {
2415 vcol += chartabsize(oldp + col + oldlen, vcol);
2416 /* Don't need to remove a TAB that takes us to the right
2417 * position. */
2418 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2419 break;
2420#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002421 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#else
2423 ++oldlen;
2424#endif
2425 /* Deleted a bit too much, insert spaces. */
2426 if (vcol > new_vcol)
2427 newlen += vcol - new_vcol;
2428 }
2429 curwin->w_p_list = old_list;
2430 }
2431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (oldp[col] != NUL)
2433 {
2434 /* normal replace */
2435#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002436 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437#else
2438 oldlen = 1;
2439#endif
2440 }
2441
2442
2443 /* Push the replaced bytes onto the replace stack, so that they can be
2444 * put back when BS is used. The bytes of a multi-byte character are
2445 * done the other way around, so that the first byte is popped off
2446 * first (it tells the byte length of the character). */
2447 replace_push(NUL);
2448 for (i = 0; i < oldlen; ++i)
2449 {
2450#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002451 if (has_mbyte)
2452 i += replace_push_mb(oldp + col + i) - 1;
2453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002455 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457 }
2458
2459 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2460 if (newp == NULL)
2461 return;
2462
2463 /* Copy bytes before the cursor. */
2464 if (col > 0)
2465 mch_memmove(newp, oldp, (size_t)col);
2466
2467 /* Copy bytes after the changed character(s). */
2468 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002469 if (linelen > col + oldlen)
2470 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 (size_t)(linelen - col - oldlen));
2472
2473 /* Insert or overwrite the new character. */
2474#ifdef FEAT_MBYTE
2475 mch_memmove(p, buf, charlen);
2476 i = charlen;
2477#else
2478 *p = c;
2479 i = 1;
2480#endif
2481
2482 /* Fill with spaces when necessary. */
2483 while (i < newlen)
2484 p[i++] = ' ';
2485
2486 /* Replace the line in the buffer. */
2487 ml_replace(lnum, newp, FALSE);
2488
2489 /* mark the buffer as changed and prepare for displaying */
2490 changed_bytes(lnum, col);
2491
2492 /*
2493 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2494 * show the match for right parens and braces.
2495 */
2496 if (p_sm && (State & INSERT)
2497 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002498#ifdef FEAT_INS_EXPAND
2499 && !ins_compl_active()
2500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002502 {
2503#ifdef FEAT_MBYTE
2504 if (has_mbyte)
2505 showmatch(mb_ptr2char(buf));
2506 else
2507#endif
2508 showmatch(c);
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511#ifdef FEAT_RIGHTLEFT
2512 if (!p_ri || (State & REPLACE_FLAG))
2513#endif
2514 {
2515 /* Normal insert: move cursor right */
2516#ifdef FEAT_MBYTE
2517 curwin->w_cursor.col += charlen;
2518#else
2519 ++curwin->w_cursor.col;
2520#endif
2521 }
2522 /*
2523 * TODO: should try to update w_row here, to avoid recomputing it later.
2524 */
2525}
2526
2527/*
2528 * Insert a string at the cursor position.
2529 * Note: Does NOT handle Replace mode.
2530 * Caller must have prepared for undo.
2531 */
2532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002533ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 char_u *oldp, *newp;
2536 int newlen = (int)STRLEN(s);
2537 int oldlen;
2538 colnr_T col;
2539 linenr_T lnum = curwin->w_cursor.lnum;
2540
2541#ifdef FEAT_VIRTUALEDIT
2542 if (virtual_active() && curwin->w_cursor.coladd > 0)
2543 coladvance_force(getviscol());
2544#endif
2545
2546 col = curwin->w_cursor.col;
2547 oldp = ml_get(lnum);
2548 oldlen = (int)STRLEN(oldp);
2549
2550 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2551 if (newp == NULL)
2552 return;
2553 if (col > 0)
2554 mch_memmove(newp, oldp, (size_t)col);
2555 mch_memmove(newp + col, s, (size_t)newlen);
2556 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2557 ml_replace(lnum, newp, FALSE);
2558 changed_bytes(lnum, col);
2559 curwin->w_cursor.col += newlen;
2560}
2561
2562/*
2563 * Delete one character under the cursor.
2564 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2565 * Caller must have prepared for undo.
2566 *
2567 * return FAIL for failure, OK otherwise
2568 */
2569 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002570del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572#ifdef FEAT_MBYTE
2573 if (has_mbyte)
2574 {
2575 /* Make sure the cursor is at the start of a character. */
2576 mb_adjust_cursor();
2577 if (*ml_get_cursor() == NUL)
2578 return FAIL;
2579 return del_chars(1L, fixpos);
2580 }
2581#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002582 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583}
2584
2585#if defined(FEAT_MBYTE) || defined(PROTO)
2586/*
2587 * Like del_bytes(), but delete characters instead of bytes.
2588 */
2589 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002590del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 long bytes = 0;
2593 long i;
2594 char_u *p;
2595 int l;
2596
2597 p = ml_get_cursor();
2598 for (i = 0; i < count && *p != NUL; ++i)
2599 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002600 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 bytes += l;
2602 p += l;
2603 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002604 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605}
2606#endif
2607
2608/*
2609 * Delete "count" bytes under the cursor.
2610 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2611 * Caller must have prepared for undo.
2612 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002613 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 */
2615 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002616del_bytes(
2617 long count,
2618 int fixpos_arg,
2619 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
2621 char_u *oldp, *newp;
2622 colnr_T oldlen;
2623 linenr_T lnum = curwin->w_cursor.lnum;
2624 colnr_T col = curwin->w_cursor.col;
2625 int was_alloced;
2626 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002627 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 oldp = ml_get(lnum);
2630 oldlen = (int)STRLEN(oldp);
2631
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002632 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (col >= oldlen)
2634 return FAIL;
2635
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002636 /* If "count" is zero there is nothing to do. */
2637 if (count == 0)
2638 return OK;
2639
2640 /* If "count" is negative the caller must be doing something wrong. */
2641 if (count < 1)
2642 {
2643 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2644 return FAIL;
2645 }
2646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647#ifdef FEAT_MBYTE
2648 /* If 'delcombine' is set and deleting (less than) one character, only
2649 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002650 if (p_deco && use_delcombine && enc_utf8
2651 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002653 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 int n;
2655
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 (void)utfc_ptr2char(oldp + col, cc);
2657 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 /* Find the last composing char, there can be several. */
2660 n = col;
2661 do
2662 {
2663 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002664 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 n += count;
2666 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2667 fixpos = 0;
2668 }
2669 }
2670#endif
2671
2672 /*
2673 * When count is too big, reduce it.
2674 */
2675 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2676 if (movelen <= 1)
2677 {
2678 /*
2679 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002680 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2681 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002683 if (col > 0 && fixpos && restart_edit == 0
2684#ifdef FEAT_VIRTUALEDIT
2685 && (ve_flags & VE_ONEMORE) == 0
2686#endif
2687 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
2689 --curwin->w_cursor.col;
2690#ifdef FEAT_VIRTUALEDIT
2691 curwin->w_cursor.coladd = 0;
2692#endif
2693#ifdef FEAT_MBYTE
2694 if (has_mbyte)
2695 curwin->w_cursor.col -=
2696 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2697#endif
2698 }
2699 count = oldlen - col;
2700 movelen = 1;
2701 }
2702
2703 /*
2704 * If the old line has been allocated the deletion can be done in the
2705 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002706 * Can't do this when using Netbeans, because we would need to invoke
2707 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002708 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002711 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002712 was_alloced = FALSE;
2713 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002715 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (was_alloced)
2717 newp = oldp; /* use same allocated memory */
2718 else
2719 { /* need to allocate a new line */
2720 newp = alloc((unsigned)(oldlen + 1 - count));
2721 if (newp == NULL)
2722 return FAIL;
2723 mch_memmove(newp, oldp, (size_t)col);
2724 }
2725 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2726 if (!was_alloced)
2727 ml_replace(lnum, newp, FALSE);
2728
2729 /* mark the buffer as changed and prepare for displaying */
2730 changed_bytes(lnum, curwin->w_cursor.col);
2731
2732 return OK;
2733}
2734
2735/*
2736 * Delete from cursor to end of line.
2737 * Caller must have prepared for undo.
2738 *
2739 * return FAIL for failure, OK otherwise
2740 */
2741 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002742truncate_line(
2743 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 char_u *newp;
2746 linenr_T lnum = curwin->w_cursor.lnum;
2747 colnr_T col = curwin->w_cursor.col;
2748
2749 if (col == 0)
2750 newp = vim_strsave((char_u *)"");
2751 else
2752 newp = vim_strnsave(ml_get(lnum), col);
2753
2754 if (newp == NULL)
2755 return FAIL;
2756
2757 ml_replace(lnum, newp, FALSE);
2758
2759 /* mark the buffer as changed and prepare for displaying */
2760 changed_bytes(lnum, curwin->w_cursor.col);
2761
2762 /*
2763 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2764 */
2765 if (fixpos && curwin->w_cursor.col > 0)
2766 --curwin->w_cursor.col;
2767
2768 return OK;
2769}
2770
2771/*
2772 * Delete "nlines" lines at the cursor.
2773 * Saves the lines for undo first if "undo" is TRUE.
2774 */
2775 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002776del_lines(
2777 long nlines, /* number of lines to delete */
2778 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
2780 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002781 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 if (nlines <= 0)
2784 return;
2785
2786 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002787 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return;
2789
2790 for (n = 0; n < nlines; )
2791 {
2792 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2793 break;
2794
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002795 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 ++n;
2797
2798 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002799 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 break;
2801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002803 /* Correct the cursor position before calling deleted_lines_mark(), it may
2804 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 curwin->w_cursor.col = 0;
2806 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002807
2808 /* adjust marks, mark the buffer as changed and prepare for displaying */
2809 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810}
2811
2812 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002813gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002815 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002817 /* When searching columns is sometimes put at the end of a line. */
2818 if (pos->col == MAXCOL)
2819 return NUL;
2820 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821#ifdef FEAT_MBYTE
2822 if (has_mbyte)
2823 return (*mb_ptr2char)(ptr);
2824#endif
2825 return (int)*ptr;
2826}
2827
2828 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002829gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831#ifdef FEAT_MBYTE
2832 if (has_mbyte)
2833 return (*mb_ptr2char)(ml_get_cursor());
2834#endif
2835 return (int)*ml_get_cursor();
2836}
2837
2838/*
2839 * Write a character at the current cursor position.
2840 * It is directly written into the block.
2841 */
2842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002843pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
2845 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2846 + curwin->w_cursor.col) = c;
2847}
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849/*
2850 * When extra == 0: Return TRUE if the cursor is before or on the first
2851 * non-blank in the line.
2852 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2853 * the line.
2854 */
2855 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002856inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 char_u *ptr;
2859 colnr_T col;
2860
Bram Moolenaar1c465442017-03-12 20:10:05 +01002861 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 ++ptr;
2863 if (col >= curwin->w_cursor.col + extra)
2864 return TRUE;
2865 else
2866 return FALSE;
2867}
2868
2869/*
2870 * Skip to next part of an option argument: Skip space and comma.
2871 */
2872 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002873skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 if (*p == ',')
2876 ++p;
2877 while (*p == ' ')
2878 ++p;
2879 return p;
2880}
2881
2882/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002883 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 *
2885 * Most often called through changed_bytes() and changed_lines(), which also
2886 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002887 *
2888 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 */
2890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002891changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002894 if (p_imst == IM_ON_THE_SPOT)
2895 {
2896 /* The text of the preediting area is inserted, but this doesn't
2897 * mean a change of the buffer yet. That is delayed until the
2898 * text is committed. (this means preedit becomes empty) */
2899 if (im_is_preediting() && !xim_changed_while_preediting)
2900 return;
2901 xim_changed_while_preediting = FALSE;
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#endif
2904
2905 if (!curbuf->b_changed)
2906 {
2907 int save_msg_scroll = msg_scroll;
2908
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002909 /* Give a warning about changing a read-only file. This may also
2910 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 /* Create a swap file if that is wanted.
2914 * Don't do this for "nofile" and "nowrite" buffer types. */
2915 if (curbuf->b_may_swap
2916#ifdef FEAT_QUICKFIX
2917 && !bt_dontwrite(curbuf)
2918#endif
2919 )
2920 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002921 int save_need_wait_return = need_wait_return;
2922
2923 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 ml_open_file(curbuf);
2925
2926 /* The ml_open_file() can cause an ATTENTION message.
2927 * Wait two seconds, to make sure the user reads this unexpected
2928 * message. Since we could be anywhere, call wait_return() now,
2929 * and don't let the emsg() set msg_scroll. */
2930 if (need_wait_return && emsg_silent == 0)
2931 {
2932 out_flush();
2933 ui_delay(2000L, TRUE);
2934 wait_return(TRUE);
2935 msg_scroll = save_msg_scroll;
2936 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002937 else
2938 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002940 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002942 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943}
2944
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002945/*
2946 * Internal part of changed(), no user interaction.
2947 */
2948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002949changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002950{
2951 curbuf->b_changed = TRUE;
2952 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002953 check_status(curbuf);
2954 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002955#ifdef FEAT_TITLE
2956 need_maketitle = TRUE; /* set window title later */
2957#endif
2958}
2959
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002960static void changedOneline(buf_T *buf, linenr_T lnum);
2961static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2962static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964/*
2965 * Changed bytes within a single line for the current buffer.
2966 * - marks the windows on this buffer to be redisplayed
2967 * - marks the buffer changed by calling changed()
2968 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002969 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 */
2971 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002972changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002974 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002976
2977#ifdef FEAT_DIFF
2978 /* Diff highlighting in other diff windows may need to be updated too. */
2979 if (curwin->w_p_diff)
2980 {
2981 win_T *wp;
2982 linenr_T wlnum;
2983
Bram Moolenaar29323592016-07-24 22:04:11 +02002984 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002985 if (wp->w_p_diff && wp != curwin)
2986 {
2987 redraw_win_later(wp, VALID);
2988 wlnum = diff_lnum_win(lnum, wp);
2989 if (wlnum > 0)
2990 changedOneline(wp->w_buffer, wlnum);
2991 }
2992 }
2993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995
2996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002997changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002999 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 {
3001 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003002 if (lnum < buf->b_mod_top)
3003 buf->b_mod_top = lnum;
3004 else if (lnum >= buf->b_mod_bot)
3005 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007 else
3008 {
3009 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003010 buf->b_mod_set = TRUE;
3011 buf->b_mod_top = lnum;
3012 buf->b_mod_bot = lnum + 1;
3013 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015}
3016
3017/*
3018 * Appended "count" lines below line "lnum" in the current buffer.
3019 * Must be called AFTER the change and after mark_adjust().
3020 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3021 */
3022 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003023appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 changed_lines(lnum + 1, 0, lnum + 1, count);
3026}
3027
3028/*
3029 * Like appended_lines(), but adjust marks first.
3030 */
3031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003032appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003034 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003035 * be marks there. But it's still needed in diff mode. */
3036 if (lnum + count < curbuf->b_ml.ml_line_count
3037#ifdef FEAT_DIFF
3038 || curwin->w_p_diff
3039#endif
3040 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003041 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 changed_lines(lnum + 1, 0, lnum + 1, count);
3043}
3044
3045/*
3046 * Deleted "count" lines at line "lnum" in the current buffer.
3047 * Must be called AFTER the change and after mark_adjust().
3048 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3049 */
3050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003051deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 changed_lines(lnum, 0, lnum + count, -count);
3054}
3055
3056/*
3057 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003058 * Make sure the cursor is on a valid line before calling, a GUI callback may
3059 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
3061 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003062deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3065 changed_lines(lnum, 0, lnum + count, -count);
3066}
3067
3068/*
3069 * Changed lines for the current buffer.
3070 * Must be called AFTER the change and after mark_adjust().
3071 * - mark the buffer changed by calling changed()
3072 * - mark the windows on this buffer to be redisplayed
3073 * - invalidate cached values
3074 * "lnum" is the first line that needs displaying, "lnume" the first line
3075 * below the changed lines (BEFORE the change).
3076 * When only inserting lines, "lnum" and "lnume" are equal.
3077 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003078 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 */
3080 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003081changed_lines(
3082 linenr_T lnum, /* first line with change */
3083 colnr_T col, /* column in first line with change */
3084 linenr_T lnume, /* line below last changed line */
3085 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003087 changed_lines_buf(curbuf, lnum, lnume, xtra);
3088
3089#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003090 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003091 {
3092 /* When the number of lines doesn't change then mark_adjust() isn't
3093 * called and other diff buffers still need to be marked for
3094 * displaying. */
3095 win_T *wp;
3096 linenr_T wlnum;
3097
Bram Moolenaar29323592016-07-24 22:04:11 +02003098 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003099 if (wp->w_p_diff && wp != curwin)
3100 {
3101 redraw_win_later(wp, VALID);
3102 wlnum = diff_lnum_win(lnum, wp);
3103 if (wlnum > 0)
3104 changed_lines_buf(wp->w_buffer, wlnum,
3105 lnume - lnum + wlnum, 0L);
3106 }
3107 }
3108#endif
3109
3110 changed_common(lnum, col, lnume, xtra);
3111}
3112
3113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003114changed_lines_buf(
3115 buf_T *buf,
3116 linenr_T lnum, /* first line with change */
3117 linenr_T lnume, /* line below last changed line */
3118 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003119{
3120 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
3122 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003123 if (lnum < buf->b_mod_top)
3124 buf->b_mod_top = lnum;
3125 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
3127 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003128 buf->b_mod_bot += xtra;
3129 if (buf->b_mod_bot < lnum)
3130 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003132 if (lnume + xtra > buf->b_mod_bot)
3133 buf->b_mod_bot = lnume + xtra;
3134 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
3136 else
3137 {
3138 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003139 buf->b_mod_set = TRUE;
3140 buf->b_mod_top = lnum;
3141 buf->b_mod_bot = lnume + xtra;
3142 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144}
3145
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003146/*
3147 * Common code for when a change is was made.
3148 * See changed_lines() for the arguments.
3149 * Careful: may trigger autocommands that reload the buffer.
3150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003152changed_common(
3153 linenr_T lnum,
3154 colnr_T col,
3155 linenr_T lnume,
3156 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003159 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 int i;
3161#ifdef FEAT_JUMPLIST
3162 int cols;
3163 pos_T *p;
3164 int add;
3165#endif
3166
3167 /* mark the buffer as modified */
3168 changed();
3169
Bram Moolenaare3521d92018-09-16 14:10:31 +02003170#ifdef FEAT_DIFF
3171 if (curwin->w_p_diff && diff_internal())
3172 curtab->tp_diff_update = TRUE;
3173#endif
3174
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 /* set the '. mark */
3176 if (!cmdmod.keepjumps)
3177 {
3178 curbuf->b_last_change.lnum = lnum;
3179 curbuf->b_last_change.col = col;
3180
3181#ifdef FEAT_JUMPLIST
3182 /* Create a new entry if a new undo-able change was started or we
3183 * don't have an entry yet. */
3184 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3185 {
3186 if (curbuf->b_changelistlen == 0)
3187 add = TRUE;
3188 else
3189 {
3190 /* Don't create a new entry when the line number is the same
3191 * as the last one and the column is not too far away. Avoids
3192 * creating many entries for typing "xxxxx". */
3193 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3194 if (p->lnum != lnum)
3195 add = TRUE;
3196 else
3197 {
3198 cols = comp_textwidth(FALSE);
3199 if (cols == 0)
3200 cols = 79;
3201 add = (p->col + cols < col || col + cols < p->col);
3202 }
3203 }
3204 if (add)
3205 {
3206 /* This is the first of a new sequence of undo-able changes
3207 * and it's at some distance of the last change. Use a new
3208 * position in the changelist. */
3209 curbuf->b_new_change = FALSE;
3210
3211 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3212 {
3213 /* changelist is full: remove oldest entry */
3214 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3215 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3216 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003217 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 /* Correct position in changelist for other windows on
3220 * this buffer. */
3221 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3222 --wp->w_changelistidx;
3223 }
3224 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003225 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
3227 /* For other windows, if the position in the changelist is
3228 * at the end it stays at the end. */
3229 if (wp->w_buffer == curbuf
3230 && wp->w_changelistidx == curbuf->b_changelistlen)
3231 ++wp->w_changelistidx;
3232 }
3233 ++curbuf->b_changelistlen;
3234 }
3235 }
3236 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3237 curbuf->b_last_change;
3238 /* The current window is always after the last change, so that "g,"
3239 * takes you back to it. */
3240 curwin->w_changelistidx = curbuf->b_changelistlen;
3241#endif
3242 }
3243
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003244 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
3246 if (wp->w_buffer == curbuf)
3247 {
3248 /* Mark this window to be redrawn later. */
3249 if (wp->w_redr_type < VALID)
3250 wp->w_redr_type = VALID;
3251
3252 /* Check if a change in the buffer has invalidated the cached
3253 * values for the cursor. */
3254#ifdef FEAT_FOLDING
3255 /*
3256 * Update the folds for this window. Can't postpone this, because
3257 * a following operator might work on the whole fold: ">>dd".
3258 */
3259 foldUpdate(wp, lnum, lnume + xtra - 1);
3260
3261 /* The change may cause lines above or below the change to become
3262 * included in a fold. Set lnum/lnume to the first/last line that
3263 * might be displayed differently.
3264 * Set w_cline_folded here as an efficient way to update it when
3265 * inserting lines just above a closed fold. */
3266 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3267 if (wp->w_cursor.lnum == lnum)
3268 wp->w_cline_folded = i;
3269 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3270 if (wp->w_cursor.lnum == lnume)
3271 wp->w_cline_folded = i;
3272
3273 /* If the changed line is in a range of previously folded lines,
3274 * compare with the first line in that range. */
3275 if (wp->w_cursor.lnum <= lnum)
3276 {
3277 i = find_wl_entry(wp, lnum);
3278 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3279 changed_line_abv_curs_win(wp);
3280 }
3281#endif
3282
3283 if (wp->w_cursor.lnum > lnum)
3284 changed_line_abv_curs_win(wp);
3285 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3286 changed_cline_bef_curs_win(wp);
3287 if (wp->w_botline >= lnum)
3288 {
3289 /* Assume that botline doesn't change (inserted lines make
3290 * other lines scroll down below botline). */
3291 approximate_botline_win(wp);
3292 }
3293
3294 /* Check if any w_lines[] entries have become invalid.
3295 * For entries below the change: Correct the lnums for
3296 * inserted/deleted lines. Makes it possible to stop displaying
3297 * after the change. */
3298 for (i = 0; i < wp->w_lines_valid; ++i)
3299 if (wp->w_lines[i].wl_valid)
3300 {
3301 if (wp->w_lines[i].wl_lnum >= lnum)
3302 {
3303 if (wp->w_lines[i].wl_lnum < lnume)
3304 {
3305 /* line included in change */
3306 wp->w_lines[i].wl_valid = FALSE;
3307 }
3308 else if (xtra != 0)
3309 {
3310 /* line below change */
3311 wp->w_lines[i].wl_lnum += xtra;
3312#ifdef FEAT_FOLDING
3313 wp->w_lines[i].wl_lastlnum += xtra;
3314#endif
3315 }
3316 }
3317#ifdef FEAT_FOLDING
3318 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3319 {
3320 /* change somewhere inside this range of folded lines,
3321 * may need to be redrawn */
3322 wp->w_lines[i].wl_valid = FALSE;
3323 }
3324#endif
3325 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003326
3327#ifdef FEAT_FOLDING
3328 /* Take care of side effects for setting w_topline when folds have
3329 * changed. Esp. when the buffer was changed in another window. */
3330 if (hasAnyFolding(wp))
3331 set_topline(wp, wp->w_topline);
3332#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003333 /* relative numbering may require updating more */
3334 if (wp->w_p_rnu)
3335 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 }
3338
3339 /* Call update_screen() later, which checks out what needs to be redrawn,
3340 * since it notices b_mod_set and then uses b_mod_*. */
3341 if (must_redraw < VALID)
3342 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003343
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003344 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003345 if (lnum <= curwin->w_cursor.lnum
3346 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003347 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348}
3349
3350/*
3351 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3352 */
3353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003354unchanged(
3355 buf_T *buf,
3356 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003358 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003361 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (ff)
3363 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003365 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#ifdef FEAT_TITLE
3367 need_maketitle = TRUE; /* set window title later */
3368#endif
3369 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003370 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#ifdef FEAT_NETBEANS_INTG
3372 netbeans_unmodified(buf);
3373#endif
3374}
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376/*
3377 * check_status: called when the status bars for the buffer 'buf'
3378 * need to be updated
3379 */
3380 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003381check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 win_T *wp;
3384
Bram Moolenaar29323592016-07-24 22:04:11 +02003385 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (wp->w_buffer == buf && wp->w_status_height)
3387 {
3388 wp->w_redr_status = TRUE;
3389 if (must_redraw < VALID)
3390 must_redraw = VALID;
3391 }
3392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
3394/*
3395 * If the file is readonly, give a warning message with the first change.
3396 * Don't do this for autocommands.
3397 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003398 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003400 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 */
3402 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003403change_warning(
3404 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 mode and 'showmode' is on */
3406{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003407 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (curbuf->b_did_warn == FALSE
3410 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 && curbuf->b_p_ro)
3413 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003414 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003416 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 if (!curbuf->b_p_ro)
3418 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 /*
3420 * Do what msg() does, but with a column offset if the warning should
3421 * be after the mode message.
3422 */
3423 msg_start();
3424 if (msg_row == Rows - 1)
3425 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003426 msg_source(HL_ATTR(HLF_W));
3427 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003428#ifdef FEAT_EVAL
3429 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 msg_clr_eos();
3432 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003433 if (msg_silent == 0 && !silent_mode
3434#ifdef FEAT_EVAL
3435 && time_for_testing != 1
3436#endif
3437 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439 out_flush();
3440 ui_delay(1000L, TRUE); /* give the user time to think about it */
3441 }
3442 curbuf->b_did_warn = TRUE;
3443 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3444 if (msg_row < Rows - 1)
3445 showmode();
3446 }
3447}
3448
3449/*
3450 * Ask for a reply from the user, a 'y' or a 'n'.
3451 * No other characters are accepted, the message is repeated until a valid
3452 * reply is entered or CTRL-C is hit.
3453 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3454 * from any buffers but directly from the user.
3455 *
3456 * return the 'y' or 'n'
3457 */
3458 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003459ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 int r = ' ';
3462 int save_State = State;
3463
3464 if (exiting) /* put terminal in raw mode for this question */
3465 settmode(TMODE_RAW);
3466 ++no_wait_return;
3467#ifdef USE_ON_FLY_SCROLL
3468 dont_scroll = TRUE; /* disallow scrolling here */
3469#endif
3470 State = CONFIRM; /* mouse behaves like with :confirm */
3471#ifdef FEAT_MOUSE
3472 setmouse(); /* disables mouse for xterm */
3473#endif
3474 ++no_mapping;
3475 ++allow_keys; /* no mapping here, but recognize keys */
3476
3477 while (r != 'y' && r != 'n')
3478 {
3479 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003480 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (direct)
3482 r = get_keystroke();
3483 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003484 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (r == Ctrl_C || r == ESC)
3486 r = 'n';
3487 msg_putchar(r); /* show what you typed */
3488 out_flush();
3489 }
3490 --no_wait_return;
3491 State = save_State;
3492#ifdef FEAT_MOUSE
3493 setmouse();
3494#endif
3495 --no_mapping;
3496 --allow_keys;
3497
3498 return r;
3499}
3500
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003501#if defined(FEAT_MOUSE) || defined(PROTO)
3502/*
3503 * Return TRUE if "c" is a mouse key.
3504 */
3505 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003506is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003507{
3508 return c == K_LEFTMOUSE
3509 || c == K_LEFTMOUSE_NM
3510 || c == K_LEFTDRAG
3511 || c == K_LEFTRELEASE
3512 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003513 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003514 || c == K_MIDDLEMOUSE
3515 || c == K_MIDDLEDRAG
3516 || c == K_MIDDLERELEASE
3517 || c == K_RIGHTMOUSE
3518 || c == K_RIGHTDRAG
3519 || c == K_RIGHTRELEASE
3520 || c == K_MOUSEDOWN
3521 || c == K_MOUSEUP
3522 || c == K_MOUSELEFT
3523 || c == K_MOUSERIGHT
3524 || c == K_X1MOUSE
3525 || c == K_X1DRAG
3526 || c == K_X1RELEASE
3527 || c == K_X2MOUSE
3528 || c == K_X2DRAG
3529 || c == K_X2RELEASE;
3530}
3531#endif
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533/*
3534 * Get a key stroke directly from the user.
3535 * Ignores mouse clicks and scrollbar events, except a click for the left
3536 * button (used at the more prompt).
3537 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3538 * Disadvantage: typeahead is ignored.
3539 * Translates the interrupt character for unix to ESC.
3540 */
3541 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003542get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003544 char_u *buf = NULL;
3545 int buflen = 150;
3546 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 int len = 0;
3548 int n;
3549 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003550 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 mapped_ctrl_c = FALSE; /* mappings are not used here */
3553 for (;;)
3554 {
3555 cursor_on();
3556 out_flush();
3557
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003558 /* Leave some room for check_termcode() to insert a key code into (max
3559 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3560 * bytes. */
3561 maxlen = (buflen - 6 - len) / 3;
3562 if (buf == NULL)
3563 buf = alloc(buflen);
3564 else if (maxlen < 10)
3565 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003566 char_u *t_buf = buf;
3567
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003568 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003569 * escape sequence. */
3570 buflen += 100;
3571 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003572 if (buf == NULL)
3573 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003574 maxlen = (buflen - 6 - len) / 3;
3575 }
3576 if (buf == NULL)
3577 {
3578 do_outofmem_msg((long_u)buflen);
3579 return ESC; /* panic! */
3580 }
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003583 * terminal code to complete. */
3584 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (n > 0)
3586 {
3587 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003588 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003590 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003592 else if (len > 0)
3593 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaar4395a712006-09-05 18:57:57 +00003595 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003596 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003597 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003599
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003600 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003601 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003602 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003603 {
3604 /* Redrawing was postponed, do it now. */
3605 update_screen(0);
3606 setcursor(); /* put cursor back where it belongs */
3607 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003608 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003609 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003610 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003612 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 continue;
3614
3615 /* Handle modifier and/or special key code. */
3616 n = buf[0];
3617 if (n == K_SPECIAL)
3618 {
3619 n = TO_SPECIAL(buf[1], buf[2]);
3620 if (buf[1] == KS_MODIFIER
3621 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003622#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003623 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003624#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003625#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 || n == K_VER_SCROLLBAR
3627 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628#endif
3629 )
3630 {
3631 if (buf[1] == KS_MODIFIER)
3632 mod_mask = buf[2];
3633 len -= 3;
3634 if (len > 0)
3635 mch_memmove(buf, buf + 3, (size_t)len);
3636 continue;
3637 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640#ifdef FEAT_MBYTE
3641 if (has_mbyte)
3642 {
3643 if (MB_BYTE2LEN(n) > len)
3644 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003645 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 n = (*mb_ptr2char)(buf);
3647 }
3648#endif
3649#ifdef UNIX
3650 if (n == intr_char)
3651 n = ESC;
3652#endif
3653 break;
3654 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003655 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
3657 mapped_ctrl_c = save_mapped_ctrl_c;
3658 return n;
3659}
3660
3661/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003662 * Get a number from the user.
3663 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
3665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003666get_number(
3667 int colon, /* allow colon to abort */
3668 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
3670 int n = 0;
3671 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003672 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003674 if (mouse_used != NULL)
3675 *mouse_used = FALSE;
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 /* When not printing messages, the user won't know what to type, return a
3678 * zero (as if CR was hit). */
3679 if (msg_silent != 0)
3680 return 0;
3681
3682#ifdef USE_ON_FLY_SCROLL
3683 dont_scroll = TRUE; /* disallow scrolling here */
3684#endif
3685 ++no_mapping;
3686 ++allow_keys; /* no mapping here, but recognize keys */
3687 for (;;)
3688 {
3689 windgoto(msg_row, msg_col);
3690 c = safe_vgetc();
3691 if (VIM_ISDIGIT(c))
3692 {
3693 n = n * 10 + c - '0';
3694 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003695 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3698 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003699 if (typed > 0)
3700 {
3701 MSG_PUTS("\b \b");
3702 --typed;
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003706#ifdef FEAT_MOUSE
3707 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3708 {
3709 *mouse_used = TRUE;
3710 n = mouse_row + 1;
3711 break;
3712 }
3713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 else if (n == 0 && c == ':' && colon)
3715 {
3716 stuffcharReadbuff(':');
3717 if (!exmode_active)
3718 cmdline_row = msg_row;
3719 skip_redraw = TRUE; /* skip redraw once */
3720 do_redraw = FALSE;
3721 break;
3722 }
3723 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3724 break;
3725 }
3726 --no_mapping;
3727 --allow_keys;
3728 return n;
3729}
3730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003731/*
3732 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003733 * When "mouse_used" is not NULL allow using the mouse and in that case return
3734 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 */
3736 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003737prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738{
3739 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003740 int save_cmdline_row;
3741 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003742
3743 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003744 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003745 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003746 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003747 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003748
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003749 // Set the state such that text can be selected/copied/pasted and we still
3750 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3751 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003752 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003753 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003754 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003755 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003756#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003757 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003758 setmouse();
3759#endif
3760
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003761 i = get_number(TRUE, mouse_used);
3762 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003764 /* don't call wait_return() now */
3765 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003766 cmdline_row = msg_row - 1;
3767 need_wait_return = FALSE;
3768 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003769 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003770 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003771 else
3772 cmdline_row = save_cmdline_row;
3773 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003774#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003775 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003776 setmouse();
3777#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 return i;
3780}
3781
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003783msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
3785 long pn;
3786
3787 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3789 return;
3790
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003791 /* We don't want to overwrite another important message, but do overwrite
3792 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3793 * then "put" reports the last action. */
3794 if (keep_msg != NULL && !keep_msg_more)
3795 return;
3796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (n > 0)
3798 pn = n;
3799 else
3800 pn = -n;
3801
3802 if (pn > p_report)
3803 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003804 if (n > 0)
3805 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3806 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003808 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3809 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003811 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (msg(msg_buf))
3813 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003814 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003815 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 }
3818}
3819
3820/*
3821 * flush map and typeahead buffers and give a warning for an error
3822 */
3823 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003824beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 if (emsg_silent == 0)
3827 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003828 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003829 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831}
3832
3833/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003834 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 */
3836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003837vim_beep(
3838 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003840#ifdef FEAT_EVAL
3841 called_vim_beep = TRUE;
3842#endif
3843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (emsg_silent == 0)
3845 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003846 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3847 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003848#ifdef ELAPSED_FUNC
3849 static int did_init = FALSE;
3850 static ELAPSED_TYPE start_tv;
3851
3852 /* Only beep once per half a second, otherwise a sequence of beeps
3853 * would freeze Vim. */
3854 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3855 {
3856 did_init = TRUE;
3857 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003859 if (p_vb
3860#ifdef FEAT_GUI
3861 /* While the GUI is starting up the termcap is set for
3862 * the GUI but the output still goes to a terminal. */
3863 && !(gui.in_use && gui.starting)
3864#endif
3865 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003866 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003867 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003868#ifdef FEAT_VTP
3869 /* No restore color information, refresh the screen. */
3870 if (has_vtp_working() != 0
3871# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003872 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003873# endif
3874 )
3875 {
3876 redraw_later(CLEAR);
3877 update_screen(0);
3878 redrawcmd();
3879 }
3880#endif
3881 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003882 else
3883 out_char(BELL);
3884#ifdef ELAPSED_FUNC
3885 }
3886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003888
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003889 /* When 'debug' contains "beep" produce a message. If we are sourcing
3890 * a script or executing a function give the user a hint where the beep
3891 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003892 if (vim_strchr(p_debug, 'e') != NULL)
3893 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003894 msg_source(HL_ATTR(HLF_W));
3895 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898}
3899
3900/*
3901 * To get the "real" home directory:
3902 * - get value of $HOME
3903 * For Unix:
3904 * - go to that directory
3905 * - do mch_dirname() to get the real name of that directory.
3906 * This also works with mounts and links.
3907 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003908 * For Windows:
3909 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 */
3911static char_u *homedir = NULL;
3912
3913 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003914init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
3916 char_u *var;
3917
Bram Moolenaar05159a02005-02-26 23:04:13 +00003918 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003919 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#ifdef VMS
3922 var = mch_getenv((char_u *)"SYS$LOGIN");
3923#else
3924 var = mch_getenv((char_u *)"HOME");
3925#endif
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927#ifdef WIN3264
3928 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003929 * Typically, $HOME is not defined on Windows, unless the user has
3930 * specifically defined it for Vim's sake. However, on Windows NT
3931 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3932 * each user. Try constructing $HOME from these.
3933 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003934 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003935 {
3936 char_u *homedrive, *homepath;
3937
3938 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3939 homepath = mch_getenv((char_u *)"HOMEPATH");
3940 if (homepath == NULL || *homepath == NUL)
3941 homepath = (char_u *)"\\";
3942 if (homedrive != NULL
3943 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3944 {
3945 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3946 if (NameBuff[0] != NUL)
3947 var = NameBuff;
3948 }
3949 }
3950
3951 if (var == NULL)
3952 var = mch_getenv((char_u *)"USERPROFILE");
3953
3954 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 * Weird but true: $HOME may contain an indirect reference to another
3956 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3957 * when $HOME is being set.
3958 */
3959 if (var != NULL && *var == '%')
3960 {
3961 char_u *p;
3962 char_u *exp;
3963
3964 p = vim_strchr(var + 1, '%');
3965 if (p != NULL)
3966 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003967 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 exp = mch_getenv(NameBuff);
3969 if (exp != NULL && *exp != NUL
3970 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3971 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003972 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976 }
3977
Bram Moolenaar48340b62017-08-29 22:08:53 +02003978 if (var != NULL && *var == NUL) /* empty is same as not set */
3979 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
Bram Moolenaar48340b62017-08-29 22:08:53 +02003981# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003982 if (enc_utf8 && var != NULL)
3983 {
3984 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003985 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003986
3987 /* Convert from active codepage to UTF-8. Other conversions are
3988 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003989 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003990 if (pp != NULL)
3991 {
3992 homedir = pp;
3993 return;
3994 }
3995 }
3996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 /*
3999 * Default home dir is C:/
4000 * Best assumption we can make in such a situation.
4001 */
4002 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004003 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (var != NULL)
4007 {
4008#ifdef UNIX
4009 /*
4010 * Change to the directory and get the actual path. This resolves
4011 * links. Don't do it when we can't return.
4012 */
4013 if (mch_dirname(NameBuff, MAXPATHL) == OK
4014 && mch_chdir((char *)NameBuff) == 0)
4015 {
4016 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4017 var = IObuff;
4018 if (mch_chdir((char *)NameBuff) != 0)
4019 EMSG(_(e_prev_dir));
4020 }
4021#endif
4022 homedir = vim_strsave(var);
4023 }
4024}
4025
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004026#if defined(EXITFREE) || defined(PROTO)
4027 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004028free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004029{
4030 vim_free(homedir);
4031}
Bram Moolenaar24305862012-08-15 14:05:05 +02004032
4033# ifdef FEAT_CMDL_COMPL
4034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004035free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004036{
4037 ga_clear_strings(&ga_users);
4038}
4039# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004040#endif
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004043 * Call expand_env() and store the result in an allocated string.
4044 * This is not very memory efficient, this expects the result to be freed
4045 * again soon.
4046 */
4047 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004048expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004049{
4050 return expand_env_save_opt(src, FALSE);
4051}
4052
4053/*
4054 * Idem, but when "one" is TRUE handle the string as one file name, only
4055 * expand "~" at the start.
4056 */
4057 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004058expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004059{
4060 char_u *p;
4061
4062 p = alloc(MAXPATHL);
4063 if (p != NULL)
4064 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4065 return p;
4066}
4067
4068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 * Expand environment variable with path name.
4070 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004071 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 * If anything fails no expansion is done and dst equals src.
4073 */
4074 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004075expand_env(
4076 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4077 char_u *dst, /* where to put the result */
4078 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004080 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081}
4082
4083 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004084expand_env_esc(
4085 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4086 char_u *dst, /* where to put the result */
4087 int dstlen, /* maximum length of the result */
4088 int esc, /* escape spaces in expanded variables */
4089 int one, /* "srcp" is one file name */
4090 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004092 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 char_u *tail;
4094 int c;
4095 char_u *var;
4096 int copy_char;
4097 int mustfree; /* var was allocated, need to free it later */
4098 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004099 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004101 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004102 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004103
4104 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 --dstlen; /* leave one char space for "\," */
4106 while (*src && dstlen > 0)
4107 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004108#ifdef FEAT_EVAL
4109 /* Skip over `=expr`. */
4110 if (src[0] == '`' && src[1] == '=')
4111 {
4112 size_t len;
4113
4114 var = src;
4115 src += 2;
4116 (void)skip_expr(&src);
4117 if (*src == '`')
4118 ++src;
4119 len = src - var;
4120 if (len > (size_t)dstlen)
4121 len = dstlen;
4122 vim_strncpy(dst, var, len);
4123 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004124 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004125 continue;
4126 }
4127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004129 if ((*src == '$'
4130#ifdef VMS
4131 && at_start
4132#endif
4133 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004134#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 || *src == '%'
4136#endif
4137 || (*src == '~' && at_start))
4138 {
4139 mustfree = FALSE;
4140
4141 /*
4142 * The variable name is copied into dst temporarily, because it may
4143 * be a string in read-only memory and a NUL needs to be appended.
4144 */
4145 if (*src != '~') /* environment var */
4146 {
4147 tail = src + 1;
4148 var = dst;
4149 c = dstlen - 1;
4150
4151#ifdef UNIX
4152 /* Unix has ${var-name} type environment vars */
4153 if (*tail == '{' && !vim_isIDc('{'))
4154 {
4155 tail++; /* ignore '{' */
4156 while (c-- > 0 && *tail && *tail != '}')
4157 *var++ = *tail++;
4158 }
4159 else
4160#endif
4161 {
4162 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004163#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 || (*src == '%' && *tail != '%')
4165#endif
4166 ))
4167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170 }
4171
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004172#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173# ifdef UNIX
4174 if (src[1] == '{' && *tail != '}')
4175# else
4176 if (*src == '%' && *tail != '%')
4177# endif
4178 var = NULL;
4179 else
4180 {
4181# ifdef UNIX
4182 if (src[1] == '{')
4183# else
4184 if (*src == '%')
4185#endif
4186 ++tail;
4187#endif
4188 *var = NUL;
4189 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004190#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192#endif
4193 }
4194 /* home directory */
4195 else if ( src[1] == NUL
4196 || vim_ispathsep(src[1])
4197 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4198 {
4199 var = homedir;
4200 tail = src + 1;
4201 }
4202 else /* user directory */
4203 {
4204#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4205 /*
4206 * Copy ~user to dst[], so we can put a NUL after it.
4207 */
4208 tail = src;
4209 var = dst;
4210 c = dstlen - 1;
4211 while ( c-- > 0
4212 && *tail
4213 && vim_isfilec(*tail)
4214 && !vim_ispathsep(*tail))
4215 *var++ = *tail++;
4216 *var = NUL;
4217# ifdef UNIX
4218 /*
4219 * If the system supports getpwnam(), use it.
4220 * Otherwise, or if getpwnam() fails, the shell is used to
4221 * expand ~user. This is slower and may fail if the shell
4222 * does not support ~user (old versions of /bin/sh).
4223 */
4224# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4225 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004226 /* Note: memory allocated by getpwnam() is never freed.
4227 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004228 struct passwd *pw = (*dst == NUL)
4229 ? NULL : getpwnam((char *)dst + 1);
4230
4231 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 if (var == NULL)
4234# endif
4235 {
4236 expand_T xpc;
4237
4238 ExpandInit(&xpc);
4239 xpc.xp_context = EXPAND_FILES;
4240 var = ExpandOne(&xpc, dst, NULL,
4241 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 mustfree = TRUE;
4243 }
4244
4245# else /* !UNIX, thus VMS */
4246 /*
4247 * USER_HOME is a comma-separated list of
4248 * directories to search for the user account in.
4249 */
4250 {
4251 char_u test[MAXPATHL], paths[MAXPATHL];
4252 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004253 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 STRCPY(paths, USER_HOME);
4256 next_path = paths;
4257 while (*next_path)
4258 {
4259 for (path = next_path; *next_path && *next_path != ',';
4260 next_path++);
4261 if (*next_path)
4262 *next_path++ = NUL;
4263 STRCPY(test, path);
4264 STRCAT(test, "/");
4265 STRCAT(test, dst + 1);
4266 if (mch_stat(test, &st) == 0)
4267 {
4268 var = alloc(STRLEN(test) + 1);
4269 STRCPY(var, test);
4270 mustfree = TRUE;
4271 break;
4272 }
4273 }
4274 }
4275# endif /* UNIX */
4276#else
4277 /* cannot expand user's home directory, so don't try */
4278 var = NULL;
4279 tail = (char_u *)""; /* for gcc */
4280#endif /* UNIX || VMS */
4281 }
4282
4283#ifdef BACKSLASH_IN_FILENAME
4284 /* If 'shellslash' is set change backslashes to forward slashes.
4285 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4286 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4287 {
4288 char_u *p = vim_strsave(var);
4289
4290 if (p != NULL)
4291 {
4292 if (mustfree)
4293 vim_free(var);
4294 var = p;
4295 mustfree = TRUE;
4296 forward_slash(var);
4297 }
4298 }
4299#endif
4300
4301 /* If "var" contains white space, escape it with a backslash.
4302 * Required for ":e ~/tt" when $HOME includes a space. */
4303 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4304 {
4305 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4306
4307 if (p != NULL)
4308 {
4309 if (mustfree)
4310 vim_free(var);
4311 var = p;
4312 mustfree = TRUE;
4313 }
4314 }
4315
4316 if (var != NULL && *var != NUL
4317 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4318 {
4319 STRCPY(dst, var);
4320 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004321 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 /* if var[] ends in a path separator and tail[] starts
4323 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004324 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4326 && dst[-1] != ':'
4327#endif
4328 && vim_ispathsep(*tail))
4329 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004330 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 src = tail;
4332 copy_char = FALSE;
4333 }
4334 if (mustfree)
4335 vim_free(var);
4336 }
4337
4338 if (copy_char) /* copy at least one char */
4339 {
4340 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004341 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004342 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4343 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 */
4345 at_start = FALSE;
4346 if (src[0] == '\\' && src[1] != NUL)
4347 {
4348 *dst++ = *src++;
4349 --dstlen;
4350 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004351 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004353 if (dstlen > 0)
4354 {
4355 *dst++ = *src++;
4356 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004357
Bram Moolenaar1c864092017-08-06 18:15:45 +02004358 if (startstr != NULL && src - startstr_len >= srcp
4359 && STRNCMP(src - startstr_len, startstr,
4360 startstr_len) == 0)
4361 at_start = TRUE;
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 *dst = NUL;
4367}
4368
4369/*
4370 * Vim's version of getenv().
4371 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004372 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004373 * "mustfree" is set to TRUE when returned is allocated, it must be
4374 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 */
4376 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004377vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
4379 char_u *p;
4380 char_u *pend;
4381 int vimruntime;
4382
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004383#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 /* use "C:/" when $HOME is not set */
4385 if (STRCMP(name, "HOME") == 0)
4386 return homedir;
4387#endif
4388
4389 p = mch_getenv(name);
4390 if (p != NULL && *p == NUL) /* empty is the same as not set */
4391 p = NULL;
4392
4393 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004394 {
4395#if defined(FEAT_MBYTE) && defined(WIN3264)
4396 if (enc_utf8)
4397 {
4398 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004399 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004400
4401 /* Convert from active codepage to UTF-8. Other conversions are
4402 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004403 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004404 if (pp != NULL)
4405 {
4406 p = pp;
4407 *mustfree = TRUE;
4408 }
4409 }
4410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4415 if (!vimruntime && STRCMP(name, "VIM") != 0)
4416 return NULL;
4417
4418 /*
4419 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4420 * Don't do this when default_vimruntime_dir is non-empty.
4421 */
4422 if (vimruntime
4423#ifdef HAVE_PATHDEF
4424 && *default_vimruntime_dir == NUL
4425#endif
4426 )
4427 {
4428 p = mch_getenv((char_u *)"VIM");
4429 if (p != NULL && *p == NUL) /* empty is the same as not set */
4430 p = NULL;
4431 if (p != NULL)
4432 {
4433 p = vim_version_dir(p);
4434 if (p != NULL)
4435 *mustfree = TRUE;
4436 else
4437 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004438
4439#if defined(FEAT_MBYTE) && defined(WIN3264)
4440 if (enc_utf8)
4441 {
4442 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004443 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004444
4445 /* Convert from active codepage to UTF-8. Other conversions
4446 * are not done, because they would fail for non-ASCII
4447 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004448 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004449 if (pp != NULL)
4450 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004451 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004452 vim_free(p);
4453 p = pp;
4454 *mustfree = TRUE;
4455 }
4456 }
4457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459 }
4460
4461 /*
4462 * When expanding $VIM or $VIMRUNTIME fails, try using:
4463 * - the directory name from 'helpfile' (unless it contains '$')
4464 * - the executable name from argv[0]
4465 */
4466 if (p == NULL)
4467 {
4468 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4469 p = p_hf;
4470#ifdef USE_EXE_NAME
4471 /*
4472 * Use the name of the executable, obtained from argv[0].
4473 */
4474 else
4475 p = exe_name;
4476#endif
4477 if (p != NULL)
4478 {
4479 /* remove the file name */
4480 pend = gettail(p);
4481
4482 /* remove "doc/" from 'helpfile', if present */
4483 if (p == p_hf)
4484 pend = remove_tail(p, pend, (char_u *)"doc");
4485
4486#ifdef USE_EXE_NAME
4487# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004488 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (p == exe_name)
4490 {
4491 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004492 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004494 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4495 if (pend1 != pend)
4496 {
4497 pnew = alloc((unsigned)(pend1 - p) + 15);
4498 if (pnew != NULL)
4499 {
4500 STRNCPY(pnew, p, (pend1 - p));
4501 STRCPY(pnew + (pend1 - p), "Resources/vim");
4502 p = pnew;
4503 pend = p + STRLEN(p);
4504 }
4505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507# endif
4508 /* remove "src/" from exe_name, if present */
4509 if (p == exe_name)
4510 pend = remove_tail(p, pend, (char_u *)"src");
4511#endif
4512
4513 /* for $VIM, remove "runtime/" or "vim54/", if present */
4514 if (!vimruntime)
4515 {
4516 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4517 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4518 }
4519
4520 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004521 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004524#ifdef MACOS_X
4525 if (p == exe_name || p == p_hf)
4526#endif
4527 /* check that the result is a directory name */
4528 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
4530 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004531 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else
4533 {
4534#ifdef USE_EXE_NAME
4535 /* may add "/vim54" or "/runtime" if it exists */
4536 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4537 {
4538 vim_free(p);
4539 p = pend;
4540 }
4541#endif
4542 *mustfree = TRUE;
4543 }
4544 }
4545 }
4546
4547#ifdef HAVE_PATHDEF
4548 /* When there is a pathdef.c file we can use default_vim_dir and
4549 * default_vimruntime_dir */
4550 if (p == NULL)
4551 {
4552 /* Only use default_vimruntime_dir when it is not empty */
4553 if (vimruntime && *default_vimruntime_dir != NUL)
4554 {
4555 p = default_vimruntime_dir;
4556 *mustfree = FALSE;
4557 }
4558 else if (*default_vim_dir != NUL)
4559 {
4560 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4561 *mustfree = TRUE;
4562 else
4563 {
4564 p = default_vim_dir;
4565 *mustfree = FALSE;
4566 }
4567 }
4568 }
4569#endif
4570
4571 /*
4572 * Set the environment variable, so that the new value can be found fast
4573 * next time, and others can also use it (e.g. Perl).
4574 */
4575 if (p != NULL)
4576 {
4577 if (vimruntime)
4578 {
4579 vim_setenv((char_u *)"VIMRUNTIME", p);
4580 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 else
4583 {
4584 vim_setenv((char_u *)"VIM", p);
4585 didset_vim = TRUE;
4586 }
4587 }
4588 return p;
4589}
4590
4591/*
4592 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4593 * Return NULL if not, return its name in allocated memory otherwise.
4594 */
4595 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004596vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597{
4598 char_u *p;
4599
4600 if (vimdir == NULL || *vimdir == NUL)
4601 return NULL;
4602 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4603 if (p != NULL && mch_isdir(p))
4604 return p;
4605 vim_free(p);
4606 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4607 if (p != NULL && mch_isdir(p))
4608 return p;
4609 vim_free(p);
4610 return NULL;
4611}
4612
4613/*
4614 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4615 * the length of "name/". Otherwise return "pend".
4616 */
4617 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004618remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619{
4620 int len = (int)STRLEN(name) + 1;
4621 char_u *newend = pend - len;
4622
4623 if (newend >= p
4624 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004625 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 return newend;
4627 return pend;
4628}
4629
Bram Moolenaar137374f2018-05-13 15:59:50 +02004630 void
4631vim_unsetenv(char_u *var)
4632{
4633#ifdef HAVE_UNSETENV
4634 unsetenv((char *)var);
4635#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004636 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004637#endif
4638}
4639
4640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 * Our portable version of setenv.
4643 */
4644 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004645vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
4647#ifdef HAVE_SETENV
4648 mch_setenv((char *)name, (char *)val, 1);
4649#else
4650 char_u *envbuf;
4651
4652 /*
4653 * Putenv does not copy the string, it has to remain
4654 * valid. The allocated memory will never be freed.
4655 */
4656 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4657 if (envbuf != NULL)
4658 {
4659 sprintf((char *)envbuf, "%s=%s", name, val);
4660 putenv((char *)envbuf);
4661 }
4662#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004663#ifdef FEAT_GETTEXT
4664 /*
4665 * When setting $VIMRUNTIME adjust the directory to find message
4666 * translations to $VIMRUNTIME/lang.
4667 */
4668 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4669 {
4670 char_u *buf = concat_str(val, (char_u *)"/lang");
4671
4672 if (buf != NULL)
4673 {
4674 bindtextdomain(VIMPACKAGE, (char *)buf);
4675 vim_free(buf);
4676 }
4677 }
4678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679}
4680
4681#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4682/*
4683 * Function given to ExpandGeneric() to obtain an environment variable name.
4684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004686get_env_name(
4687 expand_T *xp UNUSED,
4688 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
Bram Moolenaard0573012017-10-28 21:11:06 +02004690# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004692 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 */
4694 return NULL;
4695# else
4696# ifndef __WIN32__
4697 /* Borland C++ 5.2 has this in a header file. */
4698 extern char **environ;
4699# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004700# define ENVNAMELEN 100
4701 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 char_u *str;
4703 int n;
4704
4705 str = (char_u *)environ[idx];
4706 if (str == NULL)
4707 return NULL;
4708
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004709 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 if (str[n] == '=' || str[n] == NUL)
4712 break;
4713 name[n] = str[n];
4714 }
4715 name[n] = NUL;
4716 return name;
4717# endif
4718}
Bram Moolenaar24305862012-08-15 14:05:05 +02004719
4720/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004721 * Add a user name to the list of users in ga_users.
4722 * Do nothing if user name is NULL or empty.
4723 */
4724 static void
4725add_user(char_u *user, int need_copy)
4726{
4727 char_u *user_copy = (user != NULL && need_copy)
4728 ? vim_strsave(user) : user;
4729
4730 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4731 {
4732 if (need_copy)
4733 vim_free(user);
4734 return;
4735 }
4736 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4737}
4738
4739/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004740 * Find all user names for user completion.
4741 * Done only once and then cached.
4742 */
4743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004744init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004745{
Bram Moolenaar24305862012-08-15 14:05:05 +02004746 static int lazy_init_done = FALSE;
4747
4748 if (lazy_init_done)
4749 return;
4750
4751 lazy_init_done = TRUE;
4752 ga_init2(&ga_users, sizeof(char_u *), 20);
4753
4754# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4755 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004756 struct passwd* pw;
4757
4758 setpwent();
4759 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004760 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004761 endpwent();
4762 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004763# elif defined(WIN3264)
4764 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004765 DWORD nusers = 0, ntotal = 0, i;
4766 PUSER_INFO_0 uinfo;
4767
4768 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4769 &nusers, &ntotal, NULL) == NERR_Success)
4770 {
4771 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004772 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004773
4774 NetApiBufferFree(uinfo);
4775 }
4776 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004777# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004778# if defined(HAVE_GETPWNAM)
4779 {
4780 char_u *user_env = mch_getenv((char_u *)"USER");
4781
4782 // The $USER environment variable may be a valid remote user name (NIS,
4783 // LDAP) not already listed by getpwent(), as getpwent() only lists
4784 // local user names. If $USER is not already listed, check whether it
4785 // is a valid remote user name using getpwnam() and if it is, add it to
4786 // the list of user names.
4787
4788 if (user_env != NULL && *user_env != NUL)
4789 {
4790 int i;
4791
4792 for (i = 0; i < ga_users.ga_len; i++)
4793 {
4794 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4795
4796 if (STRCMP(local_user, user_env) == 0)
4797 break;
4798 }
4799
4800 if (i == ga_users.ga_len)
4801 {
4802 struct passwd *pw = getpwnam((char *)user_env);
4803
4804 if (pw != NULL)
4805 add_user((char_u *)pw->pw_name, TRUE);
4806 }
4807 }
4808 }
4809# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004810}
4811
4812/*
4813 * Function given to ExpandGeneric() to obtain an user names.
4814 */
4815 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004816get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004817{
4818 init_users();
4819 if (idx < ga_users.ga_len)
4820 return ((char_u **)ga_users.ga_data)[idx];
4821 return NULL;
4822}
4823
4824/*
4825 * Check whether name matches a user name. Return:
4826 * 0 if name does not match any user name.
4827 * 1 if name partially matches the beginning of a user name.
4828 * 2 is name fully matches a user name.
4829 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004830 int
4831match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004832{
4833 int i;
4834 int n = (int)STRLEN(name);
4835 int result = 0;
4836
4837 init_users();
4838 for (i = 0; i < ga_users.ga_len; i++)
4839 {
4840 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4841 return 2; /* full match */
4842 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4843 result = 1; /* partial match */
4844 }
4845 return result;
4846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#endif
4848
4849/*
4850 * Replace home directory by "~" in each space or comma separated file name in
4851 * 'src'.
4852 * If anything fails (except when out of space) dst equals src.
4853 */
4854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004855home_replace(
4856 buf_T *buf, /* when not NULL, check for help files */
4857 char_u *src, /* input file name */
4858 char_u *dst, /* where to put the result */
4859 int dstlen, /* maximum length of the result */
4860 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 spaces and commas in the file name. */
4862{
4863 size_t dirlen = 0, envlen = 0;
4864 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004865 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 char_u *p;
4867
4868 if (src == NULL)
4869 {
4870 *dst = NUL;
4871 return;
4872 }
4873
4874 /*
4875 * If the file is a help file, remove the path completely.
4876 */
4877 if (buf != NULL && buf->b_help)
4878 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004879 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 return;
4881 }
4882
4883 /*
4884 * We check both the value of the $HOME environment variable and the
4885 * "real" home directory.
4886 */
4887 if (homedir != NULL)
4888 dirlen = STRLEN(homedir);
4889
4890#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004891 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004893 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4894#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004895#ifdef WIN3264
4896 if (homedir_env == NULL)
4897 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4898#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004899 /* Empty is the same as not set. */
4900 if (homedir_env != NULL && *homedir_env == NUL)
4901 homedir_env = NULL;
4902
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004903#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004904 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004905 {
4906 int usedlen = 0;
4907 int flen;
4908 char_u *fbuf = NULL;
4909
4910 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004911 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004912 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004913 flen = (int)STRLEN(homedir_env);
4914 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4915 /* Remove the trailing / that is added to a directory. */
4916 homedir_env[flen - 1] = NUL;
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918#endif
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (homedir_env != NULL)
4921 envlen = STRLEN(homedir_env);
4922
4923 if (!one)
4924 src = skipwhite(src);
4925 while (*src && dstlen > 0)
4926 {
4927 /*
4928 * Here we are at the beginning of a file name.
4929 * First, check to see if the beginning of the file name matches
4930 * $HOME or the "real" home directory. Check that there is a '/'
4931 * after the match (so that if e.g. the file is "/home/pieter/bla",
4932 * and the home directory is "/home/piet", the file does not end up
4933 * as "~er/bla" (which would seem to indicate the file "bla" in user
4934 * er's home directory)).
4935 */
4936 p = homedir;
4937 len = dirlen;
4938 for (;;)
4939 {
4940 if ( len
4941 && fnamencmp(src, p, len) == 0
4942 && (vim_ispathsep(src[len])
4943 || (!one && (src[len] == ',' || src[len] == ' '))
4944 || src[len] == NUL))
4945 {
4946 src += len;
4947 if (--dstlen > 0)
4948 *dst++ = '~';
4949
4950 /*
4951 * If it's just the home directory, add "/".
4952 */
4953 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4954 *dst++ = '/';
4955 break;
4956 }
4957 if (p == homedir_env)
4958 break;
4959 p = homedir_env;
4960 len = envlen;
4961 }
4962
4963 /* if (!one) skip to separator: space or comma */
4964 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4965 *dst++ = *src++;
4966 /* skip separator */
4967 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4968 *dst++ = *src++;
4969 }
4970 /* if (dstlen == 0) out of space, what to do??? */
4971
4972 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004973
4974 if (homedir_env != homedir_env_orig)
4975 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976}
4977
4978/*
4979 * Like home_replace, store the replaced string in allocated memory.
4980 * When something fails, NULL is returned.
4981 */
4982 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004983home_replace_save(
4984 buf_T *buf, /* when not NULL, check for help files */
4985 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987 char_u *dst;
4988 unsigned len;
4989
4990 len = 3; /* space for "~/" and trailing NUL */
4991 if (src != NULL) /* just in case */
4992 len += (unsigned)STRLEN(src);
4993 dst = alloc(len);
4994 if (dst != NULL)
4995 home_replace(buf, src, dst, len, TRUE);
4996 return dst;
4997}
4998
4999/*
5000 * Compare two file names and return:
5001 * FPC_SAME if they both exist and are the same file.
5002 * FPC_SAMEX if they both don't exist and have the same file name.
5003 * FPC_DIFF if they both exist and are different files.
5004 * FPC_NOTX if they both don't exist.
5005 * FPC_DIFFX if one of them doesn't exist.
5006 * For the first name environment variables are expanded
5007 */
5008 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005009fullpathcmp(
5010 char_u *s1,
5011 char_u *s2,
5012 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014#ifdef UNIX
5015 char_u exp1[MAXPATHL];
5016 char_u full1[MAXPATHL];
5017 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005018 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 int r1, r2;
5020
5021 expand_env(s1, exp1, MAXPATHL);
5022 r1 = mch_stat((char *)exp1, &st1);
5023 r2 = mch_stat((char *)s2, &st2);
5024 if (r1 != 0 && r2 != 0)
5025 {
5026 /* if mch_stat() doesn't work, may compare the names */
5027 if (checkname)
5028 {
5029 if (fnamecmp(exp1, s2) == 0)
5030 return FPC_SAMEX;
5031 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5032 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5033 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5034 return FPC_SAMEX;
5035 }
5036 return FPC_NOTX;
5037 }
5038 if (r1 != 0 || r2 != 0)
5039 return FPC_DIFFX;
5040 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5041 return FPC_SAME;
5042 return FPC_DIFF;
5043#else
5044 char_u *exp1; /* expanded s1 */
5045 char_u *full1; /* full path of s1 */
5046 char_u *full2; /* full path of s2 */
5047 int retval = FPC_DIFF;
5048 int r1, r2;
5049
5050 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5051 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5052 {
5053 full1 = exp1 + MAXPATHL;
5054 full2 = full1 + MAXPATHL;
5055
5056 expand_env(s1, exp1, MAXPATHL);
5057 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5058 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5059
5060 /* If vim_FullName() fails, the file probably doesn't exist. */
5061 if (r1 != OK && r2 != OK)
5062 {
5063 if (checkname && fnamecmp(exp1, s2) == 0)
5064 retval = FPC_SAMEX;
5065 else
5066 retval = FPC_NOTX;
5067 }
5068 else if (r1 != OK || r2 != OK)
5069 retval = FPC_DIFFX;
5070 else if (fnamecmp(full1, full2))
5071 retval = FPC_DIFF;
5072 else
5073 retval = FPC_SAME;
5074 vim_free(exp1);
5075 }
5076 return retval;
5077#endif
5078}
5079
5080/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005081 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005082 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005083 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 */
5085 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005086gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
5088 char_u *p1, *p2;
5089
5090 if (fname == NULL)
5091 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005092 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005094 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005096 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 return p1;
5099}
5100
Bram Moolenaar31710262010-08-13 13:36:15 +02005101#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005102/*
5103 * Return the end of the directory name, on the first path
5104 * separator:
5105 * "/path/file", "/path/dir/", "/path//dir", "/file"
5106 * ^ ^ ^ ^
5107 */
5108 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005109gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005110{
5111 char_u *dir_end = fname;
5112 char_u *next_dir_end = fname;
5113 int look_for_sep = TRUE;
5114 char_u *p;
5115
5116 for (p = fname; *p != NUL; )
5117 {
5118 if (vim_ispathsep(*p))
5119 {
5120 if (look_for_sep)
5121 {
5122 next_dir_end = p;
5123 look_for_sep = FALSE;
5124 }
5125 }
5126 else
5127 {
5128 if (!look_for_sep)
5129 dir_end = next_dir_end;
5130 look_for_sep = TRUE;
5131 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005132 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005133 }
5134 return dir_end;
5135}
5136#endif
5137
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005139 * Get pointer to tail of "fname", including path separators. Putting a NUL
5140 * here leaves the directory name. Takes care of "c:/" and "//".
5141 * Always returns a valid pointer.
5142 */
5143 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005144gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005145{
5146 char_u *p;
5147 char_u *t;
5148
5149 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5150 t = gettail(fname);
5151 while (t > p && after_pathsep(fname, t))
5152 --t;
5153#ifdef VMS
5154 /* path separator is part of the path */
5155 ++t;
5156#endif
5157 return t;
5158}
5159
5160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 * get the next path component (just after the next path separator).
5162 */
5163 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005164getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
5166 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005167 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (*fname)
5169 ++fname;
5170 return fname;
5171}
5172
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173/*
5174 * Get a pointer to one character past the head of a path name.
5175 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5176 * If there is no head, path is returned.
5177 */
5178 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005179get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180{
5181 char_u *retval;
5182
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005183#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 /* may skip "c:" */
5185 if (isalpha(path[0]) && path[1] == ':')
5186 retval = path + 2;
5187 else
5188 retval = path;
5189#else
5190# if defined(AMIGA)
5191 /* may skip "label:" */
5192 retval = vim_strchr(path, ':');
5193 if (retval == NULL)
5194 retval = path;
5195# else /* Unix */
5196 retval = path;
5197# endif
5198#endif
5199
5200 while (vim_ispathsep(*retval))
5201 ++retval;
5202
5203 return retval;
5204}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005207 * Return TRUE if 'c' is a path separator.
5208 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 */
5210 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005213#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005215#else
5216# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005218# else
5219# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5221 return (c == ':' || c == '[' || c == ']' || c == '/'
5222 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005223# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005225# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228}
5229
Bram Moolenaar69c35002013-11-04 02:54:12 +01005230/*
5231 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5232 */
5233 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005234vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005235{
5236 return vim_ispathsep(c)
5237#ifdef BACKSLASH_IN_FILENAME
5238 && c != ':'
5239#endif
5240 ;
5241}
5242
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5244/*
5245 * return TRUE if 'c' is a path list separator.
5246 */
5247 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005248vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
5250#ifdef UNIX
5251 return (c == ':');
5252#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005253 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#endif
5255}
5256#endif
5257
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005258/*
5259 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5260 * It's done in-place.
5261 */
5262 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005263shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005264{
5265 char_u *tail, *s, *d;
5266 int skip = FALSE;
5267
5268 tail = gettail(str);
5269 d = str;
5270 for (s = str; ; ++s)
5271 {
5272 if (s >= tail) /* copy the whole tail */
5273 {
5274 *d++ = *s;
5275 if (*s == NUL)
5276 break;
5277 }
5278 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5279 {
5280 *d++ = *s;
5281 skip = FALSE;
5282 }
5283 else if (!skip)
5284 {
5285 *d++ = *s; /* copy next char */
5286 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5287 skip = TRUE;
5288# ifdef FEAT_MBYTE
5289 if (has_mbyte)
5290 {
5291 int l = mb_ptr2len(s);
5292
5293 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005294 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005295 }
5296# endif
5297 }
5298 }
5299}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005300
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005301/*
5302 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5303 * Also returns TRUE if there is no directory name.
5304 * "fname" must be writable!.
5305 */
5306 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005307dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005308{
5309 char_u *p;
5310 int c;
5311 int retval;
5312
5313 p = gettail_sep(fname);
5314 if (p == fname)
5315 return TRUE;
5316 c = *p;
5317 *p = NUL;
5318 retval = mch_isdir(fname);
5319 *p = c;
5320 return retval;
5321}
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005324 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5325 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 */
5327 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005328vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005330#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005332#else
5333 if (p_fic)
5334 return MB_STRICMP(x, y);
5335 return STRCMP(x, y);
5336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337}
5338
5339 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005340vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005342#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005343 char_u *px = x;
5344 char_u *py = y;
5345 int cx = NUL;
5346 int cy = NUL;
5347
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005348 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005350 cx = PTR2CHAR(px);
5351 cy = PTR2CHAR(py);
5352 if (cx == NUL || cy == NUL
5353 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5354 && !(cx == '/' && cy == '\\')
5355 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005357 len -= MB_PTR2LEN(px);
5358 px += MB_PTR2LEN(px);
5359 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361 if (len == 0)
5362 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005363 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005364#else
5365 if (p_fic)
5366 return MB_STRNICMP(x, y, len);
5367 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005369}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
5371/*
5372 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005373 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 */
5375 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005376concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 char_u *dest;
5379
5380 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5381 if (dest != NULL)
5382 {
5383 STRCPY(dest, fname1);
5384 if (sep)
5385 add_pathsep(dest);
5386 STRCAT(dest, fname2);
5387 }
5388 return dest;
5389}
5390
Bram Moolenaard6754642005-01-17 22:18:45 +00005391/*
5392 * Concatenate two strings and return the result in allocated memory.
5393 * Returns NULL when out of memory.
5394 */
5395 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005396concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005397{
5398 char_u *dest;
5399 size_t l = STRLEN(str1);
5400
5401 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5402 if (dest != NULL)
5403 {
5404 STRCPY(dest, str1);
5405 STRCPY(dest + l, str2);
5406 }
5407 return dest;
5408}
Bram Moolenaard6754642005-01-17 22:18:45 +00005409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410/*
5411 * Add a path separator to a file name, unless it already ends in a path
5412 * separator.
5413 */
5414 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005415add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005417 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 STRCAT(p, PATHSEPSTR);
5419}
5420
5421/*
5422 * FullName_save - Make an allocated copy of a full file name.
5423 * Returns NULL when out of memory.
5424 */
5425 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005426FullName_save(
5427 char_u *fname,
5428 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005429 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 char_u *buf;
5432 char_u *new_fname = NULL;
5433
5434 if (fname == NULL)
5435 return NULL;
5436
5437 buf = alloc((unsigned)MAXPATHL);
5438 if (buf != NULL)
5439 {
5440 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5441 new_fname = vim_strsave(buf);
5442 else
5443 new_fname = vim_strsave(fname);
5444 vim_free(buf);
5445 }
5446 return new_fname;
5447}
5448
5449#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5450
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005451static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005452static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454/*
5455 * Find the start of a comment, not knowing if we are in a comment right now.
5456 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005457 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005459 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005460ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005461{
5462 return find_start_comment(curbuf->b_ind_maxcomment);
5463}
5464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005466find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 pos_T *pos;
5469 char_u *line;
5470 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005471 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005473 for (;;)
5474 {
5475 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5476 if (pos == NULL)
5477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005479 /*
5480 * Check if the comment start we found is inside a string.
5481 * If it is then restrict the search to below this line and try again.
5482 */
5483 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005484 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005485 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005486 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005487 break;
5488 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5489 if (cur_maxcomment <= 0)
5490 {
5491 pos = NULL;
5492 break;
5493 }
5494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 return pos;
5496}
5497
5498/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005499 * Find the start of a comment or raw string, not knowing if we are in a
5500 * comment or raw string right now.
5501 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005502 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005503 * Return NULL when not inside a comment or raw string.
5504 * "CORS" -> Comment Or Raw String
5505 */
5506 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005507ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005508{
Bram Moolenaar089af182015-10-07 11:41:49 +02005509 static pos_T comment_pos_copy;
5510 pos_T *comment_pos;
5511 pos_T *rs_pos;
5512
5513 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5514 if (comment_pos != NULL)
5515 {
5516 /* Need to make a copy of the static pos in findmatchlimit(),
5517 * calling find_start_rawstring() may change it. */
5518 comment_pos_copy = *comment_pos;
5519 comment_pos = &comment_pos_copy;
5520 }
5521 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005522
5523 /* If comment_pos is before rs_pos the raw string is inside the comment.
5524 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005525 if (comment_pos == NULL || (rs_pos != NULL
5526 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005527 {
5528 if (is_raw != NULL && rs_pos != NULL)
5529 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005530 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005531 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005532 return comment_pos;
5533}
5534
5535/*
5536 * Find the start of a raw string, not knowing if we are in one right now.
5537 * Search starts at w_cursor.lnum and goes backwards.
5538 * Return NULL when not inside a raw string.
5539 */
5540 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005541find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005542{
5543 pos_T *pos;
5544 char_u *line;
5545 char_u *p;
5546 int cur_maxcomment = ind_maxcomment;
5547
5548 for (;;)
5549 {
5550 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5551 if (pos == NULL)
5552 break;
5553
5554 /*
5555 * Check if the raw string start we found is inside a string.
5556 * If it is then restrict the search to below this line and try again.
5557 */
5558 line = ml_get(pos->lnum);
5559 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5560 p = skip_string(p);
5561 if ((colnr_T)(p - line) <= pos->col)
5562 break;
5563 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5564 if (cur_maxcomment <= 0)
5565 {
5566 pos = NULL;
5567 break;
5568 }
5569 }
5570 return pos;
5571}
5572
5573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 * Skip to the end of a "string" and a 'c' character.
5575 * If there is no string or character, return argument unmodified.
5576 */
5577 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 int i;
5581
5582 /*
5583 * We loop, because strings may be concatenated: "date""time".
5584 */
5585 for ( ; ; ++p)
5586 {
5587 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5588 {
5589 if (!p[1]) /* ' at end of line */
5590 break;
5591 i = 2;
5592 if (p[1] == '\\') /* '\n' or '\000' */
5593 {
5594 ++i;
5595 while (vim_isdigit(p[i - 1])) /* '\000' */
5596 ++i;
5597 }
5598 if (p[i] == '\'') /* check for trailing ' */
5599 {
5600 p += i;
5601 continue;
5602 }
5603 }
5604 else if (p[0] == '"') /* start of string */
5605 {
5606 for (++p; p[0]; ++p)
5607 {
5608 if (p[0] == '\\' && p[1] != NUL)
5609 ++p;
5610 else if (p[0] == '"') /* end of string */
5611 break;
5612 }
5613 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005614 continue; /* continue for another string */
5615 }
5616 else if (p[0] == 'R' && p[1] == '"')
5617 {
5618 /* Raw string: R"[delim](...)[delim]" */
5619 char_u *delim = p + 2;
5620 char_u *paren = vim_strchr(delim, '(');
5621
5622 if (paren != NULL)
5623 {
5624 size_t delim_len = paren - delim;
5625
5626 for (p += 3; *p; ++p)
5627 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5628 && p[delim_len + 1] == '"')
5629 {
5630 p += delim_len + 1;
5631 break;
5632 }
5633 if (p[0] == '"')
5634 continue; /* continue for another string */
5635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 break; /* no string found */
5638 }
5639 if (!*p)
5640 --p; /* backup from NUL */
5641 return p;
5642}
5643#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5644
5645#if defined(FEAT_CINDENT) || defined(PROTO)
5646
5647/*
5648 * Do C or expression indenting on the current line.
5649 */
5650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005651do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653# ifdef FEAT_EVAL
5654 if (*curbuf->b_p_inde != NUL)
5655 fixthisline(get_expr_indent);
5656 else
5657# endif
5658 fixthisline(get_c_indent);
5659}
5660
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005661/* Find result cache for cpp_baseclass */
5662typedef struct {
5663 int found;
5664 lpos_T lpos;
5665} cpp_baseclass_cache_T;
5666
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667/*
5668 * Functions for C-indenting.
5669 * Most of this originally comes from Eric Fischer.
5670 */
5671/*
5672 * Below "XXX" means that this function may unlock the current line.
5673 */
5674
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005675static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005676static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005677static int cin_iscomment(char_u *);
5678static int cin_islinecomment(char_u *);
5679static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005680static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005681static int cin_ends_in(char_u *, char_u *, char_u *);
5682static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005683static pos_T *find_match_paren(int);
5684static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005685static int find_last_paren(char_u *l, int start, int end);
5686static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688/*
5689 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005690 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 */
5692 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005693cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 while (*s)
5696 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005697 char_u *prev_s = s;
5698
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005700
5701 /* Perl/shell # comment comment continues until eol. Require a space
5702 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005703 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005704 {
5705 s += STRLEN(s);
5706 break;
5707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (*s != '/')
5709 break;
5710 ++s;
5711 if (*s == '/') /* slash-slash comment continues till eol */
5712 {
5713 s += STRLEN(s);
5714 break;
5715 }
5716 if (*s != '*')
5717 break;
5718 for (++s; *s; ++s) /* skip slash-star comment */
5719 if (s[0] == '*' && s[1] == '/')
5720 {
5721 s += 2;
5722 break;
5723 }
5724 }
5725 return s;
5726}
5727
5728/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005729 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 * not considered code.
5731 */
5732 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005733cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735 return *cin_skipcomment(s) == NUL;
5736}
5737
5738/*
5739 * Check previous lines for a "//" line comment, skipping over blank lines.
5740 */
5741 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005742find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744 static pos_T pos;
5745 char_u *line;
5746 char_u *p;
5747
5748 pos = curwin->w_cursor;
5749 while (--pos.lnum > 0)
5750 {
5751 line = ml_get(pos.lnum);
5752 p = skipwhite(line);
5753 if (cin_islinecomment(p))
5754 {
5755 pos.col = (int)(p - line);
5756 return &pos;
5757 }
5758 if (*p != NUL)
5759 break;
5760 }
5761 return NULL;
5762}
5763
5764/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005765 * Return TRUE if "text" starts with "key:".
5766 */
5767 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005768cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005769{
5770 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005771 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005772
5773 if (*s == '\'' || *s == '"')
5774 {
5775 /* can be 'key': or "key": */
5776 quote = *s;
5777 ++s;
5778 }
5779 if (!vim_isIDc(*s)) /* need at least one ID character */
5780 return FALSE;
5781
5782 while (vim_isIDc(*s))
5783 ++s;
5784 if (*s == quote)
5785 ++s;
5786
5787 s = cin_skipcomment(s);
5788
5789 /* "::" is not a label, it's C++ */
5790 return (*s == ':' && s[1] != ':');
5791}
5792
5793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005795 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 */
5797 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005798cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 if (!vim_isIDc(**s)) /* need at least one ID character */
5801 return FALSE;
5802
5803 while (vim_isIDc(**s))
5804 (*s)++;
5805
5806 *s = cin_skipcomment(*s);
5807
5808 /* "::" is not a label, it's C++ */
5809 return (**s == ':' && *++*s != ':');
5810}
5811
5812/*
5813 * Recognize a label: "label:".
5814 * Note: curwin->w_cursor must be where we are looking for the label.
5815 */
5816 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005817cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819 char_u *s;
5820
5821 s = cin_skipcomment(ml_get_curline());
5822
5823 /*
5824 * Exclude "default" from labels, since it should be indented
5825 * like a switch label. Same for C++ scope declarations.
5826 */
5827 if (cin_isdefault(s))
5828 return FALSE;
5829 if (cin_isscopedecl(s))
5830 return FALSE;
5831
5832 if (cin_islabel_skip(&s))
5833 {
5834 /*
5835 * Only accept a label if the previous line is terminated or is a case
5836 * label.
5837 */
5838 pos_T cursor_save;
5839 pos_T *trypos;
5840 char_u *line;
5841
5842 cursor_save = curwin->w_cursor;
5843 while (curwin->w_cursor.lnum > 1)
5844 {
5845 --curwin->w_cursor.lnum;
5846
5847 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005848 * If we're in a comment or raw string now, skip to the start of
5849 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 */
5851 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005852 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 curwin->w_cursor = *trypos;
5854
5855 line = ml_get_curline();
5856 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5857 continue;
5858 if (*(line = cin_skipcomment(line)) == NUL)
5859 continue;
5860
5861 curwin->w_cursor = cursor_save;
5862 if (cin_isterminated(line, TRUE, FALSE)
5863 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005864 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 || (cin_islabel_skip(&line) && cin_nocode(line)))
5866 return TRUE;
5867 return FALSE;
5868 }
5869 curwin->w_cursor = cursor_save;
5870 return TRUE; /* label at start of file??? */
5871 }
5872 return FALSE;
5873}
5874
5875/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005876 * Recognize structure initialization and enumerations:
5877 * "[typedef] [static|public|protected|private] enum"
5878 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 */
5880 static int
5881cin_isinit(void)
5882{
5883 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005884 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
5886 s = cin_skipcomment(ml_get_curline());
5887
Bram Moolenaar75342212013-03-07 13:13:52 +01005888 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 s = cin_skipcomment(s + 7);
5890
Bram Moolenaar75342212013-03-07 13:13:52 +01005891 for (;;)
5892 {
5893 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005894
Bram Moolenaar75342212013-03-07 13:13:52 +01005895 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5896 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005897 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005898 if (cin_starts_with(s, skip[i]))
5899 {
5900 s = cin_skipcomment(s + l);
5901 l = 0;
5902 break;
5903 }
5904 }
5905 if (l != 0)
5906 break;
5907 }
5908
5909 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 return TRUE;
5911
5912 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5913 return TRUE;
5914
5915 return FALSE;
5916}
5917
5918/*
5919 * Recognize a switch label: "case .*:" or "default:".
5920 */
5921 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005922cin_iscase(
5923 char_u *s,
5924 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925{
5926 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005927 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
5929 for (s += 4; *s; ++s)
5930 {
5931 s = cin_skipcomment(s);
5932 if (*s == ':')
5933 {
5934 if (s[1] == ':') /* skip over "::" for C++ */
5935 ++s;
5936 else
5937 return TRUE;
5938 }
5939 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005940 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5942 return FALSE; /* stop at comment */
5943 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005944 {
5945 /* JS etc. */
5946 if (strict)
5947 return FALSE; /* stop at string */
5948 else
5949 return TRUE;
5950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 }
5952 return FALSE;
5953 }
5954
5955 if (cin_isdefault(s))
5956 return TRUE;
5957 return FALSE;
5958}
5959
5960/*
5961 * Recognize a "default" switch label.
5962 */
5963 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005964cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965{
5966 return (STRNCMP(s, "default", 7) == 0
5967 && *(s = cin_skipcomment(s + 7)) == ':'
5968 && s[1] != ':');
5969}
5970
5971/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005972 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 */
5974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005975cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 int i;
5978
5979 s = cin_skipcomment(s);
5980 if (STRNCMP(s, "public", 6) == 0)
5981 i = 6;
5982 else if (STRNCMP(s, "protected", 9) == 0)
5983 i = 9;
5984 else if (STRNCMP(s, "private", 7) == 0)
5985 i = 7;
5986 else
5987 return FALSE;
5988 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5989}
5990
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005991/* Maximum number of lines to search back for a "namespace" line. */
5992#define FIND_NAMESPACE_LIM 20
5993
5994/*
5995 * Recognize a "namespace" scope declaration.
5996 */
5997 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005998cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005999{
6000 char_u *p;
6001 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006002 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006003
6004 s = cin_skipcomment(s);
6005 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6006 {
6007 p = cin_skipcomment(skipwhite(s + 9));
6008 while (*p != NUL)
6009 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006010 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006011 {
6012 has_name = TRUE; /* found end of a name */
6013 p = cin_skipcomment(skipwhite(p));
6014 }
6015 else if (*p == '{')
6016 {
6017 break;
6018 }
6019 else if (vim_iswordc(*p))
6020 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006021 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006022 if (has_name)
6023 return FALSE; /* word character after skipping past name */
6024 ++p;
6025 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006026 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6027 {
6028 if (!has_name_start || has_name)
6029 return FALSE;
6030 /* C++ 17 nested namespace */
6031 p += 3;
6032 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006033 else
6034 {
6035 return FALSE;
6036 }
6037 }
6038 return TRUE;
6039 }
6040 return FALSE;
6041}
6042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006044 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6045 */
6046 static int
6047cin_is_cpp_extern_c(char_u *s)
6048{
6049 char_u *p;
6050 int has_string_literal = FALSE;
6051
6052 s = cin_skipcomment(s);
6053 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6054 {
6055 p = cin_skipcomment(skipwhite(s + 6));
6056 while (*p != NUL)
6057 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006058 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006059 {
6060 p = cin_skipcomment(skipwhite(p));
6061 }
6062 else if (*p == '{')
6063 {
6064 break;
6065 }
6066 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6067 {
6068 if (has_string_literal)
6069 return FALSE;
6070 has_string_literal = TRUE;
6071 p += 3;
6072 }
6073 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6074 && p[4] == '"')
6075 {
6076 if (has_string_literal)
6077 return FALSE;
6078 has_string_literal = TRUE;
6079 p += 5;
6080 }
6081 else
6082 {
6083 return FALSE;
6084 }
6085 }
6086 return has_string_literal ? TRUE : FALSE;
6087 }
6088 return FALSE;
6089}
6090
6091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 * Return a pointer to the first non-empty non-comment character after a ':'.
6093 * Return NULL if not found.
6094 * case 234: a = b;
6095 * ^
6096 */
6097 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006098after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 for ( ; *l; ++l)
6101 {
6102 if (*l == ':')
6103 {
6104 if (l[1] == ':') /* skip over "::" for C++ */
6105 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006106 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 break;
6108 }
6109 else if (*l == '\'' && l[1] && l[2] == '\'')
6110 l += 2; /* skip over 'x' */
6111 }
6112 if (*l == NUL)
6113 return NULL;
6114 l = cin_skipcomment(l + 1);
6115 if (*l == NUL)
6116 return NULL;
6117 return l;
6118}
6119
6120/*
6121 * Get indent of line "lnum", skipping a label.
6122 * Return 0 if there is nothing after the label.
6123 */
6124 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006125get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127 char_u *l;
6128 pos_T fp;
6129 colnr_T col;
6130 char_u *p;
6131
6132 l = ml_get(lnum);
6133 p = after_label(l);
6134 if (p == NULL)
6135 return 0;
6136
6137 fp.col = (colnr_T)(p - l);
6138 fp.lnum = lnum;
6139 getvcol(curwin, &fp, &col, NULL, NULL);
6140 return (int)col;
6141}
6142
6143/*
6144 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006145 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 * label: if (asdf && asdfasdf)
6147 * ^
6148 */
6149 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006150skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
6152 char_u *l;
6153 int amount;
6154 pos_T cursor_save;
6155
6156 cursor_save = curwin->w_cursor;
6157 curwin->w_cursor.lnum = lnum;
6158 l = ml_get_curline();
6159 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006160 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
6162 amount = get_indent_nolabel(lnum);
6163 l = after_label(ml_get_curline());
6164 if (l == NULL) /* just in case */
6165 l = ml_get_curline();
6166 }
6167 else
6168 {
6169 amount = get_indent();
6170 l = ml_get_curline();
6171 }
6172 *pp = l;
6173
6174 curwin->w_cursor = cursor_save;
6175 return amount;
6176}
6177
6178/*
6179 * Return the indent of the first variable name after a type in a declaration.
6180 * int a, indent of "a"
6181 * static struct foo b, indent of "b"
6182 * enum bla c, indent of "c"
6183 * Returns zero when it doesn't look like a declaration.
6184 */
6185 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006186cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
6188 char_u *line, *p, *s;
6189 int len;
6190 pos_T fp;
6191 colnr_T col;
6192
6193 line = ml_get_curline();
6194 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006195 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6197 {
6198 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006199 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 }
6201 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6202 p = skipwhite(p + 6);
6203 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6204 p = skipwhite(p + 4);
6205 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6206 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6207 {
6208 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006209 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6210 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6211 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6212 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 p = s;
6214 }
6215 for (len = 0; vim_isIDc(p[len]); ++len)
6216 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006217 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 return 0;
6219
6220 p = skipwhite(p + len);
6221 fp.lnum = curwin->w_cursor.lnum;
6222 fp.col = (colnr_T)(p - line);
6223 getvcol(curwin, &fp, &col, NULL, NULL);
6224 return (int)col;
6225}
6226
6227/*
6228 * Return the indent of the first non-blank after an equal sign.
6229 * char *foo = "here";
6230 * Return zero if no (useful) equal sign found.
6231 * Return -1 if the line above "lnum" ends in a backslash.
6232 * foo = "asdf\
6233 * asdf\
6234 * here";
6235 */
6236 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006237cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 char_u *line;
6240 char_u *s;
6241 colnr_T col;
6242 pos_T fp;
6243
6244 if (lnum > 1)
6245 {
6246 line = ml_get(lnum - 1);
6247 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6248 return -1;
6249 }
6250
6251 line = s = ml_get(lnum);
6252 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6253 {
6254 if (cin_iscomment(s)) /* ignore comments */
6255 s = cin_skipcomment(s);
6256 else
6257 ++s;
6258 }
6259 if (*s != '=')
6260 return 0;
6261
6262 s = skipwhite(s + 1);
6263 if (cin_nocode(s))
6264 return 0;
6265
6266 if (*s == '"') /* nice alignment for continued strings */
6267 ++s;
6268
6269 fp.lnum = lnum;
6270 fp.col = (colnr_T)(s - line);
6271 getvcol(curwin, &fp, &col, NULL, NULL);
6272 return (int)col;
6273}
6274
6275/*
6276 * Recognize a preprocessor statement: Any line that starts with '#'.
6277 */
6278 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006279cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006281 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 return TRUE;
6283 return FALSE;
6284}
6285
6286/*
6287 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6288 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6289 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006290 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 */
6292 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006293cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 char_u *line = *pp;
6296 linenr_T lnum = *lnump;
6297 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006298 int candidate_amount = *amount;
6299
6300 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6301 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006303 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 {
6305 if (cin_ispreproc(line))
6306 {
6307 retval = TRUE;
6308 *lnump = lnum;
6309 break;
6310 }
6311 if (lnum == 1)
6312 break;
6313 line = ml_get(--lnum);
6314 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6315 break;
6316 }
6317
6318 if (lnum != *lnump)
6319 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006320 if (retval)
6321 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 return retval;
6323}
6324
6325/*
6326 * Recognize the start of a C or C++ comment.
6327 */
6328 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006329cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6332}
6333
6334/*
6335 * Recognize the start of a "//" comment.
6336 */
6337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006338cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 return (p[0] == '/' && p[1] == '/');
6341}
6342
6343/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006344 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6345 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006347 * If a line begins with an "else", only consider it terminated if no unmatched
6348 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 * Return the character terminating the line (ending char's have precedence if
6350 * both apply in order to determine initializations).
6351 */
6352 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006353cin_isterminated(
6354 char_u *s,
6355 int incl_open, /* include '{' at the end as terminator */
6356 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006358 char_u found_start = 0;
6359 unsigned n_open = 0;
6360 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 s = cin_skipcomment(s);
6363
6364 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6365 found_start = *s;
6366
Bram Moolenaar496f9512011-05-19 16:35:09 +02006367 if (!found_start)
6368 is_else = cin_iselse(s);
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 while (*s)
6371 {
6372 /* skip over comments, "" strings and 'c'haracters */
6373 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006374 if (*s == '}' && n_open > 0)
6375 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006376 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006377 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 && cin_nocode(s + 1))
6379 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006380 else if (*s == '{')
6381 {
6382 if (incl_open && cin_nocode(s + 1))
6383 return *s;
6384 else
6385 ++n_open;
6386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388 if (*s)
6389 s++;
6390 }
6391 return found_start;
6392}
6393
6394/*
6395 * Recognize the basic picture of a function declaration -- it needs to
6396 * have an open paren somewhere and a close paren at the end of the line and
6397 * no semicolons anywhere.
6398 * When a line ends in a comma we continue looking in the next line.
6399 * "sp" points to a string with the line. When looking at other lines it must
6400 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006401 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006402 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 */
6404 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006405cin_isfuncdecl(
6406 char_u **sp,
6407 linenr_T first_lnum,
6408 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 char_u *s;
6411 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006412 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006414 pos_T *trypos;
6415 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
6417 if (sp == NULL)
6418 s = ml_get(lnum);
6419 else
6420 s = *sp;
6421
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006422 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006423 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006424 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006425 {
6426 lnum = trypos->lnum;
6427 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006428 {
6429 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006430 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006431 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006432
6433 s = ml_get(lnum);
6434 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006435 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006436
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006437 /* Ignore line starting with #. */
6438 if (cin_ispreproc(s))
6439 return FALSE;
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6442 {
6443 if (cin_iscomment(s)) /* ignore comments */
6444 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006445 else if (*s == ':')
6446 {
6447 if (*(s + 1) == ':')
6448 s += 2;
6449 else
6450 /* To avoid a mistake in the following situation:
6451 * A::A(int a, int b)
6452 * : a(0) // <--not a function decl
6453 * , b(0)
6454 * {...
6455 */
6456 return FALSE;
6457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 else
6459 ++s;
6460 }
6461 if (*s != '(')
6462 return FALSE; /* ';', ' or " before any () or no '(' */
6463
6464 while (*s && *s != ';' && *s != '\'' && *s != '"')
6465 {
6466 if (*s == ')' && cin_nocode(s + 1))
6467 {
6468 /* ')' at the end: may have found a match
6469 * Check for he previous line not to end in a backslash:
6470 * #if defined(x) && \
6471 * defined(y)
6472 */
6473 lnum = first_lnum - 1;
6474 s = ml_get(lnum);
6475 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6476 retval = TRUE;
6477 goto done;
6478 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006479 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006481 int comma = (*s == ',');
6482
6483 /* ',' at the end: continue looking in the next line.
6484 * At the end: check for ',' in the next line, for this style:
6485 * func(arg1
6486 * , arg2) */
6487 for (;;)
6488 {
6489 if (lnum >= curbuf->b_ml.ml_line_count)
6490 break;
6491 s = ml_get(++lnum);
6492 if (!cin_ispreproc(s))
6493 break;
6494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 if (lnum >= curbuf->b_ml.ml_line_count)
6496 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006497 /* Require a comma at end of the line or a comma or ')' at the
6498 * start of next line. */
6499 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006500 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006501 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006502 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 }
6504 else if (cin_iscomment(s)) /* ignore comments */
6505 s = cin_skipcomment(s);
6506 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006509 just_started = FALSE;
6510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 }
6512
6513done:
6514 if (lnum != first_lnum && sp != NULL)
6515 *sp = ml_get(first_lnum);
6516
6517 return retval;
6518}
6519
6520 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006521cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006523 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524}
6525
6526 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006527cin_iselse(
6528 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
6530 if (*p == '}') /* accept "} else" */
6531 p = cin_skipcomment(p + 1);
6532 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6533}
6534
6535 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006536cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6539}
6540
6541/*
6542 * Check if this is a "while" that should have a matching "do".
6543 * We only accept a "while (condition) ;", with only white space between the
6544 * ')' and ';'. The condition may be spread over several lines.
6545 */
6546 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006547cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 pos_T cursor_save;
6550 pos_T *trypos;
6551 int retval = FALSE;
6552
6553 p = cin_skipcomment(p);
6554 if (*p == '}') /* accept "} while (cond);" */
6555 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006556 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
6558 cursor_save = curwin->w_cursor;
6559 curwin->w_cursor.lnum = lnum;
6560 curwin->w_cursor.col = 0;
6561 p = ml_get_curline();
6562 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6563 {
6564 ++p;
6565 ++curwin->w_cursor.col;
6566 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006567 if ((trypos = findmatchlimit(NULL, 0, 0,
6568 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6570 retval = TRUE;
6571 curwin->w_cursor = cursor_save;
6572 }
6573 return retval;
6574}
6575
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006576/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006577 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006578 * Return 0 if there is none.
6579 * Otherwise return !0 and update "*poffset" to point to the place where the
6580 * string was found.
6581 */
6582 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006583cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006584{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006585 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006586
6587 if (offset-- < 2)
6588 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006589 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006590 --offset;
6591
6592 offset -= 1;
6593 if (!STRNCMP(line + offset, "if", 2))
6594 goto probablyFound;
6595
6596 if (offset >= 1)
6597 {
6598 offset -= 1;
6599 if (!STRNCMP(line + offset, "for", 3))
6600 goto probablyFound;
6601
6602 if (offset >= 2)
6603 {
6604 offset -= 2;
6605 if (!STRNCMP(line + offset, "while", 5))
6606 goto probablyFound;
6607 }
6608 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006609 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006610
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006611probablyFound:
6612 if (!offset || !vim_isIDc(line[offset - 1]))
6613 {
6614 *poffset = offset;
6615 return 1;
6616 }
6617 return 0;
6618}
6619
6620/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006621 * Return TRUE if we are at the end of a do-while.
6622 * do
6623 * nothing;
6624 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006625 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006626 * Adjust the cursor to the line with "while".
6627 */
6628 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006629cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006630{
6631 char_u *line;
6632 char_u *p;
6633 char_u *s;
6634 pos_T *trypos;
6635 int i;
6636
6637 if (terminated != ';') /* there must be a ';' at the end */
6638 return FALSE;
6639
6640 p = line = ml_get_curline();
6641 while (*p != NUL)
6642 {
6643 p = cin_skipcomment(p);
6644 if (*p == ')')
6645 {
6646 s = skipwhite(p + 1);
6647 if (*s == ';' && cin_nocode(s + 1))
6648 {
6649 /* Found ");" at end of the line, now check there is "while"
6650 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006651 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006652 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006653 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006654 if (trypos != NULL)
6655 {
6656 s = cin_skipcomment(ml_get(trypos->lnum));
6657 if (*s == '}') /* accept "} while (cond);" */
6658 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006659 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006660 {
6661 curwin->w_cursor.lnum = trypos->lnum;
6662 return TRUE;
6663 }
6664 }
6665
6666 /* Searching may have made "line" invalid, get it again. */
6667 line = ml_get_curline();
6668 p = line + i;
6669 }
6670 }
6671 if (*p != NUL)
6672 ++p;
6673 }
6674 return FALSE;
6675}
6676
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006678cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6681}
6682
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006683/*
6684 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 * constructor-initialization. eg:
6686 *
6687 * class MyClass :
6688 * baseClass <-- here
6689 * class MyClass : public baseClass,
6690 * anotherBaseClass <-- here (should probably lineup ??)
6691 * MyClass::MyClass(...) :
6692 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006693 *
6694 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 */
6696 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006697cin_is_cpp_baseclass(
6698 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006700 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 char_u *s;
6702 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006703 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006704 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006706 if (pos->lnum <= lnum)
6707 return cached->found; /* Use the cached result */
6708
6709 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006711 s = skipwhite(line);
6712 if (*s == '#') /* skip #define FOO x ? (x) : x */
6713 return FALSE;
6714 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (*s == NUL)
6716 return FALSE;
6717
6718 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6719
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006720 /* Search for a line starting with '#', empty, ending in ';' or containing
6721 * '{' or '}' and start below it. This handles the following situations:
6722 * a = cond ?
6723 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006724 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006725 * func::foo()
6726 * : something
6727 * {}
6728 * Foo::Foo (int one, int two)
6729 * : something(4),
6730 * somethingelse(3)
6731 * {}
6732 */
6733 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006735 line = ml_get(lnum - 1);
6736 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006737 if (*s == '#' || *s == NUL)
6738 break;
6739 while (*s != NUL)
6740 {
6741 s = cin_skipcomment(s);
6742 if (*s == '{' || *s == '}'
6743 || (*s == ';' && cin_nocode(s + 1)))
6744 break;
6745 if (*s != NUL)
6746 ++s;
6747 }
6748 if (*s != NUL)
6749 break;
6750 --lnum;
6751 }
6752
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006753 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006754 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006755 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006756 for (;;)
6757 {
6758 if (*s == NUL)
6759 {
6760 if (lnum == curwin->w_cursor.lnum)
6761 break;
6762 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006763 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006764 s = line;
6765 }
6766 if (s == line)
6767 {
6768 /* don't recognize "case (foo):" as a baseclass */
6769 if (cin_iscase(s, FALSE))
6770 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006771 s = cin_skipcomment(line);
6772 if (*s == NUL)
6773 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006774 }
6775
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006776 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006777 s = skip_string(s) + 1;
6778 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
6780 if (s[1] == ':')
6781 {
6782 /* skip double colon. It can't be a constructor
6783 * initialization any more */
6784 lookfor_ctor_init = FALSE;
6785 s = cin_skipcomment(s + 2);
6786 }
6787 else if (lookfor_ctor_init || class_or_struct)
6788 {
6789 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006790 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 cpp_base_class = TRUE;
6792 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006793 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 s = cin_skipcomment(s + 1);
6795 }
6796 else
6797 s = cin_skipcomment(s + 1);
6798 }
6799 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6800 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6801 {
6802 class_or_struct = TRUE;
6803 lookfor_ctor_init = FALSE;
6804
6805 if (*s == 'c')
6806 s = cin_skipcomment(s + 5);
6807 else
6808 s = cin_skipcomment(s + 6);
6809 }
6810 else
6811 {
6812 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6813 {
6814 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6815 }
6816 else if (s[0] == ')')
6817 {
6818 /* Constructor-initialization is assumed if we come across
6819 * something like "):" */
6820 class_or_struct = FALSE;
6821 lookfor_ctor_init = TRUE;
6822 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006823 else if (s[0] == '?')
6824 {
6825 /* Avoid seeing '() :' after '?' as constructor init. */
6826 return FALSE;
6827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 else if (!vim_isIDc(s[0]))
6829 {
6830 /* if it is not an identifier, we are wrong */
6831 class_or_struct = FALSE;
6832 lookfor_ctor_init = FALSE;
6833 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006834 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {
6836 /* it can't be a constructor-initialization any more */
6837 lookfor_ctor_init = FALSE;
6838
6839 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006840 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006841 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 }
6843
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006844 /* When the line ends in a comma don't align with it. */
6845 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006846 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 s = cin_skipcomment(s + 1);
6849 }
6850 }
6851
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006852 cached->found = cpp_base_class;
6853 if (cpp_base_class)
6854 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 return cpp_base_class;
6856}
6857
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006858 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006859get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006860{
6861 int amount;
6862 colnr_T vcol;
6863 pos_T *trypos;
6864
6865 if (col == 0)
6866 {
6867 amount = get_indent();
6868 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006869 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006870 amount = get_indent_lnum(trypos->lnum); /* XXX */
6871 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006872 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006873 }
6874 else
6875 {
6876 curwin->w_cursor.col = col;
6877 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6878 amount = (int)vcol;
6879 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006880 if (amount < curbuf->b_ind_cpp_baseclass)
6881 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006882 return amount;
6883}
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885/*
6886 * Return TRUE if string "s" ends with the string "find", possibly followed by
6887 * white space and comments. Skip strings and comments.
6888 * Ignore "ignore" after "find" if it's not NULL.
6889 */
6890 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006891cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 char_u *p = s;
6894 char_u *r;
6895 int len = (int)STRLEN(find);
6896
6897 while (*p != NUL)
6898 {
6899 p = cin_skipcomment(p);
6900 if (STRNCMP(p, find, len) == 0)
6901 {
6902 r = skipwhite(p + len);
6903 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6904 r = skipwhite(r + STRLEN(ignore));
6905 if (cin_nocode(r))
6906 return TRUE;
6907 }
6908 if (*p != NUL)
6909 ++p;
6910 }
6911 return FALSE;
6912}
6913
6914/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006915 * Return TRUE when "s" starts with "word" and then a non-ID character.
6916 */
6917 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006918cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006919{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006920 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006921
6922 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6923}
6924
6925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 * Skip strings, chars and comments until at or past "trypos".
6927 * Return the column found.
6928 */
6929 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006930cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
6932 char_u *line;
6933 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006934 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
6936 p = line = ml_get(trypos->lnum);
6937 while (*p && (colnr_T)(p - line) < trypos->col)
6938 {
6939 if (cin_iscomment(p))
6940 p = cin_skipcomment(p);
6941 else
6942 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006943 new_p = skip_string(p);
6944 if (new_p == p)
6945 ++p;
6946 else
6947 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
6949 }
6950 return (int)(p - line);
6951}
6952
6953/*
6954 * Find the '{' at the start of the block we are in.
6955 * Return NULL if no match found.
6956 * Ignore a '{' that is in a comment, makes indenting the next three lines
6957 * work. */
6958/* foo() */
6959/* { */
6960/* } */
6961
6962 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006963find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 pos_T cursor_save;
6966 pos_T *trypos;
6967 pos_T *pos;
6968 static pos_T pos_copy;
6969
6970 cursor_save = curwin->w_cursor;
6971 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6972 {
6973 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6974 trypos = &pos_copy;
6975 curwin->w_cursor = *trypos;
6976 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006977 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006979 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 break;
6981 if (pos != NULL)
6982 curwin->w_cursor.lnum = pos->lnum;
6983 }
6984 curwin->w_cursor = cursor_save;
6985 return trypos;
6986}
6987
6988/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006989 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006990 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 */
6992 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006993find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006995 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006996}
6997
6998 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02006999find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007000{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 pos_T cursor_save;
7002 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007003 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007004 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
7006 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007007 ind_maxp_wk = ind_maxparen;
7008retry:
7009 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011 /* check if the ( is in a // comment */
7012 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007013 {
7014 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7015 if (ind_maxp_wk > 0)
7016 {
7017 curwin->w_cursor = *trypos;
7018 curwin->w_cursor.col = 0; /* XXX */
7019 goto retry;
7020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 else
7024 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007025 pos_T *trypos_wk;
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7028 trypos = &pos_copy;
7029 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007030 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007031 {
7032 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7033 - trypos_wk->lnum);
7034 if (ind_maxp_wk > 0)
7035 {
7036 curwin->w_cursor = *trypos_wk;
7037 goto retry;
7038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 }
7042 }
7043 curwin->w_cursor = cursor_save;
7044 return trypos;
7045}
7046
7047/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007048 * Find the matching '(', ignoring it if it is in a comment or before an
7049 * unmatched {.
7050 * Return NULL if no match found.
7051 */
7052 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007053find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007054{
7055 pos_T *trypos = find_match_paren(ind_maxparen);
7056
7057 if (trypos != NULL)
7058 {
7059 pos_T *tryposBrace = find_start_brace();
7060
7061 /* If both an unmatched '(' and '{' is found. Ignore the '('
7062 * position if the '{' is further down. */
7063 if (tryposBrace != NULL
7064 && (trypos->lnum != tryposBrace->lnum
7065 ? trypos->lnum < tryposBrace->lnum
7066 : trypos->col < tryposBrace->col))
7067 trypos = NULL;
7068 }
7069 return trypos;
7070}
7071
7072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 * Return ind_maxparen corrected for the difference in line number between the
7074 * cursor position and "startpos". This makes sure that searching for a
7075 * matching paren above the cursor line doesn't find a match because of
7076 * looking a few lines further.
7077 */
7078 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007079corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
7081 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7082
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007083 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7084 return curbuf->b_ind_maxparen - (int)n;
7085 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086}
7087
7088/*
7089 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007090 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 */
7092 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007093find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
7095 int i;
7096 int retval = FALSE;
7097 int open_count = 0;
7098
7099 curwin->w_cursor.col = 0; /* default is start of line */
7100
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007101 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
7103 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7104 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7105 if (l[i] == start)
7106 ++open_count;
7107 else if (l[i] == end)
7108 {
7109 if (open_count > 0)
7110 --open_count;
7111 else
7112 {
7113 curwin->w_cursor.col = i;
7114 retval = TRUE;
7115 }
7116 }
7117 }
7118 return retval;
7119}
7120
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007121/*
7122 * Parse 'cinoptions' and set the values in "curbuf".
7123 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7124 */
7125 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007126parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007127{
7128 char_u *p;
7129 char_u *l;
7130 char_u *digits;
7131 int n;
7132 int divider;
7133 int fraction = 0;
7134 int sw = (int)get_sw_value(buf);
7135
7136 /*
7137 * Set the default values.
7138 */
7139 /* Spaces from a block's opening brace the prevailing indent for that
7140 * block should be. */
7141 buf->b_ind_level = sw;
7142
7143 /* Spaces from the edge of the line an open brace that's at the end of a
7144 * line is imagined to be. */
7145 buf->b_ind_open_imag = 0;
7146
7147 /* Spaces from the prevailing indent for a line that is not preceded by
7148 * an opening brace. */
7149 buf->b_ind_no_brace = 0;
7150
7151 /* Column where the first { of a function should be located }. */
7152 buf->b_ind_first_open = 0;
7153
7154 /* Spaces from the prevailing indent a leftmost open brace should be
7155 * located. */
7156 buf->b_ind_open_extra = 0;
7157
7158 /* Spaces from the matching open brace (real location for one at the left
7159 * edge; imaginary location from one that ends a line) the matching close
7160 * brace should be located. */
7161 buf->b_ind_close_extra = 0;
7162
7163 /* Spaces from the edge of the line an open brace sitting in the leftmost
7164 * column is imagined to be. */
7165 buf->b_ind_open_left_imag = 0;
7166
7167 /* Spaces jump labels should be shifted to the left if N is non-negative,
7168 * otherwise the jump label will be put to column 1. */
7169 buf->b_ind_jump_label = -1;
7170
7171 /* Spaces from the switch() indent a "case xx" label should be located. */
7172 buf->b_ind_case = sw;
7173
7174 /* Spaces from the "case xx:" code after a switch() should be located. */
7175 buf->b_ind_case_code = sw;
7176
7177 /* Lineup break at end of case in switch() with case label. */
7178 buf->b_ind_case_break = 0;
7179
7180 /* Spaces from the class declaration indent a scope declaration label
7181 * should be located. */
7182 buf->b_ind_scopedecl = sw;
7183
7184 /* Spaces from the scope declaration label code should be located. */
7185 buf->b_ind_scopedecl_code = sw;
7186
7187 /* Amount K&R-style parameters should be indented. */
7188 buf->b_ind_param = sw;
7189
7190 /* Amount a function type spec should be indented. */
7191 buf->b_ind_func_type = sw;
7192
7193 /* Amount a cpp base class declaration or constructor initialization
7194 * should be indented. */
7195 buf->b_ind_cpp_baseclass = sw;
7196
7197 /* additional spaces beyond the prevailing indent a continuation line
7198 * should be located. */
7199 buf->b_ind_continuation = sw;
7200
7201 /* Spaces from the indent of the line with an unclosed parentheses. */
7202 buf->b_ind_unclosed = sw * 2;
7203
7204 /* Spaces from the indent of the line with an unclosed parentheses, which
7205 * itself is also unclosed. */
7206 buf->b_ind_unclosed2 = sw;
7207
7208 /* Suppress ignoring spaces from the indent of a line starting with an
7209 * unclosed parentheses. */
7210 buf->b_ind_unclosed_noignore = 0;
7211
7212 /* If the opening paren is the last nonwhite character on the line, and
7213 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7214 * context (for very long lines). */
7215 buf->b_ind_unclosed_wrapped = 0;
7216
7217 /* Suppress ignoring white space when lining up with the character after
7218 * an unclosed parentheses. */
7219 buf->b_ind_unclosed_whiteok = 0;
7220
7221 /* Indent a closing parentheses under the line start of the matching
7222 * opening parentheses. */
7223 buf->b_ind_matching_paren = 0;
7224
7225 /* Indent a closing parentheses under the previous line. */
7226 buf->b_ind_paren_prev = 0;
7227
7228 /* Extra indent for comments. */
7229 buf->b_ind_comment = 0;
7230
7231 /* Spaces from the comment opener when there is nothing after it. */
7232 buf->b_ind_in_comment = 3;
7233
7234 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7235 * after the comment opener. */
7236 buf->b_ind_in_comment2 = 0;
7237
7238 /* Max lines to search for an open paren. */
7239 buf->b_ind_maxparen = 20;
7240
7241 /* Max lines to search for an open comment. */
7242 buf->b_ind_maxcomment = 70;
7243
7244 /* Handle braces for java code. */
7245 buf->b_ind_java = 0;
7246
7247 /* Not to confuse JS object properties with labels. */
7248 buf->b_ind_js = 0;
7249
7250 /* Handle blocked cases correctly. */
7251 buf->b_ind_keep_case_label = 0;
7252
7253 /* Handle C++ namespace. */
7254 buf->b_ind_cpp_namespace = 0;
7255
7256 /* Handle continuation lines containing conditions of if(), for() and
7257 * while(). */
7258 buf->b_ind_if_for_while = 0;
7259
Bram Moolenaar6b643942017-03-05 19:44:06 +01007260 /* indentation for # comments */
7261 buf->b_ind_hash_comment = 0;
7262
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007263 /* Handle C++ extern "C" or "C++" */
7264 buf->b_ind_cpp_extern_c = 0;
7265
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007266 for (p = buf->b_p_cino; *p; )
7267 {
7268 l = p++;
7269 if (*p == '-')
7270 ++p;
7271 digits = p; /* remember where the digits start */
7272 n = getdigits(&p);
7273 divider = 0;
7274 if (*p == '.') /* ".5s" means a fraction */
7275 {
7276 fraction = atol((char *)++p);
7277 while (VIM_ISDIGIT(*p))
7278 {
7279 ++p;
7280 if (divider)
7281 divider *= 10;
7282 else
7283 divider = 10;
7284 }
7285 }
7286 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7287 {
7288 if (p == digits)
7289 n = sw; /* just "s" is one 'shiftwidth' */
7290 else
7291 {
7292 n *= sw;
7293 if (divider)
7294 n += (sw * fraction + divider / 2) / divider;
7295 }
7296 ++p;
7297 }
7298 if (l[1] == '-')
7299 n = -n;
7300
7301 /* When adding an entry here, also update the default 'cinoptions' in
7302 * doc/indent.txt, and add explanation for it! */
7303 switch (*l)
7304 {
7305 case '>': buf->b_ind_level = n; break;
7306 case 'e': buf->b_ind_open_imag = n; break;
7307 case 'n': buf->b_ind_no_brace = n; break;
7308 case 'f': buf->b_ind_first_open = n; break;
7309 case '{': buf->b_ind_open_extra = n; break;
7310 case '}': buf->b_ind_close_extra = n; break;
7311 case '^': buf->b_ind_open_left_imag = n; break;
7312 case 'L': buf->b_ind_jump_label = n; break;
7313 case ':': buf->b_ind_case = n; break;
7314 case '=': buf->b_ind_case_code = n; break;
7315 case 'b': buf->b_ind_case_break = n; break;
7316 case 'p': buf->b_ind_param = n; break;
7317 case 't': buf->b_ind_func_type = n; break;
7318 case '/': buf->b_ind_comment = n; break;
7319 case 'c': buf->b_ind_in_comment = n; break;
7320 case 'C': buf->b_ind_in_comment2 = n; break;
7321 case 'i': buf->b_ind_cpp_baseclass = n; break;
7322 case '+': buf->b_ind_continuation = n; break;
7323 case '(': buf->b_ind_unclosed = n; break;
7324 case 'u': buf->b_ind_unclosed2 = n; break;
7325 case 'U': buf->b_ind_unclosed_noignore = n; break;
7326 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7327 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7328 case 'm': buf->b_ind_matching_paren = n; break;
7329 case 'M': buf->b_ind_paren_prev = n; break;
7330 case ')': buf->b_ind_maxparen = n; break;
7331 case '*': buf->b_ind_maxcomment = n; break;
7332 case 'g': buf->b_ind_scopedecl = n; break;
7333 case 'h': buf->b_ind_scopedecl_code = n; break;
7334 case 'j': buf->b_ind_java = n; break;
7335 case 'J': buf->b_ind_js = n; break;
7336 case 'l': buf->b_ind_keep_case_label = n; break;
7337 case '#': buf->b_ind_hash_comment = n; break;
7338 case 'N': buf->b_ind_cpp_namespace = n; break;
7339 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007340 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007341 }
7342 if (*p == ',')
7343 ++p;
7344 }
7345}
7346
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007347/*
7348 * Return the desired indent for C code.
7349 * Return -1 if the indent should be left alone (inside a raw string).
7350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007352get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 pos_T cur_curpos;
7355 int amount;
7356 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007357 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 colnr_T col;
7359 char_u *theline;
7360 char_u *linecopy;
7361 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007362 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007364 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 pos_T our_paren_pos;
7366 char_u *start;
7367 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007368#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#define BRACE_AT_START 2 /* '{' is at start of line */
7370#define BRACE_AT_END 3 /* '{' is at end of line */
7371 linenr_T ourscope;
7372 char_u *l;
7373 char_u *look;
7374 char_u terminated;
7375 int lookfor;
7376#define LOOKFOR_INITIAL 0
7377#define LOOKFOR_IF 1
7378#define LOOKFOR_DO 2
7379#define LOOKFOR_CASE 3
7380#define LOOKFOR_ANY 4
7381#define LOOKFOR_TERM 5
7382#define LOOKFOR_UNTERM 6
7383#define LOOKFOR_SCOPEDECL 7
7384#define LOOKFOR_NOBREAK 8
7385#define LOOKFOR_CPP_BASECLASS 9
7386#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007387#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007388#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
7390 int whilelevel;
7391 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 int n;
7393 int iscase;
7394 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007395 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007397 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007398 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007399 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007400 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007401 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007403 /* make a copy, value is changed below */
7404 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406 /* remember where the cursor was when we started */
7407 cur_curpos = curwin->w_cursor;
7408
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007409 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007410 if (cur_curpos.lnum == 1)
7411 return 0;
7412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 /* Get a copy of the current contents of the line.
7414 * This is required, because only the most recent line obtained with
7415 * ml_get is valid! */
7416 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7417 if (linecopy == NULL)
7418 return 0;
7419
7420 /*
7421 * In insert mode and the cursor is on a ')' truncate the line at the
7422 * cursor position. We don't want to line up with the matching '(' when
7423 * inserting new stuff.
7424 * For unknown reasons the cursor might be past the end of the line, thus
7425 * check for that.
7426 */
7427 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007428 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 && linecopy[curwin->w_cursor.col] == ')')
7430 linecopy[curwin->w_cursor.col] = NUL;
7431
7432 theline = skipwhite(linecopy);
7433
7434 /* move the cursor to the start of the line */
7435
7436 curwin->w_cursor.col = 0;
7437
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007438 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007439
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007441 * If we are inside a raw string don't change the indent.
7442 * Ignore a raw string inside a comment.
7443 */
7444 comment_pos = ind_find_start_comment();
7445 if (comment_pos != NULL)
7446 {
7447 /* findmatchlimit() static pos is overwritten, make a copy */
7448 tryposCopy = *comment_pos;
7449 comment_pos = &tryposCopy;
7450 }
7451 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007452 if (trypos != NULL && (comment_pos == NULL
7453 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007454 {
7455 amount = -1;
7456 goto laterend;
7457 }
7458
7459 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 * #defines and so on always go at the left when included in 'cinkeys'.
7461 */
7462 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007463 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007464 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007465 goto theend;
7466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007469 * Is it a non-case label? Then that goes at the left margin too unless:
7470 * - JS flag is set.
7471 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007473 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007474 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 {
7476 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007477 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
7479
7480 /*
7481 * If we're inside a "//" comment and there is a "//" comment in a
7482 * previous line, lineup with that one.
7483 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007484 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 && (trypos = find_line_comment()) != NULL) /* XXX */
7486 {
7487 /* find how indented the line beginning the comment is */
7488 getvcol(curwin, trypos, &col, NULL, NULL);
7489 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007490 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
7492
7493 /*
7494 * If we're inside a comment and not looking at the start of the
7495 * comment, try using the 'comments' option.
7496 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007497 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {
7499 int lead_start_len = 2;
7500 int lead_middle_len = 1;
7501 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7502 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7503 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7504 char_u *p;
7505 int start_align = 0;
7506 int start_off = 0;
7507 int done = FALSE;
7508
7509 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007510 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007512 *lead_start = NUL;
7513 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
7515 p = curbuf->b_p_com;
7516 while (*p != NUL)
7517 {
7518 int align = 0;
7519 int off = 0;
7520 int what = 0;
7521
7522 while (*p != NUL && *p != ':')
7523 {
7524 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7525 what = *p++;
7526 else if (*p == COM_LEFT || *p == COM_RIGHT)
7527 align = *p++;
7528 else if (VIM_ISDIGIT(*p) || *p == '-')
7529 off = getdigits(&p);
7530 else
7531 ++p;
7532 }
7533
7534 if (*p == ':')
7535 ++p;
7536 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7537 if (what == COM_START)
7538 {
7539 STRCPY(lead_start, lead_end);
7540 lead_start_len = (int)STRLEN(lead_start);
7541 start_off = off;
7542 start_align = align;
7543 }
7544 else if (what == COM_MIDDLE)
7545 {
7546 STRCPY(lead_middle, lead_end);
7547 lead_middle_len = (int)STRLEN(lead_middle);
7548 }
7549 else if (what == COM_END)
7550 {
7551 /* If our line starts with the middle comment string, line it
7552 * up with the comment opener per the 'comments' option. */
7553 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7554 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7555 {
7556 done = TRUE;
7557 if (curwin->w_cursor.lnum > 1)
7558 {
7559 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007560 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 * the middle comment string matches in the previous
7562 * line, use the indent of that line. XXX */
7563 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7564 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7565 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7566 else if (STRNCMP(look, lead_middle,
7567 lead_middle_len) == 0)
7568 {
7569 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7570 break;
7571 }
7572 /* If the start comment string doesn't match with the
7573 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007574 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 lead_start, lead_start_len) != 0)
7576 continue;
7577 }
7578 if (start_off != 0)
7579 amount += start_off;
7580 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007581 amount += vim_strsize(lead_start)
7582 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 break;
7584 }
7585
7586 /* If our line starts with the end comment string, line it up
7587 * with the middle comment */
7588 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7589 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7590 {
7591 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7592 /* XXX */
7593 if (off != 0)
7594 amount += off;
7595 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007596 amount += vim_strsize(lead_start)
7597 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 done = TRUE;
7599 break;
7600 }
7601 }
7602 }
7603
7604 /* If our line starts with an asterisk, line up with the
7605 * asterisk in the comment opener; otherwise, line up
7606 * with the first character of the comment text.
7607 */
7608 if (done)
7609 ;
7610 else if (theline[0] == '*')
7611 amount += 1;
7612 else
7613 {
7614 /*
7615 * If we are more than one line away from the comment opener, take
7616 * the indent of the previous non-empty line. If 'cino' has "CO"
7617 * and we are just below the comment opener and there are any
7618 * white characters after it line up with the text after it;
7619 * otherwise, add the amount specified by "c" in 'cino'
7620 */
7621 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007622 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {
7624 if (linewhite(lnum)) /* skip blank lines */
7625 continue;
7626 amount = get_indent_lnum(lnum); /* XXX */
7627 break;
7628 }
7629 if (amount == -1) /* use the comment opener */
7630 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007631 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007633 start = ml_get(comment_pos->lnum);
7634 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007636 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007638 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007640 if (curbuf->b_ind_in_comment2 || *look == NUL)
7641 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 }
7643 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007644 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 }
7646
7647 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007648 * Are we looking at a ']' that has a match?
7649 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007650 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007651 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7652 {
7653 /* align with the line containing the '['. */
7654 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007655 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007656 }
7657
7658 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 * Are we inside parentheses or braces?
7660 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007661 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007662 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007663 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 || trypos != NULL)
7665 {
7666 if (trypos != NULL && tryposBrace != NULL)
7667 {
7668 /* Both an unmatched '(' and '{' is found. Use the one which is
7669 * closer to the current cursor position, set the other to NULL. */
7670 if (trypos->lnum != tryposBrace->lnum
7671 ? trypos->lnum < tryposBrace->lnum
7672 : trypos->col < tryposBrace->col)
7673 trypos = NULL;
7674 else
7675 tryposBrace = NULL;
7676 }
7677
7678 if (trypos != NULL)
7679 {
7680 /*
7681 * If the matching paren is more than one line away, use the indent of
7682 * a previous non-empty line that matches the same paren.
7683 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007684 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007686 /* Line up with the start of the matching paren line. */
7687 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7688 }
7689 else
7690 {
7691 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007692 our_paren_pos = *trypos;
7693 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007695 l = skipwhite(ml_get(lnum));
7696 if (cin_nocode(l)) /* skip comment lines */
7697 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007698 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007699 continue; /* ignore #define, #if, etc. */
7700 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007702 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007703 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007704 {
7705 lnum = trypos->lnum + 1;
7706 continue;
7707 }
7708
7709 /* XXX */
7710 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007711 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007712 && trypos->lnum == our_paren_pos.lnum
7713 && trypos->col == our_paren_pos.col)
7714 {
7715 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007717 if (theline[0] == ')')
7718 {
7719 if (our_paren_pos.lnum != lnum
7720 && cur_amount > amount)
7721 cur_amount = amount;
7722 amount = -1;
7723 }
7724 break;
7725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
7727 }
7728
7729 /*
7730 * Line up with line where the matching paren is. XXX
7731 * If the line starts with a '(' or the indent for unclosed
7732 * parentheses is zero, line up with the unclosed parentheses.
7733 */
7734 if (amount == -1)
7735 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007736 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007737 int is_if_for_while = 0;
7738
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007739 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007740 {
7741 /* Look for the outermost opening parenthesis on this line
7742 * and check whether it belongs to an "if", "for" or "while". */
7743
7744 pos_T cursor_save = curwin->w_cursor;
7745 pos_T outermost;
7746 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007747
7748 trypos = &our_paren_pos;
7749 do {
7750 outermost = *trypos;
7751 curwin->w_cursor.lnum = outermost.lnum;
7752 curwin->w_cursor.col = outermost.col;
7753
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007754 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007755 } while (trypos && trypos->lnum == outermost.lnum);
7756
7757 curwin->w_cursor = cursor_save;
7758
7759 line = ml_get(outermost.lnum);
7760
7761 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007762 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007763 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007764
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007765 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007766 look = skipwhite(look);
7767 if (*look == '(')
7768 {
7769 linenr_T save_lnum = curwin->w_cursor.lnum;
7770 char_u *line;
7771 int look_col;
7772
7773 /* Ignore a '(' in front of the line that has a match before
7774 * our matching '('. */
7775 curwin->w_cursor.lnum = our_paren_pos.lnum;
7776 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007777 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007778 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007779 if ((trypos = findmatchlimit(NULL, ')', 0,
7780 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007781 != NULL
7782 && trypos->lnum == our_paren_pos.lnum
7783 && trypos->col < our_paren_pos.col)
7784 ignore_paren_col = trypos->col + 1;
7785
7786 curwin->w_cursor.lnum = save_lnum;
7787 look = ml_get(our_paren_pos.lnum) + look_col;
7788 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007789 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7790 && is_if_for_while == 0)
7791 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007792 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {
7794 /*
7795 * If we're looking at a close paren, line up right there;
7796 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007797 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 * the last nonwhite character of the line, use either the
7799 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007800 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 * lines).
7802 */
7803 if (theline[0] != ')')
7804 {
7805 cur_amount = MAXCOL;
7806 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007807 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 && cin_ends_in(l, (char_u *)"(", NULL))
7809 {
7810 /* look for opening unmatched paren, indent one level
7811 * for each additional level */
7812 n = 1;
7813 for (col = 0; col < our_paren_pos.col; ++col)
7814 {
7815 switch (l[col])
7816 {
7817 case '(':
7818 case '{': ++n;
7819 break;
7820
7821 case ')':
7822 case '}': if (n > 1)
7823 --n;
7824 break;
7825 }
7826 }
7827
7828 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007829 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007831 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 our_paren_pos.col++;
7833 else
7834 {
7835 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007836 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 col++;
7838 if (l[col] != NUL) /* In case of trailing space */
7839 our_paren_pos.col = col;
7840 else
7841 our_paren_pos.col++;
7842 }
7843 }
7844
7845 /*
7846 * Find how indented the paren is, or the character after it
7847 * if we did the above "if".
7848 */
7849 if (our_paren_pos.col > 0)
7850 {
7851 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7852 if (cur_amount > (int)col)
7853 cur_amount = col;
7854 }
7855 }
7856
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007857 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {
7859 /* Line up with the start of the matching paren line. */
7860 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007861 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7862 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007863 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {
7865 if (cur_amount != MAXCOL)
7866 amount = cur_amount;
7867 }
7868 else
7869 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007870 /* Add b_ind_unclosed2 for each '(' before our matching one,
7871 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007873 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {
7875 --our_paren_pos.col;
7876 switch (*ml_get_pos(&our_paren_pos))
7877 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007878 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 col = our_paren_pos.col;
7880 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007881 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 col = MAXCOL;
7883 break;
7884 }
7885 }
7886
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007887 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 * braces */
7889 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007890 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 else
7892 {
7893 curwin->w_cursor.lnum = our_paren_pos.lnum;
7894 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007895 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7896 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007897 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007899 {
7900 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007901 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007902 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007903 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 }
7906 /*
7907 * For a line starting with ')' use the minimum of the two
7908 * positions, to avoid giving it more indent than the previous
7909 * lines:
7910 * func_long_name( if (x
7911 * arg && yy
7912 * ) ^ not here ) ^ not here
7913 */
7914 if (cur_amount < amount)
7915 amount = cur_amount;
7916 }
7917 }
7918
7919 /* add extra indent for a comment */
7920 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007921 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 else
7924 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007925 /*
7926 * We are inside braces, there is a { before this line at the position
7927 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007928 * Make a copy of tryposBrace, it may point to pos_copy inside
7929 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007930 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007931 tryposCopy = *tryposBrace;
7932 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 ourscope = trypos->lnum;
7935 start = ml_get(ourscope);
7936
7937 /*
7938 * Now figure out how indented the line is in general.
7939 * If the brace was at the start of the line, we use that;
7940 * otherwise, check out the indentation of the line as
7941 * a whole and then add the "imaginary indent" to that.
7942 */
7943 look = skipwhite(start);
7944 if (*look == '{')
7945 {
7946 getvcol(curwin, trypos, &col, NULL, NULL);
7947 amount = col;
7948 if (*start == '{')
7949 start_brace = BRACE_IN_COL0;
7950 else
7951 start_brace = BRACE_AT_START;
7952 }
7953 else
7954 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007955 /* That opening brace might have been on a continuation
7956 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 curwin->w_cursor.lnum = ourscope;
7958
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007959 /* Position the cursor over the rightmost paren, so that
7960 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 lnum = ourscope;
7962 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007963 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7964 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 lnum = trypos->lnum;
7966
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007967 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 * case 1: if (asdf &&
7969 * ldfd) {
7970 * }
7971 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007972 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7973 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007975 else if (curbuf->b_ind_js)
7976 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007978 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979
7980 start_brace = BRACE_AT_END;
7981 }
7982
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007983 /* For Javascript check if the line starts with "key:". */
7984 if (curbuf->b_ind_js)
7985 js_cur_has_key = cin_has_js_key(theline);
7986
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007988 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 * we want to be. otherwise, add the amount of room
7990 * that an indent is supposed to be.
7991 */
7992 if (theline[0] == '}')
7993 {
7994 /*
7995 * they may want closing braces to line up with something
7996 * other than the open brace. indulge them, if so.
7997 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007998 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 }
8000 else
8001 {
8002 /*
8003 * If we're looking at an "else", try to find an "if"
8004 * to match it with.
8005 * If we're looking at a "while", try to find a "do"
8006 * to match it with.
8007 */
8008 lookfor = LOOKFOR_INITIAL;
8009 if (cin_iselse(theline))
8010 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008011 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 lookfor = LOOKFOR_DO;
8013 if (lookfor != LOOKFOR_INITIAL)
8014 {
8015 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008016 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {
8018 amount = get_indent(); /* XXX */
8019 goto theend;
8020 }
8021 }
8022
8023 /*
8024 * We get here if we are not on an "while-of-do" or "else" (or
8025 * failed to find a matching "if").
8026 * Search backwards for something to line up with.
8027 * First set amount for when we don't find anything.
8028 */
8029
8030 /*
8031 * if the '{' is _really_ at the left margin, use the imaginary
8032 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008033 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 */
8035
8036 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8037 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008038 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008039 lookfor_cpp_namespace = TRUE;
8040 }
8041 else if (start_brace == BRACE_AT_START &&
8042 lookfor_cpp_namespace) /* '{' is at start */
8043 {
8044
8045 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
8047 else
8048 {
8049 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008050 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008051 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008052
8053 l = skipwhite(ml_get_curline());
8054 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008055 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008056 else if (cin_is_cpp_extern_c(l))
8057 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 else
8060 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008061 /* Compensate for adding b_ind_open_extra later. */
8062 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 if (amount < 0)
8064 amount = 0;
8065 }
8066 }
8067
8068 lookfor_break = FALSE;
8069
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008070 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
8072 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008073 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 }
8075 else if (cin_isscopedecl(theline)) /* private:, ... */
8076 {
8077 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008078 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 }
8080 else
8081 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008082 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8083 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 lookfor_break = TRUE;
8085
8086 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008087 /* b_ind_level from start of block */
8088 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 }
8090 scope_amount = amount;
8091 whilelevel = 0;
8092
8093 /*
8094 * Search backwards. If we find something we recognize, line up
8095 * with that.
8096 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008097 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 * the usual amount relative to the conditional
8099 * that opens the block.
8100 */
8101 curwin->w_cursor = cur_curpos;
8102 for (;;)
8103 {
8104 curwin->w_cursor.lnum--;
8105 curwin->w_cursor.col = 0;
8106
8107 /*
8108 * If we went all the way back to the start of our scope, line
8109 * up with it.
8110 */
8111 if (curwin->w_cursor.lnum <= ourscope)
8112 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008113 /* We reached end of scope:
8114 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008116 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 * don't add ind_continuation, otherwise it is a variable
8118 * declaration:
8119 * int x,
8120 * here; <-- add ind_continuation
8121 */
8122 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8123 {
8124 if (curwin->w_cursor.lnum == 0
8125 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008126 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008128 /* nothing found (abuse curbuf->b_ind_maxparen as
8129 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 * initialization) */
8131 if (cont_amount > 0)
8132 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008133 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 amount += ind_continuation;
8135 break;
8136 }
8137
8138 l = ml_get_curline();
8139
8140 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008141 * If we're in a comment or raw string now, skip to
8142 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008144 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (trypos != NULL)
8146 {
8147 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008148 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 continue;
8150 }
8151
8152 /*
8153 * Skip preprocessor directives and blank lines.
8154 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008155 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8156 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 continue;
8158
8159 if (cin_nocode(l))
8160 continue;
8161
8162 terminated = cin_isterminated(l, FALSE, TRUE);
8163
8164 /*
8165 * If we are at top level and the line looks like a
8166 * function declaration, we are done
8167 * (it's a variable declaration).
8168 */
8169 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008170 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
8172 /* if the line is terminated with another ','
8173 * it is a continued variable initialization.
8174 * don't add extra indent.
8175 * TODO: does not work, if a function
8176 * declaration is split over multiple lines:
8177 * cin_isfuncdecl returns FALSE then.
8178 */
8179 if (terminated == ',')
8180 break;
8181
8182 /* if it es a enum declaration or an assignment,
8183 * we are done.
8184 */
8185 if (terminated != ';' && cin_isinit())
8186 break;
8187
8188 /* nothing useful found */
8189 if (terminated == 0 || terminated == '{')
8190 continue;
8191 }
8192
8193 if (terminated != ';')
8194 {
8195 /* Skip parens and braces. Position the cursor
8196 * over the rightmost paren, so that matching it
8197 * will take us back to the start of the line.
8198 */ /* XXX */
8199 trypos = NULL;
8200 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008201 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008202 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
8204 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008205 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206
8207 if (trypos != NULL)
8208 {
8209 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008210 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 continue;
8212 }
8213 }
8214
8215 /* it's a variable declaration, add indentation
8216 * like in
8217 * int a,
8218 * b;
8219 */
8220 if (cont_amount > 0)
8221 amount = cont_amount;
8222 else
8223 amount += ind_continuation;
8224 }
8225 else if (lookfor == LOOKFOR_UNTERM)
8226 {
8227 if (cont_amount > 0)
8228 amount = cont_amount;
8229 else
8230 amount += ind_continuation;
8231 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008232 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008233 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008234 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008235 && lookfor != LOOKFOR_CPP_BASECLASS
8236 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008237 {
8238 amount = scope_amount;
8239 if (theline[0] == '{')
8240 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008241 amount += curbuf->b_ind_open_extra;
8242 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008243 }
8244 }
8245
8246 if (lookfor_cpp_namespace)
8247 {
8248 /*
8249 * Looking for C++ namespace, need to look further
8250 * back.
8251 */
8252 if (curwin->w_cursor.lnum == ourscope)
8253 continue;
8254
8255 if (curwin->w_cursor.lnum == 0
8256 || curwin->w_cursor.lnum
8257 < ourscope - FIND_NAMESPACE_LIM)
8258 break;
8259
8260 l = ml_get_curline();
8261
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008262 /* If we're in a comment or raw string now, skip
8263 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008264 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008265 if (trypos != NULL)
8266 {
8267 curwin->w_cursor.lnum = trypos->lnum + 1;
8268 curwin->w_cursor.col = 0;
8269 continue;
8270 }
8271
8272 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008273 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8274 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008275 continue;
8276
8277 /* Finally the actual check for "namespace". */
8278 if (cin_is_cpp_namespace(l))
8279 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008280 amount += curbuf->b_ind_cpp_namespace
8281 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008282 break;
8283 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008284 else if (cin_is_cpp_extern_c(l))
8285 {
8286 amount += curbuf->b_ind_cpp_extern_c
8287 - added_to_amount;
8288 break;
8289 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008290
8291 if (cin_nocode(l))
8292 continue;
8293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 }
8295 break;
8296 }
8297
8298 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008299 * If we're in a comment or raw string now, skip to the start
8300 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008302 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {
8304 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008305 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 continue;
8307 }
8308
8309 l = ml_get_curline();
8310
8311 /*
8312 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008313 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008315 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 if (iscase || cin_isscopedecl(l))
8317 {
8318 /* we are only looking for cpp base class
8319 * declaration/initialization any longer */
8320 if (lookfor == LOOKFOR_CPP_BASECLASS)
8321 break;
8322
8323 /* When looking for a "do" we are not interested in
8324 * labels. */
8325 if (whilelevel > 0)
8326 continue;
8327
8328 /*
8329 * case xx:
8330 * c = 99 + <- this indent plus continuation
8331 *-> here;
8332 */
8333 if (lookfor == LOOKFOR_UNTERM
8334 || lookfor == LOOKFOR_ENUM_OR_INIT)
8335 {
8336 if (cont_amount > 0)
8337 amount = cont_amount;
8338 else
8339 amount += ind_continuation;
8340 break;
8341 }
8342
8343 /*
8344 * case xx: <- line up with this case
8345 * x = 333;
8346 * case yy:
8347 */
8348 if ( (iscase && lookfor == LOOKFOR_CASE)
8349 || (iscase && lookfor_break)
8350 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8351 {
8352 /*
8353 * Check that this case label is not for another
8354 * switch()
8355 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008356 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008357 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {
8359 amount = get_indent(); /* XXX */
8360 break;
8361 }
8362 continue;
8363 }
8364
8365 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8366
8367 /*
8368 * case xx: if (cond) <- line up with this if
8369 * y = y + 1;
8370 * -> s = 99;
8371 *
8372 * case xx:
8373 * if (cond) <- line up with this line
8374 * y = y + 1;
8375 * -> s = 99;
8376 */
8377 if (lookfor == LOOKFOR_TERM)
8378 {
8379 if (n)
8380 amount = n;
8381
8382 if (!lookfor_break)
8383 break;
8384 }
8385
8386 /*
8387 * case xx: x = x + 1; <- line up with this x
8388 * -> y = y + 1;
8389 *
8390 * case xx: if (cond) <- line up with this if
8391 * -> y = y + 1;
8392 */
8393 if (n)
8394 {
8395 amount = n;
8396 l = after_label(ml_get_curline());
8397 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008398 {
8399 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008400 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008401 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008402 amount += curbuf->b_ind_level
8403 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 break;
8406 }
8407
8408 /*
8409 * Try to get the indent of a statement before the switch
8410 * label. If nothing is found, line up relative to the
8411 * switch label.
8412 * break; <- may line up with this line
8413 * case xx:
8414 * -> y = 1;
8415 */
8416 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008417 ? curbuf->b_ind_case_code
8418 : curbuf->b_ind_scopedecl_code);
8419 lookfor = curbuf->b_ind_case_break
8420 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 continue;
8422 }
8423
8424 /*
8425 * Looking for a switch() label or C++ scope declaration,
8426 * ignore other lines, skip {}-blocks.
8427 */
8428 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8429 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008430 if (find_last_paren(l, '{', '}')
8431 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008434 curwin->w_cursor.col = 0;
8435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 continue;
8437 }
8438
8439 /*
8440 * Ignore jump labels with nothing after them.
8441 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008442 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
8444 l = after_label(ml_get_curline());
8445 if (l == NULL || cin_nocode(l))
8446 continue;
8447 }
8448
8449 /*
8450 * Ignore #defines, #if, etc.
8451 * Ignore comment and empty lines.
8452 * (need to get the line again, cin_islabel() may have
8453 * unlocked it)
8454 */
8455 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008456 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 || cin_nocode(l))
8458 continue;
8459
8460 /*
8461 * Are we at the start of a cpp base class declaration or
8462 * constructor initialization?
8463 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008464 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008465 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008466 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008467 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008468 l = ml_get_curline();
8469 }
8470 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
8472 if (lookfor == LOOKFOR_UNTERM)
8473 {
8474 if (cont_amount > 0)
8475 amount = cont_amount;
8476 else
8477 amount += ind_continuation;
8478 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008479 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008481 /* Need to find start of the declaration. */
8482 lookfor = LOOKFOR_UNTERM;
8483 ind_continuation = 0;
8484 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 }
8486 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008487 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008488 amount = get_baseclass_amount(
8489 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 break;
8491 }
8492 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8493 {
8494 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008495 * declaration or initialization before the opening brace.
8496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 if (cin_isterminated(l, TRUE, FALSE))
8498 break;
8499 else
8500 continue;
8501 }
8502
8503 /*
8504 * What happens next depends on the line being terminated.
8505 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008506 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 * 123,
8508 * sizeof
8509 * here
8510 * Otherwise check whether it is a enumeration or structure
8511 * initialisation (not indented) or a variable declaration
8512 * (indented).
8513 */
8514 terminated = cin_isterminated(l, FALSE, TRUE);
8515
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008516 if (js_cur_has_key)
8517 {
8518 js_cur_has_key = 0; /* only check the first line */
8519 if (curbuf->b_ind_js && terminated == ',')
8520 {
8521 /* For Javascript we might be inside an object:
8522 * key: something, <- align with this
8523 * key: something
8524 * or:
8525 * key: something + <- align with this
8526 * something,
8527 * key: something
8528 */
8529 lookfor = LOOKFOR_JS_KEY;
8530 }
8531 }
8532 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8533 {
8534 amount = get_indent();
8535 break;
8536 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008537 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008538 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008539 if (tryposBrace != NULL && tryposBrace->lnum
8540 >= curwin->w_cursor.lnum)
8541 break;
8542 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008543 /* line below current line is the one that starts a
8544 * (possibly broken) line ending in a comma. */
8545 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008546 else
8547 {
8548 amount = get_indent();
8549 if (curwin->w_cursor.lnum - 1 == ourscope)
8550 /* line above is start of the scope, thus current
8551 * line is the one that stars a (possibly broken)
8552 * line ending in a comma. */
8553 break;
8554 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008555 }
8556
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8558 && terminated == ','))
8559 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008560 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8561 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008562 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 /*
8564 * if we're in the middle of a paren thing,
8565 * go back to the line that starts it so
8566 * we can get the right prevailing indent
8567 * if ( foo &&
8568 * bar )
8569 */
8570 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008571 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008573 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 */
8575 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008576 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008577 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8578 || (trypos->lnum == tryposBrace->lnum
8579 && trypos->col < tryposBrace->col)))
8580 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581
8582 /*
8583 * If we are looking for ',', we also look for matching
8584 * braces.
8585 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008586 if (trypos == NULL && terminated == ','
8587 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008588 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 if (trypos != NULL)
8591 {
8592 /*
8593 * Check if we are on a case label now. This is
8594 * handled above.
8595 * case xx: if ( asdf &&
8596 * asdf)
8597 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008598 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008600 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 {
8602 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008603 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 continue;
8605 }
8606 }
8607
8608 /*
8609 * Skip over continuation lines to find the one to get the
8610 * indent from
8611 * char *usethis = "bla\
8612 * bla",
8613 * here;
8614 */
8615 if (terminated == ',')
8616 {
8617 while (curwin->w_cursor.lnum > 1)
8618 {
8619 l = ml_get(curwin->w_cursor.lnum - 1);
8620 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8621 break;
8622 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008623 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 }
8625 }
8626
8627 /*
8628 * Get indent and pointer to text for current line,
8629 * ignoring any jump label. XXX
8630 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008631 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008632 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008633 else
8634 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 /*
8636 * If this is just above the line we are indenting, and it
8637 * starts with a '{', line it up with this line.
8638 * while (not)
8639 * -> {
8640 * }
8641 */
8642 if (terminated != ',' && lookfor != LOOKFOR_TERM
8643 && theline[0] == '{')
8644 {
8645 amount = cur_amount;
8646 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008647 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 * doesn't start with a '{', which must have a match
8649 * in the same line (scope is the same). Probably:
8650 * { 1, 2 },
8651 * -> { 3, 4 }
8652 */
8653 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008654 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008656 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {
8658 /* have to look back, whether it is a cpp base
8659 * class declaration or initialization */
8660 lookfor = LOOKFOR_CPP_BASECLASS;
8661 continue;
8662 }
8663 break;
8664 }
8665
8666 /*
8667 * Check if we are after an "if", "while", etc.
8668 * Also allow " } else".
8669 */
8670 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8671 {
8672 /*
8673 * Found an unterminated line after an if (), line up
8674 * with the last one.
8675 * if (cond)
8676 * 100 +
8677 * -> here;
8678 */
8679 if (lookfor == LOOKFOR_UNTERM
8680 || lookfor == LOOKFOR_ENUM_OR_INIT)
8681 {
8682 if (cont_amount > 0)
8683 amount = cont_amount;
8684 else
8685 amount += ind_continuation;
8686 break;
8687 }
8688
8689 /*
8690 * If this is just above the line we are indenting, we
8691 * are finished.
8692 * while (not)
8693 * -> here;
8694 * Otherwise this indent can be used when the line
8695 * before this is terminated.
8696 * yyy;
8697 * if (stat)
8698 * while (not)
8699 * xxx;
8700 * -> here;
8701 */
8702 amount = cur_amount;
8703 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008704 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 if (lookfor != LOOKFOR_TERM)
8706 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008707 amount += curbuf->b_ind_level
8708 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 break;
8710 }
8711
8712 /*
8713 * Special trick: when expecting the while () after a
8714 * do, line up with the while()
8715 * do
8716 * x = 1;
8717 * -> here
8718 */
8719 l = skipwhite(ml_get_curline());
8720 if (cin_isdo(l))
8721 {
8722 if (whilelevel == 0)
8723 break;
8724 --whilelevel;
8725 }
8726
8727 /*
8728 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008729 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 * Need to use the scope of this "else". XXX
8731 * If whilelevel != 0 continue looking for a "do {".
8732 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008733 if (cin_iselse(l) && whilelevel == 0)
8734 {
8735 /* If we're looking at "} else", let's make sure we
8736 * find the opening brace of the enclosing scope,
8737 * not the one from "if () {". */
8738 if (*l == '}')
8739 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008740 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008741
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008742 if ((trypos = find_start_brace()) == NULL
8743 || find_match(LOOKFOR_IF, trypos->lnum)
8744 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008745 break;
8746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 }
8748
8749 /*
8750 * If we're below an unterminated line that is not an
8751 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008752 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 * the line before this one.
8754 */
8755 else
8756 {
8757 /*
8758 * Found two unterminated lines on a row, line up with
8759 * the last one.
8760 * c = 99 +
8761 * 100 +
8762 * -> here;
8763 */
8764 if (lookfor == LOOKFOR_UNTERM)
8765 {
8766 /* When line ends in a comma add extra indent */
8767 if (terminated == ',')
8768 amount += ind_continuation;
8769 break;
8770 }
8771
8772 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8773 {
8774 /* Found two lines ending in ',', lineup with the
8775 * lowest one, but check for cpp base class
8776 * declaration/initialization, if it is an
8777 * opening brace or we are looking just for
8778 * enumerations/initializations. */
8779 if (terminated == ',')
8780 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008781 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 break;
8783
8784 lookfor = LOOKFOR_CPP_BASECLASS;
8785 continue;
8786 }
8787
8788 /* Ignore unterminated lines in between, but
8789 * reduce indent. */
8790 if (amount > cur_amount)
8791 amount = cur_amount;
8792 }
8793 else
8794 {
8795 /*
8796 * Found first unterminated line on a row, may
8797 * line up with this line, remember its indent
8798 * 100 +
8799 * -> here;
8800 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008801 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008803
8804 n = (int)STRLEN(l);
8805 if (terminated == ',' && (*skipwhite(l) == ']'
8806 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
8809 /*
8810 * If previous line ends in ',', check whether we
8811 * are in an initialization or enum
8812 * struct xxx =
8813 * {
8814 * sizeof a,
8815 * 124 };
8816 * or a normal possible continuation line.
8817 * but only, of no other statement has been found
8818 * yet.
8819 */
8820 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8821 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008822 if (curbuf->b_ind_js)
8823 {
8824 /* Search for a line ending in a comma
8825 * and line up with the line below it
8826 * (could be the current line).
8827 * some = [
8828 * 1, <- line up here
8829 * 2,
8830 * some = [
8831 * 3 + <- line up here
8832 * 4 *
8833 * 5,
8834 * 6,
8835 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008836 if (cin_iscomment(skipwhite(l)))
8837 break;
8838 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008839 trypos = find_match_char('[',
8840 curbuf->b_ind_maxparen);
8841 if (trypos != NULL)
8842 {
8843 if (trypos->lnum
8844 == curwin->w_cursor.lnum - 1)
8845 {
8846 /* Current line is first inside
8847 * [], line up with it. */
8848 break;
8849 }
8850 ourscope = trypos->lnum;
8851 }
8852 }
8853 else
8854 {
8855 lookfor = LOOKFOR_ENUM_OR_INIT;
8856 cont_amount = cin_first_id_amount();
8857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 }
8859 else
8860 {
8861 if (lookfor == LOOKFOR_INITIAL
8862 && *l != NUL
8863 && l[STRLEN(l) - 1] == '\\')
8864 /* XXX */
8865 cont_amount = cin_get_equal_amount(
8866 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008867 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008868 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008869 && lookfor != LOOKFOR_COMMA
8870 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 lookfor = LOOKFOR_UNTERM;
8872 }
8873 }
8874 }
8875 }
8876
8877 /*
8878 * Check if we are after a while (cond);
8879 * If so: Ignore until the matching "do".
8880 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008881 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 {
8883 /*
8884 * Found an unterminated line after a while ();, line up
8885 * with the last one.
8886 * while (cond);
8887 * 100 + <- line up with this one
8888 * -> here;
8889 */
8890 if (lookfor == LOOKFOR_UNTERM
8891 || lookfor == LOOKFOR_ENUM_OR_INIT)
8892 {
8893 if (cont_amount > 0)
8894 amount = cont_amount;
8895 else
8896 amount += ind_continuation;
8897 break;
8898 }
8899
8900 if (whilelevel == 0)
8901 {
8902 lookfor = LOOKFOR_TERM;
8903 amount = get_indent(); /* XXX */
8904 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008905 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 }
8907 ++whilelevel;
8908 }
8909
8910 /*
8911 * We are after a "normal" statement.
8912 * If we had another statement we can stop now and use the
8913 * indent of that other statement.
8914 * Otherwise the indent of the current statement may be used,
8915 * search backwards for the next "normal" statement.
8916 */
8917 else
8918 {
8919 /*
8920 * Skip single break line, if before a switch label. It
8921 * may be lined up with the case label.
8922 */
8923 if (lookfor == LOOKFOR_NOBREAK
8924 && cin_isbreak(skipwhite(ml_get_curline())))
8925 {
8926 lookfor = LOOKFOR_ANY;
8927 continue;
8928 }
8929
8930 /*
8931 * Handle "do {" line.
8932 */
8933 if (whilelevel > 0)
8934 {
8935 l = cin_skipcomment(ml_get_curline());
8936 if (cin_isdo(l))
8937 {
8938 amount = get_indent(); /* XXX */
8939 --whilelevel;
8940 continue;
8941 }
8942 }
8943
8944 /*
8945 * Found a terminated line above an unterminated line. Add
8946 * the amount for a continuation line.
8947 * x = 1;
8948 * y = foo +
8949 * -> here;
8950 * or
8951 * int x = 1;
8952 * int foo,
8953 * -> here;
8954 */
8955 if (lookfor == LOOKFOR_UNTERM
8956 || lookfor == LOOKFOR_ENUM_OR_INIT)
8957 {
8958 if (cont_amount > 0)
8959 amount = cont_amount;
8960 else
8961 amount += ind_continuation;
8962 break;
8963 }
8964
8965 /*
8966 * Found a terminated line above a terminated line or "if"
8967 * etc. line. Use the amount of the line below us.
8968 * x = 1; x = 1;
8969 * if (asdf) y = 2;
8970 * while (asdf) ->here;
8971 * here;
8972 * ->foo;
8973 */
8974 if (lookfor == LOOKFOR_TERM)
8975 {
8976 if (!lookfor_break && whilelevel == 0)
8977 break;
8978 }
8979
8980 /*
8981 * First line above the one we're indenting is terminated.
8982 * To know what needs to be done look further backward for
8983 * a terminated line.
8984 */
8985 else
8986 {
8987 /*
8988 * position the cursor over the rightmost paren, so
8989 * that matching it will take us back to the start of
8990 * the line. Helps for:
8991 * func(asdr,
8992 * asdfasdf);
8993 * here;
8994 */
8995term_again:
8996 l = ml_get_curline();
8997 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008998 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008999 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 {
9001 /*
9002 * Check if we are on a case label now. This is
9003 * handled above.
9004 * case xx: if ( asdf &&
9005 * asdf)
9006 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009007 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009009 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 {
9011 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009012 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 continue;
9014 }
9015 }
9016
9017 /* When aligning with the case statement, don't align
9018 * with a statement after it.
9019 * case 1: { <-- don't use this { position
9020 * stat;
9021 * }
9022 * case 2:
9023 * stat;
9024 * }
9025 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009026 iscase = (curbuf->b_ind_keep_case_label
9027 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028
9029 /*
9030 * Get indent and pointer to text for current line,
9031 * ignoring any jump label.
9032 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009033 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034
9035 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009036 amount += curbuf->b_ind_open_extra;
9037 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009038 l = skipwhite(l);
9039 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009040 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9042
9043 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009044 * When a terminated line starts with "else" skip to
9045 * the matching "if":
9046 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009047 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009048 * Need to use the scope of this "else". XXX
9049 * If whilelevel != 0 continue looking for a "do {".
9050 */
9051 if (lookfor == LOOKFOR_TERM
9052 && *l != '}'
9053 && cin_iselse(l)
9054 && whilelevel == 0)
9055 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009056 if ((trypos = find_start_brace()) == NULL
9057 || find_match(LOOKFOR_IF, trypos->lnum)
9058 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009059 break;
9060 continue;
9061 }
9062
9063 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 * If we're at the end of a block, skip to the start of
9065 * that block.
9066 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009067 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009068 if (find_last_paren(l, '{', '}') /* XXX */
9069 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009071 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 /* if not "else {" check for terminated again */
9073 /* but skip block for "} else {" */
9074 l = cin_skipcomment(ml_get_curline());
9075 if (*l == '}' || !cin_iselse(l))
9076 goto term_again;
9077 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009078 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 }
9080 }
9081 }
9082 }
9083 }
9084 }
9085
9086 /* add extra indent for a comment */
9087 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009088 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009089
9090 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009091 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9092 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009093
9094 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009096
9097 /*
9098 * ok -- we're not inside any sort of structure at all!
9099 *
9100 * This means we're at the top level, and everything should
9101 * basically just match where the previous line is, except
9102 * for the lines immediately following a function declaration,
9103 * which are K&R-style parameters and need to be indented.
9104 *
9105 * if our line starts with an open brace, forget about any
9106 * prevailing indent and make sure it looks like the start
9107 * of a function
9108 */
9109
9110 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009112 amount = curbuf->b_ind_first_open;
9113 goto theend;
9114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009116 /*
9117 * If the NEXT line is a function declaration, the current
9118 * line needs to be indented as a function type spec.
9119 * Don't do this if the current line looks like a comment or if the
9120 * current line is terminated, ie. ends in ';', or if the current line
9121 * contains { or }: "void f() {\n if (1)"
9122 */
9123 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9124 && !cin_nocode(theline)
9125 && vim_strchr(theline, '{') == NULL
9126 && vim_strchr(theline, '}') == NULL
9127 && !cin_ends_in(theline, (char_u *)":", NULL)
9128 && !cin_ends_in(theline, (char_u *)",", NULL)
9129 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9130 cur_curpos.lnum + 1)
9131 && !cin_isterminated(theline, FALSE, TRUE))
9132 {
9133 amount = curbuf->b_ind_func_type;
9134 goto theend;
9135 }
9136
9137 /* search backwards until we find something we recognize */
9138 amount = 0;
9139 curwin->w_cursor = cur_curpos;
9140 while (curwin->w_cursor.lnum > 1)
9141 {
9142 curwin->w_cursor.lnum--;
9143 curwin->w_cursor.col = 0;
9144
9145 l = ml_get_curline();
9146
9147 /*
9148 * If we're in a comment or raw string now, skip to the start
9149 * of it.
9150 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009151 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009153 curwin->w_cursor.lnum = trypos->lnum + 1;
9154 curwin->w_cursor.col = 0;
9155 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 }
9157
9158 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009159 * Are we at the start of a cpp base class declaration or
9160 * constructor initialization?
9161 */ /* XXX */
9162 n = FALSE;
9163 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009165 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9166 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009168 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009170 /* XXX */
9171 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9172 break;
9173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009175 /*
9176 * Skip preprocessor directives and blank lines.
9177 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009178 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009179 continue;
9180
9181 if (cin_nocode(l))
9182 continue;
9183
9184 /*
9185 * If the previous line ends in ',', use one level of
9186 * indentation:
9187 * int foo,
9188 * bar;
9189 * do this before checking for '}' in case of eg.
9190 * enum foobar
9191 * {
9192 * ...
9193 * } foo,
9194 * bar;
9195 */
9196 n = 0;
9197 if (cin_ends_in(l, (char_u *)",", NULL)
9198 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9199 {
9200 /* take us back to opening paren */
9201 if (find_last_paren(l, '(', ')')
9202 && (trypos = find_match_paren(
9203 curbuf->b_ind_maxparen)) != NULL)
9204 curwin->w_cursor = *trypos;
9205
9206 /* For a line ending in ',' that is a continuation line go
9207 * back to the first line with a backslash:
9208 * char *foo = "bla\
9209 * bla",
9210 * here;
9211 */
9212 while (n == 0 && curwin->w_cursor.lnum > 1)
9213 {
9214 l = ml_get(curwin->w_cursor.lnum - 1);
9215 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9216 break;
9217 --curwin->w_cursor.lnum;
9218 curwin->w_cursor.col = 0;
9219 }
9220
9221 amount = get_indent(); /* XXX */
9222
9223 if (amount == 0)
9224 amount = cin_first_id_amount();
9225 if (amount == 0)
9226 amount = ind_continuation;
9227 break;
9228 }
9229
9230 /*
9231 * If the line looks like a function declaration, and we're
9232 * not in a comment, put it the left margin.
9233 */
9234 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9235 break;
9236 l = ml_get_curline();
9237
9238 /*
9239 * Finding the closing '}' of a previous function. Put
9240 * current line at the left margin. For when 'cino' has "fs".
9241 */
9242 if (*skipwhite(l) == '}')
9243 break;
9244
9245 /* (matching {)
9246 * If the previous line ends on '};' (maybe followed by
9247 * comments) align at column 0. For example:
9248 * char *string_array[] = { "foo",
9249 * / * x * / "b};ar" }; / * foobar * /
9250 */
9251 if (cin_ends_in(l, (char_u *)"};", NULL))
9252 break;
9253
9254 /*
9255 * If the previous line ends on '[' we are probably in an
9256 * array constant:
9257 * something = [
9258 * 234, <- extra indent
9259 */
9260 if (cin_ends_in(l, (char_u *)"[", NULL))
9261 {
9262 amount = get_indent() + ind_continuation;
9263 break;
9264 }
9265
9266 /*
9267 * Find a line only has a semicolon that belongs to a previous
9268 * line ending in '}', e.g. before an #endif. Don't increase
9269 * indent then.
9270 */
9271 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9272 {
9273 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274
9275 while (curwin->w_cursor.lnum > 1)
9276 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009277 look = ml_get(--curwin->w_cursor.lnum);
9278 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009279 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009281 }
9282 if (curwin->w_cursor.lnum > 0
9283 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009286 curwin->w_cursor = curpos_save;
9287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009289 /*
9290 * If the PREVIOUS line is a function declaration, the current
9291 * line (and the ones that follow) needs to be indented as
9292 * parameters.
9293 */
9294 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9295 {
9296 amount = curbuf->b_ind_param;
9297 break;
9298 }
9299
9300 /*
9301 * If the previous line ends in ';' and the line before the
9302 * previous line ends in ',' or '\', ident to column zero:
9303 * int foo,
9304 * bar;
9305 * indent_to_0 here;
9306 */
9307 if (cin_ends_in(l, (char_u *)";", NULL))
9308 {
9309 l = ml_get(curwin->w_cursor.lnum - 1);
9310 if (cin_ends_in(l, (char_u *)",", NULL)
9311 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9312 break;
9313 l = ml_get_curline();
9314 }
9315
9316 /*
9317 * Doesn't look like anything interesting -- so just
9318 * use the indent of this line.
9319 *
9320 * Position the cursor over the rightmost paren, so that
9321 * matching it will take us back to the start of the line.
9322 */
9323 find_last_paren(l, '(', ')');
9324
9325 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9326 curwin->w_cursor = *trypos;
9327 amount = get_indent(); /* XXX */
9328 break;
9329 }
9330
9331 /* add extra indent for a comment */
9332 if (cin_iscomment(theline))
9333 amount += curbuf->b_ind_comment;
9334
9335 /* add extra indent if the previous line ended in a backslash:
9336 * "asdfasdf\
9337 * here";
9338 * char *foo = "asdf\
9339 * here";
9340 */
9341 if (cur_curpos.lnum > 1)
9342 {
9343 l = ml_get(cur_curpos.lnum - 1);
9344 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9345 {
9346 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9347 if (cur_amount > 0)
9348 amount = cur_amount;
9349 else if (cur_amount == 0)
9350 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 }
9352 }
9353
9354theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009355 if (amount < 0)
9356 amount = 0;
9357
9358laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 /* put the cursor back where it belongs */
9360 curwin->w_cursor = cur_curpos;
9361
9362 vim_free(linecopy);
9363
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 return amount;
9365}
9366
9367 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009368find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 char_u *look;
9371 pos_T *theirscope;
9372 char_u *mightbeif;
9373 int elselevel;
9374 int whilelevel;
9375
9376 if (lookfor == LOOKFOR_IF)
9377 {
9378 elselevel = 1;
9379 whilelevel = 0;
9380 }
9381 else
9382 {
9383 elselevel = 0;
9384 whilelevel = 1;
9385 }
9386
9387 curwin->w_cursor.col = 0;
9388
9389 while (curwin->w_cursor.lnum > ourscope + 1)
9390 {
9391 curwin->w_cursor.lnum--;
9392 curwin->w_cursor.col = 0;
9393
9394 look = cin_skipcomment(ml_get_curline());
9395 if (cin_iselse(look)
9396 || cin_isif(look)
9397 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009398 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 {
9400 /*
9401 * if we've gone outside the braces entirely,
9402 * we must be out of scope...
9403 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009404 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 if (theirscope == NULL)
9406 break;
9407
9408 /*
9409 * and if the brace enclosing this is further
9410 * back than the one enclosing the else, we're
9411 * out of luck too.
9412 */
9413 if (theirscope->lnum < ourscope)
9414 break;
9415
9416 /*
9417 * and if they're enclosed in a *deeper* brace,
9418 * then we can ignore it because it's in a
9419 * different scope...
9420 */
9421 if (theirscope->lnum > ourscope)
9422 continue;
9423
9424 /*
9425 * if it was an "else" (that's not an "else if")
9426 * then we need to go back to another if, so
9427 * increment elselevel
9428 */
9429 look = cin_skipcomment(ml_get_curline());
9430 if (cin_iselse(look))
9431 {
9432 mightbeif = cin_skipcomment(look + 4);
9433 if (!cin_isif(mightbeif))
9434 ++elselevel;
9435 continue;
9436 }
9437
9438 /*
9439 * if it was a "while" then we need to go back to
9440 * another "do", so increment whilelevel. XXX
9441 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009442 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 {
9444 ++whilelevel;
9445 continue;
9446 }
9447
9448 /* If it's an "if" decrement elselevel */
9449 look = cin_skipcomment(ml_get_curline());
9450 if (cin_isif(look))
9451 {
9452 elselevel--;
9453 /*
9454 * When looking for an "if" ignore "while"s that
9455 * get in the way.
9456 */
9457 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9458 whilelevel = 0;
9459 }
9460
9461 /* If it's a "do" decrement whilelevel */
9462 if (cin_isdo(look))
9463 whilelevel--;
9464
9465 /*
9466 * if we've used up all the elses, then
9467 * this must be the if that we want!
9468 * match the indent level of that if.
9469 */
9470 if (elselevel <= 0 && whilelevel <= 0)
9471 {
9472 return OK;
9473 }
9474 }
9475 }
9476 return FAIL;
9477}
9478
9479# if defined(FEAT_EVAL) || defined(PROTO)
9480/*
9481 * Get indent level from 'indentexpr'.
9482 */
9483 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009484get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009486 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009487 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009488 pos_T save_pos;
9489 colnr_T save_curswant;
9490 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009492 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9493 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009495 /* Save and restore cursor position and curswant, in case it was changed
9496 * via :normal commands */
9497 save_pos = curwin->w_cursor;
9498 save_curswant = curwin->w_curswant;
9499 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009501 if (use_sandbox)
9502 ++sandbox;
9503 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009504
9505 /* Need to make a copy, the 'indentexpr' option could be changed while
9506 * evaluating it. */
9507 inde_copy = vim_strsave(curbuf->b_p_inde);
9508 if (inde_copy != NULL)
9509 {
9510 indent = (int)eval_to_number(inde_copy);
9511 vim_free(inde_copy);
9512 }
9513
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009514 if (use_sandbox)
9515 --sandbox;
9516 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517
9518 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9519 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9520 * command. */
9521 save_State = State;
9522 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009523 curwin->w_cursor = save_pos;
9524 curwin->w_curswant = save_curswant;
9525 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 check_cursor();
9527 State = save_State;
9528
9529 /* If there is an error, just keep the current indent. */
9530 if (indent < 0)
9531 indent = get_indent();
9532
9533 return indent;
9534}
9535# endif
9536
9537#endif /* FEAT_CINDENT */
9538
9539#if defined(FEAT_LISP) || defined(PROTO)
9540
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009542lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544 char_u buf[LSIZE];
9545 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009546 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
9548 while (*word != NUL)
9549 {
9550 (void)copy_option_part(&word, buf, LSIZE, ",");
9551 len = (int)STRLEN(buf);
9552 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9553 return TRUE;
9554 }
9555 return FALSE;
9556}
9557
9558/*
9559 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9560 * The incompatible newer method is quite a bit better at indenting
9561 * code in lisp-like languages than the traditional one; it's still
9562 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9563 *
9564 * TODO:
9565 * Findmatch() should be adapted for lisp, also to make showmatch
9566 * work correctly: now (v5.3) it seems all C/C++ oriented:
9567 * - it does not recognize the #\( and #\) notations as character literals
9568 * - it doesn't know about comments starting with a semicolon
9569 * - it incorrectly interprets '(' as a character literal
9570 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009571 * Update from Sergey Khorev:
9572 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 */
9574 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009575get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009577 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 int amount;
9579 char_u *that;
9580 colnr_T col;
9581 colnr_T firsttry;
9582 int parencount, quotecount;
9583 int vi_lisp;
9584
9585 /* Set vi_lisp to use the vi-compatible method */
9586 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9587
9588 realpos = curwin->w_cursor;
9589 curwin->w_cursor.col = 0;
9590
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009591 if ((pos = findmatch(NULL, '(')) == NULL)
9592 pos = findmatch(NULL, '[');
9593 else
9594 {
9595 paren = *pos;
9596 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009597 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009598 pos = &paren;
9599 }
9600 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 {
9602 /* Extra trick: Take the indent of the first previous non-white
9603 * line that is at the same () level. */
9604 amount = -1;
9605 parencount = 0;
9606
9607 while (--curwin->w_cursor.lnum >= pos->lnum)
9608 {
9609 if (linewhite(curwin->w_cursor.lnum))
9610 continue;
9611 for (that = ml_get_curline(); *that != NUL; ++that)
9612 {
9613 if (*that == ';')
9614 {
9615 while (*(that + 1) != NUL)
9616 ++that;
9617 continue;
9618 }
9619 if (*that == '\\')
9620 {
9621 if (*(that + 1) != NUL)
9622 ++that;
9623 continue;
9624 }
9625 if (*that == '"' && *(that + 1) != NUL)
9626 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009627 while (*++that && *that != '"')
9628 {
9629 /* skipping escaped characters in the string */
9630 if (*that == '\\')
9631 {
9632 if (*++that == NUL)
9633 break;
9634 if (that[1] == NUL)
9635 {
9636 ++that;
9637 break;
9638 }
9639 }
9640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009642 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009644 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 --parencount;
9646 }
9647 if (parencount == 0)
9648 {
9649 amount = get_indent();
9650 break;
9651 }
9652 }
9653
9654 if (amount == -1)
9655 {
9656 curwin->w_cursor.lnum = pos->lnum;
9657 curwin->w_cursor.col = pos->col;
9658 col = pos->col;
9659
9660 that = ml_get_curline();
9661
9662 if (vi_lisp && get_indent() == 0)
9663 amount = 2;
9664 else
9665 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009666 char_u *line = that;
9667
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 amount = 0;
9669 while (*that && col)
9670 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009671 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 col--;
9673 }
9674
9675 /*
9676 * Some keywords require "body" indenting rules (the
9677 * non-standard-lisp ones are Scheme special forms):
9678 *
9679 * (let ((a 1)) instead (let ((a 1))
9680 * (...)) of (...))
9681 */
9682
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009683 if (!vi_lisp && (*that == '(' || *that == '[')
9684 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 amount += 2;
9686 else
9687 {
9688 that++;
9689 amount++;
9690 firsttry = amount;
9691
Bram Moolenaar1c465442017-03-12 20:10:05 +01009692 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009694 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 ++that;
9696 }
9697
9698 if (*that && *that != ';') /* not a comment line */
9699 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009700 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009702 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 firsttry++;
9704
9705 parencount = 0;
9706 quotecount = 0;
9707
9708 if (vi_lisp
9709 || (*that != '"'
9710 && *that != '\''
9711 && *that != '#'
9712 && (*that < '0' || *that > '9')))
9713 {
9714 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009715 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 || quotecount
9717 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009718 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 && !quotecount
9720 && !parencount
9721 && vi_lisp)))
9722 {
9723 if (*that == '"')
9724 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009725 if ((*that == '(' || *that == '[')
9726 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009728 if ((*that == ')' || *that == ']')
9729 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 --parencount;
9731 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009732 amount += lbr_chartabsize_adv(
9733 line, &that, (colnr_T)amount);
9734 amount += lbr_chartabsize_adv(
9735 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 }
9737 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009738 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009740 amount += lbr_chartabsize(
9741 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 that++;
9743 }
9744 if (!*that || *that == ';')
9745 amount = firsttry;
9746 }
9747 }
9748 }
9749 }
9750 }
9751 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009752 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753
9754 curwin->w_cursor = realpos;
9755
9756 return amount;
9757}
9758#endif /* FEAT_LISP */
9759
9760 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009761prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009763#if defined(SIGHUP) && defined(SIG_IGN)
9764 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9765 * makes Vim exit and then handling SIGHUP causes various reentrance
9766 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009767 signal(SIGHUP, SIG_IGN);
9768#endif
9769
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770#ifdef FEAT_GUI
9771 if (gui.in_use)
9772 {
9773 gui.dying = TRUE;
9774 out_trash(); /* trash any pending output */
9775 }
9776 else
9777#endif
9778 {
9779 windgoto((int)Rows - 1, 0);
9780
9781 /*
9782 * Switch terminal mode back now, so messages end up on the "normal"
9783 * screen (if there are two screens).
9784 */
9785 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009786 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 out_flush();
9788 }
9789}
9790
9791/*
9792 * Preserve files and exit.
9793 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009794 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9795 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 */
9797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009798preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800 buf_T *buf;
9801
9802 prepare_to_exit();
9803
Bram Moolenaar4770d092006-01-12 23:22:24 +00009804 /* Setting this will prevent free() calls. That avoids calling free()
9805 * recursively when free() was invoked with a bad pointer. */
9806 really_exiting = TRUE;
9807
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 out_str(IObuff);
9809 screen_start(); /* don't know where cursor is now */
9810 out_flush();
9811
9812 ml_close_notmod(); /* close all not-modified buffers */
9813
Bram Moolenaar29323592016-07-24 22:04:11 +02009814 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 {
9816 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9817 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009818 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 screen_start(); /* don't know where cursor is now */
9820 out_flush();
9821 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9822 break;
9823 }
9824 }
9825
9826 ml_close_all(FALSE); /* close all memfiles, without deleting */
9827
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009828 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829
9830 getout(1);
9831}
9832
9833/*
9834 * return TRUE if "fname" exists.
9835 */
9836 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009837vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009839 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
9841 if (mch_stat((char *)fname, &st))
9842 return FALSE;
9843 return TRUE;
9844}
9845
9846/*
9847 * Check for CTRL-C pressed, but only once in a while.
9848 * Should be used instead of ui_breakcheck() for functions that check for
9849 * each line in the file. Calling ui_breakcheck() each time takes too much
9850 * time, because it can be a system call.
9851 */
9852
9853#ifndef BREAKCHECK_SKIP
9854# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9855# define BREAKCHECK_SKIP 200
9856# else
9857# define BREAKCHECK_SKIP 32
9858# endif
9859#endif
9860
9861static int breakcheck_count = 0;
9862
9863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009864line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
9866 if (++breakcheck_count >= BREAKCHECK_SKIP)
9867 {
9868 breakcheck_count = 0;
9869 ui_breakcheck();
9870 }
9871}
9872
9873/*
9874 * Like line_breakcheck() but check 10 times less often.
9875 */
9876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009877fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878{
9879 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9880 {
9881 breakcheck_count = 0;
9882 ui_breakcheck();
9883 }
9884}
9885
9886/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009887 * Invoke expand_wildcards() for one pattern.
9888 * Expand items like "%:h" before the expansion.
9889 * Returns OK or FAIL.
9890 */
9891 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009892expand_wildcards_eval(
9893 char_u **pat, /* pointer to input pattern */
9894 int *num_file, /* resulting number of files */
9895 char_u ***file, /* array of resulting files */
9896 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009897{
9898 int ret = FAIL;
9899 char_u *eval_pat = NULL;
9900 char_u *exp_pat = *pat;
9901 char_u *ignored_msg;
9902 int usedlen;
9903
9904 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9905 {
9906 ++emsg_off;
9907 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9908 NULL, &ignored_msg, NULL);
9909 --emsg_off;
9910 if (eval_pat != NULL)
9911 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9912 }
9913
9914 if (exp_pat != NULL)
9915 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9916
9917 if (eval_pat != NULL)
9918 {
9919 vim_free(exp_pat);
9920 vim_free(eval_pat);
9921 }
9922
9923 return ret;
9924}
9925
9926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9928 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009929 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 */
9931 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009932expand_wildcards(
9933 int num_pat, /* number of input patterns */
9934 char_u **pat, /* array of input patterns */
9935 int *num_files, /* resulting number of files */
9936 char_u ***files, /* array of resulting files */
9937 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938{
9939 int retval;
9940 int i, j;
9941 char_u *p;
9942 int non_suf_match; /* number without matching suffix */
9943
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009944 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
9946 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009947 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 return retval;
9949
9950#ifdef FEAT_WILDIGN
9951 /*
9952 * Remove names that match 'wildignore'.
9953 */
9954 if (*p_wig)
9955 {
9956 char_u *ffname;
9957
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009958 /* check all files in (*files)[] */
9959 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009961 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 if (ffname == NULL) /* out of memory */
9963 break;
9964# ifdef VMS
9965 vms_remove_version(ffname);
9966# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009967 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009969 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009970 vim_free((*files)[i]);
9971 for (j = i; j + 1 < *num_files; ++j)
9972 (*files)[j] = (*files)[j + 1];
9973 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 --i;
9975 }
9976 vim_free(ffname);
9977 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009978
9979 /* If the number of matches is now zero, we fail. */
9980 if (*num_files == 0)
9981 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009982 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009983 return FAIL;
9984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 }
9986#endif
9987
9988 /*
9989 * Move the names where 'suffixes' match to the end.
9990 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009991 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 {
9993 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009994 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009996 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
9998 /*
9999 * Move the name without matching suffix to the front
10000 * of the list.
10001 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010002 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010004 (*files)[j] = (*files)[j - 1];
10005 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 }
10007 }
10008 }
10009
10010 return retval;
10011}
10012
10013/*
10014 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10015 */
10016 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010017match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018{
10019 int fnamelen, setsuflen;
10020 char_u *setsuf;
10021#define MAXSUFLEN 30 /* maximum length of a file suffix */
10022 char_u suf_buf[MAXSUFLEN];
10023
10024 fnamelen = (int)STRLEN(fname);
10025 setsuflen = 0;
10026 for (setsuf = p_su; *setsuf; )
10027 {
10028 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010029 if (setsuflen == 0)
10030 {
10031 char_u *tail = gettail(fname);
10032
10033 /* empty entry: match name without a '.' */
10034 if (vim_strchr(tail, '.') == NULL)
10035 {
10036 setsuflen = 1;
10037 break;
10038 }
10039 }
10040 else
10041 {
10042 if (fnamelen >= setsuflen
10043 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10044 (size_t)setsuflen) == 0)
10045 break;
10046 setsuflen = 0;
10047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 }
10049 return (setsuflen != 0);
10050}
10051
10052#if !defined(NO_EXPANDPATH) || defined(PROTO)
10053
10054# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010055static int vim_backtick(char_u *p);
10056static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057# endif
10058
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010059# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060/*
10061 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10062 * it's shared between these systems.
10063 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010064# if defined(PROTO)
10065# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066# else
10067# ifdef __BORLANDC__
10068# define _cdecl _RTLENTRYF
10069# endif
10070# endif
10071
10072/*
10073 * comparison function for qsort in dos_expandpath()
10074 */
10075 static int _cdecl
10076pstrcmp(const void *a, const void *b)
10077{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010078 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079}
10080
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010082 * Recursively expand one path component into all matching files and/or
10083 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 * Return the number of matches found.
10085 * "path" has backslashes before chars that are not to be expanded, starting
10086 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010087 * Return the number of matches found.
10088 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 */
10090 static int
10091dos_expandpath(
10092 garray_T *gap,
10093 char_u *path,
10094 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010095 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010096 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010098 char_u *buf;
10099 char_u *path_end;
10100 char_u *p, *s, *e;
10101 int start_len = gap->ga_len;
10102 char_u *pat;
10103 regmatch_T regmatch;
10104 int starts_with_dot;
10105 int matches;
10106 int len;
10107 int starstar = FALSE;
10108 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 WIN32_FIND_DATA fb;
10110 HANDLE hFind = (HANDLE)0;
10111# ifdef FEAT_MBYTE
10112 WIN32_FIND_DATAW wfb;
10113 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010116 int ok;
10117
10118 /* Expanding "**" may take a long time, check for CTRL-C. */
10119 if (stardepth > 0)
10120 {
10121 ui_breakcheck();
10122 if (got_int)
10123 return 0;
10124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010126 /* Make room for file name. When doing encoding conversion the actual
10127 * length may be quite a bit longer, thus use the maximum possible length. */
10128 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 if (buf == NULL)
10130 return 0;
10131
10132 /*
10133 * Find the first part in the path name that contains a wildcard or a ~1.
10134 * Copy it into buf, including the preceding characters.
10135 */
10136 p = buf;
10137 s = buf;
10138 e = NULL;
10139 path_end = path;
10140 while (*path_end != NUL)
10141 {
10142 /* May ignore a wildcard that has a backslash before it; it will
10143 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10144 if (path_end >= path + wildoff && rem_backslash(path_end))
10145 *p++ = *path_end++;
10146 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10147 {
10148 if (e != NULL)
10149 break;
10150 s = p + 1;
10151 }
10152 else if (path_end >= path + wildoff
10153 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10154 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010155# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 if (has_mbyte)
10157 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010158 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 STRNCPY(p, path_end, len);
10160 p += len;
10161 path_end += len;
10162 }
10163 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 *p++ = *path_end++;
10166 }
10167 e = p;
10168 *e = NUL;
10169
10170 /* now we have one wildcard component between s and e */
10171 /* Remove backslashes between "wildoff" and the start of the wildcard
10172 * component. */
10173 for (p = buf + wildoff; p < s; ++p)
10174 if (rem_backslash(p))
10175 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010176 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 --e;
10178 --s;
10179 }
10180
Bram Moolenaar231334e2005-07-25 20:46:57 +000010181 /* Check for "**" between "s" and "e". */
10182 for (p = s; p < e; ++p)
10183 if (p[0] == '*' && p[1] == '*')
10184 starstar = TRUE;
10185
Bram Moolenaard82103e2016-01-17 17:04:05 +010010186 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10188 if (pat == NULL)
10189 {
10190 vim_free(buf);
10191 return 0;
10192 }
10193
10194 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010195 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010196 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 regmatch.rm_ic = TRUE; /* Always ignore case */
10198 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010199 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010200 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 vim_free(pat);
10202
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010203 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 {
10205 vim_free(buf);
10206 return 0;
10207 }
10208
10209 /* remember the pattern or file name being looked for */
10210 matchname = vim_strsave(s);
10211
Bram Moolenaar231334e2005-07-25 20:46:57 +000010212 /* If "**" is by itself, this is the first time we encounter it and more
10213 * is following then find matches without any directory. */
10214 if (!didstar && stardepth < 100 && starstar && e - s == 2
10215 && *path_end == '/')
10216 {
10217 STRCPY(s, path_end + 1);
10218 ++stardepth;
10219 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10220 --stardepth;
10221 }
10222
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 /* Scan all files in the directory with "dir/ *.*" */
10224 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225# ifdef FEAT_MBYTE
10226 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10227 {
10228 /* The active codepage differs from 'encoding'. Attempt using the
10229 * wide function. If it fails because it is not implemented fall back
10230 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010231 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 if (wn != NULL)
10233 {
10234 hFind = FindFirstFileW(wn, &wfb);
10235 if (hFind == INVALID_HANDLE_VALUE
10236 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010237 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 }
10239 }
10240
10241 if (wn == NULL)
10242# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010243 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245
10246 while (ok)
10247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248# ifdef FEAT_MBYTE
10249 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010250 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 else
10252# endif
10253 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 /* Ignore entries starting with a dot, unless when asked for. Accept
10255 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010256 if ((p[0] != '.' || starts_with_dot
10257 || ((flags & EW_DODOT)
10258 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010260 || (regmatch.regprog != NULL
10261 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010262 || ((flags & EW_NOTWILD)
10263 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010267
10268 if (starstar && stardepth < 100)
10269 {
10270 /* For "**" in the pattern first go deeper in the tree to
10271 * find matches. */
10272 STRCPY(buf + len, "/**");
10273 STRCPY(buf + len + 3, path_end);
10274 ++stardepth;
10275 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10276 --stardepth;
10277 }
10278
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 STRCPY(buf + len, path_end);
10280 if (mch_has_exp_wildcard(path_end))
10281 {
10282 /* need to expand another component of the path */
10283 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010284 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 }
10286 else
10287 {
10288 /* no more wildcards, check if there is a match */
10289 /* remove backslashes for the remaining components only */
10290 if (*path_end != 0)
10291 backslash_halve(buf + len + 1);
10292 if (mch_getperm(buf) >= 0) /* add existing file */
10293 addfile(gap, buf, flags);
10294 }
10295 }
10296
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297# ifdef FEAT_MBYTE
10298 if (wn != NULL)
10299 {
10300 vim_free(p);
10301 ok = FindNextFileW(hFind, &wfb);
10302 }
10303 else
10304# endif
10305 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306
10307 /* If no more matches and no match was used, try expanding the name
10308 * itself. Finds the long name of a short filename. */
10309 if (!ok && matchname != NULL && gap->ga_len == start_len)
10310 {
10311 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 FindClose(hFind);
10313# ifdef FEAT_MBYTE
10314 if (wn != NULL)
10315 {
10316 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010317 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 if (wn != NULL)
10319 hFind = FindFirstFileW(wn, &wfb);
10320 }
10321 if (wn == NULL)
10322# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010323 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010325 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 }
10327 }
10328
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 FindClose(hFind);
10330# ifdef FEAT_MBYTE
10331 vim_free(wn);
10332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010334 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 vim_free(matchname);
10336
10337 matches = gap->ga_len - start_len;
10338 if (matches > 0)
10339 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10340 sizeof(char_u *), pstrcmp);
10341 return matches;
10342}
10343
10344 int
10345mch_expandpath(
10346 garray_T *gap,
10347 char_u *path,
10348 int flags) /* EW_* flags */
10349{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010350 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010352# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353
Bram Moolenaar231334e2005-07-25 20:46:57 +000010354#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10355 || defined(PROTO)
10356/*
10357 * Unix style wildcard expansion code.
10358 * It's here because it's used both for Unix and Mac.
10359 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010360 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010361pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010362{
10363 return (pathcmp(*(char **)a, *(char **)b, -1));
10364}
10365
10366/*
10367 * Recursively expand one path component into all matching files and/or
10368 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10369 * "path" has backslashes before chars that are not to be expanded, starting
10370 * at "path + wildoff".
10371 * Return the number of matches found.
10372 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10373 */
10374 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010375unix_expandpath(
10376 garray_T *gap,
10377 char_u *path,
10378 int wildoff,
10379 int flags, /* EW_* flags */
10380 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010381{
10382 char_u *buf;
10383 char_u *path_end;
10384 char_u *p, *s, *e;
10385 int start_len = gap->ga_len;
10386 char_u *pat;
10387 regmatch_T regmatch;
10388 int starts_with_dot;
10389 int matches;
10390 int len;
10391 int starstar = FALSE;
10392 static int stardepth = 0; /* depth for "**" expansion */
10393
10394 DIR *dirp;
10395 struct dirent *dp;
10396
10397 /* Expanding "**" may take a long time, check for CTRL-C. */
10398 if (stardepth > 0)
10399 {
10400 ui_breakcheck();
10401 if (got_int)
10402 return 0;
10403 }
10404
10405 /* make room for file name */
10406 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10407 if (buf == NULL)
10408 return 0;
10409
10410 /*
10411 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010412 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010413 * Copy it into "buf", including the preceding characters.
10414 */
10415 p = buf;
10416 s = buf;
10417 e = NULL;
10418 path_end = path;
10419 while (*path_end != NUL)
10420 {
10421 /* May ignore a wildcard that has a backslash before it; it will
10422 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10423 if (path_end >= path + wildoff && rem_backslash(path_end))
10424 *p++ = *path_end++;
10425 else if (*path_end == '/')
10426 {
10427 if (e != NULL)
10428 break;
10429 s = p + 1;
10430 }
10431 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010432 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010433 || (!p_fic && (flags & EW_ICASE)
10434 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010435 e = p;
10436#ifdef FEAT_MBYTE
10437 if (has_mbyte)
10438 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010439 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010440 STRNCPY(p, path_end, len);
10441 p += len;
10442 path_end += len;
10443 }
10444 else
10445#endif
10446 *p++ = *path_end++;
10447 }
10448 e = p;
10449 *e = NUL;
10450
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010451 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010452 /* Remove backslashes between "wildoff" and the start of the wildcard
10453 * component. */
10454 for (p = buf + wildoff; p < s; ++p)
10455 if (rem_backslash(p))
10456 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010457 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010458 --e;
10459 --s;
10460 }
10461
10462 /* Check for "**" between "s" and "e". */
10463 for (p = s; p < e; ++p)
10464 if (p[0] == '*' && p[1] == '*')
10465 starstar = TRUE;
10466
10467 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010468 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010469 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10470 if (pat == NULL)
10471 {
10472 vim_free(buf);
10473 return 0;
10474 }
10475
10476 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010477 if (flags & EW_ICASE)
10478 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10479 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010480 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010481 if (flags & (EW_NOERROR | EW_NOTWILD))
10482 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010483 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010484 if (flags & (EW_NOERROR | EW_NOTWILD))
10485 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010486 vim_free(pat);
10487
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010488 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010489 {
10490 vim_free(buf);
10491 return 0;
10492 }
10493
10494 /* If "**" is by itself, this is the first time we encounter it and more
10495 * is following then find matches without any directory. */
10496 if (!didstar && stardepth < 100 && starstar && e - s == 2
10497 && *path_end == '/')
10498 {
10499 STRCPY(s, path_end + 1);
10500 ++stardepth;
10501 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10502 --stardepth;
10503 }
10504
10505 /* open the directory for scanning */
10506 *s = NUL;
10507 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10508
10509 /* Find all matching entries */
10510 if (dirp != NULL)
10511 {
10512 for (;;)
10513 {
10514 dp = readdir(dirp);
10515 if (dp == NULL)
10516 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010517 if ((dp->d_name[0] != '.' || starts_with_dot
10518 || ((flags & EW_DODOT)
10519 && dp->d_name[1] != NUL
10520 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010521 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10522 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010523 || ((flags & EW_NOTWILD)
10524 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010525 {
10526 STRCPY(s, dp->d_name);
10527 len = STRLEN(buf);
10528
10529 if (starstar && stardepth < 100)
10530 {
10531 /* For "**" in the pattern first go deeper in the tree to
10532 * find matches. */
10533 STRCPY(buf + len, "/**");
10534 STRCPY(buf + len + 3, path_end);
10535 ++stardepth;
10536 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10537 --stardepth;
10538 }
10539
10540 STRCPY(buf + len, path_end);
10541 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10542 {
10543 /* need to expand another component of the path */
10544 /* remove backslashes for the remaining components only */
10545 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10546 }
10547 else
10548 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010549 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010550
Bram Moolenaar231334e2005-07-25 20:46:57 +000010551 /* no more wildcards, check if there is a match */
10552 /* remove backslashes for the remaining components only */
10553 if (*path_end != NUL)
10554 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010555 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010556 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010557 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010558 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010559#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010560 size_t precomp_len = STRLEN(buf)+1;
10561 char_u *precomp_buf =
10562 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010563
Bram Moolenaar231334e2005-07-25 20:46:57 +000010564 if (precomp_buf)
10565 {
10566 mch_memmove(buf, precomp_buf, precomp_len);
10567 vim_free(precomp_buf);
10568 }
10569#endif
10570 addfile(gap, buf, flags);
10571 }
10572 }
10573 }
10574 }
10575
10576 closedir(dirp);
10577 }
10578
10579 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010580 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010581
10582 matches = gap->ga_len - start_len;
10583 if (matches > 0)
10584 qsort(((char_u **)gap->ga_data) + start_len, matches,
10585 sizeof(char_u *), pstrcmp);
10586 return matches;
10587}
10588#endif
10589
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010590#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010591/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010592 * Moves "*psep" back to the previous path separator in "path".
10593 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010594 */
10595 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010596find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010597{
10598 /* skip the current separator */
10599 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010600 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010601
10602 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010603 while (*psep > path)
10604 {
10605 if (vim_ispathsep(**psep))
10606 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010607 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010608 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010609
10610 return FAIL;
10611}
10612
10613/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010614 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10615 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010616 */
10617 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010618is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010619{
10620 int j;
10621 int candidate_len;
10622 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010623 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010624 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010625
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010626 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010627 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010628 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010629 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010630
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010631 candidate_len = (int)STRLEN(maybe_unique);
10632 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010633 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010634 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010635
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010636 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010637 if (fnamecmp(maybe_unique, rival) == 0
10638 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010640 }
10641
Bram Moolenaar162bd912010-07-28 22:29:10 +020010642 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010643}
10644
10645/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010646 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010647 * paths are expanded to their equivalent fullpath. This includes the "."
10648 * (relative to current buffer directory) and empty path (relative to current
10649 * directory) notations.
10650 *
10651 * TODO: handle upward search (;) and path limiter (**N) notations by
10652 * expanding each into their equivalent path(s).
10653 */
10654 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010655expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010656{
10657 char_u *path_option = *curbuf->b_p_path == NUL
10658 ? p_path : curbuf->b_p_path;
10659 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010660 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010661 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010662
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010663 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010664 return;
10665
10666 while (*path_option != NUL)
10667 {
10668 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10669
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010670 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010671 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010672 /* Relative to current buffer:
10673 * "/path/file" + "." -> "/path/"
10674 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010675 if (curbuf->b_ffname == NULL)
10676 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010677 p = gettail(curbuf->b_ffname);
10678 len = (int)(p - curbuf->b_ffname);
10679 if (len + (int)STRLEN(buf) >= MAXPATHL)
10680 continue;
10681 if (buf[1] == NUL)
10682 buf[len] = NUL;
10683 else
10684 STRMOVE(buf + len, buf + 2);
10685 mch_memmove(buf, curbuf->b_ffname, len);
10686 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010687 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010688 else if (buf[0] == NUL)
10689 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010690 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010691 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010692 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010693 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010694 else if (!mch_isFullName(buf))
10695 {
10696 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010697 len = (int)STRLEN(curdir);
10698 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010699 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010700 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010701 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010702 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010703 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010704 }
10705
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010706 if (ga_grow(gap, 1) == FAIL)
10707 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010708
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010709# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010710 /* Avoid the path ending in a backslash, it fails when a comma is
10711 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010712 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010713 if (buf[len - 1] == '\\')
10714 buf[len - 1] = '/';
10715# endif
10716
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010717 p = vim_strsave(buf);
10718 if (p == NULL)
10719 break;
10720 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010721 }
10722
10723 vim_free(buf);
10724}
10725
10726/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010727 * Returns a pointer to the file or directory name in "fname" that matches the
10728 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010729 *
10730 * path: /foo/bar/baz
10731 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010732 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010733 */
10734 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010735get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010736{
10737 int i;
10738 int maxlen = 0;
10739 char_u **path_part = (char_u **)gap->ga_data;
10740 char_u *cutoff = NULL;
10741
10742 for (i = 0; i < gap->ga_len; i++)
10743 {
10744 int j = 0;
10745
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010746 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010747# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010748 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10749#endif
10750 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010751 j++;
10752 if (j > maxlen)
10753 {
10754 maxlen = j;
10755 cutoff = &fname[j];
10756 }
10757 }
10758
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010759 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010760 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010761 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010762 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010763
10764 return cutoff;
10765}
10766
10767/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010768 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10769 * that they are unique with respect to each other while conserving the part
10770 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010771 */
10772 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010773uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010774{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010775 int i;
10776 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010777 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010778 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010779 char_u *pat;
10780 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010781 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010782 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010783 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010784 char_u **in_curdir = NULL;
10785 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010786
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010787 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010788 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010789
10790 /*
10791 * We need to prepend a '*' at the beginning of file_pattern so that the
10792 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010793 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010794 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010795 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010796 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010797 if (file_pattern == NULL)
10798 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010799 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010800 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010801 STRCAT(file_pattern, pattern);
10802 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10803 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010804 if (pat == NULL)
10805 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010806
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010807 regmatch.rm_ic = TRUE; /* always ignore case */
10808 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10809 vim_free(pat);
10810 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010811 return;
10812
Bram Moolenaar162bd912010-07-28 22:29:10 +020010813 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010814 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010815 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010816 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010817
10818 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010819 if (in_curdir == NULL)
10820 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010821
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010822 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010823 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010824 char_u *path = fnames[i];
10825 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010826 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010827 char_u *pathsep_p;
10828 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010829
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010830 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010831 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010832 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010833 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010834 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010835
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010836 /* Shorten the filename while maintaining its uniqueness */
10837 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010838
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010839 /* Don't assume all files can be reached without path when search
10840 * pattern starts with star star slash, so only remove path_cutoff
10841 * when possible. */
10842 if (pattern[0] == '*' && pattern[1] == '*'
10843 && vim_ispathsep_nocolon(pattern[2])
10844 && path_cutoff != NULL
10845 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10846 && is_unique(path_cutoff, gap, i))
10847 {
10848 sort_again = TRUE;
10849 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10850 }
10851 else
10852 {
10853 /* Here all files can be reached without path, so get shortest
10854 * unique path. We start at the end of the path. */
10855 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010856
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010857 while (find_previous_pathsep(path, &pathsep_p))
10858 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10859 && is_unique(pathsep_p + 1, gap, i)
10860 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10861 {
10862 sort_again = TRUE;
10863 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10864 break;
10865 }
10866 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010867
10868 if (mch_isFullName(path))
10869 {
10870 /*
10871 * Last resort: shorten relative to curdir if possible.
10872 * 'possible' means:
10873 * 1. It is under the current directory.
10874 * 2. The result is actually shorter than the original.
10875 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010876 * Before curdir After
10877 * /foo/bar/file.txt /foo/bar ./file.txt
10878 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10879 * /file.txt / /file.txt
10880 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010881 */
10882 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010883 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010884#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010885 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010886 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010887 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010888 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010889 && !vim_ispathsep(*short_name)
10890#endif
10891 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010892 {
10893 STRCPY(path, ".");
10894 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010895 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010896 }
10897 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010898 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010899 }
10900
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010901 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010902 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010903 {
10904 char_u *rel_path;
10905 char_u *path = in_curdir[i];
10906
10907 if (path == NULL)
10908 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010909
10910 /* If the {filename} is not unique, change it to ./{filename}.
10911 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010912 short_name = shorten_fname(path, curdir);
10913 if (short_name == NULL)
10914 short_name = path;
10915 if (is_unique(short_name, gap, i))
10916 {
10917 STRCPY(fnames[i], short_name);
10918 continue;
10919 }
10920
10921 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10922 if (rel_path == NULL)
10923 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010924 STRCPY(rel_path, ".");
10925 add_pathsep(rel_path);
10926 STRCAT(rel_path, short_name);
10927
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010928 vim_free(fnames[i]);
10929 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010930 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010931 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010932 }
10933
Bram Moolenaar162bd912010-07-28 22:29:10 +020010934theend:
10935 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010936 if (in_curdir != NULL)
10937 {
10938 for (i = 0; i < gap->ga_len; i++)
10939 vim_free(in_curdir[i]);
10940 vim_free(in_curdir);
10941 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010942 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010943 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010944
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010945 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010946 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010947}
10948
10949/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010950 * Calls globpath() with 'path' values for the given pattern and stores the
10951 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010952 * Returns the total number of matches.
10953 */
10954 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010955expand_in_path(
10956 garray_T *gap,
10957 char_u *pattern,
10958 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010959{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010960 char_u *curdir;
10961 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010962 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010963 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010964
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010965 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010966 return 0;
10967 mch_dirname(curdir, MAXPATHL);
10968
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010969 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010970 expand_path_option(curdir, &path_ga);
10971 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010972 if (path_ga.ga_len == 0)
10973 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010974
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010975 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010976 ga_clear_strings(&path_ga);
10977 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010978 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010979
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010980 if (flags & EW_ICASE)
10981 glob_flags |= WILD_ICASE;
10982 if (flags & EW_ADDSLASH)
10983 glob_flags |= WILD_ADD_SLASH;
10984 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010985 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010986
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010987 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010988}
10989#endif
10990
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010991#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10992/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010993 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10994 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010995 */
10996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010997remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010998{
10999 int i;
11000 int j;
11001 char_u **fnames = (char_u **)gap->ga_data;
11002
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011003 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011004 for (i = gap->ga_len - 1; i > 0; --i)
11005 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11006 {
11007 vim_free(fnames[i]);
11008 for (j = i + 1; j < gap->ga_len; ++j)
11009 fnames[j - 1] = fnames[j];
11010 --gap->ga_len;
11011 }
11012}
11013#endif
11014
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011015/*
11016 * Return TRUE if "p" contains what looks like an environment variable.
11017 * Allowing for escaping.
11018 */
11019 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011020has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011021{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011022 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011023 {
11024 if (*p == '\\' && p[1] != NUL)
11025 ++p;
11026 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011027#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011028 "$%"
11029#else
11030 "$"
11031#endif
11032 , *p) != NULL)
11033 return TRUE;
11034 }
11035 return FALSE;
11036}
11037
11038#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011039/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011040 * Return TRUE if "p" contains a special wildcard character, one that Vim
11041 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011042 */
11043 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011044has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011045{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011046 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011047 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011048 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011049 if (*p == '\\' && p[1] != NUL)
11050 ++p;
11051 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11052 return TRUE;
11053 }
11054 return FALSE;
11055}
11056#endif
11057
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058/*
11059 * Generic wildcard expansion code.
11060 *
11061 * Characters in "pat" that should not be expanded must be preceded with a
11062 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11063 *
11064 * Return FAIL when no single file was found. In this case "num_file" is not
11065 * set, and "file" may contain an error message.
11066 * Return OK when some files found. "num_file" is set to the number of
11067 * matches, "file" to the array of matches. Call FreeWild() later.
11068 */
11069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011070gen_expand_wildcards(
11071 int num_pat, /* number of input patterns */
11072 char_u **pat, /* array of input patterns */
11073 int *num_file, /* resulting number of files */
11074 char_u ***file, /* array of resulting files */
11075 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076{
11077 int i;
11078 garray_T ga;
11079 char_u *p;
11080 static int recursive = FALSE;
11081 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011082 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011083#if defined(FEAT_SEARCHPATH)
11084 int did_expand_in_path = FALSE;
11085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086
11087 /*
11088 * expand_env() is called to expand things like "~user". If this fails,
11089 * it calls ExpandOne(), which brings us back here. In this case, always
11090 * call the machine specific expansion function, if possible. Otherwise,
11091 * return FAIL.
11092 */
11093 if (recursive)
11094#ifdef SPECIAL_WILDCHAR
11095 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11096#else
11097 return FAIL;
11098#endif
11099
11100#ifdef SPECIAL_WILDCHAR
11101 /*
11102 * If there are any special wildcard characters which we cannot handle
11103 * here, call machine specific function for all the expansion. This
11104 * avoids starting the shell for each argument separately.
11105 * For `=expr` do use the internal function.
11106 */
11107 for (i = 0; i < num_pat; i++)
11108 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011109 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110# ifdef VIM_BACKTICK
11111 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11112# endif
11113 )
11114 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11115 }
11116#endif
11117
11118 recursive = TRUE;
11119
11120 /*
11121 * The matching file names are stored in a growarray. Init it empty.
11122 */
11123 ga_init2(&ga, (int)sizeof(char_u *), 30);
11124
11125 for (i = 0; i < num_pat; ++i)
11126 {
11127 add_pat = -1;
11128 p = pat[i];
11129
11130#ifdef VIM_BACKTICK
11131 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011134 if (add_pat == -1)
11135 retval = FAIL;
11136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 else
11138#endif
11139 {
11140 /*
11141 * First expand environment variables, "~/" and "~user/".
11142 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011143 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011145 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 if (p == NULL)
11147 p = pat[i];
11148#ifdef UNIX
11149 /*
11150 * On Unix, if expand_env() can't expand an environment
11151 * variable, use the shell to do that. Discard previously
11152 * found file names and start all over again.
11153 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011154 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 {
11156 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011157 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011159 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 recursive = FALSE;
11161 return i;
11162 }
11163#endif
11164 }
11165
11166 /*
11167 * If there are wildcards: Expand file names and add each match to
11168 * the list. If there is no match, and EW_NOTFOUND is given, add
11169 * the pattern.
11170 * If there are no wildcards: Add the file name if it exists or
11171 * when EW_NOTFOUND is given.
11172 */
11173 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011174 {
11175#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011176 if ((flags & EW_PATH)
11177 && !mch_isFullName(p)
11178 && !(p[0] == '.'
11179 && (vim_ispathsep(p[1])
11180 || (p[1] == '.' && vim_ispathsep(p[2]))))
11181 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011182 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011183 /* :find completion where 'path' is used.
11184 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011185 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011186 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011187 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011188 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011189 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011190 else
11191#endif
11192 add_pat = mch_expandpath(&ga, p, flags);
11193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 }
11195
11196 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11197 {
11198 char_u *t = backslash_halve_save(p);
11199
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11201 * "vim c:/" work. */
11202 if (flags & EW_NOTFOUND)
11203 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011204 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 addfile(&ga, t, flags);
11206 vim_free(t);
11207 }
11208
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011209#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011210 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011211 uniquefy_paths(&ga, p);
11212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 if (p != pat[i])
11214 vim_free(p);
11215 }
11216
11217 *num_file = ga.ga_len;
11218 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11219
11220 recursive = FALSE;
11221
Bram Moolenaar336bd622016-01-17 18:23:58 +010011222 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223}
11224
11225# ifdef VIM_BACKTICK
11226
11227/*
11228 * Return TRUE if we can expand this backtick thing here.
11229 */
11230 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011231vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232{
11233 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11234}
11235
11236/*
11237 * Expand an item in `backticks` by executing it as a command.
11238 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011239 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 */
11241 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011242expand_backtick(
11243 garray_T *gap,
11244 char_u *pat,
11245 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246{
11247 char_u *p;
11248 char_u *cmd;
11249 char_u *buffer;
11250 int cnt = 0;
11251 int i;
11252
11253 /* Create the command: lop off the backticks. */
11254 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11255 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011256 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257
11258#ifdef FEAT_EVAL
11259 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011260 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 else
11262#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011263 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011264 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 vim_free(cmd);
11266 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011267 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268
11269 cmd = buffer;
11270 while (*cmd != NUL)
11271 {
11272 cmd = skipwhite(cmd); /* skip over white space */
11273 p = cmd;
11274 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11275 ++p;
11276 /* add an entry if it is not empty */
11277 if (p > cmd)
11278 {
11279 i = *p;
11280 *p = NUL;
11281 addfile(gap, cmd, flags);
11282 *p = i;
11283 ++cnt;
11284 }
11285 cmd = p;
11286 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11287 ++cmd;
11288 }
11289
11290 vim_free(buffer);
11291 return cnt;
11292}
11293# endif /* VIM_BACKTICK */
11294
11295/*
11296 * Add a file to a file list. Accepted flags:
11297 * EW_DIR add directories
11298 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011299 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 * EW_NOTFOUND add even when it doesn't exist
11301 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011302 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 */
11304 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011305addfile(
11306 garray_T *gap,
11307 char_u *f, /* filename */
11308 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309{
11310 char_u *p;
11311 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011312 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011314 /* if the file/dir/link doesn't exist, may not add it */
11315 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011316 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 return;
11318
11319#ifdef FNAME_ILLEGAL
11320 /* if the file/dir contains illegal characters, don't add it */
11321 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11322 return;
11323#endif
11324
11325 isdir = mch_isdir(f);
11326 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11327 return;
11328
Bram Moolenaarb5971142015-03-21 17:32:19 +010011329 /* If the file isn't executable, may not add it. Do accept directories.
11330 * When invoked from expand_shellcmd() do not use $PATH. */
11331 if (!isdir && (flags & EW_EXEC)
11332 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011333 return;
11334
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 /* Make room for another item in the file list. */
11336 if (ga_grow(gap, 1) == FAIL)
11337 return;
11338
11339 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11340 if (p == NULL)
11341 return;
11342
11343 STRCPY(p, f);
11344#ifdef BACKSLASH_IN_FILENAME
11345 slash_adjust(p);
11346#endif
11347 /*
11348 * Append a slash or backslash after directory names if none is present.
11349 */
11350#ifndef DONT_ADD_PATHSEP_TO_DIR
11351 if (isdir && (flags & EW_ADDSLASH))
11352 add_pathsep(p);
11353#endif
11354 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355}
11356#endif /* !NO_EXPANDPATH */
11357
11358#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11359
11360#ifndef SEEK_SET
11361# define SEEK_SET 0
11362#endif
11363#ifndef SEEK_END
11364# define SEEK_END 2
11365#endif
11366
11367/*
11368 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011369 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11370 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 * Returns an allocated string, or NULL for error.
11372 */
11373 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011374get_cmd_output(
11375 char_u *cmd,
11376 char_u *infile, /* optional input file name */
11377 int flags, /* can be SHELL_SILENT */
11378 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379{
11380 char_u *tempname;
11381 char_u *command;
11382 char_u *buffer = NULL;
11383 int len;
11384 int i = 0;
11385 FILE *fd;
11386
11387 if (check_restricted() || check_secure())
11388 return NULL;
11389
11390 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011391 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 {
11393 EMSG(_(e_notmp));
11394 return NULL;
11395 }
11396
11397 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011398 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 if (command == NULL)
11400 goto done;
11401
11402 /*
11403 * Call the shell to execute the command (errors are ignored).
11404 * Don't check timestamps here.
11405 */
11406 ++no_check_timestamps;
11407 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11408 --no_check_timestamps;
11409
11410 vim_free(command);
11411
11412 /*
11413 * read the names from the file into memory
11414 */
11415# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011416 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 fd = mch_fopen((char *)tempname, "r");
11418# else
11419 fd = mch_fopen((char *)tempname, READBIN);
11420# endif
11421
11422 if (fd == NULL)
11423 {
11424 EMSG2(_(e_notopen), tempname);
11425 goto done;
11426 }
11427
11428 fseek(fd, 0L, SEEK_END);
11429 len = ftell(fd); /* get size of temp file */
11430 fseek(fd, 0L, SEEK_SET);
11431
11432 buffer = alloc(len + 1);
11433 if (buffer != NULL)
11434 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11435 fclose(fd);
11436 mch_remove(tempname);
11437 if (buffer == NULL)
11438 goto done;
11439#ifdef VMS
11440 len = i; /* VMS doesn't give us what we asked for... */
11441#endif
11442 if (i != len)
11443 {
11444 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011445 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011447 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011448 {
11449 /* Change NUL into SOH, otherwise the string is truncated. */
11450 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011451 if (buffer[i] == NUL)
11452 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011453
Bram Moolenaar162bd912010-07-28 22:29:10 +020011454 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011455 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011456 else
11457 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458
11459done:
11460 vim_free(tempname);
11461 return buffer;
11462}
11463#endif
11464
11465/*
11466 * Free the list of files returned by expand_wildcards() or other expansion
11467 * functions.
11468 */
11469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011470FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011472 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 while (count--)
11475 vim_free(files[count]);
11476 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477}
11478
11479/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011480 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481 * Don't do this when still processing a command or a mapping.
11482 * Don't do this when inside a ":normal" command.
11483 */
11484 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011485goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
11487 return (p_im && stuff_empty() && typebuf_typed());
11488}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011489
11490/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011491 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011492 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11493 * - Remove any argument. E.g., "csh -f" -> "csh".
11494 * But don't allow a space in the path, so that this works:
11495 * "/usr/bin/csh --rcfile ~/.cshrc"
11496 * But don't do that for Windows, it's common to have a space in the path.
11497 */
11498 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011499get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011500{
11501 char_u *p;
11502
11503#ifdef WIN3264
11504 p = gettail(p_sh);
11505 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11506#else
11507 p = skiptowhite(p_sh);
11508 if (*p == NUL)
11509 {
11510 /* No white space, use the tail. */
11511 p = vim_strsave(gettail(p_sh));
11512 }
11513 else
11514 {
11515 char_u *p1, *p2;
11516
11517 /* Find the last path separator before the space. */
11518 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011519 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011520 if (vim_ispathsep(*p2))
11521 p1 = p2 + 1;
11522 p = vim_strnsave(p1, (int)(p - p1));
11523 }
11524#endif
11525 return p;
11526}