blob: 5242ca572917ed893227a2fdd5a9dba03252e169 [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 */
787#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
788 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
789 char_u *p;
790#endif
791 int saved_char = NUL; /* init for GCC */
792#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
793 pos_T *pos;
794#endif
795#ifdef FEAT_SMARTINDENT
796 int do_si = (!p_paste && curbuf->b_p_si
797# ifdef FEAT_CINDENT
798 && !curbuf->b_p_cin
799# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200800# ifdef FEAT_EVAL
801 && *curbuf->b_p_inde == NUL
802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 );
804 int no_si = FALSE; /* reset did_si afterwards */
805 int first_char = NUL; /* init for GCC */
806#endif
807#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
808 int vreplace_mode;
809#endif
810 int did_append; /* appended a new line */
811 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
812
813 /*
814 * make a copy of the current line so we can mess with it
815 */
816 saved_line = vim_strsave(ml_get_curline());
817 if (saved_line == NULL) /* out of memory! */
818 return FALSE;
819
820#ifdef FEAT_VREPLACE
821 if (State & VREPLACE_FLAG)
822 {
823 /*
824 * With VREPLACE we make a copy of the next line, which we will be
825 * starting to replace. First make the new line empty and let vim play
826 * with the indenting and comment leader to its heart's content. Then
827 * we grab what it ended up putting on the new line, put back the
828 * original line, and call ins_char() to put each new character onto
829 * the line, replacing what was there before and pushing the right
830 * stuff onto the replace stack. -- webb.
831 */
832 if (curwin->w_cursor.lnum < orig_line_count)
833 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
834 else
835 next_line = vim_strsave((char_u *)"");
836 if (next_line == NULL) /* out of memory! */
837 goto theend;
838
839 /*
840 * In VREPLACE mode, a NL replaces the rest of the line, and starts
841 * replacing the next line, so push all of the characters left on the
842 * line onto the replace stack. We'll push any other characters that
843 * might be replaced at the start of the next line (due to autoindent
844 * etc) a bit later.
845 */
846 replace_push(NUL); /* Call twice because BS over NL expects it */
847 replace_push(NUL);
848 p = saved_line + curwin->w_cursor.col;
849 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000850 {
851#ifdef FEAT_MBYTE
852 if (has_mbyte)
853 p += replace_push_mb(p);
854 else
855#endif
856 replace_push(*p++);
857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 saved_line[curwin->w_cursor.col] = NUL;
859 }
860#endif
861
862 if ((State & INSERT)
863#ifdef FEAT_VREPLACE
864 && !(State & VREPLACE_FLAG)
865#endif
866 )
867 {
868 p_extra = saved_line + curwin->w_cursor.col;
869#ifdef FEAT_SMARTINDENT
870 if (do_si) /* need first char after new line break */
871 {
872 p = skipwhite(p_extra);
873 first_char = *p;
874 }
875#endif
876#ifdef FEAT_COMMENTS
877 extra_len = (int)STRLEN(p_extra);
878#endif
879 saved_char = *p_extra;
880 *p_extra = NUL;
881 }
882
883 u_clearline(); /* cannot do "U" command when adding lines */
884#ifdef FEAT_SMARTINDENT
885 did_si = FALSE;
886#endif
887 ai_col = 0;
888
889 /*
890 * If we just did an auto-indent, then we didn't type anything on
891 * the prior line, and it should be truncated. Do this even if 'ai' is not
892 * set because automatically inserting a comment leader also sets did_ai.
893 */
894 if (dir == FORWARD && did_ai)
895 trunc_line = TRUE;
896
897 /*
898 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
899 * indent to use for the new line.
900 */
901 if (curbuf->b_p_ai
902#ifdef FEAT_SMARTINDENT
903 || do_si
904#endif
905 )
906 {
907 /*
908 * count white space on current line
909 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200910#ifdef FEAT_VARTABS
911 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
912 curbuf->b_p_vts_array, FALSE);
913#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200914 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200915#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200916 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
917 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
919#ifdef FEAT_SMARTINDENT
920 /*
921 * Do smart indenting.
922 * In insert/replace mode (only when dir == FORWARD)
923 * we may move some text to the next line. If it starts with '{'
924 * don't add an indent. Fixes inserting a NL before '{' in line
925 * "if (condition) {"
926 */
927 if (!trunc_line && do_si && *saved_line != NUL
928 && (p_extra == NULL || first_char != '{'))
929 {
930 char_u *ptr;
931 char_u last_char;
932
933 old_cursor = curwin->w_cursor;
934 ptr = saved_line;
935# ifdef FEAT_COMMENTS
936 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200937 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 else
939 lead_len = 0;
940# endif
941 if (dir == FORWARD)
942 {
943 /*
944 * Skip preprocessor directives, unless they are
945 * recognised as comments.
946 */
947 if (
948# ifdef FEAT_COMMENTS
949 lead_len == 0 &&
950# endif
951 ptr[0] == '#')
952 {
953 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
954 ptr = ml_get(--curwin->w_cursor.lnum);
955 newindent = get_indent();
956 }
957# ifdef FEAT_COMMENTS
958 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200959 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 else
961 lead_len = 0;
962 if (lead_len > 0)
963 {
964 /*
965 * This case gets the following right:
966 * \*
967 * * A comment (read '\' as '/').
968 * *\
969 * #define IN_THE_WAY
970 * This should line up here;
971 */
972 p = skipwhite(ptr);
973 if (p[0] == '/' && p[1] == '*')
974 p++;
975 if (p[0] == '*')
976 {
977 for (p++; *p; p++)
978 {
979 if (p[0] == '/' && p[-1] == '*')
980 {
981 /*
982 * End of C comment, indent should line up
983 * with the line containing the start of
984 * the comment
985 */
986 curwin->w_cursor.col = (colnr_T)(p - ptr);
987 if ((pos = findmatch(NULL, NUL)) != NULL)
988 {
989 curwin->w_cursor.lnum = pos->lnum;
990 newindent = get_indent();
991 }
992 }
993 }
994 }
995 }
996 else /* Not a comment line */
997# endif
998 {
999 /* Find last non-blank in line */
1000 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001001 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 --p;
1003 last_char = *p;
1004
1005 /*
1006 * find the character just before the '{' or ';'
1007 */
1008 if (last_char == '{' || last_char == ';')
1009 {
1010 if (p > ptr)
1011 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001012 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 --p;
1014 }
1015 /*
1016 * Try to catch lines that are split over multiple
1017 * lines. eg:
1018 * if (condition &&
1019 * condition) {
1020 * Should line up here!
1021 * }
1022 */
1023 if (*p == ')')
1024 {
1025 curwin->w_cursor.col = (colnr_T)(p - ptr);
1026 if ((pos = findmatch(NULL, '(')) != NULL)
1027 {
1028 curwin->w_cursor.lnum = pos->lnum;
1029 newindent = get_indent();
1030 ptr = ml_get_curline();
1031 }
1032 }
1033 /*
1034 * If last character is '{' do indent, without
1035 * checking for "if" and the like.
1036 */
1037 if (last_char == '{')
1038 {
1039 did_si = TRUE; /* do indent */
1040 no_si = TRUE; /* don't delete it when '{' typed */
1041 }
1042 /*
1043 * Look for "if" and the like, use 'cinwords'.
1044 * Don't do this if the previous line ended in ';' or
1045 * '}'.
1046 */
1047 else if (last_char != ';' && last_char != '}'
1048 && cin_is_cinword(ptr))
1049 did_si = TRUE;
1050 }
1051 }
1052 else /* dir == BACKWARD */
1053 {
1054 /*
1055 * Skip preprocessor directives, unless they are
1056 * recognised as comments.
1057 */
1058 if (
1059# ifdef FEAT_COMMENTS
1060 lead_len == 0 &&
1061# endif
1062 ptr[0] == '#')
1063 {
1064 int was_backslashed = FALSE;
1065
1066 while ((ptr[0] == '#' || was_backslashed) &&
1067 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1068 {
1069 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1070 was_backslashed = TRUE;
1071 else
1072 was_backslashed = FALSE;
1073 ptr = ml_get(++curwin->w_cursor.lnum);
1074 }
1075 if (was_backslashed)
1076 newindent = 0; /* Got to end of file */
1077 else
1078 newindent = get_indent();
1079 }
1080 p = skipwhite(ptr);
1081 if (*p == '}') /* if line starts with '}': do indent */
1082 did_si = TRUE;
1083 else /* can delete indent when '{' typed */
1084 can_si_back = TRUE;
1085 }
1086 curwin->w_cursor = old_cursor;
1087 }
1088 if (do_si)
1089 can_si = TRUE;
1090#endif /* FEAT_SMARTINDENT */
1091
1092 did_ai = TRUE;
1093 }
1094
1095#ifdef FEAT_COMMENTS
1096 /*
1097 * Find out if the current line starts with a comment leader.
1098 * This may then be inserted in front of the new line.
1099 */
1100 end_comment_pending = NUL;
1101 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001102 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 else
1104 lead_len = 0;
1105 if (lead_len > 0)
1106 {
1107 char_u *lead_repl = NULL; /* replaces comment leader */
1108 int lead_repl_len = 0; /* length of *lead_repl */
1109 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1110 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1111 char_u *comment_end = NULL; /* where lead_end has been found */
1112 int extra_space = FALSE; /* append extra space */
1113 int current_flag;
1114 int require_blank = FALSE; /* requires blank after middle */
1115 char_u *p2;
1116
1117 /*
1118 * If the comment leader has the start, middle or end flag, it may not
1119 * be used or may be replaced with the middle leader.
1120 */
1121 for (p = lead_flags; *p && *p != ':'; ++p)
1122 {
1123 if (*p == COM_BLANK)
1124 {
1125 require_blank = TRUE;
1126 continue;
1127 }
1128 if (*p == COM_START || *p == COM_MIDDLE)
1129 {
1130 current_flag = *p;
1131 if (*p == COM_START)
1132 {
1133 /*
1134 * Doing "O" on a start of comment does not insert leader.
1135 */
1136 if (dir == BACKWARD)
1137 {
1138 lead_len = 0;
1139 break;
1140 }
1141
1142 /* find start of middle part */
1143 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1144 require_blank = FALSE;
1145 }
1146
1147 /*
1148 * Isolate the strings of the middle and end leader.
1149 */
1150 while (*p && p[-1] != ':') /* find end of middle flags */
1151 {
1152 if (*p == COM_BLANK)
1153 require_blank = TRUE;
1154 ++p;
1155 }
1156 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1157
1158 while (*p && p[-1] != ':') /* find end of end flags */
1159 {
1160 /* Check whether we allow automatic ending of comments */
1161 if (*p == COM_AUTO_END)
1162 end_comment_pending = -1; /* means we want to set it */
1163 ++p;
1164 }
1165 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1166
1167 if (end_comment_pending == -1) /* we can set it now */
1168 end_comment_pending = lead_end[n - 1];
1169
1170 /*
1171 * If the end of the comment is in the same line, don't use
1172 * the comment leader.
1173 */
1174 if (dir == FORWARD)
1175 {
1176 for (p = saved_line + lead_len; *p; ++p)
1177 if (STRNCMP(p, lead_end, n) == 0)
1178 {
1179 comment_end = p;
1180 lead_len = 0;
1181 break;
1182 }
1183 }
1184
1185 /*
1186 * Doing "o" on a start of comment inserts the middle leader.
1187 */
1188 if (lead_len > 0)
1189 {
1190 if (current_flag == COM_START)
1191 {
1192 lead_repl = lead_middle;
1193 lead_repl_len = (int)STRLEN(lead_middle);
1194 }
1195
1196 /*
1197 * If we have hit RETURN immediately after the start
1198 * comment leader, then put a space after the middle
1199 * comment leader on the next line.
1200 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001201 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 && ((p_extra != NULL
1203 && (int)curwin->w_cursor.col == lead_len)
1204 || (p_extra == NULL
1205 && saved_line[lead_len] == NUL)
1206 || require_blank))
1207 extra_space = TRUE;
1208 }
1209 break;
1210 }
1211 if (*p == COM_END)
1212 {
1213 /*
1214 * Doing "o" on the end of a comment does not insert leader.
1215 * Remember where the end is, might want to use it to find the
1216 * start (for C-comments).
1217 */
1218 if (dir == FORWARD)
1219 {
1220 comment_end = skipwhite(saved_line);
1221 lead_len = 0;
1222 break;
1223 }
1224
1225 /*
1226 * Doing "O" on the end of a comment inserts the middle leader.
1227 * Find the string for the middle leader, searching backwards.
1228 */
1229 while (p > curbuf->b_p_com && *p != ',')
1230 --p;
1231 for (lead_repl = p; lead_repl > curbuf->b_p_com
1232 && lead_repl[-1] != ':'; --lead_repl)
1233 ;
1234 lead_repl_len = (int)(p - lead_repl);
1235
1236 /* We can probably always add an extra space when doing "O" on
1237 * the comment-end */
1238 extra_space = TRUE;
1239
1240 /* Check whether we allow automatic ending of comments */
1241 for (p2 = p; *p2 && *p2 != ':'; p2++)
1242 {
1243 if (*p2 == COM_AUTO_END)
1244 end_comment_pending = -1; /* means we want to set it */
1245 }
1246 if (end_comment_pending == -1)
1247 {
1248 /* Find last character in end-comment string */
1249 while (*p2 && *p2 != ',')
1250 p2++;
1251 end_comment_pending = p2[-1];
1252 }
1253 break;
1254 }
1255 if (*p == COM_FIRST)
1256 {
1257 /*
1258 * Comment leader for first line only: Don't repeat leader
1259 * when using "O", blank out leader when using "o".
1260 */
1261 if (dir == BACKWARD)
1262 lead_len = 0;
1263 else
1264 {
1265 lead_repl = (char_u *)"";
1266 lead_repl_len = 0;
1267 }
1268 break;
1269 }
1270 }
1271 if (lead_len)
1272 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001273 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001274 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001275 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 allocated = leader; /* remember to free it later */
1277
1278 if (leader == NULL)
1279 lead_len = 0;
1280 else
1281 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001282 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
1284 /*
1285 * Replace leader with lead_repl, right or left adjusted
1286 */
1287 if (lead_repl != NULL)
1288 {
1289 int c = 0;
1290 int off = 0;
1291
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001292 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {
1294 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001295 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 else if (VIM_ISDIGIT(*p) || *p == '-')
1297 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001298 else
1299 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301 if (c == COM_RIGHT) /* right adjusted leader */
1302 {
1303 /* find last non-white in the leader to line up with */
1304 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001305 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001308
1309#ifdef FEAT_MBYTE
1310 /* Compute the length of the replaced characters in
1311 * screen characters, not bytes. */
1312 {
1313 int repl_size = vim_strnsize(lead_repl,
1314 lead_repl_len);
1315 int old_size = 0;
1316 char_u *endp = p;
1317 int l;
1318
1319 while (old_size < repl_size && p > leader)
1320 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001321 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001322 old_size += ptr2cells(p);
1323 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001324 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001325 if (l != 0)
1326 mch_memmove(endp + l, endp,
1327 (size_t)((leader + lead_len) - endp));
1328 lead_len += l;
1329 }
1330#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (p < leader + lead_repl_len)
1332 p = leader;
1333 else
1334 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1337 if (p + lead_repl_len > leader + lead_len)
1338 p[lead_repl_len] = NUL;
1339
1340 /* blank-out any other chars from the old leader. */
1341 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001342 {
1343#ifdef FEAT_MBYTE
1344 int l = mb_head_off(leader, p);
1345
1346 if (l > 1)
1347 {
1348 p -= l;
1349 if (ptr2cells(p) > 1)
1350 {
1351 p[1] = ' ';
1352 --l;
1353 }
1354 mch_memmove(p + 1, p + l + 1,
1355 (size_t)((leader + lead_len) - (p + l + 1)));
1356 lead_len -= l;
1357 *p = ' ';
1358 }
1359 else
1360#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001361 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365 else /* left adjusted leader */
1366 {
1367 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001368#ifdef FEAT_MBYTE
1369 /* Compute the length of the replaced characters in
1370 * screen characters, not bytes. Move the part that is
1371 * not to be overwritten. */
1372 {
1373 int repl_size = vim_strnsize(lead_repl,
1374 lead_repl_len);
1375 int i;
1376 int l;
1377
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001378 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001379 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001380 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001381 if (vim_strnsize(p, i + l) > repl_size)
1382 break;
1383 }
1384 if (i != lead_repl_len)
1385 {
1386 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001387 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001388 lead_len += lead_repl_len - i;
1389 }
1390 }
1391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1393
1394 /* Replace any remaining non-white chars in the old
1395 * leader by spaces. Keep Tabs, the indent must
1396 * remain the same. */
1397 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001398 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 {
1400 /* Don't put a space before a TAB. */
1401 if (p + 1 < leader + lead_len && p[1] == TAB)
1402 {
1403 --lead_len;
1404 mch_memmove(p, p + 1,
1405 (leader + lead_len) - p);
1406 }
1407 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001408 {
1409#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001410 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001411
1412 if (l > 1)
1413 {
1414 if (ptr2cells(p) > 1)
1415 {
1416 /* Replace a double-wide char with
1417 * two spaces */
1418 --l;
1419 *p++ = ' ';
1420 }
1421 mch_memmove(p + 1, p + l,
1422 (leader + lead_len) - p);
1423 lead_len -= l - 1;
1424 }
1425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
1429 *p = NUL;
1430 }
1431
1432 /* Recompute the indent, it may have changed. */
1433 if (curbuf->b_p_ai
1434#ifdef FEAT_SMARTINDENT
1435 || do_si
1436#endif
1437 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001438#ifdef FEAT_VARTABS
1439 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1440 curbuf->b_p_vts_array, FALSE);
1441#else
1442 newindent = get_indent_str(leader,
1443 (int)curbuf->b_p_ts, FALSE);
1444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 /* Add the indent offset */
1447 if (newindent + off < 0)
1448 {
1449 off = -newindent;
1450 newindent = 0;
1451 }
1452 else
1453 newindent += off;
1454
1455 /* Correct trailing spaces for the shift, so that
1456 * alignment remains equal. */
1457 while (off > 0 && lead_len > 0
1458 && leader[lead_len - 1] == ' ')
1459 {
1460 /* Don't do it when there is a tab before the space */
1461 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1462 break;
1463 --lead_len;
1464 --off;
1465 }
1466
1467 /* If the leader ends in white space, don't add an
1468 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001469 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 extra_space = FALSE;
1471 leader[lead_len] = NUL;
1472 }
1473
1474 if (extra_space)
1475 {
1476 leader[lead_len++] = ' ';
1477 leader[lead_len] = NUL;
1478 }
1479
1480 newcol = lead_len;
1481
1482 /*
1483 * if a new indent will be set below, remove the indent that
1484 * is in the comment leader
1485 */
1486 if (newindent
1487#ifdef FEAT_SMARTINDENT
1488 || did_si
1489#endif
1490 )
1491 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001492 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
1494 --lead_len;
1495 --newcol;
1496 ++leader;
1497 }
1498 }
1499
1500 }
1501#ifdef FEAT_SMARTINDENT
1502 did_si = can_si = FALSE;
1503#endif
1504 }
1505 else if (comment_end != NULL)
1506 {
1507 /*
1508 * We have finished a comment, so we don't use the leader.
1509 * If this was a C-comment and 'ai' or 'si' is set do a normal
1510 * indent to align with the line containing the start of the
1511 * comment.
1512 */
1513 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1514 (curbuf->b_p_ai
1515#ifdef FEAT_SMARTINDENT
1516 || do_si
1517#endif
1518 ))
1519 {
1520 old_cursor = curwin->w_cursor;
1521 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1522 if ((pos = findmatch(NULL, NUL)) != NULL)
1523 {
1524 curwin->w_cursor.lnum = pos->lnum;
1525 newindent = get_indent();
1526 }
1527 curwin->w_cursor = old_cursor;
1528 }
1529 }
1530 }
1531#endif
1532
1533 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1534 if (p_extra != NULL)
1535 {
1536 *p_extra = saved_char; /* restore char that NUL replaced */
1537
1538 /*
1539 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1540 * non-blank.
1541 *
1542 * When in REPLACE mode, put the deleted blanks on the replace stack,
1543 * preceded by a NUL, so they can be put back when a BS is entered.
1544 */
1545 if (REPLACE_NORMAL(State))
1546 replace_push(NUL); /* end of extra blanks */
1547 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1548 {
1549 while ((*p_extra == ' ' || *p_extra == '\t')
1550#ifdef FEAT_MBYTE
1551 && (!enc_utf8
1552 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1553#endif
1554 )
1555 {
1556 if (REPLACE_NORMAL(State))
1557 replace_push(*p_extra);
1558 ++p_extra;
1559 ++less_cols_off;
1560 }
1561 }
1562 if (*p_extra != NUL)
1563 did_ai = FALSE; /* append some text, don't truncate now */
1564
1565 /* columns for marks adjusted for removed columns */
1566 less_cols = (int)(p_extra - saved_line);
1567 }
1568
1569 if (p_extra == NULL)
1570 p_extra = (char_u *)""; /* append empty line */
1571
1572#ifdef FEAT_COMMENTS
1573 /* concatenate leader and p_extra, if there is a leader */
1574 if (lead_len)
1575 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001576 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1577 {
1578 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001579 int padding = second_line_indent
1580 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001581
1582 /* Here whitespace is inserted after the comment char.
1583 * Below, set_indent(newindent, SIN_INSERT) will insert the
1584 * whitespace needed before the comment char. */
1585 for (i = 0; i < padding; i++)
1586 {
1587 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001588 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001589 newcol++;
1590 }
1591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 STRCAT(leader, p_extra);
1593 p_extra = leader;
1594 did_ai = TRUE; /* So truncating blanks works with comments */
1595 less_cols -= lead_len;
1596 }
1597 else
1598 end_comment_pending = NUL; /* turns out there was no leader */
1599#endif
1600
1601 old_cursor = curwin->w_cursor;
1602 if (dir == BACKWARD)
1603 --curwin->w_cursor.lnum;
1604#ifdef FEAT_VREPLACE
1605 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1606#endif
1607 {
1608 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1609 == FAIL)
1610 goto theend;
1611 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001612 * with markers.
1613 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001614 * be marks there. But still needed in diff mode. */
1615 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1616#ifdef FEAT_DIFF
1617 || curwin->w_p_diff
1618#endif
1619 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001620 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 did_append = TRUE;
1622 }
1623#ifdef FEAT_VREPLACE
1624 else
1625 {
1626 /*
1627 * In VREPLACE mode we are starting to replace the next line.
1628 */
1629 curwin->w_cursor.lnum++;
1630 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1631 {
1632 /* In case we NL to a new line, BS to the previous one, and NL
1633 * again, we don't want to save the new line for undo twice.
1634 */
1635 (void)u_save_cursor(); /* errors are ignored! */
1636 vr_lines_changed++;
1637 }
1638 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1639 changed_bytes(curwin->w_cursor.lnum, 0);
1640 curwin->w_cursor.lnum--;
1641 did_append = FALSE;
1642 }
1643#endif
1644
1645 if (newindent
1646#ifdef FEAT_SMARTINDENT
1647 || did_si
1648#endif
1649 )
1650 {
1651 ++curwin->w_cursor.lnum;
1652#ifdef FEAT_SMARTINDENT
1653 if (did_si)
1654 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001655 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001656
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001658 newindent -= newindent % sw;
1659 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001662 /* Copy the indent */
1663 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
1665 (void)copy_indent(newindent, saved_line);
1666
1667 /*
1668 * Set the 'preserveindent' option so that any further screwing
1669 * with the line doesn't entirely destroy our efforts to preserve
1670 * it. It gets restored at the function end.
1671 */
1672 curbuf->b_p_pi = TRUE;
1673 }
1674 else
1675 (void)set_indent(newindent, SIN_INSERT);
1676 less_cols -= curwin->w_cursor.col;
1677
1678 ai_col = curwin->w_cursor.col;
1679
1680 /*
1681 * In REPLACE mode, for each character in the new indent, there must
1682 * be a NUL on the replace stack, for when it is deleted with BS
1683 */
1684 if (REPLACE_NORMAL(State))
1685 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1686 replace_push(NUL);
1687 newcol += curwin->w_cursor.col;
1688#ifdef FEAT_SMARTINDENT
1689 if (no_si)
1690 did_si = FALSE;
1691#endif
1692 }
1693
1694#ifdef FEAT_COMMENTS
1695 /*
1696 * In REPLACE mode, for each character in the extra leader, there must be
1697 * a NUL on the replace stack, for when it is deleted with BS.
1698 */
1699 if (REPLACE_NORMAL(State))
1700 while (lead_len-- > 0)
1701 replace_push(NUL);
1702#endif
1703
1704 curwin->w_cursor = old_cursor;
1705
1706 if (dir == FORWARD)
1707 {
1708 if (trunc_line || (State & INSERT))
1709 {
1710 /* truncate current line at cursor */
1711 saved_line[curwin->w_cursor.col] = NUL;
1712 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1713 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1714 truncate_spaces(saved_line);
1715 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1716 saved_line = NULL;
1717 if (did_append)
1718 {
1719 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1720 curwin->w_cursor.lnum + 1, 1L);
1721 did_append = FALSE;
1722
1723 /* Move marks after the line break to the new line. */
1724 if (flags & OPENLINE_MARKFIX)
1725 mark_col_adjust(curwin->w_cursor.lnum,
1726 curwin->w_cursor.col + less_cols_off,
1727 1L, (long)-less_cols);
1728 }
1729 else
1730 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1731 }
1732
1733 /*
1734 * Put the cursor on the new line. Careful: the scrollup() above may
1735 * have moved w_cursor, we must use old_cursor.
1736 */
1737 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1738 }
1739 if (did_append)
1740 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1741
1742 curwin->w_cursor.col = newcol;
1743#ifdef FEAT_VIRTUALEDIT
1744 curwin->w_cursor.coladd = 0;
1745#endif
1746
1747#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1748 /*
1749 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1750 * fixthisline() from doing it (via change_indent()) by telling it we're in
1751 * normal INSERT mode.
1752 */
1753 if (State & VREPLACE_FLAG)
1754 {
1755 vreplace_mode = State; /* So we know to put things right later */
1756 State = INSERT;
1757 }
1758 else
1759 vreplace_mode = 0;
1760#endif
1761#ifdef FEAT_LISP
1762 /*
1763 * May do lisp indenting.
1764 */
1765 if (!p_paste
1766# ifdef FEAT_COMMENTS
1767 && leader == NULL
1768# endif
1769 && curbuf->b_p_lisp
1770 && curbuf->b_p_ai)
1771 {
1772 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001773 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775#endif
1776#ifdef FEAT_CINDENT
1777 /*
1778 * May do indenting after opening a new line.
1779 */
1780 if (!p_paste
1781 && (curbuf->b_p_cin
1782# ifdef FEAT_EVAL
1783 || *curbuf->b_p_inde != NUL
1784# endif
1785 )
1786 && in_cinkeys(dir == FORWARD
1787 ? KEY_OPEN_FORW
1788 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1789 {
1790 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001791 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 }
1793#endif
1794#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1795 if (vreplace_mode != 0)
1796 State = vreplace_mode;
1797#endif
1798
1799#ifdef FEAT_VREPLACE
1800 /*
1801 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1802 * original line, and inserts the new stuff char by char, pushing old stuff
1803 * onto the replace stack (via ins_char()).
1804 */
1805 if (State & VREPLACE_FLAG)
1806 {
1807 /* Put new line in p_extra */
1808 p_extra = vim_strsave(ml_get_curline());
1809 if (p_extra == NULL)
1810 goto theend;
1811
1812 /* Put back original line */
1813 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1814
1815 /* Insert new stuff into line again */
1816 curwin->w_cursor.col = 0;
1817#ifdef FEAT_VIRTUALEDIT
1818 curwin->w_cursor.coladd = 0;
1819#endif
1820 ins_bytes(p_extra); /* will call changed_bytes() */
1821 vim_free(p_extra);
1822 next_line = NULL;
1823 }
1824#endif
1825
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001826 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827theend:
1828 curbuf->b_p_pi = saved_pi;
1829 vim_free(saved_line);
1830 vim_free(next_line);
1831 vim_free(allocated);
1832 return retval;
1833}
1834
1835#if defined(FEAT_COMMENTS) || defined(PROTO)
1836/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001837 * get_leader_len() returns the length in bytes of the prefix of the given
1838 * string which introduces a comment. If this string is not a comment then
1839 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 * When "flags" is not NULL, it is set to point to the flags of the recognized
1841 * comment leader.
1842 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001843 * If "include_space" is set, include trailing whitespace while calculating the
1844 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001847get_leader_len(
1848 char_u *line,
1849 char_u **flags,
1850 int backward,
1851 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001854 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 int got_com = FALSE;
1856 int found_one;
1857 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1858 char_u *string; /* pointer to comment string */
1859 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001860 int middle_match_len = 0;
1861 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001862 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaar81340392012-06-06 16:12:59 +02001864 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001865 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 ++i;
1867
1868 /*
1869 * Repeat to match several nested comment strings.
1870 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001871 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
1873 /*
1874 * scan through the 'comments' option for a match
1875 */
1876 found_one = FALSE;
1877 for (list = curbuf->b_p_com; *list; )
1878 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001879 /* Get one option part into part_buf[]. Advance "list" to next
1880 * one. Put "string" at start of string. */
1881 if (!got_com && flags != NULL)
1882 *flags = list; /* remember where flags started */
1883 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1885 string = vim_strchr(part_buf, ':');
1886 if (string == NULL) /* missing ':', ignore this part */
1887 continue;
1888 *string++ = NUL; /* isolate flags from string */
1889
Bram Moolenaara4271d52011-05-10 13:38:27 +02001890 /* If we found a middle match previously, use that match when this
1891 * is not a middle or end. */
1892 if (middle_match_len != 0
1893 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1894 && vim_strchr(part_buf, COM_END) == NULL)
1895 break;
1896
1897 /* When we already found a nested comment, only accept further
1898 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1900 continue;
1901
Bram Moolenaara4271d52011-05-10 13:38:27 +02001902 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1904 continue;
1905
Bram Moolenaara4271d52011-05-10 13:38:27 +02001906 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 * When string starts with white space, must have some white space
1908 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001909 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001910 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001912 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001913 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001914 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 ++string;
1916 }
1917 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1918 ;
1919 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001920 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
Bram Moolenaara4271d52011-05-10 13:38:27 +02001922 /* When 'b' flag used, there must be white space or an
1923 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001925 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 continue;
1927
Bram Moolenaara4271d52011-05-10 13:38:27 +02001928 /* We have found a match, stop searching unless this is a middle
1929 * comment. The middle comment can be a substring of the end
1930 * comment in which case it's better to return the length of the
1931 * end comment and its flags. Thus we keep searching with middle
1932 * and end matches and use an end match if it matches better. */
1933 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1934 {
1935 if (middle_match_len == 0)
1936 {
1937 middle_match_len = j;
1938 saved_flags = prev_list;
1939 }
1940 continue;
1941 }
1942 if (middle_match_len != 0 && j > middle_match_len)
1943 /* Use this match instead of the middle match, since it's a
1944 * longer thus better match. */
1945 middle_match_len = 0;
1946
1947 if (middle_match_len == 0)
1948 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 found_one = TRUE;
1950 break;
1951 }
1952
Bram Moolenaara4271d52011-05-10 13:38:27 +02001953 if (middle_match_len != 0)
1954 {
1955 /* Use the previously found middle match after failing to find a
1956 * match with an end. */
1957 if (!got_com && flags != NULL)
1958 *flags = saved_flags;
1959 i += middle_match_len;
1960 found_one = TRUE;
1961 }
1962
1963 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (!found_one)
1965 break;
1966
Bram Moolenaar81340392012-06-06 16:12:59 +02001967 result = i;
1968
Bram Moolenaara4271d52011-05-10 13:38:27 +02001969 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001970 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 ++i;
1972
Bram Moolenaar81340392012-06-06 16:12:59 +02001973 if (include_space)
1974 result = i;
1975
Bram Moolenaara4271d52011-05-10 13:38:27 +02001976 /* If this comment doesn't nest, stop here. */
1977 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (vim_strchr(part_buf, COM_NEST) == NULL)
1979 break;
1980 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001981 return result;
1982}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001983
Bram Moolenaar81340392012-06-06 16:12:59 +02001984/*
1985 * Return the offset at which the last comment in line starts. If there is no
1986 * comment in the whole line, -1 is returned.
1987 *
1988 * When "flags" is not null, it is set to point to the flags describing the
1989 * recognized comment leader.
1990 */
1991 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001992get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001993{
1994 int result = -1;
1995 int i, j;
1996 int lower_check_bound = 0;
1997 char_u *string;
1998 char_u *com_leader;
1999 char_u *com_flags;
2000 char_u *list;
2001 int found_one;
2002 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
2003
2004 /*
2005 * Repeat to match several nested comment strings.
2006 */
2007 i = (int)STRLEN(line);
2008 while (--i >= lower_check_bound)
2009 {
2010 /*
2011 * scan through the 'comments' option for a match
2012 */
2013 found_one = FALSE;
2014 for (list = curbuf->b_p_com; *list; )
2015 {
2016 char_u *flags_save = list;
2017
2018 /*
2019 * Get one option part into part_buf[]. Advance list to next one.
2020 * put string at start of string.
2021 */
2022 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2023 string = vim_strchr(part_buf, ':');
2024 if (string == NULL) /* If everything is fine, this cannot actually
2025 * happen. */
2026 {
2027 continue;
2028 }
2029 *string++ = NUL; /* Isolate flags from string. */
2030 com_leader = string;
2031
2032 /*
2033 * Line contents and string must match.
2034 * When string starts with white space, must have some white space
2035 * (but the amount does not need to match, there might be a mix of
2036 * TABs and spaces).
2037 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002038 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002039 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002040 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002041 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002042 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002043 ++string;
2044 }
2045 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2046 /* do nothing */;
2047 if (string[j] != NUL)
2048 continue;
2049
2050 /*
2051 * When 'b' flag used, there must be white space or an
2052 * end-of-line after the string in the line.
2053 */
2054 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002055 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002056 {
2057 continue;
2058 }
2059
2060 /*
2061 * We have found a match, stop searching.
2062 */
2063 found_one = TRUE;
2064
2065 if (flags)
2066 *flags = flags_save;
2067 com_flags = flags_save;
2068
2069 break;
2070 }
2071
2072 if (found_one)
2073 {
2074 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2075 int len1, len2, off;
2076
2077 result = i;
2078 /*
2079 * If this comment nests, continue searching.
2080 */
2081 if (vim_strchr(part_buf, COM_NEST) != NULL)
2082 continue;
2083
2084 lower_check_bound = i;
2085
2086 /* Let's verify whether the comment leader found is a substring
2087 * of other comment leaders. If it is, let's adjust the
2088 * lower_check_bound so that we make sure that we have determined
2089 * the comment leader correctly.
2090 */
2091
Bram Moolenaar1c465442017-03-12 20:10:05 +01002092 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002093 ++com_leader;
2094 len1 = (int)STRLEN(com_leader);
2095
2096 for (list = curbuf->b_p_com; *list; )
2097 {
2098 char_u *flags_save = list;
2099
2100 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2101 if (flags_save == com_flags)
2102 continue;
2103 string = vim_strchr(part_buf2, ':');
2104 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002105 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002106 ++string;
2107 len2 = (int)STRLEN(string);
2108 if (len2 == 0)
2109 continue;
2110
2111 /* Now we have to verify whether string ends with a substring
2112 * beginning the com_leader. */
2113 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2114 {
2115 --off;
2116 if (!STRNCMP(string + off, com_leader, len2 - off))
2117 {
2118 if (i - off < lower_check_bound)
2119 lower_check_bound = i - off;
2120 }
2121 }
2122 }
2123 }
2124 }
2125 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126}
2127#endif
2128
2129/*
2130 * Return the number of window lines occupied by buffer line "lnum".
2131 */
2132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002133plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135 return plines_win(curwin, lnum, TRUE);
2136}
2137
2138 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139plines_win(
2140 win_T *wp,
2141 linenr_T lnum,
2142 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144#if defined(FEAT_DIFF) || defined(PROTO)
2145 /* Check for filler lines above this buffer line. When folded the result
2146 * is one line anyway. */
2147 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2148}
2149
2150 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002151plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 return plines_win_nofill(curwin, lnum, TRUE);
2154}
2155
2156 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002157plines_win_nofill(
2158 win_T *wp,
2159 linenr_T lnum,
2160 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162#endif
2163 int lines;
2164
2165 if (!wp->w_p_wrap)
2166 return 1;
2167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (wp->w_width == 0)
2169 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171#ifdef FEAT_FOLDING
2172 /* A folded lines is handled just like an empty line. */
2173 /* NOTE: Caller must handle lines that are MAYBE folded. */
2174 if (lineFolded(wp, lnum) == TRUE)
2175 return 1;
2176#endif
2177
2178 lines = plines_win_nofold(wp, lnum);
2179 if (winheight > 0 && lines > wp->w_height)
2180 return (int)wp->w_height;
2181 return lines;
2182}
2183
2184/*
2185 * Return number of window lines physical line "lnum" will occupy in window
2186 * "wp". Does not care about folding, 'wrap' or 'diff'.
2187 */
2188 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002189plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 char_u *s;
2192 long col;
2193 int width;
2194
2195 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2196 if (*s == NUL) /* empty line */
2197 return 1;
2198 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2199
2200 /*
2201 * If list mode is on, then the '$' at the end of the line may take up one
2202 * extra column.
2203 */
2204 if (wp->w_p_list && lcs_eol != NUL)
2205 col += 1;
2206
2207 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002208 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002210 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (width <= 0)
2212 return 32000;
2213 if (col <= width)
2214 return 1;
2215 col -= width;
2216 width += win_col_off2(wp);
2217 return (col + (width - 1)) / width + 1;
2218}
2219
2220/*
2221 * Like plines_win(), but only reports the number of physical screen lines
2222 * used from the start of the line to the given column number.
2223 */
2224 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002225plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227 long col;
2228 char_u *s;
2229 int lines = 0;
2230 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002231 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233#ifdef FEAT_DIFF
2234 /* Check for filler lines above this buffer line. When folded the result
2235 * is one line anyway. */
2236 lines = diff_check_fill(wp, lnum);
2237#endif
2238
2239 if (!wp->w_p_wrap)
2240 return lines + 1;
2241
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (wp->w_width == 0)
2243 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
Bram Moolenaar597a4222014-06-25 14:39:50 +02002245 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247 col = 0;
2248 while (*s != NUL && --column >= 0)
2249 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002250 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002251 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253
2254 /*
2255 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2256 * INSERT mode, then col must be adjusted so that it represents the last
2257 * screen position of the TAB. This only fixes an error when the TAB wraps
2258 * from one screen line to the next (when 'columns' is not a multiple of
2259 * 'ts') -- webb.
2260 */
2261 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002262 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002265 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002267 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002268 if (width <= 0)
2269 return 9999;
2270
2271 lines += 1;
2272 if (col > width)
2273 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2274 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275}
2276
2277 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002278plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 int count = 0;
2281
2282 while (first <= last)
2283 {
2284#ifdef FEAT_FOLDING
2285 int x;
2286
2287 /* Check if there are any really folded lines, but also included lines
2288 * that are maybe folded. */
2289 x = foldedCount(wp, first, NULL);
2290 if (x > 0)
2291 {
2292 ++count; /* count 1 for "+-- folded" line */
2293 first += x;
2294 }
2295 else
2296#endif
2297 {
2298#ifdef FEAT_DIFF
2299 if (first == wp->w_topline)
2300 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2301 else
2302#endif
2303 count += plines_win(wp, first, TRUE);
2304 ++first;
2305 }
2306 }
2307 return (count);
2308}
2309
2310#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2311/*
2312 * Insert string "p" at the cursor position. Stops at a NUL byte.
2313 * Handles Replace mode and multi-byte characters.
2314 */
2315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002316ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 ins_bytes_len(p, (int)STRLEN(p));
2319}
2320#endif
2321
2322#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2323 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2324/*
2325 * Insert string "p" with length "len" at the cursor position.
2326 * Handles Replace mode and multi-byte characters.
2327 */
2328 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002329ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
2331 int i;
2332# ifdef FEAT_MBYTE
2333 int n;
2334
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002335 if (has_mbyte)
2336 for (i = 0; i < len; i += n)
2337 {
2338 if (enc_utf8)
2339 /* avoid reading past p[len] */
2340 n = utfc_ptr2len_len(p + i, len - i);
2341 else
2342 n = (*mb_ptr2len)(p + i);
2343 ins_char_bytes(p + i, n);
2344 }
2345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002347 for (i = 0; i < len; ++i)
2348 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349}
2350#endif
2351
2352/*
2353 * Insert or replace a single character at the cursor position.
2354 * When in REPLACE or VREPLACE mode, replace any existing character.
2355 * Caller must have prepared for undo.
2356 * For multi-byte characters we get the whole character, the caller must
2357 * convert bytes to a character.
2358 */
2359 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002360ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002362 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002363 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002365#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 n = (*mb_char2bytes)(c, buf);
2367
2368 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2369 * Happens for CTRL-Vu9900. */
2370 if (buf[0] == 0)
2371 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002372#else
2373 buf[0] = c;
2374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 ins_char_bytes(buf, n);
2377}
2378
2379 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002380ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 int newlen; /* nr of bytes inserted */
2384 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2385 char_u *p;
2386 char_u *newp;
2387 char_u *oldp;
2388 int linelen; /* length of old line including NUL */
2389 colnr_T col;
2390 linenr_T lnum = curwin->w_cursor.lnum;
2391 int i;
2392
2393#ifdef FEAT_VIRTUALEDIT
2394 /* Break tabs if needed. */
2395 if (virtual_active() && curwin->w_cursor.coladd > 0)
2396 coladvance_force(getviscol());
2397#endif
2398
2399 col = curwin->w_cursor.col;
2400 oldp = ml_get(lnum);
2401 linelen = (int)STRLEN(oldp) + 1;
2402
2403 /* The lengths default to the values for when not replacing. */
2404 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407 if (State & REPLACE_FLAG)
2408 {
2409#ifdef FEAT_VREPLACE
2410 if (State & VREPLACE_FLAG)
2411 {
2412 colnr_T new_vcol = 0; /* init for GCC */
2413 colnr_T vcol;
2414 int old_list;
2415#ifndef FEAT_MBYTE
2416 char_u buf[2];
2417#endif
2418
2419 /*
2420 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2421 * Returns the old value of list, so when finished,
2422 * curwin->w_p_list should be set back to this.
2423 */
2424 old_list = curwin->w_p_list;
2425 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2426 curwin->w_p_list = FALSE;
2427
2428 /*
2429 * In virtual replace mode each character may replace one or more
2430 * characters (zero if it's a TAB). Count the number of bytes to
2431 * be deleted to make room for the new character, counting screen
2432 * cells. May result in adding spaces to fill a gap.
2433 */
2434 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2435#ifndef FEAT_MBYTE
2436 buf[0] = c;
2437 buf[1] = NUL;
2438#endif
2439 new_vcol = vcol + chartabsize(buf, vcol);
2440 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2441 {
2442 vcol += chartabsize(oldp + col + oldlen, vcol);
2443 /* Don't need to remove a TAB that takes us to the right
2444 * position. */
2445 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2446 break;
2447#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002448 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449#else
2450 ++oldlen;
2451#endif
2452 /* Deleted a bit too much, insert spaces. */
2453 if (vcol > new_vcol)
2454 newlen += vcol - new_vcol;
2455 }
2456 curwin->w_p_list = old_list;
2457 }
2458 else
2459#endif
2460 if (oldp[col] != NUL)
2461 {
2462 /* normal replace */
2463#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002464 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465#else
2466 oldlen = 1;
2467#endif
2468 }
2469
2470
2471 /* Push the replaced bytes onto the replace stack, so that they can be
2472 * put back when BS is used. The bytes of a multi-byte character are
2473 * done the other way around, so that the first byte is popped off
2474 * first (it tells the byte length of the character). */
2475 replace_push(NUL);
2476 for (i = 0; i < oldlen; ++i)
2477 {
2478#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002479 if (has_mbyte)
2480 i += replace_push_mb(oldp + col + i) - 1;
2481 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002483 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
2485 }
2486
2487 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2488 if (newp == NULL)
2489 return;
2490
2491 /* Copy bytes before the cursor. */
2492 if (col > 0)
2493 mch_memmove(newp, oldp, (size_t)col);
2494
2495 /* Copy bytes after the changed character(s). */
2496 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002497 if (linelen > col + oldlen)
2498 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 (size_t)(linelen - col - oldlen));
2500
2501 /* Insert or overwrite the new character. */
2502#ifdef FEAT_MBYTE
2503 mch_memmove(p, buf, charlen);
2504 i = charlen;
2505#else
2506 *p = c;
2507 i = 1;
2508#endif
2509
2510 /* Fill with spaces when necessary. */
2511 while (i < newlen)
2512 p[i++] = ' ';
2513
2514 /* Replace the line in the buffer. */
2515 ml_replace(lnum, newp, FALSE);
2516
2517 /* mark the buffer as changed and prepare for displaying */
2518 changed_bytes(lnum, col);
2519
2520 /*
2521 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2522 * show the match for right parens and braces.
2523 */
2524 if (p_sm && (State & INSERT)
2525 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002526#ifdef FEAT_INS_EXPAND
2527 && !ins_compl_active()
2528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002530 {
2531#ifdef FEAT_MBYTE
2532 if (has_mbyte)
2533 showmatch(mb_ptr2char(buf));
2534 else
2535#endif
2536 showmatch(c);
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
2539#ifdef FEAT_RIGHTLEFT
2540 if (!p_ri || (State & REPLACE_FLAG))
2541#endif
2542 {
2543 /* Normal insert: move cursor right */
2544#ifdef FEAT_MBYTE
2545 curwin->w_cursor.col += charlen;
2546#else
2547 ++curwin->w_cursor.col;
2548#endif
2549 }
2550 /*
2551 * TODO: should try to update w_row here, to avoid recomputing it later.
2552 */
2553}
2554
2555/*
2556 * Insert a string at the cursor position.
2557 * Note: Does NOT handle Replace mode.
2558 * Caller must have prepared for undo.
2559 */
2560 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002561ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 char_u *oldp, *newp;
2564 int newlen = (int)STRLEN(s);
2565 int oldlen;
2566 colnr_T col;
2567 linenr_T lnum = curwin->w_cursor.lnum;
2568
2569#ifdef FEAT_VIRTUALEDIT
2570 if (virtual_active() && curwin->w_cursor.coladd > 0)
2571 coladvance_force(getviscol());
2572#endif
2573
2574 col = curwin->w_cursor.col;
2575 oldp = ml_get(lnum);
2576 oldlen = (int)STRLEN(oldp);
2577
2578 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2579 if (newp == NULL)
2580 return;
2581 if (col > 0)
2582 mch_memmove(newp, oldp, (size_t)col);
2583 mch_memmove(newp + col, s, (size_t)newlen);
2584 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2585 ml_replace(lnum, newp, FALSE);
2586 changed_bytes(lnum, col);
2587 curwin->w_cursor.col += newlen;
2588}
2589
2590/*
2591 * Delete one character under the cursor.
2592 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2593 * Caller must have prepared for undo.
2594 *
2595 * return FAIL for failure, OK otherwise
2596 */
2597 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002598del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600#ifdef FEAT_MBYTE
2601 if (has_mbyte)
2602 {
2603 /* Make sure the cursor is at the start of a character. */
2604 mb_adjust_cursor();
2605 if (*ml_get_cursor() == NUL)
2606 return FAIL;
2607 return del_chars(1L, fixpos);
2608 }
2609#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002610 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611}
2612
2613#if defined(FEAT_MBYTE) || defined(PROTO)
2614/*
2615 * Like del_bytes(), but delete characters instead of bytes.
2616 */
2617 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002618del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
2620 long bytes = 0;
2621 long i;
2622 char_u *p;
2623 int l;
2624
2625 p = ml_get_cursor();
2626 for (i = 0; i < count && *p != NUL; ++i)
2627 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002628 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 bytes += l;
2630 p += l;
2631 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002632 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633}
2634#endif
2635
2636/*
2637 * Delete "count" bytes under the cursor.
2638 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2639 * Caller must have prepared for undo.
2640 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002641 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 */
2643 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002644del_bytes(
2645 long count,
2646 int fixpos_arg,
2647 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 char_u *oldp, *newp;
2650 colnr_T oldlen;
2651 linenr_T lnum = curwin->w_cursor.lnum;
2652 colnr_T col = curwin->w_cursor.col;
2653 int was_alloced;
2654 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002655 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657 oldp = ml_get(lnum);
2658 oldlen = (int)STRLEN(oldp);
2659
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002660 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (col >= oldlen)
2662 return FAIL;
2663
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002664 /* If "count" is zero there is nothing to do. */
2665 if (count == 0)
2666 return OK;
2667
2668 /* If "count" is negative the caller must be doing something wrong. */
2669 if (count < 1)
2670 {
2671 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2672 return FAIL;
2673 }
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675#ifdef FEAT_MBYTE
2676 /* If 'delcombine' is set and deleting (less than) one character, only
2677 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002678 if (p_deco && use_delcombine && enc_utf8
2679 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002681 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int n;
2683
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002684 (void)utfc_ptr2char(oldp + col, cc);
2685 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
2687 /* Find the last composing char, there can be several. */
2688 n = col;
2689 do
2690 {
2691 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002692 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 n += count;
2694 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2695 fixpos = 0;
2696 }
2697 }
2698#endif
2699
2700 /*
2701 * When count is too big, reduce it.
2702 */
2703 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2704 if (movelen <= 1)
2705 {
2706 /*
2707 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002708 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2709 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002711 if (col > 0 && fixpos && restart_edit == 0
2712#ifdef FEAT_VIRTUALEDIT
2713 && (ve_flags & VE_ONEMORE) == 0
2714#endif
2715 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
2717 --curwin->w_cursor.col;
2718#ifdef FEAT_VIRTUALEDIT
2719 curwin->w_cursor.coladd = 0;
2720#endif
2721#ifdef FEAT_MBYTE
2722 if (has_mbyte)
2723 curwin->w_cursor.col -=
2724 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2725#endif
2726 }
2727 count = oldlen - col;
2728 movelen = 1;
2729 }
2730
2731 /*
2732 * If the old line has been allocated the deletion can be done in the
2733 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002734 * Can't do this when using Netbeans, because we would need to invoke
2735 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002736 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002739 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002740 was_alloced = FALSE;
2741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002743 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (was_alloced)
2745 newp = oldp; /* use same allocated memory */
2746 else
2747 { /* need to allocate a new line */
2748 newp = alloc((unsigned)(oldlen + 1 - count));
2749 if (newp == NULL)
2750 return FAIL;
2751 mch_memmove(newp, oldp, (size_t)col);
2752 }
2753 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2754 if (!was_alloced)
2755 ml_replace(lnum, newp, FALSE);
2756
2757 /* mark the buffer as changed and prepare for displaying */
2758 changed_bytes(lnum, curwin->w_cursor.col);
2759
2760 return OK;
2761}
2762
2763/*
2764 * Delete from cursor to end of line.
2765 * Caller must have prepared for undo.
2766 *
2767 * return FAIL for failure, OK otherwise
2768 */
2769 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002770truncate_line(
2771 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
2773 char_u *newp;
2774 linenr_T lnum = curwin->w_cursor.lnum;
2775 colnr_T col = curwin->w_cursor.col;
2776
2777 if (col == 0)
2778 newp = vim_strsave((char_u *)"");
2779 else
2780 newp = vim_strnsave(ml_get(lnum), col);
2781
2782 if (newp == NULL)
2783 return FAIL;
2784
2785 ml_replace(lnum, newp, FALSE);
2786
2787 /* mark the buffer as changed and prepare for displaying */
2788 changed_bytes(lnum, curwin->w_cursor.col);
2789
2790 /*
2791 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2792 */
2793 if (fixpos && curwin->w_cursor.col > 0)
2794 --curwin->w_cursor.col;
2795
2796 return OK;
2797}
2798
2799/*
2800 * Delete "nlines" lines at the cursor.
2801 * Saves the lines for undo first if "undo" is TRUE.
2802 */
2803 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002804del_lines(
2805 long nlines, /* number of lines to delete */
2806 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002809 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811 if (nlines <= 0)
2812 return;
2813
2814 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002815 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 return;
2817
2818 for (n = 0; n < nlines; )
2819 {
2820 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2821 break;
2822
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002823 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 ++n;
2825
2826 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002827 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 break;
2829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002831 /* Correct the cursor position before calling deleted_lines_mark(), it may
2832 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 curwin->w_cursor.col = 0;
2834 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002835
2836 /* adjust marks, mark the buffer as changed and prepare for displaying */
2837 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838}
2839
2840 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002841gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002843 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002845 /* When searching columns is sometimes put at the end of a line. */
2846 if (pos->col == MAXCOL)
2847 return NUL;
2848 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#ifdef FEAT_MBYTE
2850 if (has_mbyte)
2851 return (*mb_ptr2char)(ptr);
2852#endif
2853 return (int)*ptr;
2854}
2855
2856 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002857gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
2859#ifdef FEAT_MBYTE
2860 if (has_mbyte)
2861 return (*mb_ptr2char)(ml_get_cursor());
2862#endif
2863 return (int)*ml_get_cursor();
2864}
2865
2866/*
2867 * Write a character at the current cursor position.
2868 * It is directly written into the block.
2869 */
2870 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002871pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2874 + curwin->w_cursor.col) = c;
2875}
2876
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877/*
2878 * When extra == 0: Return TRUE if the cursor is before or on the first
2879 * non-blank in the line.
2880 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2881 * the line.
2882 */
2883 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002884inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 char_u *ptr;
2887 colnr_T col;
2888
Bram Moolenaar1c465442017-03-12 20:10:05 +01002889 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 ++ptr;
2891 if (col >= curwin->w_cursor.col + extra)
2892 return TRUE;
2893 else
2894 return FALSE;
2895}
2896
2897/*
2898 * Skip to next part of an option argument: Skip space and comma.
2899 */
2900 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002901skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 if (*p == ',')
2904 ++p;
2905 while (*p == ' ')
2906 ++p;
2907 return p;
2908}
2909
2910/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002911 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 *
2913 * Most often called through changed_bytes() and changed_lines(), which also
2914 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002915 *
2916 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 */
2918 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002919changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
2921#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002922 if (p_imst == IM_ON_THE_SPOT)
2923 {
2924 /* The text of the preediting area is inserted, but this doesn't
2925 * mean a change of the buffer yet. That is delayed until the
2926 * text is committed. (this means preedit becomes empty) */
2927 if (im_is_preediting() && !xim_changed_while_preediting)
2928 return;
2929 xim_changed_while_preediting = FALSE;
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931#endif
2932
2933 if (!curbuf->b_changed)
2934 {
2935 int save_msg_scroll = msg_scroll;
2936
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002937 /* Give a warning about changing a read-only file. This may also
2938 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 /* Create a swap file if that is wanted.
2942 * Don't do this for "nofile" and "nowrite" buffer types. */
2943 if (curbuf->b_may_swap
2944#ifdef FEAT_QUICKFIX
2945 && !bt_dontwrite(curbuf)
2946#endif
2947 )
2948 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002949 int save_need_wait_return = need_wait_return;
2950
2951 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 ml_open_file(curbuf);
2953
2954 /* The ml_open_file() can cause an ATTENTION message.
2955 * Wait two seconds, to make sure the user reads this unexpected
2956 * message. Since we could be anywhere, call wait_return() now,
2957 * and don't let the emsg() set msg_scroll. */
2958 if (need_wait_return && emsg_silent == 0)
2959 {
2960 out_flush();
2961 ui_delay(2000L, TRUE);
2962 wait_return(TRUE);
2963 msg_scroll = save_msg_scroll;
2964 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002965 else
2966 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002968 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002970 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002973/*
2974 * Internal part of changed(), no user interaction.
2975 */
2976 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002977changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002978{
2979 curbuf->b_changed = TRUE;
2980 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002981 check_status(curbuf);
2982 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002983#ifdef FEAT_TITLE
2984 need_maketitle = TRUE; /* set window title later */
2985#endif
2986}
2987
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002988static void changedOneline(buf_T *buf, linenr_T lnum);
2989static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2990static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
2992/*
2993 * Changed bytes within a single line for the current buffer.
2994 * - marks the windows on this buffer to be redisplayed
2995 * - marks the buffer changed by calling changed()
2996 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002997 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 */
2999 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003000changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003002 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00003004
3005#ifdef FEAT_DIFF
3006 /* Diff highlighting in other diff windows may need to be updated too. */
3007 if (curwin->w_p_diff)
3008 {
3009 win_T *wp;
3010 linenr_T wlnum;
3011
Bram Moolenaar29323592016-07-24 22:04:11 +02003012 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003013 if (wp->w_p_diff && wp != curwin)
3014 {
3015 redraw_win_later(wp, VALID);
3016 wlnum = diff_lnum_win(lnum, wp);
3017 if (wlnum > 0)
3018 changedOneline(wp->w_buffer, wlnum);
3019 }
3020 }
3021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022}
3023
3024 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003025changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003027 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
3029 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003030 if (lnum < buf->b_mod_top)
3031 buf->b_mod_top = lnum;
3032 else if (lnum >= buf->b_mod_bot)
3033 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
3035 else
3036 {
3037 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003038 buf->b_mod_set = TRUE;
3039 buf->b_mod_top = lnum;
3040 buf->b_mod_bot = lnum + 1;
3041 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043}
3044
3045/*
3046 * Appended "count" lines below line "lnum" in the current buffer.
3047 * Must be called AFTER the change and after mark_adjust().
3048 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3049 */
3050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003051appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 changed_lines(lnum + 1, 0, lnum + 1, count);
3054}
3055
3056/*
3057 * Like appended_lines(), but adjust marks first.
3058 */
3059 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003060appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003062 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003063 * be marks there. But it's still needed in diff mode. */
3064 if (lnum + count < curbuf->b_ml.ml_line_count
3065#ifdef FEAT_DIFF
3066 || curwin->w_p_diff
3067#endif
3068 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003069 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 changed_lines(lnum + 1, 0, lnum + 1, count);
3071}
3072
3073/*
3074 * Deleted "count" lines at line "lnum" in the current buffer.
3075 * Must be called AFTER the change and after mark_adjust().
3076 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3077 */
3078 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003079deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 changed_lines(lnum, 0, lnum + count, -count);
3082}
3083
3084/*
3085 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003086 * Make sure the cursor is on a valid line before calling, a GUI callback may
3087 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 */
3089 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003090deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3093 changed_lines(lnum, 0, lnum + count, -count);
3094}
3095
3096/*
3097 * Changed lines for the current buffer.
3098 * Must be called AFTER the change and after mark_adjust().
3099 * - mark the buffer changed by calling changed()
3100 * - mark the windows on this buffer to be redisplayed
3101 * - invalidate cached values
3102 * "lnum" is the first line that needs displaying, "lnume" the first line
3103 * below the changed lines (BEFORE the change).
3104 * When only inserting lines, "lnum" and "lnume" are equal.
3105 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003106 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
3108 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003109changed_lines(
3110 linenr_T lnum, /* first line with change */
3111 colnr_T col, /* column in first line with change */
3112 linenr_T lnume, /* line below last changed line */
3113 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003115 changed_lines_buf(curbuf, lnum, lnume, xtra);
3116
3117#ifdef FEAT_DIFF
3118 if (xtra == 0 && curwin->w_p_diff)
3119 {
3120 /* When the number of lines doesn't change then mark_adjust() isn't
3121 * called and other diff buffers still need to be marked for
3122 * displaying. */
3123 win_T *wp;
3124 linenr_T wlnum;
3125
Bram Moolenaar29323592016-07-24 22:04:11 +02003126 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003127 if (wp->w_p_diff && wp != curwin)
3128 {
3129 redraw_win_later(wp, VALID);
3130 wlnum = diff_lnum_win(lnum, wp);
3131 if (wlnum > 0)
3132 changed_lines_buf(wp->w_buffer, wlnum,
3133 lnume - lnum + wlnum, 0L);
3134 }
3135 }
3136#endif
3137
3138 changed_common(lnum, col, lnume, xtra);
3139}
3140
3141 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003142changed_lines_buf(
3143 buf_T *buf,
3144 linenr_T lnum, /* first line with change */
3145 linenr_T lnume, /* line below last changed line */
3146 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003147{
3148 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
3150 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003151 if (lnum < buf->b_mod_top)
3152 buf->b_mod_top = lnum;
3153 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
3155 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003156 buf->b_mod_bot += xtra;
3157 if (buf->b_mod_bot < lnum)
3158 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003160 if (lnume + xtra > buf->b_mod_bot)
3161 buf->b_mod_bot = lnume + xtra;
3162 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164 else
3165 {
3166 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003167 buf->b_mod_set = TRUE;
3168 buf->b_mod_top = lnum;
3169 buf->b_mod_bot = lnume + xtra;
3170 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172}
3173
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003174/*
3175 * Common code for when a change is was made.
3176 * See changed_lines() for the arguments.
3177 * Careful: may trigger autocommands that reload the buffer.
3178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003180changed_common(
3181 linenr_T lnum,
3182 colnr_T col,
3183 linenr_T lnume,
3184 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
3186 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003187 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 int i;
3189#ifdef FEAT_JUMPLIST
3190 int cols;
3191 pos_T *p;
3192 int add;
3193#endif
3194
3195 /* mark the buffer as modified */
3196 changed();
3197
3198 /* set the '. mark */
3199 if (!cmdmod.keepjumps)
3200 {
3201 curbuf->b_last_change.lnum = lnum;
3202 curbuf->b_last_change.col = col;
3203
3204#ifdef FEAT_JUMPLIST
3205 /* Create a new entry if a new undo-able change was started or we
3206 * don't have an entry yet. */
3207 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3208 {
3209 if (curbuf->b_changelistlen == 0)
3210 add = TRUE;
3211 else
3212 {
3213 /* Don't create a new entry when the line number is the same
3214 * as the last one and the column is not too far away. Avoids
3215 * creating many entries for typing "xxxxx". */
3216 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3217 if (p->lnum != lnum)
3218 add = TRUE;
3219 else
3220 {
3221 cols = comp_textwidth(FALSE);
3222 if (cols == 0)
3223 cols = 79;
3224 add = (p->col + cols < col || col + cols < p->col);
3225 }
3226 }
3227 if (add)
3228 {
3229 /* This is the first of a new sequence of undo-able changes
3230 * and it's at some distance of the last change. Use a new
3231 * position in the changelist. */
3232 curbuf->b_new_change = FALSE;
3233
3234 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3235 {
3236 /* changelist is full: remove oldest entry */
3237 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3238 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3239 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003240 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
3242 /* Correct position in changelist for other windows on
3243 * this buffer. */
3244 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3245 --wp->w_changelistidx;
3246 }
3247 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003248 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
3250 /* For other windows, if the position in the changelist is
3251 * at the end it stays at the end. */
3252 if (wp->w_buffer == curbuf
3253 && wp->w_changelistidx == curbuf->b_changelistlen)
3254 ++wp->w_changelistidx;
3255 }
3256 ++curbuf->b_changelistlen;
3257 }
3258 }
3259 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3260 curbuf->b_last_change;
3261 /* The current window is always after the last change, so that "g,"
3262 * takes you back to it. */
3263 curwin->w_changelistidx = curbuf->b_changelistlen;
3264#endif
3265 }
3266
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003267 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 if (wp->w_buffer == curbuf)
3270 {
3271 /* Mark this window to be redrawn later. */
3272 if (wp->w_redr_type < VALID)
3273 wp->w_redr_type = VALID;
3274
3275 /* Check if a change in the buffer has invalidated the cached
3276 * values for the cursor. */
3277#ifdef FEAT_FOLDING
3278 /*
3279 * Update the folds for this window. Can't postpone this, because
3280 * a following operator might work on the whole fold: ">>dd".
3281 */
3282 foldUpdate(wp, lnum, lnume + xtra - 1);
3283
3284 /* The change may cause lines above or below the change to become
3285 * included in a fold. Set lnum/lnume to the first/last line that
3286 * might be displayed differently.
3287 * Set w_cline_folded here as an efficient way to update it when
3288 * inserting lines just above a closed fold. */
3289 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3290 if (wp->w_cursor.lnum == lnum)
3291 wp->w_cline_folded = i;
3292 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3293 if (wp->w_cursor.lnum == lnume)
3294 wp->w_cline_folded = i;
3295
3296 /* If the changed line is in a range of previously folded lines,
3297 * compare with the first line in that range. */
3298 if (wp->w_cursor.lnum <= lnum)
3299 {
3300 i = find_wl_entry(wp, lnum);
3301 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3302 changed_line_abv_curs_win(wp);
3303 }
3304#endif
3305
3306 if (wp->w_cursor.lnum > lnum)
3307 changed_line_abv_curs_win(wp);
3308 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3309 changed_cline_bef_curs_win(wp);
3310 if (wp->w_botline >= lnum)
3311 {
3312 /* Assume that botline doesn't change (inserted lines make
3313 * other lines scroll down below botline). */
3314 approximate_botline_win(wp);
3315 }
3316
3317 /* Check if any w_lines[] entries have become invalid.
3318 * For entries below the change: Correct the lnums for
3319 * inserted/deleted lines. Makes it possible to stop displaying
3320 * after the change. */
3321 for (i = 0; i < wp->w_lines_valid; ++i)
3322 if (wp->w_lines[i].wl_valid)
3323 {
3324 if (wp->w_lines[i].wl_lnum >= lnum)
3325 {
3326 if (wp->w_lines[i].wl_lnum < lnume)
3327 {
3328 /* line included in change */
3329 wp->w_lines[i].wl_valid = FALSE;
3330 }
3331 else if (xtra != 0)
3332 {
3333 /* line below change */
3334 wp->w_lines[i].wl_lnum += xtra;
3335#ifdef FEAT_FOLDING
3336 wp->w_lines[i].wl_lastlnum += xtra;
3337#endif
3338 }
3339 }
3340#ifdef FEAT_FOLDING
3341 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3342 {
3343 /* change somewhere inside this range of folded lines,
3344 * may need to be redrawn */
3345 wp->w_lines[i].wl_valid = FALSE;
3346 }
3347#endif
3348 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003349
3350#ifdef FEAT_FOLDING
3351 /* Take care of side effects for setting w_topline when folds have
3352 * changed. Esp. when the buffer was changed in another window. */
3353 if (hasAnyFolding(wp))
3354 set_topline(wp, wp->w_topline);
3355#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003356 /* relative numbering may require updating more */
3357 if (wp->w_p_rnu)
3358 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360 }
3361
3362 /* Call update_screen() later, which checks out what needs to be redrawn,
3363 * since it notices b_mod_set and then uses b_mod_*. */
3364 if (must_redraw < VALID)
3365 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003366
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003367 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003368 if (lnum <= curwin->w_cursor.lnum
3369 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003370 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373/*
3374 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3375 */
3376 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003377unchanged(
3378 buf_T *buf,
3379 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003381 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003384 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (ff)
3386 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003388 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#ifdef FEAT_TITLE
3390 need_maketitle = TRUE; /* set window title later */
3391#endif
3392 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003393 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394#ifdef FEAT_NETBEANS_INTG
3395 netbeans_unmodified(buf);
3396#endif
3397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
3400 * check_status: called when the status bars for the buffer 'buf'
3401 * need to be updated
3402 */
3403 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003404check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
3406 win_T *wp;
3407
Bram Moolenaar29323592016-07-24 22:04:11 +02003408 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (wp->w_buffer == buf && wp->w_status_height)
3410 {
3411 wp->w_redr_status = TRUE;
3412 if (must_redraw < VALID)
3413 must_redraw = VALID;
3414 }
3415}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417/*
3418 * If the file is readonly, give a warning message with the first change.
3419 * Don't do this for autocommands.
3420 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003421 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003423 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 */
3425 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003426change_warning(
3427 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 mode and 'showmode' is on */
3429{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003430 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (curbuf->b_did_warn == FALSE
3433 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 && curbuf->b_p_ro)
3436 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003437 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003439 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (!curbuf->b_p_ro)
3441 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 /*
3443 * Do what msg() does, but with a column offset if the warning should
3444 * be after the mode message.
3445 */
3446 msg_start();
3447 if (msg_row == Rows - 1)
3448 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003449 msg_source(HL_ATTR(HLF_W));
3450 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003451#ifdef FEAT_EVAL
3452 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 msg_clr_eos();
3455 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003456 if (msg_silent == 0 && !silent_mode
3457#ifdef FEAT_EVAL
3458 && time_for_testing != 1
3459#endif
3460 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 out_flush();
3463 ui_delay(1000L, TRUE); /* give the user time to think about it */
3464 }
3465 curbuf->b_did_warn = TRUE;
3466 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3467 if (msg_row < Rows - 1)
3468 showmode();
3469 }
3470}
3471
3472/*
3473 * Ask for a reply from the user, a 'y' or a 'n'.
3474 * No other characters are accepted, the message is repeated until a valid
3475 * reply is entered or CTRL-C is hit.
3476 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3477 * from any buffers but directly from the user.
3478 *
3479 * return the 'y' or 'n'
3480 */
3481 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003482ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 int r = ' ';
3485 int save_State = State;
3486
3487 if (exiting) /* put terminal in raw mode for this question */
3488 settmode(TMODE_RAW);
3489 ++no_wait_return;
3490#ifdef USE_ON_FLY_SCROLL
3491 dont_scroll = TRUE; /* disallow scrolling here */
3492#endif
3493 State = CONFIRM; /* mouse behaves like with :confirm */
3494#ifdef FEAT_MOUSE
3495 setmouse(); /* disables mouse for xterm */
3496#endif
3497 ++no_mapping;
3498 ++allow_keys; /* no mapping here, but recognize keys */
3499
3500 while (r != 'y' && r != 'n')
3501 {
3502 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003503 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (direct)
3505 r = get_keystroke();
3506 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003507 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (r == Ctrl_C || r == ESC)
3509 r = 'n';
3510 msg_putchar(r); /* show what you typed */
3511 out_flush();
3512 }
3513 --no_wait_return;
3514 State = save_State;
3515#ifdef FEAT_MOUSE
3516 setmouse();
3517#endif
3518 --no_mapping;
3519 --allow_keys;
3520
3521 return r;
3522}
3523
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003524#if defined(FEAT_MOUSE) || defined(PROTO)
3525/*
3526 * Return TRUE if "c" is a mouse key.
3527 */
3528 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003529is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003530{
3531 return c == K_LEFTMOUSE
3532 || c == K_LEFTMOUSE_NM
3533 || c == K_LEFTDRAG
3534 || c == K_LEFTRELEASE
3535 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003536 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003537 || c == K_MIDDLEMOUSE
3538 || c == K_MIDDLEDRAG
3539 || c == K_MIDDLERELEASE
3540 || c == K_RIGHTMOUSE
3541 || c == K_RIGHTDRAG
3542 || c == K_RIGHTRELEASE
3543 || c == K_MOUSEDOWN
3544 || c == K_MOUSEUP
3545 || c == K_MOUSELEFT
3546 || c == K_MOUSERIGHT
3547 || c == K_X1MOUSE
3548 || c == K_X1DRAG
3549 || c == K_X1RELEASE
3550 || c == K_X2MOUSE
3551 || c == K_X2DRAG
3552 || c == K_X2RELEASE;
3553}
3554#endif
3555
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556/*
3557 * Get a key stroke directly from the user.
3558 * Ignores mouse clicks and scrollbar events, except a click for the left
3559 * button (used at the more prompt).
3560 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3561 * Disadvantage: typeahead is ignored.
3562 * Translates the interrupt character for unix to ESC.
3563 */
3564 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003565get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003567 char_u *buf = NULL;
3568 int buflen = 150;
3569 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 int len = 0;
3571 int n;
3572 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003573 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 mapped_ctrl_c = FALSE; /* mappings are not used here */
3576 for (;;)
3577 {
3578 cursor_on();
3579 out_flush();
3580
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003581 /* Leave some room for check_termcode() to insert a key code into (max
3582 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3583 * bytes. */
3584 maxlen = (buflen - 6 - len) / 3;
3585 if (buf == NULL)
3586 buf = alloc(buflen);
3587 else if (maxlen < 10)
3588 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003589 char_u *t_buf = buf;
3590
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003591 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003592 * escape sequence. */
3593 buflen += 100;
3594 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003595 if (buf == NULL)
3596 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003597 maxlen = (buflen - 6 - len) / 3;
3598 }
3599 if (buf == NULL)
3600 {
3601 do_outofmem_msg((long_u)buflen);
3602 return ESC; /* panic! */
3603 }
3604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003606 * terminal code to complete. */
3607 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 if (n > 0)
3609 {
3610 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003611 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003613 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003615 else if (len > 0)
3616 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
Bram Moolenaar4395a712006-09-05 18:57:57 +00003618 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003619 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003620 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003622
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003623 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003624 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003625 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003626 {
3627 /* Redrawing was postponed, do it now. */
3628 update_screen(0);
3629 setcursor(); /* put cursor back where it belongs */
3630 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003631 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003632 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003633 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003635 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 continue;
3637
3638 /* Handle modifier and/or special key code. */
3639 n = buf[0];
3640 if (n == K_SPECIAL)
3641 {
3642 n = TO_SPECIAL(buf[1], buf[2]);
3643 if (buf[1] == KS_MODIFIER
3644 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003645#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003646 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003647#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003648#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 || n == K_VER_SCROLLBAR
3650 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651#endif
3652 )
3653 {
3654 if (buf[1] == KS_MODIFIER)
3655 mod_mask = buf[2];
3656 len -= 3;
3657 if (len > 0)
3658 mch_memmove(buf, buf + 3, (size_t)len);
3659 continue;
3660 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003661 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663#ifdef FEAT_MBYTE
3664 if (has_mbyte)
3665 {
3666 if (MB_BYTE2LEN(n) > len)
3667 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003668 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 n = (*mb_ptr2char)(buf);
3670 }
3671#endif
3672#ifdef UNIX
3673 if (n == intr_char)
3674 n = ESC;
3675#endif
3676 break;
3677 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003678 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 mapped_ctrl_c = save_mapped_ctrl_c;
3681 return n;
3682}
3683
3684/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003685 * Get a number from the user.
3686 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 */
3688 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003689get_number(
3690 int colon, /* allow colon to abort */
3691 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692{
3693 int n = 0;
3694 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003695 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003697 if (mouse_used != NULL)
3698 *mouse_used = FALSE;
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 /* When not printing messages, the user won't know what to type, return a
3701 * zero (as if CR was hit). */
3702 if (msg_silent != 0)
3703 return 0;
3704
3705#ifdef USE_ON_FLY_SCROLL
3706 dont_scroll = TRUE; /* disallow scrolling here */
3707#endif
3708 ++no_mapping;
3709 ++allow_keys; /* no mapping here, but recognize keys */
3710 for (;;)
3711 {
3712 windgoto(msg_row, msg_col);
3713 c = safe_vgetc();
3714 if (VIM_ISDIGIT(c))
3715 {
3716 n = n * 10 + c - '0';
3717 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003718 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3721 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003722 if (typed > 0)
3723 {
3724 MSG_PUTS("\b \b");
3725 --typed;
3726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003729#ifdef FEAT_MOUSE
3730 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3731 {
3732 *mouse_used = TRUE;
3733 n = mouse_row + 1;
3734 break;
3735 }
3736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 else if (n == 0 && c == ':' && colon)
3738 {
3739 stuffcharReadbuff(':');
3740 if (!exmode_active)
3741 cmdline_row = msg_row;
3742 skip_redraw = TRUE; /* skip redraw once */
3743 do_redraw = FALSE;
3744 break;
3745 }
3746 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3747 break;
3748 }
3749 --no_mapping;
3750 --allow_keys;
3751 return n;
3752}
3753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754/*
3755 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003756 * When "mouse_used" is not NULL allow using the mouse and in that case return
3757 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 */
3759 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003760prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761{
3762 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003763 int save_cmdline_row;
3764 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765
3766 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003767 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003768 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003769 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003770 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003771
Bram Moolenaar203335e2006-09-03 14:35:42 +00003772 /* Set the state such that text can be selected/copied/pasted and we still
3773 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003774 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003775 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003776 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003777 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaar73658312018-04-24 17:41:57 +02003778#ifdef FEAT_MOUSE
3779 /* May show different mouse shape. */
3780 setmouse();
3781#endif
3782
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003783
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003784 i = get_number(TRUE, mouse_used);
3785 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003787 /* don't call wait_return() now */
3788 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 cmdline_row = msg_row - 1;
3790 need_wait_return = FALSE;
3791 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003792 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003793 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003794 else
3795 cmdline_row = save_cmdline_row;
3796 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003797#ifdef FEAT_MOUSE
3798 /* May need to restore mouse shape. */
3799 setmouse();
3800#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003802 return i;
3803}
3804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003806msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 long pn;
3809
3810 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3812 return;
3813
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003814 /* We don't want to overwrite another important message, but do overwrite
3815 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3816 * then "put" reports the last action. */
3817 if (keep_msg != NULL && !keep_msg_more)
3818 return;
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 if (n > 0)
3821 pn = n;
3822 else
3823 pn = -n;
3824
3825 if (pn > p_report)
3826 {
3827 if (pn == 1)
3828 {
3829 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003830 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3831 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003833 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3834 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 else
3837 {
3838 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003839 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3840 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003842 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3843 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003846 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (msg(msg_buf))
3848 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003849 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003850 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 }
3853}
3854
3855/*
3856 * flush map and typeahead buffers and give a warning for an error
3857 */
3858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003859beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
3861 if (emsg_silent == 0)
3862 {
3863 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003864 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866}
3867
3868/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003869 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 */
3871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003872vim_beep(
3873 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003875#ifdef FEAT_EVAL
3876 called_vim_beep = TRUE;
3877#endif
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (emsg_silent == 0)
3880 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003881 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3882 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003883#ifdef ELAPSED_FUNC
3884 static int did_init = FALSE;
3885 static ELAPSED_TYPE start_tv;
3886
3887 /* Only beep once per half a second, otherwise a sequence of beeps
3888 * would freeze Vim. */
3889 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3890 {
3891 did_init = TRUE;
3892 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003894 if (p_vb
3895#ifdef FEAT_GUI
3896 /* While the GUI is starting up the termcap is set for
3897 * the GUI but the output still goes to a terminal. */
3898 && !(gui.in_use && gui.starting)
3899#endif
3900 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003901 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003902 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003903#ifdef FEAT_VTP
3904 /* No restore color information, refresh the screen. */
3905 if (has_vtp_working() != 0
3906# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003907 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003908# endif
3909 )
3910 {
3911 redraw_later(CLEAR);
3912 update_screen(0);
3913 redrawcmd();
3914 }
3915#endif
3916 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003917 else
3918 out_char(BELL);
3919#ifdef ELAPSED_FUNC
3920 }
3921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003923
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003924 /* When 'debug' contains "beep" produce a message. If we are sourcing
3925 * a script or executing a function give the user a hint where the beep
3926 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003927 if (vim_strchr(p_debug, 'e') != NULL)
3928 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003929 msg_source(HL_ATTR(HLF_W));
3930 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
3933}
3934
3935/*
3936 * To get the "real" home directory:
3937 * - get value of $HOME
3938 * For Unix:
3939 * - go to that directory
3940 * - do mch_dirname() to get the real name of that directory.
3941 * This also works with mounts and links.
3942 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3943 */
3944static char_u *homedir = NULL;
3945
3946 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003947init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 char_u *var;
3950
Bram Moolenaar05159a02005-02-26 23:04:13 +00003951 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003952 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954#ifdef VMS
3955 var = mch_getenv((char_u *)"SYS$LOGIN");
3956#else
3957 var = mch_getenv((char_u *)"HOME");
3958#endif
3959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960#ifdef WIN3264
3961 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003962 * Typically, $HOME is not defined on Windows, unless the user has
3963 * specifically defined it for Vim's sake. However, on Windows NT
3964 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3965 * each user. Try constructing $HOME from these.
3966 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003967 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003968 {
3969 char_u *homedrive, *homepath;
3970
3971 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3972 homepath = mch_getenv((char_u *)"HOMEPATH");
3973 if (homepath == NULL || *homepath == NUL)
3974 homepath = (char_u *)"\\";
3975 if (homedrive != NULL
3976 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3977 {
3978 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3979 if (NameBuff[0] != NUL)
3980 var = NameBuff;
3981 }
3982 }
3983
3984 if (var == NULL)
3985 var = mch_getenv((char_u *)"USERPROFILE");
3986
3987 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * Weird but true: $HOME may contain an indirect reference to another
3989 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3990 * when $HOME is being set.
3991 */
3992 if (var != NULL && *var == '%')
3993 {
3994 char_u *p;
3995 char_u *exp;
3996
3997 p = vim_strchr(var + 1, '%');
3998 if (p != NULL)
3999 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004000 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 exp = mch_getenv(NameBuff);
4002 if (exp != NULL && *exp != NUL
4003 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
4004 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004005 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
4008 }
4009 }
4010
Bram Moolenaar48340b62017-08-29 22:08:53 +02004011 if (var != NULL && *var == NUL) /* empty is same as not set */
4012 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaar48340b62017-08-29 22:08:53 +02004014# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00004015 if (enc_utf8 && var != NULL)
4016 {
4017 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004018 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004019
4020 /* Convert from active codepage to UTF-8. Other conversions are
4021 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004022 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004023 if (pp != NULL)
4024 {
4025 homedir = pp;
4026 return;
4027 }
4028 }
4029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 /*
4032 * Default home dir is C:/
4033 * Best assumption we can make in such a situation.
4034 */
4035 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004036 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (var != NULL)
4040 {
4041#ifdef UNIX
4042 /*
4043 * Change to the directory and get the actual path. This resolves
4044 * links. Don't do it when we can't return.
4045 */
4046 if (mch_dirname(NameBuff, MAXPATHL) == OK
4047 && mch_chdir((char *)NameBuff) == 0)
4048 {
4049 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4050 var = IObuff;
4051 if (mch_chdir((char *)NameBuff) != 0)
4052 EMSG(_(e_prev_dir));
4053 }
4054#endif
4055 homedir = vim_strsave(var);
4056 }
4057}
4058
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004059#if defined(EXITFREE) || defined(PROTO)
4060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004061free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004062{
4063 vim_free(homedir);
4064}
Bram Moolenaar24305862012-08-15 14:05:05 +02004065
4066# ifdef FEAT_CMDL_COMPL
4067 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004068free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004069{
4070 ga_clear_strings(&ga_users);
4071}
4072# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004073#endif
4074
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004076 * Call expand_env() and store the result in an allocated string.
4077 * This is not very memory efficient, this expects the result to be freed
4078 * again soon.
4079 */
4080 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004081expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004082{
4083 return expand_env_save_opt(src, FALSE);
4084}
4085
4086/*
4087 * Idem, but when "one" is TRUE handle the string as one file name, only
4088 * expand "~" at the start.
4089 */
4090 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004091expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004092{
4093 char_u *p;
4094
4095 p = alloc(MAXPATHL);
4096 if (p != NULL)
4097 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4098 return p;
4099}
4100
4101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 * Expand environment variable with path name.
4103 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004104 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 * If anything fails no expansion is done and dst equals src.
4106 */
4107 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004108expand_env(
4109 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4110 char_u *dst, /* where to put the result */
4111 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004113 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114}
4115
4116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004117expand_env_esc(
4118 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4119 char_u *dst, /* where to put the result */
4120 int dstlen, /* maximum length of the result */
4121 int esc, /* escape spaces in expanded variables */
4122 int one, /* "srcp" is one file name */
4123 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004125 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 char_u *tail;
4127 int c;
4128 char_u *var;
4129 int copy_char;
4130 int mustfree; /* var was allocated, need to free it later */
4131 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004132 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004134 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004135 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004136
4137 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 --dstlen; /* leave one char space for "\," */
4139 while (*src && dstlen > 0)
4140 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004141#ifdef FEAT_EVAL
4142 /* Skip over `=expr`. */
4143 if (src[0] == '`' && src[1] == '=')
4144 {
4145 size_t len;
4146
4147 var = src;
4148 src += 2;
4149 (void)skip_expr(&src);
4150 if (*src == '`')
4151 ++src;
4152 len = src - var;
4153 if (len > (size_t)dstlen)
4154 len = dstlen;
4155 vim_strncpy(dst, var, len);
4156 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004157 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004158 continue;
4159 }
4160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004162 if ((*src == '$'
4163#ifdef VMS
4164 && at_start
4165#endif
4166 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004167#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 || *src == '%'
4169#endif
4170 || (*src == '~' && at_start))
4171 {
4172 mustfree = FALSE;
4173
4174 /*
4175 * The variable name is copied into dst temporarily, because it may
4176 * be a string in read-only memory and a NUL needs to be appended.
4177 */
4178 if (*src != '~') /* environment var */
4179 {
4180 tail = src + 1;
4181 var = dst;
4182 c = dstlen - 1;
4183
4184#ifdef UNIX
4185 /* Unix has ${var-name} type environment vars */
4186 if (*tail == '{' && !vim_isIDc('{'))
4187 {
4188 tail++; /* ignore '{' */
4189 while (c-- > 0 && *tail && *tail != '}')
4190 *var++ = *tail++;
4191 }
4192 else
4193#endif
4194 {
4195 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004196#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 || (*src == '%' && *tail != '%')
4198#endif
4199 ))
4200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 }
4204
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004205#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206# ifdef UNIX
4207 if (src[1] == '{' && *tail != '}')
4208# else
4209 if (*src == '%' && *tail != '%')
4210# endif
4211 var = NULL;
4212 else
4213 {
4214# ifdef UNIX
4215 if (src[1] == '{')
4216# else
4217 if (*src == '%')
4218#endif
4219 ++tail;
4220#endif
4221 *var = NUL;
4222 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004223#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225#endif
4226 }
4227 /* home directory */
4228 else if ( src[1] == NUL
4229 || vim_ispathsep(src[1])
4230 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4231 {
4232 var = homedir;
4233 tail = src + 1;
4234 }
4235 else /* user directory */
4236 {
4237#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4238 /*
4239 * Copy ~user to dst[], so we can put a NUL after it.
4240 */
4241 tail = src;
4242 var = dst;
4243 c = dstlen - 1;
4244 while ( c-- > 0
4245 && *tail
4246 && vim_isfilec(*tail)
4247 && !vim_ispathsep(*tail))
4248 *var++ = *tail++;
4249 *var = NUL;
4250# ifdef UNIX
4251 /*
4252 * If the system supports getpwnam(), use it.
4253 * Otherwise, or if getpwnam() fails, the shell is used to
4254 * expand ~user. This is slower and may fail if the shell
4255 * does not support ~user (old versions of /bin/sh).
4256 */
4257# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4258 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004259 /* Note: memory allocated by getpwnam() is never freed.
4260 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004261 struct passwd *pw = (*dst == NUL)
4262 ? NULL : getpwnam((char *)dst + 1);
4263
4264 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 if (var == NULL)
4267# endif
4268 {
4269 expand_T xpc;
4270
4271 ExpandInit(&xpc);
4272 xpc.xp_context = EXPAND_FILES;
4273 var = ExpandOne(&xpc, dst, NULL,
4274 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 mustfree = TRUE;
4276 }
4277
4278# else /* !UNIX, thus VMS */
4279 /*
4280 * USER_HOME is a comma-separated list of
4281 * directories to search for the user account in.
4282 */
4283 {
4284 char_u test[MAXPATHL], paths[MAXPATHL];
4285 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004286 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 STRCPY(paths, USER_HOME);
4289 next_path = paths;
4290 while (*next_path)
4291 {
4292 for (path = next_path; *next_path && *next_path != ',';
4293 next_path++);
4294 if (*next_path)
4295 *next_path++ = NUL;
4296 STRCPY(test, path);
4297 STRCAT(test, "/");
4298 STRCAT(test, dst + 1);
4299 if (mch_stat(test, &st) == 0)
4300 {
4301 var = alloc(STRLEN(test) + 1);
4302 STRCPY(var, test);
4303 mustfree = TRUE;
4304 break;
4305 }
4306 }
4307 }
4308# endif /* UNIX */
4309#else
4310 /* cannot expand user's home directory, so don't try */
4311 var = NULL;
4312 tail = (char_u *)""; /* for gcc */
4313#endif /* UNIX || VMS */
4314 }
4315
4316#ifdef BACKSLASH_IN_FILENAME
4317 /* If 'shellslash' is set change backslashes to forward slashes.
4318 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4319 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4320 {
4321 char_u *p = vim_strsave(var);
4322
4323 if (p != NULL)
4324 {
4325 if (mustfree)
4326 vim_free(var);
4327 var = p;
4328 mustfree = TRUE;
4329 forward_slash(var);
4330 }
4331 }
4332#endif
4333
4334 /* If "var" contains white space, escape it with a backslash.
4335 * Required for ":e ~/tt" when $HOME includes a space. */
4336 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4337 {
4338 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4339
4340 if (p != NULL)
4341 {
4342 if (mustfree)
4343 vim_free(var);
4344 var = p;
4345 mustfree = TRUE;
4346 }
4347 }
4348
4349 if (var != NULL && *var != NUL
4350 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4351 {
4352 STRCPY(dst, var);
4353 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004354 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 /* if var[] ends in a path separator and tail[] starts
4356 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004357 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4359 && dst[-1] != ':'
4360#endif
4361 && vim_ispathsep(*tail))
4362 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004363 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 src = tail;
4365 copy_char = FALSE;
4366 }
4367 if (mustfree)
4368 vim_free(var);
4369 }
4370
4371 if (copy_char) /* copy at least one char */
4372 {
4373 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004374 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004375 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4376 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
4378 at_start = FALSE;
4379 if (src[0] == '\\' && src[1] != NUL)
4380 {
4381 *dst++ = *src++;
4382 --dstlen;
4383 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004384 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004386 if (dstlen > 0)
4387 {
4388 *dst++ = *src++;
4389 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004390
Bram Moolenaar1c864092017-08-06 18:15:45 +02004391 if (startstr != NULL && src - startstr_len >= srcp
4392 && STRNCMP(src - startstr_len, startstr,
4393 startstr_len) == 0)
4394 at_start = TRUE;
4395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
4399 *dst = NUL;
4400}
4401
4402/*
4403 * Vim's version of getenv().
4404 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004405 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004406 * "mustfree" is set to TRUE when returned is allocated, it must be
4407 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 */
4409 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004410vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412 char_u *p;
4413 char_u *pend;
4414 int vimruntime;
4415
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004416#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 /* use "C:/" when $HOME is not set */
4418 if (STRCMP(name, "HOME") == 0)
4419 return homedir;
4420#endif
4421
4422 p = mch_getenv(name);
4423 if (p != NULL && *p == NUL) /* empty is the same as not set */
4424 p = NULL;
4425
4426 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004427 {
4428#if defined(FEAT_MBYTE) && defined(WIN3264)
4429 if (enc_utf8)
4430 {
4431 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004432 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004433
4434 /* Convert from active codepage to UTF-8. Other conversions are
4435 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004436 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004437 if (pp != NULL)
4438 {
4439 p = pp;
4440 *mustfree = TRUE;
4441 }
4442 }
4443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4448 if (!vimruntime && STRCMP(name, "VIM") != 0)
4449 return NULL;
4450
4451 /*
4452 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4453 * Don't do this when default_vimruntime_dir is non-empty.
4454 */
4455 if (vimruntime
4456#ifdef HAVE_PATHDEF
4457 && *default_vimruntime_dir == NUL
4458#endif
4459 )
4460 {
4461 p = mch_getenv((char_u *)"VIM");
4462 if (p != NULL && *p == NUL) /* empty is the same as not set */
4463 p = NULL;
4464 if (p != NULL)
4465 {
4466 p = vim_version_dir(p);
4467 if (p != NULL)
4468 *mustfree = TRUE;
4469 else
4470 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004471
4472#if defined(FEAT_MBYTE) && defined(WIN3264)
4473 if (enc_utf8)
4474 {
4475 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004476 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004477
4478 /* Convert from active codepage to UTF-8. Other conversions
4479 * are not done, because they would fail for non-ASCII
4480 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004481 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004482 if (pp != NULL)
4483 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004484 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004485 vim_free(p);
4486 p = pp;
4487 *mustfree = TRUE;
4488 }
4489 }
4490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 }
4493
4494 /*
4495 * When expanding $VIM or $VIMRUNTIME fails, try using:
4496 * - the directory name from 'helpfile' (unless it contains '$')
4497 * - the executable name from argv[0]
4498 */
4499 if (p == NULL)
4500 {
4501 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4502 p = p_hf;
4503#ifdef USE_EXE_NAME
4504 /*
4505 * Use the name of the executable, obtained from argv[0].
4506 */
4507 else
4508 p = exe_name;
4509#endif
4510 if (p != NULL)
4511 {
4512 /* remove the file name */
4513 pend = gettail(p);
4514
4515 /* remove "doc/" from 'helpfile', if present */
4516 if (p == p_hf)
4517 pend = remove_tail(p, pend, (char_u *)"doc");
4518
4519#ifdef USE_EXE_NAME
4520# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004521 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (p == exe_name)
4523 {
4524 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004525 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004527 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4528 if (pend1 != pend)
4529 {
4530 pnew = alloc((unsigned)(pend1 - p) + 15);
4531 if (pnew != NULL)
4532 {
4533 STRNCPY(pnew, p, (pend1 - p));
4534 STRCPY(pnew + (pend1 - p), "Resources/vim");
4535 p = pnew;
4536 pend = p + STRLEN(p);
4537 }
4538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540# endif
4541 /* remove "src/" from exe_name, if present */
4542 if (p == exe_name)
4543 pend = remove_tail(p, pend, (char_u *)"src");
4544#endif
4545
4546 /* for $VIM, remove "runtime/" or "vim54/", if present */
4547 if (!vimruntime)
4548 {
4549 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4550 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4551 }
4552
4553 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004554 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004557#ifdef MACOS_X
4558 if (p == exe_name || p == p_hf)
4559#endif
4560 /* check that the result is a directory name */
4561 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004564 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 else
4566 {
4567#ifdef USE_EXE_NAME
4568 /* may add "/vim54" or "/runtime" if it exists */
4569 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4570 {
4571 vim_free(p);
4572 p = pend;
4573 }
4574#endif
4575 *mustfree = TRUE;
4576 }
4577 }
4578 }
4579
4580#ifdef HAVE_PATHDEF
4581 /* When there is a pathdef.c file we can use default_vim_dir and
4582 * default_vimruntime_dir */
4583 if (p == NULL)
4584 {
4585 /* Only use default_vimruntime_dir when it is not empty */
4586 if (vimruntime && *default_vimruntime_dir != NUL)
4587 {
4588 p = default_vimruntime_dir;
4589 *mustfree = FALSE;
4590 }
4591 else if (*default_vim_dir != NUL)
4592 {
4593 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4594 *mustfree = TRUE;
4595 else
4596 {
4597 p = default_vim_dir;
4598 *mustfree = FALSE;
4599 }
4600 }
4601 }
4602#endif
4603
4604 /*
4605 * Set the environment variable, so that the new value can be found fast
4606 * next time, and others can also use it (e.g. Perl).
4607 */
4608 if (p != NULL)
4609 {
4610 if (vimruntime)
4611 {
4612 vim_setenv((char_u *)"VIMRUNTIME", p);
4613 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 else
4616 {
4617 vim_setenv((char_u *)"VIM", p);
4618 didset_vim = TRUE;
4619 }
4620 }
4621 return p;
4622}
4623
4624/*
4625 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4626 * Return NULL if not, return its name in allocated memory otherwise.
4627 */
4628 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004629vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 char_u *p;
4632
4633 if (vimdir == NULL || *vimdir == NUL)
4634 return NULL;
4635 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4636 if (p != NULL && mch_isdir(p))
4637 return p;
4638 vim_free(p);
4639 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4640 if (p != NULL && mch_isdir(p))
4641 return p;
4642 vim_free(p);
4643 return NULL;
4644}
4645
4646/*
4647 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4648 * the length of "name/". Otherwise return "pend".
4649 */
4650 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004651remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
4653 int len = (int)STRLEN(name) + 1;
4654 char_u *newend = pend - len;
4655
4656 if (newend >= p
4657 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004658 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 return newend;
4660 return pend;
4661}
4662
Bram Moolenaar137374f2018-05-13 15:59:50 +02004663 void
4664vim_unsetenv(char_u *var)
4665{
4666#ifdef HAVE_UNSETENV
4667 unsetenv((char *)var);
4668#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004669 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004670#endif
4671}
4672
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 * Our portable version of setenv.
4676 */
4677 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004678vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680#ifdef HAVE_SETENV
4681 mch_setenv((char *)name, (char *)val, 1);
4682#else
4683 char_u *envbuf;
4684
4685 /*
4686 * Putenv does not copy the string, it has to remain
4687 * valid. The allocated memory will never be freed.
4688 */
4689 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4690 if (envbuf != NULL)
4691 {
4692 sprintf((char *)envbuf, "%s=%s", name, val);
4693 putenv((char *)envbuf);
4694 }
4695#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004696#ifdef FEAT_GETTEXT
4697 /*
4698 * When setting $VIMRUNTIME adjust the directory to find message
4699 * translations to $VIMRUNTIME/lang.
4700 */
4701 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4702 {
4703 char_u *buf = concat_str(val, (char_u *)"/lang");
4704
4705 if (buf != NULL)
4706 {
4707 bindtextdomain(VIMPACKAGE, (char *)buf);
4708 vim_free(buf);
4709 }
4710 }
4711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712}
4713
4714#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4715/*
4716 * Function given to ExpandGeneric() to obtain an environment variable name.
4717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004719get_env_name(
4720 expand_T *xp UNUSED,
4721 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
Bram Moolenaard0573012017-10-28 21:11:06 +02004723# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004725 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 */
4727 return NULL;
4728# else
4729# ifndef __WIN32__
4730 /* Borland C++ 5.2 has this in a header file. */
4731 extern char **environ;
4732# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004733# define ENVNAMELEN 100
4734 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 char_u *str;
4736 int n;
4737
4738 str = (char_u *)environ[idx];
4739 if (str == NULL)
4740 return NULL;
4741
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004742 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
4744 if (str[n] == '=' || str[n] == NUL)
4745 break;
4746 name[n] = str[n];
4747 }
4748 name[n] = NUL;
4749 return name;
4750# endif
4751}
Bram Moolenaar24305862012-08-15 14:05:05 +02004752
4753/*
4754 * Find all user names for user completion.
4755 * Done only once and then cached.
4756 */
4757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004758init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004759{
Bram Moolenaar24305862012-08-15 14:05:05 +02004760 static int lazy_init_done = FALSE;
4761
4762 if (lazy_init_done)
4763 return;
4764
4765 lazy_init_done = TRUE;
4766 ga_init2(&ga_users, sizeof(char_u *), 20);
4767
4768# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4769 {
4770 char_u* user;
4771 struct passwd* pw;
4772
4773 setpwent();
4774 while ((pw = getpwent()) != NULL)
4775 /* pw->pw_name shouldn't be NULL but just in case... */
4776 if (pw->pw_name != NULL)
4777 {
4778 if (ga_grow(&ga_users, 1) == FAIL)
4779 break;
4780 user = vim_strsave((char_u*)pw->pw_name);
4781 if (user == NULL)
4782 break;
4783 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4784 }
4785 endpwent();
4786 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004787# elif defined(WIN3264)
4788 {
4789 char_u* user;
4790 DWORD nusers = 0, ntotal = 0, i;
4791 PUSER_INFO_0 uinfo;
4792
4793 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4794 &nusers, &ntotal, NULL) == NERR_Success)
4795 {
4796 for (i = 0; i < nusers; i++)
4797 {
4798 if (ga_grow(&ga_users, 1) == FAIL)
4799 break;
4800 user = utf16_to_enc(uinfo[i].usri0_name, NULL);
4801 if (user == NULL)
4802 break;
4803 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4804 }
4805
4806 NetApiBufferFree(uinfo);
4807 }
4808 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004809# endif
4810}
4811
4812/*
4813 * Function given to ExpandGeneric() to obtain an user names.
4814 */
4815 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004816get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004817{
4818 init_users();
4819 if (idx < ga_users.ga_len)
4820 return ((char_u **)ga_users.ga_data)[idx];
4821 return NULL;
4822}
4823
4824/*
4825 * Check whether name matches a user name. Return:
4826 * 0 if name does not match any user name.
4827 * 1 if name partially matches the beginning of a user name.
4828 * 2 is name fully matches a user name.
4829 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004830int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004831{
4832 int i;
4833 int n = (int)STRLEN(name);
4834 int result = 0;
4835
4836 init_users();
4837 for (i = 0; i < ga_users.ga_len; i++)
4838 {
4839 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4840 return 2; /* full match */
4841 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4842 result = 1; /* partial match */
4843 }
4844 return result;
4845}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846#endif
4847
4848/*
4849 * Replace home directory by "~" in each space or comma separated file name in
4850 * 'src'.
4851 * If anything fails (except when out of space) dst equals src.
4852 */
4853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004854home_replace(
4855 buf_T *buf, /* when not NULL, check for help files */
4856 char_u *src, /* input file name */
4857 char_u *dst, /* where to put the result */
4858 int dstlen, /* maximum length of the result */
4859 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 spaces and commas in the file name. */
4861{
4862 size_t dirlen = 0, envlen = 0;
4863 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004864 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 char_u *p;
4866
4867 if (src == NULL)
4868 {
4869 *dst = NUL;
4870 return;
4871 }
4872
4873 /*
4874 * If the file is a help file, remove the path completely.
4875 */
4876 if (buf != NULL && buf->b_help)
4877 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004878 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return;
4880 }
4881
4882 /*
4883 * We check both the value of the $HOME environment variable and the
4884 * "real" home directory.
4885 */
4886 if (homedir != NULL)
4887 dirlen = STRLEN(homedir);
4888
4889#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004890 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004892 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4893#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004894#ifdef WIN3264
4895 if (homedir_env == NULL)
4896 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4897#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004898 /* Empty is the same as not set. */
4899 if (homedir_env != NULL && *homedir_env == NUL)
4900 homedir_env = NULL;
4901
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004902#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004903 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004904 {
4905 int usedlen = 0;
4906 int flen;
4907 char_u *fbuf = NULL;
4908
4909 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004910 (void)modify_fname((char_u *)":p", &usedlen,
4911 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004912 flen = (int)STRLEN(homedir_env);
4913 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4914 /* Remove the trailing / that is added to a directory. */
4915 homedir_env[flen - 1] = NUL;
4916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917#endif
4918
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (homedir_env != NULL)
4920 envlen = STRLEN(homedir_env);
4921
4922 if (!one)
4923 src = skipwhite(src);
4924 while (*src && dstlen > 0)
4925 {
4926 /*
4927 * Here we are at the beginning of a file name.
4928 * First, check to see if the beginning of the file name matches
4929 * $HOME or the "real" home directory. Check that there is a '/'
4930 * after the match (so that if e.g. the file is "/home/pieter/bla",
4931 * and the home directory is "/home/piet", the file does not end up
4932 * as "~er/bla" (which would seem to indicate the file "bla" in user
4933 * er's home directory)).
4934 */
4935 p = homedir;
4936 len = dirlen;
4937 for (;;)
4938 {
4939 if ( len
4940 && fnamencmp(src, p, len) == 0
4941 && (vim_ispathsep(src[len])
4942 || (!one && (src[len] == ',' || src[len] == ' '))
4943 || src[len] == NUL))
4944 {
4945 src += len;
4946 if (--dstlen > 0)
4947 *dst++ = '~';
4948
4949 /*
4950 * If it's just the home directory, add "/".
4951 */
4952 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4953 *dst++ = '/';
4954 break;
4955 }
4956 if (p == homedir_env)
4957 break;
4958 p = homedir_env;
4959 len = envlen;
4960 }
4961
4962 /* if (!one) skip to separator: space or comma */
4963 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4964 *dst++ = *src++;
4965 /* skip separator */
4966 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4967 *dst++ = *src++;
4968 }
4969 /* if (dstlen == 0) out of space, what to do??? */
4970
4971 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004972
4973 if (homedir_env != homedir_env_orig)
4974 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975}
4976
4977/*
4978 * Like home_replace, store the replaced string in allocated memory.
4979 * When something fails, NULL is returned.
4980 */
4981 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004982home_replace_save(
4983 buf_T *buf, /* when not NULL, check for help files */
4984 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985{
4986 char_u *dst;
4987 unsigned len;
4988
4989 len = 3; /* space for "~/" and trailing NUL */
4990 if (src != NULL) /* just in case */
4991 len += (unsigned)STRLEN(src);
4992 dst = alloc(len);
4993 if (dst != NULL)
4994 home_replace(buf, src, dst, len, TRUE);
4995 return dst;
4996}
4997
4998/*
4999 * Compare two file names and return:
5000 * FPC_SAME if they both exist and are the same file.
5001 * FPC_SAMEX if they both don't exist and have the same file name.
5002 * FPC_DIFF if they both exist and are different files.
5003 * FPC_NOTX if they both don't exist.
5004 * FPC_DIFFX if one of them doesn't exist.
5005 * For the first name environment variables are expanded
5006 */
5007 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005008fullpathcmp(
5009 char_u *s1,
5010 char_u *s2,
5011 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013#ifdef UNIX
5014 char_u exp1[MAXPATHL];
5015 char_u full1[MAXPATHL];
5016 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005017 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 int r1, r2;
5019
5020 expand_env(s1, exp1, MAXPATHL);
5021 r1 = mch_stat((char *)exp1, &st1);
5022 r2 = mch_stat((char *)s2, &st2);
5023 if (r1 != 0 && r2 != 0)
5024 {
5025 /* if mch_stat() doesn't work, may compare the names */
5026 if (checkname)
5027 {
5028 if (fnamecmp(exp1, s2) == 0)
5029 return FPC_SAMEX;
5030 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5031 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5032 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5033 return FPC_SAMEX;
5034 }
5035 return FPC_NOTX;
5036 }
5037 if (r1 != 0 || r2 != 0)
5038 return FPC_DIFFX;
5039 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5040 return FPC_SAME;
5041 return FPC_DIFF;
5042#else
5043 char_u *exp1; /* expanded s1 */
5044 char_u *full1; /* full path of s1 */
5045 char_u *full2; /* full path of s2 */
5046 int retval = FPC_DIFF;
5047 int r1, r2;
5048
5049 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5050 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5051 {
5052 full1 = exp1 + MAXPATHL;
5053 full2 = full1 + MAXPATHL;
5054
5055 expand_env(s1, exp1, MAXPATHL);
5056 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5057 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5058
5059 /* If vim_FullName() fails, the file probably doesn't exist. */
5060 if (r1 != OK && r2 != OK)
5061 {
5062 if (checkname && fnamecmp(exp1, s2) == 0)
5063 retval = FPC_SAMEX;
5064 else
5065 retval = FPC_NOTX;
5066 }
5067 else if (r1 != OK || r2 != OK)
5068 retval = FPC_DIFFX;
5069 else if (fnamecmp(full1, full2))
5070 retval = FPC_DIFF;
5071 else
5072 retval = FPC_SAME;
5073 vim_free(exp1);
5074 }
5075 return retval;
5076#endif
5077}
5078
5079/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005080 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005081 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005082 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
5084 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005085gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 char_u *p1, *p2;
5088
5089 if (fname == NULL)
5090 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005091 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005093 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005095 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 return p1;
5098}
5099
Bram Moolenaar31710262010-08-13 13:36:15 +02005100#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005101static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02005102
5103/*
5104 * Return the end of the directory name, on the first path
5105 * separator:
5106 * "/path/file", "/path/dir/", "/path//dir", "/file"
5107 * ^ ^ ^ ^
5108 */
5109 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005110gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005111{
5112 char_u *dir_end = fname;
5113 char_u *next_dir_end = fname;
5114 int look_for_sep = TRUE;
5115 char_u *p;
5116
5117 for (p = fname; *p != NUL; )
5118 {
5119 if (vim_ispathsep(*p))
5120 {
5121 if (look_for_sep)
5122 {
5123 next_dir_end = p;
5124 look_for_sep = FALSE;
5125 }
5126 }
5127 else
5128 {
5129 if (!look_for_sep)
5130 dir_end = next_dir_end;
5131 look_for_sep = TRUE;
5132 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005133 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005134 }
5135 return dir_end;
5136}
5137#endif
5138
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005140 * Get pointer to tail of "fname", including path separators. Putting a NUL
5141 * here leaves the directory name. Takes care of "c:/" and "//".
5142 * Always returns a valid pointer.
5143 */
5144 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005145gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005146{
5147 char_u *p;
5148 char_u *t;
5149
5150 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5151 t = gettail(fname);
5152 while (t > p && after_pathsep(fname, t))
5153 --t;
5154#ifdef VMS
5155 /* path separator is part of the path */
5156 ++t;
5157#endif
5158 return t;
5159}
5160
5161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 * get the next path component (just after the next path separator).
5163 */
5164 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005165getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005168 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (*fname)
5170 ++fname;
5171 return fname;
5172}
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174/*
5175 * Get a pointer to one character past the head of a path name.
5176 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5177 * If there is no head, path is returned.
5178 */
5179 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005180get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181{
5182 char_u *retval;
5183
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005184#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 /* may skip "c:" */
5186 if (isalpha(path[0]) && path[1] == ':')
5187 retval = path + 2;
5188 else
5189 retval = path;
5190#else
5191# if defined(AMIGA)
5192 /* may skip "label:" */
5193 retval = vim_strchr(path, ':');
5194 if (retval == NULL)
5195 retval = path;
5196# else /* Unix */
5197 retval = path;
5198# endif
5199#endif
5200
5201 while (vim_ispathsep(*retval))
5202 ++retval;
5203
5204 return retval;
5205}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005208 * Return TRUE if 'c' is a path separator.
5209 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 */
5211 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005212vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005214#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005216#else
5217# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005219# else
5220# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5222 return (c == ':' || c == '[' || c == ']' || c == '/'
5223 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005224# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005226# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229}
5230
Bram Moolenaar69c35002013-11-04 02:54:12 +01005231/*
5232 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5233 */
5234 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005235vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005236{
5237 return vim_ispathsep(c)
5238#ifdef BACKSLASH_IN_FILENAME
5239 && c != ':'
5240#endif
5241 ;
5242}
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5245/*
5246 * return TRUE if 'c' is a path list separator.
5247 */
5248 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005249vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
5251#ifdef UNIX
5252 return (c == ':');
5253#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005254 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#endif
5256}
5257#endif
5258
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005259/*
5260 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5261 * It's done in-place.
5262 */
5263 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005264shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005265{
5266 char_u *tail, *s, *d;
5267 int skip = FALSE;
5268
5269 tail = gettail(str);
5270 d = str;
5271 for (s = str; ; ++s)
5272 {
5273 if (s >= tail) /* copy the whole tail */
5274 {
5275 *d++ = *s;
5276 if (*s == NUL)
5277 break;
5278 }
5279 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5280 {
5281 *d++ = *s;
5282 skip = FALSE;
5283 }
5284 else if (!skip)
5285 {
5286 *d++ = *s; /* copy next char */
5287 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5288 skip = TRUE;
5289# ifdef FEAT_MBYTE
5290 if (has_mbyte)
5291 {
5292 int l = mb_ptr2len(s);
5293
5294 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005295 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005296 }
5297# endif
5298 }
5299 }
5300}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005301
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005302/*
5303 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5304 * Also returns TRUE if there is no directory name.
5305 * "fname" must be writable!.
5306 */
5307 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005308dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005309{
5310 char_u *p;
5311 int c;
5312 int retval;
5313
5314 p = gettail_sep(fname);
5315 if (p == fname)
5316 return TRUE;
5317 c = *p;
5318 *p = NUL;
5319 retval = mch_isdir(fname);
5320 *p = c;
5321 return retval;
5322}
5323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005325 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5326 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 */
5328 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005329vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005331#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005333#else
5334 if (p_fic)
5335 return MB_STRICMP(x, y);
5336 return STRCMP(x, y);
5337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338}
5339
5340 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005341vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005343#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005344 char_u *px = x;
5345 char_u *py = y;
5346 int cx = NUL;
5347 int cy = NUL;
5348
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005349 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005351 cx = PTR2CHAR(px);
5352 cy = PTR2CHAR(py);
5353 if (cx == NUL || cy == NUL
5354 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5355 && !(cx == '/' && cy == '\\')
5356 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005358 len -= MB_PTR2LEN(px);
5359 px += MB_PTR2LEN(px);
5360 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362 if (len == 0)
5363 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005364 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005365#else
5366 if (p_fic)
5367 return MB_STRNICMP(x, y, len);
5368 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372/*
5373 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005374 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 */
5376 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005377concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 char_u *dest;
5380
5381 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5382 if (dest != NULL)
5383 {
5384 STRCPY(dest, fname1);
5385 if (sep)
5386 add_pathsep(dest);
5387 STRCAT(dest, fname2);
5388 }
5389 return dest;
5390}
5391
Bram Moolenaard6754642005-01-17 22:18:45 +00005392/*
5393 * Concatenate two strings and return the result in allocated memory.
5394 * Returns NULL when out of memory.
5395 */
5396 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005397concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005398{
5399 char_u *dest;
5400 size_t l = STRLEN(str1);
5401
5402 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5403 if (dest != NULL)
5404 {
5405 STRCPY(dest, str1);
5406 STRCPY(dest + l, str2);
5407 }
5408 return dest;
5409}
Bram Moolenaard6754642005-01-17 22:18:45 +00005410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411/*
5412 * Add a path separator to a file name, unless it already ends in a path
5413 * separator.
5414 */
5415 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005416add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005418 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 STRCAT(p, PATHSEPSTR);
5420}
5421
5422/*
5423 * FullName_save - Make an allocated copy of a full file name.
5424 * Returns NULL when out of memory.
5425 */
5426 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005427FullName_save(
5428 char_u *fname,
5429 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005430 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 char_u *buf;
5433 char_u *new_fname = NULL;
5434
5435 if (fname == NULL)
5436 return NULL;
5437
5438 buf = alloc((unsigned)MAXPATHL);
5439 if (buf != NULL)
5440 {
5441 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5442 new_fname = vim_strsave(buf);
5443 else
5444 new_fname = vim_strsave(fname);
5445 vim_free(buf);
5446 }
5447 return new_fname;
5448}
5449
5450#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5451
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005452static char_u *skip_string(char_u *p);
5453static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005454static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005455static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457/*
5458 * Find the start of a comment, not knowing if we are in a comment right now.
5459 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005460 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005462 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005463ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005464{
5465 return find_start_comment(curbuf->b_ind_maxcomment);
5466}
5467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005469find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471 pos_T *pos;
5472 char_u *line;
5473 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005474 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005476 for (;;)
5477 {
5478 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5479 if (pos == NULL)
5480 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005482 /*
5483 * Check if the comment start we found is inside a string.
5484 * If it is then restrict the search to below this line and try again.
5485 */
5486 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005487 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005488 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005489 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005490 break;
5491 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5492 if (cur_maxcomment <= 0)
5493 {
5494 pos = NULL;
5495 break;
5496 }
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 return pos;
5499}
5500
5501/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005502 * Find the start of a comment or raw string, not knowing if we are in a
5503 * comment or raw string right now.
5504 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005505 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005506 * Return NULL when not inside a comment or raw string.
5507 * "CORS" -> Comment Or Raw String
5508 */
5509 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005510ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005511{
Bram Moolenaar089af182015-10-07 11:41:49 +02005512 static pos_T comment_pos_copy;
5513 pos_T *comment_pos;
5514 pos_T *rs_pos;
5515
5516 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5517 if (comment_pos != NULL)
5518 {
5519 /* Need to make a copy of the static pos in findmatchlimit(),
5520 * calling find_start_rawstring() may change it. */
5521 comment_pos_copy = *comment_pos;
5522 comment_pos = &comment_pos_copy;
5523 }
5524 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005525
5526 /* If comment_pos is before rs_pos the raw string is inside the comment.
5527 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005528 if (comment_pos == NULL || (rs_pos != NULL
5529 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005530 {
5531 if (is_raw != NULL && rs_pos != NULL)
5532 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005533 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005534 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005535 return comment_pos;
5536}
5537
5538/*
5539 * Find the start of a raw string, not knowing if we are in one right now.
5540 * Search starts at w_cursor.lnum and goes backwards.
5541 * Return NULL when not inside a raw string.
5542 */
5543 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005544find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005545{
5546 pos_T *pos;
5547 char_u *line;
5548 char_u *p;
5549 int cur_maxcomment = ind_maxcomment;
5550
5551 for (;;)
5552 {
5553 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5554 if (pos == NULL)
5555 break;
5556
5557 /*
5558 * Check if the raw string start we found is inside a string.
5559 * If it is then restrict the search to below this line and try again.
5560 */
5561 line = ml_get(pos->lnum);
5562 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5563 p = skip_string(p);
5564 if ((colnr_T)(p - line) <= pos->col)
5565 break;
5566 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5567 if (cur_maxcomment <= 0)
5568 {
5569 pos = NULL;
5570 break;
5571 }
5572 }
5573 return pos;
5574}
5575
5576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 * Skip to the end of a "string" and a 'c' character.
5578 * If there is no string or character, return argument unmodified.
5579 */
5580 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005581skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 int i;
5584
5585 /*
5586 * We loop, because strings may be concatenated: "date""time".
5587 */
5588 for ( ; ; ++p)
5589 {
5590 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5591 {
5592 if (!p[1]) /* ' at end of line */
5593 break;
5594 i = 2;
5595 if (p[1] == '\\') /* '\n' or '\000' */
5596 {
5597 ++i;
5598 while (vim_isdigit(p[i - 1])) /* '\000' */
5599 ++i;
5600 }
5601 if (p[i] == '\'') /* check for trailing ' */
5602 {
5603 p += i;
5604 continue;
5605 }
5606 }
5607 else if (p[0] == '"') /* start of string */
5608 {
5609 for (++p; p[0]; ++p)
5610 {
5611 if (p[0] == '\\' && p[1] != NUL)
5612 ++p;
5613 else if (p[0] == '"') /* end of string */
5614 break;
5615 }
5616 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005617 continue; /* continue for another string */
5618 }
5619 else if (p[0] == 'R' && p[1] == '"')
5620 {
5621 /* Raw string: R"[delim](...)[delim]" */
5622 char_u *delim = p + 2;
5623 char_u *paren = vim_strchr(delim, '(');
5624
5625 if (paren != NULL)
5626 {
5627 size_t delim_len = paren - delim;
5628
5629 for (p += 3; *p; ++p)
5630 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5631 && p[delim_len + 1] == '"')
5632 {
5633 p += delim_len + 1;
5634 break;
5635 }
5636 if (p[0] == '"')
5637 continue; /* continue for another string */
5638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 }
5640 break; /* no string found */
5641 }
5642 if (!*p)
5643 --p; /* backup from NUL */
5644 return p;
5645}
5646#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5647
5648#if defined(FEAT_CINDENT) || defined(PROTO)
5649
5650/*
5651 * Do C or expression indenting on the current line.
5652 */
5653 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005654do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656# ifdef FEAT_EVAL
5657 if (*curbuf->b_p_inde != NUL)
5658 fixthisline(get_expr_indent);
5659 else
5660# endif
5661 fixthisline(get_c_indent);
5662}
5663
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005664/* Find result cache for cpp_baseclass */
5665typedef struct {
5666 int found;
5667 lpos_T lpos;
5668} cpp_baseclass_cache_T;
5669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670/*
5671 * Functions for C-indenting.
5672 * Most of this originally comes from Eric Fischer.
5673 */
5674/*
5675 * Below "XXX" means that this function may unlock the current line.
5676 */
5677
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005678static char_u *cin_skipcomment(char_u *);
5679static int cin_nocode(char_u *);
5680static pos_T *find_line_comment(void);
5681static int cin_has_js_key(char_u *text);
5682static int cin_islabel_skip(char_u **);
5683static int cin_isdefault(char_u *);
5684static char_u *after_label(char_u *l);
5685static int get_indent_nolabel(linenr_T lnum);
5686static int skip_label(linenr_T, char_u **pp);
5687static int cin_first_id_amount(void);
5688static int cin_get_equal_amount(linenr_T lnum);
5689static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005690static int cin_iscomment(char_u *);
5691static int cin_islinecomment(char_u *);
5692static int cin_isterminated(char_u *, int, int);
5693static int cin_isinit(void);
5694static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5695static int cin_isif(char_u *);
5696static int cin_iselse(char_u *);
5697static int cin_isdo(char_u *);
5698static int cin_iswhileofdo(char_u *, linenr_T);
5699static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5700static int cin_iswhileofdo_end(int terminated);
5701static int cin_isbreak(char_u *);
5702static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5703static int get_baseclass_amount(int col);
5704static int cin_ends_in(char_u *, char_u *, char_u *);
5705static int cin_starts_with(char_u *s, char *word);
5706static int cin_skip2pos(pos_T *trypos);
5707static pos_T *find_start_brace(void);
5708static pos_T *find_match_paren(int);
5709static pos_T *find_match_char(int c, int ind_maxparen);
5710static int corr_ind_maxparen(pos_T *startpos);
5711static int find_last_paren(char_u *l, int start, int end);
5712static int find_match(int lookfor, linenr_T ourscope);
5713static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715/*
5716 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005717 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 */
5719 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005720cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 while (*s)
5723 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005724 char_u *prev_s = s;
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005727
5728 /* Perl/shell # comment comment continues until eol. Require a space
5729 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005730 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005731 {
5732 s += STRLEN(s);
5733 break;
5734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 if (*s != '/')
5736 break;
5737 ++s;
5738 if (*s == '/') /* slash-slash comment continues till eol */
5739 {
5740 s += STRLEN(s);
5741 break;
5742 }
5743 if (*s != '*')
5744 break;
5745 for (++s; *s; ++s) /* skip slash-star comment */
5746 if (s[0] == '*' && s[1] == '/')
5747 {
5748 s += 2;
5749 break;
5750 }
5751 }
5752 return s;
5753}
5754
5755/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005756 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 * not considered code.
5758 */
5759 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005760cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
5762 return *cin_skipcomment(s) == NUL;
5763}
5764
5765/*
5766 * Check previous lines for a "//" line comment, skipping over blank lines.
5767 */
5768 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005769find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
5771 static pos_T pos;
5772 char_u *line;
5773 char_u *p;
5774
5775 pos = curwin->w_cursor;
5776 while (--pos.lnum > 0)
5777 {
5778 line = ml_get(pos.lnum);
5779 p = skipwhite(line);
5780 if (cin_islinecomment(p))
5781 {
5782 pos.col = (int)(p - line);
5783 return &pos;
5784 }
5785 if (*p != NUL)
5786 break;
5787 }
5788 return NULL;
5789}
5790
5791/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005792 * Return TRUE if "text" starts with "key:".
5793 */
5794 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005795cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005796{
5797 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005798 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005799
5800 if (*s == '\'' || *s == '"')
5801 {
5802 /* can be 'key': or "key": */
5803 quote = *s;
5804 ++s;
5805 }
5806 if (!vim_isIDc(*s)) /* need at least one ID character */
5807 return FALSE;
5808
5809 while (vim_isIDc(*s))
5810 ++s;
5811 if (*s == quote)
5812 ++s;
5813
5814 s = cin_skipcomment(s);
5815
5816 /* "::" is not a label, it's C++ */
5817 return (*s == ':' && s[1] != ':');
5818}
5819
5820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005822 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 */
5824 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005825cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 if (!vim_isIDc(**s)) /* need at least one ID character */
5828 return FALSE;
5829
5830 while (vim_isIDc(**s))
5831 (*s)++;
5832
5833 *s = cin_skipcomment(*s);
5834
5835 /* "::" is not a label, it's C++ */
5836 return (**s == ':' && *++*s != ':');
5837}
5838
5839/*
5840 * Recognize a label: "label:".
5841 * Note: curwin->w_cursor must be where we are looking for the label.
5842 */
5843 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005844cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
5846 char_u *s;
5847
5848 s = cin_skipcomment(ml_get_curline());
5849
5850 /*
5851 * Exclude "default" from labels, since it should be indented
5852 * like a switch label. Same for C++ scope declarations.
5853 */
5854 if (cin_isdefault(s))
5855 return FALSE;
5856 if (cin_isscopedecl(s))
5857 return FALSE;
5858
5859 if (cin_islabel_skip(&s))
5860 {
5861 /*
5862 * Only accept a label if the previous line is terminated or is a case
5863 * label.
5864 */
5865 pos_T cursor_save;
5866 pos_T *trypos;
5867 char_u *line;
5868
5869 cursor_save = curwin->w_cursor;
5870 while (curwin->w_cursor.lnum > 1)
5871 {
5872 --curwin->w_cursor.lnum;
5873
5874 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005875 * If we're in a comment or raw string now, skip to the start of
5876 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 */
5878 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005879 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 curwin->w_cursor = *trypos;
5881
5882 line = ml_get_curline();
5883 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5884 continue;
5885 if (*(line = cin_skipcomment(line)) == NUL)
5886 continue;
5887
5888 curwin->w_cursor = cursor_save;
5889 if (cin_isterminated(line, TRUE, FALSE)
5890 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005891 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 || (cin_islabel_skip(&line) && cin_nocode(line)))
5893 return TRUE;
5894 return FALSE;
5895 }
5896 curwin->w_cursor = cursor_save;
5897 return TRUE; /* label at start of file??? */
5898 }
5899 return FALSE;
5900}
5901
5902/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005903 * Recognize structure initialization and enumerations:
5904 * "[typedef] [static|public|protected|private] enum"
5905 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 */
5907 static int
5908cin_isinit(void)
5909{
5910 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005911 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913 s = cin_skipcomment(ml_get_curline());
5914
Bram Moolenaar75342212013-03-07 13:13:52 +01005915 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 s = cin_skipcomment(s + 7);
5917
Bram Moolenaar75342212013-03-07 13:13:52 +01005918 for (;;)
5919 {
5920 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005921
Bram Moolenaar75342212013-03-07 13:13:52 +01005922 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5923 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005924 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005925 if (cin_starts_with(s, skip[i]))
5926 {
5927 s = cin_skipcomment(s + l);
5928 l = 0;
5929 break;
5930 }
5931 }
5932 if (l != 0)
5933 break;
5934 }
5935
5936 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 return TRUE;
5938
5939 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5940 return TRUE;
5941
5942 return FALSE;
5943}
5944
5945/*
5946 * Recognize a switch label: "case .*:" or "default:".
5947 */
5948 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005949cin_iscase(
5950 char_u *s,
5951 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005954 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 {
5956 for (s += 4; *s; ++s)
5957 {
5958 s = cin_skipcomment(s);
5959 if (*s == ':')
5960 {
5961 if (s[1] == ':') /* skip over "::" for C++ */
5962 ++s;
5963 else
5964 return TRUE;
5965 }
5966 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005967 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5969 return FALSE; /* stop at comment */
5970 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005971 {
5972 /* JS etc. */
5973 if (strict)
5974 return FALSE; /* stop at string */
5975 else
5976 return TRUE;
5977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
5979 return FALSE;
5980 }
5981
5982 if (cin_isdefault(s))
5983 return TRUE;
5984 return FALSE;
5985}
5986
5987/*
5988 * Recognize a "default" switch label.
5989 */
5990 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005991cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
5993 return (STRNCMP(s, "default", 7) == 0
5994 && *(s = cin_skipcomment(s + 7)) == ':'
5995 && s[1] != ':');
5996}
5997
5998/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005999 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 */
6001 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006002cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 int i;
6005
6006 s = cin_skipcomment(s);
6007 if (STRNCMP(s, "public", 6) == 0)
6008 i = 6;
6009 else if (STRNCMP(s, "protected", 9) == 0)
6010 i = 9;
6011 else if (STRNCMP(s, "private", 7) == 0)
6012 i = 7;
6013 else
6014 return FALSE;
6015 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6016}
6017
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006018/* Maximum number of lines to search back for a "namespace" line. */
6019#define FIND_NAMESPACE_LIM 20
6020
6021/*
6022 * Recognize a "namespace" scope declaration.
6023 */
6024 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006025cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006026{
6027 char_u *p;
6028 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006029 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006030
6031 s = cin_skipcomment(s);
6032 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6033 {
6034 p = cin_skipcomment(skipwhite(s + 9));
6035 while (*p != NUL)
6036 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006037 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006038 {
6039 has_name = TRUE; /* found end of a name */
6040 p = cin_skipcomment(skipwhite(p));
6041 }
6042 else if (*p == '{')
6043 {
6044 break;
6045 }
6046 else if (vim_iswordc(*p))
6047 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006048 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006049 if (has_name)
6050 return FALSE; /* word character after skipping past name */
6051 ++p;
6052 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006053 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6054 {
6055 if (!has_name_start || has_name)
6056 return FALSE;
6057 /* C++ 17 nested namespace */
6058 p += 3;
6059 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006060 else
6061 {
6062 return FALSE;
6063 }
6064 }
6065 return TRUE;
6066 }
6067 return FALSE;
6068}
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006071 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6072 */
6073 static int
6074cin_is_cpp_extern_c(char_u *s)
6075{
6076 char_u *p;
6077 int has_string_literal = FALSE;
6078
6079 s = cin_skipcomment(s);
6080 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6081 {
6082 p = cin_skipcomment(skipwhite(s + 6));
6083 while (*p != NUL)
6084 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006085 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006086 {
6087 p = cin_skipcomment(skipwhite(p));
6088 }
6089 else if (*p == '{')
6090 {
6091 break;
6092 }
6093 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6094 {
6095 if (has_string_literal)
6096 return FALSE;
6097 has_string_literal = TRUE;
6098 p += 3;
6099 }
6100 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6101 && p[4] == '"')
6102 {
6103 if (has_string_literal)
6104 return FALSE;
6105 has_string_literal = TRUE;
6106 p += 5;
6107 }
6108 else
6109 {
6110 return FALSE;
6111 }
6112 }
6113 return has_string_literal ? TRUE : FALSE;
6114 }
6115 return FALSE;
6116}
6117
6118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 * Return a pointer to the first non-empty non-comment character after a ':'.
6120 * Return NULL if not found.
6121 * case 234: a = b;
6122 * ^
6123 */
6124 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006125after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127 for ( ; *l; ++l)
6128 {
6129 if (*l == ':')
6130 {
6131 if (l[1] == ':') /* skip over "::" for C++ */
6132 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006133 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 break;
6135 }
6136 else if (*l == '\'' && l[1] && l[2] == '\'')
6137 l += 2; /* skip over 'x' */
6138 }
6139 if (*l == NUL)
6140 return NULL;
6141 l = cin_skipcomment(l + 1);
6142 if (*l == NUL)
6143 return NULL;
6144 return l;
6145}
6146
6147/*
6148 * Get indent of line "lnum", skipping a label.
6149 * Return 0 if there is nothing after the label.
6150 */
6151 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006152get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 char_u *l;
6155 pos_T fp;
6156 colnr_T col;
6157 char_u *p;
6158
6159 l = ml_get(lnum);
6160 p = after_label(l);
6161 if (p == NULL)
6162 return 0;
6163
6164 fp.col = (colnr_T)(p - l);
6165 fp.lnum = lnum;
6166 getvcol(curwin, &fp, &col, NULL, NULL);
6167 return (int)col;
6168}
6169
6170/*
6171 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006172 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 * label: if (asdf && asdfasdf)
6174 * ^
6175 */
6176 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006177skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 char_u *l;
6180 int amount;
6181 pos_T cursor_save;
6182
6183 cursor_save = curwin->w_cursor;
6184 curwin->w_cursor.lnum = lnum;
6185 l = ml_get_curline();
6186 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006187 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 {
6189 amount = get_indent_nolabel(lnum);
6190 l = after_label(ml_get_curline());
6191 if (l == NULL) /* just in case */
6192 l = ml_get_curline();
6193 }
6194 else
6195 {
6196 amount = get_indent();
6197 l = ml_get_curline();
6198 }
6199 *pp = l;
6200
6201 curwin->w_cursor = cursor_save;
6202 return amount;
6203}
6204
6205/*
6206 * Return the indent of the first variable name after a type in a declaration.
6207 * int a, indent of "a"
6208 * static struct foo b, indent of "b"
6209 * enum bla c, indent of "c"
6210 * Returns zero when it doesn't look like a declaration.
6211 */
6212 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006213cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 char_u *line, *p, *s;
6216 int len;
6217 pos_T fp;
6218 colnr_T col;
6219
6220 line = ml_get_curline();
6221 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006222 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6224 {
6225 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006226 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
6228 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6229 p = skipwhite(p + 6);
6230 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6231 p = skipwhite(p + 4);
6232 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6233 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6234 {
6235 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006236 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6237 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6238 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6239 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 p = s;
6241 }
6242 for (len = 0; vim_isIDc(p[len]); ++len)
6243 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006244 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 return 0;
6246
6247 p = skipwhite(p + len);
6248 fp.lnum = curwin->w_cursor.lnum;
6249 fp.col = (colnr_T)(p - line);
6250 getvcol(curwin, &fp, &col, NULL, NULL);
6251 return (int)col;
6252}
6253
6254/*
6255 * Return the indent of the first non-blank after an equal sign.
6256 * char *foo = "here";
6257 * Return zero if no (useful) equal sign found.
6258 * Return -1 if the line above "lnum" ends in a backslash.
6259 * foo = "asdf\
6260 * asdf\
6261 * here";
6262 */
6263 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006264cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 char_u *line;
6267 char_u *s;
6268 colnr_T col;
6269 pos_T fp;
6270
6271 if (lnum > 1)
6272 {
6273 line = ml_get(lnum - 1);
6274 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6275 return -1;
6276 }
6277
6278 line = s = ml_get(lnum);
6279 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6280 {
6281 if (cin_iscomment(s)) /* ignore comments */
6282 s = cin_skipcomment(s);
6283 else
6284 ++s;
6285 }
6286 if (*s != '=')
6287 return 0;
6288
6289 s = skipwhite(s + 1);
6290 if (cin_nocode(s))
6291 return 0;
6292
6293 if (*s == '"') /* nice alignment for continued strings */
6294 ++s;
6295
6296 fp.lnum = lnum;
6297 fp.col = (colnr_T)(s - line);
6298 getvcol(curwin, &fp, &col, NULL, NULL);
6299 return (int)col;
6300}
6301
6302/*
6303 * Recognize a preprocessor statement: Any line that starts with '#'.
6304 */
6305 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006306cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006308 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 return TRUE;
6310 return FALSE;
6311}
6312
6313/*
6314 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6315 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6316 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006317 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 */
6319 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006320cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321{
6322 char_u *line = *pp;
6323 linenr_T lnum = *lnump;
6324 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006325 int candidate_amount = *amount;
6326
6327 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6328 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006330 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
6332 if (cin_ispreproc(line))
6333 {
6334 retval = TRUE;
6335 *lnump = lnum;
6336 break;
6337 }
6338 if (lnum == 1)
6339 break;
6340 line = ml_get(--lnum);
6341 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6342 break;
6343 }
6344
6345 if (lnum != *lnump)
6346 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006347 if (retval)
6348 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 return retval;
6350}
6351
6352/*
6353 * Recognize the start of a C or C++ comment.
6354 */
6355 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006356cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6359}
6360
6361/*
6362 * Recognize the start of a "//" comment.
6363 */
6364 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006365cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
6367 return (p[0] == '/' && p[1] == '/');
6368}
6369
6370/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006371 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6372 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006374 * If a line begins with an "else", only consider it terminated if no unmatched
6375 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * Return the character terminating the line (ending char's have precedence if
6377 * both apply in order to determine initializations).
6378 */
6379 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006380cin_isterminated(
6381 char_u *s,
6382 int incl_open, /* include '{' at the end as terminator */
6383 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006385 char_u found_start = 0;
6386 unsigned n_open = 0;
6387 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
6389 s = cin_skipcomment(s);
6390
6391 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6392 found_start = *s;
6393
Bram Moolenaar496f9512011-05-19 16:35:09 +02006394 if (!found_start)
6395 is_else = cin_iselse(s);
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 while (*s)
6398 {
6399 /* skip over comments, "" strings and 'c'haracters */
6400 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006401 if (*s == '}' && n_open > 0)
6402 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006403 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006404 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 && cin_nocode(s + 1))
6406 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006407 else if (*s == '{')
6408 {
6409 if (incl_open && cin_nocode(s + 1))
6410 return *s;
6411 else
6412 ++n_open;
6413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415 if (*s)
6416 s++;
6417 }
6418 return found_start;
6419}
6420
6421/*
6422 * Recognize the basic picture of a function declaration -- it needs to
6423 * have an open paren somewhere and a close paren at the end of the line and
6424 * no semicolons anywhere.
6425 * When a line ends in a comma we continue looking in the next line.
6426 * "sp" points to a string with the line. When looking at other lines it must
6427 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006428 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006429 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 */
6431 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006432cin_isfuncdecl(
6433 char_u **sp,
6434 linenr_T first_lnum,
6435 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 char_u *s;
6438 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006439 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006441 pos_T *trypos;
6442 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443
6444 if (sp == NULL)
6445 s = ml_get(lnum);
6446 else
6447 s = *sp;
6448
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006449 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006450 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006451 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006452 {
6453 lnum = trypos->lnum;
6454 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006455 {
6456 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006457 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006458 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006459
6460 s = ml_get(lnum);
6461 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006462 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006463
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006464 /* Ignore line starting with #. */
6465 if (cin_ispreproc(s))
6466 return FALSE;
6467
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6469 {
6470 if (cin_iscomment(s)) /* ignore comments */
6471 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006472 else if (*s == ':')
6473 {
6474 if (*(s + 1) == ':')
6475 s += 2;
6476 else
6477 /* To avoid a mistake in the following situation:
6478 * A::A(int a, int b)
6479 * : a(0) // <--not a function decl
6480 * , b(0)
6481 * {...
6482 */
6483 return FALSE;
6484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 else
6486 ++s;
6487 }
6488 if (*s != '(')
6489 return FALSE; /* ';', ' or " before any () or no '(' */
6490
6491 while (*s && *s != ';' && *s != '\'' && *s != '"')
6492 {
6493 if (*s == ')' && cin_nocode(s + 1))
6494 {
6495 /* ')' at the end: may have found a match
6496 * Check for he previous line not to end in a backslash:
6497 * #if defined(x) && \
6498 * defined(y)
6499 */
6500 lnum = first_lnum - 1;
6501 s = ml_get(lnum);
6502 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6503 retval = TRUE;
6504 goto done;
6505 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006506 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006508 int comma = (*s == ',');
6509
6510 /* ',' at the end: continue looking in the next line.
6511 * At the end: check for ',' in the next line, for this style:
6512 * func(arg1
6513 * , arg2) */
6514 for (;;)
6515 {
6516 if (lnum >= curbuf->b_ml.ml_line_count)
6517 break;
6518 s = ml_get(++lnum);
6519 if (!cin_ispreproc(s))
6520 break;
6521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 if (lnum >= curbuf->b_ml.ml_line_count)
6523 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006524 /* Require a comma at end of the line or a comma or ')' at the
6525 * start of next line. */
6526 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006527 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006528 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006529 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 }
6531 else if (cin_iscomment(s)) /* ignore comments */
6532 s = cin_skipcomment(s);
6533 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006536 just_started = FALSE;
6537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 }
6539
6540done:
6541 if (lnum != first_lnum && sp != NULL)
6542 *sp = ml_get(first_lnum);
6543
6544 return retval;
6545}
6546
6547 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006548cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006550 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551}
6552
6553 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006554cin_iselse(
6555 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556{
6557 if (*p == '}') /* accept "} else" */
6558 p = cin_skipcomment(p + 1);
6559 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6560}
6561
6562 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006563cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
6565 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6566}
6567
6568/*
6569 * Check if this is a "while" that should have a matching "do".
6570 * We only accept a "while (condition) ;", with only white space between the
6571 * ')' and ';'. The condition may be spread over several lines.
6572 */
6573 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006574cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 pos_T cursor_save;
6577 pos_T *trypos;
6578 int retval = FALSE;
6579
6580 p = cin_skipcomment(p);
6581 if (*p == '}') /* accept "} while (cond);" */
6582 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006583 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 {
6585 cursor_save = curwin->w_cursor;
6586 curwin->w_cursor.lnum = lnum;
6587 curwin->w_cursor.col = 0;
6588 p = ml_get_curline();
6589 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6590 {
6591 ++p;
6592 ++curwin->w_cursor.col;
6593 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006594 if ((trypos = findmatchlimit(NULL, 0, 0,
6595 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6597 retval = TRUE;
6598 curwin->w_cursor = cursor_save;
6599 }
6600 return retval;
6601}
6602
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006603/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006604 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006605 * Return 0 if there is none.
6606 * Otherwise return !0 and update "*poffset" to point to the place where the
6607 * string was found.
6608 */
6609 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006610cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006611{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006612 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006613
6614 if (offset-- < 2)
6615 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006616 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006617 --offset;
6618
6619 offset -= 1;
6620 if (!STRNCMP(line + offset, "if", 2))
6621 goto probablyFound;
6622
6623 if (offset >= 1)
6624 {
6625 offset -= 1;
6626 if (!STRNCMP(line + offset, "for", 3))
6627 goto probablyFound;
6628
6629 if (offset >= 2)
6630 {
6631 offset -= 2;
6632 if (!STRNCMP(line + offset, "while", 5))
6633 goto probablyFound;
6634 }
6635 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006636 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006637
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006638probablyFound:
6639 if (!offset || !vim_isIDc(line[offset - 1]))
6640 {
6641 *poffset = offset;
6642 return 1;
6643 }
6644 return 0;
6645}
6646
6647/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006648 * Return TRUE if we are at the end of a do-while.
6649 * do
6650 * nothing;
6651 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006652 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006653 * Adjust the cursor to the line with "while".
6654 */
6655 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006656cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006657{
6658 char_u *line;
6659 char_u *p;
6660 char_u *s;
6661 pos_T *trypos;
6662 int i;
6663
6664 if (terminated != ';') /* there must be a ';' at the end */
6665 return FALSE;
6666
6667 p = line = ml_get_curline();
6668 while (*p != NUL)
6669 {
6670 p = cin_skipcomment(p);
6671 if (*p == ')')
6672 {
6673 s = skipwhite(p + 1);
6674 if (*s == ';' && cin_nocode(s + 1))
6675 {
6676 /* Found ");" at end of the line, now check there is "while"
6677 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006678 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006679 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006680 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006681 if (trypos != NULL)
6682 {
6683 s = cin_skipcomment(ml_get(trypos->lnum));
6684 if (*s == '}') /* accept "} while (cond);" */
6685 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006686 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006687 {
6688 curwin->w_cursor.lnum = trypos->lnum;
6689 return TRUE;
6690 }
6691 }
6692
6693 /* Searching may have made "line" invalid, get it again. */
6694 line = ml_get_curline();
6695 p = line + i;
6696 }
6697 }
6698 if (*p != NUL)
6699 ++p;
6700 }
6701 return FALSE;
6702}
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006705cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6708}
6709
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006710/*
6711 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 * constructor-initialization. eg:
6713 *
6714 * class MyClass :
6715 * baseClass <-- here
6716 * class MyClass : public baseClass,
6717 * anotherBaseClass <-- here (should probably lineup ??)
6718 * MyClass::MyClass(...) :
6719 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006720 *
6721 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 */
6723 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006724cin_is_cpp_baseclass(
6725 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006727 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 char_u *s;
6729 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006730 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006731 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006733 if (pos->lnum <= lnum)
6734 return cached->found; /* Use the cached result */
6735
6736 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006738 s = skipwhite(line);
6739 if (*s == '#') /* skip #define FOO x ? (x) : x */
6740 return FALSE;
6741 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (*s == NUL)
6743 return FALSE;
6744
6745 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6746
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006747 /* Search for a line starting with '#', empty, ending in ';' or containing
6748 * '{' or '}' and start below it. This handles the following situations:
6749 * a = cond ?
6750 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006751 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006752 * func::foo()
6753 * : something
6754 * {}
6755 * Foo::Foo (int one, int two)
6756 * : something(4),
6757 * somethingelse(3)
6758 * {}
6759 */
6760 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006762 line = ml_get(lnum - 1);
6763 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006764 if (*s == '#' || *s == NUL)
6765 break;
6766 while (*s != NUL)
6767 {
6768 s = cin_skipcomment(s);
6769 if (*s == '{' || *s == '}'
6770 || (*s == ';' && cin_nocode(s + 1)))
6771 break;
6772 if (*s != NUL)
6773 ++s;
6774 }
6775 if (*s != NUL)
6776 break;
6777 --lnum;
6778 }
6779
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006780 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006781 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006782 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006783 for (;;)
6784 {
6785 if (*s == NUL)
6786 {
6787 if (lnum == curwin->w_cursor.lnum)
6788 break;
6789 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006790 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006791 s = line;
6792 }
6793 if (s == line)
6794 {
6795 /* don't recognize "case (foo):" as a baseclass */
6796 if (cin_iscase(s, FALSE))
6797 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006798 s = cin_skipcomment(line);
6799 if (*s == NUL)
6800 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006801 }
6802
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006803 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006804 s = skip_string(s) + 1;
6805 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
6807 if (s[1] == ':')
6808 {
6809 /* skip double colon. It can't be a constructor
6810 * initialization any more */
6811 lookfor_ctor_init = FALSE;
6812 s = cin_skipcomment(s + 2);
6813 }
6814 else if (lookfor_ctor_init || class_or_struct)
6815 {
6816 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006817 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 cpp_base_class = TRUE;
6819 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006820 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 s = cin_skipcomment(s + 1);
6822 }
6823 else
6824 s = cin_skipcomment(s + 1);
6825 }
6826 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6827 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6828 {
6829 class_or_struct = TRUE;
6830 lookfor_ctor_init = FALSE;
6831
6832 if (*s == 'c')
6833 s = cin_skipcomment(s + 5);
6834 else
6835 s = cin_skipcomment(s + 6);
6836 }
6837 else
6838 {
6839 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6840 {
6841 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6842 }
6843 else if (s[0] == ')')
6844 {
6845 /* Constructor-initialization is assumed if we come across
6846 * something like "):" */
6847 class_or_struct = FALSE;
6848 lookfor_ctor_init = TRUE;
6849 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006850 else if (s[0] == '?')
6851 {
6852 /* Avoid seeing '() :' after '?' as constructor init. */
6853 return FALSE;
6854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 else if (!vim_isIDc(s[0]))
6856 {
6857 /* if it is not an identifier, we are wrong */
6858 class_or_struct = FALSE;
6859 lookfor_ctor_init = FALSE;
6860 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006861 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
6863 /* it can't be a constructor-initialization any more */
6864 lookfor_ctor_init = FALSE;
6865
6866 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006867 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006868 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006871 /* When the line ends in a comma don't align with it. */
6872 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006873 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006874
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 s = cin_skipcomment(s + 1);
6876 }
6877 }
6878
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006879 cached->found = cpp_base_class;
6880 if (cpp_base_class)
6881 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 return cpp_base_class;
6883}
6884
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006885 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006886get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006887{
6888 int amount;
6889 colnr_T vcol;
6890 pos_T *trypos;
6891
6892 if (col == 0)
6893 {
6894 amount = get_indent();
6895 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006896 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006897 amount = get_indent_lnum(trypos->lnum); /* XXX */
6898 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006899 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006900 }
6901 else
6902 {
6903 curwin->w_cursor.col = col;
6904 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6905 amount = (int)vcol;
6906 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006907 if (amount < curbuf->b_ind_cpp_baseclass)
6908 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006909 return amount;
6910}
6911
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912/*
6913 * Return TRUE if string "s" ends with the string "find", possibly followed by
6914 * white space and comments. Skip strings and comments.
6915 * Ignore "ignore" after "find" if it's not NULL.
6916 */
6917 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006918cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 char_u *p = s;
6921 char_u *r;
6922 int len = (int)STRLEN(find);
6923
6924 while (*p != NUL)
6925 {
6926 p = cin_skipcomment(p);
6927 if (STRNCMP(p, find, len) == 0)
6928 {
6929 r = skipwhite(p + len);
6930 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6931 r = skipwhite(r + STRLEN(ignore));
6932 if (cin_nocode(r))
6933 return TRUE;
6934 }
6935 if (*p != NUL)
6936 ++p;
6937 }
6938 return FALSE;
6939}
6940
6941/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006942 * Return TRUE when "s" starts with "word" and then a non-ID character.
6943 */
6944 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006945cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006946{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006947 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006948
6949 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6950}
6951
6952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 * Skip strings, chars and comments until at or past "trypos".
6954 * Return the column found.
6955 */
6956 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006957cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
6959 char_u *line;
6960 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006961 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
6963 p = line = ml_get(trypos->lnum);
6964 while (*p && (colnr_T)(p - line) < trypos->col)
6965 {
6966 if (cin_iscomment(p))
6967 p = cin_skipcomment(p);
6968 else
6969 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006970 new_p = skip_string(p);
6971 if (new_p == p)
6972 ++p;
6973 else
6974 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 }
6976 }
6977 return (int)(p - line);
6978}
6979
6980/*
6981 * Find the '{' at the start of the block we are in.
6982 * Return NULL if no match found.
6983 * Ignore a '{' that is in a comment, makes indenting the next three lines
6984 * work. */
6985/* foo() */
6986/* { */
6987/* } */
6988
6989 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006990find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 pos_T cursor_save;
6993 pos_T *trypos;
6994 pos_T *pos;
6995 static pos_T pos_copy;
6996
6997 cursor_save = curwin->w_cursor;
6998 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6999 {
7000 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
7001 trypos = &pos_copy;
7002 curwin->w_cursor = *trypos;
7003 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007004 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02007006 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 break;
7008 if (pos != NULL)
7009 curwin->w_cursor.lnum = pos->lnum;
7010 }
7011 curwin->w_cursor = cursor_save;
7012 return trypos;
7013}
7014
7015/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007016 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007017 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 */
7019 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007020find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007022 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007023}
7024
7025 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007026find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007027{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 pos_T cursor_save;
7029 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007030 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007031 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
7033 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007034 ind_maxp_wk = ind_maxparen;
7035retry:
7036 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 /* check if the ( is in a // comment */
7039 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007040 {
7041 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7042 if (ind_maxp_wk > 0)
7043 {
7044 curwin->w_cursor = *trypos;
7045 curwin->w_cursor.col = 0; /* XXX */
7046 goto retry;
7047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 else
7051 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007052 pos_T *trypos_wk;
7053
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7055 trypos = &pos_copy;
7056 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007057 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007058 {
7059 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7060 - trypos_wk->lnum);
7061 if (ind_maxp_wk > 0)
7062 {
7063 curwin->w_cursor = *trypos_wk;
7064 goto retry;
7065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 }
7069 }
7070 curwin->w_cursor = cursor_save;
7071 return trypos;
7072}
7073
7074/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007075 * Find the matching '(', ignoring it if it is in a comment or before an
7076 * unmatched {.
7077 * Return NULL if no match found.
7078 */
7079 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007080find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007081{
7082 pos_T *trypos = find_match_paren(ind_maxparen);
7083
7084 if (trypos != NULL)
7085 {
7086 pos_T *tryposBrace = find_start_brace();
7087
7088 /* If both an unmatched '(' and '{' is found. Ignore the '('
7089 * position if the '{' is further down. */
7090 if (tryposBrace != NULL
7091 && (trypos->lnum != tryposBrace->lnum
7092 ? trypos->lnum < tryposBrace->lnum
7093 : trypos->col < tryposBrace->col))
7094 trypos = NULL;
7095 }
7096 return trypos;
7097}
7098
7099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 * Return ind_maxparen corrected for the difference in line number between the
7101 * cursor position and "startpos". This makes sure that searching for a
7102 * matching paren above the cursor line doesn't find a match because of
7103 * looking a few lines further.
7104 */
7105 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007106corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
7108 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7109
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007110 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7111 return curbuf->b_ind_maxparen - (int)n;
7112 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113}
7114
7115/*
7116 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007117 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 */
7119 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007120find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 int i;
7123 int retval = FALSE;
7124 int open_count = 0;
7125
7126 curwin->w_cursor.col = 0; /* default is start of line */
7127
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007128 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {
7130 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7131 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7132 if (l[i] == start)
7133 ++open_count;
7134 else if (l[i] == end)
7135 {
7136 if (open_count > 0)
7137 --open_count;
7138 else
7139 {
7140 curwin->w_cursor.col = i;
7141 retval = TRUE;
7142 }
7143 }
7144 }
7145 return retval;
7146}
7147
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007148/*
7149 * Parse 'cinoptions' and set the values in "curbuf".
7150 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7151 */
7152 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007153parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007154{
7155 char_u *p;
7156 char_u *l;
7157 char_u *digits;
7158 int n;
7159 int divider;
7160 int fraction = 0;
7161 int sw = (int)get_sw_value(buf);
7162
7163 /*
7164 * Set the default values.
7165 */
7166 /* Spaces from a block's opening brace the prevailing indent for that
7167 * block should be. */
7168 buf->b_ind_level = sw;
7169
7170 /* Spaces from the edge of the line an open brace that's at the end of a
7171 * line is imagined to be. */
7172 buf->b_ind_open_imag = 0;
7173
7174 /* Spaces from the prevailing indent for a line that is not preceded by
7175 * an opening brace. */
7176 buf->b_ind_no_brace = 0;
7177
7178 /* Column where the first { of a function should be located }. */
7179 buf->b_ind_first_open = 0;
7180
7181 /* Spaces from the prevailing indent a leftmost open brace should be
7182 * located. */
7183 buf->b_ind_open_extra = 0;
7184
7185 /* Spaces from the matching open brace (real location for one at the left
7186 * edge; imaginary location from one that ends a line) the matching close
7187 * brace should be located. */
7188 buf->b_ind_close_extra = 0;
7189
7190 /* Spaces from the edge of the line an open brace sitting in the leftmost
7191 * column is imagined to be. */
7192 buf->b_ind_open_left_imag = 0;
7193
7194 /* Spaces jump labels should be shifted to the left if N is non-negative,
7195 * otherwise the jump label will be put to column 1. */
7196 buf->b_ind_jump_label = -1;
7197
7198 /* Spaces from the switch() indent a "case xx" label should be located. */
7199 buf->b_ind_case = sw;
7200
7201 /* Spaces from the "case xx:" code after a switch() should be located. */
7202 buf->b_ind_case_code = sw;
7203
7204 /* Lineup break at end of case in switch() with case label. */
7205 buf->b_ind_case_break = 0;
7206
7207 /* Spaces from the class declaration indent a scope declaration label
7208 * should be located. */
7209 buf->b_ind_scopedecl = sw;
7210
7211 /* Spaces from the scope declaration label code should be located. */
7212 buf->b_ind_scopedecl_code = sw;
7213
7214 /* Amount K&R-style parameters should be indented. */
7215 buf->b_ind_param = sw;
7216
7217 /* Amount a function type spec should be indented. */
7218 buf->b_ind_func_type = sw;
7219
7220 /* Amount a cpp base class declaration or constructor initialization
7221 * should be indented. */
7222 buf->b_ind_cpp_baseclass = sw;
7223
7224 /* additional spaces beyond the prevailing indent a continuation line
7225 * should be located. */
7226 buf->b_ind_continuation = sw;
7227
7228 /* Spaces from the indent of the line with an unclosed parentheses. */
7229 buf->b_ind_unclosed = sw * 2;
7230
7231 /* Spaces from the indent of the line with an unclosed parentheses, which
7232 * itself is also unclosed. */
7233 buf->b_ind_unclosed2 = sw;
7234
7235 /* Suppress ignoring spaces from the indent of a line starting with an
7236 * unclosed parentheses. */
7237 buf->b_ind_unclosed_noignore = 0;
7238
7239 /* If the opening paren is the last nonwhite character on the line, and
7240 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7241 * context (for very long lines). */
7242 buf->b_ind_unclosed_wrapped = 0;
7243
7244 /* Suppress ignoring white space when lining up with the character after
7245 * an unclosed parentheses. */
7246 buf->b_ind_unclosed_whiteok = 0;
7247
7248 /* Indent a closing parentheses under the line start of the matching
7249 * opening parentheses. */
7250 buf->b_ind_matching_paren = 0;
7251
7252 /* Indent a closing parentheses under the previous line. */
7253 buf->b_ind_paren_prev = 0;
7254
7255 /* Extra indent for comments. */
7256 buf->b_ind_comment = 0;
7257
7258 /* Spaces from the comment opener when there is nothing after it. */
7259 buf->b_ind_in_comment = 3;
7260
7261 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7262 * after the comment opener. */
7263 buf->b_ind_in_comment2 = 0;
7264
7265 /* Max lines to search for an open paren. */
7266 buf->b_ind_maxparen = 20;
7267
7268 /* Max lines to search for an open comment. */
7269 buf->b_ind_maxcomment = 70;
7270
7271 /* Handle braces for java code. */
7272 buf->b_ind_java = 0;
7273
7274 /* Not to confuse JS object properties with labels. */
7275 buf->b_ind_js = 0;
7276
7277 /* Handle blocked cases correctly. */
7278 buf->b_ind_keep_case_label = 0;
7279
7280 /* Handle C++ namespace. */
7281 buf->b_ind_cpp_namespace = 0;
7282
7283 /* Handle continuation lines containing conditions of if(), for() and
7284 * while(). */
7285 buf->b_ind_if_for_while = 0;
7286
Bram Moolenaar6b643942017-03-05 19:44:06 +01007287 /* indentation for # comments */
7288 buf->b_ind_hash_comment = 0;
7289
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007290 /* Handle C++ extern "C" or "C++" */
7291 buf->b_ind_cpp_extern_c = 0;
7292
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007293 for (p = buf->b_p_cino; *p; )
7294 {
7295 l = p++;
7296 if (*p == '-')
7297 ++p;
7298 digits = p; /* remember where the digits start */
7299 n = getdigits(&p);
7300 divider = 0;
7301 if (*p == '.') /* ".5s" means a fraction */
7302 {
7303 fraction = atol((char *)++p);
7304 while (VIM_ISDIGIT(*p))
7305 {
7306 ++p;
7307 if (divider)
7308 divider *= 10;
7309 else
7310 divider = 10;
7311 }
7312 }
7313 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7314 {
7315 if (p == digits)
7316 n = sw; /* just "s" is one 'shiftwidth' */
7317 else
7318 {
7319 n *= sw;
7320 if (divider)
7321 n += (sw * fraction + divider / 2) / divider;
7322 }
7323 ++p;
7324 }
7325 if (l[1] == '-')
7326 n = -n;
7327
7328 /* When adding an entry here, also update the default 'cinoptions' in
7329 * doc/indent.txt, and add explanation for it! */
7330 switch (*l)
7331 {
7332 case '>': buf->b_ind_level = n; break;
7333 case 'e': buf->b_ind_open_imag = n; break;
7334 case 'n': buf->b_ind_no_brace = n; break;
7335 case 'f': buf->b_ind_first_open = n; break;
7336 case '{': buf->b_ind_open_extra = n; break;
7337 case '}': buf->b_ind_close_extra = n; break;
7338 case '^': buf->b_ind_open_left_imag = n; break;
7339 case 'L': buf->b_ind_jump_label = n; break;
7340 case ':': buf->b_ind_case = n; break;
7341 case '=': buf->b_ind_case_code = n; break;
7342 case 'b': buf->b_ind_case_break = n; break;
7343 case 'p': buf->b_ind_param = n; break;
7344 case 't': buf->b_ind_func_type = n; break;
7345 case '/': buf->b_ind_comment = n; break;
7346 case 'c': buf->b_ind_in_comment = n; break;
7347 case 'C': buf->b_ind_in_comment2 = n; break;
7348 case 'i': buf->b_ind_cpp_baseclass = n; break;
7349 case '+': buf->b_ind_continuation = n; break;
7350 case '(': buf->b_ind_unclosed = n; break;
7351 case 'u': buf->b_ind_unclosed2 = n; break;
7352 case 'U': buf->b_ind_unclosed_noignore = n; break;
7353 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7354 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7355 case 'm': buf->b_ind_matching_paren = n; break;
7356 case 'M': buf->b_ind_paren_prev = n; break;
7357 case ')': buf->b_ind_maxparen = n; break;
7358 case '*': buf->b_ind_maxcomment = n; break;
7359 case 'g': buf->b_ind_scopedecl = n; break;
7360 case 'h': buf->b_ind_scopedecl_code = n; break;
7361 case 'j': buf->b_ind_java = n; break;
7362 case 'J': buf->b_ind_js = n; break;
7363 case 'l': buf->b_ind_keep_case_label = n; break;
7364 case '#': buf->b_ind_hash_comment = n; break;
7365 case 'N': buf->b_ind_cpp_namespace = n; break;
7366 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007367 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007368 }
7369 if (*p == ',')
7370 ++p;
7371 }
7372}
7373
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007374/*
7375 * Return the desired indent for C code.
7376 * Return -1 if the indent should be left alone (inside a raw string).
7377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007379get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 pos_T cur_curpos;
7382 int amount;
7383 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007384 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 colnr_T col;
7386 char_u *theline;
7387 char_u *linecopy;
7388 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007389 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007391 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 pos_T our_paren_pos;
7393 char_u *start;
7394 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007395#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396#define BRACE_AT_START 2 /* '{' is at start of line */
7397#define BRACE_AT_END 3 /* '{' is at end of line */
7398 linenr_T ourscope;
7399 char_u *l;
7400 char_u *look;
7401 char_u terminated;
7402 int lookfor;
7403#define LOOKFOR_INITIAL 0
7404#define LOOKFOR_IF 1
7405#define LOOKFOR_DO 2
7406#define LOOKFOR_CASE 3
7407#define LOOKFOR_ANY 4
7408#define LOOKFOR_TERM 5
7409#define LOOKFOR_UNTERM 6
7410#define LOOKFOR_SCOPEDECL 7
7411#define LOOKFOR_NOBREAK 8
7412#define LOOKFOR_CPP_BASECLASS 9
7413#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007414#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007415#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
7417 int whilelevel;
7418 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 int n;
7420 int iscase;
7421 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007422 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007424 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007425 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007426 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007427 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007428 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007430 /* make a copy, value is changed below */
7431 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
7433 /* remember where the cursor was when we started */
7434 cur_curpos = curwin->w_cursor;
7435
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007436 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007437 if (cur_curpos.lnum == 1)
7438 return 0;
7439
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 /* Get a copy of the current contents of the line.
7441 * This is required, because only the most recent line obtained with
7442 * ml_get is valid! */
7443 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7444 if (linecopy == NULL)
7445 return 0;
7446
7447 /*
7448 * In insert mode and the cursor is on a ')' truncate the line at the
7449 * cursor position. We don't want to line up with the matching '(' when
7450 * inserting new stuff.
7451 * For unknown reasons the cursor might be past the end of the line, thus
7452 * check for that.
7453 */
7454 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007455 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 && linecopy[curwin->w_cursor.col] == ')')
7457 linecopy[curwin->w_cursor.col] = NUL;
7458
7459 theline = skipwhite(linecopy);
7460
7461 /* move the cursor to the start of the line */
7462
7463 curwin->w_cursor.col = 0;
7464
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007465 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007466
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007468 * If we are inside a raw string don't change the indent.
7469 * Ignore a raw string inside a comment.
7470 */
7471 comment_pos = ind_find_start_comment();
7472 if (comment_pos != NULL)
7473 {
7474 /* findmatchlimit() static pos is overwritten, make a copy */
7475 tryposCopy = *comment_pos;
7476 comment_pos = &tryposCopy;
7477 }
7478 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007479 if (trypos != NULL && (comment_pos == NULL
7480 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007481 {
7482 amount = -1;
7483 goto laterend;
7484 }
7485
7486 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 * #defines and so on always go at the left when included in 'cinkeys'.
7488 */
7489 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007490 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007491 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007492 goto theend;
7493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
7495 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007496 * Is it a non-case label? Then that goes at the left margin too unless:
7497 * - JS flag is set.
7498 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007500 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007501 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
7503 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007504 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 }
7506
7507 /*
7508 * If we're inside a "//" comment and there is a "//" comment in a
7509 * previous line, lineup with that one.
7510 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007511 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 && (trypos = find_line_comment()) != NULL) /* XXX */
7513 {
7514 /* find how indented the line beginning the comment is */
7515 getvcol(curwin, trypos, &col, NULL, NULL);
7516 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007517 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
7519
7520 /*
7521 * If we're inside a comment and not looking at the start of the
7522 * comment, try using the 'comments' option.
7523 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007524 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
7526 int lead_start_len = 2;
7527 int lead_middle_len = 1;
7528 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7529 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7530 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7531 char_u *p;
7532 int start_align = 0;
7533 int start_off = 0;
7534 int done = FALSE;
7535
7536 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007537 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007539 *lead_start = NUL;
7540 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541
7542 p = curbuf->b_p_com;
7543 while (*p != NUL)
7544 {
7545 int align = 0;
7546 int off = 0;
7547 int what = 0;
7548
7549 while (*p != NUL && *p != ':')
7550 {
7551 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7552 what = *p++;
7553 else if (*p == COM_LEFT || *p == COM_RIGHT)
7554 align = *p++;
7555 else if (VIM_ISDIGIT(*p) || *p == '-')
7556 off = getdigits(&p);
7557 else
7558 ++p;
7559 }
7560
7561 if (*p == ':')
7562 ++p;
7563 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7564 if (what == COM_START)
7565 {
7566 STRCPY(lead_start, lead_end);
7567 lead_start_len = (int)STRLEN(lead_start);
7568 start_off = off;
7569 start_align = align;
7570 }
7571 else if (what == COM_MIDDLE)
7572 {
7573 STRCPY(lead_middle, lead_end);
7574 lead_middle_len = (int)STRLEN(lead_middle);
7575 }
7576 else if (what == COM_END)
7577 {
7578 /* If our line starts with the middle comment string, line it
7579 * up with the comment opener per the 'comments' option. */
7580 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7581 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7582 {
7583 done = TRUE;
7584 if (curwin->w_cursor.lnum > 1)
7585 {
7586 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007587 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 * the middle comment string matches in the previous
7589 * line, use the indent of that line. XXX */
7590 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7591 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7592 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7593 else if (STRNCMP(look, lead_middle,
7594 lead_middle_len) == 0)
7595 {
7596 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7597 break;
7598 }
7599 /* If the start comment string doesn't match with the
7600 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007601 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 lead_start, lead_start_len) != 0)
7603 continue;
7604 }
7605 if (start_off != 0)
7606 amount += start_off;
7607 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007608 amount += vim_strsize(lead_start)
7609 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 break;
7611 }
7612
7613 /* If our line starts with the end comment string, line it up
7614 * with the middle comment */
7615 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7616 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7617 {
7618 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7619 /* XXX */
7620 if (off != 0)
7621 amount += off;
7622 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007623 amount += vim_strsize(lead_start)
7624 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 done = TRUE;
7626 break;
7627 }
7628 }
7629 }
7630
7631 /* If our line starts with an asterisk, line up with the
7632 * asterisk in the comment opener; otherwise, line up
7633 * with the first character of the comment text.
7634 */
7635 if (done)
7636 ;
7637 else if (theline[0] == '*')
7638 amount += 1;
7639 else
7640 {
7641 /*
7642 * If we are more than one line away from the comment opener, take
7643 * the indent of the previous non-empty line. If 'cino' has "CO"
7644 * and we are just below the comment opener and there are any
7645 * white characters after it line up with the text after it;
7646 * otherwise, add the amount specified by "c" in 'cino'
7647 */
7648 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007649 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {
7651 if (linewhite(lnum)) /* skip blank lines */
7652 continue;
7653 amount = get_indent_lnum(lnum); /* XXX */
7654 break;
7655 }
7656 if (amount == -1) /* use the comment opener */
7657 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007658 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007660 start = ml_get(comment_pos->lnum);
7661 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007663 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007665 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007667 if (curbuf->b_ind_in_comment2 || *look == NUL)
7668 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
7670 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007671 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 }
7673
7674 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007675 * Are we looking at a ']' that has a match?
7676 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007677 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007678 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7679 {
7680 /* align with the line containing the '['. */
7681 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007682 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007683 }
7684
7685 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 * Are we inside parentheses or braces?
7687 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007688 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007689 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007690 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 || trypos != NULL)
7692 {
7693 if (trypos != NULL && tryposBrace != NULL)
7694 {
7695 /* Both an unmatched '(' and '{' is found. Use the one which is
7696 * closer to the current cursor position, set the other to NULL. */
7697 if (trypos->lnum != tryposBrace->lnum
7698 ? trypos->lnum < tryposBrace->lnum
7699 : trypos->col < tryposBrace->col)
7700 trypos = NULL;
7701 else
7702 tryposBrace = NULL;
7703 }
7704
7705 if (trypos != NULL)
7706 {
7707 /*
7708 * If the matching paren is more than one line away, use the indent of
7709 * a previous non-empty line that matches the same paren.
7710 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007711 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007713 /* Line up with the start of the matching paren line. */
7714 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7715 }
7716 else
7717 {
7718 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007719 our_paren_pos = *trypos;
7720 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007722 l = skipwhite(ml_get(lnum));
7723 if (cin_nocode(l)) /* skip comment lines */
7724 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007725 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007726 continue; /* ignore #define, #if, etc. */
7727 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007729 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007730 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007731 {
7732 lnum = trypos->lnum + 1;
7733 continue;
7734 }
7735
7736 /* XXX */
7737 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007738 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007739 && trypos->lnum == our_paren_pos.lnum
7740 && trypos->col == our_paren_pos.col)
7741 {
7742 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007744 if (theline[0] == ')')
7745 {
7746 if (our_paren_pos.lnum != lnum
7747 && cur_amount > amount)
7748 cur_amount = amount;
7749 amount = -1;
7750 }
7751 break;
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754 }
7755
7756 /*
7757 * Line up with line where the matching paren is. XXX
7758 * If the line starts with a '(' or the indent for unclosed
7759 * parentheses is zero, line up with the unclosed parentheses.
7760 */
7761 if (amount == -1)
7762 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007763 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007764 int is_if_for_while = 0;
7765
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007766 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007767 {
7768 /* Look for the outermost opening parenthesis on this line
7769 * and check whether it belongs to an "if", "for" or "while". */
7770
7771 pos_T cursor_save = curwin->w_cursor;
7772 pos_T outermost;
7773 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007774
7775 trypos = &our_paren_pos;
7776 do {
7777 outermost = *trypos;
7778 curwin->w_cursor.lnum = outermost.lnum;
7779 curwin->w_cursor.col = outermost.col;
7780
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007781 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007782 } while (trypos && trypos->lnum == outermost.lnum);
7783
7784 curwin->w_cursor = cursor_save;
7785
7786 line = ml_get(outermost.lnum);
7787
7788 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007789 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007790 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007791
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007792 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007793 look = skipwhite(look);
7794 if (*look == '(')
7795 {
7796 linenr_T save_lnum = curwin->w_cursor.lnum;
7797 char_u *line;
7798 int look_col;
7799
7800 /* Ignore a '(' in front of the line that has a match before
7801 * our matching '('. */
7802 curwin->w_cursor.lnum = our_paren_pos.lnum;
7803 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007804 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007805 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007806 if ((trypos = findmatchlimit(NULL, ')', 0,
7807 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007808 != NULL
7809 && trypos->lnum == our_paren_pos.lnum
7810 && trypos->col < our_paren_pos.col)
7811 ignore_paren_col = trypos->col + 1;
7812
7813 curwin->w_cursor.lnum = save_lnum;
7814 look = ml_get(our_paren_pos.lnum) + look_col;
7815 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007816 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7817 && is_if_for_while == 0)
7818 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007819 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {
7821 /*
7822 * If we're looking at a close paren, line up right there;
7823 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007824 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 * the last nonwhite character of the line, use either the
7826 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007827 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 * lines).
7829 */
7830 if (theline[0] != ')')
7831 {
7832 cur_amount = MAXCOL;
7833 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007834 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 && cin_ends_in(l, (char_u *)"(", NULL))
7836 {
7837 /* look for opening unmatched paren, indent one level
7838 * for each additional level */
7839 n = 1;
7840 for (col = 0; col < our_paren_pos.col; ++col)
7841 {
7842 switch (l[col])
7843 {
7844 case '(':
7845 case '{': ++n;
7846 break;
7847
7848 case ')':
7849 case '}': if (n > 1)
7850 --n;
7851 break;
7852 }
7853 }
7854
7855 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007856 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007858 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 our_paren_pos.col++;
7860 else
7861 {
7862 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007863 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 col++;
7865 if (l[col] != NUL) /* In case of trailing space */
7866 our_paren_pos.col = col;
7867 else
7868 our_paren_pos.col++;
7869 }
7870 }
7871
7872 /*
7873 * Find how indented the paren is, or the character after it
7874 * if we did the above "if".
7875 */
7876 if (our_paren_pos.col > 0)
7877 {
7878 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7879 if (cur_amount > (int)col)
7880 cur_amount = col;
7881 }
7882 }
7883
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007884 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {
7886 /* Line up with the start of the matching paren line. */
7887 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007888 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7889 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007890 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
7892 if (cur_amount != MAXCOL)
7893 amount = cur_amount;
7894 }
7895 else
7896 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007897 /* Add b_ind_unclosed2 for each '(' before our matching one,
7898 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007900 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {
7902 --our_paren_pos.col;
7903 switch (*ml_get_pos(&our_paren_pos))
7904 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007905 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 col = our_paren_pos.col;
7907 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007908 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 col = MAXCOL;
7910 break;
7911 }
7912 }
7913
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007914 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 * braces */
7916 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007917 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 else
7919 {
7920 curwin->w_cursor.lnum = our_paren_pos.lnum;
7921 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007922 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7923 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007924 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007926 {
7927 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007928 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007929 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007930 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 }
7933 /*
7934 * For a line starting with ')' use the minimum of the two
7935 * positions, to avoid giving it more indent than the previous
7936 * lines:
7937 * func_long_name( if (x
7938 * arg && yy
7939 * ) ^ not here ) ^ not here
7940 */
7941 if (cur_amount < amount)
7942 amount = cur_amount;
7943 }
7944 }
7945
7946 /* add extra indent for a comment */
7947 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007948 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 else
7951 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007952 /*
7953 * We are inside braces, there is a { before this line at the position
7954 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007955 * Make a copy of tryposBrace, it may point to pos_copy inside
7956 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007957 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007958 tryposCopy = *tryposBrace;
7959 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 ourscope = trypos->lnum;
7962 start = ml_get(ourscope);
7963
7964 /*
7965 * Now figure out how indented the line is in general.
7966 * If the brace was at the start of the line, we use that;
7967 * otherwise, check out the indentation of the line as
7968 * a whole and then add the "imaginary indent" to that.
7969 */
7970 look = skipwhite(start);
7971 if (*look == '{')
7972 {
7973 getvcol(curwin, trypos, &col, NULL, NULL);
7974 amount = col;
7975 if (*start == '{')
7976 start_brace = BRACE_IN_COL0;
7977 else
7978 start_brace = BRACE_AT_START;
7979 }
7980 else
7981 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007982 /* That opening brace might have been on a continuation
7983 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 curwin->w_cursor.lnum = ourscope;
7985
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007986 /* Position the cursor over the rightmost paren, so that
7987 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 lnum = ourscope;
7989 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007990 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7991 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 lnum = trypos->lnum;
7993
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007994 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 * case 1: if (asdf &&
7996 * ldfd) {
7997 * }
7998 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007999 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
8000 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008002 else if (curbuf->b_ind_js)
8003 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008005 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
8007 start_brace = BRACE_AT_END;
8008 }
8009
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008010 /* For Javascript check if the line starts with "key:". */
8011 if (curbuf->b_ind_js)
8012 js_cur_has_key = cin_has_js_key(theline);
8013
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008015 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 * we want to be. otherwise, add the amount of room
8017 * that an indent is supposed to be.
8018 */
8019 if (theline[0] == '}')
8020 {
8021 /*
8022 * they may want closing braces to line up with something
8023 * other than the open brace. indulge them, if so.
8024 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008025 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
8027 else
8028 {
8029 /*
8030 * If we're looking at an "else", try to find an "if"
8031 * to match it with.
8032 * If we're looking at a "while", try to find a "do"
8033 * to match it with.
8034 */
8035 lookfor = LOOKFOR_INITIAL;
8036 if (cin_iselse(theline))
8037 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008038 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 lookfor = LOOKFOR_DO;
8040 if (lookfor != LOOKFOR_INITIAL)
8041 {
8042 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008043 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {
8045 amount = get_indent(); /* XXX */
8046 goto theend;
8047 }
8048 }
8049
8050 /*
8051 * We get here if we are not on an "while-of-do" or "else" (or
8052 * failed to find a matching "if").
8053 * Search backwards for something to line up with.
8054 * First set amount for when we don't find anything.
8055 */
8056
8057 /*
8058 * if the '{' is _really_ at the left margin, use the imaginary
8059 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008060 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 */
8062
8063 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8064 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008065 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008066 lookfor_cpp_namespace = TRUE;
8067 }
8068 else if (start_brace == BRACE_AT_START &&
8069 lookfor_cpp_namespace) /* '{' is at start */
8070 {
8071
8072 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 }
8074 else
8075 {
8076 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008077 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008078 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008079
8080 l = skipwhite(ml_get_curline());
8081 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008082 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008083 else if (cin_is_cpp_extern_c(l))
8084 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 else
8087 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008088 /* Compensate for adding b_ind_open_extra later. */
8089 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 if (amount < 0)
8091 amount = 0;
8092 }
8093 }
8094
8095 lookfor_break = FALSE;
8096
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008097 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {
8099 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008100 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
8102 else if (cin_isscopedecl(theline)) /* private:, ... */
8103 {
8104 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008105 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 }
8107 else
8108 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008109 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8110 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 lookfor_break = TRUE;
8112
8113 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008114 /* b_ind_level from start of block */
8115 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 }
8117 scope_amount = amount;
8118 whilelevel = 0;
8119
8120 /*
8121 * Search backwards. If we find something we recognize, line up
8122 * with that.
8123 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008124 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 * the usual amount relative to the conditional
8126 * that opens the block.
8127 */
8128 curwin->w_cursor = cur_curpos;
8129 for (;;)
8130 {
8131 curwin->w_cursor.lnum--;
8132 curwin->w_cursor.col = 0;
8133
8134 /*
8135 * If we went all the way back to the start of our scope, line
8136 * up with it.
8137 */
8138 if (curwin->w_cursor.lnum <= ourscope)
8139 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008140 /* We reached end of scope:
8141 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008143 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 * don't add ind_continuation, otherwise it is a variable
8145 * declaration:
8146 * int x,
8147 * here; <-- add ind_continuation
8148 */
8149 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8150 {
8151 if (curwin->w_cursor.lnum == 0
8152 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008153 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008155 /* nothing found (abuse curbuf->b_ind_maxparen as
8156 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 * initialization) */
8158 if (cont_amount > 0)
8159 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008160 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 amount += ind_continuation;
8162 break;
8163 }
8164
8165 l = ml_get_curline();
8166
8167 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008168 * If we're in a comment or raw string now, skip to
8169 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008171 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 if (trypos != NULL)
8173 {
8174 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008175 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 continue;
8177 }
8178
8179 /*
8180 * Skip preprocessor directives and blank lines.
8181 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008182 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8183 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 continue;
8185
8186 if (cin_nocode(l))
8187 continue;
8188
8189 terminated = cin_isterminated(l, FALSE, TRUE);
8190
8191 /*
8192 * If we are at top level and the line looks like a
8193 * function declaration, we are done
8194 * (it's a variable declaration).
8195 */
8196 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008197 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
8199 /* if the line is terminated with another ','
8200 * it is a continued variable initialization.
8201 * don't add extra indent.
8202 * TODO: does not work, if a function
8203 * declaration is split over multiple lines:
8204 * cin_isfuncdecl returns FALSE then.
8205 */
8206 if (terminated == ',')
8207 break;
8208
8209 /* if it es a enum declaration or an assignment,
8210 * we are done.
8211 */
8212 if (terminated != ';' && cin_isinit())
8213 break;
8214
8215 /* nothing useful found */
8216 if (terminated == 0 || terminated == '{')
8217 continue;
8218 }
8219
8220 if (terminated != ';')
8221 {
8222 /* Skip parens and braces. Position the cursor
8223 * over the rightmost paren, so that matching it
8224 * will take us back to the start of the line.
8225 */ /* XXX */
8226 trypos = NULL;
8227 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008228 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008229 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
8231 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008232 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
8234 if (trypos != NULL)
8235 {
8236 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008237 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 continue;
8239 }
8240 }
8241
8242 /* it's a variable declaration, add indentation
8243 * like in
8244 * int a,
8245 * b;
8246 */
8247 if (cont_amount > 0)
8248 amount = cont_amount;
8249 else
8250 amount += ind_continuation;
8251 }
8252 else if (lookfor == LOOKFOR_UNTERM)
8253 {
8254 if (cont_amount > 0)
8255 amount = cont_amount;
8256 else
8257 amount += ind_continuation;
8258 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008259 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008260 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008261 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008262 && lookfor != LOOKFOR_CPP_BASECLASS
8263 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008264 {
8265 amount = scope_amount;
8266 if (theline[0] == '{')
8267 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008268 amount += curbuf->b_ind_open_extra;
8269 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008270 }
8271 }
8272
8273 if (lookfor_cpp_namespace)
8274 {
8275 /*
8276 * Looking for C++ namespace, need to look further
8277 * back.
8278 */
8279 if (curwin->w_cursor.lnum == ourscope)
8280 continue;
8281
8282 if (curwin->w_cursor.lnum == 0
8283 || curwin->w_cursor.lnum
8284 < ourscope - FIND_NAMESPACE_LIM)
8285 break;
8286
8287 l = ml_get_curline();
8288
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008289 /* If we're in a comment or raw string now, skip
8290 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008291 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008292 if (trypos != NULL)
8293 {
8294 curwin->w_cursor.lnum = trypos->lnum + 1;
8295 curwin->w_cursor.col = 0;
8296 continue;
8297 }
8298
8299 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008300 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8301 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008302 continue;
8303
8304 /* Finally the actual check for "namespace". */
8305 if (cin_is_cpp_namespace(l))
8306 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008307 amount += curbuf->b_ind_cpp_namespace
8308 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008309 break;
8310 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008311 else if (cin_is_cpp_extern_c(l))
8312 {
8313 amount += curbuf->b_ind_cpp_extern_c
8314 - added_to_amount;
8315 break;
8316 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008317
8318 if (cin_nocode(l))
8319 continue;
8320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
8322 break;
8323 }
8324
8325 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008326 * If we're in a comment or raw string now, skip to the start
8327 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008329 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {
8331 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008332 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 continue;
8334 }
8335
8336 l = ml_get_curline();
8337
8338 /*
8339 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008340 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008342 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 if (iscase || cin_isscopedecl(l))
8344 {
8345 /* we are only looking for cpp base class
8346 * declaration/initialization any longer */
8347 if (lookfor == LOOKFOR_CPP_BASECLASS)
8348 break;
8349
8350 /* When looking for a "do" we are not interested in
8351 * labels. */
8352 if (whilelevel > 0)
8353 continue;
8354
8355 /*
8356 * case xx:
8357 * c = 99 + <- this indent plus continuation
8358 *-> here;
8359 */
8360 if (lookfor == LOOKFOR_UNTERM
8361 || lookfor == LOOKFOR_ENUM_OR_INIT)
8362 {
8363 if (cont_amount > 0)
8364 amount = cont_amount;
8365 else
8366 amount += ind_continuation;
8367 break;
8368 }
8369
8370 /*
8371 * case xx: <- line up with this case
8372 * x = 333;
8373 * case yy:
8374 */
8375 if ( (iscase && lookfor == LOOKFOR_CASE)
8376 || (iscase && lookfor_break)
8377 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8378 {
8379 /*
8380 * Check that this case label is not for another
8381 * switch()
8382 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008383 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008384 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {
8386 amount = get_indent(); /* XXX */
8387 break;
8388 }
8389 continue;
8390 }
8391
8392 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8393
8394 /*
8395 * case xx: if (cond) <- line up with this if
8396 * y = y + 1;
8397 * -> s = 99;
8398 *
8399 * case xx:
8400 * if (cond) <- line up with this line
8401 * y = y + 1;
8402 * -> s = 99;
8403 */
8404 if (lookfor == LOOKFOR_TERM)
8405 {
8406 if (n)
8407 amount = n;
8408
8409 if (!lookfor_break)
8410 break;
8411 }
8412
8413 /*
8414 * case xx: x = x + 1; <- line up with this x
8415 * -> y = y + 1;
8416 *
8417 * case xx: if (cond) <- line up with this if
8418 * -> y = y + 1;
8419 */
8420 if (n)
8421 {
8422 amount = n;
8423 l = after_label(ml_get_curline());
8424 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008425 {
8426 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008427 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008428 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008429 amount += curbuf->b_ind_level
8430 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 break;
8433 }
8434
8435 /*
8436 * Try to get the indent of a statement before the switch
8437 * label. If nothing is found, line up relative to the
8438 * switch label.
8439 * break; <- may line up with this line
8440 * case xx:
8441 * -> y = 1;
8442 */
8443 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008444 ? curbuf->b_ind_case_code
8445 : curbuf->b_ind_scopedecl_code);
8446 lookfor = curbuf->b_ind_case_break
8447 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 continue;
8449 }
8450
8451 /*
8452 * Looking for a switch() label or C++ scope declaration,
8453 * ignore other lines, skip {}-blocks.
8454 */
8455 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8456 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008457 if (find_last_paren(l, '{', '}')
8458 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008461 curwin->w_cursor.col = 0;
8462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 continue;
8464 }
8465
8466 /*
8467 * Ignore jump labels with nothing after them.
8468 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008469 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
8471 l = after_label(ml_get_curline());
8472 if (l == NULL || cin_nocode(l))
8473 continue;
8474 }
8475
8476 /*
8477 * Ignore #defines, #if, etc.
8478 * Ignore comment and empty lines.
8479 * (need to get the line again, cin_islabel() may have
8480 * unlocked it)
8481 */
8482 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008483 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 || cin_nocode(l))
8485 continue;
8486
8487 /*
8488 * Are we at the start of a cpp base class declaration or
8489 * constructor initialization?
8490 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008491 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008492 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008493 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008494 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008495 l = ml_get_curline();
8496 }
8497 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {
8499 if (lookfor == LOOKFOR_UNTERM)
8500 {
8501 if (cont_amount > 0)
8502 amount = cont_amount;
8503 else
8504 amount += ind_continuation;
8505 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008506 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008508 /* Need to find start of the declaration. */
8509 lookfor = LOOKFOR_UNTERM;
8510 ind_continuation = 0;
8511 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 }
8513 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008514 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008515 amount = get_baseclass_amount(
8516 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 break;
8518 }
8519 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8520 {
8521 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008522 * declaration or initialization before the opening brace.
8523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 if (cin_isterminated(l, TRUE, FALSE))
8525 break;
8526 else
8527 continue;
8528 }
8529
8530 /*
8531 * What happens next depends on the line being terminated.
8532 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008533 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 * 123,
8535 * sizeof
8536 * here
8537 * Otherwise check whether it is a enumeration or structure
8538 * initialisation (not indented) or a variable declaration
8539 * (indented).
8540 */
8541 terminated = cin_isterminated(l, FALSE, TRUE);
8542
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008543 if (js_cur_has_key)
8544 {
8545 js_cur_has_key = 0; /* only check the first line */
8546 if (curbuf->b_ind_js && terminated == ',')
8547 {
8548 /* For Javascript we might be inside an object:
8549 * key: something, <- align with this
8550 * key: something
8551 * or:
8552 * key: something + <- align with this
8553 * something,
8554 * key: something
8555 */
8556 lookfor = LOOKFOR_JS_KEY;
8557 }
8558 }
8559 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8560 {
8561 amount = get_indent();
8562 break;
8563 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008564 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008565 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008566 if (tryposBrace != NULL && tryposBrace->lnum
8567 >= curwin->w_cursor.lnum)
8568 break;
8569 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008570 /* line below current line is the one that starts a
8571 * (possibly broken) line ending in a comma. */
8572 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008573 else
8574 {
8575 amount = get_indent();
8576 if (curwin->w_cursor.lnum - 1 == ourscope)
8577 /* line above is start of the scope, thus current
8578 * line is the one that stars a (possibly broken)
8579 * line ending in a comma. */
8580 break;
8581 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008582 }
8583
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8585 && terminated == ','))
8586 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008587 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8588 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008589 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 /*
8591 * if we're in the middle of a paren thing,
8592 * go back to the line that starts it so
8593 * we can get the right prevailing indent
8594 * if ( foo &&
8595 * bar )
8596 */
8597 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008598 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008600 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 */
8602 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008603 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008604 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8605 || (trypos->lnum == tryposBrace->lnum
8606 && trypos->col < tryposBrace->col)))
8607 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609 /*
8610 * If we are looking for ',', we also look for matching
8611 * braces.
8612 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008613 if (trypos == NULL && terminated == ','
8614 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008615 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617 if (trypos != NULL)
8618 {
8619 /*
8620 * Check if we are on a case label now. This is
8621 * handled above.
8622 * case xx: if ( asdf &&
8623 * asdf)
8624 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008625 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008627 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {
8629 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008630 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 continue;
8632 }
8633 }
8634
8635 /*
8636 * Skip over continuation lines to find the one to get the
8637 * indent from
8638 * char *usethis = "bla\
8639 * bla",
8640 * here;
8641 */
8642 if (terminated == ',')
8643 {
8644 while (curwin->w_cursor.lnum > 1)
8645 {
8646 l = ml_get(curwin->w_cursor.lnum - 1);
8647 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8648 break;
8649 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008650 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652 }
8653
8654 /*
8655 * Get indent and pointer to text for current line,
8656 * ignoring any jump label. XXX
8657 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008658 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008659 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008660 else
8661 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 /*
8663 * If this is just above the line we are indenting, and it
8664 * starts with a '{', line it up with this line.
8665 * while (not)
8666 * -> {
8667 * }
8668 */
8669 if (terminated != ',' && lookfor != LOOKFOR_TERM
8670 && theline[0] == '{')
8671 {
8672 amount = cur_amount;
8673 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008674 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 * doesn't start with a '{', which must have a match
8676 * in the same line (scope is the same). Probably:
8677 * { 1, 2 },
8678 * -> { 3, 4 }
8679 */
8680 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008681 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008683 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 {
8685 /* have to look back, whether it is a cpp base
8686 * class declaration or initialization */
8687 lookfor = LOOKFOR_CPP_BASECLASS;
8688 continue;
8689 }
8690 break;
8691 }
8692
8693 /*
8694 * Check if we are after an "if", "while", etc.
8695 * Also allow " } else".
8696 */
8697 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8698 {
8699 /*
8700 * Found an unterminated line after an if (), line up
8701 * with the last one.
8702 * if (cond)
8703 * 100 +
8704 * -> here;
8705 */
8706 if (lookfor == LOOKFOR_UNTERM
8707 || lookfor == LOOKFOR_ENUM_OR_INIT)
8708 {
8709 if (cont_amount > 0)
8710 amount = cont_amount;
8711 else
8712 amount += ind_continuation;
8713 break;
8714 }
8715
8716 /*
8717 * If this is just above the line we are indenting, we
8718 * are finished.
8719 * while (not)
8720 * -> here;
8721 * Otherwise this indent can be used when the line
8722 * before this is terminated.
8723 * yyy;
8724 * if (stat)
8725 * while (not)
8726 * xxx;
8727 * -> here;
8728 */
8729 amount = cur_amount;
8730 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008731 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 if (lookfor != LOOKFOR_TERM)
8733 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008734 amount += curbuf->b_ind_level
8735 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 break;
8737 }
8738
8739 /*
8740 * Special trick: when expecting the while () after a
8741 * do, line up with the while()
8742 * do
8743 * x = 1;
8744 * -> here
8745 */
8746 l = skipwhite(ml_get_curline());
8747 if (cin_isdo(l))
8748 {
8749 if (whilelevel == 0)
8750 break;
8751 --whilelevel;
8752 }
8753
8754 /*
8755 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008756 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 * Need to use the scope of this "else". XXX
8758 * If whilelevel != 0 continue looking for a "do {".
8759 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008760 if (cin_iselse(l) && whilelevel == 0)
8761 {
8762 /* If we're looking at "} else", let's make sure we
8763 * find the opening brace of the enclosing scope,
8764 * not the one from "if () {". */
8765 if (*l == '}')
8766 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008767 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008768
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008769 if ((trypos = find_start_brace()) == NULL
8770 || find_match(LOOKFOR_IF, trypos->lnum)
8771 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008772 break;
8773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 }
8775
8776 /*
8777 * If we're below an unterminated line that is not an
8778 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008779 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 * the line before this one.
8781 */
8782 else
8783 {
8784 /*
8785 * Found two unterminated lines on a row, line up with
8786 * the last one.
8787 * c = 99 +
8788 * 100 +
8789 * -> here;
8790 */
8791 if (lookfor == LOOKFOR_UNTERM)
8792 {
8793 /* When line ends in a comma add extra indent */
8794 if (terminated == ',')
8795 amount += ind_continuation;
8796 break;
8797 }
8798
8799 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8800 {
8801 /* Found two lines ending in ',', lineup with the
8802 * lowest one, but check for cpp base class
8803 * declaration/initialization, if it is an
8804 * opening brace or we are looking just for
8805 * enumerations/initializations. */
8806 if (terminated == ',')
8807 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008808 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 break;
8810
8811 lookfor = LOOKFOR_CPP_BASECLASS;
8812 continue;
8813 }
8814
8815 /* Ignore unterminated lines in between, but
8816 * reduce indent. */
8817 if (amount > cur_amount)
8818 amount = cur_amount;
8819 }
8820 else
8821 {
8822 /*
8823 * Found first unterminated line on a row, may
8824 * line up with this line, remember its indent
8825 * 100 +
8826 * -> here;
8827 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008828 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008830
8831 n = (int)STRLEN(l);
8832 if (terminated == ',' && (*skipwhite(l) == ']'
8833 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008834 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835
8836 /*
8837 * If previous line ends in ',', check whether we
8838 * are in an initialization or enum
8839 * struct xxx =
8840 * {
8841 * sizeof a,
8842 * 124 };
8843 * or a normal possible continuation line.
8844 * but only, of no other statement has been found
8845 * yet.
8846 */
8847 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8848 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008849 if (curbuf->b_ind_js)
8850 {
8851 /* Search for a line ending in a comma
8852 * and line up with the line below it
8853 * (could be the current line).
8854 * some = [
8855 * 1, <- line up here
8856 * 2,
8857 * some = [
8858 * 3 + <- line up here
8859 * 4 *
8860 * 5,
8861 * 6,
8862 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008863 if (cin_iscomment(skipwhite(l)))
8864 break;
8865 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008866 trypos = find_match_char('[',
8867 curbuf->b_ind_maxparen);
8868 if (trypos != NULL)
8869 {
8870 if (trypos->lnum
8871 == curwin->w_cursor.lnum - 1)
8872 {
8873 /* Current line is first inside
8874 * [], line up with it. */
8875 break;
8876 }
8877 ourscope = trypos->lnum;
8878 }
8879 }
8880 else
8881 {
8882 lookfor = LOOKFOR_ENUM_OR_INIT;
8883 cont_amount = cin_first_id_amount();
8884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886 else
8887 {
8888 if (lookfor == LOOKFOR_INITIAL
8889 && *l != NUL
8890 && l[STRLEN(l) - 1] == '\\')
8891 /* XXX */
8892 cont_amount = cin_get_equal_amount(
8893 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008894 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008895 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008896 && lookfor != LOOKFOR_COMMA
8897 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 lookfor = LOOKFOR_UNTERM;
8899 }
8900 }
8901 }
8902 }
8903
8904 /*
8905 * Check if we are after a while (cond);
8906 * If so: Ignore until the matching "do".
8907 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008908 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 {
8910 /*
8911 * Found an unterminated line after a while ();, line up
8912 * with the last one.
8913 * while (cond);
8914 * 100 + <- line up with this one
8915 * -> here;
8916 */
8917 if (lookfor == LOOKFOR_UNTERM
8918 || lookfor == LOOKFOR_ENUM_OR_INIT)
8919 {
8920 if (cont_amount > 0)
8921 amount = cont_amount;
8922 else
8923 amount += ind_continuation;
8924 break;
8925 }
8926
8927 if (whilelevel == 0)
8928 {
8929 lookfor = LOOKFOR_TERM;
8930 amount = get_indent(); /* XXX */
8931 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008932 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 }
8934 ++whilelevel;
8935 }
8936
8937 /*
8938 * We are after a "normal" statement.
8939 * If we had another statement we can stop now and use the
8940 * indent of that other statement.
8941 * Otherwise the indent of the current statement may be used,
8942 * search backwards for the next "normal" statement.
8943 */
8944 else
8945 {
8946 /*
8947 * Skip single break line, if before a switch label. It
8948 * may be lined up with the case label.
8949 */
8950 if (lookfor == LOOKFOR_NOBREAK
8951 && cin_isbreak(skipwhite(ml_get_curline())))
8952 {
8953 lookfor = LOOKFOR_ANY;
8954 continue;
8955 }
8956
8957 /*
8958 * Handle "do {" line.
8959 */
8960 if (whilelevel > 0)
8961 {
8962 l = cin_skipcomment(ml_get_curline());
8963 if (cin_isdo(l))
8964 {
8965 amount = get_indent(); /* XXX */
8966 --whilelevel;
8967 continue;
8968 }
8969 }
8970
8971 /*
8972 * Found a terminated line above an unterminated line. Add
8973 * the amount for a continuation line.
8974 * x = 1;
8975 * y = foo +
8976 * -> here;
8977 * or
8978 * int x = 1;
8979 * int foo,
8980 * -> here;
8981 */
8982 if (lookfor == LOOKFOR_UNTERM
8983 || lookfor == LOOKFOR_ENUM_OR_INIT)
8984 {
8985 if (cont_amount > 0)
8986 amount = cont_amount;
8987 else
8988 amount += ind_continuation;
8989 break;
8990 }
8991
8992 /*
8993 * Found a terminated line above a terminated line or "if"
8994 * etc. line. Use the amount of the line below us.
8995 * x = 1; x = 1;
8996 * if (asdf) y = 2;
8997 * while (asdf) ->here;
8998 * here;
8999 * ->foo;
9000 */
9001 if (lookfor == LOOKFOR_TERM)
9002 {
9003 if (!lookfor_break && whilelevel == 0)
9004 break;
9005 }
9006
9007 /*
9008 * First line above the one we're indenting is terminated.
9009 * To know what needs to be done look further backward for
9010 * a terminated line.
9011 */
9012 else
9013 {
9014 /*
9015 * position the cursor over the rightmost paren, so
9016 * that matching it will take us back to the start of
9017 * the line. Helps for:
9018 * func(asdr,
9019 * asdfasdf);
9020 * here;
9021 */
9022term_again:
9023 l = ml_get_curline();
9024 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009025 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009026 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 {
9028 /*
9029 * Check if we are on a case label now. This is
9030 * handled above.
9031 * case xx: if ( asdf &&
9032 * asdf)
9033 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009034 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009036 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 {
9038 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009039 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 continue;
9041 }
9042 }
9043
9044 /* When aligning with the case statement, don't align
9045 * with a statement after it.
9046 * case 1: { <-- don't use this { position
9047 * stat;
9048 * }
9049 * case 2:
9050 * stat;
9051 * }
9052 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009053 iscase = (curbuf->b_ind_keep_case_label
9054 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055
9056 /*
9057 * Get indent and pointer to text for current line,
9058 * ignoring any jump label.
9059 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009060 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061
9062 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009063 amount += curbuf->b_ind_open_extra;
9064 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009065 l = skipwhite(l);
9066 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009067 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9069
9070 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009071 * When a terminated line starts with "else" skip to
9072 * the matching "if":
9073 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009074 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009075 * Need to use the scope of this "else". XXX
9076 * If whilelevel != 0 continue looking for a "do {".
9077 */
9078 if (lookfor == LOOKFOR_TERM
9079 && *l != '}'
9080 && cin_iselse(l)
9081 && whilelevel == 0)
9082 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009083 if ((trypos = find_start_brace()) == NULL
9084 || find_match(LOOKFOR_IF, trypos->lnum)
9085 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009086 break;
9087 continue;
9088 }
9089
9090 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 * If we're at the end of a block, skip to the start of
9092 * that block.
9093 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009094 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009095 if (find_last_paren(l, '{', '}') /* XXX */
9096 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009098 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 /* if not "else {" check for terminated again */
9100 /* but skip block for "} else {" */
9101 l = cin_skipcomment(ml_get_curline());
9102 if (*l == '}' || !cin_iselse(l))
9103 goto term_again;
9104 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009105 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 }
9107 }
9108 }
9109 }
9110 }
9111 }
9112
9113 /* add extra indent for a comment */
9114 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009115 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009116
9117 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009118 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9119 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009120
9121 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009123
9124 /*
9125 * ok -- we're not inside any sort of structure at all!
9126 *
9127 * This means we're at the top level, and everything should
9128 * basically just match where the previous line is, except
9129 * for the lines immediately following a function declaration,
9130 * which are K&R-style parameters and need to be indented.
9131 *
9132 * if our line starts with an open brace, forget about any
9133 * prevailing indent and make sure it looks like the start
9134 * of a function
9135 */
9136
9137 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009139 amount = curbuf->b_ind_first_open;
9140 goto theend;
9141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009143 /*
9144 * If the NEXT line is a function declaration, the current
9145 * line needs to be indented as a function type spec.
9146 * Don't do this if the current line looks like a comment or if the
9147 * current line is terminated, ie. ends in ';', or if the current line
9148 * contains { or }: "void f() {\n if (1)"
9149 */
9150 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9151 && !cin_nocode(theline)
9152 && vim_strchr(theline, '{') == NULL
9153 && vim_strchr(theline, '}') == NULL
9154 && !cin_ends_in(theline, (char_u *)":", NULL)
9155 && !cin_ends_in(theline, (char_u *)",", NULL)
9156 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9157 cur_curpos.lnum + 1)
9158 && !cin_isterminated(theline, FALSE, TRUE))
9159 {
9160 amount = curbuf->b_ind_func_type;
9161 goto theend;
9162 }
9163
9164 /* search backwards until we find something we recognize */
9165 amount = 0;
9166 curwin->w_cursor = cur_curpos;
9167 while (curwin->w_cursor.lnum > 1)
9168 {
9169 curwin->w_cursor.lnum--;
9170 curwin->w_cursor.col = 0;
9171
9172 l = ml_get_curline();
9173
9174 /*
9175 * If we're in a comment or raw string now, skip to the start
9176 * of it.
9177 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009178 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009180 curwin->w_cursor.lnum = trypos->lnum + 1;
9181 curwin->w_cursor.col = 0;
9182 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 }
9184
9185 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009186 * Are we at the start of a cpp base class declaration or
9187 * constructor initialization?
9188 */ /* XXX */
9189 n = FALSE;
9190 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009192 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9193 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009195 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009197 /* XXX */
9198 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9199 break;
9200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009202 /*
9203 * Skip preprocessor directives and blank lines.
9204 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009205 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009206 continue;
9207
9208 if (cin_nocode(l))
9209 continue;
9210
9211 /*
9212 * If the previous line ends in ',', use one level of
9213 * indentation:
9214 * int foo,
9215 * bar;
9216 * do this before checking for '}' in case of eg.
9217 * enum foobar
9218 * {
9219 * ...
9220 * } foo,
9221 * bar;
9222 */
9223 n = 0;
9224 if (cin_ends_in(l, (char_u *)",", NULL)
9225 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9226 {
9227 /* take us back to opening paren */
9228 if (find_last_paren(l, '(', ')')
9229 && (trypos = find_match_paren(
9230 curbuf->b_ind_maxparen)) != NULL)
9231 curwin->w_cursor = *trypos;
9232
9233 /* For a line ending in ',' that is a continuation line go
9234 * back to the first line with a backslash:
9235 * char *foo = "bla\
9236 * bla",
9237 * here;
9238 */
9239 while (n == 0 && curwin->w_cursor.lnum > 1)
9240 {
9241 l = ml_get(curwin->w_cursor.lnum - 1);
9242 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9243 break;
9244 --curwin->w_cursor.lnum;
9245 curwin->w_cursor.col = 0;
9246 }
9247
9248 amount = get_indent(); /* XXX */
9249
9250 if (amount == 0)
9251 amount = cin_first_id_amount();
9252 if (amount == 0)
9253 amount = ind_continuation;
9254 break;
9255 }
9256
9257 /*
9258 * If the line looks like a function declaration, and we're
9259 * not in a comment, put it the left margin.
9260 */
9261 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9262 break;
9263 l = ml_get_curline();
9264
9265 /*
9266 * Finding the closing '}' of a previous function. Put
9267 * current line at the left margin. For when 'cino' has "fs".
9268 */
9269 if (*skipwhite(l) == '}')
9270 break;
9271
9272 /* (matching {)
9273 * If the previous line ends on '};' (maybe followed by
9274 * comments) align at column 0. For example:
9275 * char *string_array[] = { "foo",
9276 * / * x * / "b};ar" }; / * foobar * /
9277 */
9278 if (cin_ends_in(l, (char_u *)"};", NULL))
9279 break;
9280
9281 /*
9282 * If the previous line ends on '[' we are probably in an
9283 * array constant:
9284 * something = [
9285 * 234, <- extra indent
9286 */
9287 if (cin_ends_in(l, (char_u *)"[", NULL))
9288 {
9289 amount = get_indent() + ind_continuation;
9290 break;
9291 }
9292
9293 /*
9294 * Find a line only has a semicolon that belongs to a previous
9295 * line ending in '}', e.g. before an #endif. Don't increase
9296 * indent then.
9297 */
9298 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9299 {
9300 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301
9302 while (curwin->w_cursor.lnum > 1)
9303 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009304 look = ml_get(--curwin->w_cursor.lnum);
9305 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009306 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009308 }
9309 if (curwin->w_cursor.lnum > 0
9310 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009313 curwin->w_cursor = curpos_save;
9314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009316 /*
9317 * If the PREVIOUS line is a function declaration, the current
9318 * line (and the ones that follow) needs to be indented as
9319 * parameters.
9320 */
9321 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9322 {
9323 amount = curbuf->b_ind_param;
9324 break;
9325 }
9326
9327 /*
9328 * If the previous line ends in ';' and the line before the
9329 * previous line ends in ',' or '\', ident to column zero:
9330 * int foo,
9331 * bar;
9332 * indent_to_0 here;
9333 */
9334 if (cin_ends_in(l, (char_u *)";", NULL))
9335 {
9336 l = ml_get(curwin->w_cursor.lnum - 1);
9337 if (cin_ends_in(l, (char_u *)",", NULL)
9338 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9339 break;
9340 l = ml_get_curline();
9341 }
9342
9343 /*
9344 * Doesn't look like anything interesting -- so just
9345 * use the indent of this line.
9346 *
9347 * Position the cursor over the rightmost paren, so that
9348 * matching it will take us back to the start of the line.
9349 */
9350 find_last_paren(l, '(', ')');
9351
9352 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9353 curwin->w_cursor = *trypos;
9354 amount = get_indent(); /* XXX */
9355 break;
9356 }
9357
9358 /* add extra indent for a comment */
9359 if (cin_iscomment(theline))
9360 amount += curbuf->b_ind_comment;
9361
9362 /* add extra indent if the previous line ended in a backslash:
9363 * "asdfasdf\
9364 * here";
9365 * char *foo = "asdf\
9366 * here";
9367 */
9368 if (cur_curpos.lnum > 1)
9369 {
9370 l = ml_get(cur_curpos.lnum - 1);
9371 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9372 {
9373 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9374 if (cur_amount > 0)
9375 amount = cur_amount;
9376 else if (cur_amount == 0)
9377 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 }
9379 }
9380
9381theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009382 if (amount < 0)
9383 amount = 0;
9384
9385laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 /* put the cursor back where it belongs */
9387 curwin->w_cursor = cur_curpos;
9388
9389 vim_free(linecopy);
9390
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 return amount;
9392}
9393
9394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009395find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
9397 char_u *look;
9398 pos_T *theirscope;
9399 char_u *mightbeif;
9400 int elselevel;
9401 int whilelevel;
9402
9403 if (lookfor == LOOKFOR_IF)
9404 {
9405 elselevel = 1;
9406 whilelevel = 0;
9407 }
9408 else
9409 {
9410 elselevel = 0;
9411 whilelevel = 1;
9412 }
9413
9414 curwin->w_cursor.col = 0;
9415
9416 while (curwin->w_cursor.lnum > ourscope + 1)
9417 {
9418 curwin->w_cursor.lnum--;
9419 curwin->w_cursor.col = 0;
9420
9421 look = cin_skipcomment(ml_get_curline());
9422 if (cin_iselse(look)
9423 || cin_isif(look)
9424 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009425 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 {
9427 /*
9428 * if we've gone outside the braces entirely,
9429 * we must be out of scope...
9430 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009431 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 if (theirscope == NULL)
9433 break;
9434
9435 /*
9436 * and if the brace enclosing this is further
9437 * back than the one enclosing the else, we're
9438 * out of luck too.
9439 */
9440 if (theirscope->lnum < ourscope)
9441 break;
9442
9443 /*
9444 * and if they're enclosed in a *deeper* brace,
9445 * then we can ignore it because it's in a
9446 * different scope...
9447 */
9448 if (theirscope->lnum > ourscope)
9449 continue;
9450
9451 /*
9452 * if it was an "else" (that's not an "else if")
9453 * then we need to go back to another if, so
9454 * increment elselevel
9455 */
9456 look = cin_skipcomment(ml_get_curline());
9457 if (cin_iselse(look))
9458 {
9459 mightbeif = cin_skipcomment(look + 4);
9460 if (!cin_isif(mightbeif))
9461 ++elselevel;
9462 continue;
9463 }
9464
9465 /*
9466 * if it was a "while" then we need to go back to
9467 * another "do", so increment whilelevel. XXX
9468 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009469 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 {
9471 ++whilelevel;
9472 continue;
9473 }
9474
9475 /* If it's an "if" decrement elselevel */
9476 look = cin_skipcomment(ml_get_curline());
9477 if (cin_isif(look))
9478 {
9479 elselevel--;
9480 /*
9481 * When looking for an "if" ignore "while"s that
9482 * get in the way.
9483 */
9484 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9485 whilelevel = 0;
9486 }
9487
9488 /* If it's a "do" decrement whilelevel */
9489 if (cin_isdo(look))
9490 whilelevel--;
9491
9492 /*
9493 * if we've used up all the elses, then
9494 * this must be the if that we want!
9495 * match the indent level of that if.
9496 */
9497 if (elselevel <= 0 && whilelevel <= 0)
9498 {
9499 return OK;
9500 }
9501 }
9502 }
9503 return FAIL;
9504}
9505
9506# if defined(FEAT_EVAL) || defined(PROTO)
9507/*
9508 * Get indent level from 'indentexpr'.
9509 */
9510 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009511get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009513 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009514 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009515 pos_T save_pos;
9516 colnr_T save_curswant;
9517 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009519 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9520 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009522 /* Save and restore cursor position and curswant, in case it was changed
9523 * via :normal commands */
9524 save_pos = curwin->w_cursor;
9525 save_curswant = curwin->w_curswant;
9526 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009528 if (use_sandbox)
9529 ++sandbox;
9530 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009531
9532 /* Need to make a copy, the 'indentexpr' option could be changed while
9533 * evaluating it. */
9534 inde_copy = vim_strsave(curbuf->b_p_inde);
9535 if (inde_copy != NULL)
9536 {
9537 indent = (int)eval_to_number(inde_copy);
9538 vim_free(inde_copy);
9539 }
9540
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009541 if (use_sandbox)
9542 --sandbox;
9543 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
9545 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9546 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9547 * command. */
9548 save_State = State;
9549 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009550 curwin->w_cursor = save_pos;
9551 curwin->w_curswant = save_curswant;
9552 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 check_cursor();
9554 State = save_State;
9555
9556 /* If there is an error, just keep the current indent. */
9557 if (indent < 0)
9558 indent = get_indent();
9559
9560 return indent;
9561}
9562# endif
9563
9564#endif /* FEAT_CINDENT */
9565
9566#if defined(FEAT_LISP) || defined(PROTO)
9567
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009568static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569
9570 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009571lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 char_u buf[LSIZE];
9574 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009575 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576
9577 while (*word != NUL)
9578 {
9579 (void)copy_option_part(&word, buf, LSIZE, ",");
9580 len = (int)STRLEN(buf);
9581 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9582 return TRUE;
9583 }
9584 return FALSE;
9585}
9586
9587/*
9588 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9589 * The incompatible newer method is quite a bit better at indenting
9590 * code in lisp-like languages than the traditional one; it's still
9591 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9592 *
9593 * TODO:
9594 * Findmatch() should be adapted for lisp, also to make showmatch
9595 * work correctly: now (v5.3) it seems all C/C++ oriented:
9596 * - it does not recognize the #\( and #\) notations as character literals
9597 * - it doesn't know about comments starting with a semicolon
9598 * - it incorrectly interprets '(' as a character literal
9599 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009600 * Update from Sergey Khorev:
9601 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 */
9603 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009604get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009606 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 int amount;
9608 char_u *that;
9609 colnr_T col;
9610 colnr_T firsttry;
9611 int parencount, quotecount;
9612 int vi_lisp;
9613
9614 /* Set vi_lisp to use the vi-compatible method */
9615 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9616
9617 realpos = curwin->w_cursor;
9618 curwin->w_cursor.col = 0;
9619
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009620 if ((pos = findmatch(NULL, '(')) == NULL)
9621 pos = findmatch(NULL, '[');
9622 else
9623 {
9624 paren = *pos;
9625 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009626 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009627 pos = &paren;
9628 }
9629 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 {
9631 /* Extra trick: Take the indent of the first previous non-white
9632 * line that is at the same () level. */
9633 amount = -1;
9634 parencount = 0;
9635
9636 while (--curwin->w_cursor.lnum >= pos->lnum)
9637 {
9638 if (linewhite(curwin->w_cursor.lnum))
9639 continue;
9640 for (that = ml_get_curline(); *that != NUL; ++that)
9641 {
9642 if (*that == ';')
9643 {
9644 while (*(that + 1) != NUL)
9645 ++that;
9646 continue;
9647 }
9648 if (*that == '\\')
9649 {
9650 if (*(that + 1) != NUL)
9651 ++that;
9652 continue;
9653 }
9654 if (*that == '"' && *(that + 1) != NUL)
9655 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009656 while (*++that && *that != '"')
9657 {
9658 /* skipping escaped characters in the string */
9659 if (*that == '\\')
9660 {
9661 if (*++that == NUL)
9662 break;
9663 if (that[1] == NUL)
9664 {
9665 ++that;
9666 break;
9667 }
9668 }
9669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009671 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009673 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 --parencount;
9675 }
9676 if (parencount == 0)
9677 {
9678 amount = get_indent();
9679 break;
9680 }
9681 }
9682
9683 if (amount == -1)
9684 {
9685 curwin->w_cursor.lnum = pos->lnum;
9686 curwin->w_cursor.col = pos->col;
9687 col = pos->col;
9688
9689 that = ml_get_curline();
9690
9691 if (vi_lisp && get_indent() == 0)
9692 amount = 2;
9693 else
9694 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009695 char_u *line = that;
9696
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 amount = 0;
9698 while (*that && col)
9699 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009700 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 col--;
9702 }
9703
9704 /*
9705 * Some keywords require "body" indenting rules (the
9706 * non-standard-lisp ones are Scheme special forms):
9707 *
9708 * (let ((a 1)) instead (let ((a 1))
9709 * (...)) of (...))
9710 */
9711
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009712 if (!vi_lisp && (*that == '(' || *that == '[')
9713 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 amount += 2;
9715 else
9716 {
9717 that++;
9718 amount++;
9719 firsttry = amount;
9720
Bram Moolenaar1c465442017-03-12 20:10:05 +01009721 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009723 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 ++that;
9725 }
9726
9727 if (*that && *that != ';') /* not a comment line */
9728 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009729 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009731 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 firsttry++;
9733
9734 parencount = 0;
9735 quotecount = 0;
9736
9737 if (vi_lisp
9738 || (*that != '"'
9739 && *that != '\''
9740 && *that != '#'
9741 && (*that < '0' || *that > '9')))
9742 {
9743 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009744 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 || quotecount
9746 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009747 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 && !quotecount
9749 && !parencount
9750 && vi_lisp)))
9751 {
9752 if (*that == '"')
9753 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009754 if ((*that == '(' || *that == '[')
9755 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009757 if ((*that == ')' || *that == ']')
9758 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 --parencount;
9760 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009761 amount += lbr_chartabsize_adv(
9762 line, &that, (colnr_T)amount);
9763 amount += lbr_chartabsize_adv(
9764 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 }
9766 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009767 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009769 amount += lbr_chartabsize(
9770 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 that++;
9772 }
9773 if (!*that || *that == ';')
9774 amount = firsttry;
9775 }
9776 }
9777 }
9778 }
9779 }
9780 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009781 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782
9783 curwin->w_cursor = realpos;
9784
9785 return amount;
9786}
9787#endif /* FEAT_LISP */
9788
9789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009790prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009792#if defined(SIGHUP) && defined(SIG_IGN)
9793 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9794 * makes Vim exit and then handling SIGHUP causes various reentrance
9795 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009796 signal(SIGHUP, SIG_IGN);
9797#endif
9798
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799#ifdef FEAT_GUI
9800 if (gui.in_use)
9801 {
9802 gui.dying = TRUE;
9803 out_trash(); /* trash any pending output */
9804 }
9805 else
9806#endif
9807 {
9808 windgoto((int)Rows - 1, 0);
9809
9810 /*
9811 * Switch terminal mode back now, so messages end up on the "normal"
9812 * screen (if there are two screens).
9813 */
9814 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009815 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 out_flush();
9817 }
9818}
9819
9820/*
9821 * Preserve files and exit.
9822 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009823 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9824 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 */
9826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009827preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828{
9829 buf_T *buf;
9830
9831 prepare_to_exit();
9832
Bram Moolenaar4770d092006-01-12 23:22:24 +00009833 /* Setting this will prevent free() calls. That avoids calling free()
9834 * recursively when free() was invoked with a bad pointer. */
9835 really_exiting = TRUE;
9836
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 out_str(IObuff);
9838 screen_start(); /* don't know where cursor is now */
9839 out_flush();
9840
9841 ml_close_notmod(); /* close all not-modified buffers */
9842
Bram Moolenaar29323592016-07-24 22:04:11 +02009843 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 {
9845 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9846 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009847 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 screen_start(); /* don't know where cursor is now */
9849 out_flush();
9850 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9851 break;
9852 }
9853 }
9854
9855 ml_close_all(FALSE); /* close all memfiles, without deleting */
9856
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009857 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858
9859 getout(1);
9860}
9861
9862/*
9863 * return TRUE if "fname" exists.
9864 */
9865 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009866vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009868 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
9870 if (mch_stat((char *)fname, &st))
9871 return FALSE;
9872 return TRUE;
9873}
9874
9875/*
9876 * Check for CTRL-C pressed, but only once in a while.
9877 * Should be used instead of ui_breakcheck() for functions that check for
9878 * each line in the file. Calling ui_breakcheck() each time takes too much
9879 * time, because it can be a system call.
9880 */
9881
9882#ifndef BREAKCHECK_SKIP
9883# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9884# define BREAKCHECK_SKIP 200
9885# else
9886# define BREAKCHECK_SKIP 32
9887# endif
9888#endif
9889
9890static int breakcheck_count = 0;
9891
9892 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009893line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895 if (++breakcheck_count >= BREAKCHECK_SKIP)
9896 {
9897 breakcheck_count = 0;
9898 ui_breakcheck();
9899 }
9900}
9901
9902/*
9903 * Like line_breakcheck() but check 10 times less often.
9904 */
9905 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009906fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907{
9908 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9909 {
9910 breakcheck_count = 0;
9911 ui_breakcheck();
9912 }
9913}
9914
9915/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009916 * Invoke expand_wildcards() for one pattern.
9917 * Expand items like "%:h" before the expansion.
9918 * Returns OK or FAIL.
9919 */
9920 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009921expand_wildcards_eval(
9922 char_u **pat, /* pointer to input pattern */
9923 int *num_file, /* resulting number of files */
9924 char_u ***file, /* array of resulting files */
9925 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009926{
9927 int ret = FAIL;
9928 char_u *eval_pat = NULL;
9929 char_u *exp_pat = *pat;
9930 char_u *ignored_msg;
9931 int usedlen;
9932
9933 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9934 {
9935 ++emsg_off;
9936 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9937 NULL, &ignored_msg, NULL);
9938 --emsg_off;
9939 if (eval_pat != NULL)
9940 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9941 }
9942
9943 if (exp_pat != NULL)
9944 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9945
9946 if (eval_pat != NULL)
9947 {
9948 vim_free(exp_pat);
9949 vim_free(eval_pat);
9950 }
9951
9952 return ret;
9953}
9954
9955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9957 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009958 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 */
9960 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009961expand_wildcards(
9962 int num_pat, /* number of input patterns */
9963 char_u **pat, /* array of input patterns */
9964 int *num_files, /* resulting number of files */
9965 char_u ***files, /* array of resulting files */
9966 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
9968 int retval;
9969 int i, j;
9970 char_u *p;
9971 int non_suf_match; /* number without matching suffix */
9972
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009973 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974
9975 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009976 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 return retval;
9978
9979#ifdef FEAT_WILDIGN
9980 /*
9981 * Remove names that match 'wildignore'.
9982 */
9983 if (*p_wig)
9984 {
9985 char_u *ffname;
9986
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009987 /* check all files in (*files)[] */
9988 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009990 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 if (ffname == NULL) /* out of memory */
9992 break;
9993# ifdef VMS
9994 vms_remove_version(ffname);
9995# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009996 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009998 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009999 vim_free((*files)[i]);
10000 for (j = i; j + 1 < *num_files; ++j)
10001 (*files)[j] = (*files)[j + 1];
10002 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 --i;
10004 }
10005 vim_free(ffname);
10006 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010007
10008 /* If the number of matches is now zero, we fail. */
10009 if (*num_files == 0)
10010 {
Bram Moolenaard23a8232018-02-10 18:45:26 +010010011 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010012 return FAIL;
10013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 }
10015#endif
10016
10017 /*
10018 * Move the names where 'suffixes' match to the end.
10019 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010020 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 {
10022 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010023 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010025 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 {
10027 /*
10028 * Move the name without matching suffix to the front
10029 * of the list.
10030 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010031 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010033 (*files)[j] = (*files)[j - 1];
10034 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 }
10036 }
10037 }
10038
10039 return retval;
10040}
10041
10042/*
10043 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10044 */
10045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010046match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047{
10048 int fnamelen, setsuflen;
10049 char_u *setsuf;
10050#define MAXSUFLEN 30 /* maximum length of a file suffix */
10051 char_u suf_buf[MAXSUFLEN];
10052
10053 fnamelen = (int)STRLEN(fname);
10054 setsuflen = 0;
10055 for (setsuf = p_su; *setsuf; )
10056 {
10057 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010058 if (setsuflen == 0)
10059 {
10060 char_u *tail = gettail(fname);
10061
10062 /* empty entry: match name without a '.' */
10063 if (vim_strchr(tail, '.') == NULL)
10064 {
10065 setsuflen = 1;
10066 break;
10067 }
10068 }
10069 else
10070 {
10071 if (fnamelen >= setsuflen
10072 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10073 (size_t)setsuflen) == 0)
10074 break;
10075 setsuflen = 0;
10076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 }
10078 return (setsuflen != 0);
10079}
10080
10081#if !defined(NO_EXPANDPATH) || defined(PROTO)
10082
10083# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010084static int vim_backtick(char_u *p);
10085static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086# endif
10087
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010088# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089/*
10090 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10091 * it's shared between these systems.
10092 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010093# if defined(PROTO)
10094# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095# else
10096# ifdef __BORLANDC__
10097# define _cdecl _RTLENTRYF
10098# endif
10099# endif
10100
10101/*
10102 * comparison function for qsort in dos_expandpath()
10103 */
10104 static int _cdecl
10105pstrcmp(const void *a, const void *b)
10106{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010107 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108}
10109
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010111 * Recursively expand one path component into all matching files and/or
10112 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 * Return the number of matches found.
10114 * "path" has backslashes before chars that are not to be expanded, starting
10115 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010116 * Return the number of matches found.
10117 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 */
10119 static int
10120dos_expandpath(
10121 garray_T *gap,
10122 char_u *path,
10123 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010124 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010125 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010127 char_u *buf;
10128 char_u *path_end;
10129 char_u *p, *s, *e;
10130 int start_len = gap->ga_len;
10131 char_u *pat;
10132 regmatch_T regmatch;
10133 int starts_with_dot;
10134 int matches;
10135 int len;
10136 int starstar = FALSE;
10137 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 WIN32_FIND_DATA fb;
10139 HANDLE hFind = (HANDLE)0;
10140# ifdef FEAT_MBYTE
10141 WIN32_FIND_DATAW wfb;
10142 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010145 int ok;
10146
10147 /* Expanding "**" may take a long time, check for CTRL-C. */
10148 if (stardepth > 0)
10149 {
10150 ui_breakcheck();
10151 if (got_int)
10152 return 0;
10153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010155 /* Make room for file name. When doing encoding conversion the actual
10156 * length may be quite a bit longer, thus use the maximum possible length. */
10157 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 if (buf == NULL)
10159 return 0;
10160
10161 /*
10162 * Find the first part in the path name that contains a wildcard or a ~1.
10163 * Copy it into buf, including the preceding characters.
10164 */
10165 p = buf;
10166 s = buf;
10167 e = NULL;
10168 path_end = path;
10169 while (*path_end != NUL)
10170 {
10171 /* May ignore a wildcard that has a backslash before it; it will
10172 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10173 if (path_end >= path + wildoff && rem_backslash(path_end))
10174 *p++ = *path_end++;
10175 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10176 {
10177 if (e != NULL)
10178 break;
10179 s = p + 1;
10180 }
10181 else if (path_end >= path + wildoff
10182 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10183 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010184# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 if (has_mbyte)
10186 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010187 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 STRNCPY(p, path_end, len);
10189 p += len;
10190 path_end += len;
10191 }
10192 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 *p++ = *path_end++;
10195 }
10196 e = p;
10197 *e = NUL;
10198
10199 /* now we have one wildcard component between s and e */
10200 /* Remove backslashes between "wildoff" and the start of the wildcard
10201 * component. */
10202 for (p = buf + wildoff; p < s; ++p)
10203 if (rem_backslash(p))
10204 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010205 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 --e;
10207 --s;
10208 }
10209
Bram Moolenaar231334e2005-07-25 20:46:57 +000010210 /* Check for "**" between "s" and "e". */
10211 for (p = s; p < e; ++p)
10212 if (p[0] == '*' && p[1] == '*')
10213 starstar = TRUE;
10214
Bram Moolenaard82103e2016-01-17 17:04:05 +010010215 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10217 if (pat == NULL)
10218 {
10219 vim_free(buf);
10220 return 0;
10221 }
10222
10223 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010224 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010225 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 regmatch.rm_ic = TRUE; /* Always ignore case */
10227 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010228 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010229 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 vim_free(pat);
10231
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010232 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 {
10234 vim_free(buf);
10235 return 0;
10236 }
10237
10238 /* remember the pattern or file name being looked for */
10239 matchname = vim_strsave(s);
10240
Bram Moolenaar231334e2005-07-25 20:46:57 +000010241 /* If "**" is by itself, this is the first time we encounter it and more
10242 * is following then find matches without any directory. */
10243 if (!didstar && stardepth < 100 && starstar && e - s == 2
10244 && *path_end == '/')
10245 {
10246 STRCPY(s, path_end + 1);
10247 ++stardepth;
10248 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10249 --stardepth;
10250 }
10251
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 /* Scan all files in the directory with "dir/ *.*" */
10253 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254# ifdef FEAT_MBYTE
10255 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10256 {
10257 /* The active codepage differs from 'encoding'. Attempt using the
10258 * wide function. If it fails because it is not implemented fall back
10259 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010260 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 if (wn != NULL)
10262 {
10263 hFind = FindFirstFileW(wn, &wfb);
10264 if (hFind == INVALID_HANDLE_VALUE
10265 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010266 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 }
10268 }
10269
10270 if (wn == NULL)
10271# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010272 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274
10275 while (ok)
10276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277# ifdef FEAT_MBYTE
10278 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010279 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 else
10281# endif
10282 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 /* Ignore entries starting with a dot, unless when asked for. Accept
10284 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010285 if ((p[0] != '.' || starts_with_dot
10286 || ((flags & EW_DODOT)
10287 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010289 || (regmatch.regprog != NULL
10290 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010291 || ((flags & EW_NOTWILD)
10292 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010296
10297 if (starstar && stardepth < 100)
10298 {
10299 /* For "**" in the pattern first go deeper in the tree to
10300 * find matches. */
10301 STRCPY(buf + len, "/**");
10302 STRCPY(buf + len + 3, path_end);
10303 ++stardepth;
10304 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10305 --stardepth;
10306 }
10307
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 STRCPY(buf + len, path_end);
10309 if (mch_has_exp_wildcard(path_end))
10310 {
10311 /* need to expand another component of the path */
10312 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010313 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 }
10315 else
10316 {
10317 /* no more wildcards, check if there is a match */
10318 /* remove backslashes for the remaining components only */
10319 if (*path_end != 0)
10320 backslash_halve(buf + len + 1);
10321 if (mch_getperm(buf) >= 0) /* add existing file */
10322 addfile(gap, buf, flags);
10323 }
10324 }
10325
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326# ifdef FEAT_MBYTE
10327 if (wn != NULL)
10328 {
10329 vim_free(p);
10330 ok = FindNextFileW(hFind, &wfb);
10331 }
10332 else
10333# endif
10334 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335
10336 /* If no more matches and no match was used, try expanding the name
10337 * itself. Finds the long name of a short filename. */
10338 if (!ok && matchname != NULL && gap->ga_len == start_len)
10339 {
10340 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 FindClose(hFind);
10342# ifdef FEAT_MBYTE
10343 if (wn != NULL)
10344 {
10345 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010346 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 if (wn != NULL)
10348 hFind = FindFirstFileW(wn, &wfb);
10349 }
10350 if (wn == NULL)
10351# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010352 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010354 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 }
10356 }
10357
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 FindClose(hFind);
10359# ifdef FEAT_MBYTE
10360 vim_free(wn);
10361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010363 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 vim_free(matchname);
10365
10366 matches = gap->ga_len - start_len;
10367 if (matches > 0)
10368 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10369 sizeof(char_u *), pstrcmp);
10370 return matches;
10371}
10372
10373 int
10374mch_expandpath(
10375 garray_T *gap,
10376 char_u *path,
10377 int flags) /* EW_* flags */
10378{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010379 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010381# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382
Bram Moolenaar231334e2005-07-25 20:46:57 +000010383#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10384 || defined(PROTO)
10385/*
10386 * Unix style wildcard expansion code.
10387 * It's here because it's used both for Unix and Mac.
10388 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010389static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010390
10391 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010392pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010393{
10394 return (pathcmp(*(char **)a, *(char **)b, -1));
10395}
10396
10397/*
10398 * Recursively expand one path component into all matching files and/or
10399 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10400 * "path" has backslashes before chars that are not to be expanded, starting
10401 * at "path + wildoff".
10402 * Return the number of matches found.
10403 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10404 */
10405 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010406unix_expandpath(
10407 garray_T *gap,
10408 char_u *path,
10409 int wildoff,
10410 int flags, /* EW_* flags */
10411 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010412{
10413 char_u *buf;
10414 char_u *path_end;
10415 char_u *p, *s, *e;
10416 int start_len = gap->ga_len;
10417 char_u *pat;
10418 regmatch_T regmatch;
10419 int starts_with_dot;
10420 int matches;
10421 int len;
10422 int starstar = FALSE;
10423 static int stardepth = 0; /* depth for "**" expansion */
10424
10425 DIR *dirp;
10426 struct dirent *dp;
10427
10428 /* Expanding "**" may take a long time, check for CTRL-C. */
10429 if (stardepth > 0)
10430 {
10431 ui_breakcheck();
10432 if (got_int)
10433 return 0;
10434 }
10435
10436 /* make room for file name */
10437 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10438 if (buf == NULL)
10439 return 0;
10440
10441 /*
10442 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010443 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010444 * Copy it into "buf", including the preceding characters.
10445 */
10446 p = buf;
10447 s = buf;
10448 e = NULL;
10449 path_end = path;
10450 while (*path_end != NUL)
10451 {
10452 /* May ignore a wildcard that has a backslash before it; it will
10453 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10454 if (path_end >= path + wildoff && rem_backslash(path_end))
10455 *p++ = *path_end++;
10456 else if (*path_end == '/')
10457 {
10458 if (e != NULL)
10459 break;
10460 s = p + 1;
10461 }
10462 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010463 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010464 || (!p_fic && (flags & EW_ICASE)
10465 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010466 e = p;
10467#ifdef FEAT_MBYTE
10468 if (has_mbyte)
10469 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010470 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010471 STRNCPY(p, path_end, len);
10472 p += len;
10473 path_end += len;
10474 }
10475 else
10476#endif
10477 *p++ = *path_end++;
10478 }
10479 e = p;
10480 *e = NUL;
10481
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010482 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010483 /* Remove backslashes between "wildoff" and the start of the wildcard
10484 * component. */
10485 for (p = buf + wildoff; p < s; ++p)
10486 if (rem_backslash(p))
10487 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010488 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010489 --e;
10490 --s;
10491 }
10492
10493 /* Check for "**" between "s" and "e". */
10494 for (p = s; p < e; ++p)
10495 if (p[0] == '*' && p[1] == '*')
10496 starstar = TRUE;
10497
10498 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010499 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010500 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10501 if (pat == NULL)
10502 {
10503 vim_free(buf);
10504 return 0;
10505 }
10506
10507 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010508 if (flags & EW_ICASE)
10509 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10510 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010511 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010512 if (flags & (EW_NOERROR | EW_NOTWILD))
10513 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010514 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010515 if (flags & (EW_NOERROR | EW_NOTWILD))
10516 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010517 vim_free(pat);
10518
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010519 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010520 {
10521 vim_free(buf);
10522 return 0;
10523 }
10524
10525 /* If "**" is by itself, this is the first time we encounter it and more
10526 * is following then find matches without any directory. */
10527 if (!didstar && stardepth < 100 && starstar && e - s == 2
10528 && *path_end == '/')
10529 {
10530 STRCPY(s, path_end + 1);
10531 ++stardepth;
10532 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10533 --stardepth;
10534 }
10535
10536 /* open the directory for scanning */
10537 *s = NUL;
10538 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10539
10540 /* Find all matching entries */
10541 if (dirp != NULL)
10542 {
10543 for (;;)
10544 {
10545 dp = readdir(dirp);
10546 if (dp == NULL)
10547 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010548 if ((dp->d_name[0] != '.' || starts_with_dot
10549 || ((flags & EW_DODOT)
10550 && dp->d_name[1] != NUL
10551 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010552 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10553 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010554 || ((flags & EW_NOTWILD)
10555 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010556 {
10557 STRCPY(s, dp->d_name);
10558 len = STRLEN(buf);
10559
10560 if (starstar && stardepth < 100)
10561 {
10562 /* For "**" in the pattern first go deeper in the tree to
10563 * find matches. */
10564 STRCPY(buf + len, "/**");
10565 STRCPY(buf + len + 3, path_end);
10566 ++stardepth;
10567 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10568 --stardepth;
10569 }
10570
10571 STRCPY(buf + len, path_end);
10572 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10573 {
10574 /* need to expand another component of the path */
10575 /* remove backslashes for the remaining components only */
10576 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10577 }
10578 else
10579 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010580 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010581
Bram Moolenaar231334e2005-07-25 20:46:57 +000010582 /* no more wildcards, check if there is a match */
10583 /* remove backslashes for the remaining components only */
10584 if (*path_end != NUL)
10585 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010586 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010587 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010588 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010589 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010590#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010591 size_t precomp_len = STRLEN(buf)+1;
10592 char_u *precomp_buf =
10593 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010594
Bram Moolenaar231334e2005-07-25 20:46:57 +000010595 if (precomp_buf)
10596 {
10597 mch_memmove(buf, precomp_buf, precomp_len);
10598 vim_free(precomp_buf);
10599 }
10600#endif
10601 addfile(gap, buf, flags);
10602 }
10603 }
10604 }
10605 }
10606
10607 closedir(dirp);
10608 }
10609
10610 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010611 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010612
10613 matches = gap->ga_len - start_len;
10614 if (matches > 0)
10615 qsort(((char_u **)gap->ga_data) + start_len, matches,
10616 sizeof(char_u *), pstrcmp);
10617 return matches;
10618}
10619#endif
10620
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010621#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010622static int find_previous_pathsep(char_u *path, char_u **psep);
10623static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10624static void expand_path_option(char_u *curdir, garray_T *gap);
10625static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10626static void uniquefy_paths(garray_T *gap, char_u *pattern);
10627static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010628
10629/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010630 * Moves "*psep" back to the previous path separator in "path".
10631 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010632 */
10633 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010634find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010635{
10636 /* skip the current separator */
10637 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010638 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010639
10640 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010641 while (*psep > path)
10642 {
10643 if (vim_ispathsep(**psep))
10644 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010645 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010646 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010647
10648 return FAIL;
10649}
10650
10651/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010652 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10653 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010654 */
10655 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010656is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010657{
10658 int j;
10659 int candidate_len;
10660 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010661 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010662 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010663
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010664 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010665 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010666 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010667 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010668
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010669 candidate_len = (int)STRLEN(maybe_unique);
10670 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010671 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010672 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010673
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010674 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010675 if (fnamecmp(maybe_unique, rival) == 0
10676 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010677 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010678 }
10679
Bram Moolenaar162bd912010-07-28 22:29:10 +020010680 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010681}
10682
10683/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010684 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010685 * paths are expanded to their equivalent fullpath. This includes the "."
10686 * (relative to current buffer directory) and empty path (relative to current
10687 * directory) notations.
10688 *
10689 * TODO: handle upward search (;) and path limiter (**N) notations by
10690 * expanding each into their equivalent path(s).
10691 */
10692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010693expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010694{
10695 char_u *path_option = *curbuf->b_p_path == NUL
10696 ? p_path : curbuf->b_p_path;
10697 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010698 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010699 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010700
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010701 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010702 return;
10703
10704 while (*path_option != NUL)
10705 {
10706 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10707
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010708 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010709 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010710 /* Relative to current buffer:
10711 * "/path/file" + "." -> "/path/"
10712 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010713 if (curbuf->b_ffname == NULL)
10714 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010715 p = gettail(curbuf->b_ffname);
10716 len = (int)(p - curbuf->b_ffname);
10717 if (len + (int)STRLEN(buf) >= MAXPATHL)
10718 continue;
10719 if (buf[1] == NUL)
10720 buf[len] = NUL;
10721 else
10722 STRMOVE(buf + len, buf + 2);
10723 mch_memmove(buf, curbuf->b_ffname, len);
10724 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010725 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010726 else if (buf[0] == NUL)
10727 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010728 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010729 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010730 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010731 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010732 else if (!mch_isFullName(buf))
10733 {
10734 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010735 len = (int)STRLEN(curdir);
10736 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010737 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010738 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010739 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010740 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010741 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010742 }
10743
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010744 if (ga_grow(gap, 1) == FAIL)
10745 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010746
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010747# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010748 /* Avoid the path ending in a backslash, it fails when a comma is
10749 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010750 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010751 if (buf[len - 1] == '\\')
10752 buf[len - 1] = '/';
10753# endif
10754
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010755 p = vim_strsave(buf);
10756 if (p == NULL)
10757 break;
10758 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010759 }
10760
10761 vim_free(buf);
10762}
10763
10764/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010765 * Returns a pointer to the file or directory name in "fname" that matches the
10766 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010767 *
10768 * path: /foo/bar/baz
10769 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010770 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010771 */
10772 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010773get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010774{
10775 int i;
10776 int maxlen = 0;
10777 char_u **path_part = (char_u **)gap->ga_data;
10778 char_u *cutoff = NULL;
10779
10780 for (i = 0; i < gap->ga_len; i++)
10781 {
10782 int j = 0;
10783
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010784 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010785# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010786 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10787#endif
10788 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010789 j++;
10790 if (j > maxlen)
10791 {
10792 maxlen = j;
10793 cutoff = &fname[j];
10794 }
10795 }
10796
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010797 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010798 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010799 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010800 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010801
10802 return cutoff;
10803}
10804
10805/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010806 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10807 * that they are unique with respect to each other while conserving the part
10808 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010809 */
10810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010811uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010812{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010813 int i;
10814 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010815 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010816 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010817 char_u *pat;
10818 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010819 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010820 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010821 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010822 char_u **in_curdir = NULL;
10823 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010824
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010825 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010826 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010827
10828 /*
10829 * We need to prepend a '*' at the beginning of file_pattern so that the
10830 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010831 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010832 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010833 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010834 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010835 if (file_pattern == NULL)
10836 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010837 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010838 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010839 STRCAT(file_pattern, pattern);
10840 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10841 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010842 if (pat == NULL)
10843 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010844
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010845 regmatch.rm_ic = TRUE; /* always ignore case */
10846 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10847 vim_free(pat);
10848 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010849 return;
10850
Bram Moolenaar162bd912010-07-28 22:29:10 +020010851 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010852 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010853 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010854 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010855
10856 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010857 if (in_curdir == NULL)
10858 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010859
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010860 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010861 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010862 char_u *path = fnames[i];
10863 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010864 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010865 char_u *pathsep_p;
10866 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010867
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010868 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010869 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010870 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010871 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010872 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010873
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010874 /* Shorten the filename while maintaining its uniqueness */
10875 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010876
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010877 /* Don't assume all files can be reached without path when search
10878 * pattern starts with star star slash, so only remove path_cutoff
10879 * when possible. */
10880 if (pattern[0] == '*' && pattern[1] == '*'
10881 && vim_ispathsep_nocolon(pattern[2])
10882 && path_cutoff != NULL
10883 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10884 && is_unique(path_cutoff, gap, i))
10885 {
10886 sort_again = TRUE;
10887 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10888 }
10889 else
10890 {
10891 /* Here all files can be reached without path, so get shortest
10892 * unique path. We start at the end of the path. */
10893 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010894
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010895 while (find_previous_pathsep(path, &pathsep_p))
10896 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10897 && is_unique(pathsep_p + 1, gap, i)
10898 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10899 {
10900 sort_again = TRUE;
10901 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10902 break;
10903 }
10904 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010905
10906 if (mch_isFullName(path))
10907 {
10908 /*
10909 * Last resort: shorten relative to curdir if possible.
10910 * 'possible' means:
10911 * 1. It is under the current directory.
10912 * 2. The result is actually shorter than the original.
10913 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010914 * Before curdir After
10915 * /foo/bar/file.txt /foo/bar ./file.txt
10916 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10917 * /file.txt / /file.txt
10918 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010919 */
10920 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010921 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010922#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010923 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010924 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010925 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010926 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010927 && !vim_ispathsep(*short_name)
10928#endif
10929 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010930 {
10931 STRCPY(path, ".");
10932 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010933 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010934 }
10935 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010936 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010937 }
10938
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010939 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010940 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010941 {
10942 char_u *rel_path;
10943 char_u *path = in_curdir[i];
10944
10945 if (path == NULL)
10946 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010947
10948 /* If the {filename} is not unique, change it to ./{filename}.
10949 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010950 short_name = shorten_fname(path, curdir);
10951 if (short_name == NULL)
10952 short_name = path;
10953 if (is_unique(short_name, gap, i))
10954 {
10955 STRCPY(fnames[i], short_name);
10956 continue;
10957 }
10958
10959 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10960 if (rel_path == NULL)
10961 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010962 STRCPY(rel_path, ".");
10963 add_pathsep(rel_path);
10964 STRCAT(rel_path, short_name);
10965
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010966 vim_free(fnames[i]);
10967 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010968 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010969 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010970 }
10971
Bram Moolenaar162bd912010-07-28 22:29:10 +020010972theend:
10973 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010974 if (in_curdir != NULL)
10975 {
10976 for (i = 0; i < gap->ga_len; i++)
10977 vim_free(in_curdir[i]);
10978 vim_free(in_curdir);
10979 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010980 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010981 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010982
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010983 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010984 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010985}
10986
10987/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010988 * Calls globpath() with 'path' values for the given pattern and stores the
10989 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010990 * Returns the total number of matches.
10991 */
10992 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010993expand_in_path(
10994 garray_T *gap,
10995 char_u *pattern,
10996 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010997{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010998 char_u *curdir;
10999 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011000 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011001 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011002
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011003 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020011004 return 0;
11005 mch_dirname(curdir, MAXPATHL);
11006
Bram Moolenaar0be992e2010-08-12 21:50:51 +020011007 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011008 expand_path_option(curdir, &path_ga);
11009 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020011010 if (path_ga.ga_len == 0)
11011 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011012
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020011013 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011014 ga_clear_strings(&path_ga);
11015 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011016 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020011017
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011018 if (flags & EW_ICASE)
11019 glob_flags |= WILD_ICASE;
11020 if (flags & EW_ADDSLASH)
11021 glob_flags |= WILD_ADD_SLASH;
11022 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011023 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011024
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011025 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011026}
11027#endif
11028
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011029#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11030/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011031 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11032 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011033 */
11034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011035remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011036{
11037 int i;
11038 int j;
11039 char_u **fnames = (char_u **)gap->ga_data;
11040
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011041 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011042 for (i = gap->ga_len - 1; i > 0; --i)
11043 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11044 {
11045 vim_free(fnames[i]);
11046 for (j = i + 1; j < gap->ga_len; ++j)
11047 fnames[j - 1] = fnames[j];
11048 --gap->ga_len;
11049 }
11050}
11051#endif
11052
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010011053static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011054
11055/*
11056 * Return TRUE if "p" contains what looks like an environment variable.
11057 * Allowing for escaping.
11058 */
11059 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011060has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011061{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011062 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011063 {
11064 if (*p == '\\' && p[1] != NUL)
11065 ++p;
11066 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011067#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011068 "$%"
11069#else
11070 "$"
11071#endif
11072 , *p) != NULL)
11073 return TRUE;
11074 }
11075 return FALSE;
11076}
11077
11078#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010011079static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011080
11081/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011082 * Return TRUE if "p" contains a special wildcard character, one that Vim
11083 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011084 */
11085 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011086has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011087{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011088 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011089 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011090 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011091 if (*p == '\\' && p[1] != NUL)
11092 ++p;
11093 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11094 return TRUE;
11095 }
11096 return FALSE;
11097}
11098#endif
11099
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100/*
11101 * Generic wildcard expansion code.
11102 *
11103 * Characters in "pat" that should not be expanded must be preceded with a
11104 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11105 *
11106 * Return FAIL when no single file was found. In this case "num_file" is not
11107 * set, and "file" may contain an error message.
11108 * Return OK when some files found. "num_file" is set to the number of
11109 * matches, "file" to the array of matches. Call FreeWild() later.
11110 */
11111 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011112gen_expand_wildcards(
11113 int num_pat, /* number of input patterns */
11114 char_u **pat, /* array of input patterns */
11115 int *num_file, /* resulting number of files */
11116 char_u ***file, /* array of resulting files */
11117 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118{
11119 int i;
11120 garray_T ga;
11121 char_u *p;
11122 static int recursive = FALSE;
11123 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011124 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011125#if defined(FEAT_SEARCHPATH)
11126 int did_expand_in_path = FALSE;
11127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128
11129 /*
11130 * expand_env() is called to expand things like "~user". If this fails,
11131 * it calls ExpandOne(), which brings us back here. In this case, always
11132 * call the machine specific expansion function, if possible. Otherwise,
11133 * return FAIL.
11134 */
11135 if (recursive)
11136#ifdef SPECIAL_WILDCHAR
11137 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11138#else
11139 return FAIL;
11140#endif
11141
11142#ifdef SPECIAL_WILDCHAR
11143 /*
11144 * If there are any special wildcard characters which we cannot handle
11145 * here, call machine specific function for all the expansion. This
11146 * avoids starting the shell for each argument separately.
11147 * For `=expr` do use the internal function.
11148 */
11149 for (i = 0; i < num_pat; i++)
11150 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011151 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152# ifdef VIM_BACKTICK
11153 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11154# endif
11155 )
11156 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11157 }
11158#endif
11159
11160 recursive = TRUE;
11161
11162 /*
11163 * The matching file names are stored in a growarray. Init it empty.
11164 */
11165 ga_init2(&ga, (int)sizeof(char_u *), 30);
11166
11167 for (i = 0; i < num_pat; ++i)
11168 {
11169 add_pat = -1;
11170 p = pat[i];
11171
11172#ifdef VIM_BACKTICK
11173 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011176 if (add_pat == -1)
11177 retval = FAIL;
11178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 else
11180#endif
11181 {
11182 /*
11183 * First expand environment variables, "~/" and "~user/".
11184 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011185 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011187 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 if (p == NULL)
11189 p = pat[i];
11190#ifdef UNIX
11191 /*
11192 * On Unix, if expand_env() can't expand an environment
11193 * variable, use the shell to do that. Discard previously
11194 * found file names and start all over again.
11195 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011196 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 {
11198 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011199 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011201 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 recursive = FALSE;
11203 return i;
11204 }
11205#endif
11206 }
11207
11208 /*
11209 * If there are wildcards: Expand file names and add each match to
11210 * the list. If there is no match, and EW_NOTFOUND is given, add
11211 * the pattern.
11212 * If there are no wildcards: Add the file name if it exists or
11213 * when EW_NOTFOUND is given.
11214 */
11215 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011216 {
11217#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011218 if ((flags & EW_PATH)
11219 && !mch_isFullName(p)
11220 && !(p[0] == '.'
11221 && (vim_ispathsep(p[1])
11222 || (p[1] == '.' && vim_ispathsep(p[2]))))
11223 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011224 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011225 /* :find completion where 'path' is used.
11226 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011227 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011228 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011229 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011230 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011231 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011232 else
11233#endif
11234 add_pat = mch_expandpath(&ga, p, flags);
11235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 }
11237
11238 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11239 {
11240 char_u *t = backslash_halve_save(p);
11241
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11243 * "vim c:/" work. */
11244 if (flags & EW_NOTFOUND)
11245 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 addfile(&ga, t, flags);
11248 vim_free(t);
11249 }
11250
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011251#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011252 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011253 uniquefy_paths(&ga, p);
11254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 if (p != pat[i])
11256 vim_free(p);
11257 }
11258
11259 *num_file = ga.ga_len;
11260 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11261
11262 recursive = FALSE;
11263
Bram Moolenaar336bd622016-01-17 18:23:58 +010011264 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265}
11266
11267# ifdef VIM_BACKTICK
11268
11269/*
11270 * Return TRUE if we can expand this backtick thing here.
11271 */
11272 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011273vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
11275 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11276}
11277
11278/*
11279 * Expand an item in `backticks` by executing it as a command.
11280 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011281 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 */
11283 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011284expand_backtick(
11285 garray_T *gap,
11286 char_u *pat,
11287 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288{
11289 char_u *p;
11290 char_u *cmd;
11291 char_u *buffer;
11292 int cnt = 0;
11293 int i;
11294
11295 /* Create the command: lop off the backticks. */
11296 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11297 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011298 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299
11300#ifdef FEAT_EVAL
11301 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011302 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 else
11304#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011305 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011306 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 vim_free(cmd);
11308 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011309 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310
11311 cmd = buffer;
11312 while (*cmd != NUL)
11313 {
11314 cmd = skipwhite(cmd); /* skip over white space */
11315 p = cmd;
11316 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11317 ++p;
11318 /* add an entry if it is not empty */
11319 if (p > cmd)
11320 {
11321 i = *p;
11322 *p = NUL;
11323 addfile(gap, cmd, flags);
11324 *p = i;
11325 ++cnt;
11326 }
11327 cmd = p;
11328 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11329 ++cmd;
11330 }
11331
11332 vim_free(buffer);
11333 return cnt;
11334}
11335# endif /* VIM_BACKTICK */
11336
11337/*
11338 * Add a file to a file list. Accepted flags:
11339 * EW_DIR add directories
11340 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011341 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 * EW_NOTFOUND add even when it doesn't exist
11343 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011344 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 */
11346 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011347addfile(
11348 garray_T *gap,
11349 char_u *f, /* filename */
11350 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351{
11352 char_u *p;
11353 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011354 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011356 /* if the file/dir/link doesn't exist, may not add it */
11357 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011358 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 return;
11360
11361#ifdef FNAME_ILLEGAL
11362 /* if the file/dir contains illegal characters, don't add it */
11363 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11364 return;
11365#endif
11366
11367 isdir = mch_isdir(f);
11368 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11369 return;
11370
Bram Moolenaarb5971142015-03-21 17:32:19 +010011371 /* If the file isn't executable, may not add it. Do accept directories.
11372 * When invoked from expand_shellcmd() do not use $PATH. */
11373 if (!isdir && (flags & EW_EXEC)
11374 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011375 return;
11376
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 /* Make room for another item in the file list. */
11378 if (ga_grow(gap, 1) == FAIL)
11379 return;
11380
11381 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11382 if (p == NULL)
11383 return;
11384
11385 STRCPY(p, f);
11386#ifdef BACKSLASH_IN_FILENAME
11387 slash_adjust(p);
11388#endif
11389 /*
11390 * Append a slash or backslash after directory names if none is present.
11391 */
11392#ifndef DONT_ADD_PATHSEP_TO_DIR
11393 if (isdir && (flags & EW_ADDSLASH))
11394 add_pathsep(p);
11395#endif
11396 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397}
11398#endif /* !NO_EXPANDPATH */
11399
11400#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11401
11402#ifndef SEEK_SET
11403# define SEEK_SET 0
11404#endif
11405#ifndef SEEK_END
11406# define SEEK_END 2
11407#endif
11408
11409/*
11410 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011411 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11412 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 * Returns an allocated string, or NULL for error.
11414 */
11415 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011416get_cmd_output(
11417 char_u *cmd,
11418 char_u *infile, /* optional input file name */
11419 int flags, /* can be SHELL_SILENT */
11420 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
11422 char_u *tempname;
11423 char_u *command;
11424 char_u *buffer = NULL;
11425 int len;
11426 int i = 0;
11427 FILE *fd;
11428
11429 if (check_restricted() || check_secure())
11430 return NULL;
11431
11432 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011433 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 {
11435 EMSG(_(e_notmp));
11436 return NULL;
11437 }
11438
11439 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011440 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 if (command == NULL)
11442 goto done;
11443
11444 /*
11445 * Call the shell to execute the command (errors are ignored).
11446 * Don't check timestamps here.
11447 */
11448 ++no_check_timestamps;
11449 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11450 --no_check_timestamps;
11451
11452 vim_free(command);
11453
11454 /*
11455 * read the names from the file into memory
11456 */
11457# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011458 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 fd = mch_fopen((char *)tempname, "r");
11460# else
11461 fd = mch_fopen((char *)tempname, READBIN);
11462# endif
11463
11464 if (fd == NULL)
11465 {
11466 EMSG2(_(e_notopen), tempname);
11467 goto done;
11468 }
11469
11470 fseek(fd, 0L, SEEK_END);
11471 len = ftell(fd); /* get size of temp file */
11472 fseek(fd, 0L, SEEK_SET);
11473
11474 buffer = alloc(len + 1);
11475 if (buffer != NULL)
11476 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11477 fclose(fd);
11478 mch_remove(tempname);
11479 if (buffer == NULL)
11480 goto done;
11481#ifdef VMS
11482 len = i; /* VMS doesn't give us what we asked for... */
11483#endif
11484 if (i != len)
11485 {
11486 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011487 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011489 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011490 {
11491 /* Change NUL into SOH, otherwise the string is truncated. */
11492 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011493 if (buffer[i] == NUL)
11494 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011495
Bram Moolenaar162bd912010-07-28 22:29:10 +020011496 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011497 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011498 else
11499 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500
11501done:
11502 vim_free(tempname);
11503 return buffer;
11504}
11505#endif
11506
11507/*
11508 * Free the list of files returned by expand_wildcards() or other expansion
11509 * functions.
11510 */
11511 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011512FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011514 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 while (count--)
11517 vim_free(files[count]);
11518 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519}
11520
11521/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011522 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 * Don't do this when still processing a command or a mapping.
11524 * Don't do this when inside a ":normal" command.
11525 */
11526 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011527goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528{
11529 return (p_im && stuff_empty() && typebuf_typed());
11530}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011531
11532/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011533 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011534 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11535 * - Remove any argument. E.g., "csh -f" -> "csh".
11536 * But don't allow a space in the path, so that this works:
11537 * "/usr/bin/csh --rcfile ~/.cshrc"
11538 * But don't do that for Windows, it's common to have a space in the path.
11539 */
11540 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011541get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011542{
11543 char_u *p;
11544
11545#ifdef WIN3264
11546 p = gettail(p_sh);
11547 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11548#else
11549 p = skiptowhite(p_sh);
11550 if (*p == NUL)
11551 {
11552 /* No white space, use the tail. */
11553 p = vim_strsave(gettail(p_sh));
11554 }
11555 else
11556 {
11557 char_u *p1, *p2;
11558
11559 /* Find the last path separator before the space. */
11560 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011561 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011562 if (vim_ispathsep(*p2))
11563 p1 = p2 + 1;
11564 p = vim_strnsave(p1, (int)(p - p1));
11565 }
11566#endif
11567 return p;
11568}