blob: 28e44da1cca5f48682c40f76b2ccdb7ecc050f61 [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 Moolenaar06ae70d2013-06-17 19:26:36 +020023#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020025#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
Bram Moolenaar24305862012-08-15 14:05:05 +020028/* All user names (for ~user completion as done by shell). */
29#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
30static garray_T ga_users;
31#endif
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033/*
34 * Count the size (in window cells) of the indent in the current line.
35 */
36 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010037get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000038{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020039#ifdef FEAT_VARTABS
40 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
41 curbuf->b_p_vts_array, FALSE);
42#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020043 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47/*
48 * Count the size (in window cells) of the indent in line "lnum".
49 */
50 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010051get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020053#ifdef FEAT_VARTABS
54 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
55 curbuf->b_p_vts_array, FALSE);
56#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020057 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000059}
60
61#if defined(FEAT_FOLDING) || defined(PROTO)
62/*
63 * Count the size (in window cells) of the indent in line "lnum" of buffer
64 * "buf".
65 */
66 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010067get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020069#ifdef FEAT_VARTABS
70 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
71 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
72#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000075}
76#endif
77
78/*
79 * count the size (in window cells) of the indent in line "ptr", with
80 * 'tabstop' at "ts"
81 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010083get_indent_str(
84 char_u *ptr,
85 int ts,
86 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087{
88 int count = 0;
89
90 for ( ; *ptr; ++ptr)
91 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020092 if (*ptr == TAB)
93 {
94 if (!list || lcs_tab1) /* count a tab for what it is worth */
95 count += ts - (count % ts);
96 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020097 /* In list mode, when tab is not set, count screen char width
98 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020099 count += ptr2cells(ptr);
100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 else if (*ptr == ' ')
102 ++count; /* count a space for one */
103 else
104 break;
105 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000106 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107}
108
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200109#ifdef FEAT_VARTABS
110/*
111 * Count the size (in window cells) of the indent in line "ptr", using
112 * variable tabstops.
113 * if "list" is TRUE, count only screen size for tabs.
114 */
115 int
116get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
117{
118 int count = 0;
119
120 for ( ; *ptr; ++ptr)
121 {
122 if (*ptr == TAB) /* count a tab for what it is worth */
123 {
124 if (!list || lcs_tab1)
125 count += tabstop_padding(count, ts, vts);
126 else
127 /* In list mode, when tab is not set, count screen char width
128 * for Tab, displays: ^I */
129 count += ptr2cells(ptr);
130 }
131 else if (*ptr == ' ')
132 ++count; /* count a space for one */
133 else
134 break;
135 }
136 return count;
137}
138#endif
139
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140/*
141 * Set the indent of the current line.
142 * Leaves the cursor on the first non-blank in the line.
143 * Caller must take care of undo.
144 * "flags":
145 * SIN_CHANGED: call changed_bytes() if the line was changed.
146 * SIN_INSERT: insert the indent in front of the line.
147 * SIN_UNDO: save line for undo before changing it.
148 * Returns TRUE if the line was changed.
149 */
150 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100151set_indent(
152 int size, /* measured in spaces */
153 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 char_u *p;
156 char_u *newline;
157 char_u *oldline;
158 char_u *s;
159 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000160 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 int line_len;
162 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000163 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200164#ifdef FEAT_VARTABS
165 int ind_col = 0;
166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000168 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000169 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000170 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
172 /*
173 * First check if there is anything to do and compute the number of
174 * characters needed for the indent.
175 */
176 todo = size;
177 ind_len = 0;
178 p = oldline = ml_get_curline();
179
180 /* Calculate the buffer size for the new indent, and check to see if it
181 * isn't already set */
182
Bram Moolenaar5002c292007-07-24 13:26:15 +0000183 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
184 * 'preserveindent' are set count the number of characters at the
185 * beginning of the line to be copied */
186 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {
188 /* If 'preserveindent' is set then reuse as much as possible of
189 * the existing indent structure for the new indent */
190 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
191 {
192 ind_done = 0;
193
194 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100195 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
197 if (*p == TAB)
198 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200199#ifdef FEAT_VARTABS
200 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
201 curbuf->b_p_vts_array);
202#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 tab_pad = (int)curbuf->b_p_ts
204 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 /* stop if this tab will overshoot the target */
207 if (todo < tab_pad)
208 break;
209 todo -= tab_pad;
210 ++ind_len;
211 ind_done += tab_pad;
212 }
213 else
214 {
215 --todo;
216 ++ind_len;
217 ++ind_done;
218 }
219 ++p;
220 }
221
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200222#ifdef FEAT_VARTABS
223 /* These diverge from this point. */
224 ind_col = ind_done;
225#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000226 /* Set initial number of whitespace chars to copy if we are
227 * preserving indent but expandtab is set */
228 if (curbuf->b_p_et)
229 orig_char_len = ind_len;
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200232#ifdef FEAT_VARTABS
233 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
234 curbuf->b_p_vts_array);
235#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200237#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000238 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {
240 doit = TRUE;
241 todo -= tab_pad;
242 ++ind_len;
243 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200244#ifdef FEAT_VARTABS
245 ind_col += tab_pad;
246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 }
248 }
249
250 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200251#ifdef FEAT_VARTABS
252 for (;;)
253 {
254 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
255 curbuf->b_p_vts_array);
256 if (todo < tab_pad)
257 break;
258 if (*p != TAB)
259 doit = TRUE;
260 else
261 ++p;
262 todo -= tab_pad;
263 ++ind_len;
264 ind_col += tab_pad;
265 }
266#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 while (todo >= (int)curbuf->b_p_ts)
268 {
269 if (*p != TAB)
270 doit = TRUE;
271 else
272 ++p;
273 todo -= (int)curbuf->b_p_ts;
274 ++ind_len;
275 /* ind_done += (int)curbuf->b_p_ts; */
276 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 }
279 /* count spaces required for indent */
280 while (todo > 0)
281 {
282 if (*p != ' ')
283 doit = TRUE;
284 else
285 ++p;
286 --todo;
287 ++ind_len;
288 /* ++ind_done; */
289 }
290
291 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100292 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 return FALSE;
294
295 /* Allocate memory for the new line. */
296 if (flags & SIN_INSERT)
297 p = oldline;
298 else
299 p = skipwhite(p);
300 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000301
302 /* If 'preserveindent' and 'expandtab' are both set keep the original
303 * characters and allocate accordingly. We will fill the rest with spaces
304 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000305 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000306 {
307 newline = alloc(orig_char_len + size - ind_done + line_len);
308 if (newline == NULL)
309 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000310 todo = size - ind_done;
311 ind_len = orig_char_len + todo; /* Set total length of indent in
312 * characters, which may have been
313 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000314 p = oldline;
315 s = newline;
316 while (orig_char_len > 0)
317 {
318 *s++ = *p++;
319 orig_char_len--;
320 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000321
Bram Moolenaar5002c292007-07-24 13:26:15 +0000322 /* Skip over any additional white space (useful when newindent is less
323 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100324 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000325 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000326
Bram Moolenaar5002c292007-07-24 13:26:15 +0000327 }
328 else
329 {
330 todo = size;
331 newline = alloc(ind_len + line_len);
332 if (newline == NULL)
333 return FALSE;
334 s = newline;
335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
337 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 /* if 'expandtab' isn't set: use TABs */
339 if (!curbuf->b_p_et)
340 {
341 /* If 'preserveindent' is set then reuse as much as possible of
342 * the existing indent structure for the new indent */
343 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
344 {
345 p = oldline;
346 ind_done = 0;
347
Bram Moolenaar1c465442017-03-12 20:10:05 +0100348 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {
350 if (*p == TAB)
351 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200352#ifdef FEAT_VARTABS
353 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
354 curbuf->b_p_vts_array);
355#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 tab_pad = (int)curbuf->b_p_ts
357 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 /* stop if this tab will overshoot the target */
360 if (todo < tab_pad)
361 break;
362 todo -= tab_pad;
363 ind_done += tab_pad;
364 }
365 else
366 {
367 --todo;
368 ++ind_done;
369 }
370 *s++ = *p++;
371 }
372
373 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200374#ifdef FEAT_VARTABS
375 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
376 curbuf->b_p_vts_array);
377#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (todo >= tab_pad)
381 {
382 *s++ = TAB;
383 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200384#ifdef FEAT_VARTABS
385 ind_done += tab_pad;
386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388
389 p = skipwhite(p);
390 }
391
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200392#ifdef FEAT_VARTABS
393 for (;;)
394 {
395 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
396 curbuf->b_p_vts_array);
397 if (todo < tab_pad)
398 break;
399 *s++ = TAB;
400 todo -= tab_pad;
401 ind_done += tab_pad;
402 }
403#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 while (todo >= (int)curbuf->b_p_ts)
405 {
406 *s++ = TAB;
407 todo -= (int)curbuf->b_p_ts;
408 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 }
411 while (todo > 0)
412 {
413 *s++ = ' ';
414 --todo;
415 }
416 mch_memmove(s, p, (size_t)line_len);
417
418 /* Replace the line (unless undo fails). */
419 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
420 {
421 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
422 if (flags & SIN_CHANGED)
423 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200424 /* Correct saved cursor position if it is in this line. */
425 if (saved_cursor.lnum == curwin->w_cursor.lnum)
426 {
427 if (saved_cursor.col >= (colnr_T)(p - oldline))
428 /* cursor was after the indent, adjust for the number of
429 * bytes added/removed */
430 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
431 else if (saved_cursor.col >= (colnr_T)(s - newline))
432 /* cursor was in the indent, and is now after it, put it back
433 * at the start of the indent (replacing spaces with TAB) */
434 saved_cursor.col = (colnr_T)(s - newline);
435 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000436 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 }
438 else
439 vim_free(newline);
440
441 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000442 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443}
444
445/*
446 * Copy the indent from ptr to the current line (and fill to size)
447 * Leaves the cursor on the first non-blank in the line.
448 * Returns TRUE if the line was changed.
449 */
450 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100451copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 char_u *p = NULL;
454 char_u *line = NULL;
455 char_u *s;
456 int todo;
457 int ind_len;
458 int line_len = 0;
459 int tab_pad;
460 int ind_done;
461 int round;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200462#ifdef FEAT_VARTABS
463 int ind_col;
464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466 /* Round 1: compute the number of characters needed for the indent
467 * Round 2: copy the characters. */
468 for (round = 1; round <= 2; ++round)
469 {
470 todo = size;
471 ind_len = 0;
472 ind_done = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200473#ifdef FEAT_VARTABS
474 ind_col = 0;
475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 s = src;
477
478 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100479 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 {
481 if (*s == TAB)
482 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200483#ifdef FEAT_VARTABS
484 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
485 curbuf->b_p_vts_array);
486#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 tab_pad = (int)curbuf->b_p_ts
488 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 /* Stop if this tab will overshoot the target */
491 if (todo < tab_pad)
492 break;
493 todo -= tab_pad;
494 ind_done += tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200495#ifdef FEAT_VARTABS
496 ind_col += tab_pad;
497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 }
499 else
500 {
501 --todo;
502 ++ind_done;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200503#ifdef FEAT_VARTABS
504 ++ind_col;
505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 }
507 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000508 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 *p++ = *s;
510 ++s;
511 }
512
513 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200514#ifdef FEAT_VARTABS
515 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
516 curbuf->b_p_vts_array);
517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200519#endif
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200520 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 {
522 todo -= tab_pad;
523 ++ind_len;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200524#ifdef FEAT_VARTABS
525 ind_col += tab_pad;
526#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000527 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 *p++ = TAB;
529 }
530
531 /* Add tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200532 if (!curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200534#ifdef FEAT_VARTABS
535 for (;;)
536 {
537 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
538 curbuf->b_p_vts_array);
539 if (todo < tab_pad)
540 break;
541 todo -= tab_pad;
542 ++ind_len;
543 ind_col += tab_pad;
544 if (p != NULL)
545 *p++ = TAB;
546 }
547#else
548 while (todo >= (int)curbuf->b_p_ts)
549 {
550 todo -= (int)curbuf->b_p_ts;
551 ++ind_len;
552 if (p != NULL)
553 *p++ = TAB;
554 }
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 }
557
558 /* Count/add spaces required for indent */
559 while (todo > 0)
560 {
561 --todo;
562 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000563 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 *p++ = ' ';
565 }
566
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000567 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 /* Allocate memory for the result: the copied indent, new indent
570 * and the rest of the line. */
571 line_len = (int)STRLEN(ml_get_curline()) + 1;
572 line = alloc(ind_len + line_len);
573 if (line == NULL)
574 return FALSE;
575 p = line;
576 }
577 }
578
579 /* Append the original line */
580 mch_memmove(p, ml_get_curline(), (size_t)line_len);
581
582 /* Replace the line */
583 ml_replace(curwin->w_cursor.lnum, line, FALSE);
584
585 /* Put the cursor after the indent. */
586 curwin->w_cursor.col = ind_len;
587 return TRUE;
588}
589
590/*
591 * Return the indent of the current line after a number. Return -1 if no
592 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000593 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 */
595 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100596get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 colnr_T col;
599 pos_T pos;
600
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200601 regmatch_T regmatch;
602 int lead_len = 0; /* length of comment leader */
603
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (lnum > curbuf->b_ml.ml_line_count)
605 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000606 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200607
608#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200609 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
610 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200611 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000612#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200613 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
614 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200615 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200616 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200617
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200618 /* vim_regexec() expects a pointer to a line. This lets us
619 * start matching for the flp beyond any comment leader... */
620 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200621 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200622 pos.lnum = lnum;
623 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200624#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200625 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200626#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200627 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200628 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200629 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000630
631 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 getvcol(curwin, &pos, &col, NULL, NULL);
634 return (int)col;
635}
636
Bram Moolenaar597a4222014-06-25 14:39:50 +0200637#if defined(FEAT_LINEBREAK) || defined(PROTO)
638/*
639 * Return appropriate space number for breakindent, taking influencing
640 * parameters into account. Window must be specified, since it is not
641 * necessarily always the current one.
642 */
643 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644get_breakindent_win(
645 win_T *wp,
646 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200647{
648 static int prev_indent = 0; /* cached indent value */
649 static long prev_ts = 0L; /* cached tabstop value */
650 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100651 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200652#ifdef FEAT_VARTABS
653 static int *prev_vts = NULL; /* cached vartabs values */
654#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200655 int bri = 0;
656 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200657 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200658 - ((wp->w_p_nu || wp->w_p_rnu)
659 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
660 ? number_width(wp) + 1 : 0);
661
662 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200663 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200664 || prev_tick != CHANGEDTICK(wp->w_buffer)
665#ifdef FEAT_VARTABS
666 || prev_vts != wp->w_buffer->b_p_vts_array
667#endif
668 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200669 {
670 prev_line = line;
671 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100672 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200673#ifdef FEAT_VARTABS
674 prev_vts = wp->w_buffer->b_p_vts_array;
675 prev_indent = get_indent_str_vtab(line,
676 (int)wp->w_buffer->b_p_ts,
677 wp->w_buffer->b_p_vts_array, wp->w_p_list);
678#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200679 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200680 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200681#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200682 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200683 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200684
685 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200686 if (wp->w_p_brisbr)
687 bri -= vim_strsize(p_sbr);
688
689 /* Add offset for number column, if 'n' is in 'cpoptions' */
690 bri += win_col_off2(wp);
691
692 /* never indent past left window margin */
693 if (bri < 0)
694 bri = 0;
695 /* always leave at least bri_min characters on the left,
696 * if text width is sufficient */
697 else if (bri > eff_wwidth - wp->w_p_brimin)
698 bri = (eff_wwidth - wp->w_p_brimin < 0)
699 ? 0 : eff_wwidth - wp->w_p_brimin;
700
701 return bri;
702}
703#endif
704
705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
707
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100708static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
710/*
711 * Return TRUE if the string "line" starts with a word from 'cinwords'.
712 */
713 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100714cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 char_u *cinw;
717 char_u *cinw_buf;
718 int cinw_len;
719 int retval = FALSE;
720 int len;
721
722 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
723 cinw_buf = alloc((unsigned)cinw_len);
724 if (cinw_buf != NULL)
725 {
726 line = skipwhite(line);
727 for (cinw = curbuf->b_p_cinw; *cinw; )
728 {
729 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
730 if (STRNCMP(line, cinw_buf, len) == 0
731 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
732 {
733 retval = TRUE;
734 break;
735 }
736 }
737 vim_free(cinw_buf);
738 }
739 return retval;
740}
741#endif
742
743/*
744 * open_line: Add a new line below or above the current line.
745 *
746 * For VREPLACE mode, we only add a new line when we get to the end of the
747 * file, otherwise we just start replacing the next line.
748 *
749 * Caller must take care of undo. Since VREPLACE may affect any number of
750 * lines however, it may call u_save_cursor() again when starting to change a
751 * new line.
752 * "flags": OPENLINE_DELSPACES delete spaces after cursor
753 * OPENLINE_DO_COM format comments
754 * OPENLINE_KEEPTRAIL keep trailing spaces
755 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200756 * OPENLINE_COM_LIST format comments with list or 2nd line indent
757 *
758 * "second_line_indent": indent for after ^^D in Insert mode or if flag
759 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200761 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
763 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100764open_line(
765 int dir, /* FORWARD or BACKWARD */
766 int flags,
767 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
769 char_u *saved_line; /* copy of the original line */
770 char_u *next_line = NULL; /* copy of the next line */
771 char_u *p_extra = NULL; /* what goes to next line */
772 int less_cols = 0; /* less columns for mark in new line */
773 int less_cols_off = 0; /* columns to skip for mark adjust */
774 pos_T old_cursor; /* old cursor position */
775 int newcol = 0; /* new cursor column */
776 int newindent = 0; /* auto-indent of the new line */
777 int n;
778 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200779 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#ifdef FEAT_COMMENTS
781 int extra_len = 0; /* length of p_extra string */
782 int lead_len; /* length of comment leader */
783 char_u *lead_flags; /* position in 'comments' for comment leader */
784 char_u *leader = NULL; /* copy of comment leader */
785#endif
786 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 int saved_char = NUL; /* init for GCC */
789#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
790 pos_T *pos;
791#endif
792#ifdef FEAT_SMARTINDENT
793 int do_si = (!p_paste && curbuf->b_p_si
794# ifdef FEAT_CINDENT
795 && !curbuf->b_p_cin
796# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200797# ifdef FEAT_EVAL
798 && *curbuf->b_p_inde == NUL
799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 );
801 int no_si = FALSE; /* reset did_si afterwards */
802 int first_char = NUL; /* init for GCC */
803#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200804#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 int vreplace_mode;
806#endif
807 int did_append; /* appended a new line */
808 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
809
810 /*
811 * make a copy of the current line so we can mess with it
812 */
813 saved_line = vim_strsave(ml_get_curline());
814 if (saved_line == NULL) /* out of memory! */
815 return FALSE;
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (State & VREPLACE_FLAG)
818 {
819 /*
820 * With VREPLACE we make a copy of the next line, which we will be
821 * starting to replace. First make the new line empty and let vim play
822 * with the indenting and comment leader to its heart's content. Then
823 * we grab what it ended up putting on the new line, put back the
824 * original line, and call ins_char() to put each new character onto
825 * the line, replacing what was there before and pushing the right
826 * stuff onto the replace stack. -- webb.
827 */
828 if (curwin->w_cursor.lnum < orig_line_count)
829 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
830 else
831 next_line = vim_strsave((char_u *)"");
832 if (next_line == NULL) /* out of memory! */
833 goto theend;
834
835 /*
836 * In VREPLACE mode, a NL replaces the rest of the line, and starts
837 * replacing the next line, so push all of the characters left on the
838 * line onto the replace stack. We'll push any other characters that
839 * might be replaced at the start of the next line (due to autoindent
840 * etc) a bit later.
841 */
842 replace_push(NUL); /* Call twice because BS over NL expects it */
843 replace_push(NUL);
844 p = saved_line + curwin->w_cursor.col;
845 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000846 {
847#ifdef FEAT_MBYTE
848 if (has_mbyte)
849 p += replace_push_mb(p);
850 else
851#endif
852 replace_push(*p++);
853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 saved_line[curwin->w_cursor.col] = NUL;
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200857 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859 p_extra = saved_line + curwin->w_cursor.col;
860#ifdef FEAT_SMARTINDENT
861 if (do_si) /* need first char after new line break */
862 {
863 p = skipwhite(p_extra);
864 first_char = *p;
865 }
866#endif
867#ifdef FEAT_COMMENTS
868 extra_len = (int)STRLEN(p_extra);
869#endif
870 saved_char = *p_extra;
871 *p_extra = NUL;
872 }
873
874 u_clearline(); /* cannot do "U" command when adding lines */
875#ifdef FEAT_SMARTINDENT
876 did_si = FALSE;
877#endif
878 ai_col = 0;
879
880 /*
881 * If we just did an auto-indent, then we didn't type anything on
882 * the prior line, and it should be truncated. Do this even if 'ai' is not
883 * set because automatically inserting a comment leader also sets did_ai.
884 */
885 if (dir == FORWARD && did_ai)
886 trunc_line = TRUE;
887
888 /*
889 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
890 * indent to use for the new line.
891 */
892 if (curbuf->b_p_ai
893#ifdef FEAT_SMARTINDENT
894 || do_si
895#endif
896 )
897 {
898 /*
899 * count white space on current line
900 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200901#ifdef FEAT_VARTABS
902 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
903 curbuf->b_p_vts_array, FALSE);
904#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200905 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200906#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200907 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
908 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
910#ifdef FEAT_SMARTINDENT
911 /*
912 * Do smart indenting.
913 * In insert/replace mode (only when dir == FORWARD)
914 * we may move some text to the next line. If it starts with '{'
915 * don't add an indent. Fixes inserting a NL before '{' in line
916 * "if (condition) {"
917 */
918 if (!trunc_line && do_si && *saved_line != NUL
919 && (p_extra == NULL || first_char != '{'))
920 {
921 char_u *ptr;
922 char_u last_char;
923
924 old_cursor = curwin->w_cursor;
925 ptr = saved_line;
926# ifdef FEAT_COMMENTS
927 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200928 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 else
930 lead_len = 0;
931# endif
932 if (dir == FORWARD)
933 {
934 /*
935 * Skip preprocessor directives, unless they are
936 * recognised as comments.
937 */
938 if (
939# ifdef FEAT_COMMENTS
940 lead_len == 0 &&
941# endif
942 ptr[0] == '#')
943 {
944 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
945 ptr = ml_get(--curwin->w_cursor.lnum);
946 newindent = get_indent();
947 }
948# ifdef FEAT_COMMENTS
949 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200950 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else
952 lead_len = 0;
953 if (lead_len > 0)
954 {
955 /*
956 * This case gets the following right:
957 * \*
958 * * A comment (read '\' as '/').
959 * *\
960 * #define IN_THE_WAY
961 * This should line up here;
962 */
963 p = skipwhite(ptr);
964 if (p[0] == '/' && p[1] == '*')
965 p++;
966 if (p[0] == '*')
967 {
968 for (p++; *p; p++)
969 {
970 if (p[0] == '/' && p[-1] == '*')
971 {
972 /*
973 * End of C comment, indent should line up
974 * with the line containing the start of
975 * the comment
976 */
977 curwin->w_cursor.col = (colnr_T)(p - ptr);
978 if ((pos = findmatch(NULL, NUL)) != NULL)
979 {
980 curwin->w_cursor.lnum = pos->lnum;
981 newindent = get_indent();
982 }
983 }
984 }
985 }
986 }
987 else /* Not a comment line */
988# endif
989 {
990 /* Find last non-blank in line */
991 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100992 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 --p;
994 last_char = *p;
995
996 /*
997 * find the character just before the '{' or ';'
998 */
999 if (last_char == '{' || last_char == ';')
1000 {
1001 if (p > ptr)
1002 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001003 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 --p;
1005 }
1006 /*
1007 * Try to catch lines that are split over multiple
1008 * lines. eg:
1009 * if (condition &&
1010 * condition) {
1011 * Should line up here!
1012 * }
1013 */
1014 if (*p == ')')
1015 {
1016 curwin->w_cursor.col = (colnr_T)(p - ptr);
1017 if ((pos = findmatch(NULL, '(')) != NULL)
1018 {
1019 curwin->w_cursor.lnum = pos->lnum;
1020 newindent = get_indent();
1021 ptr = ml_get_curline();
1022 }
1023 }
1024 /*
1025 * If last character is '{' do indent, without
1026 * checking for "if" and the like.
1027 */
1028 if (last_char == '{')
1029 {
1030 did_si = TRUE; /* do indent */
1031 no_si = TRUE; /* don't delete it when '{' typed */
1032 }
1033 /*
1034 * Look for "if" and the like, use 'cinwords'.
1035 * Don't do this if the previous line ended in ';' or
1036 * '}'.
1037 */
1038 else if (last_char != ';' && last_char != '}'
1039 && cin_is_cinword(ptr))
1040 did_si = TRUE;
1041 }
1042 }
1043 else /* dir == BACKWARD */
1044 {
1045 /*
1046 * Skip preprocessor directives, unless they are
1047 * recognised as comments.
1048 */
1049 if (
1050# ifdef FEAT_COMMENTS
1051 lead_len == 0 &&
1052# endif
1053 ptr[0] == '#')
1054 {
1055 int was_backslashed = FALSE;
1056
1057 while ((ptr[0] == '#' || was_backslashed) &&
1058 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1059 {
1060 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1061 was_backslashed = TRUE;
1062 else
1063 was_backslashed = FALSE;
1064 ptr = ml_get(++curwin->w_cursor.lnum);
1065 }
1066 if (was_backslashed)
1067 newindent = 0; /* Got to end of file */
1068 else
1069 newindent = get_indent();
1070 }
1071 p = skipwhite(ptr);
1072 if (*p == '}') /* if line starts with '}': do indent */
1073 did_si = TRUE;
1074 else /* can delete indent when '{' typed */
1075 can_si_back = TRUE;
1076 }
1077 curwin->w_cursor = old_cursor;
1078 }
1079 if (do_si)
1080 can_si = TRUE;
1081#endif /* FEAT_SMARTINDENT */
1082
1083 did_ai = TRUE;
1084 }
1085
1086#ifdef FEAT_COMMENTS
1087 /*
1088 * Find out if the current line starts with a comment leader.
1089 * This may then be inserted in front of the new line.
1090 */
1091 end_comment_pending = NUL;
1092 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001093 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 else
1095 lead_len = 0;
1096 if (lead_len > 0)
1097 {
1098 char_u *lead_repl = NULL; /* replaces comment leader */
1099 int lead_repl_len = 0; /* length of *lead_repl */
1100 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1101 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1102 char_u *comment_end = NULL; /* where lead_end has been found */
1103 int extra_space = FALSE; /* append extra space */
1104 int current_flag;
1105 int require_blank = FALSE; /* requires blank after middle */
1106 char_u *p2;
1107
1108 /*
1109 * If the comment leader has the start, middle or end flag, it may not
1110 * be used or may be replaced with the middle leader.
1111 */
1112 for (p = lead_flags; *p && *p != ':'; ++p)
1113 {
1114 if (*p == COM_BLANK)
1115 {
1116 require_blank = TRUE;
1117 continue;
1118 }
1119 if (*p == COM_START || *p == COM_MIDDLE)
1120 {
1121 current_flag = *p;
1122 if (*p == COM_START)
1123 {
1124 /*
1125 * Doing "O" on a start of comment does not insert leader.
1126 */
1127 if (dir == BACKWARD)
1128 {
1129 lead_len = 0;
1130 break;
1131 }
1132
1133 /* find start of middle part */
1134 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1135 require_blank = FALSE;
1136 }
1137
1138 /*
1139 * Isolate the strings of the middle and end leader.
1140 */
1141 while (*p && p[-1] != ':') /* find end of middle flags */
1142 {
1143 if (*p == COM_BLANK)
1144 require_blank = TRUE;
1145 ++p;
1146 }
1147 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1148
1149 while (*p && p[-1] != ':') /* find end of end flags */
1150 {
1151 /* Check whether we allow automatic ending of comments */
1152 if (*p == COM_AUTO_END)
1153 end_comment_pending = -1; /* means we want to set it */
1154 ++p;
1155 }
1156 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1157
1158 if (end_comment_pending == -1) /* we can set it now */
1159 end_comment_pending = lead_end[n - 1];
1160
1161 /*
1162 * If the end of the comment is in the same line, don't use
1163 * the comment leader.
1164 */
1165 if (dir == FORWARD)
1166 {
1167 for (p = saved_line + lead_len; *p; ++p)
1168 if (STRNCMP(p, lead_end, n) == 0)
1169 {
1170 comment_end = p;
1171 lead_len = 0;
1172 break;
1173 }
1174 }
1175
1176 /*
1177 * Doing "o" on a start of comment inserts the middle leader.
1178 */
1179 if (lead_len > 0)
1180 {
1181 if (current_flag == COM_START)
1182 {
1183 lead_repl = lead_middle;
1184 lead_repl_len = (int)STRLEN(lead_middle);
1185 }
1186
1187 /*
1188 * If we have hit RETURN immediately after the start
1189 * comment leader, then put a space after the middle
1190 * comment leader on the next line.
1191 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001192 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && ((p_extra != NULL
1194 && (int)curwin->w_cursor.col == lead_len)
1195 || (p_extra == NULL
1196 && saved_line[lead_len] == NUL)
1197 || require_blank))
1198 extra_space = TRUE;
1199 }
1200 break;
1201 }
1202 if (*p == COM_END)
1203 {
1204 /*
1205 * Doing "o" on the end of a comment does not insert leader.
1206 * Remember where the end is, might want to use it to find the
1207 * start (for C-comments).
1208 */
1209 if (dir == FORWARD)
1210 {
1211 comment_end = skipwhite(saved_line);
1212 lead_len = 0;
1213 break;
1214 }
1215
1216 /*
1217 * Doing "O" on the end of a comment inserts the middle leader.
1218 * Find the string for the middle leader, searching backwards.
1219 */
1220 while (p > curbuf->b_p_com && *p != ',')
1221 --p;
1222 for (lead_repl = p; lead_repl > curbuf->b_p_com
1223 && lead_repl[-1] != ':'; --lead_repl)
1224 ;
1225 lead_repl_len = (int)(p - lead_repl);
1226
1227 /* We can probably always add an extra space when doing "O" on
1228 * the comment-end */
1229 extra_space = TRUE;
1230
1231 /* Check whether we allow automatic ending of comments */
1232 for (p2 = p; *p2 && *p2 != ':'; p2++)
1233 {
1234 if (*p2 == COM_AUTO_END)
1235 end_comment_pending = -1; /* means we want to set it */
1236 }
1237 if (end_comment_pending == -1)
1238 {
1239 /* Find last character in end-comment string */
1240 while (*p2 && *p2 != ',')
1241 p2++;
1242 end_comment_pending = p2[-1];
1243 }
1244 break;
1245 }
1246 if (*p == COM_FIRST)
1247 {
1248 /*
1249 * Comment leader for first line only: Don't repeat leader
1250 * when using "O", blank out leader when using "o".
1251 */
1252 if (dir == BACKWARD)
1253 lead_len = 0;
1254 else
1255 {
1256 lead_repl = (char_u *)"";
1257 lead_repl_len = 0;
1258 }
1259 break;
1260 }
1261 }
1262 if (lead_len)
1263 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001264 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001265 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001266 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 allocated = leader; /* remember to free it later */
1268
1269 if (leader == NULL)
1270 lead_len = 0;
1271 else
1272 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001273 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275 /*
1276 * Replace leader with lead_repl, right or left adjusted
1277 */
1278 if (lead_repl != NULL)
1279 {
1280 int c = 0;
1281 int off = 0;
1282
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001283 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {
1285 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001286 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else if (VIM_ISDIGIT(*p) || *p == '-')
1288 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001289 else
1290 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 if (c == COM_RIGHT) /* right adjusted leader */
1293 {
1294 /* find last non-white in the leader to line up with */
1295 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001296 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001299
1300#ifdef FEAT_MBYTE
1301 /* Compute the length of the replaced characters in
1302 * screen characters, not bytes. */
1303 {
1304 int repl_size = vim_strnsize(lead_repl,
1305 lead_repl_len);
1306 int old_size = 0;
1307 char_u *endp = p;
1308 int l;
1309
1310 while (old_size < repl_size && p > leader)
1311 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001312 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001313 old_size += ptr2cells(p);
1314 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001315 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001316 if (l != 0)
1317 mch_memmove(endp + l, endp,
1318 (size_t)((leader + lead_len) - endp));
1319 lead_len += l;
1320 }
1321#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (p < leader + lead_repl_len)
1323 p = leader;
1324 else
1325 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1328 if (p + lead_repl_len > leader + lead_len)
1329 p[lead_repl_len] = NUL;
1330
1331 /* blank-out any other chars from the old leader. */
1332 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001333 {
1334#ifdef FEAT_MBYTE
1335 int l = mb_head_off(leader, p);
1336
1337 if (l > 1)
1338 {
1339 p -= l;
1340 if (ptr2cells(p) > 1)
1341 {
1342 p[1] = ' ';
1343 --l;
1344 }
1345 mch_memmove(p + 1, p + l + 1,
1346 (size_t)((leader + lead_len) - (p + l + 1)));
1347 lead_len -= l;
1348 *p = ' ';
1349 }
1350 else
1351#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001352 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 else /* left adjusted leader */
1357 {
1358 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001359#ifdef FEAT_MBYTE
1360 /* Compute the length of the replaced characters in
1361 * screen characters, not bytes. Move the part that is
1362 * not to be overwritten. */
1363 {
1364 int repl_size = vim_strnsize(lead_repl,
1365 lead_repl_len);
1366 int i;
1367 int l;
1368
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001369 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001370 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001371 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001372 if (vim_strnsize(p, i + l) > repl_size)
1373 break;
1374 }
1375 if (i != lead_repl_len)
1376 {
1377 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001378 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001379 lead_len += lead_repl_len - i;
1380 }
1381 }
1382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1384
1385 /* Replace any remaining non-white chars in the old
1386 * leader by spaces. Keep Tabs, the indent must
1387 * remain the same. */
1388 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001389 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
1391 /* Don't put a space before a TAB. */
1392 if (p + 1 < leader + lead_len && p[1] == TAB)
1393 {
1394 --lead_len;
1395 mch_memmove(p, p + 1,
1396 (leader + lead_len) - p);
1397 }
1398 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001399 {
1400#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001401 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001402
1403 if (l > 1)
1404 {
1405 if (ptr2cells(p) > 1)
1406 {
1407 /* Replace a double-wide char with
1408 * two spaces */
1409 --l;
1410 *p++ = ' ';
1411 }
1412 mch_memmove(p + 1, p + l,
1413 (leader + lead_len) - p);
1414 lead_len -= l - 1;
1415 }
1416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420 *p = NUL;
1421 }
1422
1423 /* Recompute the indent, it may have changed. */
1424 if (curbuf->b_p_ai
1425#ifdef FEAT_SMARTINDENT
1426 || do_si
1427#endif
1428 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001429#ifdef FEAT_VARTABS
1430 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1431 curbuf->b_p_vts_array, FALSE);
1432#else
1433 newindent = get_indent_str(leader,
1434 (int)curbuf->b_p_ts, FALSE);
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 /* Add the indent offset */
1438 if (newindent + off < 0)
1439 {
1440 off = -newindent;
1441 newindent = 0;
1442 }
1443 else
1444 newindent += off;
1445
1446 /* Correct trailing spaces for the shift, so that
1447 * alignment remains equal. */
1448 while (off > 0 && lead_len > 0
1449 && leader[lead_len - 1] == ' ')
1450 {
1451 /* Don't do it when there is a tab before the space */
1452 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1453 break;
1454 --lead_len;
1455 --off;
1456 }
1457
1458 /* If the leader ends in white space, don't add an
1459 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001460 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 extra_space = FALSE;
1462 leader[lead_len] = NUL;
1463 }
1464
1465 if (extra_space)
1466 {
1467 leader[lead_len++] = ' ';
1468 leader[lead_len] = NUL;
1469 }
1470
1471 newcol = lead_len;
1472
1473 /*
1474 * if a new indent will be set below, remove the indent that
1475 * is in the comment leader
1476 */
1477 if (newindent
1478#ifdef FEAT_SMARTINDENT
1479 || did_si
1480#endif
1481 )
1482 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001483 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 --lead_len;
1486 --newcol;
1487 ++leader;
1488 }
1489 }
1490
1491 }
1492#ifdef FEAT_SMARTINDENT
1493 did_si = can_si = FALSE;
1494#endif
1495 }
1496 else if (comment_end != NULL)
1497 {
1498 /*
1499 * We have finished a comment, so we don't use the leader.
1500 * If this was a C-comment and 'ai' or 'si' is set do a normal
1501 * indent to align with the line containing the start of the
1502 * comment.
1503 */
1504 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1505 (curbuf->b_p_ai
1506#ifdef FEAT_SMARTINDENT
1507 || do_si
1508#endif
1509 ))
1510 {
1511 old_cursor = curwin->w_cursor;
1512 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1513 if ((pos = findmatch(NULL, NUL)) != NULL)
1514 {
1515 curwin->w_cursor.lnum = pos->lnum;
1516 newindent = get_indent();
1517 }
1518 curwin->w_cursor = old_cursor;
1519 }
1520 }
1521 }
1522#endif
1523
1524 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1525 if (p_extra != NULL)
1526 {
1527 *p_extra = saved_char; /* restore char that NUL replaced */
1528
1529 /*
1530 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1531 * non-blank.
1532 *
1533 * When in REPLACE mode, put the deleted blanks on the replace stack,
1534 * preceded by a NUL, so they can be put back when a BS is entered.
1535 */
1536 if (REPLACE_NORMAL(State))
1537 replace_push(NUL); /* end of extra blanks */
1538 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1539 {
1540 while ((*p_extra == ' ' || *p_extra == '\t')
1541#ifdef FEAT_MBYTE
1542 && (!enc_utf8
1543 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1544#endif
1545 )
1546 {
1547 if (REPLACE_NORMAL(State))
1548 replace_push(*p_extra);
1549 ++p_extra;
1550 ++less_cols_off;
1551 }
1552 }
1553 if (*p_extra != NUL)
1554 did_ai = FALSE; /* append some text, don't truncate now */
1555
1556 /* columns for marks adjusted for removed columns */
1557 less_cols = (int)(p_extra - saved_line);
1558 }
1559
1560 if (p_extra == NULL)
1561 p_extra = (char_u *)""; /* append empty line */
1562
1563#ifdef FEAT_COMMENTS
1564 /* concatenate leader and p_extra, if there is a leader */
1565 if (lead_len)
1566 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001567 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1568 {
1569 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001570 int padding = second_line_indent
1571 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001572
1573 /* Here whitespace is inserted after the comment char.
1574 * Below, set_indent(newindent, SIN_INSERT) will insert the
1575 * whitespace needed before the comment char. */
1576 for (i = 0; i < padding; i++)
1577 {
1578 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001579 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001580 newcol++;
1581 }
1582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 STRCAT(leader, p_extra);
1584 p_extra = leader;
1585 did_ai = TRUE; /* So truncating blanks works with comments */
1586 less_cols -= lead_len;
1587 }
1588 else
1589 end_comment_pending = NUL; /* turns out there was no leader */
1590#endif
1591
1592 old_cursor = curwin->w_cursor;
1593 if (dir == BACKWARD)
1594 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1598 == FAIL)
1599 goto theend;
1600 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001601 * with markers.
1602 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001603 * be marks there. But still needed in diff mode. */
1604 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1605#ifdef FEAT_DIFF
1606 || curwin->w_p_diff
1607#endif
1608 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001609 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 did_append = TRUE;
1611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 else
1613 {
1614 /*
1615 * In VREPLACE mode we are starting to replace the next line.
1616 */
1617 curwin->w_cursor.lnum++;
1618 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1619 {
1620 /* In case we NL to a new line, BS to the previous one, and NL
1621 * again, we don't want to save the new line for undo twice.
1622 */
1623 (void)u_save_cursor(); /* errors are ignored! */
1624 vr_lines_changed++;
1625 }
1626 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1627 changed_bytes(curwin->w_cursor.lnum, 0);
1628 curwin->w_cursor.lnum--;
1629 did_append = FALSE;
1630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 if (newindent
1633#ifdef FEAT_SMARTINDENT
1634 || did_si
1635#endif
1636 )
1637 {
1638 ++curwin->w_cursor.lnum;
1639#ifdef FEAT_SMARTINDENT
1640 if (did_si)
1641 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001642 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001645 newindent -= newindent % sw;
1646 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 }
1648#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001649 /* Copy the indent */
1650 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
1652 (void)copy_indent(newindent, saved_line);
1653
1654 /*
1655 * Set the 'preserveindent' option so that any further screwing
1656 * with the line doesn't entirely destroy our efforts to preserve
1657 * it. It gets restored at the function end.
1658 */
1659 curbuf->b_p_pi = TRUE;
1660 }
1661 else
1662 (void)set_indent(newindent, SIN_INSERT);
1663 less_cols -= curwin->w_cursor.col;
1664
1665 ai_col = curwin->w_cursor.col;
1666
1667 /*
1668 * In REPLACE mode, for each character in the new indent, there must
1669 * be a NUL on the replace stack, for when it is deleted with BS
1670 */
1671 if (REPLACE_NORMAL(State))
1672 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1673 replace_push(NUL);
1674 newcol += curwin->w_cursor.col;
1675#ifdef FEAT_SMARTINDENT
1676 if (no_si)
1677 did_si = FALSE;
1678#endif
1679 }
1680
1681#ifdef FEAT_COMMENTS
1682 /*
1683 * In REPLACE mode, for each character in the extra leader, there must be
1684 * a NUL on the replace stack, for when it is deleted with BS.
1685 */
1686 if (REPLACE_NORMAL(State))
1687 while (lead_len-- > 0)
1688 replace_push(NUL);
1689#endif
1690
1691 curwin->w_cursor = old_cursor;
1692
1693 if (dir == FORWARD)
1694 {
1695 if (trunc_line || (State & INSERT))
1696 {
1697 /* truncate current line at cursor */
1698 saved_line[curwin->w_cursor.col] = NUL;
1699 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1700 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1701 truncate_spaces(saved_line);
1702 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1703 saved_line = NULL;
1704 if (did_append)
1705 {
1706 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1707 curwin->w_cursor.lnum + 1, 1L);
1708 did_append = FALSE;
1709
1710 /* Move marks after the line break to the new line. */
1711 if (flags & OPENLINE_MARKFIX)
1712 mark_col_adjust(curwin->w_cursor.lnum,
1713 curwin->w_cursor.col + less_cols_off,
1714 1L, (long)-less_cols);
1715 }
1716 else
1717 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1718 }
1719
1720 /*
1721 * Put the cursor on the new line. Careful: the scrollup() above may
1722 * have moved w_cursor, we must use old_cursor.
1723 */
1724 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1725 }
1726 if (did_append)
1727 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1728
1729 curwin->w_cursor.col = newcol;
1730#ifdef FEAT_VIRTUALEDIT
1731 curwin->w_cursor.coladd = 0;
1732#endif
1733
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001734#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 /*
1736 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1737 * fixthisline() from doing it (via change_indent()) by telling it we're in
1738 * normal INSERT mode.
1739 */
1740 if (State & VREPLACE_FLAG)
1741 {
1742 vreplace_mode = State; /* So we know to put things right later */
1743 State = INSERT;
1744 }
1745 else
1746 vreplace_mode = 0;
1747#endif
1748#ifdef FEAT_LISP
1749 /*
1750 * May do lisp indenting.
1751 */
1752 if (!p_paste
1753# ifdef FEAT_COMMENTS
1754 && leader == NULL
1755# endif
1756 && curbuf->b_p_lisp
1757 && curbuf->b_p_ai)
1758 {
1759 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001760 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762#endif
1763#ifdef FEAT_CINDENT
1764 /*
1765 * May do indenting after opening a new line.
1766 */
1767 if (!p_paste
1768 && (curbuf->b_p_cin
1769# ifdef FEAT_EVAL
1770 || *curbuf->b_p_inde != NUL
1771# endif
1772 )
1773 && in_cinkeys(dir == FORWARD
1774 ? KEY_OPEN_FORW
1775 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1776 {
1777 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001778 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 }
1780#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001781#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (vreplace_mode != 0)
1783 State = vreplace_mode;
1784#endif
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
1787 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1788 * original line, and inserts the new stuff char by char, pushing old stuff
1789 * onto the replace stack (via ins_char()).
1790 */
1791 if (State & VREPLACE_FLAG)
1792 {
1793 /* Put new line in p_extra */
1794 p_extra = vim_strsave(ml_get_curline());
1795 if (p_extra == NULL)
1796 goto theend;
1797
1798 /* Put back original line */
1799 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1800
1801 /* Insert new stuff into line again */
1802 curwin->w_cursor.col = 0;
1803#ifdef FEAT_VIRTUALEDIT
1804 curwin->w_cursor.coladd = 0;
1805#endif
1806 ins_bytes(p_extra); /* will call changed_bytes() */
1807 vim_free(p_extra);
1808 next_line = NULL;
1809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001811 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812theend:
1813 curbuf->b_p_pi = saved_pi;
1814 vim_free(saved_line);
1815 vim_free(next_line);
1816 vim_free(allocated);
1817 return retval;
1818}
1819
1820#if defined(FEAT_COMMENTS) || defined(PROTO)
1821/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001822 * get_leader_len() returns the length in bytes of the prefix of the given
1823 * string which introduces a comment. If this string is not a comment then
1824 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 * When "flags" is not NULL, it is set to point to the flags of the recognized
1826 * comment leader.
1827 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001828 * If "include_space" is set, include trailing whitespace while calculating the
1829 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001832get_leader_len(
1833 char_u *line,
1834 char_u **flags,
1835 int backward,
1836 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001839 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int got_com = FALSE;
1841 int found_one;
1842 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1843 char_u *string; /* pointer to comment string */
1844 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001845 int middle_match_len = 0;
1846 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001847 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaar81340392012-06-06 16:12:59 +02001849 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001850 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 ++i;
1852
1853 /*
1854 * Repeat to match several nested comment strings.
1855 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001856 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 /*
1859 * scan through the 'comments' option for a match
1860 */
1861 found_one = FALSE;
1862 for (list = curbuf->b_p_com; *list; )
1863 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001864 /* Get one option part into part_buf[]. Advance "list" to next
1865 * one. Put "string" at start of string. */
1866 if (!got_com && flags != NULL)
1867 *flags = list; /* remember where flags started */
1868 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1870 string = vim_strchr(part_buf, ':');
1871 if (string == NULL) /* missing ':', ignore this part */
1872 continue;
1873 *string++ = NUL; /* isolate flags from string */
1874
Bram Moolenaara4271d52011-05-10 13:38:27 +02001875 /* If we found a middle match previously, use that match when this
1876 * is not a middle or end. */
1877 if (middle_match_len != 0
1878 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1879 && vim_strchr(part_buf, COM_END) == NULL)
1880 break;
1881
1882 /* When we already found a nested comment, only accept further
1883 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1885 continue;
1886
Bram Moolenaara4271d52011-05-10 13:38:27 +02001887 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1889 continue;
1890
Bram Moolenaara4271d52011-05-10 13:38:27 +02001891 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 * When string starts with white space, must have some white space
1893 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001894 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001895 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001897 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001898 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001899 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 ++string;
1901 }
1902 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1903 ;
1904 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001905 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaara4271d52011-05-10 13:38:27 +02001907 /* When 'b' flag used, there must be white space or an
1908 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001910 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 continue;
1912
Bram Moolenaara4271d52011-05-10 13:38:27 +02001913 /* We have found a match, stop searching unless this is a middle
1914 * comment. The middle comment can be a substring of the end
1915 * comment in which case it's better to return the length of the
1916 * end comment and its flags. Thus we keep searching with middle
1917 * and end matches and use an end match if it matches better. */
1918 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1919 {
1920 if (middle_match_len == 0)
1921 {
1922 middle_match_len = j;
1923 saved_flags = prev_list;
1924 }
1925 continue;
1926 }
1927 if (middle_match_len != 0 && j > middle_match_len)
1928 /* Use this match instead of the middle match, since it's a
1929 * longer thus better match. */
1930 middle_match_len = 0;
1931
1932 if (middle_match_len == 0)
1933 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 found_one = TRUE;
1935 break;
1936 }
1937
Bram Moolenaara4271d52011-05-10 13:38:27 +02001938 if (middle_match_len != 0)
1939 {
1940 /* Use the previously found middle match after failing to find a
1941 * match with an end. */
1942 if (!got_com && flags != NULL)
1943 *flags = saved_flags;
1944 i += middle_match_len;
1945 found_one = TRUE;
1946 }
1947
1948 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (!found_one)
1950 break;
1951
Bram Moolenaar81340392012-06-06 16:12:59 +02001952 result = i;
1953
Bram Moolenaara4271d52011-05-10 13:38:27 +02001954 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001955 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 ++i;
1957
Bram Moolenaar81340392012-06-06 16:12:59 +02001958 if (include_space)
1959 result = i;
1960
Bram Moolenaara4271d52011-05-10 13:38:27 +02001961 /* If this comment doesn't nest, stop here. */
1962 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (vim_strchr(part_buf, COM_NEST) == NULL)
1964 break;
1965 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001966 return result;
1967}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001968
Bram Moolenaar81340392012-06-06 16:12:59 +02001969/*
1970 * Return the offset at which the last comment in line starts. If there is no
1971 * comment in the whole line, -1 is returned.
1972 *
1973 * When "flags" is not null, it is set to point to the flags describing the
1974 * recognized comment leader.
1975 */
1976 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001977get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001978{
1979 int result = -1;
1980 int i, j;
1981 int lower_check_bound = 0;
1982 char_u *string;
1983 char_u *com_leader;
1984 char_u *com_flags;
1985 char_u *list;
1986 int found_one;
1987 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1988
1989 /*
1990 * Repeat to match several nested comment strings.
1991 */
1992 i = (int)STRLEN(line);
1993 while (--i >= lower_check_bound)
1994 {
1995 /*
1996 * scan through the 'comments' option for a match
1997 */
1998 found_one = FALSE;
1999 for (list = curbuf->b_p_com; *list; )
2000 {
2001 char_u *flags_save = list;
2002
2003 /*
2004 * Get one option part into part_buf[]. Advance list to next one.
2005 * put string at start of string.
2006 */
2007 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2008 string = vim_strchr(part_buf, ':');
2009 if (string == NULL) /* If everything is fine, this cannot actually
2010 * happen. */
2011 {
2012 continue;
2013 }
2014 *string++ = NUL; /* Isolate flags from string. */
2015 com_leader = string;
2016
2017 /*
2018 * Line contents and string must match.
2019 * When string starts with white space, must have some white space
2020 * (but the amount does not need to match, there might be a mix of
2021 * TABs and spaces).
2022 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002023 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002024 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002025 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002026 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002027 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002028 ++string;
2029 }
2030 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2031 /* do nothing */;
2032 if (string[j] != NUL)
2033 continue;
2034
2035 /*
2036 * When 'b' flag used, there must be white space or an
2037 * end-of-line after the string in the line.
2038 */
2039 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002040 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002041 {
2042 continue;
2043 }
2044
2045 /*
2046 * We have found a match, stop searching.
2047 */
2048 found_one = TRUE;
2049
2050 if (flags)
2051 *flags = flags_save;
2052 com_flags = flags_save;
2053
2054 break;
2055 }
2056
2057 if (found_one)
2058 {
2059 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2060 int len1, len2, off;
2061
2062 result = i;
2063 /*
2064 * If this comment nests, continue searching.
2065 */
2066 if (vim_strchr(part_buf, COM_NEST) != NULL)
2067 continue;
2068
2069 lower_check_bound = i;
2070
2071 /* Let's verify whether the comment leader found is a substring
2072 * of other comment leaders. If it is, let's adjust the
2073 * lower_check_bound so that we make sure that we have determined
2074 * the comment leader correctly.
2075 */
2076
Bram Moolenaar1c465442017-03-12 20:10:05 +01002077 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002078 ++com_leader;
2079 len1 = (int)STRLEN(com_leader);
2080
2081 for (list = curbuf->b_p_com; *list; )
2082 {
2083 char_u *flags_save = list;
2084
2085 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2086 if (flags_save == com_flags)
2087 continue;
2088 string = vim_strchr(part_buf2, ':');
2089 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002090 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002091 ++string;
2092 len2 = (int)STRLEN(string);
2093 if (len2 == 0)
2094 continue;
2095
2096 /* Now we have to verify whether string ends with a substring
2097 * beginning the com_leader. */
2098 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2099 {
2100 --off;
2101 if (!STRNCMP(string + off, com_leader, len2 - off))
2102 {
2103 if (i - off < lower_check_bound)
2104 lower_check_bound = i - off;
2105 }
2106 }
2107 }
2108 }
2109 }
2110 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112#endif
2113
2114/*
2115 * Return the number of window lines occupied by buffer line "lnum".
2116 */
2117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 return plines_win(curwin, lnum, TRUE);
2121}
2122
2123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124plines_win(
2125 win_T *wp,
2126 linenr_T lnum,
2127 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
2129#if defined(FEAT_DIFF) || defined(PROTO)
2130 /* Check for filler lines above this buffer line. When folded the result
2131 * is one line anyway. */
2132 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2133}
2134
2135 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002136plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 return plines_win_nofill(curwin, lnum, TRUE);
2139}
2140
2141 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002142plines_win_nofill(
2143 win_T *wp,
2144 linenr_T lnum,
2145 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
2147#endif
2148 int lines;
2149
2150 if (!wp->w_p_wrap)
2151 return 1;
2152
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (wp->w_width == 0)
2154 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156#ifdef FEAT_FOLDING
2157 /* A folded lines is handled just like an empty line. */
2158 /* NOTE: Caller must handle lines that are MAYBE folded. */
2159 if (lineFolded(wp, lnum) == TRUE)
2160 return 1;
2161#endif
2162
2163 lines = plines_win_nofold(wp, lnum);
2164 if (winheight > 0 && lines > wp->w_height)
2165 return (int)wp->w_height;
2166 return lines;
2167}
2168
2169/*
2170 * Return number of window lines physical line "lnum" will occupy in window
2171 * "wp". Does not care about folding, 'wrap' or 'diff'.
2172 */
2173 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002174plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 char_u *s;
2177 long col;
2178 int width;
2179
2180 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2181 if (*s == NUL) /* empty line */
2182 return 1;
2183 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2184
2185 /*
2186 * If list mode is on, then the '$' at the end of the line may take up one
2187 * extra column.
2188 */
2189 if (wp->w_p_list && lcs_eol != NUL)
2190 col += 1;
2191
2192 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002193 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002195 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if (width <= 0)
2197 return 32000;
2198 if (col <= width)
2199 return 1;
2200 col -= width;
2201 width += win_col_off2(wp);
2202 return (col + (width - 1)) / width + 1;
2203}
2204
2205/*
2206 * Like plines_win(), but only reports the number of physical screen lines
2207 * used from the start of the line to the given column number.
2208 */
2209 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002210plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 long col;
2213 char_u *s;
2214 int lines = 0;
2215 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002216 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218#ifdef FEAT_DIFF
2219 /* Check for filler lines above this buffer line. When folded the result
2220 * is one line anyway. */
2221 lines = diff_check_fill(wp, lnum);
2222#endif
2223
2224 if (!wp->w_p_wrap)
2225 return lines + 1;
2226
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (wp->w_width == 0)
2228 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
Bram Moolenaar597a4222014-06-25 14:39:50 +02002230 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 col = 0;
2233 while (*s != NUL && --column >= 0)
2234 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002235 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002236 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238
2239 /*
2240 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2241 * INSERT mode, then col must be adjusted so that it represents the last
2242 * screen position of the TAB. This only fixes an error when the TAB wraps
2243 * from one screen line to the next (when 'columns' is not a multiple of
2244 * 'ts') -- webb.
2245 */
2246 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002247 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002250 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002252 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002253 if (width <= 0)
2254 return 9999;
2255
2256 lines += 1;
2257 if (col > width)
2258 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2259 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260}
2261
2262 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002263plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 int count = 0;
2266
2267 while (first <= last)
2268 {
2269#ifdef FEAT_FOLDING
2270 int x;
2271
2272 /* Check if there are any really folded lines, but also included lines
2273 * that are maybe folded. */
2274 x = foldedCount(wp, first, NULL);
2275 if (x > 0)
2276 {
2277 ++count; /* count 1 for "+-- folded" line */
2278 first += x;
2279 }
2280 else
2281#endif
2282 {
2283#ifdef FEAT_DIFF
2284 if (first == wp->w_topline)
2285 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2286 else
2287#endif
2288 count += plines_win(wp, first, TRUE);
2289 ++first;
2290 }
2291 }
2292 return (count);
2293}
2294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295/*
2296 * Insert string "p" at the cursor position. Stops at a NUL byte.
2297 * Handles Replace mode and multi-byte characters.
2298 */
2299 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002300ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 ins_bytes_len(p, (int)STRLEN(p));
2303}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
2306 * Insert string "p" with length "len" at the cursor position.
2307 * Handles Replace mode and multi-byte characters.
2308 */
2309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002310ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002313#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 int n;
2315
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002316 if (has_mbyte)
2317 for (i = 0; i < len; i += n)
2318 {
2319 if (enc_utf8)
2320 /* avoid reading past p[len] */
2321 n = utfc_ptr2len_len(p + i, len - i);
2322 else
2323 n = (*mb_ptr2len)(p + i);
2324 ins_char_bytes(p + i, n);
2325 }
2326 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002327#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002328 for (i = 0; i < len; ++i)
2329 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
2332/*
2333 * Insert or replace a single character at the cursor position.
2334 * When in REPLACE or VREPLACE mode, replace any existing character.
2335 * Caller must have prepared for undo.
2336 * For multi-byte characters we get the whole character, the caller must
2337 * convert bytes to a character.
2338 */
2339 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002340ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002342 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002343 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002345#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 n = (*mb_char2bytes)(c, buf);
2347
2348 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2349 * Happens for CTRL-Vu9900. */
2350 if (buf[0] == 0)
2351 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002352#else
2353 buf[0] = c;
2354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356 ins_char_bytes(buf, n);
2357}
2358
2359 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002360ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 int newlen; /* nr of bytes inserted */
2364 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2365 char_u *p;
2366 char_u *newp;
2367 char_u *oldp;
2368 int linelen; /* length of old line including NUL */
2369 colnr_T col;
2370 linenr_T lnum = curwin->w_cursor.lnum;
2371 int i;
2372
2373#ifdef FEAT_VIRTUALEDIT
2374 /* Break tabs if needed. */
2375 if (virtual_active() && curwin->w_cursor.coladd > 0)
2376 coladvance_force(getviscol());
2377#endif
2378
2379 col = curwin->w_cursor.col;
2380 oldp = ml_get(lnum);
2381 linelen = (int)STRLEN(oldp) + 1;
2382
2383 /* The lengths default to the values for when not replacing. */
2384 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 if (State & REPLACE_FLAG)
2388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (State & VREPLACE_FLAG)
2390 {
2391 colnr_T new_vcol = 0; /* init for GCC */
2392 colnr_T vcol;
2393 int old_list;
2394#ifndef FEAT_MBYTE
2395 char_u buf[2];
2396#endif
2397
2398 /*
2399 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2400 * Returns the old value of list, so when finished,
2401 * curwin->w_p_list should be set back to this.
2402 */
2403 old_list = curwin->w_p_list;
2404 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2405 curwin->w_p_list = FALSE;
2406
2407 /*
2408 * In virtual replace mode each character may replace one or more
2409 * characters (zero if it's a TAB). Count the number of bytes to
2410 * be deleted to make room for the new character, counting screen
2411 * cells. May result in adding spaces to fill a gap.
2412 */
2413 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2414#ifndef FEAT_MBYTE
2415 buf[0] = c;
2416 buf[1] = NUL;
2417#endif
2418 new_vcol = vcol + chartabsize(buf, vcol);
2419 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2420 {
2421 vcol += chartabsize(oldp + col + oldlen, vcol);
2422 /* Don't need to remove a TAB that takes us to the right
2423 * position. */
2424 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2425 break;
2426#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002427 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428#else
2429 ++oldlen;
2430#endif
2431 /* Deleted a bit too much, insert spaces. */
2432 if (vcol > new_vcol)
2433 newlen += vcol - new_vcol;
2434 }
2435 curwin->w_p_list = old_list;
2436 }
2437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (oldp[col] != NUL)
2439 {
2440 /* normal replace */
2441#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002442 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443#else
2444 oldlen = 1;
2445#endif
2446 }
2447
2448
2449 /* Push the replaced bytes onto the replace stack, so that they can be
2450 * put back when BS is used. The bytes of a multi-byte character are
2451 * done the other way around, so that the first byte is popped off
2452 * first (it tells the byte length of the character). */
2453 replace_push(NUL);
2454 for (i = 0; i < oldlen; ++i)
2455 {
2456#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002457 if (has_mbyte)
2458 i += replace_push_mb(oldp + col + i) - 1;
2459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002461 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 }
2464
2465 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2466 if (newp == NULL)
2467 return;
2468
2469 /* Copy bytes before the cursor. */
2470 if (col > 0)
2471 mch_memmove(newp, oldp, (size_t)col);
2472
2473 /* Copy bytes after the changed character(s). */
2474 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002475 if (linelen > col + oldlen)
2476 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 (size_t)(linelen - col - oldlen));
2478
2479 /* Insert or overwrite the new character. */
2480#ifdef FEAT_MBYTE
2481 mch_memmove(p, buf, charlen);
2482 i = charlen;
2483#else
2484 *p = c;
2485 i = 1;
2486#endif
2487
2488 /* Fill with spaces when necessary. */
2489 while (i < newlen)
2490 p[i++] = ' ';
2491
2492 /* Replace the line in the buffer. */
2493 ml_replace(lnum, newp, FALSE);
2494
2495 /* mark the buffer as changed and prepare for displaying */
2496 changed_bytes(lnum, col);
2497
2498 /*
2499 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2500 * show the match for right parens and braces.
2501 */
2502 if (p_sm && (State & INSERT)
2503 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002504#ifdef FEAT_INS_EXPAND
2505 && !ins_compl_active()
2506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002508 {
2509#ifdef FEAT_MBYTE
2510 if (has_mbyte)
2511 showmatch(mb_ptr2char(buf));
2512 else
2513#endif
2514 showmatch(c);
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517#ifdef FEAT_RIGHTLEFT
2518 if (!p_ri || (State & REPLACE_FLAG))
2519#endif
2520 {
2521 /* Normal insert: move cursor right */
2522#ifdef FEAT_MBYTE
2523 curwin->w_cursor.col += charlen;
2524#else
2525 ++curwin->w_cursor.col;
2526#endif
2527 }
2528 /*
2529 * TODO: should try to update w_row here, to avoid recomputing it later.
2530 */
2531}
2532
2533/*
2534 * Insert a string at the cursor position.
2535 * Note: Does NOT handle Replace mode.
2536 * Caller must have prepared for undo.
2537 */
2538 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002539ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 char_u *oldp, *newp;
2542 int newlen = (int)STRLEN(s);
2543 int oldlen;
2544 colnr_T col;
2545 linenr_T lnum = curwin->w_cursor.lnum;
2546
2547#ifdef FEAT_VIRTUALEDIT
2548 if (virtual_active() && curwin->w_cursor.coladd > 0)
2549 coladvance_force(getviscol());
2550#endif
2551
2552 col = curwin->w_cursor.col;
2553 oldp = ml_get(lnum);
2554 oldlen = (int)STRLEN(oldp);
2555
2556 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2557 if (newp == NULL)
2558 return;
2559 if (col > 0)
2560 mch_memmove(newp, oldp, (size_t)col);
2561 mch_memmove(newp + col, s, (size_t)newlen);
2562 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2563 ml_replace(lnum, newp, FALSE);
2564 changed_bytes(lnum, col);
2565 curwin->w_cursor.col += newlen;
2566}
2567
2568/*
2569 * Delete one character under the cursor.
2570 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2571 * Caller must have prepared for undo.
2572 *
2573 * return FAIL for failure, OK otherwise
2574 */
2575 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002576del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578#ifdef FEAT_MBYTE
2579 if (has_mbyte)
2580 {
2581 /* Make sure the cursor is at the start of a character. */
2582 mb_adjust_cursor();
2583 if (*ml_get_cursor() == NUL)
2584 return FAIL;
2585 return del_chars(1L, fixpos);
2586 }
2587#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002588 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589}
2590
2591#if defined(FEAT_MBYTE) || defined(PROTO)
2592/*
2593 * Like del_bytes(), but delete characters instead of bytes.
2594 */
2595 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002596del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 long bytes = 0;
2599 long i;
2600 char_u *p;
2601 int l;
2602
2603 p = ml_get_cursor();
2604 for (i = 0; i < count && *p != NUL; ++i)
2605 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002606 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 bytes += l;
2608 p += l;
2609 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002610 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611}
2612#endif
2613
2614/*
2615 * Delete "count" bytes under the cursor.
2616 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2617 * Caller must have prepared for undo.
2618 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002619 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
2621 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002622del_bytes(
2623 long count,
2624 int fixpos_arg,
2625 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 char_u *oldp, *newp;
2628 colnr_T oldlen;
2629 linenr_T lnum = curwin->w_cursor.lnum;
2630 colnr_T col = curwin->w_cursor.col;
2631 int was_alloced;
2632 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002633 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 oldp = ml_get(lnum);
2636 oldlen = (int)STRLEN(oldp);
2637
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002638 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (col >= oldlen)
2640 return FAIL;
2641
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002642 /* If "count" is zero there is nothing to do. */
2643 if (count == 0)
2644 return OK;
2645
2646 /* If "count" is negative the caller must be doing something wrong. */
2647 if (count < 1)
2648 {
2649 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2650 return FAIL;
2651 }
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653#ifdef FEAT_MBYTE
2654 /* If 'delcombine' is set and deleting (less than) one character, only
2655 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002656 if (p_deco && use_delcombine && enc_utf8
2657 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002659 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 int n;
2661
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002662 (void)utfc_ptr2char(oldp + col, cc);
2663 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
2665 /* Find the last composing char, there can be several. */
2666 n = col;
2667 do
2668 {
2669 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002670 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 n += count;
2672 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2673 fixpos = 0;
2674 }
2675 }
2676#endif
2677
2678 /*
2679 * When count is too big, reduce it.
2680 */
2681 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2682 if (movelen <= 1)
2683 {
2684 /*
2685 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002686 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2687 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002689 if (col > 0 && fixpos && restart_edit == 0
2690#ifdef FEAT_VIRTUALEDIT
2691 && (ve_flags & VE_ONEMORE) == 0
2692#endif
2693 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
2695 --curwin->w_cursor.col;
2696#ifdef FEAT_VIRTUALEDIT
2697 curwin->w_cursor.coladd = 0;
2698#endif
2699#ifdef FEAT_MBYTE
2700 if (has_mbyte)
2701 curwin->w_cursor.col -=
2702 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2703#endif
2704 }
2705 count = oldlen - col;
2706 movelen = 1;
2707 }
2708
2709 /*
2710 * If the old line has been allocated the deletion can be done in the
2711 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002712 * Can't do this when using Netbeans, because we would need to invoke
2713 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002714 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002717 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002718 was_alloced = FALSE;
2719 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002721 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (was_alloced)
2723 newp = oldp; /* use same allocated memory */
2724 else
2725 { /* need to allocate a new line */
2726 newp = alloc((unsigned)(oldlen + 1 - count));
2727 if (newp == NULL)
2728 return FAIL;
2729 mch_memmove(newp, oldp, (size_t)col);
2730 }
2731 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2732 if (!was_alloced)
2733 ml_replace(lnum, newp, FALSE);
2734
2735 /* mark the buffer as changed and prepare for displaying */
2736 changed_bytes(lnum, curwin->w_cursor.col);
2737
2738 return OK;
2739}
2740
2741/*
2742 * Delete from cursor to end of line.
2743 * Caller must have prepared for undo.
2744 *
2745 * return FAIL for failure, OK otherwise
2746 */
2747 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002748truncate_line(
2749 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
2751 char_u *newp;
2752 linenr_T lnum = curwin->w_cursor.lnum;
2753 colnr_T col = curwin->w_cursor.col;
2754
2755 if (col == 0)
2756 newp = vim_strsave((char_u *)"");
2757 else
2758 newp = vim_strnsave(ml_get(lnum), col);
2759
2760 if (newp == NULL)
2761 return FAIL;
2762
2763 ml_replace(lnum, newp, FALSE);
2764
2765 /* mark the buffer as changed and prepare for displaying */
2766 changed_bytes(lnum, curwin->w_cursor.col);
2767
2768 /*
2769 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2770 */
2771 if (fixpos && curwin->w_cursor.col > 0)
2772 --curwin->w_cursor.col;
2773
2774 return OK;
2775}
2776
2777/*
2778 * Delete "nlines" lines at the cursor.
2779 * Saves the lines for undo first if "undo" is TRUE.
2780 */
2781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002782del_lines(
2783 long nlines, /* number of lines to delete */
2784 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
2786 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002787 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 if (nlines <= 0)
2790 return;
2791
2792 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002793 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 return;
2795
2796 for (n = 0; n < nlines; )
2797 {
2798 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2799 break;
2800
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002801 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 ++n;
2803
2804 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002805 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 break;
2807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002809 /* Correct the cursor position before calling deleted_lines_mark(), it may
2810 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 curwin->w_cursor.col = 0;
2812 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002813
2814 /* adjust marks, mark the buffer as changed and prepare for displaying */
2815 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816}
2817
2818 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002819gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002821 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002823 /* When searching columns is sometimes put at the end of a line. */
2824 if (pos->col == MAXCOL)
2825 return NUL;
2826 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827#ifdef FEAT_MBYTE
2828 if (has_mbyte)
2829 return (*mb_ptr2char)(ptr);
2830#endif
2831 return (int)*ptr;
2832}
2833
2834 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002835gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837#ifdef FEAT_MBYTE
2838 if (has_mbyte)
2839 return (*mb_ptr2char)(ml_get_cursor());
2840#endif
2841 return (int)*ml_get_cursor();
2842}
2843
2844/*
2845 * Write a character at the current cursor position.
2846 * It is directly written into the block.
2847 */
2848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002849pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
2851 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2852 + curwin->w_cursor.col) = c;
2853}
2854
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855/*
2856 * When extra == 0: Return TRUE if the cursor is before or on the first
2857 * non-blank in the line.
2858 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2859 * the line.
2860 */
2861 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002862inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 char_u *ptr;
2865 colnr_T col;
2866
Bram Moolenaar1c465442017-03-12 20:10:05 +01002867 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 ++ptr;
2869 if (col >= curwin->w_cursor.col + extra)
2870 return TRUE;
2871 else
2872 return FALSE;
2873}
2874
2875/*
2876 * Skip to next part of an option argument: Skip space and comma.
2877 */
2878 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002879skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 if (*p == ',')
2882 ++p;
2883 while (*p == ' ')
2884 ++p;
2885 return p;
2886}
2887
2888/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002889 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 *
2891 * Most often called through changed_bytes() and changed_lines(), which also
2892 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002893 *
2894 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 */
2896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002897changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002900 if (p_imst == IM_ON_THE_SPOT)
2901 {
2902 /* The text of the preediting area is inserted, but this doesn't
2903 * mean a change of the buffer yet. That is delayed until the
2904 * text is committed. (this means preedit becomes empty) */
2905 if (im_is_preediting() && !xim_changed_while_preediting)
2906 return;
2907 xim_changed_while_preediting = FALSE;
2908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#endif
2910
2911 if (!curbuf->b_changed)
2912 {
2913 int save_msg_scroll = msg_scroll;
2914
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002915 /* Give a warning about changing a read-only file. This may also
2916 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /* Create a swap file if that is wanted.
2920 * Don't do this for "nofile" and "nowrite" buffer types. */
2921 if (curbuf->b_may_swap
2922#ifdef FEAT_QUICKFIX
2923 && !bt_dontwrite(curbuf)
2924#endif
2925 )
2926 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002927 int save_need_wait_return = need_wait_return;
2928
2929 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 ml_open_file(curbuf);
2931
2932 /* The ml_open_file() can cause an ATTENTION message.
2933 * Wait two seconds, to make sure the user reads this unexpected
2934 * message. Since we could be anywhere, call wait_return() now,
2935 * and don't let the emsg() set msg_scroll. */
2936 if (need_wait_return && emsg_silent == 0)
2937 {
2938 out_flush();
2939 ui_delay(2000L, TRUE);
2940 wait_return(TRUE);
2941 msg_scroll = save_msg_scroll;
2942 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002943 else
2944 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002946 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002948 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949}
2950
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002951/*
2952 * Internal part of changed(), no user interaction.
2953 */
2954 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002955changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002956{
2957 curbuf->b_changed = TRUE;
2958 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002959 check_status(curbuf);
2960 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002961#ifdef FEAT_TITLE
2962 need_maketitle = TRUE; /* set window title later */
2963#endif
2964}
2965
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002966static void changedOneline(buf_T *buf, linenr_T lnum);
2967static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2968static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
2970/*
2971 * Changed bytes within a single line for the current buffer.
2972 * - marks the windows on this buffer to be redisplayed
2973 * - marks the buffer changed by calling changed()
2974 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002975 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 */
2977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002978changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002980 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002982
2983#ifdef FEAT_DIFF
2984 /* Diff highlighting in other diff windows may need to be updated too. */
2985 if (curwin->w_p_diff)
2986 {
2987 win_T *wp;
2988 linenr_T wlnum;
2989
Bram Moolenaar29323592016-07-24 22:04:11 +02002990 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002991 if (wp->w_p_diff && wp != curwin)
2992 {
2993 redraw_win_later(wp, VALID);
2994 wlnum = diff_lnum_win(lnum, wp);
2995 if (wlnum > 0)
2996 changedOneline(wp->w_buffer, wlnum);
2997 }
2998 }
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000}
3001
3002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003003changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003005 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
3007 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003008 if (lnum < buf->b_mod_top)
3009 buf->b_mod_top = lnum;
3010 else if (lnum >= buf->b_mod_bot)
3011 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013 else
3014 {
3015 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003016 buf->b_mod_set = TRUE;
3017 buf->b_mod_top = lnum;
3018 buf->b_mod_bot = lnum + 1;
3019 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021}
3022
3023/*
3024 * Appended "count" lines below line "lnum" in the current buffer.
3025 * Must be called AFTER the change and after mark_adjust().
3026 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3027 */
3028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003029appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 changed_lines(lnum + 1, 0, lnum + 1, count);
3032}
3033
3034/*
3035 * Like appended_lines(), but adjust marks first.
3036 */
3037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003038appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003040 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003041 * be marks there. But it's still needed in diff mode. */
3042 if (lnum + count < curbuf->b_ml.ml_line_count
3043#ifdef FEAT_DIFF
3044 || curwin->w_p_diff
3045#endif
3046 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003047 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 changed_lines(lnum + 1, 0, lnum + 1, count);
3049}
3050
3051/*
3052 * Deleted "count" lines at line "lnum" in the current buffer.
3053 * Must be called AFTER the change and after mark_adjust().
3054 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3055 */
3056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003057deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 changed_lines(lnum, 0, lnum + count, -count);
3060}
3061
3062/*
3063 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003064 * Make sure the cursor is on a valid line before calling, a GUI callback may
3065 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 */
3067 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003068deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3071 changed_lines(lnum, 0, lnum + count, -count);
3072}
3073
3074/*
3075 * Changed lines for the current buffer.
3076 * Must be called AFTER the change and after mark_adjust().
3077 * - mark the buffer changed by calling changed()
3078 * - mark the windows on this buffer to be redisplayed
3079 * - invalidate cached values
3080 * "lnum" is the first line that needs displaying, "lnume" the first line
3081 * below the changed lines (BEFORE the change).
3082 * When only inserting lines, "lnum" and "lnume" are equal.
3083 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003084 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
3086 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003087changed_lines(
3088 linenr_T lnum, /* first line with change */
3089 colnr_T col, /* column in first line with change */
3090 linenr_T lnume, /* line below last changed line */
3091 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003093 changed_lines_buf(curbuf, lnum, lnume, xtra);
3094
3095#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003096 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003097 {
3098 /* When the number of lines doesn't change then mark_adjust() isn't
3099 * called and other diff buffers still need to be marked for
3100 * displaying. */
3101 win_T *wp;
3102 linenr_T wlnum;
3103
Bram Moolenaar29323592016-07-24 22:04:11 +02003104 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003105 if (wp->w_p_diff && wp != curwin)
3106 {
3107 redraw_win_later(wp, VALID);
3108 wlnum = diff_lnum_win(lnum, wp);
3109 if (wlnum > 0)
3110 changed_lines_buf(wp->w_buffer, wlnum,
3111 lnume - lnum + wlnum, 0L);
3112 }
3113 }
3114#endif
3115
3116 changed_common(lnum, col, lnume, xtra);
3117}
3118
3119 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003120changed_lines_buf(
3121 buf_T *buf,
3122 linenr_T lnum, /* first line with change */
3123 linenr_T lnume, /* line below last changed line */
3124 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003125{
3126 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003129 if (lnum < buf->b_mod_top)
3130 buf->b_mod_top = lnum;
3131 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003134 buf->b_mod_bot += xtra;
3135 if (buf->b_mod_bot < lnum)
3136 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003138 if (lnume + xtra > buf->b_mod_bot)
3139 buf->b_mod_bot = lnume + xtra;
3140 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
3142 else
3143 {
3144 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003145 buf->b_mod_set = TRUE;
3146 buf->b_mod_top = lnum;
3147 buf->b_mod_bot = lnume + xtra;
3148 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150}
3151
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003152/*
3153 * Common code for when a change is was made.
3154 * See changed_lines() for the arguments.
3155 * Careful: may trigger autocommands that reload the buffer.
3156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003158changed_common(
3159 linenr_T lnum,
3160 colnr_T col,
3161 linenr_T lnume,
3162 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003165 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 int i;
3167#ifdef FEAT_JUMPLIST
3168 int cols;
3169 pos_T *p;
3170 int add;
3171#endif
3172
3173 /* mark the buffer as modified */
3174 changed();
3175
Bram Moolenaare3521d92018-09-16 14:10:31 +02003176#ifdef FEAT_DIFF
3177 if (curwin->w_p_diff && diff_internal())
3178 curtab->tp_diff_update = TRUE;
3179#endif
3180
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 /* set the '. mark */
3182 if (!cmdmod.keepjumps)
3183 {
3184 curbuf->b_last_change.lnum = lnum;
3185 curbuf->b_last_change.col = col;
3186
3187#ifdef FEAT_JUMPLIST
3188 /* Create a new entry if a new undo-able change was started or we
3189 * don't have an entry yet. */
3190 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3191 {
3192 if (curbuf->b_changelistlen == 0)
3193 add = TRUE;
3194 else
3195 {
3196 /* Don't create a new entry when the line number is the same
3197 * as the last one and the column is not too far away. Avoids
3198 * creating many entries for typing "xxxxx". */
3199 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3200 if (p->lnum != lnum)
3201 add = TRUE;
3202 else
3203 {
3204 cols = comp_textwidth(FALSE);
3205 if (cols == 0)
3206 cols = 79;
3207 add = (p->col + cols < col || col + cols < p->col);
3208 }
3209 }
3210 if (add)
3211 {
3212 /* This is the first of a new sequence of undo-able changes
3213 * and it's at some distance of the last change. Use a new
3214 * position in the changelist. */
3215 curbuf->b_new_change = FALSE;
3216
3217 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3218 {
3219 /* changelist is full: remove oldest entry */
3220 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3221 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3222 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003223 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 {
3225 /* Correct position in changelist for other windows on
3226 * this buffer. */
3227 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3228 --wp->w_changelistidx;
3229 }
3230 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003231 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
3233 /* For other windows, if the position in the changelist is
3234 * at the end it stays at the end. */
3235 if (wp->w_buffer == curbuf
3236 && wp->w_changelistidx == curbuf->b_changelistlen)
3237 ++wp->w_changelistidx;
3238 }
3239 ++curbuf->b_changelistlen;
3240 }
3241 }
3242 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3243 curbuf->b_last_change;
3244 /* The current window is always after the last change, so that "g,"
3245 * takes you back to it. */
3246 curwin->w_changelistidx = curbuf->b_changelistlen;
3247#endif
3248 }
3249
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003250 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
3252 if (wp->w_buffer == curbuf)
3253 {
3254 /* Mark this window to be redrawn later. */
3255 if (wp->w_redr_type < VALID)
3256 wp->w_redr_type = VALID;
3257
3258 /* Check if a change in the buffer has invalidated the cached
3259 * values for the cursor. */
3260#ifdef FEAT_FOLDING
3261 /*
3262 * Update the folds for this window. Can't postpone this, because
3263 * a following operator might work on the whole fold: ">>dd".
3264 */
3265 foldUpdate(wp, lnum, lnume + xtra - 1);
3266
3267 /* The change may cause lines above or below the change to become
3268 * included in a fold. Set lnum/lnume to the first/last line that
3269 * might be displayed differently.
3270 * Set w_cline_folded here as an efficient way to update it when
3271 * inserting lines just above a closed fold. */
3272 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3273 if (wp->w_cursor.lnum == lnum)
3274 wp->w_cline_folded = i;
3275 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3276 if (wp->w_cursor.lnum == lnume)
3277 wp->w_cline_folded = i;
3278
3279 /* If the changed line is in a range of previously folded lines,
3280 * compare with the first line in that range. */
3281 if (wp->w_cursor.lnum <= lnum)
3282 {
3283 i = find_wl_entry(wp, lnum);
3284 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3285 changed_line_abv_curs_win(wp);
3286 }
3287#endif
3288
3289 if (wp->w_cursor.lnum > lnum)
3290 changed_line_abv_curs_win(wp);
3291 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3292 changed_cline_bef_curs_win(wp);
3293 if (wp->w_botline >= lnum)
3294 {
3295 /* Assume that botline doesn't change (inserted lines make
3296 * other lines scroll down below botline). */
3297 approximate_botline_win(wp);
3298 }
3299
3300 /* Check if any w_lines[] entries have become invalid.
3301 * For entries below the change: Correct the lnums for
3302 * inserted/deleted lines. Makes it possible to stop displaying
3303 * after the change. */
3304 for (i = 0; i < wp->w_lines_valid; ++i)
3305 if (wp->w_lines[i].wl_valid)
3306 {
3307 if (wp->w_lines[i].wl_lnum >= lnum)
3308 {
3309 if (wp->w_lines[i].wl_lnum < lnume)
3310 {
3311 /* line included in change */
3312 wp->w_lines[i].wl_valid = FALSE;
3313 }
3314 else if (xtra != 0)
3315 {
3316 /* line below change */
3317 wp->w_lines[i].wl_lnum += xtra;
3318#ifdef FEAT_FOLDING
3319 wp->w_lines[i].wl_lastlnum += xtra;
3320#endif
3321 }
3322 }
3323#ifdef FEAT_FOLDING
3324 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3325 {
3326 /* change somewhere inside this range of folded lines,
3327 * may need to be redrawn */
3328 wp->w_lines[i].wl_valid = FALSE;
3329 }
3330#endif
3331 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003332
3333#ifdef FEAT_FOLDING
3334 /* Take care of side effects for setting w_topline when folds have
3335 * changed. Esp. when the buffer was changed in another window. */
3336 if (hasAnyFolding(wp))
3337 set_topline(wp, wp->w_topline);
3338#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003339 /* relative numbering may require updating more */
3340 if (wp->w_p_rnu)
3341 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343 }
3344
3345 /* Call update_screen() later, which checks out what needs to be redrawn,
3346 * since it notices b_mod_set and then uses b_mod_*. */
3347 if (must_redraw < VALID)
3348 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003349
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003350 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003351 if (lnum <= curwin->w_cursor.lnum
3352 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003353 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354}
3355
3356/*
3357 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3358 */
3359 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003360unchanged(
3361 buf_T *buf,
3362 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003364 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
3366 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003367 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (ff)
3369 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003371 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#ifdef FEAT_TITLE
3373 need_maketitle = TRUE; /* set window title later */
3374#endif
3375 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003376 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#ifdef FEAT_NETBEANS_INTG
3378 netbeans_unmodified(buf);
3379#endif
3380}
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382/*
3383 * check_status: called when the status bars for the buffer 'buf'
3384 * need to be updated
3385 */
3386 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003387check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 win_T *wp;
3390
Bram Moolenaar29323592016-07-24 22:04:11 +02003391 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 if (wp->w_buffer == buf && wp->w_status_height)
3393 {
3394 wp->w_redr_status = TRUE;
3395 if (must_redraw < VALID)
3396 must_redraw = VALID;
3397 }
3398}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
3400/*
3401 * If the file is readonly, give a warning message with the first change.
3402 * Don't do this for autocommands.
3403 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003404 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003406 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 */
3408 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003409change_warning(
3410 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 mode and 'showmode' is on */
3412{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003413 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (curbuf->b_did_warn == FALSE
3416 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 && curbuf->b_p_ro)
3419 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003420 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003422 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 if (!curbuf->b_p_ro)
3424 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 /*
3426 * Do what msg() does, but with a column offset if the warning should
3427 * be after the mode message.
3428 */
3429 msg_start();
3430 if (msg_row == Rows - 1)
3431 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003432 msg_source(HL_ATTR(HLF_W));
3433 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003434#ifdef FEAT_EVAL
3435 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 msg_clr_eos();
3438 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003439 if (msg_silent == 0 && !silent_mode
3440#ifdef FEAT_EVAL
3441 && time_for_testing != 1
3442#endif
3443 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
3445 out_flush();
3446 ui_delay(1000L, TRUE); /* give the user time to think about it */
3447 }
3448 curbuf->b_did_warn = TRUE;
3449 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3450 if (msg_row < Rows - 1)
3451 showmode();
3452 }
3453}
3454
3455/*
3456 * Ask for a reply from the user, a 'y' or a 'n'.
3457 * No other characters are accepted, the message is repeated until a valid
3458 * reply is entered or CTRL-C is hit.
3459 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3460 * from any buffers but directly from the user.
3461 *
3462 * return the 'y' or 'n'
3463 */
3464 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003465ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 int r = ' ';
3468 int save_State = State;
3469
3470 if (exiting) /* put terminal in raw mode for this question */
3471 settmode(TMODE_RAW);
3472 ++no_wait_return;
3473#ifdef USE_ON_FLY_SCROLL
3474 dont_scroll = TRUE; /* disallow scrolling here */
3475#endif
3476 State = CONFIRM; /* mouse behaves like with :confirm */
3477#ifdef FEAT_MOUSE
3478 setmouse(); /* disables mouse for xterm */
3479#endif
3480 ++no_mapping;
3481 ++allow_keys; /* no mapping here, but recognize keys */
3482
3483 while (r != 'y' && r != 'n')
3484 {
3485 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003486 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (direct)
3488 r = get_keystroke();
3489 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003490 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (r == Ctrl_C || r == ESC)
3492 r = 'n';
3493 msg_putchar(r); /* show what you typed */
3494 out_flush();
3495 }
3496 --no_wait_return;
3497 State = save_State;
3498#ifdef FEAT_MOUSE
3499 setmouse();
3500#endif
3501 --no_mapping;
3502 --allow_keys;
3503
3504 return r;
3505}
3506
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003507#if defined(FEAT_MOUSE) || defined(PROTO)
3508/*
3509 * Return TRUE if "c" is a mouse key.
3510 */
3511 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003512is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003513{
3514 return c == K_LEFTMOUSE
3515 || c == K_LEFTMOUSE_NM
3516 || c == K_LEFTDRAG
3517 || c == K_LEFTRELEASE
3518 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003519 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003520 || c == K_MIDDLEMOUSE
3521 || c == K_MIDDLEDRAG
3522 || c == K_MIDDLERELEASE
3523 || c == K_RIGHTMOUSE
3524 || c == K_RIGHTDRAG
3525 || c == K_RIGHTRELEASE
3526 || c == K_MOUSEDOWN
3527 || c == K_MOUSEUP
3528 || c == K_MOUSELEFT
3529 || c == K_MOUSERIGHT
3530 || c == K_X1MOUSE
3531 || c == K_X1DRAG
3532 || c == K_X1RELEASE
3533 || c == K_X2MOUSE
3534 || c == K_X2DRAG
3535 || c == K_X2RELEASE;
3536}
3537#endif
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539/*
3540 * Get a key stroke directly from the user.
3541 * Ignores mouse clicks and scrollbar events, except a click for the left
3542 * button (used at the more prompt).
3543 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3544 * Disadvantage: typeahead is ignored.
3545 * Translates the interrupt character for unix to ESC.
3546 */
3547 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003548get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003550 char_u *buf = NULL;
3551 int buflen = 150;
3552 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 int len = 0;
3554 int n;
3555 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003556 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
3558 mapped_ctrl_c = FALSE; /* mappings are not used here */
3559 for (;;)
3560 {
3561 cursor_on();
3562 out_flush();
3563
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003564 /* Leave some room for check_termcode() to insert a key code into (max
3565 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3566 * bytes. */
3567 maxlen = (buflen - 6 - len) / 3;
3568 if (buf == NULL)
3569 buf = alloc(buflen);
3570 else if (maxlen < 10)
3571 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003572 char_u *t_buf = buf;
3573
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003574 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003575 * escape sequence. */
3576 buflen += 100;
3577 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003578 if (buf == NULL)
3579 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003580 maxlen = (buflen - 6 - len) / 3;
3581 }
3582 if (buf == NULL)
3583 {
3584 do_outofmem_msg((long_u)buflen);
3585 return ESC; /* panic! */
3586 }
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003589 * terminal code to complete. */
3590 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (n > 0)
3592 {
3593 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003594 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003596 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003598 else if (len > 0)
3599 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
Bram Moolenaar4395a712006-09-05 18:57:57 +00003601 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003602 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003603 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003605
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003606 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003607 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003608 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003609 {
3610 /* Redrawing was postponed, do it now. */
3611 update_screen(0);
3612 setcursor(); /* put cursor back where it belongs */
3613 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003614 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003615 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003616 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003618 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 continue;
3620
3621 /* Handle modifier and/or special key code. */
3622 n = buf[0];
3623 if (n == K_SPECIAL)
3624 {
3625 n = TO_SPECIAL(buf[1], buf[2]);
3626 if (buf[1] == KS_MODIFIER
3627 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003628#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003629 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003630#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003631#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 || n == K_VER_SCROLLBAR
3633 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#endif
3635 )
3636 {
3637 if (buf[1] == KS_MODIFIER)
3638 mod_mask = buf[2];
3639 len -= 3;
3640 if (len > 0)
3641 mch_memmove(buf, buf + 3, (size_t)len);
3642 continue;
3643 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003644 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646#ifdef FEAT_MBYTE
3647 if (has_mbyte)
3648 {
3649 if (MB_BYTE2LEN(n) > len)
3650 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003651 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 n = (*mb_ptr2char)(buf);
3653 }
3654#endif
3655#ifdef UNIX
3656 if (n == intr_char)
3657 n = ESC;
3658#endif
3659 break;
3660 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003661 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 mapped_ctrl_c = save_mapped_ctrl_c;
3664 return n;
3665}
3666
3667/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003668 * Get a number from the user.
3669 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 */
3671 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003672get_number(
3673 int colon, /* allow colon to abort */
3674 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 int n = 0;
3677 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003678 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003680 if (mouse_used != NULL)
3681 *mouse_used = FALSE;
3682
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 /* When not printing messages, the user won't know what to type, return a
3684 * zero (as if CR was hit). */
3685 if (msg_silent != 0)
3686 return 0;
3687
3688#ifdef USE_ON_FLY_SCROLL
3689 dont_scroll = TRUE; /* disallow scrolling here */
3690#endif
3691 ++no_mapping;
3692 ++allow_keys; /* no mapping here, but recognize keys */
3693 for (;;)
3694 {
3695 windgoto(msg_row, msg_col);
3696 c = safe_vgetc();
3697 if (VIM_ISDIGIT(c))
3698 {
3699 n = n * 10 + c - '0';
3700 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003701 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3704 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003705 if (typed > 0)
3706 {
3707 MSG_PUTS("\b \b");
3708 --typed;
3709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003712#ifdef FEAT_MOUSE
3713 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3714 {
3715 *mouse_used = TRUE;
3716 n = mouse_row + 1;
3717 break;
3718 }
3719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 else if (n == 0 && c == ':' && colon)
3721 {
3722 stuffcharReadbuff(':');
3723 if (!exmode_active)
3724 cmdline_row = msg_row;
3725 skip_redraw = TRUE; /* skip redraw once */
3726 do_redraw = FALSE;
3727 break;
3728 }
3729 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3730 break;
3731 }
3732 --no_mapping;
3733 --allow_keys;
3734 return n;
3735}
3736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003737/*
3738 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003739 * When "mouse_used" is not NULL allow using the mouse and in that case return
3740 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 */
3742 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003743prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744{
3745 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003746 int save_cmdline_row;
3747 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003748
3749 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003750 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003751 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003752 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003753 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003754
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003755 // Set the state such that text can be selected/copied/pasted and we still
3756 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3757 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003758 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003759 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003760 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003761 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003762#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003763 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003764 setmouse();
3765#endif
3766
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003767 i = get_number(TRUE, mouse_used);
3768 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003769 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003770 /* don't call wait_return() now */
3771 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003772 cmdline_row = msg_row - 1;
3773 need_wait_return = FALSE;
3774 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003775 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003776 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003777 else
3778 cmdline_row = save_cmdline_row;
3779 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003780#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003781 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003782 setmouse();
3783#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003785 return i;
3786}
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003789msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790{
3791 long pn;
3792
3793 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3795 return;
3796
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003797 /* We don't want to overwrite another important message, but do overwrite
3798 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3799 * then "put" reports the last action. */
3800 if (keep_msg != NULL && !keep_msg_more)
3801 return;
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (n > 0)
3804 pn = n;
3805 else
3806 pn = -n;
3807
3808 if (pn > p_report)
3809 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003810 if (n > 0)
3811 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3812 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003814 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3815 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003817 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (msg(msg_buf))
3819 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003820 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003821 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 }
3824}
3825
3826/*
3827 * flush map and typeahead buffers and give a warning for an error
3828 */
3829 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003830beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832 if (emsg_silent == 0)
3833 {
3834 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003835 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837}
3838
3839/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003840 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 */
3842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003843vim_beep(
3844 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003846#ifdef FEAT_EVAL
3847 called_vim_beep = TRUE;
3848#endif
3849
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (emsg_silent == 0)
3851 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003852 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3853 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003854#ifdef ELAPSED_FUNC
3855 static int did_init = FALSE;
3856 static ELAPSED_TYPE start_tv;
3857
3858 /* Only beep once per half a second, otherwise a sequence of beeps
3859 * would freeze Vim. */
3860 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3861 {
3862 did_init = TRUE;
3863 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003865 if (p_vb
3866#ifdef FEAT_GUI
3867 /* While the GUI is starting up the termcap is set for
3868 * the GUI but the output still goes to a terminal. */
3869 && !(gui.in_use && gui.starting)
3870#endif
3871 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003872 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003873 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003874#ifdef FEAT_VTP
3875 /* No restore color information, refresh the screen. */
3876 if (has_vtp_working() != 0
3877# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003878 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003879# endif
3880 )
3881 {
3882 redraw_later(CLEAR);
3883 update_screen(0);
3884 redrawcmd();
3885 }
3886#endif
3887 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003888 else
3889 out_char(BELL);
3890#ifdef ELAPSED_FUNC
3891 }
3892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003894
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003895 /* When 'debug' contains "beep" produce a message. If we are sourcing
3896 * a script or executing a function give the user a hint where the beep
3897 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003898 if (vim_strchr(p_debug, 'e') != NULL)
3899 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003900 msg_source(HL_ATTR(HLF_W));
3901 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904}
3905
3906/*
3907 * To get the "real" home directory:
3908 * - get value of $HOME
3909 * For Unix:
3910 * - go to that directory
3911 * - do mch_dirname() to get the real name of that directory.
3912 * This also works with mounts and links.
3913 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3914 */
3915static char_u *homedir = NULL;
3916
3917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003918init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
3920 char_u *var;
3921
Bram Moolenaar05159a02005-02-26 23:04:13 +00003922 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003923 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#ifdef VMS
3926 var = mch_getenv((char_u *)"SYS$LOGIN");
3927#else
3928 var = mch_getenv((char_u *)"HOME");
3929#endif
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#ifdef WIN3264
3932 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003933 * Typically, $HOME is not defined on Windows, unless the user has
3934 * specifically defined it for Vim's sake. However, on Windows NT
3935 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3936 * each user. Try constructing $HOME from these.
3937 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003938 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003939 {
3940 char_u *homedrive, *homepath;
3941
3942 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3943 homepath = mch_getenv((char_u *)"HOMEPATH");
3944 if (homepath == NULL || *homepath == NUL)
3945 homepath = (char_u *)"\\";
3946 if (homedrive != NULL
3947 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3948 {
3949 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3950 if (NameBuff[0] != NUL)
3951 var = NameBuff;
3952 }
3953 }
3954
3955 if (var == NULL)
3956 var = mch_getenv((char_u *)"USERPROFILE");
3957
3958 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 * Weird but true: $HOME may contain an indirect reference to another
3960 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3961 * when $HOME is being set.
3962 */
3963 if (var != NULL && *var == '%')
3964 {
3965 char_u *p;
3966 char_u *exp;
3967
3968 p = vim_strchr(var + 1, '%');
3969 if (p != NULL)
3970 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003971 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 exp = mch_getenv(NameBuff);
3973 if (exp != NULL && *exp != NUL
3974 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3975 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003976 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
3979 }
3980 }
3981
Bram Moolenaar48340b62017-08-29 22:08:53 +02003982 if (var != NULL && *var == NUL) /* empty is same as not set */
3983 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaar48340b62017-08-29 22:08:53 +02003985# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003986 if (enc_utf8 && var != NULL)
3987 {
3988 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003989 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003990
3991 /* Convert from active codepage to UTF-8. Other conversions are
3992 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003993 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003994 if (pp != NULL)
3995 {
3996 homedir = pp;
3997 return;
3998 }
3999 }
4000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 /*
4003 * Default home dir is C:/
4004 * Best assumption we can make in such a situation.
4005 */
4006 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004007 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (var != NULL)
4011 {
4012#ifdef UNIX
4013 /*
4014 * Change to the directory and get the actual path. This resolves
4015 * links. Don't do it when we can't return.
4016 */
4017 if (mch_dirname(NameBuff, MAXPATHL) == OK
4018 && mch_chdir((char *)NameBuff) == 0)
4019 {
4020 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4021 var = IObuff;
4022 if (mch_chdir((char *)NameBuff) != 0)
4023 EMSG(_(e_prev_dir));
4024 }
4025#endif
4026 homedir = vim_strsave(var);
4027 }
4028}
4029
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004030#if defined(EXITFREE) || defined(PROTO)
4031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004032free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004033{
4034 vim_free(homedir);
4035}
Bram Moolenaar24305862012-08-15 14:05:05 +02004036
4037# ifdef FEAT_CMDL_COMPL
4038 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004039free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004040{
4041 ga_clear_strings(&ga_users);
4042}
4043# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004044#endif
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004047 * Call expand_env() and store the result in an allocated string.
4048 * This is not very memory efficient, this expects the result to be freed
4049 * again soon.
4050 */
4051 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004052expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004053{
4054 return expand_env_save_opt(src, FALSE);
4055}
4056
4057/*
4058 * Idem, but when "one" is TRUE handle the string as one file name, only
4059 * expand "~" at the start.
4060 */
4061 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004062expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004063{
4064 char_u *p;
4065
4066 p = alloc(MAXPATHL);
4067 if (p != NULL)
4068 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4069 return p;
4070}
4071
4072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 * Expand environment variable with path name.
4074 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004075 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 * If anything fails no expansion is done and dst equals src.
4077 */
4078 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004079expand_env(
4080 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4081 char_u *dst, /* where to put the result */
4082 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004084 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085}
4086
4087 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004088expand_env_esc(
4089 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4090 char_u *dst, /* where to put the result */
4091 int dstlen, /* maximum length of the result */
4092 int esc, /* escape spaces in expanded variables */
4093 int one, /* "srcp" is one file name */
4094 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004096 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 char_u *tail;
4098 int c;
4099 char_u *var;
4100 int copy_char;
4101 int mustfree; /* var was allocated, need to free it later */
4102 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004103 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004105 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004106 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004107
4108 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 --dstlen; /* leave one char space for "\," */
4110 while (*src && dstlen > 0)
4111 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004112#ifdef FEAT_EVAL
4113 /* Skip over `=expr`. */
4114 if (src[0] == '`' && src[1] == '=')
4115 {
4116 size_t len;
4117
4118 var = src;
4119 src += 2;
4120 (void)skip_expr(&src);
4121 if (*src == '`')
4122 ++src;
4123 len = src - var;
4124 if (len > (size_t)dstlen)
4125 len = dstlen;
4126 vim_strncpy(dst, var, len);
4127 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004128 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004129 continue;
4130 }
4131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004133 if ((*src == '$'
4134#ifdef VMS
4135 && at_start
4136#endif
4137 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004138#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 || *src == '%'
4140#endif
4141 || (*src == '~' && at_start))
4142 {
4143 mustfree = FALSE;
4144
4145 /*
4146 * The variable name is copied into dst temporarily, because it may
4147 * be a string in read-only memory and a NUL needs to be appended.
4148 */
4149 if (*src != '~') /* environment var */
4150 {
4151 tail = src + 1;
4152 var = dst;
4153 c = dstlen - 1;
4154
4155#ifdef UNIX
4156 /* Unix has ${var-name} type environment vars */
4157 if (*tail == '{' && !vim_isIDc('{'))
4158 {
4159 tail++; /* ignore '{' */
4160 while (c-- > 0 && *tail && *tail != '}')
4161 *var++ = *tail++;
4162 }
4163 else
4164#endif
4165 {
4166 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004167#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 || (*src == '%' && *tail != '%')
4169#endif
4170 ))
4171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 }
4174 }
4175
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004176#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177# ifdef UNIX
4178 if (src[1] == '{' && *tail != '}')
4179# else
4180 if (*src == '%' && *tail != '%')
4181# endif
4182 var = NULL;
4183 else
4184 {
4185# ifdef UNIX
4186 if (src[1] == '{')
4187# else
4188 if (*src == '%')
4189#endif
4190 ++tail;
4191#endif
4192 *var = NUL;
4193 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004194#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196#endif
4197 }
4198 /* home directory */
4199 else if ( src[1] == NUL
4200 || vim_ispathsep(src[1])
4201 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4202 {
4203 var = homedir;
4204 tail = src + 1;
4205 }
4206 else /* user directory */
4207 {
4208#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4209 /*
4210 * Copy ~user to dst[], so we can put a NUL after it.
4211 */
4212 tail = src;
4213 var = dst;
4214 c = dstlen - 1;
4215 while ( c-- > 0
4216 && *tail
4217 && vim_isfilec(*tail)
4218 && !vim_ispathsep(*tail))
4219 *var++ = *tail++;
4220 *var = NUL;
4221# ifdef UNIX
4222 /*
4223 * If the system supports getpwnam(), use it.
4224 * Otherwise, or if getpwnam() fails, the shell is used to
4225 * expand ~user. This is slower and may fail if the shell
4226 * does not support ~user (old versions of /bin/sh).
4227 */
4228# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4229 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004230 /* Note: memory allocated by getpwnam() is never freed.
4231 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004232 struct passwd *pw = (*dst == NUL)
4233 ? NULL : getpwnam((char *)dst + 1);
4234
4235 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 if (var == NULL)
4238# endif
4239 {
4240 expand_T xpc;
4241
4242 ExpandInit(&xpc);
4243 xpc.xp_context = EXPAND_FILES;
4244 var = ExpandOne(&xpc, dst, NULL,
4245 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 mustfree = TRUE;
4247 }
4248
4249# else /* !UNIX, thus VMS */
4250 /*
4251 * USER_HOME is a comma-separated list of
4252 * directories to search for the user account in.
4253 */
4254 {
4255 char_u test[MAXPATHL], paths[MAXPATHL];
4256 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004257 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 STRCPY(paths, USER_HOME);
4260 next_path = paths;
4261 while (*next_path)
4262 {
4263 for (path = next_path; *next_path && *next_path != ',';
4264 next_path++);
4265 if (*next_path)
4266 *next_path++ = NUL;
4267 STRCPY(test, path);
4268 STRCAT(test, "/");
4269 STRCAT(test, dst + 1);
4270 if (mch_stat(test, &st) == 0)
4271 {
4272 var = alloc(STRLEN(test) + 1);
4273 STRCPY(var, test);
4274 mustfree = TRUE;
4275 break;
4276 }
4277 }
4278 }
4279# endif /* UNIX */
4280#else
4281 /* cannot expand user's home directory, so don't try */
4282 var = NULL;
4283 tail = (char_u *)""; /* for gcc */
4284#endif /* UNIX || VMS */
4285 }
4286
4287#ifdef BACKSLASH_IN_FILENAME
4288 /* If 'shellslash' is set change backslashes to forward slashes.
4289 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4290 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4291 {
4292 char_u *p = vim_strsave(var);
4293
4294 if (p != NULL)
4295 {
4296 if (mustfree)
4297 vim_free(var);
4298 var = p;
4299 mustfree = TRUE;
4300 forward_slash(var);
4301 }
4302 }
4303#endif
4304
4305 /* If "var" contains white space, escape it with a backslash.
4306 * Required for ":e ~/tt" when $HOME includes a space. */
4307 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4308 {
4309 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4310
4311 if (p != NULL)
4312 {
4313 if (mustfree)
4314 vim_free(var);
4315 var = p;
4316 mustfree = TRUE;
4317 }
4318 }
4319
4320 if (var != NULL && *var != NUL
4321 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4322 {
4323 STRCPY(dst, var);
4324 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004325 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 /* if var[] ends in a path separator and tail[] starts
4327 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004328 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4330 && dst[-1] != ':'
4331#endif
4332 && vim_ispathsep(*tail))
4333 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004334 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 src = tail;
4336 copy_char = FALSE;
4337 }
4338 if (mustfree)
4339 vim_free(var);
4340 }
4341
4342 if (copy_char) /* copy at least one char */
4343 {
4344 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004345 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004346 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4347 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 */
4349 at_start = FALSE;
4350 if (src[0] == '\\' && src[1] != NUL)
4351 {
4352 *dst++ = *src++;
4353 --dstlen;
4354 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004355 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004357 if (dstlen > 0)
4358 {
4359 *dst++ = *src++;
4360 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004361
Bram Moolenaar1c864092017-08-06 18:15:45 +02004362 if (startstr != NULL && src - startstr_len >= srcp
4363 && STRNCMP(src - startstr_len, startstr,
4364 startstr_len) == 0)
4365 at_start = TRUE;
4366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004368
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 *dst = NUL;
4371}
4372
4373/*
4374 * Vim's version of getenv().
4375 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004376 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004377 * "mustfree" is set to TRUE when returned is allocated, it must be
4378 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 */
4380 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004381vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
4383 char_u *p;
4384 char_u *pend;
4385 int vimruntime;
4386
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004387#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 /* use "C:/" when $HOME is not set */
4389 if (STRCMP(name, "HOME") == 0)
4390 return homedir;
4391#endif
4392
4393 p = mch_getenv(name);
4394 if (p != NULL && *p == NUL) /* empty is the same as not set */
4395 p = NULL;
4396
4397 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004398 {
4399#if defined(FEAT_MBYTE) && defined(WIN3264)
4400 if (enc_utf8)
4401 {
4402 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004403 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004404
4405 /* Convert from active codepage to UTF-8. Other conversions are
4406 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004407 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004408 if (pp != NULL)
4409 {
4410 p = pp;
4411 *mustfree = TRUE;
4412 }
4413 }
4414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4419 if (!vimruntime && STRCMP(name, "VIM") != 0)
4420 return NULL;
4421
4422 /*
4423 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4424 * Don't do this when default_vimruntime_dir is non-empty.
4425 */
4426 if (vimruntime
4427#ifdef HAVE_PATHDEF
4428 && *default_vimruntime_dir == NUL
4429#endif
4430 )
4431 {
4432 p = mch_getenv((char_u *)"VIM");
4433 if (p != NULL && *p == NUL) /* empty is the same as not set */
4434 p = NULL;
4435 if (p != NULL)
4436 {
4437 p = vim_version_dir(p);
4438 if (p != NULL)
4439 *mustfree = TRUE;
4440 else
4441 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004442
4443#if defined(FEAT_MBYTE) && defined(WIN3264)
4444 if (enc_utf8)
4445 {
4446 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004447 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004448
4449 /* Convert from active codepage to UTF-8. Other conversions
4450 * are not done, because they would fail for non-ASCII
4451 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004452 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004453 if (pp != NULL)
4454 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004455 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004456 vim_free(p);
4457 p = pp;
4458 *mustfree = TRUE;
4459 }
4460 }
4461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 }
4464
4465 /*
4466 * When expanding $VIM or $VIMRUNTIME fails, try using:
4467 * - the directory name from 'helpfile' (unless it contains '$')
4468 * - the executable name from argv[0]
4469 */
4470 if (p == NULL)
4471 {
4472 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4473 p = p_hf;
4474#ifdef USE_EXE_NAME
4475 /*
4476 * Use the name of the executable, obtained from argv[0].
4477 */
4478 else
4479 p = exe_name;
4480#endif
4481 if (p != NULL)
4482 {
4483 /* remove the file name */
4484 pend = gettail(p);
4485
4486 /* remove "doc/" from 'helpfile', if present */
4487 if (p == p_hf)
4488 pend = remove_tail(p, pend, (char_u *)"doc");
4489
4490#ifdef USE_EXE_NAME
4491# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004492 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (p == exe_name)
4494 {
4495 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004496 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004498 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4499 if (pend1 != pend)
4500 {
4501 pnew = alloc((unsigned)(pend1 - p) + 15);
4502 if (pnew != NULL)
4503 {
4504 STRNCPY(pnew, p, (pend1 - p));
4505 STRCPY(pnew + (pend1 - p), "Resources/vim");
4506 p = pnew;
4507 pend = p + STRLEN(p);
4508 }
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511# endif
4512 /* remove "src/" from exe_name, if present */
4513 if (p == exe_name)
4514 pend = remove_tail(p, pend, (char_u *)"src");
4515#endif
4516
4517 /* for $VIM, remove "runtime/" or "vim54/", if present */
4518 if (!vimruntime)
4519 {
4520 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4521 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4522 }
4523
4524 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004525 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004528#ifdef MACOS_X
4529 if (p == exe_name || p == p_hf)
4530#endif
4531 /* check that the result is a directory name */
4532 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004535 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 else
4537 {
4538#ifdef USE_EXE_NAME
4539 /* may add "/vim54" or "/runtime" if it exists */
4540 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4541 {
4542 vim_free(p);
4543 p = pend;
4544 }
4545#endif
4546 *mustfree = TRUE;
4547 }
4548 }
4549 }
4550
4551#ifdef HAVE_PATHDEF
4552 /* When there is a pathdef.c file we can use default_vim_dir and
4553 * default_vimruntime_dir */
4554 if (p == NULL)
4555 {
4556 /* Only use default_vimruntime_dir when it is not empty */
4557 if (vimruntime && *default_vimruntime_dir != NUL)
4558 {
4559 p = default_vimruntime_dir;
4560 *mustfree = FALSE;
4561 }
4562 else if (*default_vim_dir != NUL)
4563 {
4564 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4565 *mustfree = TRUE;
4566 else
4567 {
4568 p = default_vim_dir;
4569 *mustfree = FALSE;
4570 }
4571 }
4572 }
4573#endif
4574
4575 /*
4576 * Set the environment variable, so that the new value can be found fast
4577 * next time, and others can also use it (e.g. Perl).
4578 */
4579 if (p != NULL)
4580 {
4581 if (vimruntime)
4582 {
4583 vim_setenv((char_u *)"VIMRUNTIME", p);
4584 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 else
4587 {
4588 vim_setenv((char_u *)"VIM", p);
4589 didset_vim = TRUE;
4590 }
4591 }
4592 return p;
4593}
4594
4595/*
4596 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4597 * Return NULL if not, return its name in allocated memory otherwise.
4598 */
4599 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004600vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601{
4602 char_u *p;
4603
4604 if (vimdir == NULL || *vimdir == NUL)
4605 return NULL;
4606 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4607 if (p != NULL && mch_isdir(p))
4608 return p;
4609 vim_free(p);
4610 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4611 if (p != NULL && mch_isdir(p))
4612 return p;
4613 vim_free(p);
4614 return NULL;
4615}
4616
4617/*
4618 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4619 * the length of "name/". Otherwise return "pend".
4620 */
4621 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004622remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 int len = (int)STRLEN(name) + 1;
4625 char_u *newend = pend - len;
4626
4627 if (newend >= p
4628 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004629 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 return newend;
4631 return pend;
4632}
4633
Bram Moolenaar137374f2018-05-13 15:59:50 +02004634 void
4635vim_unsetenv(char_u *var)
4636{
4637#ifdef HAVE_UNSETENV
4638 unsetenv((char *)var);
4639#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004640 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004641#endif
4642}
4643
4644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 * Our portable version of setenv.
4647 */
4648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004649vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650{
4651#ifdef HAVE_SETENV
4652 mch_setenv((char *)name, (char *)val, 1);
4653#else
4654 char_u *envbuf;
4655
4656 /*
4657 * Putenv does not copy the string, it has to remain
4658 * valid. The allocated memory will never be freed.
4659 */
4660 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4661 if (envbuf != NULL)
4662 {
4663 sprintf((char *)envbuf, "%s=%s", name, val);
4664 putenv((char *)envbuf);
4665 }
4666#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004667#ifdef FEAT_GETTEXT
4668 /*
4669 * When setting $VIMRUNTIME adjust the directory to find message
4670 * translations to $VIMRUNTIME/lang.
4671 */
4672 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4673 {
4674 char_u *buf = concat_str(val, (char_u *)"/lang");
4675
4676 if (buf != NULL)
4677 {
4678 bindtextdomain(VIMPACKAGE, (char *)buf);
4679 vim_free(buf);
4680 }
4681 }
4682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683}
4684
4685#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4686/*
4687 * Function given to ExpandGeneric() to obtain an environment variable name.
4688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004690get_env_name(
4691 expand_T *xp UNUSED,
4692 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
Bram Moolenaard0573012017-10-28 21:11:06 +02004694# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004696 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 */
4698 return NULL;
4699# else
4700# ifndef __WIN32__
4701 /* Borland C++ 5.2 has this in a header file. */
4702 extern char **environ;
4703# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004704# define ENVNAMELEN 100
4705 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 char_u *str;
4707 int n;
4708
4709 str = (char_u *)environ[idx];
4710 if (str == NULL)
4711 return NULL;
4712
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004713 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
4715 if (str[n] == '=' || str[n] == NUL)
4716 break;
4717 name[n] = str[n];
4718 }
4719 name[n] = NUL;
4720 return name;
4721# endif
4722}
Bram Moolenaar24305862012-08-15 14:05:05 +02004723
4724/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004725 * Add a user name to the list of users in ga_users.
4726 * Do nothing if user name is NULL or empty.
4727 */
4728 static void
4729add_user(char_u *user, int need_copy)
4730{
4731 char_u *user_copy = (user != NULL && need_copy)
4732 ? vim_strsave(user) : user;
4733
4734 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4735 {
4736 if (need_copy)
4737 vim_free(user);
4738 return;
4739 }
4740 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4741}
4742
4743/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004744 * Find all user names for user completion.
4745 * Done only once and then cached.
4746 */
4747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004748init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004749{
Bram Moolenaar24305862012-08-15 14:05:05 +02004750 static int lazy_init_done = FALSE;
4751
4752 if (lazy_init_done)
4753 return;
4754
4755 lazy_init_done = TRUE;
4756 ga_init2(&ga_users, sizeof(char_u *), 20);
4757
4758# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4759 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004760 struct passwd* pw;
4761
4762 setpwent();
4763 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004764 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004765 endpwent();
4766 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004767# elif defined(WIN3264)
4768 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004769 DWORD nusers = 0, ntotal = 0, i;
4770 PUSER_INFO_0 uinfo;
4771
4772 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4773 &nusers, &ntotal, NULL) == NERR_Success)
4774 {
4775 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004776 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004777
4778 NetApiBufferFree(uinfo);
4779 }
4780 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004781# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004782# if defined(HAVE_GETPWNAM)
4783 {
4784 char_u *user_env = mch_getenv((char_u *)"USER");
4785
4786 // The $USER environment variable may be a valid remote user name (NIS,
4787 // LDAP) not already listed by getpwent(), as getpwent() only lists
4788 // local user names. If $USER is not already listed, check whether it
4789 // is a valid remote user name using getpwnam() and if it is, add it to
4790 // the list of user names.
4791
4792 if (user_env != NULL && *user_env != NUL)
4793 {
4794 int i;
4795
4796 for (i = 0; i < ga_users.ga_len; i++)
4797 {
4798 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4799
4800 if (STRCMP(local_user, user_env) == 0)
4801 break;
4802 }
4803
4804 if (i == ga_users.ga_len)
4805 {
4806 struct passwd *pw = getpwnam((char *)user_env);
4807
4808 if (pw != NULL)
4809 add_user((char_u *)pw->pw_name, TRUE);
4810 }
4811 }
4812 }
4813# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004814}
4815
4816/*
4817 * Function given to ExpandGeneric() to obtain an user names.
4818 */
4819 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004820get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004821{
4822 init_users();
4823 if (idx < ga_users.ga_len)
4824 return ((char_u **)ga_users.ga_data)[idx];
4825 return NULL;
4826}
4827
4828/*
4829 * Check whether name matches a user name. Return:
4830 * 0 if name does not match any user name.
4831 * 1 if name partially matches the beginning of a user name.
4832 * 2 is name fully matches a user name.
4833 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004834 int
4835match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004836{
4837 int i;
4838 int n = (int)STRLEN(name);
4839 int result = 0;
4840
4841 init_users();
4842 for (i = 0; i < ga_users.ga_len; i++)
4843 {
4844 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4845 return 2; /* full match */
4846 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4847 result = 1; /* partial match */
4848 }
4849 return result;
4850}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#endif
4852
4853/*
4854 * Replace home directory by "~" in each space or comma separated file name in
4855 * 'src'.
4856 * If anything fails (except when out of space) dst equals src.
4857 */
4858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004859home_replace(
4860 buf_T *buf, /* when not NULL, check for help files */
4861 char_u *src, /* input file name */
4862 char_u *dst, /* where to put the result */
4863 int dstlen, /* maximum length of the result */
4864 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 spaces and commas in the file name. */
4866{
4867 size_t dirlen = 0, envlen = 0;
4868 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004869 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 char_u *p;
4871
4872 if (src == NULL)
4873 {
4874 *dst = NUL;
4875 return;
4876 }
4877
4878 /*
4879 * If the file is a help file, remove the path completely.
4880 */
4881 if (buf != NULL && buf->b_help)
4882 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004883 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 return;
4885 }
4886
4887 /*
4888 * We check both the value of the $HOME environment variable and the
4889 * "real" home directory.
4890 */
4891 if (homedir != NULL)
4892 dirlen = STRLEN(homedir);
4893
4894#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004895 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004897 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4898#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004899#ifdef WIN3264
4900 if (homedir_env == NULL)
4901 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4902#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004903 /* Empty is the same as not set. */
4904 if (homedir_env != NULL && *homedir_env == NUL)
4905 homedir_env = NULL;
4906
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004907#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004908 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004909 {
4910 int usedlen = 0;
4911 int flen;
4912 char_u *fbuf = NULL;
4913
4914 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004915 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004916 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004917 flen = (int)STRLEN(homedir_env);
4918 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4919 /* Remove the trailing / that is added to a directory. */
4920 homedir_env[flen - 1] = NUL;
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922#endif
4923
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 if (homedir_env != NULL)
4925 envlen = STRLEN(homedir_env);
4926
4927 if (!one)
4928 src = skipwhite(src);
4929 while (*src && dstlen > 0)
4930 {
4931 /*
4932 * Here we are at the beginning of a file name.
4933 * First, check to see if the beginning of the file name matches
4934 * $HOME or the "real" home directory. Check that there is a '/'
4935 * after the match (so that if e.g. the file is "/home/pieter/bla",
4936 * and the home directory is "/home/piet", the file does not end up
4937 * as "~er/bla" (which would seem to indicate the file "bla" in user
4938 * er's home directory)).
4939 */
4940 p = homedir;
4941 len = dirlen;
4942 for (;;)
4943 {
4944 if ( len
4945 && fnamencmp(src, p, len) == 0
4946 && (vim_ispathsep(src[len])
4947 || (!one && (src[len] == ',' || src[len] == ' '))
4948 || src[len] == NUL))
4949 {
4950 src += len;
4951 if (--dstlen > 0)
4952 *dst++ = '~';
4953
4954 /*
4955 * If it's just the home directory, add "/".
4956 */
4957 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4958 *dst++ = '/';
4959 break;
4960 }
4961 if (p == homedir_env)
4962 break;
4963 p = homedir_env;
4964 len = envlen;
4965 }
4966
4967 /* if (!one) skip to separator: space or comma */
4968 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4969 *dst++ = *src++;
4970 /* skip separator */
4971 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4972 *dst++ = *src++;
4973 }
4974 /* if (dstlen == 0) out of space, what to do??? */
4975
4976 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004977
4978 if (homedir_env != homedir_env_orig)
4979 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980}
4981
4982/*
4983 * Like home_replace, store the replaced string in allocated memory.
4984 * When something fails, NULL is returned.
4985 */
4986 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004987home_replace_save(
4988 buf_T *buf, /* when not NULL, check for help files */
4989 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
4991 char_u *dst;
4992 unsigned len;
4993
4994 len = 3; /* space for "~/" and trailing NUL */
4995 if (src != NULL) /* just in case */
4996 len += (unsigned)STRLEN(src);
4997 dst = alloc(len);
4998 if (dst != NULL)
4999 home_replace(buf, src, dst, len, TRUE);
5000 return dst;
5001}
5002
5003/*
5004 * Compare two file names and return:
5005 * FPC_SAME if they both exist and are the same file.
5006 * FPC_SAMEX if they both don't exist and have the same file name.
5007 * FPC_DIFF if they both exist and are different files.
5008 * FPC_NOTX if they both don't exist.
5009 * FPC_DIFFX if one of them doesn't exist.
5010 * For the first name environment variables are expanded
5011 */
5012 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005013fullpathcmp(
5014 char_u *s1,
5015 char_u *s2,
5016 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
5018#ifdef UNIX
5019 char_u exp1[MAXPATHL];
5020 char_u full1[MAXPATHL];
5021 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005022 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 int r1, r2;
5024
5025 expand_env(s1, exp1, MAXPATHL);
5026 r1 = mch_stat((char *)exp1, &st1);
5027 r2 = mch_stat((char *)s2, &st2);
5028 if (r1 != 0 && r2 != 0)
5029 {
5030 /* if mch_stat() doesn't work, may compare the names */
5031 if (checkname)
5032 {
5033 if (fnamecmp(exp1, s2) == 0)
5034 return FPC_SAMEX;
5035 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5036 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5037 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5038 return FPC_SAMEX;
5039 }
5040 return FPC_NOTX;
5041 }
5042 if (r1 != 0 || r2 != 0)
5043 return FPC_DIFFX;
5044 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5045 return FPC_SAME;
5046 return FPC_DIFF;
5047#else
5048 char_u *exp1; /* expanded s1 */
5049 char_u *full1; /* full path of s1 */
5050 char_u *full2; /* full path of s2 */
5051 int retval = FPC_DIFF;
5052 int r1, r2;
5053
5054 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5055 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5056 {
5057 full1 = exp1 + MAXPATHL;
5058 full2 = full1 + MAXPATHL;
5059
5060 expand_env(s1, exp1, MAXPATHL);
5061 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5062 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5063
5064 /* If vim_FullName() fails, the file probably doesn't exist. */
5065 if (r1 != OK && r2 != OK)
5066 {
5067 if (checkname && fnamecmp(exp1, s2) == 0)
5068 retval = FPC_SAMEX;
5069 else
5070 retval = FPC_NOTX;
5071 }
5072 else if (r1 != OK || r2 != OK)
5073 retval = FPC_DIFFX;
5074 else if (fnamecmp(full1, full2))
5075 retval = FPC_DIFF;
5076 else
5077 retval = FPC_SAME;
5078 vim_free(exp1);
5079 }
5080 return retval;
5081#endif
5082}
5083
5084/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005085 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005086 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005087 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 */
5089 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005090gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
5092 char_u *p1, *p2;
5093
5094 if (fname == NULL)
5095 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005096 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005098 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005100 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102 return p1;
5103}
5104
Bram Moolenaar31710262010-08-13 13:36:15 +02005105#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005106static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02005107
5108/*
5109 * Return the end of the directory name, on the first path
5110 * separator:
5111 * "/path/file", "/path/dir/", "/path//dir", "/file"
5112 * ^ ^ ^ ^
5113 */
5114 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005115gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005116{
5117 char_u *dir_end = fname;
5118 char_u *next_dir_end = fname;
5119 int look_for_sep = TRUE;
5120 char_u *p;
5121
5122 for (p = fname; *p != NUL; )
5123 {
5124 if (vim_ispathsep(*p))
5125 {
5126 if (look_for_sep)
5127 {
5128 next_dir_end = p;
5129 look_for_sep = FALSE;
5130 }
5131 }
5132 else
5133 {
5134 if (!look_for_sep)
5135 dir_end = next_dir_end;
5136 look_for_sep = TRUE;
5137 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005138 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005139 }
5140 return dir_end;
5141}
5142#endif
5143
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005145 * Get pointer to tail of "fname", including path separators. Putting a NUL
5146 * here leaves the directory name. Takes care of "c:/" and "//".
5147 * Always returns a valid pointer.
5148 */
5149 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005150gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005151{
5152 char_u *p;
5153 char_u *t;
5154
5155 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5156 t = gettail(fname);
5157 while (t > p && after_pathsep(fname, t))
5158 --t;
5159#ifdef VMS
5160 /* path separator is part of the path */
5161 ++t;
5162#endif
5163 return t;
5164}
5165
5166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 * get the next path component (just after the next path separator).
5168 */
5169 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005173 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (*fname)
5175 ++fname;
5176 return fname;
5177}
5178
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179/*
5180 * Get a pointer to one character past the head of a path name.
5181 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5182 * If there is no head, path is returned.
5183 */
5184 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005185get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 char_u *retval;
5188
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005189#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 /* may skip "c:" */
5191 if (isalpha(path[0]) && path[1] == ':')
5192 retval = path + 2;
5193 else
5194 retval = path;
5195#else
5196# if defined(AMIGA)
5197 /* may skip "label:" */
5198 retval = vim_strchr(path, ':');
5199 if (retval == NULL)
5200 retval = path;
5201# else /* Unix */
5202 retval = path;
5203# endif
5204#endif
5205
5206 while (vim_ispathsep(*retval))
5207 ++retval;
5208
5209 return retval;
5210}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
5212/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005213 * Return TRUE if 'c' is a path separator.
5214 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 */
5216 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005217vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005219#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005221#else
5222# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005224# else
5225# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5227 return (c == ':' || c == '[' || c == ']' || c == '/'
5228 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005229# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005231# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234}
5235
Bram Moolenaar69c35002013-11-04 02:54:12 +01005236/*
5237 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5238 */
5239 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005240vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005241{
5242 return vim_ispathsep(c)
5243#ifdef BACKSLASH_IN_FILENAME
5244 && c != ':'
5245#endif
5246 ;
5247}
5248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5250/*
5251 * return TRUE if 'c' is a path list separator.
5252 */
5253 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005254vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256#ifdef UNIX
5257 return (c == ':');
5258#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005259 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#endif
5261}
5262#endif
5263
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005264/*
5265 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5266 * It's done in-place.
5267 */
5268 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005269shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005270{
5271 char_u *tail, *s, *d;
5272 int skip = FALSE;
5273
5274 tail = gettail(str);
5275 d = str;
5276 for (s = str; ; ++s)
5277 {
5278 if (s >= tail) /* copy the whole tail */
5279 {
5280 *d++ = *s;
5281 if (*s == NUL)
5282 break;
5283 }
5284 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5285 {
5286 *d++ = *s;
5287 skip = FALSE;
5288 }
5289 else if (!skip)
5290 {
5291 *d++ = *s; /* copy next char */
5292 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5293 skip = TRUE;
5294# ifdef FEAT_MBYTE
5295 if (has_mbyte)
5296 {
5297 int l = mb_ptr2len(s);
5298
5299 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005300 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005301 }
5302# endif
5303 }
5304 }
5305}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005306
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005307/*
5308 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5309 * Also returns TRUE if there is no directory name.
5310 * "fname" must be writable!.
5311 */
5312 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005313dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005314{
5315 char_u *p;
5316 int c;
5317 int retval;
5318
5319 p = gettail_sep(fname);
5320 if (p == fname)
5321 return TRUE;
5322 c = *p;
5323 *p = NUL;
5324 retval = mch_isdir(fname);
5325 *p = c;
5326 return retval;
5327}
5328
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005330 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5331 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 */
5333 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005334vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005336#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005338#else
5339 if (p_fic)
5340 return MB_STRICMP(x, y);
5341 return STRCMP(x, y);
5342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343}
5344
5345 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005346vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005348#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005349 char_u *px = x;
5350 char_u *py = y;
5351 int cx = NUL;
5352 int cy = NUL;
5353
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005354 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005356 cx = PTR2CHAR(px);
5357 cy = PTR2CHAR(py);
5358 if (cx == NUL || cy == NUL
5359 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5360 && !(cx == '/' && cy == '\\')
5361 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005363 len -= MB_PTR2LEN(px);
5364 px += MB_PTR2LEN(px);
5365 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367 if (len == 0)
5368 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005369 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005370#else
5371 if (p_fic)
5372 return MB_STRNICMP(x, y, len);
5373 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005375}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377/*
5378 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005379 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 */
5381 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005382concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383{
5384 char_u *dest;
5385
5386 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5387 if (dest != NULL)
5388 {
5389 STRCPY(dest, fname1);
5390 if (sep)
5391 add_pathsep(dest);
5392 STRCAT(dest, fname2);
5393 }
5394 return dest;
5395}
5396
Bram Moolenaard6754642005-01-17 22:18:45 +00005397/*
5398 * Concatenate two strings and return the result in allocated memory.
5399 * Returns NULL when out of memory.
5400 */
5401 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005402concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005403{
5404 char_u *dest;
5405 size_t l = STRLEN(str1);
5406
5407 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5408 if (dest != NULL)
5409 {
5410 STRCPY(dest, str1);
5411 STRCPY(dest + l, str2);
5412 }
5413 return dest;
5414}
Bram Moolenaard6754642005-01-17 22:18:45 +00005415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416/*
5417 * Add a path separator to a file name, unless it already ends in a path
5418 * separator.
5419 */
5420 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005421add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005423 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 STRCAT(p, PATHSEPSTR);
5425}
5426
5427/*
5428 * FullName_save - Make an allocated copy of a full file name.
5429 * Returns NULL when out of memory.
5430 */
5431 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005432FullName_save(
5433 char_u *fname,
5434 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005435 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 char_u *buf;
5438 char_u *new_fname = NULL;
5439
5440 if (fname == NULL)
5441 return NULL;
5442
5443 buf = alloc((unsigned)MAXPATHL);
5444 if (buf != NULL)
5445 {
5446 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5447 new_fname = vim_strsave(buf);
5448 else
5449 new_fname = vim_strsave(fname);
5450 vim_free(buf);
5451 }
5452 return new_fname;
5453}
5454
5455#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5456
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005457static char_u *skip_string(char_u *p);
5458static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005459static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005460static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462/*
5463 * Find the start of a comment, not knowing if we are in a comment right now.
5464 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005465 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005467 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005468ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005469{
5470 return find_start_comment(curbuf->b_ind_maxcomment);
5471}
5472
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 pos_T *pos;
5477 char_u *line;
5478 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005479 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005481 for (;;)
5482 {
5483 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5484 if (pos == NULL)
5485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005487 /*
5488 * Check if the comment start we found is inside a string.
5489 * If it is then restrict the search to below this line and try again.
5490 */
5491 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005492 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005493 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005494 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005495 break;
5496 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5497 if (cur_maxcomment <= 0)
5498 {
5499 pos = NULL;
5500 break;
5501 }
5502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 return pos;
5504}
5505
5506/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005507 * Find the start of a comment or raw string, not knowing if we are in a
5508 * comment or raw string right now.
5509 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005510 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005511 * Return NULL when not inside a comment or raw string.
5512 * "CORS" -> Comment Or Raw String
5513 */
5514 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005515ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005516{
Bram Moolenaar089af182015-10-07 11:41:49 +02005517 static pos_T comment_pos_copy;
5518 pos_T *comment_pos;
5519 pos_T *rs_pos;
5520
5521 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5522 if (comment_pos != NULL)
5523 {
5524 /* Need to make a copy of the static pos in findmatchlimit(),
5525 * calling find_start_rawstring() may change it. */
5526 comment_pos_copy = *comment_pos;
5527 comment_pos = &comment_pos_copy;
5528 }
5529 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005530
5531 /* If comment_pos is before rs_pos the raw string is inside the comment.
5532 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005533 if (comment_pos == NULL || (rs_pos != NULL
5534 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005535 {
5536 if (is_raw != NULL && rs_pos != NULL)
5537 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005538 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005539 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005540 return comment_pos;
5541}
5542
5543/*
5544 * Find the start of a raw string, not knowing if we are in one right now.
5545 * Search starts at w_cursor.lnum and goes backwards.
5546 * Return NULL when not inside a raw string.
5547 */
5548 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005549find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005550{
5551 pos_T *pos;
5552 char_u *line;
5553 char_u *p;
5554 int cur_maxcomment = ind_maxcomment;
5555
5556 for (;;)
5557 {
5558 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5559 if (pos == NULL)
5560 break;
5561
5562 /*
5563 * Check if the raw string start we found is inside a string.
5564 * If it is then restrict the search to below this line and try again.
5565 */
5566 line = ml_get(pos->lnum);
5567 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5568 p = skip_string(p);
5569 if ((colnr_T)(p - line) <= pos->col)
5570 break;
5571 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5572 if (cur_maxcomment <= 0)
5573 {
5574 pos = NULL;
5575 break;
5576 }
5577 }
5578 return pos;
5579}
5580
5581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 * Skip to the end of a "string" and a 'c' character.
5583 * If there is no string or character, return argument unmodified.
5584 */
5585 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005586skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 int i;
5589
5590 /*
5591 * We loop, because strings may be concatenated: "date""time".
5592 */
5593 for ( ; ; ++p)
5594 {
5595 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5596 {
5597 if (!p[1]) /* ' at end of line */
5598 break;
5599 i = 2;
5600 if (p[1] == '\\') /* '\n' or '\000' */
5601 {
5602 ++i;
5603 while (vim_isdigit(p[i - 1])) /* '\000' */
5604 ++i;
5605 }
5606 if (p[i] == '\'') /* check for trailing ' */
5607 {
5608 p += i;
5609 continue;
5610 }
5611 }
5612 else if (p[0] == '"') /* start of string */
5613 {
5614 for (++p; p[0]; ++p)
5615 {
5616 if (p[0] == '\\' && p[1] != NUL)
5617 ++p;
5618 else if (p[0] == '"') /* end of string */
5619 break;
5620 }
5621 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005622 continue; /* continue for another string */
5623 }
5624 else if (p[0] == 'R' && p[1] == '"')
5625 {
5626 /* Raw string: R"[delim](...)[delim]" */
5627 char_u *delim = p + 2;
5628 char_u *paren = vim_strchr(delim, '(');
5629
5630 if (paren != NULL)
5631 {
5632 size_t delim_len = paren - delim;
5633
5634 for (p += 3; *p; ++p)
5635 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5636 && p[delim_len + 1] == '"')
5637 {
5638 p += delim_len + 1;
5639 break;
5640 }
5641 if (p[0] == '"')
5642 continue; /* continue for another string */
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645 break; /* no string found */
5646 }
5647 if (!*p)
5648 --p; /* backup from NUL */
5649 return p;
5650}
5651#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5652
5653#if defined(FEAT_CINDENT) || defined(PROTO)
5654
5655/*
5656 * Do C or expression indenting on the current line.
5657 */
5658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005659do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661# ifdef FEAT_EVAL
5662 if (*curbuf->b_p_inde != NUL)
5663 fixthisline(get_expr_indent);
5664 else
5665# endif
5666 fixthisline(get_c_indent);
5667}
5668
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005669/* Find result cache for cpp_baseclass */
5670typedef struct {
5671 int found;
5672 lpos_T lpos;
5673} cpp_baseclass_cache_T;
5674
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675/*
5676 * Functions for C-indenting.
5677 * Most of this originally comes from Eric Fischer.
5678 */
5679/*
5680 * Below "XXX" means that this function may unlock the current line.
5681 */
5682
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005683static char_u *cin_skipcomment(char_u *);
5684static int cin_nocode(char_u *);
5685static pos_T *find_line_comment(void);
5686static int cin_has_js_key(char_u *text);
5687static int cin_islabel_skip(char_u **);
5688static int cin_isdefault(char_u *);
5689static char_u *after_label(char_u *l);
5690static int get_indent_nolabel(linenr_T lnum);
5691static int skip_label(linenr_T, char_u **pp);
5692static int cin_first_id_amount(void);
5693static int cin_get_equal_amount(linenr_T lnum);
5694static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005695static int cin_iscomment(char_u *);
5696static int cin_islinecomment(char_u *);
5697static int cin_isterminated(char_u *, int, int);
5698static int cin_isinit(void);
5699static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5700static int cin_isif(char_u *);
5701static int cin_iselse(char_u *);
5702static int cin_isdo(char_u *);
5703static int cin_iswhileofdo(char_u *, linenr_T);
5704static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5705static int cin_iswhileofdo_end(int terminated);
5706static int cin_isbreak(char_u *);
5707static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5708static int get_baseclass_amount(int col);
5709static int cin_ends_in(char_u *, char_u *, char_u *);
5710static int cin_starts_with(char_u *s, char *word);
5711static int cin_skip2pos(pos_T *trypos);
5712static pos_T *find_start_brace(void);
5713static pos_T *find_match_paren(int);
5714static pos_T *find_match_char(int c, int ind_maxparen);
5715static int corr_ind_maxparen(pos_T *startpos);
5716static int find_last_paren(char_u *l, int start, int end);
5717static int find_match(int lookfor, linenr_T ourscope);
5718static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720/*
5721 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005722 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 */
5724 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005725cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 while (*s)
5728 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005729 char_u *prev_s = s;
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005732
5733 /* Perl/shell # comment comment continues until eol. Require a space
5734 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005735 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005736 {
5737 s += STRLEN(s);
5738 break;
5739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (*s != '/')
5741 break;
5742 ++s;
5743 if (*s == '/') /* slash-slash comment continues till eol */
5744 {
5745 s += STRLEN(s);
5746 break;
5747 }
5748 if (*s != '*')
5749 break;
5750 for (++s; *s; ++s) /* skip slash-star comment */
5751 if (s[0] == '*' && s[1] == '/')
5752 {
5753 s += 2;
5754 break;
5755 }
5756 }
5757 return s;
5758}
5759
5760/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005761 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 * not considered code.
5763 */
5764 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005765cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 return *cin_skipcomment(s) == NUL;
5768}
5769
5770/*
5771 * Check previous lines for a "//" line comment, skipping over blank lines.
5772 */
5773 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005774find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 static pos_T pos;
5777 char_u *line;
5778 char_u *p;
5779
5780 pos = curwin->w_cursor;
5781 while (--pos.lnum > 0)
5782 {
5783 line = ml_get(pos.lnum);
5784 p = skipwhite(line);
5785 if (cin_islinecomment(p))
5786 {
5787 pos.col = (int)(p - line);
5788 return &pos;
5789 }
5790 if (*p != NUL)
5791 break;
5792 }
5793 return NULL;
5794}
5795
5796/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005797 * Return TRUE if "text" starts with "key:".
5798 */
5799 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005800cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005801{
5802 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005803 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005804
5805 if (*s == '\'' || *s == '"')
5806 {
5807 /* can be 'key': or "key": */
5808 quote = *s;
5809 ++s;
5810 }
5811 if (!vim_isIDc(*s)) /* need at least one ID character */
5812 return FALSE;
5813
5814 while (vim_isIDc(*s))
5815 ++s;
5816 if (*s == quote)
5817 ++s;
5818
5819 s = cin_skipcomment(s);
5820
5821 /* "::" is not a label, it's C++ */
5822 return (*s == ':' && s[1] != ':');
5823}
5824
5825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005827 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 */
5829 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005830cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 if (!vim_isIDc(**s)) /* need at least one ID character */
5833 return FALSE;
5834
5835 while (vim_isIDc(**s))
5836 (*s)++;
5837
5838 *s = cin_skipcomment(*s);
5839
5840 /* "::" is not a label, it's C++ */
5841 return (**s == ':' && *++*s != ':');
5842}
5843
5844/*
5845 * Recognize a label: "label:".
5846 * Note: curwin->w_cursor must be where we are looking for the label.
5847 */
5848 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005849cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
5851 char_u *s;
5852
5853 s = cin_skipcomment(ml_get_curline());
5854
5855 /*
5856 * Exclude "default" from labels, since it should be indented
5857 * like a switch label. Same for C++ scope declarations.
5858 */
5859 if (cin_isdefault(s))
5860 return FALSE;
5861 if (cin_isscopedecl(s))
5862 return FALSE;
5863
5864 if (cin_islabel_skip(&s))
5865 {
5866 /*
5867 * Only accept a label if the previous line is terminated or is a case
5868 * label.
5869 */
5870 pos_T cursor_save;
5871 pos_T *trypos;
5872 char_u *line;
5873
5874 cursor_save = curwin->w_cursor;
5875 while (curwin->w_cursor.lnum > 1)
5876 {
5877 --curwin->w_cursor.lnum;
5878
5879 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005880 * If we're in a comment or raw string now, skip to the start of
5881 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 */
5883 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005884 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 curwin->w_cursor = *trypos;
5886
5887 line = ml_get_curline();
5888 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5889 continue;
5890 if (*(line = cin_skipcomment(line)) == NUL)
5891 continue;
5892
5893 curwin->w_cursor = cursor_save;
5894 if (cin_isterminated(line, TRUE, FALSE)
5895 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005896 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 || (cin_islabel_skip(&line) && cin_nocode(line)))
5898 return TRUE;
5899 return FALSE;
5900 }
5901 curwin->w_cursor = cursor_save;
5902 return TRUE; /* label at start of file??? */
5903 }
5904 return FALSE;
5905}
5906
5907/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005908 * Recognize structure initialization and enumerations:
5909 * "[typedef] [static|public|protected|private] enum"
5910 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 */
5912 static int
5913cin_isinit(void)
5914{
5915 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005916 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918 s = cin_skipcomment(ml_get_curline());
5919
Bram Moolenaar75342212013-03-07 13:13:52 +01005920 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 s = cin_skipcomment(s + 7);
5922
Bram Moolenaar75342212013-03-07 13:13:52 +01005923 for (;;)
5924 {
5925 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005926
Bram Moolenaar75342212013-03-07 13:13:52 +01005927 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5928 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005929 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005930 if (cin_starts_with(s, skip[i]))
5931 {
5932 s = cin_skipcomment(s + l);
5933 l = 0;
5934 break;
5935 }
5936 }
5937 if (l != 0)
5938 break;
5939 }
5940
5941 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 return TRUE;
5943
5944 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5945 return TRUE;
5946
5947 return FALSE;
5948}
5949
5950/*
5951 * Recognize a switch label: "case .*:" or "default:".
5952 */
5953 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005954cin_iscase(
5955 char_u *s,
5956 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957{
5958 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005959 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
5961 for (s += 4; *s; ++s)
5962 {
5963 s = cin_skipcomment(s);
5964 if (*s == ':')
5965 {
5966 if (s[1] == ':') /* skip over "::" for C++ */
5967 ++s;
5968 else
5969 return TRUE;
5970 }
5971 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005972 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5974 return FALSE; /* stop at comment */
5975 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005976 {
5977 /* JS etc. */
5978 if (strict)
5979 return FALSE; /* stop at string */
5980 else
5981 return TRUE;
5982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 }
5984 return FALSE;
5985 }
5986
5987 if (cin_isdefault(s))
5988 return TRUE;
5989 return FALSE;
5990}
5991
5992/*
5993 * Recognize a "default" switch label.
5994 */
5995 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005996cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 return (STRNCMP(s, "default", 7) == 0
5999 && *(s = cin_skipcomment(s + 7)) == ':'
6000 && s[1] != ':');
6001}
6002
6003/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006004 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 */
6006 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006007cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 int i;
6010
6011 s = cin_skipcomment(s);
6012 if (STRNCMP(s, "public", 6) == 0)
6013 i = 6;
6014 else if (STRNCMP(s, "protected", 9) == 0)
6015 i = 9;
6016 else if (STRNCMP(s, "private", 7) == 0)
6017 i = 7;
6018 else
6019 return FALSE;
6020 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6021}
6022
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006023/* Maximum number of lines to search back for a "namespace" line. */
6024#define FIND_NAMESPACE_LIM 20
6025
6026/*
6027 * Recognize a "namespace" scope declaration.
6028 */
6029 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006030cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006031{
6032 char_u *p;
6033 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006034 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006035
6036 s = cin_skipcomment(s);
6037 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6038 {
6039 p = cin_skipcomment(skipwhite(s + 9));
6040 while (*p != NUL)
6041 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006042 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006043 {
6044 has_name = TRUE; /* found end of a name */
6045 p = cin_skipcomment(skipwhite(p));
6046 }
6047 else if (*p == '{')
6048 {
6049 break;
6050 }
6051 else if (vim_iswordc(*p))
6052 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006053 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006054 if (has_name)
6055 return FALSE; /* word character after skipping past name */
6056 ++p;
6057 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006058 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6059 {
6060 if (!has_name_start || has_name)
6061 return FALSE;
6062 /* C++ 17 nested namespace */
6063 p += 3;
6064 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006065 else
6066 {
6067 return FALSE;
6068 }
6069 }
6070 return TRUE;
6071 }
6072 return FALSE;
6073}
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006076 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6077 */
6078 static int
6079cin_is_cpp_extern_c(char_u *s)
6080{
6081 char_u *p;
6082 int has_string_literal = FALSE;
6083
6084 s = cin_skipcomment(s);
6085 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6086 {
6087 p = cin_skipcomment(skipwhite(s + 6));
6088 while (*p != NUL)
6089 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006090 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006091 {
6092 p = cin_skipcomment(skipwhite(p));
6093 }
6094 else if (*p == '{')
6095 {
6096 break;
6097 }
6098 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6099 {
6100 if (has_string_literal)
6101 return FALSE;
6102 has_string_literal = TRUE;
6103 p += 3;
6104 }
6105 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6106 && p[4] == '"')
6107 {
6108 if (has_string_literal)
6109 return FALSE;
6110 has_string_literal = TRUE;
6111 p += 5;
6112 }
6113 else
6114 {
6115 return FALSE;
6116 }
6117 }
6118 return has_string_literal ? TRUE : FALSE;
6119 }
6120 return FALSE;
6121}
6122
6123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 * Return a pointer to the first non-empty non-comment character after a ':'.
6125 * Return NULL if not found.
6126 * case 234: a = b;
6127 * ^
6128 */
6129 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006130after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 for ( ; *l; ++l)
6133 {
6134 if (*l == ':')
6135 {
6136 if (l[1] == ':') /* skip over "::" for C++ */
6137 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006138 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 break;
6140 }
6141 else if (*l == '\'' && l[1] && l[2] == '\'')
6142 l += 2; /* skip over 'x' */
6143 }
6144 if (*l == NUL)
6145 return NULL;
6146 l = cin_skipcomment(l + 1);
6147 if (*l == NUL)
6148 return NULL;
6149 return l;
6150}
6151
6152/*
6153 * Get indent of line "lnum", skipping a label.
6154 * Return 0 if there is nothing after the label.
6155 */
6156 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006157get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 char_u *l;
6160 pos_T fp;
6161 colnr_T col;
6162 char_u *p;
6163
6164 l = ml_get(lnum);
6165 p = after_label(l);
6166 if (p == NULL)
6167 return 0;
6168
6169 fp.col = (colnr_T)(p - l);
6170 fp.lnum = lnum;
6171 getvcol(curwin, &fp, &col, NULL, NULL);
6172 return (int)col;
6173}
6174
6175/*
6176 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006177 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 * label: if (asdf && asdfasdf)
6179 * ^
6180 */
6181 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006182skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
6184 char_u *l;
6185 int amount;
6186 pos_T cursor_save;
6187
6188 cursor_save = curwin->w_cursor;
6189 curwin->w_cursor.lnum = lnum;
6190 l = ml_get_curline();
6191 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006192 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
6194 amount = get_indent_nolabel(lnum);
6195 l = after_label(ml_get_curline());
6196 if (l == NULL) /* just in case */
6197 l = ml_get_curline();
6198 }
6199 else
6200 {
6201 amount = get_indent();
6202 l = ml_get_curline();
6203 }
6204 *pp = l;
6205
6206 curwin->w_cursor = cursor_save;
6207 return amount;
6208}
6209
6210/*
6211 * Return the indent of the first variable name after a type in a declaration.
6212 * int a, indent of "a"
6213 * static struct foo b, indent of "b"
6214 * enum bla c, indent of "c"
6215 * Returns zero when it doesn't look like a declaration.
6216 */
6217 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006218cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
6220 char_u *line, *p, *s;
6221 int len;
6222 pos_T fp;
6223 colnr_T col;
6224
6225 line = ml_get_curline();
6226 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006227 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6229 {
6230 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006231 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
6233 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6234 p = skipwhite(p + 6);
6235 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6236 p = skipwhite(p + 4);
6237 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6238 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6239 {
6240 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006241 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6242 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6243 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6244 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 p = s;
6246 }
6247 for (len = 0; vim_isIDc(p[len]); ++len)
6248 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006249 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 return 0;
6251
6252 p = skipwhite(p + len);
6253 fp.lnum = curwin->w_cursor.lnum;
6254 fp.col = (colnr_T)(p - line);
6255 getvcol(curwin, &fp, &col, NULL, NULL);
6256 return (int)col;
6257}
6258
6259/*
6260 * Return the indent of the first non-blank after an equal sign.
6261 * char *foo = "here";
6262 * Return zero if no (useful) equal sign found.
6263 * Return -1 if the line above "lnum" ends in a backslash.
6264 * foo = "asdf\
6265 * asdf\
6266 * here";
6267 */
6268 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006269cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 char_u *line;
6272 char_u *s;
6273 colnr_T col;
6274 pos_T fp;
6275
6276 if (lnum > 1)
6277 {
6278 line = ml_get(lnum - 1);
6279 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6280 return -1;
6281 }
6282
6283 line = s = ml_get(lnum);
6284 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6285 {
6286 if (cin_iscomment(s)) /* ignore comments */
6287 s = cin_skipcomment(s);
6288 else
6289 ++s;
6290 }
6291 if (*s != '=')
6292 return 0;
6293
6294 s = skipwhite(s + 1);
6295 if (cin_nocode(s))
6296 return 0;
6297
6298 if (*s == '"') /* nice alignment for continued strings */
6299 ++s;
6300
6301 fp.lnum = lnum;
6302 fp.col = (colnr_T)(s - line);
6303 getvcol(curwin, &fp, &col, NULL, NULL);
6304 return (int)col;
6305}
6306
6307/*
6308 * Recognize a preprocessor statement: Any line that starts with '#'.
6309 */
6310 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006311cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006313 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 return TRUE;
6315 return FALSE;
6316}
6317
6318/*
6319 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6320 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6321 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006322 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 */
6324 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006325cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 char_u *line = *pp;
6328 linenr_T lnum = *lnump;
6329 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006330 int candidate_amount = *amount;
6331
6332 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6333 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006335 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 {
6337 if (cin_ispreproc(line))
6338 {
6339 retval = TRUE;
6340 *lnump = lnum;
6341 break;
6342 }
6343 if (lnum == 1)
6344 break;
6345 line = ml_get(--lnum);
6346 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6347 break;
6348 }
6349
6350 if (lnum != *lnump)
6351 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006352 if (retval)
6353 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 return retval;
6355}
6356
6357/*
6358 * Recognize the start of a C or C++ comment.
6359 */
6360 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006361cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6364}
6365
6366/*
6367 * Recognize the start of a "//" comment.
6368 */
6369 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006370cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 return (p[0] == '/' && p[1] == '/');
6373}
6374
6375/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006376 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6377 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006379 * If a line begins with an "else", only consider it terminated if no unmatched
6380 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 * Return the character terminating the line (ending char's have precedence if
6382 * both apply in order to determine initializations).
6383 */
6384 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006385cin_isterminated(
6386 char_u *s,
6387 int incl_open, /* include '{' at the end as terminator */
6388 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006390 char_u found_start = 0;
6391 unsigned n_open = 0;
6392 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
6394 s = cin_skipcomment(s);
6395
6396 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6397 found_start = *s;
6398
Bram Moolenaar496f9512011-05-19 16:35:09 +02006399 if (!found_start)
6400 is_else = cin_iselse(s);
6401
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 while (*s)
6403 {
6404 /* skip over comments, "" strings and 'c'haracters */
6405 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006406 if (*s == '}' && n_open > 0)
6407 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006408 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006409 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 && cin_nocode(s + 1))
6411 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006412 else if (*s == '{')
6413 {
6414 if (incl_open && cin_nocode(s + 1))
6415 return *s;
6416 else
6417 ++n_open;
6418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 if (*s)
6421 s++;
6422 }
6423 return found_start;
6424}
6425
6426/*
6427 * Recognize the basic picture of a function declaration -- it needs to
6428 * have an open paren somewhere and a close paren at the end of the line and
6429 * no semicolons anywhere.
6430 * When a line ends in a comma we continue looking in the next line.
6431 * "sp" points to a string with the line. When looking at other lines it must
6432 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006433 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006434 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 */
6436 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006437cin_isfuncdecl(
6438 char_u **sp,
6439 linenr_T first_lnum,
6440 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 char_u *s;
6443 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006444 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006446 pos_T *trypos;
6447 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448
6449 if (sp == NULL)
6450 s = ml_get(lnum);
6451 else
6452 s = *sp;
6453
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006454 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006455 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006456 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006457 {
6458 lnum = trypos->lnum;
6459 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006460 {
6461 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006462 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006463 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006464
6465 s = ml_get(lnum);
6466 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006467 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006468
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006469 /* Ignore line starting with #. */
6470 if (cin_ispreproc(s))
6471 return FALSE;
6472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6474 {
6475 if (cin_iscomment(s)) /* ignore comments */
6476 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006477 else if (*s == ':')
6478 {
6479 if (*(s + 1) == ':')
6480 s += 2;
6481 else
6482 /* To avoid a mistake in the following situation:
6483 * A::A(int a, int b)
6484 * : a(0) // <--not a function decl
6485 * , b(0)
6486 * {...
6487 */
6488 return FALSE;
6489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 else
6491 ++s;
6492 }
6493 if (*s != '(')
6494 return FALSE; /* ';', ' or " before any () or no '(' */
6495
6496 while (*s && *s != ';' && *s != '\'' && *s != '"')
6497 {
6498 if (*s == ')' && cin_nocode(s + 1))
6499 {
6500 /* ')' at the end: may have found a match
6501 * Check for he previous line not to end in a backslash:
6502 * #if defined(x) && \
6503 * defined(y)
6504 */
6505 lnum = first_lnum - 1;
6506 s = ml_get(lnum);
6507 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6508 retval = TRUE;
6509 goto done;
6510 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006511 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006513 int comma = (*s == ',');
6514
6515 /* ',' at the end: continue looking in the next line.
6516 * At the end: check for ',' in the next line, for this style:
6517 * func(arg1
6518 * , arg2) */
6519 for (;;)
6520 {
6521 if (lnum >= curbuf->b_ml.ml_line_count)
6522 break;
6523 s = ml_get(++lnum);
6524 if (!cin_ispreproc(s))
6525 break;
6526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (lnum >= curbuf->b_ml.ml_line_count)
6528 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006529 /* Require a comma at end of the line or a comma or ')' at the
6530 * start of next line. */
6531 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006532 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006533 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006534 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
6536 else if (cin_iscomment(s)) /* ignore comments */
6537 s = cin_skipcomment(s);
6538 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006541 just_started = FALSE;
6542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
6544
6545done:
6546 if (lnum != first_lnum && sp != NULL)
6547 *sp = ml_get(first_lnum);
6548
6549 return retval;
6550}
6551
6552 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006553cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006555 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556}
6557
6558 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006559cin_iselse(
6560 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 if (*p == '}') /* accept "} else" */
6563 p = cin_skipcomment(p + 1);
6564 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6565}
6566
6567 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006568cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6571}
6572
6573/*
6574 * Check if this is a "while" that should have a matching "do".
6575 * We only accept a "while (condition) ;", with only white space between the
6576 * ')' and ';'. The condition may be spread over several lines.
6577 */
6578 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006579cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
6581 pos_T cursor_save;
6582 pos_T *trypos;
6583 int retval = FALSE;
6584
6585 p = cin_skipcomment(p);
6586 if (*p == '}') /* accept "} while (cond);" */
6587 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006588 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {
6590 cursor_save = curwin->w_cursor;
6591 curwin->w_cursor.lnum = lnum;
6592 curwin->w_cursor.col = 0;
6593 p = ml_get_curline();
6594 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6595 {
6596 ++p;
6597 ++curwin->w_cursor.col;
6598 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006599 if ((trypos = findmatchlimit(NULL, 0, 0,
6600 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6602 retval = TRUE;
6603 curwin->w_cursor = cursor_save;
6604 }
6605 return retval;
6606}
6607
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006608/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006609 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006610 * Return 0 if there is none.
6611 * Otherwise return !0 and update "*poffset" to point to the place where the
6612 * string was found.
6613 */
6614 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006615cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006616{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006617 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006618
6619 if (offset-- < 2)
6620 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006621 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006622 --offset;
6623
6624 offset -= 1;
6625 if (!STRNCMP(line + offset, "if", 2))
6626 goto probablyFound;
6627
6628 if (offset >= 1)
6629 {
6630 offset -= 1;
6631 if (!STRNCMP(line + offset, "for", 3))
6632 goto probablyFound;
6633
6634 if (offset >= 2)
6635 {
6636 offset -= 2;
6637 if (!STRNCMP(line + offset, "while", 5))
6638 goto probablyFound;
6639 }
6640 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006641 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006642
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006643probablyFound:
6644 if (!offset || !vim_isIDc(line[offset - 1]))
6645 {
6646 *poffset = offset;
6647 return 1;
6648 }
6649 return 0;
6650}
6651
6652/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006653 * Return TRUE if we are at the end of a do-while.
6654 * do
6655 * nothing;
6656 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006657 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006658 * Adjust the cursor to the line with "while".
6659 */
6660 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006661cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006662{
6663 char_u *line;
6664 char_u *p;
6665 char_u *s;
6666 pos_T *trypos;
6667 int i;
6668
6669 if (terminated != ';') /* there must be a ';' at the end */
6670 return FALSE;
6671
6672 p = line = ml_get_curline();
6673 while (*p != NUL)
6674 {
6675 p = cin_skipcomment(p);
6676 if (*p == ')')
6677 {
6678 s = skipwhite(p + 1);
6679 if (*s == ';' && cin_nocode(s + 1))
6680 {
6681 /* Found ");" at end of the line, now check there is "while"
6682 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006683 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006684 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006685 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006686 if (trypos != NULL)
6687 {
6688 s = cin_skipcomment(ml_get(trypos->lnum));
6689 if (*s == '}') /* accept "} while (cond);" */
6690 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006691 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006692 {
6693 curwin->w_cursor.lnum = trypos->lnum;
6694 return TRUE;
6695 }
6696 }
6697
6698 /* Searching may have made "line" invalid, get it again. */
6699 line = ml_get_curline();
6700 p = line + i;
6701 }
6702 }
6703 if (*p != NUL)
6704 ++p;
6705 }
6706 return FALSE;
6707}
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006710cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6713}
6714
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006715/*
6716 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 * constructor-initialization. eg:
6718 *
6719 * class MyClass :
6720 * baseClass <-- here
6721 * class MyClass : public baseClass,
6722 * anotherBaseClass <-- here (should probably lineup ??)
6723 * MyClass::MyClass(...) :
6724 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006725 *
6726 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 */
6728 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006729cin_is_cpp_baseclass(
6730 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006732 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 char_u *s;
6734 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006735 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006736 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006738 if (pos->lnum <= lnum)
6739 return cached->found; /* Use the cached result */
6740
6741 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006743 s = skipwhite(line);
6744 if (*s == '#') /* skip #define FOO x ? (x) : x */
6745 return FALSE;
6746 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if (*s == NUL)
6748 return FALSE;
6749
6750 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6751
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006752 /* Search for a line starting with '#', empty, ending in ';' or containing
6753 * '{' or '}' and start below it. This handles the following situations:
6754 * a = cond ?
6755 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006756 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006757 * func::foo()
6758 * : something
6759 * {}
6760 * Foo::Foo (int one, int two)
6761 * : something(4),
6762 * somethingelse(3)
6763 * {}
6764 */
6765 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006767 line = ml_get(lnum - 1);
6768 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006769 if (*s == '#' || *s == NUL)
6770 break;
6771 while (*s != NUL)
6772 {
6773 s = cin_skipcomment(s);
6774 if (*s == '{' || *s == '}'
6775 || (*s == ';' && cin_nocode(s + 1)))
6776 break;
6777 if (*s != NUL)
6778 ++s;
6779 }
6780 if (*s != NUL)
6781 break;
6782 --lnum;
6783 }
6784
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006785 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006786 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006787 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006788 for (;;)
6789 {
6790 if (*s == NUL)
6791 {
6792 if (lnum == curwin->w_cursor.lnum)
6793 break;
6794 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006795 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006796 s = line;
6797 }
6798 if (s == line)
6799 {
6800 /* don't recognize "case (foo):" as a baseclass */
6801 if (cin_iscase(s, FALSE))
6802 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006803 s = cin_skipcomment(line);
6804 if (*s == NUL)
6805 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006806 }
6807
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006808 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006809 s = skip_string(s) + 1;
6810 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
6812 if (s[1] == ':')
6813 {
6814 /* skip double colon. It can't be a constructor
6815 * initialization any more */
6816 lookfor_ctor_init = FALSE;
6817 s = cin_skipcomment(s + 2);
6818 }
6819 else if (lookfor_ctor_init || class_or_struct)
6820 {
6821 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006822 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 cpp_base_class = TRUE;
6824 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006825 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 s = cin_skipcomment(s + 1);
6827 }
6828 else
6829 s = cin_skipcomment(s + 1);
6830 }
6831 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6832 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6833 {
6834 class_or_struct = TRUE;
6835 lookfor_ctor_init = FALSE;
6836
6837 if (*s == 'c')
6838 s = cin_skipcomment(s + 5);
6839 else
6840 s = cin_skipcomment(s + 6);
6841 }
6842 else
6843 {
6844 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6845 {
6846 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6847 }
6848 else if (s[0] == ')')
6849 {
6850 /* Constructor-initialization is assumed if we come across
6851 * something like "):" */
6852 class_or_struct = FALSE;
6853 lookfor_ctor_init = TRUE;
6854 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006855 else if (s[0] == '?')
6856 {
6857 /* Avoid seeing '() :' after '?' as constructor init. */
6858 return FALSE;
6859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 else if (!vim_isIDc(s[0]))
6861 {
6862 /* if it is not an identifier, we are wrong */
6863 class_or_struct = FALSE;
6864 lookfor_ctor_init = FALSE;
6865 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006866 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 {
6868 /* it can't be a constructor-initialization any more */
6869 lookfor_ctor_init = FALSE;
6870
6871 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006872 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006873 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006876 /* When the line ends in a comma don't align with it. */
6877 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006878 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 s = cin_skipcomment(s + 1);
6881 }
6882 }
6883
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006884 cached->found = cpp_base_class;
6885 if (cpp_base_class)
6886 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 return cpp_base_class;
6888}
6889
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006890 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006891get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006892{
6893 int amount;
6894 colnr_T vcol;
6895 pos_T *trypos;
6896
6897 if (col == 0)
6898 {
6899 amount = get_indent();
6900 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006901 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006902 amount = get_indent_lnum(trypos->lnum); /* XXX */
6903 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006904 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006905 }
6906 else
6907 {
6908 curwin->w_cursor.col = col;
6909 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6910 amount = (int)vcol;
6911 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006912 if (amount < curbuf->b_ind_cpp_baseclass)
6913 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006914 return amount;
6915}
6916
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917/*
6918 * Return TRUE if string "s" ends with the string "find", possibly followed by
6919 * white space and comments. Skip strings and comments.
6920 * Ignore "ignore" after "find" if it's not NULL.
6921 */
6922 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006923cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 char_u *p = s;
6926 char_u *r;
6927 int len = (int)STRLEN(find);
6928
6929 while (*p != NUL)
6930 {
6931 p = cin_skipcomment(p);
6932 if (STRNCMP(p, find, len) == 0)
6933 {
6934 r = skipwhite(p + len);
6935 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6936 r = skipwhite(r + STRLEN(ignore));
6937 if (cin_nocode(r))
6938 return TRUE;
6939 }
6940 if (*p != NUL)
6941 ++p;
6942 }
6943 return FALSE;
6944}
6945
6946/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006947 * Return TRUE when "s" starts with "word" and then a non-ID character.
6948 */
6949 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006950cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006951{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006952 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006953
6954 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6955}
6956
6957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 * Skip strings, chars and comments until at or past "trypos".
6959 * Return the column found.
6960 */
6961 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006962cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
6964 char_u *line;
6965 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006966 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
6968 p = line = ml_get(trypos->lnum);
6969 while (*p && (colnr_T)(p - line) < trypos->col)
6970 {
6971 if (cin_iscomment(p))
6972 p = cin_skipcomment(p);
6973 else
6974 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006975 new_p = skip_string(p);
6976 if (new_p == p)
6977 ++p;
6978 else
6979 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 }
6981 }
6982 return (int)(p - line);
6983}
6984
6985/*
6986 * Find the '{' at the start of the block we are in.
6987 * Return NULL if no match found.
6988 * Ignore a '{' that is in a comment, makes indenting the next three lines
6989 * work. */
6990/* foo() */
6991/* { */
6992/* } */
6993
6994 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006995find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996{
6997 pos_T cursor_save;
6998 pos_T *trypos;
6999 pos_T *pos;
7000 static pos_T pos_copy;
7001
7002 cursor_save = curwin->w_cursor;
7003 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
7004 {
7005 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
7006 trypos = &pos_copy;
7007 curwin->w_cursor = *trypos;
7008 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007009 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02007011 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 break;
7013 if (pos != NULL)
7014 curwin->w_cursor.lnum = pos->lnum;
7015 }
7016 curwin->w_cursor = cursor_save;
7017 return trypos;
7018}
7019
7020/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007021 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007022 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 */
7024 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007025find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007027 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007028}
7029
7030 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007031find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007032{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 pos_T cursor_save;
7034 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007035 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007036 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007039 ind_maxp_wk = ind_maxparen;
7040retry:
7041 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {
7043 /* check if the ( is in a // comment */
7044 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007045 {
7046 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7047 if (ind_maxp_wk > 0)
7048 {
7049 curwin->w_cursor = *trypos;
7050 curwin->w_cursor.col = 0; /* XXX */
7051 goto retry;
7052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 else
7056 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007057 pos_T *trypos_wk;
7058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7060 trypos = &pos_copy;
7061 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007062 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007063 {
7064 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7065 - trypos_wk->lnum);
7066 if (ind_maxp_wk > 0)
7067 {
7068 curwin->w_cursor = *trypos_wk;
7069 goto retry;
7070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 }
7074 }
7075 curwin->w_cursor = cursor_save;
7076 return trypos;
7077}
7078
7079/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007080 * Find the matching '(', ignoring it if it is in a comment or before an
7081 * unmatched {.
7082 * Return NULL if no match found.
7083 */
7084 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007085find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007086{
7087 pos_T *trypos = find_match_paren(ind_maxparen);
7088
7089 if (trypos != NULL)
7090 {
7091 pos_T *tryposBrace = find_start_brace();
7092
7093 /* If both an unmatched '(' and '{' is found. Ignore the '('
7094 * position if the '{' is further down. */
7095 if (tryposBrace != NULL
7096 && (trypos->lnum != tryposBrace->lnum
7097 ? trypos->lnum < tryposBrace->lnum
7098 : trypos->col < tryposBrace->col))
7099 trypos = NULL;
7100 }
7101 return trypos;
7102}
7103
7104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 * Return ind_maxparen corrected for the difference in line number between the
7106 * cursor position and "startpos". This makes sure that searching for a
7107 * matching paren above the cursor line doesn't find a match because of
7108 * looking a few lines further.
7109 */
7110 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007111corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7114
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007115 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7116 return curbuf->b_ind_maxparen - (int)n;
7117 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118}
7119
7120/*
7121 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007122 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 */
7124 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007125find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 int i;
7128 int retval = FALSE;
7129 int open_count = 0;
7130
7131 curwin->w_cursor.col = 0; /* default is start of line */
7132
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007133 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
7135 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7136 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7137 if (l[i] == start)
7138 ++open_count;
7139 else if (l[i] == end)
7140 {
7141 if (open_count > 0)
7142 --open_count;
7143 else
7144 {
7145 curwin->w_cursor.col = i;
7146 retval = TRUE;
7147 }
7148 }
7149 }
7150 return retval;
7151}
7152
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007153/*
7154 * Parse 'cinoptions' and set the values in "curbuf".
7155 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7156 */
7157 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007158parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007159{
7160 char_u *p;
7161 char_u *l;
7162 char_u *digits;
7163 int n;
7164 int divider;
7165 int fraction = 0;
7166 int sw = (int)get_sw_value(buf);
7167
7168 /*
7169 * Set the default values.
7170 */
7171 /* Spaces from a block's opening brace the prevailing indent for that
7172 * block should be. */
7173 buf->b_ind_level = sw;
7174
7175 /* Spaces from the edge of the line an open brace that's at the end of a
7176 * line is imagined to be. */
7177 buf->b_ind_open_imag = 0;
7178
7179 /* Spaces from the prevailing indent for a line that is not preceded by
7180 * an opening brace. */
7181 buf->b_ind_no_brace = 0;
7182
7183 /* Column where the first { of a function should be located }. */
7184 buf->b_ind_first_open = 0;
7185
7186 /* Spaces from the prevailing indent a leftmost open brace should be
7187 * located. */
7188 buf->b_ind_open_extra = 0;
7189
7190 /* Spaces from the matching open brace (real location for one at the left
7191 * edge; imaginary location from one that ends a line) the matching close
7192 * brace should be located. */
7193 buf->b_ind_close_extra = 0;
7194
7195 /* Spaces from the edge of the line an open brace sitting in the leftmost
7196 * column is imagined to be. */
7197 buf->b_ind_open_left_imag = 0;
7198
7199 /* Spaces jump labels should be shifted to the left if N is non-negative,
7200 * otherwise the jump label will be put to column 1. */
7201 buf->b_ind_jump_label = -1;
7202
7203 /* Spaces from the switch() indent a "case xx" label should be located. */
7204 buf->b_ind_case = sw;
7205
7206 /* Spaces from the "case xx:" code after a switch() should be located. */
7207 buf->b_ind_case_code = sw;
7208
7209 /* Lineup break at end of case in switch() with case label. */
7210 buf->b_ind_case_break = 0;
7211
7212 /* Spaces from the class declaration indent a scope declaration label
7213 * should be located. */
7214 buf->b_ind_scopedecl = sw;
7215
7216 /* Spaces from the scope declaration label code should be located. */
7217 buf->b_ind_scopedecl_code = sw;
7218
7219 /* Amount K&R-style parameters should be indented. */
7220 buf->b_ind_param = sw;
7221
7222 /* Amount a function type spec should be indented. */
7223 buf->b_ind_func_type = sw;
7224
7225 /* Amount a cpp base class declaration or constructor initialization
7226 * should be indented. */
7227 buf->b_ind_cpp_baseclass = sw;
7228
7229 /* additional spaces beyond the prevailing indent a continuation line
7230 * should be located. */
7231 buf->b_ind_continuation = sw;
7232
7233 /* Spaces from the indent of the line with an unclosed parentheses. */
7234 buf->b_ind_unclosed = sw * 2;
7235
7236 /* Spaces from the indent of the line with an unclosed parentheses, which
7237 * itself is also unclosed. */
7238 buf->b_ind_unclosed2 = sw;
7239
7240 /* Suppress ignoring spaces from the indent of a line starting with an
7241 * unclosed parentheses. */
7242 buf->b_ind_unclosed_noignore = 0;
7243
7244 /* If the opening paren is the last nonwhite character on the line, and
7245 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7246 * context (for very long lines). */
7247 buf->b_ind_unclosed_wrapped = 0;
7248
7249 /* Suppress ignoring white space when lining up with the character after
7250 * an unclosed parentheses. */
7251 buf->b_ind_unclosed_whiteok = 0;
7252
7253 /* Indent a closing parentheses under the line start of the matching
7254 * opening parentheses. */
7255 buf->b_ind_matching_paren = 0;
7256
7257 /* Indent a closing parentheses under the previous line. */
7258 buf->b_ind_paren_prev = 0;
7259
7260 /* Extra indent for comments. */
7261 buf->b_ind_comment = 0;
7262
7263 /* Spaces from the comment opener when there is nothing after it. */
7264 buf->b_ind_in_comment = 3;
7265
7266 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7267 * after the comment opener. */
7268 buf->b_ind_in_comment2 = 0;
7269
7270 /* Max lines to search for an open paren. */
7271 buf->b_ind_maxparen = 20;
7272
7273 /* Max lines to search for an open comment. */
7274 buf->b_ind_maxcomment = 70;
7275
7276 /* Handle braces for java code. */
7277 buf->b_ind_java = 0;
7278
7279 /* Not to confuse JS object properties with labels. */
7280 buf->b_ind_js = 0;
7281
7282 /* Handle blocked cases correctly. */
7283 buf->b_ind_keep_case_label = 0;
7284
7285 /* Handle C++ namespace. */
7286 buf->b_ind_cpp_namespace = 0;
7287
7288 /* Handle continuation lines containing conditions of if(), for() and
7289 * while(). */
7290 buf->b_ind_if_for_while = 0;
7291
Bram Moolenaar6b643942017-03-05 19:44:06 +01007292 /* indentation for # comments */
7293 buf->b_ind_hash_comment = 0;
7294
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007295 /* Handle C++ extern "C" or "C++" */
7296 buf->b_ind_cpp_extern_c = 0;
7297
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007298 for (p = buf->b_p_cino; *p; )
7299 {
7300 l = p++;
7301 if (*p == '-')
7302 ++p;
7303 digits = p; /* remember where the digits start */
7304 n = getdigits(&p);
7305 divider = 0;
7306 if (*p == '.') /* ".5s" means a fraction */
7307 {
7308 fraction = atol((char *)++p);
7309 while (VIM_ISDIGIT(*p))
7310 {
7311 ++p;
7312 if (divider)
7313 divider *= 10;
7314 else
7315 divider = 10;
7316 }
7317 }
7318 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7319 {
7320 if (p == digits)
7321 n = sw; /* just "s" is one 'shiftwidth' */
7322 else
7323 {
7324 n *= sw;
7325 if (divider)
7326 n += (sw * fraction + divider / 2) / divider;
7327 }
7328 ++p;
7329 }
7330 if (l[1] == '-')
7331 n = -n;
7332
7333 /* When adding an entry here, also update the default 'cinoptions' in
7334 * doc/indent.txt, and add explanation for it! */
7335 switch (*l)
7336 {
7337 case '>': buf->b_ind_level = n; break;
7338 case 'e': buf->b_ind_open_imag = n; break;
7339 case 'n': buf->b_ind_no_brace = n; break;
7340 case 'f': buf->b_ind_first_open = n; break;
7341 case '{': buf->b_ind_open_extra = n; break;
7342 case '}': buf->b_ind_close_extra = n; break;
7343 case '^': buf->b_ind_open_left_imag = n; break;
7344 case 'L': buf->b_ind_jump_label = n; break;
7345 case ':': buf->b_ind_case = n; break;
7346 case '=': buf->b_ind_case_code = n; break;
7347 case 'b': buf->b_ind_case_break = n; break;
7348 case 'p': buf->b_ind_param = n; break;
7349 case 't': buf->b_ind_func_type = n; break;
7350 case '/': buf->b_ind_comment = n; break;
7351 case 'c': buf->b_ind_in_comment = n; break;
7352 case 'C': buf->b_ind_in_comment2 = n; break;
7353 case 'i': buf->b_ind_cpp_baseclass = n; break;
7354 case '+': buf->b_ind_continuation = n; break;
7355 case '(': buf->b_ind_unclosed = n; break;
7356 case 'u': buf->b_ind_unclosed2 = n; break;
7357 case 'U': buf->b_ind_unclosed_noignore = n; break;
7358 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7359 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7360 case 'm': buf->b_ind_matching_paren = n; break;
7361 case 'M': buf->b_ind_paren_prev = n; break;
7362 case ')': buf->b_ind_maxparen = n; break;
7363 case '*': buf->b_ind_maxcomment = n; break;
7364 case 'g': buf->b_ind_scopedecl = n; break;
7365 case 'h': buf->b_ind_scopedecl_code = n; break;
7366 case 'j': buf->b_ind_java = n; break;
7367 case 'J': buf->b_ind_js = n; break;
7368 case 'l': buf->b_ind_keep_case_label = n; break;
7369 case '#': buf->b_ind_hash_comment = n; break;
7370 case 'N': buf->b_ind_cpp_namespace = n; break;
7371 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007372 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007373 }
7374 if (*p == ',')
7375 ++p;
7376 }
7377}
7378
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007379/*
7380 * Return the desired indent for C code.
7381 * Return -1 if the indent should be left alone (inside a raw string).
7382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007384get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 pos_T cur_curpos;
7387 int amount;
7388 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007389 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 colnr_T col;
7391 char_u *theline;
7392 char_u *linecopy;
7393 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007394 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007396 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 pos_T our_paren_pos;
7398 char_u *start;
7399 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007400#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401#define BRACE_AT_START 2 /* '{' is at start of line */
7402#define BRACE_AT_END 3 /* '{' is at end of line */
7403 linenr_T ourscope;
7404 char_u *l;
7405 char_u *look;
7406 char_u terminated;
7407 int lookfor;
7408#define LOOKFOR_INITIAL 0
7409#define LOOKFOR_IF 1
7410#define LOOKFOR_DO 2
7411#define LOOKFOR_CASE 3
7412#define LOOKFOR_ANY 4
7413#define LOOKFOR_TERM 5
7414#define LOOKFOR_UNTERM 6
7415#define LOOKFOR_SCOPEDECL 7
7416#define LOOKFOR_NOBREAK 8
7417#define LOOKFOR_CPP_BASECLASS 9
7418#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007419#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007420#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421
7422 int whilelevel;
7423 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 int n;
7425 int iscase;
7426 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007427 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007429 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007430 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007431 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007432 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007433 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007435 /* make a copy, value is changed below */
7436 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
7438 /* remember where the cursor was when we started */
7439 cur_curpos = curwin->w_cursor;
7440
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007441 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007442 if (cur_curpos.lnum == 1)
7443 return 0;
7444
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 /* Get a copy of the current contents of the line.
7446 * This is required, because only the most recent line obtained with
7447 * ml_get is valid! */
7448 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7449 if (linecopy == NULL)
7450 return 0;
7451
7452 /*
7453 * In insert mode and the cursor is on a ')' truncate the line at the
7454 * cursor position. We don't want to line up with the matching '(' when
7455 * inserting new stuff.
7456 * For unknown reasons the cursor might be past the end of the line, thus
7457 * check for that.
7458 */
7459 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007460 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 && linecopy[curwin->w_cursor.col] == ')')
7462 linecopy[curwin->w_cursor.col] = NUL;
7463
7464 theline = skipwhite(linecopy);
7465
7466 /* move the cursor to the start of the line */
7467
7468 curwin->w_cursor.col = 0;
7469
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007470 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007471
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007473 * If we are inside a raw string don't change the indent.
7474 * Ignore a raw string inside a comment.
7475 */
7476 comment_pos = ind_find_start_comment();
7477 if (comment_pos != NULL)
7478 {
7479 /* findmatchlimit() static pos is overwritten, make a copy */
7480 tryposCopy = *comment_pos;
7481 comment_pos = &tryposCopy;
7482 }
7483 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007484 if (trypos != NULL && (comment_pos == NULL
7485 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007486 {
7487 amount = -1;
7488 goto laterend;
7489 }
7490
7491 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 * #defines and so on always go at the left when included in 'cinkeys'.
7493 */
7494 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007495 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007496 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007497 goto theend;
7498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499
7500 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007501 * Is it a non-case label? Then that goes at the left margin too unless:
7502 * - JS flag is set.
7503 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007505 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007506 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {
7508 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007509 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
7511
7512 /*
7513 * If we're inside a "//" comment and there is a "//" comment in a
7514 * previous line, lineup with that one.
7515 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007516 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 && (trypos = find_line_comment()) != NULL) /* XXX */
7518 {
7519 /* find how indented the line beginning the comment is */
7520 getvcol(curwin, trypos, &col, NULL, NULL);
7521 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007522 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
7524
7525 /*
7526 * If we're inside a comment and not looking at the start of the
7527 * comment, try using the 'comments' option.
7528 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007529 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 {
7531 int lead_start_len = 2;
7532 int lead_middle_len = 1;
7533 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7534 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7535 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7536 char_u *p;
7537 int start_align = 0;
7538 int start_off = 0;
7539 int done = FALSE;
7540
7541 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007542 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007544 *lead_start = NUL;
7545 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546
7547 p = curbuf->b_p_com;
7548 while (*p != NUL)
7549 {
7550 int align = 0;
7551 int off = 0;
7552 int what = 0;
7553
7554 while (*p != NUL && *p != ':')
7555 {
7556 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7557 what = *p++;
7558 else if (*p == COM_LEFT || *p == COM_RIGHT)
7559 align = *p++;
7560 else if (VIM_ISDIGIT(*p) || *p == '-')
7561 off = getdigits(&p);
7562 else
7563 ++p;
7564 }
7565
7566 if (*p == ':')
7567 ++p;
7568 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7569 if (what == COM_START)
7570 {
7571 STRCPY(lead_start, lead_end);
7572 lead_start_len = (int)STRLEN(lead_start);
7573 start_off = off;
7574 start_align = align;
7575 }
7576 else if (what == COM_MIDDLE)
7577 {
7578 STRCPY(lead_middle, lead_end);
7579 lead_middle_len = (int)STRLEN(lead_middle);
7580 }
7581 else if (what == COM_END)
7582 {
7583 /* If our line starts with the middle comment string, line it
7584 * up with the comment opener per the 'comments' option. */
7585 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7586 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7587 {
7588 done = TRUE;
7589 if (curwin->w_cursor.lnum > 1)
7590 {
7591 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007592 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 * the middle comment string matches in the previous
7594 * line, use the indent of that line. XXX */
7595 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7596 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7597 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7598 else if (STRNCMP(look, lead_middle,
7599 lead_middle_len) == 0)
7600 {
7601 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7602 break;
7603 }
7604 /* If the start comment string doesn't match with the
7605 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007606 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 lead_start, lead_start_len) != 0)
7608 continue;
7609 }
7610 if (start_off != 0)
7611 amount += start_off;
7612 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007613 amount += vim_strsize(lead_start)
7614 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 break;
7616 }
7617
7618 /* If our line starts with the end comment string, line it up
7619 * with the middle comment */
7620 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7621 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7622 {
7623 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7624 /* XXX */
7625 if (off != 0)
7626 amount += off;
7627 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007628 amount += vim_strsize(lead_start)
7629 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 done = TRUE;
7631 break;
7632 }
7633 }
7634 }
7635
7636 /* If our line starts with an asterisk, line up with the
7637 * asterisk in the comment opener; otherwise, line up
7638 * with the first character of the comment text.
7639 */
7640 if (done)
7641 ;
7642 else if (theline[0] == '*')
7643 amount += 1;
7644 else
7645 {
7646 /*
7647 * If we are more than one line away from the comment opener, take
7648 * the indent of the previous non-empty line. If 'cino' has "CO"
7649 * and we are just below the comment opener and there are any
7650 * white characters after it line up with the text after it;
7651 * otherwise, add the amount specified by "c" in 'cino'
7652 */
7653 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007654 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
7656 if (linewhite(lnum)) /* skip blank lines */
7657 continue;
7658 amount = get_indent_lnum(lnum); /* XXX */
7659 break;
7660 }
7661 if (amount == -1) /* use the comment opener */
7662 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007663 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007665 start = ml_get(comment_pos->lnum);
7666 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007668 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007670 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007672 if (curbuf->b_ind_in_comment2 || *look == NUL)
7673 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007676 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678
7679 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007680 * Are we looking at a ']' that has a match?
7681 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007682 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007683 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7684 {
7685 /* align with the line containing the '['. */
7686 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007687 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007688 }
7689
7690 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 * Are we inside parentheses or braces?
7692 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007693 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007694 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007695 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 || trypos != NULL)
7697 {
7698 if (trypos != NULL && tryposBrace != NULL)
7699 {
7700 /* Both an unmatched '(' and '{' is found. Use the one which is
7701 * closer to the current cursor position, set the other to NULL. */
7702 if (trypos->lnum != tryposBrace->lnum
7703 ? trypos->lnum < tryposBrace->lnum
7704 : trypos->col < tryposBrace->col)
7705 trypos = NULL;
7706 else
7707 tryposBrace = NULL;
7708 }
7709
7710 if (trypos != NULL)
7711 {
7712 /*
7713 * If the matching paren is more than one line away, use the indent of
7714 * a previous non-empty line that matches the same paren.
7715 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007716 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007718 /* Line up with the start of the matching paren line. */
7719 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7720 }
7721 else
7722 {
7723 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007724 our_paren_pos = *trypos;
7725 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007727 l = skipwhite(ml_get(lnum));
7728 if (cin_nocode(l)) /* skip comment lines */
7729 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007730 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007731 continue; /* ignore #define, #if, etc. */
7732 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007734 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007735 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007736 {
7737 lnum = trypos->lnum + 1;
7738 continue;
7739 }
7740
7741 /* XXX */
7742 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007743 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007744 && trypos->lnum == our_paren_pos.lnum
7745 && trypos->col == our_paren_pos.col)
7746 {
7747 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007749 if (theline[0] == ')')
7750 {
7751 if (our_paren_pos.lnum != lnum
7752 && cur_amount > amount)
7753 cur_amount = amount;
7754 amount = -1;
7755 }
7756 break;
7757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 }
7759 }
7760
7761 /*
7762 * Line up with line where the matching paren is. XXX
7763 * If the line starts with a '(' or the indent for unclosed
7764 * parentheses is zero, line up with the unclosed parentheses.
7765 */
7766 if (amount == -1)
7767 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007768 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007769 int is_if_for_while = 0;
7770
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007771 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007772 {
7773 /* Look for the outermost opening parenthesis on this line
7774 * and check whether it belongs to an "if", "for" or "while". */
7775
7776 pos_T cursor_save = curwin->w_cursor;
7777 pos_T outermost;
7778 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007779
7780 trypos = &our_paren_pos;
7781 do {
7782 outermost = *trypos;
7783 curwin->w_cursor.lnum = outermost.lnum;
7784 curwin->w_cursor.col = outermost.col;
7785
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007786 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007787 } while (trypos && trypos->lnum == outermost.lnum);
7788
7789 curwin->w_cursor = cursor_save;
7790
7791 line = ml_get(outermost.lnum);
7792
7793 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007794 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007795 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007796
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007797 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007798 look = skipwhite(look);
7799 if (*look == '(')
7800 {
7801 linenr_T save_lnum = curwin->w_cursor.lnum;
7802 char_u *line;
7803 int look_col;
7804
7805 /* Ignore a '(' in front of the line that has a match before
7806 * our matching '('. */
7807 curwin->w_cursor.lnum = our_paren_pos.lnum;
7808 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007809 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007810 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007811 if ((trypos = findmatchlimit(NULL, ')', 0,
7812 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007813 != NULL
7814 && trypos->lnum == our_paren_pos.lnum
7815 && trypos->col < our_paren_pos.col)
7816 ignore_paren_col = trypos->col + 1;
7817
7818 curwin->w_cursor.lnum = save_lnum;
7819 look = ml_get(our_paren_pos.lnum) + look_col;
7820 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007821 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7822 && is_if_for_while == 0)
7823 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007824 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
7826 /*
7827 * If we're looking at a close paren, line up right there;
7828 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007829 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 * the last nonwhite character of the line, use either the
7831 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007832 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 * lines).
7834 */
7835 if (theline[0] != ')')
7836 {
7837 cur_amount = MAXCOL;
7838 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007839 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 && cin_ends_in(l, (char_u *)"(", NULL))
7841 {
7842 /* look for opening unmatched paren, indent one level
7843 * for each additional level */
7844 n = 1;
7845 for (col = 0; col < our_paren_pos.col; ++col)
7846 {
7847 switch (l[col])
7848 {
7849 case '(':
7850 case '{': ++n;
7851 break;
7852
7853 case ')':
7854 case '}': if (n > 1)
7855 --n;
7856 break;
7857 }
7858 }
7859
7860 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007861 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007863 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 our_paren_pos.col++;
7865 else
7866 {
7867 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007868 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 col++;
7870 if (l[col] != NUL) /* In case of trailing space */
7871 our_paren_pos.col = col;
7872 else
7873 our_paren_pos.col++;
7874 }
7875 }
7876
7877 /*
7878 * Find how indented the paren is, or the character after it
7879 * if we did the above "if".
7880 */
7881 if (our_paren_pos.col > 0)
7882 {
7883 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7884 if (cur_amount > (int)col)
7885 cur_amount = col;
7886 }
7887 }
7888
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007889 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {
7891 /* Line up with the start of the matching paren line. */
7892 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007893 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7894 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007895 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {
7897 if (cur_amount != MAXCOL)
7898 amount = cur_amount;
7899 }
7900 else
7901 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007902 /* Add b_ind_unclosed2 for each '(' before our matching one,
7903 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007905 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {
7907 --our_paren_pos.col;
7908 switch (*ml_get_pos(&our_paren_pos))
7909 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007910 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 col = our_paren_pos.col;
7912 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007913 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 col = MAXCOL;
7915 break;
7916 }
7917 }
7918
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007919 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 * braces */
7921 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007922 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 else
7924 {
7925 curwin->w_cursor.lnum = our_paren_pos.lnum;
7926 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007927 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7928 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007929 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007931 {
7932 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007933 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007934 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007935 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
7938 /*
7939 * For a line starting with ')' use the minimum of the two
7940 * positions, to avoid giving it more indent than the previous
7941 * lines:
7942 * func_long_name( if (x
7943 * arg && yy
7944 * ) ^ not here ) ^ not here
7945 */
7946 if (cur_amount < amount)
7947 amount = cur_amount;
7948 }
7949 }
7950
7951 /* add extra indent for a comment */
7952 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007953 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 else
7956 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007957 /*
7958 * We are inside braces, there is a { before this line at the position
7959 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007960 * Make a copy of tryposBrace, it may point to pos_copy inside
7961 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007962 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007963 tryposCopy = *tryposBrace;
7964 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 ourscope = trypos->lnum;
7967 start = ml_get(ourscope);
7968
7969 /*
7970 * Now figure out how indented the line is in general.
7971 * If the brace was at the start of the line, we use that;
7972 * otherwise, check out the indentation of the line as
7973 * a whole and then add the "imaginary indent" to that.
7974 */
7975 look = skipwhite(start);
7976 if (*look == '{')
7977 {
7978 getvcol(curwin, trypos, &col, NULL, NULL);
7979 amount = col;
7980 if (*start == '{')
7981 start_brace = BRACE_IN_COL0;
7982 else
7983 start_brace = BRACE_AT_START;
7984 }
7985 else
7986 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007987 /* That opening brace might have been on a continuation
7988 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 curwin->w_cursor.lnum = ourscope;
7990
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007991 /* Position the cursor over the rightmost paren, so that
7992 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 lnum = ourscope;
7994 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007995 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7996 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 lnum = trypos->lnum;
7998
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007999 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 * case 1: if (asdf &&
8001 * ldfd) {
8002 * }
8003 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008004 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
8005 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008007 else if (curbuf->b_ind_js)
8008 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008010 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011
8012 start_brace = BRACE_AT_END;
8013 }
8014
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008015 /* For Javascript check if the line starts with "key:". */
8016 if (curbuf->b_ind_js)
8017 js_cur_has_key = cin_has_js_key(theline);
8018
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008020 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 * we want to be. otherwise, add the amount of room
8022 * that an indent is supposed to be.
8023 */
8024 if (theline[0] == '}')
8025 {
8026 /*
8027 * they may want closing braces to line up with something
8028 * other than the open brace. indulge them, if so.
8029 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008030 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 }
8032 else
8033 {
8034 /*
8035 * If we're looking at an "else", try to find an "if"
8036 * to match it with.
8037 * If we're looking at a "while", try to find a "do"
8038 * to match it with.
8039 */
8040 lookfor = LOOKFOR_INITIAL;
8041 if (cin_iselse(theline))
8042 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008043 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 lookfor = LOOKFOR_DO;
8045 if (lookfor != LOOKFOR_INITIAL)
8046 {
8047 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008048 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {
8050 amount = get_indent(); /* XXX */
8051 goto theend;
8052 }
8053 }
8054
8055 /*
8056 * We get here if we are not on an "while-of-do" or "else" (or
8057 * failed to find a matching "if").
8058 * Search backwards for something to line up with.
8059 * First set amount for when we don't find anything.
8060 */
8061
8062 /*
8063 * if the '{' is _really_ at the left margin, use the imaginary
8064 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008065 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 */
8067
8068 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8069 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008070 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008071 lookfor_cpp_namespace = TRUE;
8072 }
8073 else if (start_brace == BRACE_AT_START &&
8074 lookfor_cpp_namespace) /* '{' is at start */
8075 {
8076
8077 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 }
8079 else
8080 {
8081 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008082 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008083 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008084
8085 l = skipwhite(ml_get_curline());
8086 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008087 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008088 else if (cin_is_cpp_extern_c(l))
8089 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 else
8092 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008093 /* Compensate for adding b_ind_open_extra later. */
8094 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 if (amount < 0)
8096 amount = 0;
8097 }
8098 }
8099
8100 lookfor_break = FALSE;
8101
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008102 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
8104 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008105 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 }
8107 else if (cin_isscopedecl(theline)) /* private:, ... */
8108 {
8109 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008110 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 }
8112 else
8113 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008114 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8115 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 lookfor_break = TRUE;
8117
8118 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008119 /* b_ind_level from start of block */
8120 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122 scope_amount = amount;
8123 whilelevel = 0;
8124
8125 /*
8126 * Search backwards. If we find something we recognize, line up
8127 * with that.
8128 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008129 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 * the usual amount relative to the conditional
8131 * that opens the block.
8132 */
8133 curwin->w_cursor = cur_curpos;
8134 for (;;)
8135 {
8136 curwin->w_cursor.lnum--;
8137 curwin->w_cursor.col = 0;
8138
8139 /*
8140 * If we went all the way back to the start of our scope, line
8141 * up with it.
8142 */
8143 if (curwin->w_cursor.lnum <= ourscope)
8144 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008145 /* We reached end of scope:
8146 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008148 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 * don't add ind_continuation, otherwise it is a variable
8150 * declaration:
8151 * int x,
8152 * here; <-- add ind_continuation
8153 */
8154 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8155 {
8156 if (curwin->w_cursor.lnum == 0
8157 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008158 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008160 /* nothing found (abuse curbuf->b_ind_maxparen as
8161 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 * initialization) */
8163 if (cont_amount > 0)
8164 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008165 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 amount += ind_continuation;
8167 break;
8168 }
8169
8170 l = ml_get_curline();
8171
8172 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008173 * If we're in a comment or raw string now, skip to
8174 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008176 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 if (trypos != NULL)
8178 {
8179 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008180 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 continue;
8182 }
8183
8184 /*
8185 * Skip preprocessor directives and blank lines.
8186 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008187 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8188 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 continue;
8190
8191 if (cin_nocode(l))
8192 continue;
8193
8194 terminated = cin_isterminated(l, FALSE, TRUE);
8195
8196 /*
8197 * If we are at top level and the line looks like a
8198 * function declaration, we are done
8199 * (it's a variable declaration).
8200 */
8201 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008202 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
8204 /* if the line is terminated with another ','
8205 * it is a continued variable initialization.
8206 * don't add extra indent.
8207 * TODO: does not work, if a function
8208 * declaration is split over multiple lines:
8209 * cin_isfuncdecl returns FALSE then.
8210 */
8211 if (terminated == ',')
8212 break;
8213
8214 /* if it es a enum declaration or an assignment,
8215 * we are done.
8216 */
8217 if (terminated != ';' && cin_isinit())
8218 break;
8219
8220 /* nothing useful found */
8221 if (terminated == 0 || terminated == '{')
8222 continue;
8223 }
8224
8225 if (terminated != ';')
8226 {
8227 /* Skip parens and braces. Position the cursor
8228 * over the rightmost paren, so that matching it
8229 * will take us back to the start of the line.
8230 */ /* XXX */
8231 trypos = NULL;
8232 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008233 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008234 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235
8236 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008237 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
8239 if (trypos != NULL)
8240 {
8241 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008242 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 continue;
8244 }
8245 }
8246
8247 /* it's a variable declaration, add indentation
8248 * like in
8249 * int a,
8250 * b;
8251 */
8252 if (cont_amount > 0)
8253 amount = cont_amount;
8254 else
8255 amount += ind_continuation;
8256 }
8257 else if (lookfor == LOOKFOR_UNTERM)
8258 {
8259 if (cont_amount > 0)
8260 amount = cont_amount;
8261 else
8262 amount += ind_continuation;
8263 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008264 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008265 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008266 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008267 && lookfor != LOOKFOR_CPP_BASECLASS
8268 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008269 {
8270 amount = scope_amount;
8271 if (theline[0] == '{')
8272 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008273 amount += curbuf->b_ind_open_extra;
8274 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008275 }
8276 }
8277
8278 if (lookfor_cpp_namespace)
8279 {
8280 /*
8281 * Looking for C++ namespace, need to look further
8282 * back.
8283 */
8284 if (curwin->w_cursor.lnum == ourscope)
8285 continue;
8286
8287 if (curwin->w_cursor.lnum == 0
8288 || curwin->w_cursor.lnum
8289 < ourscope - FIND_NAMESPACE_LIM)
8290 break;
8291
8292 l = ml_get_curline();
8293
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008294 /* If we're in a comment or raw string now, skip
8295 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008296 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008297 if (trypos != NULL)
8298 {
8299 curwin->w_cursor.lnum = trypos->lnum + 1;
8300 curwin->w_cursor.col = 0;
8301 continue;
8302 }
8303
8304 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008305 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8306 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008307 continue;
8308
8309 /* Finally the actual check for "namespace". */
8310 if (cin_is_cpp_namespace(l))
8311 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008312 amount += curbuf->b_ind_cpp_namespace
8313 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008314 break;
8315 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008316 else if (cin_is_cpp_extern_c(l))
8317 {
8318 amount += curbuf->b_ind_cpp_extern_c
8319 - added_to_amount;
8320 break;
8321 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008322
8323 if (cin_nocode(l))
8324 continue;
8325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 }
8327 break;
8328 }
8329
8330 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008331 * If we're in a comment or raw string now, skip to the start
8332 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008334 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
8336 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008337 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 continue;
8339 }
8340
8341 l = ml_get_curline();
8342
8343 /*
8344 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008345 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008347 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 if (iscase || cin_isscopedecl(l))
8349 {
8350 /* we are only looking for cpp base class
8351 * declaration/initialization any longer */
8352 if (lookfor == LOOKFOR_CPP_BASECLASS)
8353 break;
8354
8355 /* When looking for a "do" we are not interested in
8356 * labels. */
8357 if (whilelevel > 0)
8358 continue;
8359
8360 /*
8361 * case xx:
8362 * c = 99 + <- this indent plus continuation
8363 *-> here;
8364 */
8365 if (lookfor == LOOKFOR_UNTERM
8366 || lookfor == LOOKFOR_ENUM_OR_INIT)
8367 {
8368 if (cont_amount > 0)
8369 amount = cont_amount;
8370 else
8371 amount += ind_continuation;
8372 break;
8373 }
8374
8375 /*
8376 * case xx: <- line up with this case
8377 * x = 333;
8378 * case yy:
8379 */
8380 if ( (iscase && lookfor == LOOKFOR_CASE)
8381 || (iscase && lookfor_break)
8382 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8383 {
8384 /*
8385 * Check that this case label is not for another
8386 * switch()
8387 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008388 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008389 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {
8391 amount = get_indent(); /* XXX */
8392 break;
8393 }
8394 continue;
8395 }
8396
8397 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8398
8399 /*
8400 * case xx: if (cond) <- line up with this if
8401 * y = y + 1;
8402 * -> s = 99;
8403 *
8404 * case xx:
8405 * if (cond) <- line up with this line
8406 * y = y + 1;
8407 * -> s = 99;
8408 */
8409 if (lookfor == LOOKFOR_TERM)
8410 {
8411 if (n)
8412 amount = n;
8413
8414 if (!lookfor_break)
8415 break;
8416 }
8417
8418 /*
8419 * case xx: x = x + 1; <- line up with this x
8420 * -> y = y + 1;
8421 *
8422 * case xx: if (cond) <- line up with this if
8423 * -> y = y + 1;
8424 */
8425 if (n)
8426 {
8427 amount = n;
8428 l = after_label(ml_get_curline());
8429 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008430 {
8431 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008432 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008433 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008434 amount += curbuf->b_ind_level
8435 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 break;
8438 }
8439
8440 /*
8441 * Try to get the indent of a statement before the switch
8442 * label. If nothing is found, line up relative to the
8443 * switch label.
8444 * break; <- may line up with this line
8445 * case xx:
8446 * -> y = 1;
8447 */
8448 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008449 ? curbuf->b_ind_case_code
8450 : curbuf->b_ind_scopedecl_code);
8451 lookfor = curbuf->b_ind_case_break
8452 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 continue;
8454 }
8455
8456 /*
8457 * Looking for a switch() label or C++ scope declaration,
8458 * ignore other lines, skip {}-blocks.
8459 */
8460 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8461 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008462 if (find_last_paren(l, '{', '}')
8463 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008466 curwin->w_cursor.col = 0;
8467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 continue;
8469 }
8470
8471 /*
8472 * Ignore jump labels with nothing after them.
8473 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008474 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {
8476 l = after_label(ml_get_curline());
8477 if (l == NULL || cin_nocode(l))
8478 continue;
8479 }
8480
8481 /*
8482 * Ignore #defines, #if, etc.
8483 * Ignore comment and empty lines.
8484 * (need to get the line again, cin_islabel() may have
8485 * unlocked it)
8486 */
8487 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008488 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 || cin_nocode(l))
8490 continue;
8491
8492 /*
8493 * Are we at the start of a cpp base class declaration or
8494 * constructor initialization?
8495 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008496 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008497 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008498 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008499 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008500 l = ml_get_curline();
8501 }
8502 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {
8504 if (lookfor == LOOKFOR_UNTERM)
8505 {
8506 if (cont_amount > 0)
8507 amount = cont_amount;
8508 else
8509 amount += ind_continuation;
8510 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008511 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008513 /* Need to find start of the declaration. */
8514 lookfor = LOOKFOR_UNTERM;
8515 ind_continuation = 0;
8516 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008519 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008520 amount = get_baseclass_amount(
8521 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 break;
8523 }
8524 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8525 {
8526 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008527 * declaration or initialization before the opening brace.
8528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (cin_isterminated(l, TRUE, FALSE))
8530 break;
8531 else
8532 continue;
8533 }
8534
8535 /*
8536 * What happens next depends on the line being terminated.
8537 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008538 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 * 123,
8540 * sizeof
8541 * here
8542 * Otherwise check whether it is a enumeration or structure
8543 * initialisation (not indented) or a variable declaration
8544 * (indented).
8545 */
8546 terminated = cin_isterminated(l, FALSE, TRUE);
8547
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008548 if (js_cur_has_key)
8549 {
8550 js_cur_has_key = 0; /* only check the first line */
8551 if (curbuf->b_ind_js && terminated == ',')
8552 {
8553 /* For Javascript we might be inside an object:
8554 * key: something, <- align with this
8555 * key: something
8556 * or:
8557 * key: something + <- align with this
8558 * something,
8559 * key: something
8560 */
8561 lookfor = LOOKFOR_JS_KEY;
8562 }
8563 }
8564 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8565 {
8566 amount = get_indent();
8567 break;
8568 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008569 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008570 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008571 if (tryposBrace != NULL && tryposBrace->lnum
8572 >= curwin->w_cursor.lnum)
8573 break;
8574 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008575 /* line below current line is the one that starts a
8576 * (possibly broken) line ending in a comma. */
8577 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008578 else
8579 {
8580 amount = get_indent();
8581 if (curwin->w_cursor.lnum - 1 == ourscope)
8582 /* line above is start of the scope, thus current
8583 * line is the one that stars a (possibly broken)
8584 * line ending in a comma. */
8585 break;
8586 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008587 }
8588
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8590 && terminated == ','))
8591 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008592 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8593 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008594 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 /*
8596 * if we're in the middle of a paren thing,
8597 * go back to the line that starts it so
8598 * we can get the right prevailing indent
8599 * if ( foo &&
8600 * bar )
8601 */
8602 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008603 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008605 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 */
8607 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008608 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008609 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8610 || (trypos->lnum == tryposBrace->lnum
8611 && trypos->col < tryposBrace->col)))
8612 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
8614 /*
8615 * If we are looking for ',', we also look for matching
8616 * braces.
8617 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008618 if (trypos == NULL && terminated == ','
8619 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008620 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621
8622 if (trypos != NULL)
8623 {
8624 /*
8625 * Check if we are on a case label now. This is
8626 * handled above.
8627 * case xx: if ( asdf &&
8628 * asdf)
8629 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008630 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008632 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {
8634 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008635 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 continue;
8637 }
8638 }
8639
8640 /*
8641 * Skip over continuation lines to find the one to get the
8642 * indent from
8643 * char *usethis = "bla\
8644 * bla",
8645 * here;
8646 */
8647 if (terminated == ',')
8648 {
8649 while (curwin->w_cursor.lnum > 1)
8650 {
8651 l = ml_get(curwin->w_cursor.lnum - 1);
8652 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8653 break;
8654 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008655 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
8657 }
8658
8659 /*
8660 * Get indent and pointer to text for current line,
8661 * ignoring any jump label. XXX
8662 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008663 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008664 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008665 else
8666 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 /*
8668 * If this is just above the line we are indenting, and it
8669 * starts with a '{', line it up with this line.
8670 * while (not)
8671 * -> {
8672 * }
8673 */
8674 if (terminated != ',' && lookfor != LOOKFOR_TERM
8675 && theline[0] == '{')
8676 {
8677 amount = cur_amount;
8678 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008679 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 * doesn't start with a '{', which must have a match
8681 * in the same line (scope is the same). Probably:
8682 * { 1, 2 },
8683 * -> { 3, 4 }
8684 */
8685 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008686 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008688 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {
8690 /* have to look back, whether it is a cpp base
8691 * class declaration or initialization */
8692 lookfor = LOOKFOR_CPP_BASECLASS;
8693 continue;
8694 }
8695 break;
8696 }
8697
8698 /*
8699 * Check if we are after an "if", "while", etc.
8700 * Also allow " } else".
8701 */
8702 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8703 {
8704 /*
8705 * Found an unterminated line after an if (), line up
8706 * with the last one.
8707 * if (cond)
8708 * 100 +
8709 * -> here;
8710 */
8711 if (lookfor == LOOKFOR_UNTERM
8712 || lookfor == LOOKFOR_ENUM_OR_INIT)
8713 {
8714 if (cont_amount > 0)
8715 amount = cont_amount;
8716 else
8717 amount += ind_continuation;
8718 break;
8719 }
8720
8721 /*
8722 * If this is just above the line we are indenting, we
8723 * are finished.
8724 * while (not)
8725 * -> here;
8726 * Otherwise this indent can be used when the line
8727 * before this is terminated.
8728 * yyy;
8729 * if (stat)
8730 * while (not)
8731 * xxx;
8732 * -> here;
8733 */
8734 amount = cur_amount;
8735 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008736 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 if (lookfor != LOOKFOR_TERM)
8738 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008739 amount += curbuf->b_ind_level
8740 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 break;
8742 }
8743
8744 /*
8745 * Special trick: when expecting the while () after a
8746 * do, line up with the while()
8747 * do
8748 * x = 1;
8749 * -> here
8750 */
8751 l = skipwhite(ml_get_curline());
8752 if (cin_isdo(l))
8753 {
8754 if (whilelevel == 0)
8755 break;
8756 --whilelevel;
8757 }
8758
8759 /*
8760 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008761 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 * Need to use the scope of this "else". XXX
8763 * If whilelevel != 0 continue looking for a "do {".
8764 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008765 if (cin_iselse(l) && whilelevel == 0)
8766 {
8767 /* If we're looking at "} else", let's make sure we
8768 * find the opening brace of the enclosing scope,
8769 * not the one from "if () {". */
8770 if (*l == '}')
8771 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008772 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008773
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008774 if ((trypos = find_start_brace()) == NULL
8775 || find_match(LOOKFOR_IF, trypos->lnum)
8776 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008777 break;
8778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
8780
8781 /*
8782 * If we're below an unterminated line that is not an
8783 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008784 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 * the line before this one.
8786 */
8787 else
8788 {
8789 /*
8790 * Found two unterminated lines on a row, line up with
8791 * the last one.
8792 * c = 99 +
8793 * 100 +
8794 * -> here;
8795 */
8796 if (lookfor == LOOKFOR_UNTERM)
8797 {
8798 /* When line ends in a comma add extra indent */
8799 if (terminated == ',')
8800 amount += ind_continuation;
8801 break;
8802 }
8803
8804 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8805 {
8806 /* Found two lines ending in ',', lineup with the
8807 * lowest one, but check for cpp base class
8808 * declaration/initialization, if it is an
8809 * opening brace or we are looking just for
8810 * enumerations/initializations. */
8811 if (terminated == ',')
8812 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008813 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 break;
8815
8816 lookfor = LOOKFOR_CPP_BASECLASS;
8817 continue;
8818 }
8819
8820 /* Ignore unterminated lines in between, but
8821 * reduce indent. */
8822 if (amount > cur_amount)
8823 amount = cur_amount;
8824 }
8825 else
8826 {
8827 /*
8828 * Found first unterminated line on a row, may
8829 * line up with this line, remember its indent
8830 * 100 +
8831 * -> here;
8832 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008833 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008835
8836 n = (int)STRLEN(l);
8837 if (terminated == ',' && (*skipwhite(l) == ']'
8838 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008839 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
8841 /*
8842 * If previous line ends in ',', check whether we
8843 * are in an initialization or enum
8844 * struct xxx =
8845 * {
8846 * sizeof a,
8847 * 124 };
8848 * or a normal possible continuation line.
8849 * but only, of no other statement has been found
8850 * yet.
8851 */
8852 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8853 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008854 if (curbuf->b_ind_js)
8855 {
8856 /* Search for a line ending in a comma
8857 * and line up with the line below it
8858 * (could be the current line).
8859 * some = [
8860 * 1, <- line up here
8861 * 2,
8862 * some = [
8863 * 3 + <- line up here
8864 * 4 *
8865 * 5,
8866 * 6,
8867 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008868 if (cin_iscomment(skipwhite(l)))
8869 break;
8870 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008871 trypos = find_match_char('[',
8872 curbuf->b_ind_maxparen);
8873 if (trypos != NULL)
8874 {
8875 if (trypos->lnum
8876 == curwin->w_cursor.lnum - 1)
8877 {
8878 /* Current line is first inside
8879 * [], line up with it. */
8880 break;
8881 }
8882 ourscope = trypos->lnum;
8883 }
8884 }
8885 else
8886 {
8887 lookfor = LOOKFOR_ENUM_OR_INIT;
8888 cont_amount = cin_first_id_amount();
8889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
8891 else
8892 {
8893 if (lookfor == LOOKFOR_INITIAL
8894 && *l != NUL
8895 && l[STRLEN(l) - 1] == '\\')
8896 /* XXX */
8897 cont_amount = cin_get_equal_amount(
8898 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008899 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008900 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008901 && lookfor != LOOKFOR_COMMA
8902 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 lookfor = LOOKFOR_UNTERM;
8904 }
8905 }
8906 }
8907 }
8908
8909 /*
8910 * Check if we are after a while (cond);
8911 * If so: Ignore until the matching "do".
8912 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008913 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 {
8915 /*
8916 * Found an unterminated line after a while ();, line up
8917 * with the last one.
8918 * while (cond);
8919 * 100 + <- line up with this one
8920 * -> here;
8921 */
8922 if (lookfor == LOOKFOR_UNTERM
8923 || lookfor == LOOKFOR_ENUM_OR_INIT)
8924 {
8925 if (cont_amount > 0)
8926 amount = cont_amount;
8927 else
8928 amount += ind_continuation;
8929 break;
8930 }
8931
8932 if (whilelevel == 0)
8933 {
8934 lookfor = LOOKFOR_TERM;
8935 amount = get_indent(); /* XXX */
8936 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008937 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 }
8939 ++whilelevel;
8940 }
8941
8942 /*
8943 * We are after a "normal" statement.
8944 * If we had another statement we can stop now and use the
8945 * indent of that other statement.
8946 * Otherwise the indent of the current statement may be used,
8947 * search backwards for the next "normal" statement.
8948 */
8949 else
8950 {
8951 /*
8952 * Skip single break line, if before a switch label. It
8953 * may be lined up with the case label.
8954 */
8955 if (lookfor == LOOKFOR_NOBREAK
8956 && cin_isbreak(skipwhite(ml_get_curline())))
8957 {
8958 lookfor = LOOKFOR_ANY;
8959 continue;
8960 }
8961
8962 /*
8963 * Handle "do {" line.
8964 */
8965 if (whilelevel > 0)
8966 {
8967 l = cin_skipcomment(ml_get_curline());
8968 if (cin_isdo(l))
8969 {
8970 amount = get_indent(); /* XXX */
8971 --whilelevel;
8972 continue;
8973 }
8974 }
8975
8976 /*
8977 * Found a terminated line above an unterminated line. Add
8978 * the amount for a continuation line.
8979 * x = 1;
8980 * y = foo +
8981 * -> here;
8982 * or
8983 * int x = 1;
8984 * int foo,
8985 * -> here;
8986 */
8987 if (lookfor == LOOKFOR_UNTERM
8988 || lookfor == LOOKFOR_ENUM_OR_INIT)
8989 {
8990 if (cont_amount > 0)
8991 amount = cont_amount;
8992 else
8993 amount += ind_continuation;
8994 break;
8995 }
8996
8997 /*
8998 * Found a terminated line above a terminated line or "if"
8999 * etc. line. Use the amount of the line below us.
9000 * x = 1; x = 1;
9001 * if (asdf) y = 2;
9002 * while (asdf) ->here;
9003 * here;
9004 * ->foo;
9005 */
9006 if (lookfor == LOOKFOR_TERM)
9007 {
9008 if (!lookfor_break && whilelevel == 0)
9009 break;
9010 }
9011
9012 /*
9013 * First line above the one we're indenting is terminated.
9014 * To know what needs to be done look further backward for
9015 * a terminated line.
9016 */
9017 else
9018 {
9019 /*
9020 * position the cursor over the rightmost paren, so
9021 * that matching it will take us back to the start of
9022 * the line. Helps for:
9023 * func(asdr,
9024 * asdfasdf);
9025 * here;
9026 */
9027term_again:
9028 l = ml_get_curline();
9029 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009030 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009031 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 {
9033 /*
9034 * Check if we are on a case label now. This is
9035 * handled above.
9036 * case xx: if ( asdf &&
9037 * asdf)
9038 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009039 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009041 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 {
9043 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009044 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 continue;
9046 }
9047 }
9048
9049 /* When aligning with the case statement, don't align
9050 * with a statement after it.
9051 * case 1: { <-- don't use this { position
9052 * stat;
9053 * }
9054 * case 2:
9055 * stat;
9056 * }
9057 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009058 iscase = (curbuf->b_ind_keep_case_label
9059 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
9061 /*
9062 * Get indent and pointer to text for current line,
9063 * ignoring any jump label.
9064 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009065 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066
9067 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009068 amount += curbuf->b_ind_open_extra;
9069 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009070 l = skipwhite(l);
9071 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009072 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9074
9075 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009076 * When a terminated line starts with "else" skip to
9077 * the matching "if":
9078 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009079 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009080 * Need to use the scope of this "else". XXX
9081 * If whilelevel != 0 continue looking for a "do {".
9082 */
9083 if (lookfor == LOOKFOR_TERM
9084 && *l != '}'
9085 && cin_iselse(l)
9086 && whilelevel == 0)
9087 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009088 if ((trypos = find_start_brace()) == NULL
9089 || find_match(LOOKFOR_IF, trypos->lnum)
9090 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009091 break;
9092 continue;
9093 }
9094
9095 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 * If we're at the end of a block, skip to the start of
9097 * that block.
9098 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009099 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009100 if (find_last_paren(l, '{', '}') /* XXX */
9101 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009103 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 /* if not "else {" check for terminated again */
9105 /* but skip block for "} else {" */
9106 l = cin_skipcomment(ml_get_curline());
9107 if (*l == '}' || !cin_iselse(l))
9108 goto term_again;
9109 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009110 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 }
9112 }
9113 }
9114 }
9115 }
9116 }
9117
9118 /* add extra indent for a comment */
9119 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009120 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009121
9122 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009123 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9124 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009125
9126 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009128
9129 /*
9130 * ok -- we're not inside any sort of structure at all!
9131 *
9132 * This means we're at the top level, and everything should
9133 * basically just match where the previous line is, except
9134 * for the lines immediately following a function declaration,
9135 * which are K&R-style parameters and need to be indented.
9136 *
9137 * if our line starts with an open brace, forget about any
9138 * prevailing indent and make sure it looks like the start
9139 * of a function
9140 */
9141
9142 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009144 amount = curbuf->b_ind_first_open;
9145 goto theend;
9146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009148 /*
9149 * If the NEXT line is a function declaration, the current
9150 * line needs to be indented as a function type spec.
9151 * Don't do this if the current line looks like a comment or if the
9152 * current line is terminated, ie. ends in ';', or if the current line
9153 * contains { or }: "void f() {\n if (1)"
9154 */
9155 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9156 && !cin_nocode(theline)
9157 && vim_strchr(theline, '{') == NULL
9158 && vim_strchr(theline, '}') == NULL
9159 && !cin_ends_in(theline, (char_u *)":", NULL)
9160 && !cin_ends_in(theline, (char_u *)",", NULL)
9161 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9162 cur_curpos.lnum + 1)
9163 && !cin_isterminated(theline, FALSE, TRUE))
9164 {
9165 amount = curbuf->b_ind_func_type;
9166 goto theend;
9167 }
9168
9169 /* search backwards until we find something we recognize */
9170 amount = 0;
9171 curwin->w_cursor = cur_curpos;
9172 while (curwin->w_cursor.lnum > 1)
9173 {
9174 curwin->w_cursor.lnum--;
9175 curwin->w_cursor.col = 0;
9176
9177 l = ml_get_curline();
9178
9179 /*
9180 * If we're in a comment or raw string now, skip to the start
9181 * of it.
9182 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009183 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009185 curwin->w_cursor.lnum = trypos->lnum + 1;
9186 curwin->w_cursor.col = 0;
9187 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 }
9189
9190 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009191 * Are we at the start of a cpp base class declaration or
9192 * constructor initialization?
9193 */ /* XXX */
9194 n = FALSE;
9195 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009197 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9198 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009200 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009202 /* XXX */
9203 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9204 break;
9205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009207 /*
9208 * Skip preprocessor directives and blank lines.
9209 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009210 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009211 continue;
9212
9213 if (cin_nocode(l))
9214 continue;
9215
9216 /*
9217 * If the previous line ends in ',', use one level of
9218 * indentation:
9219 * int foo,
9220 * bar;
9221 * do this before checking for '}' in case of eg.
9222 * enum foobar
9223 * {
9224 * ...
9225 * } foo,
9226 * bar;
9227 */
9228 n = 0;
9229 if (cin_ends_in(l, (char_u *)",", NULL)
9230 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9231 {
9232 /* take us back to opening paren */
9233 if (find_last_paren(l, '(', ')')
9234 && (trypos = find_match_paren(
9235 curbuf->b_ind_maxparen)) != NULL)
9236 curwin->w_cursor = *trypos;
9237
9238 /* For a line ending in ',' that is a continuation line go
9239 * back to the first line with a backslash:
9240 * char *foo = "bla\
9241 * bla",
9242 * here;
9243 */
9244 while (n == 0 && curwin->w_cursor.lnum > 1)
9245 {
9246 l = ml_get(curwin->w_cursor.lnum - 1);
9247 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9248 break;
9249 --curwin->w_cursor.lnum;
9250 curwin->w_cursor.col = 0;
9251 }
9252
9253 amount = get_indent(); /* XXX */
9254
9255 if (amount == 0)
9256 amount = cin_first_id_amount();
9257 if (amount == 0)
9258 amount = ind_continuation;
9259 break;
9260 }
9261
9262 /*
9263 * If the line looks like a function declaration, and we're
9264 * not in a comment, put it the left margin.
9265 */
9266 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9267 break;
9268 l = ml_get_curline();
9269
9270 /*
9271 * Finding the closing '}' of a previous function. Put
9272 * current line at the left margin. For when 'cino' has "fs".
9273 */
9274 if (*skipwhite(l) == '}')
9275 break;
9276
9277 /* (matching {)
9278 * If the previous line ends on '};' (maybe followed by
9279 * comments) align at column 0. For example:
9280 * char *string_array[] = { "foo",
9281 * / * x * / "b};ar" }; / * foobar * /
9282 */
9283 if (cin_ends_in(l, (char_u *)"};", NULL))
9284 break;
9285
9286 /*
9287 * If the previous line ends on '[' we are probably in an
9288 * array constant:
9289 * something = [
9290 * 234, <- extra indent
9291 */
9292 if (cin_ends_in(l, (char_u *)"[", NULL))
9293 {
9294 amount = get_indent() + ind_continuation;
9295 break;
9296 }
9297
9298 /*
9299 * Find a line only has a semicolon that belongs to a previous
9300 * line ending in '}', e.g. before an #endif. Don't increase
9301 * indent then.
9302 */
9303 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9304 {
9305 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306
9307 while (curwin->w_cursor.lnum > 1)
9308 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009309 look = ml_get(--curwin->w_cursor.lnum);
9310 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009311 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009313 }
9314 if (curwin->w_cursor.lnum > 0
9315 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009318 curwin->w_cursor = curpos_save;
9319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009321 /*
9322 * If the PREVIOUS line is a function declaration, the current
9323 * line (and the ones that follow) needs to be indented as
9324 * parameters.
9325 */
9326 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9327 {
9328 amount = curbuf->b_ind_param;
9329 break;
9330 }
9331
9332 /*
9333 * If the previous line ends in ';' and the line before the
9334 * previous line ends in ',' or '\', ident to column zero:
9335 * int foo,
9336 * bar;
9337 * indent_to_0 here;
9338 */
9339 if (cin_ends_in(l, (char_u *)";", NULL))
9340 {
9341 l = ml_get(curwin->w_cursor.lnum - 1);
9342 if (cin_ends_in(l, (char_u *)",", NULL)
9343 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9344 break;
9345 l = ml_get_curline();
9346 }
9347
9348 /*
9349 * Doesn't look like anything interesting -- so just
9350 * use the indent of this line.
9351 *
9352 * Position the cursor over the rightmost paren, so that
9353 * matching it will take us back to the start of the line.
9354 */
9355 find_last_paren(l, '(', ')');
9356
9357 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9358 curwin->w_cursor = *trypos;
9359 amount = get_indent(); /* XXX */
9360 break;
9361 }
9362
9363 /* add extra indent for a comment */
9364 if (cin_iscomment(theline))
9365 amount += curbuf->b_ind_comment;
9366
9367 /* add extra indent if the previous line ended in a backslash:
9368 * "asdfasdf\
9369 * here";
9370 * char *foo = "asdf\
9371 * here";
9372 */
9373 if (cur_curpos.lnum > 1)
9374 {
9375 l = ml_get(cur_curpos.lnum - 1);
9376 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9377 {
9378 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9379 if (cur_amount > 0)
9380 amount = cur_amount;
9381 else if (cur_amount == 0)
9382 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 }
9384 }
9385
9386theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009387 if (amount < 0)
9388 amount = 0;
9389
9390laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 /* put the cursor back where it belongs */
9392 curwin->w_cursor = cur_curpos;
9393
9394 vim_free(linecopy);
9395
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 return amount;
9397}
9398
9399 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009400find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
9402 char_u *look;
9403 pos_T *theirscope;
9404 char_u *mightbeif;
9405 int elselevel;
9406 int whilelevel;
9407
9408 if (lookfor == LOOKFOR_IF)
9409 {
9410 elselevel = 1;
9411 whilelevel = 0;
9412 }
9413 else
9414 {
9415 elselevel = 0;
9416 whilelevel = 1;
9417 }
9418
9419 curwin->w_cursor.col = 0;
9420
9421 while (curwin->w_cursor.lnum > ourscope + 1)
9422 {
9423 curwin->w_cursor.lnum--;
9424 curwin->w_cursor.col = 0;
9425
9426 look = cin_skipcomment(ml_get_curline());
9427 if (cin_iselse(look)
9428 || cin_isif(look)
9429 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009430 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 {
9432 /*
9433 * if we've gone outside the braces entirely,
9434 * we must be out of scope...
9435 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009436 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 if (theirscope == NULL)
9438 break;
9439
9440 /*
9441 * and if the brace enclosing this is further
9442 * back than the one enclosing the else, we're
9443 * out of luck too.
9444 */
9445 if (theirscope->lnum < ourscope)
9446 break;
9447
9448 /*
9449 * and if they're enclosed in a *deeper* brace,
9450 * then we can ignore it because it's in a
9451 * different scope...
9452 */
9453 if (theirscope->lnum > ourscope)
9454 continue;
9455
9456 /*
9457 * if it was an "else" (that's not an "else if")
9458 * then we need to go back to another if, so
9459 * increment elselevel
9460 */
9461 look = cin_skipcomment(ml_get_curline());
9462 if (cin_iselse(look))
9463 {
9464 mightbeif = cin_skipcomment(look + 4);
9465 if (!cin_isif(mightbeif))
9466 ++elselevel;
9467 continue;
9468 }
9469
9470 /*
9471 * if it was a "while" then we need to go back to
9472 * another "do", so increment whilelevel. XXX
9473 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009474 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
9476 ++whilelevel;
9477 continue;
9478 }
9479
9480 /* If it's an "if" decrement elselevel */
9481 look = cin_skipcomment(ml_get_curline());
9482 if (cin_isif(look))
9483 {
9484 elselevel--;
9485 /*
9486 * When looking for an "if" ignore "while"s that
9487 * get in the way.
9488 */
9489 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9490 whilelevel = 0;
9491 }
9492
9493 /* If it's a "do" decrement whilelevel */
9494 if (cin_isdo(look))
9495 whilelevel--;
9496
9497 /*
9498 * if we've used up all the elses, then
9499 * this must be the if that we want!
9500 * match the indent level of that if.
9501 */
9502 if (elselevel <= 0 && whilelevel <= 0)
9503 {
9504 return OK;
9505 }
9506 }
9507 }
9508 return FAIL;
9509}
9510
9511# if defined(FEAT_EVAL) || defined(PROTO)
9512/*
9513 * Get indent level from 'indentexpr'.
9514 */
9515 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009516get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009518 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009519 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009520 pos_T save_pos;
9521 colnr_T save_curswant;
9522 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009524 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9525 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009527 /* Save and restore cursor position and curswant, in case it was changed
9528 * via :normal commands */
9529 save_pos = curwin->w_cursor;
9530 save_curswant = curwin->w_curswant;
9531 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009533 if (use_sandbox)
9534 ++sandbox;
9535 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009536
9537 /* Need to make a copy, the 'indentexpr' option could be changed while
9538 * evaluating it. */
9539 inde_copy = vim_strsave(curbuf->b_p_inde);
9540 if (inde_copy != NULL)
9541 {
9542 indent = (int)eval_to_number(inde_copy);
9543 vim_free(inde_copy);
9544 }
9545
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009546 if (use_sandbox)
9547 --sandbox;
9548 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549
9550 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9551 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9552 * command. */
9553 save_State = State;
9554 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009555 curwin->w_cursor = save_pos;
9556 curwin->w_curswant = save_curswant;
9557 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 check_cursor();
9559 State = save_State;
9560
9561 /* If there is an error, just keep the current indent. */
9562 if (indent < 0)
9563 indent = get_indent();
9564
9565 return indent;
9566}
9567# endif
9568
9569#endif /* FEAT_CINDENT */
9570
9571#if defined(FEAT_LISP) || defined(PROTO)
9572
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009573static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574
9575 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009576lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578 char_u buf[LSIZE];
9579 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009580 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581
9582 while (*word != NUL)
9583 {
9584 (void)copy_option_part(&word, buf, LSIZE, ",");
9585 len = (int)STRLEN(buf);
9586 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9587 return TRUE;
9588 }
9589 return FALSE;
9590}
9591
9592/*
9593 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9594 * The incompatible newer method is quite a bit better at indenting
9595 * code in lisp-like languages than the traditional one; it's still
9596 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9597 *
9598 * TODO:
9599 * Findmatch() should be adapted for lisp, also to make showmatch
9600 * work correctly: now (v5.3) it seems all C/C++ oriented:
9601 * - it does not recognize the #\( and #\) notations as character literals
9602 * - it doesn't know about comments starting with a semicolon
9603 * - it incorrectly interprets '(' as a character literal
9604 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009605 * Update from Sergey Khorev:
9606 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 */
9608 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009609get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009611 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 int amount;
9613 char_u *that;
9614 colnr_T col;
9615 colnr_T firsttry;
9616 int parencount, quotecount;
9617 int vi_lisp;
9618
9619 /* Set vi_lisp to use the vi-compatible method */
9620 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9621
9622 realpos = curwin->w_cursor;
9623 curwin->w_cursor.col = 0;
9624
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009625 if ((pos = findmatch(NULL, '(')) == NULL)
9626 pos = findmatch(NULL, '[');
9627 else
9628 {
9629 paren = *pos;
9630 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009631 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009632 pos = &paren;
9633 }
9634 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 {
9636 /* Extra trick: Take the indent of the first previous non-white
9637 * line that is at the same () level. */
9638 amount = -1;
9639 parencount = 0;
9640
9641 while (--curwin->w_cursor.lnum >= pos->lnum)
9642 {
9643 if (linewhite(curwin->w_cursor.lnum))
9644 continue;
9645 for (that = ml_get_curline(); *that != NUL; ++that)
9646 {
9647 if (*that == ';')
9648 {
9649 while (*(that + 1) != NUL)
9650 ++that;
9651 continue;
9652 }
9653 if (*that == '\\')
9654 {
9655 if (*(that + 1) != NUL)
9656 ++that;
9657 continue;
9658 }
9659 if (*that == '"' && *(that + 1) != NUL)
9660 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009661 while (*++that && *that != '"')
9662 {
9663 /* skipping escaped characters in the string */
9664 if (*that == '\\')
9665 {
9666 if (*++that == NUL)
9667 break;
9668 if (that[1] == NUL)
9669 {
9670 ++that;
9671 break;
9672 }
9673 }
9674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009676 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009678 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 --parencount;
9680 }
9681 if (parencount == 0)
9682 {
9683 amount = get_indent();
9684 break;
9685 }
9686 }
9687
9688 if (amount == -1)
9689 {
9690 curwin->w_cursor.lnum = pos->lnum;
9691 curwin->w_cursor.col = pos->col;
9692 col = pos->col;
9693
9694 that = ml_get_curline();
9695
9696 if (vi_lisp && get_indent() == 0)
9697 amount = 2;
9698 else
9699 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009700 char_u *line = that;
9701
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 amount = 0;
9703 while (*that && col)
9704 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009705 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 col--;
9707 }
9708
9709 /*
9710 * Some keywords require "body" indenting rules (the
9711 * non-standard-lisp ones are Scheme special forms):
9712 *
9713 * (let ((a 1)) instead (let ((a 1))
9714 * (...)) of (...))
9715 */
9716
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009717 if (!vi_lisp && (*that == '(' || *that == '[')
9718 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 amount += 2;
9720 else
9721 {
9722 that++;
9723 amount++;
9724 firsttry = amount;
9725
Bram Moolenaar1c465442017-03-12 20:10:05 +01009726 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009728 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 ++that;
9730 }
9731
9732 if (*that && *that != ';') /* not a comment line */
9733 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009734 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009736 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 firsttry++;
9738
9739 parencount = 0;
9740 quotecount = 0;
9741
9742 if (vi_lisp
9743 || (*that != '"'
9744 && *that != '\''
9745 && *that != '#'
9746 && (*that < '0' || *that > '9')))
9747 {
9748 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009749 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 || quotecount
9751 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009752 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 && !quotecount
9754 && !parencount
9755 && vi_lisp)))
9756 {
9757 if (*that == '"')
9758 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009759 if ((*that == '(' || *that == '[')
9760 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009762 if ((*that == ')' || *that == ']')
9763 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 --parencount;
9765 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009766 amount += lbr_chartabsize_adv(
9767 line, &that, (colnr_T)amount);
9768 amount += lbr_chartabsize_adv(
9769 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 }
9771 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009772 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009774 amount += lbr_chartabsize(
9775 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 that++;
9777 }
9778 if (!*that || *that == ';')
9779 amount = firsttry;
9780 }
9781 }
9782 }
9783 }
9784 }
9785 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009786 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787
9788 curwin->w_cursor = realpos;
9789
9790 return amount;
9791}
9792#endif /* FEAT_LISP */
9793
9794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009795prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009797#if defined(SIGHUP) && defined(SIG_IGN)
9798 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9799 * makes Vim exit and then handling SIGHUP causes various reentrance
9800 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009801 signal(SIGHUP, SIG_IGN);
9802#endif
9803
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804#ifdef FEAT_GUI
9805 if (gui.in_use)
9806 {
9807 gui.dying = TRUE;
9808 out_trash(); /* trash any pending output */
9809 }
9810 else
9811#endif
9812 {
9813 windgoto((int)Rows - 1, 0);
9814
9815 /*
9816 * Switch terminal mode back now, so messages end up on the "normal"
9817 * screen (if there are two screens).
9818 */
9819 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009820 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 out_flush();
9822 }
9823}
9824
9825/*
9826 * Preserve files and exit.
9827 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009828 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9829 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 */
9831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009832preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
9834 buf_T *buf;
9835
9836 prepare_to_exit();
9837
Bram Moolenaar4770d092006-01-12 23:22:24 +00009838 /* Setting this will prevent free() calls. That avoids calling free()
9839 * recursively when free() was invoked with a bad pointer. */
9840 really_exiting = TRUE;
9841
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 out_str(IObuff);
9843 screen_start(); /* don't know where cursor is now */
9844 out_flush();
9845
9846 ml_close_notmod(); /* close all not-modified buffers */
9847
Bram Moolenaar29323592016-07-24 22:04:11 +02009848 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 {
9850 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9851 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009852 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 screen_start(); /* don't know where cursor is now */
9854 out_flush();
9855 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9856 break;
9857 }
9858 }
9859
9860 ml_close_all(FALSE); /* close all memfiles, without deleting */
9861
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009862 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863
9864 getout(1);
9865}
9866
9867/*
9868 * return TRUE if "fname" exists.
9869 */
9870 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009871vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009873 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874
9875 if (mch_stat((char *)fname, &st))
9876 return FALSE;
9877 return TRUE;
9878}
9879
9880/*
9881 * Check for CTRL-C pressed, but only once in a while.
9882 * Should be used instead of ui_breakcheck() for functions that check for
9883 * each line in the file. Calling ui_breakcheck() each time takes too much
9884 * time, because it can be a system call.
9885 */
9886
9887#ifndef BREAKCHECK_SKIP
9888# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9889# define BREAKCHECK_SKIP 200
9890# else
9891# define BREAKCHECK_SKIP 32
9892# endif
9893#endif
9894
9895static int breakcheck_count = 0;
9896
9897 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009898line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
9900 if (++breakcheck_count >= BREAKCHECK_SKIP)
9901 {
9902 breakcheck_count = 0;
9903 ui_breakcheck();
9904 }
9905}
9906
9907/*
9908 * Like line_breakcheck() but check 10 times less often.
9909 */
9910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009911fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
9913 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9914 {
9915 breakcheck_count = 0;
9916 ui_breakcheck();
9917 }
9918}
9919
9920/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009921 * Invoke expand_wildcards() for one pattern.
9922 * Expand items like "%:h" before the expansion.
9923 * Returns OK or FAIL.
9924 */
9925 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009926expand_wildcards_eval(
9927 char_u **pat, /* pointer to input pattern */
9928 int *num_file, /* resulting number of files */
9929 char_u ***file, /* array of resulting files */
9930 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009931{
9932 int ret = FAIL;
9933 char_u *eval_pat = NULL;
9934 char_u *exp_pat = *pat;
9935 char_u *ignored_msg;
9936 int usedlen;
9937
9938 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9939 {
9940 ++emsg_off;
9941 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9942 NULL, &ignored_msg, NULL);
9943 --emsg_off;
9944 if (eval_pat != NULL)
9945 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9946 }
9947
9948 if (exp_pat != NULL)
9949 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9950
9951 if (eval_pat != NULL)
9952 {
9953 vim_free(exp_pat);
9954 vim_free(eval_pat);
9955 }
9956
9957 return ret;
9958}
9959
9960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9962 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009963 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 */
9965 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009966expand_wildcards(
9967 int num_pat, /* number of input patterns */
9968 char_u **pat, /* array of input patterns */
9969 int *num_files, /* resulting number of files */
9970 char_u ***files, /* array of resulting files */
9971 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972{
9973 int retval;
9974 int i, j;
9975 char_u *p;
9976 int non_suf_match; /* number without matching suffix */
9977
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009978 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979
9980 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009981 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 return retval;
9983
9984#ifdef FEAT_WILDIGN
9985 /*
9986 * Remove names that match 'wildignore'.
9987 */
9988 if (*p_wig)
9989 {
9990 char_u *ffname;
9991
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009992 /* check all files in (*files)[] */
9993 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009995 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 if (ffname == NULL) /* out of memory */
9997 break;
9998# ifdef VMS
9999 vms_remove_version(ffname);
10000# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010001 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010003 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010004 vim_free((*files)[i]);
10005 for (j = i; j + 1 < *num_files; ++j)
10006 (*files)[j] = (*files)[j + 1];
10007 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 --i;
10009 }
10010 vim_free(ffname);
10011 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010012
10013 /* If the number of matches is now zero, we fail. */
10014 if (*num_files == 0)
10015 {
Bram Moolenaard23a8232018-02-10 18:45:26 +010010016 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010017 return FAIL;
10018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 }
10020#endif
10021
10022 /*
10023 * Move the names where 'suffixes' match to the end.
10024 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010025 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 {
10027 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010028 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010030 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 {
10032 /*
10033 * Move the name without matching suffix to the front
10034 * of the list.
10035 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010036 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010038 (*files)[j] = (*files)[j - 1];
10039 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 }
10041 }
10042 }
10043
10044 return retval;
10045}
10046
10047/*
10048 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10049 */
10050 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010051match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
10053 int fnamelen, setsuflen;
10054 char_u *setsuf;
10055#define MAXSUFLEN 30 /* maximum length of a file suffix */
10056 char_u suf_buf[MAXSUFLEN];
10057
10058 fnamelen = (int)STRLEN(fname);
10059 setsuflen = 0;
10060 for (setsuf = p_su; *setsuf; )
10061 {
10062 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010063 if (setsuflen == 0)
10064 {
10065 char_u *tail = gettail(fname);
10066
10067 /* empty entry: match name without a '.' */
10068 if (vim_strchr(tail, '.') == NULL)
10069 {
10070 setsuflen = 1;
10071 break;
10072 }
10073 }
10074 else
10075 {
10076 if (fnamelen >= setsuflen
10077 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10078 (size_t)setsuflen) == 0)
10079 break;
10080 setsuflen = 0;
10081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 }
10083 return (setsuflen != 0);
10084}
10085
10086#if !defined(NO_EXPANDPATH) || defined(PROTO)
10087
10088# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010089static int vim_backtick(char_u *p);
10090static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091# endif
10092
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010093# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094/*
10095 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10096 * it's shared between these systems.
10097 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010098# if defined(PROTO)
10099# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100# else
10101# ifdef __BORLANDC__
10102# define _cdecl _RTLENTRYF
10103# endif
10104# endif
10105
10106/*
10107 * comparison function for qsort in dos_expandpath()
10108 */
10109 static int _cdecl
10110pstrcmp(const void *a, const void *b)
10111{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010112 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113}
10114
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010116 * Recursively expand one path component into all matching files and/or
10117 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 * Return the number of matches found.
10119 * "path" has backslashes before chars that are not to be expanded, starting
10120 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010121 * Return the number of matches found.
10122 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 */
10124 static int
10125dos_expandpath(
10126 garray_T *gap,
10127 char_u *path,
10128 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010129 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010130 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010132 char_u *buf;
10133 char_u *path_end;
10134 char_u *p, *s, *e;
10135 int start_len = gap->ga_len;
10136 char_u *pat;
10137 regmatch_T regmatch;
10138 int starts_with_dot;
10139 int matches;
10140 int len;
10141 int starstar = FALSE;
10142 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 WIN32_FIND_DATA fb;
10144 HANDLE hFind = (HANDLE)0;
10145# ifdef FEAT_MBYTE
10146 WIN32_FIND_DATAW wfb;
10147 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010150 int ok;
10151
10152 /* Expanding "**" may take a long time, check for CTRL-C. */
10153 if (stardepth > 0)
10154 {
10155 ui_breakcheck();
10156 if (got_int)
10157 return 0;
10158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010160 /* Make room for file name. When doing encoding conversion the actual
10161 * length may be quite a bit longer, thus use the maximum possible length. */
10162 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (buf == NULL)
10164 return 0;
10165
10166 /*
10167 * Find the first part in the path name that contains a wildcard or a ~1.
10168 * Copy it into buf, including the preceding characters.
10169 */
10170 p = buf;
10171 s = buf;
10172 e = NULL;
10173 path_end = path;
10174 while (*path_end != NUL)
10175 {
10176 /* May ignore a wildcard that has a backslash before it; it will
10177 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10178 if (path_end >= path + wildoff && rem_backslash(path_end))
10179 *p++ = *path_end++;
10180 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10181 {
10182 if (e != NULL)
10183 break;
10184 s = p + 1;
10185 }
10186 else if (path_end >= path + wildoff
10187 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10188 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010189# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 if (has_mbyte)
10191 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010192 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 STRNCPY(p, path_end, len);
10194 p += len;
10195 path_end += len;
10196 }
10197 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 *p++ = *path_end++;
10200 }
10201 e = p;
10202 *e = NUL;
10203
10204 /* now we have one wildcard component between s and e */
10205 /* Remove backslashes between "wildoff" and the start of the wildcard
10206 * component. */
10207 for (p = buf + wildoff; p < s; ++p)
10208 if (rem_backslash(p))
10209 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010210 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 --e;
10212 --s;
10213 }
10214
Bram Moolenaar231334e2005-07-25 20:46:57 +000010215 /* Check for "**" between "s" and "e". */
10216 for (p = s; p < e; ++p)
10217 if (p[0] == '*' && p[1] == '*')
10218 starstar = TRUE;
10219
Bram Moolenaard82103e2016-01-17 17:04:05 +010010220 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10222 if (pat == NULL)
10223 {
10224 vim_free(buf);
10225 return 0;
10226 }
10227
10228 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010229 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010230 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 regmatch.rm_ic = TRUE; /* Always ignore case */
10232 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010233 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010234 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 vim_free(pat);
10236
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010237 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 {
10239 vim_free(buf);
10240 return 0;
10241 }
10242
10243 /* remember the pattern or file name being looked for */
10244 matchname = vim_strsave(s);
10245
Bram Moolenaar231334e2005-07-25 20:46:57 +000010246 /* If "**" is by itself, this is the first time we encounter it and more
10247 * is following then find matches without any directory. */
10248 if (!didstar && stardepth < 100 && starstar && e - s == 2
10249 && *path_end == '/')
10250 {
10251 STRCPY(s, path_end + 1);
10252 ++stardepth;
10253 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10254 --stardepth;
10255 }
10256
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 /* Scan all files in the directory with "dir/ *.*" */
10258 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259# ifdef FEAT_MBYTE
10260 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10261 {
10262 /* The active codepage differs from 'encoding'. Attempt using the
10263 * wide function. If it fails because it is not implemented fall back
10264 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010265 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 if (wn != NULL)
10267 {
10268 hFind = FindFirstFileW(wn, &wfb);
10269 if (hFind == INVALID_HANDLE_VALUE
10270 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010271 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 }
10273 }
10274
10275 if (wn == NULL)
10276# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010277 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279
10280 while (ok)
10281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282# ifdef FEAT_MBYTE
10283 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010284 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 else
10286# endif
10287 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 /* Ignore entries starting with a dot, unless when asked for. Accept
10289 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010290 if ((p[0] != '.' || starts_with_dot
10291 || ((flags & EW_DODOT)
10292 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010294 || (regmatch.regprog != NULL
10295 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010296 || ((flags & EW_NOTWILD)
10297 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010301
10302 if (starstar && stardepth < 100)
10303 {
10304 /* For "**" in the pattern first go deeper in the tree to
10305 * find matches. */
10306 STRCPY(buf + len, "/**");
10307 STRCPY(buf + len + 3, path_end);
10308 ++stardepth;
10309 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10310 --stardepth;
10311 }
10312
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 STRCPY(buf + len, path_end);
10314 if (mch_has_exp_wildcard(path_end))
10315 {
10316 /* need to expand another component of the path */
10317 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010318 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 }
10320 else
10321 {
10322 /* no more wildcards, check if there is a match */
10323 /* remove backslashes for the remaining components only */
10324 if (*path_end != 0)
10325 backslash_halve(buf + len + 1);
10326 if (mch_getperm(buf) >= 0) /* add existing file */
10327 addfile(gap, buf, flags);
10328 }
10329 }
10330
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331# ifdef FEAT_MBYTE
10332 if (wn != NULL)
10333 {
10334 vim_free(p);
10335 ok = FindNextFileW(hFind, &wfb);
10336 }
10337 else
10338# endif
10339 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340
10341 /* If no more matches and no match was used, try expanding the name
10342 * itself. Finds the long name of a short filename. */
10343 if (!ok && matchname != NULL && gap->ga_len == start_len)
10344 {
10345 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 FindClose(hFind);
10347# ifdef FEAT_MBYTE
10348 if (wn != NULL)
10349 {
10350 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010351 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 if (wn != NULL)
10353 hFind = FindFirstFileW(wn, &wfb);
10354 }
10355 if (wn == NULL)
10356# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010357 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010359 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 }
10361 }
10362
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 FindClose(hFind);
10364# ifdef FEAT_MBYTE
10365 vim_free(wn);
10366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010368 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 vim_free(matchname);
10370
10371 matches = gap->ga_len - start_len;
10372 if (matches > 0)
10373 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10374 sizeof(char_u *), pstrcmp);
10375 return matches;
10376}
10377
10378 int
10379mch_expandpath(
10380 garray_T *gap,
10381 char_u *path,
10382 int flags) /* EW_* flags */
10383{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010384 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010386# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387
Bram Moolenaar231334e2005-07-25 20:46:57 +000010388#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10389 || defined(PROTO)
10390/*
10391 * Unix style wildcard expansion code.
10392 * It's here because it's used both for Unix and Mac.
10393 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010394static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010395
10396 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010397pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010398{
10399 return (pathcmp(*(char **)a, *(char **)b, -1));
10400}
10401
10402/*
10403 * Recursively expand one path component into all matching files and/or
10404 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10405 * "path" has backslashes before chars that are not to be expanded, starting
10406 * at "path + wildoff".
10407 * Return the number of matches found.
10408 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10409 */
10410 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010411unix_expandpath(
10412 garray_T *gap,
10413 char_u *path,
10414 int wildoff,
10415 int flags, /* EW_* flags */
10416 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010417{
10418 char_u *buf;
10419 char_u *path_end;
10420 char_u *p, *s, *e;
10421 int start_len = gap->ga_len;
10422 char_u *pat;
10423 regmatch_T regmatch;
10424 int starts_with_dot;
10425 int matches;
10426 int len;
10427 int starstar = FALSE;
10428 static int stardepth = 0; /* depth for "**" expansion */
10429
10430 DIR *dirp;
10431 struct dirent *dp;
10432
10433 /* Expanding "**" may take a long time, check for CTRL-C. */
10434 if (stardepth > 0)
10435 {
10436 ui_breakcheck();
10437 if (got_int)
10438 return 0;
10439 }
10440
10441 /* make room for file name */
10442 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10443 if (buf == NULL)
10444 return 0;
10445
10446 /*
10447 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010448 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010449 * Copy it into "buf", including the preceding characters.
10450 */
10451 p = buf;
10452 s = buf;
10453 e = NULL;
10454 path_end = path;
10455 while (*path_end != NUL)
10456 {
10457 /* May ignore a wildcard that has a backslash before it; it will
10458 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10459 if (path_end >= path + wildoff && rem_backslash(path_end))
10460 *p++ = *path_end++;
10461 else if (*path_end == '/')
10462 {
10463 if (e != NULL)
10464 break;
10465 s = p + 1;
10466 }
10467 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010468 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010469 || (!p_fic && (flags & EW_ICASE)
10470 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010471 e = p;
10472#ifdef FEAT_MBYTE
10473 if (has_mbyte)
10474 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010475 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010476 STRNCPY(p, path_end, len);
10477 p += len;
10478 path_end += len;
10479 }
10480 else
10481#endif
10482 *p++ = *path_end++;
10483 }
10484 e = p;
10485 *e = NUL;
10486
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010487 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010488 /* Remove backslashes between "wildoff" and the start of the wildcard
10489 * component. */
10490 for (p = buf + wildoff; p < s; ++p)
10491 if (rem_backslash(p))
10492 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010493 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010494 --e;
10495 --s;
10496 }
10497
10498 /* Check for "**" between "s" and "e". */
10499 for (p = s; p < e; ++p)
10500 if (p[0] == '*' && p[1] == '*')
10501 starstar = TRUE;
10502
10503 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010504 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010505 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10506 if (pat == NULL)
10507 {
10508 vim_free(buf);
10509 return 0;
10510 }
10511
10512 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010513 if (flags & EW_ICASE)
10514 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10515 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010516 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010517 if (flags & (EW_NOERROR | EW_NOTWILD))
10518 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010519 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010520 if (flags & (EW_NOERROR | EW_NOTWILD))
10521 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010522 vim_free(pat);
10523
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010524 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010525 {
10526 vim_free(buf);
10527 return 0;
10528 }
10529
10530 /* If "**" is by itself, this is the first time we encounter it and more
10531 * is following then find matches without any directory. */
10532 if (!didstar && stardepth < 100 && starstar && e - s == 2
10533 && *path_end == '/')
10534 {
10535 STRCPY(s, path_end + 1);
10536 ++stardepth;
10537 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10538 --stardepth;
10539 }
10540
10541 /* open the directory for scanning */
10542 *s = NUL;
10543 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10544
10545 /* Find all matching entries */
10546 if (dirp != NULL)
10547 {
10548 for (;;)
10549 {
10550 dp = readdir(dirp);
10551 if (dp == NULL)
10552 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010553 if ((dp->d_name[0] != '.' || starts_with_dot
10554 || ((flags & EW_DODOT)
10555 && dp->d_name[1] != NUL
10556 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010557 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10558 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010559 || ((flags & EW_NOTWILD)
10560 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010561 {
10562 STRCPY(s, dp->d_name);
10563 len = STRLEN(buf);
10564
10565 if (starstar && stardepth < 100)
10566 {
10567 /* For "**" in the pattern first go deeper in the tree to
10568 * find matches. */
10569 STRCPY(buf + len, "/**");
10570 STRCPY(buf + len + 3, path_end);
10571 ++stardepth;
10572 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10573 --stardepth;
10574 }
10575
10576 STRCPY(buf + len, path_end);
10577 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10578 {
10579 /* need to expand another component of the path */
10580 /* remove backslashes for the remaining components only */
10581 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10582 }
10583 else
10584 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010585 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010586
Bram Moolenaar231334e2005-07-25 20:46:57 +000010587 /* no more wildcards, check if there is a match */
10588 /* remove backslashes for the remaining components only */
10589 if (*path_end != NUL)
10590 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010591 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010592 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010593 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010594 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010595#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010596 size_t precomp_len = STRLEN(buf)+1;
10597 char_u *precomp_buf =
10598 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010599
Bram Moolenaar231334e2005-07-25 20:46:57 +000010600 if (precomp_buf)
10601 {
10602 mch_memmove(buf, precomp_buf, precomp_len);
10603 vim_free(precomp_buf);
10604 }
10605#endif
10606 addfile(gap, buf, flags);
10607 }
10608 }
10609 }
10610 }
10611
10612 closedir(dirp);
10613 }
10614
10615 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010616 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010617
10618 matches = gap->ga_len - start_len;
10619 if (matches > 0)
10620 qsort(((char_u **)gap->ga_data) + start_len, matches,
10621 sizeof(char_u *), pstrcmp);
10622 return matches;
10623}
10624#endif
10625
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010626#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010627static int find_previous_pathsep(char_u *path, char_u **psep);
10628static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10629static void expand_path_option(char_u *curdir, garray_T *gap);
10630static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10631static void uniquefy_paths(garray_T *gap, char_u *pattern);
10632static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010633
10634/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010635 * Moves "*psep" back to the previous path separator in "path".
10636 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010637 */
10638 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010639find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010640{
10641 /* skip the current separator */
10642 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010643 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644
10645 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010646 while (*psep > path)
10647 {
10648 if (vim_ispathsep(**psep))
10649 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010650 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010651 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010652
10653 return FAIL;
10654}
10655
10656/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010657 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10658 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010659 */
10660 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010661is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010662{
10663 int j;
10664 int candidate_len;
10665 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010666 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010667 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010668
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010669 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010670 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010671 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010672 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010673
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010674 candidate_len = (int)STRLEN(maybe_unique);
10675 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010676 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010677 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010678
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010679 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010680 if (fnamecmp(maybe_unique, rival) == 0
10681 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010682 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010683 }
10684
Bram Moolenaar162bd912010-07-28 22:29:10 +020010685 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010686}
10687
10688/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010689 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010690 * paths are expanded to their equivalent fullpath. This includes the "."
10691 * (relative to current buffer directory) and empty path (relative to current
10692 * directory) notations.
10693 *
10694 * TODO: handle upward search (;) and path limiter (**N) notations by
10695 * expanding each into their equivalent path(s).
10696 */
10697 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010698expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010699{
10700 char_u *path_option = *curbuf->b_p_path == NUL
10701 ? p_path : curbuf->b_p_path;
10702 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010703 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010704 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010705
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010706 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010707 return;
10708
10709 while (*path_option != NUL)
10710 {
10711 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10712
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010713 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010714 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010715 /* Relative to current buffer:
10716 * "/path/file" + "." -> "/path/"
10717 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010718 if (curbuf->b_ffname == NULL)
10719 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010720 p = gettail(curbuf->b_ffname);
10721 len = (int)(p - curbuf->b_ffname);
10722 if (len + (int)STRLEN(buf) >= MAXPATHL)
10723 continue;
10724 if (buf[1] == NUL)
10725 buf[len] = NUL;
10726 else
10727 STRMOVE(buf + len, buf + 2);
10728 mch_memmove(buf, curbuf->b_ffname, len);
10729 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010730 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010731 else if (buf[0] == NUL)
10732 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010733 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010734 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010735 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010736 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010737 else if (!mch_isFullName(buf))
10738 {
10739 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010740 len = (int)STRLEN(curdir);
10741 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010742 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010743 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010744 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010745 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010746 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010747 }
10748
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010749 if (ga_grow(gap, 1) == FAIL)
10750 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010751
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010752# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010753 /* Avoid the path ending in a backslash, it fails when a comma is
10754 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010755 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010756 if (buf[len - 1] == '\\')
10757 buf[len - 1] = '/';
10758# endif
10759
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010760 p = vim_strsave(buf);
10761 if (p == NULL)
10762 break;
10763 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010764 }
10765
10766 vim_free(buf);
10767}
10768
10769/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010770 * Returns a pointer to the file or directory name in "fname" that matches the
10771 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010772 *
10773 * path: /foo/bar/baz
10774 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010775 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010776 */
10777 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010778get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010779{
10780 int i;
10781 int maxlen = 0;
10782 char_u **path_part = (char_u **)gap->ga_data;
10783 char_u *cutoff = NULL;
10784
10785 for (i = 0; i < gap->ga_len; i++)
10786 {
10787 int j = 0;
10788
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010789 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010790# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010791 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10792#endif
10793 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010794 j++;
10795 if (j > maxlen)
10796 {
10797 maxlen = j;
10798 cutoff = &fname[j];
10799 }
10800 }
10801
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010802 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010803 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010804 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010805 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010806
10807 return cutoff;
10808}
10809
10810/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010811 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10812 * that they are unique with respect to each other while conserving the part
10813 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010814 */
10815 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010816uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010817{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010818 int i;
10819 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010820 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010821 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010822 char_u *pat;
10823 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010824 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010825 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010826 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010827 char_u **in_curdir = NULL;
10828 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010829
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010830 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010831 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010832
10833 /*
10834 * We need to prepend a '*' at the beginning of file_pattern so that the
10835 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010836 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010837 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010838 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010839 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010840 if (file_pattern == NULL)
10841 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010842 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010843 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010844 STRCAT(file_pattern, pattern);
10845 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10846 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010847 if (pat == NULL)
10848 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010849
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010850 regmatch.rm_ic = TRUE; /* always ignore case */
10851 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10852 vim_free(pat);
10853 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010854 return;
10855
Bram Moolenaar162bd912010-07-28 22:29:10 +020010856 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010857 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010858 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010859 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010860
10861 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010862 if (in_curdir == NULL)
10863 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010864
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010865 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010866 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010867 char_u *path = fnames[i];
10868 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010869 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010870 char_u *pathsep_p;
10871 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010872
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010873 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010874 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010875 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010876 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010877 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010878
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010879 /* Shorten the filename while maintaining its uniqueness */
10880 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010881
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010882 /* Don't assume all files can be reached without path when search
10883 * pattern starts with star star slash, so only remove path_cutoff
10884 * when possible. */
10885 if (pattern[0] == '*' && pattern[1] == '*'
10886 && vim_ispathsep_nocolon(pattern[2])
10887 && path_cutoff != NULL
10888 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10889 && is_unique(path_cutoff, gap, i))
10890 {
10891 sort_again = TRUE;
10892 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10893 }
10894 else
10895 {
10896 /* Here all files can be reached without path, so get shortest
10897 * unique path. We start at the end of the path. */
10898 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010899
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010900 while (find_previous_pathsep(path, &pathsep_p))
10901 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10902 && is_unique(pathsep_p + 1, gap, i)
10903 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10904 {
10905 sort_again = TRUE;
10906 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10907 break;
10908 }
10909 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010910
10911 if (mch_isFullName(path))
10912 {
10913 /*
10914 * Last resort: shorten relative to curdir if possible.
10915 * 'possible' means:
10916 * 1. It is under the current directory.
10917 * 2. The result is actually shorter than the original.
10918 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010919 * Before curdir After
10920 * /foo/bar/file.txt /foo/bar ./file.txt
10921 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10922 * /file.txt / /file.txt
10923 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010924 */
10925 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010926 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010927#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010928 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010929 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010930 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010931 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010932 && !vim_ispathsep(*short_name)
10933#endif
10934 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010935 {
10936 STRCPY(path, ".");
10937 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010938 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010939 }
10940 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010941 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010942 }
10943
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010944 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010945 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010946 {
10947 char_u *rel_path;
10948 char_u *path = in_curdir[i];
10949
10950 if (path == NULL)
10951 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010952
10953 /* If the {filename} is not unique, change it to ./{filename}.
10954 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010955 short_name = shorten_fname(path, curdir);
10956 if (short_name == NULL)
10957 short_name = path;
10958 if (is_unique(short_name, gap, i))
10959 {
10960 STRCPY(fnames[i], short_name);
10961 continue;
10962 }
10963
10964 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10965 if (rel_path == NULL)
10966 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010967 STRCPY(rel_path, ".");
10968 add_pathsep(rel_path);
10969 STRCAT(rel_path, short_name);
10970
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010971 vim_free(fnames[i]);
10972 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010973 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010974 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010975 }
10976
Bram Moolenaar162bd912010-07-28 22:29:10 +020010977theend:
10978 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010979 if (in_curdir != NULL)
10980 {
10981 for (i = 0; i < gap->ga_len; i++)
10982 vim_free(in_curdir[i]);
10983 vim_free(in_curdir);
10984 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010985 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010986 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010987
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010988 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010989 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010990}
10991
10992/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010993 * Calls globpath() with 'path' values for the given pattern and stores the
10994 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010995 * Returns the total number of matches.
10996 */
10997 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010998expand_in_path(
10999 garray_T *gap,
11000 char_u *pattern,
11001 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011002{
Bram Moolenaar162bd912010-07-28 22:29:10 +020011003 char_u *curdir;
11004 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011005 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011006 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011007
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011008 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020011009 return 0;
11010 mch_dirname(curdir, MAXPATHL);
11011
Bram Moolenaar0be992e2010-08-12 21:50:51 +020011012 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011013 expand_path_option(curdir, &path_ga);
11014 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020011015 if (path_ga.ga_len == 0)
11016 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011017
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020011018 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011019 ga_clear_strings(&path_ga);
11020 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011021 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020011022
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011023 if (flags & EW_ICASE)
11024 glob_flags |= WILD_ICASE;
11025 if (flags & EW_ADDSLASH)
11026 glob_flags |= WILD_ADD_SLASH;
11027 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011028 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011029
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011030 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011031}
11032#endif
11033
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011034#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11035/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011036 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11037 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011038 */
11039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011040remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011041{
11042 int i;
11043 int j;
11044 char_u **fnames = (char_u **)gap->ga_data;
11045
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011046 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011047 for (i = gap->ga_len - 1; i > 0; --i)
11048 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11049 {
11050 vim_free(fnames[i]);
11051 for (j = i + 1; j < gap->ga_len; ++j)
11052 fnames[j - 1] = fnames[j];
11053 --gap->ga_len;
11054 }
11055}
11056#endif
11057
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010011058static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011059
11060/*
11061 * Return TRUE if "p" contains what looks like an environment variable.
11062 * Allowing for escaping.
11063 */
11064 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011065has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011066{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011067 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011068 {
11069 if (*p == '\\' && p[1] != NUL)
11070 ++p;
11071 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011072#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011073 "$%"
11074#else
11075 "$"
11076#endif
11077 , *p) != NULL)
11078 return TRUE;
11079 }
11080 return FALSE;
11081}
11082
11083#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010011084static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011085
11086/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011087 * Return TRUE if "p" contains a special wildcard character, one that Vim
11088 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011089 */
11090 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011091has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011092{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011093 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011094 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011095 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011096 if (*p == '\\' && p[1] != NUL)
11097 ++p;
11098 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11099 return TRUE;
11100 }
11101 return FALSE;
11102}
11103#endif
11104
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105/*
11106 * Generic wildcard expansion code.
11107 *
11108 * Characters in "pat" that should not be expanded must be preceded with a
11109 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11110 *
11111 * Return FAIL when no single file was found. In this case "num_file" is not
11112 * set, and "file" may contain an error message.
11113 * Return OK when some files found. "num_file" is set to the number of
11114 * matches, "file" to the array of matches. Call FreeWild() later.
11115 */
11116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011117gen_expand_wildcards(
11118 int num_pat, /* number of input patterns */
11119 char_u **pat, /* array of input patterns */
11120 int *num_file, /* resulting number of files */
11121 char_u ***file, /* array of resulting files */
11122 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123{
11124 int i;
11125 garray_T ga;
11126 char_u *p;
11127 static int recursive = FALSE;
11128 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011129 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011130#if defined(FEAT_SEARCHPATH)
11131 int did_expand_in_path = FALSE;
11132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133
11134 /*
11135 * expand_env() is called to expand things like "~user". If this fails,
11136 * it calls ExpandOne(), which brings us back here. In this case, always
11137 * call the machine specific expansion function, if possible. Otherwise,
11138 * return FAIL.
11139 */
11140 if (recursive)
11141#ifdef SPECIAL_WILDCHAR
11142 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11143#else
11144 return FAIL;
11145#endif
11146
11147#ifdef SPECIAL_WILDCHAR
11148 /*
11149 * If there are any special wildcard characters which we cannot handle
11150 * here, call machine specific function for all the expansion. This
11151 * avoids starting the shell for each argument separately.
11152 * For `=expr` do use the internal function.
11153 */
11154 for (i = 0; i < num_pat; i++)
11155 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011156 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157# ifdef VIM_BACKTICK
11158 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11159# endif
11160 )
11161 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11162 }
11163#endif
11164
11165 recursive = TRUE;
11166
11167 /*
11168 * The matching file names are stored in a growarray. Init it empty.
11169 */
11170 ga_init2(&ga, (int)sizeof(char_u *), 30);
11171
11172 for (i = 0; i < num_pat; ++i)
11173 {
11174 add_pat = -1;
11175 p = pat[i];
11176
11177#ifdef VIM_BACKTICK
11178 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011181 if (add_pat == -1)
11182 retval = FAIL;
11183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 else
11185#endif
11186 {
11187 /*
11188 * First expand environment variables, "~/" and "~user/".
11189 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011190 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011192 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 if (p == NULL)
11194 p = pat[i];
11195#ifdef UNIX
11196 /*
11197 * On Unix, if expand_env() can't expand an environment
11198 * variable, use the shell to do that. Discard previously
11199 * found file names and start all over again.
11200 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011201 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 {
11203 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011204 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011206 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 recursive = FALSE;
11208 return i;
11209 }
11210#endif
11211 }
11212
11213 /*
11214 * If there are wildcards: Expand file names and add each match to
11215 * the list. If there is no match, and EW_NOTFOUND is given, add
11216 * the pattern.
11217 * If there are no wildcards: Add the file name if it exists or
11218 * when EW_NOTFOUND is given.
11219 */
11220 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011221 {
11222#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011223 if ((flags & EW_PATH)
11224 && !mch_isFullName(p)
11225 && !(p[0] == '.'
11226 && (vim_ispathsep(p[1])
11227 || (p[1] == '.' && vim_ispathsep(p[2]))))
11228 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011229 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011230 /* :find completion where 'path' is used.
11231 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011232 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011233 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011234 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011235 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011236 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011237 else
11238#endif
11239 add_pat = mch_expandpath(&ga, p, flags);
11240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 }
11242
11243 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11244 {
11245 char_u *t = backslash_halve_save(p);
11246
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11248 * "vim c:/" work. */
11249 if (flags & EW_NOTFOUND)
11250 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 addfile(&ga, t, flags);
11253 vim_free(t);
11254 }
11255
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011256#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011257 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011258 uniquefy_paths(&ga, p);
11259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 if (p != pat[i])
11261 vim_free(p);
11262 }
11263
11264 *num_file = ga.ga_len;
11265 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11266
11267 recursive = FALSE;
11268
Bram Moolenaar336bd622016-01-17 18:23:58 +010011269 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270}
11271
11272# ifdef VIM_BACKTICK
11273
11274/*
11275 * Return TRUE if we can expand this backtick thing here.
11276 */
11277 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011278vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279{
11280 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11281}
11282
11283/*
11284 * Expand an item in `backticks` by executing it as a command.
11285 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011286 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 */
11288 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011289expand_backtick(
11290 garray_T *gap,
11291 char_u *pat,
11292 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
11294 char_u *p;
11295 char_u *cmd;
11296 char_u *buffer;
11297 int cnt = 0;
11298 int i;
11299
11300 /* Create the command: lop off the backticks. */
11301 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11302 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011303 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304
11305#ifdef FEAT_EVAL
11306 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011307 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 else
11309#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011310 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011311 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 vim_free(cmd);
11313 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011314 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315
11316 cmd = buffer;
11317 while (*cmd != NUL)
11318 {
11319 cmd = skipwhite(cmd); /* skip over white space */
11320 p = cmd;
11321 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11322 ++p;
11323 /* add an entry if it is not empty */
11324 if (p > cmd)
11325 {
11326 i = *p;
11327 *p = NUL;
11328 addfile(gap, cmd, flags);
11329 *p = i;
11330 ++cnt;
11331 }
11332 cmd = p;
11333 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11334 ++cmd;
11335 }
11336
11337 vim_free(buffer);
11338 return cnt;
11339}
11340# endif /* VIM_BACKTICK */
11341
11342/*
11343 * Add a file to a file list. Accepted flags:
11344 * EW_DIR add directories
11345 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011346 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 * EW_NOTFOUND add even when it doesn't exist
11348 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011349 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 */
11351 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011352addfile(
11353 garray_T *gap,
11354 char_u *f, /* filename */
11355 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356{
11357 char_u *p;
11358 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011359 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011361 /* if the file/dir/link doesn't exist, may not add it */
11362 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011363 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 return;
11365
11366#ifdef FNAME_ILLEGAL
11367 /* if the file/dir contains illegal characters, don't add it */
11368 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11369 return;
11370#endif
11371
11372 isdir = mch_isdir(f);
11373 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11374 return;
11375
Bram Moolenaarb5971142015-03-21 17:32:19 +010011376 /* If the file isn't executable, may not add it. Do accept directories.
11377 * When invoked from expand_shellcmd() do not use $PATH. */
11378 if (!isdir && (flags & EW_EXEC)
11379 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011380 return;
11381
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 /* Make room for another item in the file list. */
11383 if (ga_grow(gap, 1) == FAIL)
11384 return;
11385
11386 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11387 if (p == NULL)
11388 return;
11389
11390 STRCPY(p, f);
11391#ifdef BACKSLASH_IN_FILENAME
11392 slash_adjust(p);
11393#endif
11394 /*
11395 * Append a slash or backslash after directory names if none is present.
11396 */
11397#ifndef DONT_ADD_PATHSEP_TO_DIR
11398 if (isdir && (flags & EW_ADDSLASH))
11399 add_pathsep(p);
11400#endif
11401 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402}
11403#endif /* !NO_EXPANDPATH */
11404
11405#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11406
11407#ifndef SEEK_SET
11408# define SEEK_SET 0
11409#endif
11410#ifndef SEEK_END
11411# define SEEK_END 2
11412#endif
11413
11414/*
11415 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011416 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11417 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 * Returns an allocated string, or NULL for error.
11419 */
11420 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011421get_cmd_output(
11422 char_u *cmd,
11423 char_u *infile, /* optional input file name */
11424 int flags, /* can be SHELL_SILENT */
11425 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426{
11427 char_u *tempname;
11428 char_u *command;
11429 char_u *buffer = NULL;
11430 int len;
11431 int i = 0;
11432 FILE *fd;
11433
11434 if (check_restricted() || check_secure())
11435 return NULL;
11436
11437 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011438 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 {
11440 EMSG(_(e_notmp));
11441 return NULL;
11442 }
11443
11444 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011445 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 if (command == NULL)
11447 goto done;
11448
11449 /*
11450 * Call the shell to execute the command (errors are ignored).
11451 * Don't check timestamps here.
11452 */
11453 ++no_check_timestamps;
11454 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11455 --no_check_timestamps;
11456
11457 vim_free(command);
11458
11459 /*
11460 * read the names from the file into memory
11461 */
11462# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011463 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 fd = mch_fopen((char *)tempname, "r");
11465# else
11466 fd = mch_fopen((char *)tempname, READBIN);
11467# endif
11468
11469 if (fd == NULL)
11470 {
11471 EMSG2(_(e_notopen), tempname);
11472 goto done;
11473 }
11474
11475 fseek(fd, 0L, SEEK_END);
11476 len = ftell(fd); /* get size of temp file */
11477 fseek(fd, 0L, SEEK_SET);
11478
11479 buffer = alloc(len + 1);
11480 if (buffer != NULL)
11481 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11482 fclose(fd);
11483 mch_remove(tempname);
11484 if (buffer == NULL)
11485 goto done;
11486#ifdef VMS
11487 len = i; /* VMS doesn't give us what we asked for... */
11488#endif
11489 if (i != len)
11490 {
11491 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011492 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011494 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011495 {
11496 /* Change NUL into SOH, otherwise the string is truncated. */
11497 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011498 if (buffer[i] == NUL)
11499 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011500
Bram Moolenaar162bd912010-07-28 22:29:10 +020011501 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011502 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011503 else
11504 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505
11506done:
11507 vim_free(tempname);
11508 return buffer;
11509}
11510#endif
11511
11512/*
11513 * Free the list of files returned by expand_wildcards() or other expansion
11514 * functions.
11515 */
11516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011517FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011519 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 while (count--)
11522 vim_free(files[count]);
11523 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524}
11525
11526/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011527 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 * Don't do this when still processing a command or a mapping.
11529 * Don't do this when inside a ":normal" command.
11530 */
11531 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011532goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533{
11534 return (p_im && stuff_empty() && typebuf_typed());
11535}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011536
11537/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011538 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011539 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11540 * - Remove any argument. E.g., "csh -f" -> "csh".
11541 * But don't allow a space in the path, so that this works:
11542 * "/usr/bin/csh --rcfile ~/.cshrc"
11543 * But don't do that for Windows, it's common to have a space in the path.
11544 */
11545 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011546get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011547{
11548 char_u *p;
11549
11550#ifdef WIN3264
11551 p = gettail(p_sh);
11552 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11553#else
11554 p = skiptowhite(p_sh);
11555 if (*p == NUL)
11556 {
11557 /* No white space, use the tail. */
11558 p = vim_strsave(gettail(p_sh));
11559 }
11560 else
11561 {
11562 char_u *p1, *p2;
11563
11564 /* Find the last path separator before the space. */
11565 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011566 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011567 if (vim_ispathsep(*p2))
11568 p1 = p2 + 1;
11569 p = vim_strnsave(p1, (int)(p - p1));
11570 }
11571#endif
11572 return p;
11573}