blob: f0e629b5bd085996072ec21c2820c309daa94ec2 [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 Moolenaar6c5d1042018-07-07 16:41:13 +02004830 int
4831match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004832{
4833 int i;
4834 int n = (int)STRLEN(name);
4835 int result = 0;
4836
4837 init_users();
4838 for (i = 0; i < ga_users.ga_len; i++)
4839 {
4840 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4841 return 2; /* full match */
4842 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4843 result = 1; /* partial match */
4844 }
4845 return result;
4846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#endif
4848
4849/*
4850 * Replace home directory by "~" in each space or comma separated file name in
4851 * 'src'.
4852 * If anything fails (except when out of space) dst equals src.
4853 */
4854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004855home_replace(
4856 buf_T *buf, /* when not NULL, check for help files */
4857 char_u *src, /* input file name */
4858 char_u *dst, /* where to put the result */
4859 int dstlen, /* maximum length of the result */
4860 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 spaces and commas in the file name. */
4862{
4863 size_t dirlen = 0, envlen = 0;
4864 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004865 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 char_u *p;
4867
4868 if (src == NULL)
4869 {
4870 *dst = NUL;
4871 return;
4872 }
4873
4874 /*
4875 * If the file is a help file, remove the path completely.
4876 */
4877 if (buf != NULL && buf->b_help)
4878 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004879 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 return;
4881 }
4882
4883 /*
4884 * We check both the value of the $HOME environment variable and the
4885 * "real" home directory.
4886 */
4887 if (homedir != NULL)
4888 dirlen = STRLEN(homedir);
4889
4890#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004891 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004893 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4894#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004895#ifdef WIN3264
4896 if (homedir_env == NULL)
4897 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4898#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004899 /* Empty is the same as not set. */
4900 if (homedir_env != NULL && *homedir_env == NUL)
4901 homedir_env = NULL;
4902
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004903#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004904 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004905 {
4906 int usedlen = 0;
4907 int flen;
4908 char_u *fbuf = NULL;
4909
4910 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004911 (void)modify_fname((char_u *)":p", &usedlen,
4912 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004913 flen = (int)STRLEN(homedir_env);
4914 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4915 /* Remove the trailing / that is added to a directory. */
4916 homedir_env[flen - 1] = NUL;
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918#endif
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (homedir_env != NULL)
4921 envlen = STRLEN(homedir_env);
4922
4923 if (!one)
4924 src = skipwhite(src);
4925 while (*src && dstlen > 0)
4926 {
4927 /*
4928 * Here we are at the beginning of a file name.
4929 * First, check to see if the beginning of the file name matches
4930 * $HOME or the "real" home directory. Check that there is a '/'
4931 * after the match (so that if e.g. the file is "/home/pieter/bla",
4932 * and the home directory is "/home/piet", the file does not end up
4933 * as "~er/bla" (which would seem to indicate the file "bla" in user
4934 * er's home directory)).
4935 */
4936 p = homedir;
4937 len = dirlen;
4938 for (;;)
4939 {
4940 if ( len
4941 && fnamencmp(src, p, len) == 0
4942 && (vim_ispathsep(src[len])
4943 || (!one && (src[len] == ',' || src[len] == ' '))
4944 || src[len] == NUL))
4945 {
4946 src += len;
4947 if (--dstlen > 0)
4948 *dst++ = '~';
4949
4950 /*
4951 * If it's just the home directory, add "/".
4952 */
4953 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4954 *dst++ = '/';
4955 break;
4956 }
4957 if (p == homedir_env)
4958 break;
4959 p = homedir_env;
4960 len = envlen;
4961 }
4962
4963 /* if (!one) skip to separator: space or comma */
4964 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4965 *dst++ = *src++;
4966 /* skip separator */
4967 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4968 *dst++ = *src++;
4969 }
4970 /* if (dstlen == 0) out of space, what to do??? */
4971
4972 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004973
4974 if (homedir_env != homedir_env_orig)
4975 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976}
4977
4978/*
4979 * Like home_replace, store the replaced string in allocated memory.
4980 * When something fails, NULL is returned.
4981 */
4982 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004983home_replace_save(
4984 buf_T *buf, /* when not NULL, check for help files */
4985 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987 char_u *dst;
4988 unsigned len;
4989
4990 len = 3; /* space for "~/" and trailing NUL */
4991 if (src != NULL) /* just in case */
4992 len += (unsigned)STRLEN(src);
4993 dst = alloc(len);
4994 if (dst != NULL)
4995 home_replace(buf, src, dst, len, TRUE);
4996 return dst;
4997}
4998
4999/*
5000 * Compare two file names and return:
5001 * FPC_SAME if they both exist and are the same file.
5002 * FPC_SAMEX if they both don't exist and have the same file name.
5003 * FPC_DIFF if they both exist and are different files.
5004 * FPC_NOTX if they both don't exist.
5005 * FPC_DIFFX if one of them doesn't exist.
5006 * For the first name environment variables are expanded
5007 */
5008 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005009fullpathcmp(
5010 char_u *s1,
5011 char_u *s2,
5012 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014#ifdef UNIX
5015 char_u exp1[MAXPATHL];
5016 char_u full1[MAXPATHL];
5017 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005018 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 int r1, r2;
5020
5021 expand_env(s1, exp1, MAXPATHL);
5022 r1 = mch_stat((char *)exp1, &st1);
5023 r2 = mch_stat((char *)s2, &st2);
5024 if (r1 != 0 && r2 != 0)
5025 {
5026 /* if mch_stat() doesn't work, may compare the names */
5027 if (checkname)
5028 {
5029 if (fnamecmp(exp1, s2) == 0)
5030 return FPC_SAMEX;
5031 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5032 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5033 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5034 return FPC_SAMEX;
5035 }
5036 return FPC_NOTX;
5037 }
5038 if (r1 != 0 || r2 != 0)
5039 return FPC_DIFFX;
5040 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5041 return FPC_SAME;
5042 return FPC_DIFF;
5043#else
5044 char_u *exp1; /* expanded s1 */
5045 char_u *full1; /* full path of s1 */
5046 char_u *full2; /* full path of s2 */
5047 int retval = FPC_DIFF;
5048 int r1, r2;
5049
5050 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5051 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5052 {
5053 full1 = exp1 + MAXPATHL;
5054 full2 = full1 + MAXPATHL;
5055
5056 expand_env(s1, exp1, MAXPATHL);
5057 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5058 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5059
5060 /* If vim_FullName() fails, the file probably doesn't exist. */
5061 if (r1 != OK && r2 != OK)
5062 {
5063 if (checkname && fnamecmp(exp1, s2) == 0)
5064 retval = FPC_SAMEX;
5065 else
5066 retval = FPC_NOTX;
5067 }
5068 else if (r1 != OK || r2 != OK)
5069 retval = FPC_DIFFX;
5070 else if (fnamecmp(full1, full2))
5071 retval = FPC_DIFF;
5072 else
5073 retval = FPC_SAME;
5074 vim_free(exp1);
5075 }
5076 return retval;
5077#endif
5078}
5079
5080/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005081 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005082 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005083 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 */
5085 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005086gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
5088 char_u *p1, *p2;
5089
5090 if (fname == NULL)
5091 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005092 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005094 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005096 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 return p1;
5099}
5100
Bram Moolenaar31710262010-08-13 13:36:15 +02005101#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005102static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02005103
5104/*
5105 * Return the end of the directory name, on the first path
5106 * separator:
5107 * "/path/file", "/path/dir/", "/path//dir", "/file"
5108 * ^ ^ ^ ^
5109 */
5110 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005111gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005112{
5113 char_u *dir_end = fname;
5114 char_u *next_dir_end = fname;
5115 int look_for_sep = TRUE;
5116 char_u *p;
5117
5118 for (p = fname; *p != NUL; )
5119 {
5120 if (vim_ispathsep(*p))
5121 {
5122 if (look_for_sep)
5123 {
5124 next_dir_end = p;
5125 look_for_sep = FALSE;
5126 }
5127 }
5128 else
5129 {
5130 if (!look_for_sep)
5131 dir_end = next_dir_end;
5132 look_for_sep = TRUE;
5133 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005134 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005135 }
5136 return dir_end;
5137}
5138#endif
5139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005141 * Get pointer to tail of "fname", including path separators. Putting a NUL
5142 * here leaves the directory name. Takes care of "c:/" and "//".
5143 * Always returns a valid pointer.
5144 */
5145 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005146gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005147{
5148 char_u *p;
5149 char_u *t;
5150
5151 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5152 t = gettail(fname);
5153 while (t > p && after_pathsep(fname, t))
5154 --t;
5155#ifdef VMS
5156 /* path separator is part of the path */
5157 ++t;
5158#endif
5159 return t;
5160}
5161
5162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 * get the next path component (just after the next path separator).
5164 */
5165 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005166getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005169 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (*fname)
5171 ++fname;
5172 return fname;
5173}
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175/*
5176 * Get a pointer to one character past the head of a path name.
5177 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5178 * If there is no head, path is returned.
5179 */
5180 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 char_u *retval;
5184
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005185#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 /* may skip "c:" */
5187 if (isalpha(path[0]) && path[1] == ':')
5188 retval = path + 2;
5189 else
5190 retval = path;
5191#else
5192# if defined(AMIGA)
5193 /* may skip "label:" */
5194 retval = vim_strchr(path, ':');
5195 if (retval == NULL)
5196 retval = path;
5197# else /* Unix */
5198 retval = path;
5199# endif
5200#endif
5201
5202 while (vim_ispathsep(*retval))
5203 ++retval;
5204
5205 return retval;
5206}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
5208/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005209 * Return TRUE if 'c' is a path separator.
5210 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 */
5212 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005213vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005215#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005217#else
5218# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005220# else
5221# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5223 return (c == ':' || c == '[' || c == ']' || c == '/'
5224 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005225# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005227# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230}
5231
Bram Moolenaar69c35002013-11-04 02:54:12 +01005232/*
5233 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5234 */
5235 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005236vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005237{
5238 return vim_ispathsep(c)
5239#ifdef BACKSLASH_IN_FILENAME
5240 && c != ':'
5241#endif
5242 ;
5243}
5244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5246/*
5247 * return TRUE if 'c' is a path list separator.
5248 */
5249 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252#ifdef UNIX
5253 return (c == ':');
5254#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005255 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#endif
5257}
5258#endif
5259
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005260/*
5261 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5262 * It's done in-place.
5263 */
5264 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005265shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005266{
5267 char_u *tail, *s, *d;
5268 int skip = FALSE;
5269
5270 tail = gettail(str);
5271 d = str;
5272 for (s = str; ; ++s)
5273 {
5274 if (s >= tail) /* copy the whole tail */
5275 {
5276 *d++ = *s;
5277 if (*s == NUL)
5278 break;
5279 }
5280 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5281 {
5282 *d++ = *s;
5283 skip = FALSE;
5284 }
5285 else if (!skip)
5286 {
5287 *d++ = *s; /* copy next char */
5288 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5289 skip = TRUE;
5290# ifdef FEAT_MBYTE
5291 if (has_mbyte)
5292 {
5293 int l = mb_ptr2len(s);
5294
5295 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005296 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005297 }
5298# endif
5299 }
5300 }
5301}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005302
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005303/*
5304 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5305 * Also returns TRUE if there is no directory name.
5306 * "fname" must be writable!.
5307 */
5308 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005309dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005310{
5311 char_u *p;
5312 int c;
5313 int retval;
5314
5315 p = gettail_sep(fname);
5316 if (p == fname)
5317 return TRUE;
5318 c = *p;
5319 *p = NUL;
5320 retval = mch_isdir(fname);
5321 *p = c;
5322 return retval;
5323}
5324
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005326 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5327 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 */
5329 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005330vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005332#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005334#else
5335 if (p_fic)
5336 return MB_STRICMP(x, y);
5337 return STRCMP(x, y);
5338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339}
5340
5341 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005342vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005344#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005345 char_u *px = x;
5346 char_u *py = y;
5347 int cx = NUL;
5348 int cy = NUL;
5349
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005350 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005352 cx = PTR2CHAR(px);
5353 cy = PTR2CHAR(py);
5354 if (cx == NUL || cy == NUL
5355 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5356 && !(cx == '/' && cy == '\\')
5357 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005359 len -= MB_PTR2LEN(px);
5360 px += MB_PTR2LEN(px);
5361 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 if (len == 0)
5364 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005365 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005366#else
5367 if (p_fic)
5368 return MB_STRNICMP(x, y, len);
5369 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005371}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373/*
5374 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005375 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 */
5377 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005378concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 char_u *dest;
5381
5382 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5383 if (dest != NULL)
5384 {
5385 STRCPY(dest, fname1);
5386 if (sep)
5387 add_pathsep(dest);
5388 STRCAT(dest, fname2);
5389 }
5390 return dest;
5391}
5392
Bram Moolenaard6754642005-01-17 22:18:45 +00005393/*
5394 * Concatenate two strings and return the result in allocated memory.
5395 * Returns NULL when out of memory.
5396 */
5397 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005398concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005399{
5400 char_u *dest;
5401 size_t l = STRLEN(str1);
5402
5403 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5404 if (dest != NULL)
5405 {
5406 STRCPY(dest, str1);
5407 STRCPY(dest + l, str2);
5408 }
5409 return dest;
5410}
Bram Moolenaard6754642005-01-17 22:18:45 +00005411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412/*
5413 * Add a path separator to a file name, unless it already ends in a path
5414 * separator.
5415 */
5416 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005417add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005419 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 STRCAT(p, PATHSEPSTR);
5421}
5422
5423/*
5424 * FullName_save - Make an allocated copy of a full file name.
5425 * Returns NULL when out of memory.
5426 */
5427 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005428FullName_save(
5429 char_u *fname,
5430 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005431 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 char_u *buf;
5434 char_u *new_fname = NULL;
5435
5436 if (fname == NULL)
5437 return NULL;
5438
5439 buf = alloc((unsigned)MAXPATHL);
5440 if (buf != NULL)
5441 {
5442 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5443 new_fname = vim_strsave(buf);
5444 else
5445 new_fname = vim_strsave(fname);
5446 vim_free(buf);
5447 }
5448 return new_fname;
5449}
5450
5451#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5452
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005453static char_u *skip_string(char_u *p);
5454static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005455static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005456static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
5458/*
5459 * Find the start of a comment, not knowing if we are in a comment right now.
5460 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005461 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005463 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005464ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005465{
5466 return find_start_comment(curbuf->b_ind_maxcomment);
5467}
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005470find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471{
5472 pos_T *pos;
5473 char_u *line;
5474 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005475 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005477 for (;;)
5478 {
5479 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5480 if (pos == NULL)
5481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005483 /*
5484 * Check if the comment start we found is inside a string.
5485 * If it is then restrict the search to below this line and try again.
5486 */
5487 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005488 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005489 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005490 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005491 break;
5492 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5493 if (cur_maxcomment <= 0)
5494 {
5495 pos = NULL;
5496 break;
5497 }
5498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 return pos;
5500}
5501
5502/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005503 * Find the start of a comment or raw string, not knowing if we are in a
5504 * comment or raw string right now.
5505 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005506 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005507 * Return NULL when not inside a comment or raw string.
5508 * "CORS" -> Comment Or Raw String
5509 */
5510 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005511ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005512{
Bram Moolenaar089af182015-10-07 11:41:49 +02005513 static pos_T comment_pos_copy;
5514 pos_T *comment_pos;
5515 pos_T *rs_pos;
5516
5517 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5518 if (comment_pos != NULL)
5519 {
5520 /* Need to make a copy of the static pos in findmatchlimit(),
5521 * calling find_start_rawstring() may change it. */
5522 comment_pos_copy = *comment_pos;
5523 comment_pos = &comment_pos_copy;
5524 }
5525 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005526
5527 /* If comment_pos is before rs_pos the raw string is inside the comment.
5528 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005529 if (comment_pos == NULL || (rs_pos != NULL
5530 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005531 {
5532 if (is_raw != NULL && rs_pos != NULL)
5533 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005534 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005535 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005536 return comment_pos;
5537}
5538
5539/*
5540 * Find the start of a raw string, not knowing if we are in one right now.
5541 * Search starts at w_cursor.lnum and goes backwards.
5542 * Return NULL when not inside a raw string.
5543 */
5544 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005545find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005546{
5547 pos_T *pos;
5548 char_u *line;
5549 char_u *p;
5550 int cur_maxcomment = ind_maxcomment;
5551
5552 for (;;)
5553 {
5554 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5555 if (pos == NULL)
5556 break;
5557
5558 /*
5559 * Check if the raw string start we found is inside a string.
5560 * If it is then restrict the search to below this line and try again.
5561 */
5562 line = ml_get(pos->lnum);
5563 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5564 p = skip_string(p);
5565 if ((colnr_T)(p - line) <= pos->col)
5566 break;
5567 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5568 if (cur_maxcomment <= 0)
5569 {
5570 pos = NULL;
5571 break;
5572 }
5573 }
5574 return pos;
5575}
5576
5577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 * Skip to the end of a "string" and a 'c' character.
5579 * If there is no string or character, return argument unmodified.
5580 */
5581 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005582skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 int i;
5585
5586 /*
5587 * We loop, because strings may be concatenated: "date""time".
5588 */
5589 for ( ; ; ++p)
5590 {
5591 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5592 {
5593 if (!p[1]) /* ' at end of line */
5594 break;
5595 i = 2;
5596 if (p[1] == '\\') /* '\n' or '\000' */
5597 {
5598 ++i;
5599 while (vim_isdigit(p[i - 1])) /* '\000' */
5600 ++i;
5601 }
5602 if (p[i] == '\'') /* check for trailing ' */
5603 {
5604 p += i;
5605 continue;
5606 }
5607 }
5608 else if (p[0] == '"') /* start of string */
5609 {
5610 for (++p; p[0]; ++p)
5611 {
5612 if (p[0] == '\\' && p[1] != NUL)
5613 ++p;
5614 else if (p[0] == '"') /* end of string */
5615 break;
5616 }
5617 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005618 continue; /* continue for another string */
5619 }
5620 else if (p[0] == 'R' && p[1] == '"')
5621 {
5622 /* Raw string: R"[delim](...)[delim]" */
5623 char_u *delim = p + 2;
5624 char_u *paren = vim_strchr(delim, '(');
5625
5626 if (paren != NULL)
5627 {
5628 size_t delim_len = paren - delim;
5629
5630 for (p += 3; *p; ++p)
5631 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5632 && p[delim_len + 1] == '"')
5633 {
5634 p += delim_len + 1;
5635 break;
5636 }
5637 if (p[0] == '"')
5638 continue; /* continue for another string */
5639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641 break; /* no string found */
5642 }
5643 if (!*p)
5644 --p; /* backup from NUL */
5645 return p;
5646}
5647#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5648
5649#if defined(FEAT_CINDENT) || defined(PROTO)
5650
5651/*
5652 * Do C or expression indenting on the current line.
5653 */
5654 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005655do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657# ifdef FEAT_EVAL
5658 if (*curbuf->b_p_inde != NUL)
5659 fixthisline(get_expr_indent);
5660 else
5661# endif
5662 fixthisline(get_c_indent);
5663}
5664
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005665/* Find result cache for cpp_baseclass */
5666typedef struct {
5667 int found;
5668 lpos_T lpos;
5669} cpp_baseclass_cache_T;
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671/*
5672 * Functions for C-indenting.
5673 * Most of this originally comes from Eric Fischer.
5674 */
5675/*
5676 * Below "XXX" means that this function may unlock the current line.
5677 */
5678
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005679static char_u *cin_skipcomment(char_u *);
5680static int cin_nocode(char_u *);
5681static pos_T *find_line_comment(void);
5682static int cin_has_js_key(char_u *text);
5683static int cin_islabel_skip(char_u **);
5684static int cin_isdefault(char_u *);
5685static char_u *after_label(char_u *l);
5686static int get_indent_nolabel(linenr_T lnum);
5687static int skip_label(linenr_T, char_u **pp);
5688static int cin_first_id_amount(void);
5689static int cin_get_equal_amount(linenr_T lnum);
5690static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005691static int cin_iscomment(char_u *);
5692static int cin_islinecomment(char_u *);
5693static int cin_isterminated(char_u *, int, int);
5694static int cin_isinit(void);
5695static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5696static int cin_isif(char_u *);
5697static int cin_iselse(char_u *);
5698static int cin_isdo(char_u *);
5699static int cin_iswhileofdo(char_u *, linenr_T);
5700static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5701static int cin_iswhileofdo_end(int terminated);
5702static int cin_isbreak(char_u *);
5703static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5704static int get_baseclass_amount(int col);
5705static int cin_ends_in(char_u *, char_u *, char_u *);
5706static int cin_starts_with(char_u *s, char *word);
5707static int cin_skip2pos(pos_T *trypos);
5708static pos_T *find_start_brace(void);
5709static pos_T *find_match_paren(int);
5710static pos_T *find_match_char(int c, int ind_maxparen);
5711static int corr_ind_maxparen(pos_T *startpos);
5712static int find_last_paren(char_u *l, int start, int end);
5713static int find_match(int lookfor, linenr_T ourscope);
5714static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716/*
5717 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005718 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 */
5720 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005721cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 while (*s)
5724 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005725 char_u *prev_s = s;
5726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005728
5729 /* Perl/shell # comment comment continues until eol. Require a space
5730 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005731 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005732 {
5733 s += STRLEN(s);
5734 break;
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 if (*s != '/')
5737 break;
5738 ++s;
5739 if (*s == '/') /* slash-slash comment continues till eol */
5740 {
5741 s += STRLEN(s);
5742 break;
5743 }
5744 if (*s != '*')
5745 break;
5746 for (++s; *s; ++s) /* skip slash-star comment */
5747 if (s[0] == '*' && s[1] == '/')
5748 {
5749 s += 2;
5750 break;
5751 }
5752 }
5753 return s;
5754}
5755
5756/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005757 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 * not considered code.
5759 */
5760 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005761cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
5763 return *cin_skipcomment(s) == NUL;
5764}
5765
5766/*
5767 * Check previous lines for a "//" line comment, skipping over blank lines.
5768 */
5769 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005770find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 static pos_T pos;
5773 char_u *line;
5774 char_u *p;
5775
5776 pos = curwin->w_cursor;
5777 while (--pos.lnum > 0)
5778 {
5779 line = ml_get(pos.lnum);
5780 p = skipwhite(line);
5781 if (cin_islinecomment(p))
5782 {
5783 pos.col = (int)(p - line);
5784 return &pos;
5785 }
5786 if (*p != NUL)
5787 break;
5788 }
5789 return NULL;
5790}
5791
5792/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005793 * Return TRUE if "text" starts with "key:".
5794 */
5795 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005796cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005797{
5798 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005799 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005800
5801 if (*s == '\'' || *s == '"')
5802 {
5803 /* can be 'key': or "key": */
5804 quote = *s;
5805 ++s;
5806 }
5807 if (!vim_isIDc(*s)) /* need at least one ID character */
5808 return FALSE;
5809
5810 while (vim_isIDc(*s))
5811 ++s;
5812 if (*s == quote)
5813 ++s;
5814
5815 s = cin_skipcomment(s);
5816
5817 /* "::" is not a label, it's C++ */
5818 return (*s == ':' && s[1] != ':');
5819}
5820
5821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005823 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 */
5825 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005826cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828 if (!vim_isIDc(**s)) /* need at least one ID character */
5829 return FALSE;
5830
5831 while (vim_isIDc(**s))
5832 (*s)++;
5833
5834 *s = cin_skipcomment(*s);
5835
5836 /* "::" is not a label, it's C++ */
5837 return (**s == ':' && *++*s != ':');
5838}
5839
5840/*
5841 * Recognize a label: "label:".
5842 * Note: curwin->w_cursor must be where we are looking for the label.
5843 */
5844 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005845cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
5847 char_u *s;
5848
5849 s = cin_skipcomment(ml_get_curline());
5850
5851 /*
5852 * Exclude "default" from labels, since it should be indented
5853 * like a switch label. Same for C++ scope declarations.
5854 */
5855 if (cin_isdefault(s))
5856 return FALSE;
5857 if (cin_isscopedecl(s))
5858 return FALSE;
5859
5860 if (cin_islabel_skip(&s))
5861 {
5862 /*
5863 * Only accept a label if the previous line is terminated or is a case
5864 * label.
5865 */
5866 pos_T cursor_save;
5867 pos_T *trypos;
5868 char_u *line;
5869
5870 cursor_save = curwin->w_cursor;
5871 while (curwin->w_cursor.lnum > 1)
5872 {
5873 --curwin->w_cursor.lnum;
5874
5875 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005876 * If we're in a comment or raw string now, skip to the start of
5877 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 */
5879 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005880 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 curwin->w_cursor = *trypos;
5882
5883 line = ml_get_curline();
5884 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5885 continue;
5886 if (*(line = cin_skipcomment(line)) == NUL)
5887 continue;
5888
5889 curwin->w_cursor = cursor_save;
5890 if (cin_isterminated(line, TRUE, FALSE)
5891 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005892 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 || (cin_islabel_skip(&line) && cin_nocode(line)))
5894 return TRUE;
5895 return FALSE;
5896 }
5897 curwin->w_cursor = cursor_save;
5898 return TRUE; /* label at start of file??? */
5899 }
5900 return FALSE;
5901}
5902
5903/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005904 * Recognize structure initialization and enumerations:
5905 * "[typedef] [static|public|protected|private] enum"
5906 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 */
5908 static int
5909cin_isinit(void)
5910{
5911 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005912 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
5914 s = cin_skipcomment(ml_get_curline());
5915
Bram Moolenaar75342212013-03-07 13:13:52 +01005916 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 s = cin_skipcomment(s + 7);
5918
Bram Moolenaar75342212013-03-07 13:13:52 +01005919 for (;;)
5920 {
5921 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005922
Bram Moolenaar75342212013-03-07 13:13:52 +01005923 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5924 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005925 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005926 if (cin_starts_with(s, skip[i]))
5927 {
5928 s = cin_skipcomment(s + l);
5929 l = 0;
5930 break;
5931 }
5932 }
5933 if (l != 0)
5934 break;
5935 }
5936
5937 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 return TRUE;
5939
5940 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5941 return TRUE;
5942
5943 return FALSE;
5944}
5945
5946/*
5947 * Recognize a switch label: "case .*:" or "default:".
5948 */
5949 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005950cin_iscase(
5951 char_u *s,
5952 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
5954 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005955 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {
5957 for (s += 4; *s; ++s)
5958 {
5959 s = cin_skipcomment(s);
5960 if (*s == ':')
5961 {
5962 if (s[1] == ':') /* skip over "::" for C++ */
5963 ++s;
5964 else
5965 return TRUE;
5966 }
5967 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005968 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5970 return FALSE; /* stop at comment */
5971 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005972 {
5973 /* JS etc. */
5974 if (strict)
5975 return FALSE; /* stop at string */
5976 else
5977 return TRUE;
5978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980 return FALSE;
5981 }
5982
5983 if (cin_isdefault(s))
5984 return TRUE;
5985 return FALSE;
5986}
5987
5988/*
5989 * Recognize a "default" switch label.
5990 */
5991 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005992cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 return (STRNCMP(s, "default", 7) == 0
5995 && *(s = cin_skipcomment(s + 7)) == ':'
5996 && s[1] != ':');
5997}
5998
5999/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006000 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 */
6002 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006003cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
6005 int i;
6006
6007 s = cin_skipcomment(s);
6008 if (STRNCMP(s, "public", 6) == 0)
6009 i = 6;
6010 else if (STRNCMP(s, "protected", 9) == 0)
6011 i = 9;
6012 else if (STRNCMP(s, "private", 7) == 0)
6013 i = 7;
6014 else
6015 return FALSE;
6016 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6017}
6018
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006019/* Maximum number of lines to search back for a "namespace" line. */
6020#define FIND_NAMESPACE_LIM 20
6021
6022/*
6023 * Recognize a "namespace" scope declaration.
6024 */
6025 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006026cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006027{
6028 char_u *p;
6029 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006030 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006031
6032 s = cin_skipcomment(s);
6033 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6034 {
6035 p = cin_skipcomment(skipwhite(s + 9));
6036 while (*p != NUL)
6037 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006038 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006039 {
6040 has_name = TRUE; /* found end of a name */
6041 p = cin_skipcomment(skipwhite(p));
6042 }
6043 else if (*p == '{')
6044 {
6045 break;
6046 }
6047 else if (vim_iswordc(*p))
6048 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006049 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006050 if (has_name)
6051 return FALSE; /* word character after skipping past name */
6052 ++p;
6053 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006054 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6055 {
6056 if (!has_name_start || has_name)
6057 return FALSE;
6058 /* C++ 17 nested namespace */
6059 p += 3;
6060 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006061 else
6062 {
6063 return FALSE;
6064 }
6065 }
6066 return TRUE;
6067 }
6068 return FALSE;
6069}
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006072 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6073 */
6074 static int
6075cin_is_cpp_extern_c(char_u *s)
6076{
6077 char_u *p;
6078 int has_string_literal = FALSE;
6079
6080 s = cin_skipcomment(s);
6081 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6082 {
6083 p = cin_skipcomment(skipwhite(s + 6));
6084 while (*p != NUL)
6085 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006086 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006087 {
6088 p = cin_skipcomment(skipwhite(p));
6089 }
6090 else if (*p == '{')
6091 {
6092 break;
6093 }
6094 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6095 {
6096 if (has_string_literal)
6097 return FALSE;
6098 has_string_literal = TRUE;
6099 p += 3;
6100 }
6101 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6102 && p[4] == '"')
6103 {
6104 if (has_string_literal)
6105 return FALSE;
6106 has_string_literal = TRUE;
6107 p += 5;
6108 }
6109 else
6110 {
6111 return FALSE;
6112 }
6113 }
6114 return has_string_literal ? TRUE : FALSE;
6115 }
6116 return FALSE;
6117}
6118
6119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 * Return a pointer to the first non-empty non-comment character after a ':'.
6121 * Return NULL if not found.
6122 * case 234: a = b;
6123 * ^
6124 */
6125 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006126after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128 for ( ; *l; ++l)
6129 {
6130 if (*l == ':')
6131 {
6132 if (l[1] == ':') /* skip over "::" for C++ */
6133 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006134 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 break;
6136 }
6137 else if (*l == '\'' && l[1] && l[2] == '\'')
6138 l += 2; /* skip over 'x' */
6139 }
6140 if (*l == NUL)
6141 return NULL;
6142 l = cin_skipcomment(l + 1);
6143 if (*l == NUL)
6144 return NULL;
6145 return l;
6146}
6147
6148/*
6149 * Get indent of line "lnum", skipping a label.
6150 * Return 0 if there is nothing after the label.
6151 */
6152 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006153get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 char_u *l;
6156 pos_T fp;
6157 colnr_T col;
6158 char_u *p;
6159
6160 l = ml_get(lnum);
6161 p = after_label(l);
6162 if (p == NULL)
6163 return 0;
6164
6165 fp.col = (colnr_T)(p - l);
6166 fp.lnum = lnum;
6167 getvcol(curwin, &fp, &col, NULL, NULL);
6168 return (int)col;
6169}
6170
6171/*
6172 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006173 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 * label: if (asdf && asdfasdf)
6175 * ^
6176 */
6177 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006178skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
6180 char_u *l;
6181 int amount;
6182 pos_T cursor_save;
6183
6184 cursor_save = curwin->w_cursor;
6185 curwin->w_cursor.lnum = lnum;
6186 l = ml_get_curline();
6187 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006188 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 {
6190 amount = get_indent_nolabel(lnum);
6191 l = after_label(ml_get_curline());
6192 if (l == NULL) /* just in case */
6193 l = ml_get_curline();
6194 }
6195 else
6196 {
6197 amount = get_indent();
6198 l = ml_get_curline();
6199 }
6200 *pp = l;
6201
6202 curwin->w_cursor = cursor_save;
6203 return amount;
6204}
6205
6206/*
6207 * Return the indent of the first variable name after a type in a declaration.
6208 * int a, indent of "a"
6209 * static struct foo b, indent of "b"
6210 * enum bla c, indent of "c"
6211 * Returns zero when it doesn't look like a declaration.
6212 */
6213 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006214cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 char_u *line, *p, *s;
6217 int len;
6218 pos_T fp;
6219 colnr_T col;
6220
6221 line = ml_get_curline();
6222 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006223 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6225 {
6226 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006227 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 }
6229 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6230 p = skipwhite(p + 6);
6231 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6232 p = skipwhite(p + 4);
6233 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6234 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6235 {
6236 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006237 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6238 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6239 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6240 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 p = s;
6242 }
6243 for (len = 0; vim_isIDc(p[len]); ++len)
6244 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006245 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 return 0;
6247
6248 p = skipwhite(p + len);
6249 fp.lnum = curwin->w_cursor.lnum;
6250 fp.col = (colnr_T)(p - line);
6251 getvcol(curwin, &fp, &col, NULL, NULL);
6252 return (int)col;
6253}
6254
6255/*
6256 * Return the indent of the first non-blank after an equal sign.
6257 * char *foo = "here";
6258 * Return zero if no (useful) equal sign found.
6259 * Return -1 if the line above "lnum" ends in a backslash.
6260 * foo = "asdf\
6261 * asdf\
6262 * here";
6263 */
6264 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006265cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
6267 char_u *line;
6268 char_u *s;
6269 colnr_T col;
6270 pos_T fp;
6271
6272 if (lnum > 1)
6273 {
6274 line = ml_get(lnum - 1);
6275 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6276 return -1;
6277 }
6278
6279 line = s = ml_get(lnum);
6280 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6281 {
6282 if (cin_iscomment(s)) /* ignore comments */
6283 s = cin_skipcomment(s);
6284 else
6285 ++s;
6286 }
6287 if (*s != '=')
6288 return 0;
6289
6290 s = skipwhite(s + 1);
6291 if (cin_nocode(s))
6292 return 0;
6293
6294 if (*s == '"') /* nice alignment for continued strings */
6295 ++s;
6296
6297 fp.lnum = lnum;
6298 fp.col = (colnr_T)(s - line);
6299 getvcol(curwin, &fp, &col, NULL, NULL);
6300 return (int)col;
6301}
6302
6303/*
6304 * Recognize a preprocessor statement: Any line that starts with '#'.
6305 */
6306 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006307cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006309 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 return TRUE;
6311 return FALSE;
6312}
6313
6314/*
6315 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6316 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6317 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006318 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 */
6320 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006321cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 char_u *line = *pp;
6324 linenr_T lnum = *lnump;
6325 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006326 int candidate_amount = *amount;
6327
6328 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6329 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006331 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 {
6333 if (cin_ispreproc(line))
6334 {
6335 retval = TRUE;
6336 *lnump = lnum;
6337 break;
6338 }
6339 if (lnum == 1)
6340 break;
6341 line = ml_get(--lnum);
6342 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6343 break;
6344 }
6345
6346 if (lnum != *lnump)
6347 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006348 if (retval)
6349 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 return retval;
6351}
6352
6353/*
6354 * Recognize the start of a C or C++ comment.
6355 */
6356 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006357cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6360}
6361
6362/*
6363 * Recognize the start of a "//" comment.
6364 */
6365 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006366cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 return (p[0] == '/' && p[1] == '/');
6369}
6370
6371/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006372 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6373 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006375 * If a line begins with an "else", only consider it terminated if no unmatched
6376 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 * Return the character terminating the line (ending char's have precedence if
6378 * both apply in order to determine initializations).
6379 */
6380 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006381cin_isterminated(
6382 char_u *s,
6383 int incl_open, /* include '{' at the end as terminator */
6384 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006386 char_u found_start = 0;
6387 unsigned n_open = 0;
6388 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390 s = cin_skipcomment(s);
6391
6392 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6393 found_start = *s;
6394
Bram Moolenaar496f9512011-05-19 16:35:09 +02006395 if (!found_start)
6396 is_else = cin_iselse(s);
6397
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 while (*s)
6399 {
6400 /* skip over comments, "" strings and 'c'haracters */
6401 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006402 if (*s == '}' && n_open > 0)
6403 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006404 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006405 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 && cin_nocode(s + 1))
6407 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006408 else if (*s == '{')
6409 {
6410 if (incl_open && cin_nocode(s + 1))
6411 return *s;
6412 else
6413 ++n_open;
6414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415
6416 if (*s)
6417 s++;
6418 }
6419 return found_start;
6420}
6421
6422/*
6423 * Recognize the basic picture of a function declaration -- it needs to
6424 * have an open paren somewhere and a close paren at the end of the line and
6425 * no semicolons anywhere.
6426 * When a line ends in a comma we continue looking in the next line.
6427 * "sp" points to a string with the line. When looking at other lines it must
6428 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006429 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006430 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 */
6432 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006433cin_isfuncdecl(
6434 char_u **sp,
6435 linenr_T first_lnum,
6436 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
6438 char_u *s;
6439 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006440 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006442 pos_T *trypos;
6443 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
6445 if (sp == NULL)
6446 s = ml_get(lnum);
6447 else
6448 s = *sp;
6449
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006450 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006451 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006452 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006453 {
6454 lnum = trypos->lnum;
6455 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006456 {
6457 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006458 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006459 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006460
6461 s = ml_get(lnum);
6462 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006463 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006464
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006465 /* Ignore line starting with #. */
6466 if (cin_ispreproc(s))
6467 return FALSE;
6468
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6470 {
6471 if (cin_iscomment(s)) /* ignore comments */
6472 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006473 else if (*s == ':')
6474 {
6475 if (*(s + 1) == ':')
6476 s += 2;
6477 else
6478 /* To avoid a mistake in the following situation:
6479 * A::A(int a, int b)
6480 * : a(0) // <--not a function decl
6481 * , b(0)
6482 * {...
6483 */
6484 return FALSE;
6485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 else
6487 ++s;
6488 }
6489 if (*s != '(')
6490 return FALSE; /* ';', ' or " before any () or no '(' */
6491
6492 while (*s && *s != ';' && *s != '\'' && *s != '"')
6493 {
6494 if (*s == ')' && cin_nocode(s + 1))
6495 {
6496 /* ')' at the end: may have found a match
6497 * Check for he previous line not to end in a backslash:
6498 * #if defined(x) && \
6499 * defined(y)
6500 */
6501 lnum = first_lnum - 1;
6502 s = ml_get(lnum);
6503 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6504 retval = TRUE;
6505 goto done;
6506 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006507 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006509 int comma = (*s == ',');
6510
6511 /* ',' at the end: continue looking in the next line.
6512 * At the end: check for ',' in the next line, for this style:
6513 * func(arg1
6514 * , arg2) */
6515 for (;;)
6516 {
6517 if (lnum >= curbuf->b_ml.ml_line_count)
6518 break;
6519 s = ml_get(++lnum);
6520 if (!cin_ispreproc(s))
6521 break;
6522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (lnum >= curbuf->b_ml.ml_line_count)
6524 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006525 /* Require a comma at end of the line or a comma or ')' at the
6526 * start of next line. */
6527 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006528 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006529 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006530 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532 else if (cin_iscomment(s)) /* ignore comments */
6533 s = cin_skipcomment(s);
6534 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006537 just_started = FALSE;
6538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 }
6540
6541done:
6542 if (lnum != first_lnum && sp != NULL)
6543 *sp = ml_get(first_lnum);
6544
6545 return retval;
6546}
6547
6548 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006549cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006551 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552}
6553
6554 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006555cin_iselse(
6556 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558 if (*p == '}') /* accept "} else" */
6559 p = cin_skipcomment(p + 1);
6560 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6561}
6562
6563 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006564cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
6566 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6567}
6568
6569/*
6570 * Check if this is a "while" that should have a matching "do".
6571 * We only accept a "while (condition) ;", with only white space between the
6572 * ')' and ';'. The condition may be spread over several lines.
6573 */
6574 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006575cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 pos_T cursor_save;
6578 pos_T *trypos;
6579 int retval = FALSE;
6580
6581 p = cin_skipcomment(p);
6582 if (*p == '}') /* accept "} while (cond);" */
6583 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006584 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {
6586 cursor_save = curwin->w_cursor;
6587 curwin->w_cursor.lnum = lnum;
6588 curwin->w_cursor.col = 0;
6589 p = ml_get_curline();
6590 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6591 {
6592 ++p;
6593 ++curwin->w_cursor.col;
6594 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006595 if ((trypos = findmatchlimit(NULL, 0, 0,
6596 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6598 retval = TRUE;
6599 curwin->w_cursor = cursor_save;
6600 }
6601 return retval;
6602}
6603
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006604/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006605 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006606 * Return 0 if there is none.
6607 * Otherwise return !0 and update "*poffset" to point to the place where the
6608 * string was found.
6609 */
6610 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006611cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006612{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006613 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006614
6615 if (offset-- < 2)
6616 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006617 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006618 --offset;
6619
6620 offset -= 1;
6621 if (!STRNCMP(line + offset, "if", 2))
6622 goto probablyFound;
6623
6624 if (offset >= 1)
6625 {
6626 offset -= 1;
6627 if (!STRNCMP(line + offset, "for", 3))
6628 goto probablyFound;
6629
6630 if (offset >= 2)
6631 {
6632 offset -= 2;
6633 if (!STRNCMP(line + offset, "while", 5))
6634 goto probablyFound;
6635 }
6636 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006637 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006638
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006639probablyFound:
6640 if (!offset || !vim_isIDc(line[offset - 1]))
6641 {
6642 *poffset = offset;
6643 return 1;
6644 }
6645 return 0;
6646}
6647
6648/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006649 * Return TRUE if we are at the end of a do-while.
6650 * do
6651 * nothing;
6652 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006653 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006654 * Adjust the cursor to the line with "while".
6655 */
6656 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006657cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006658{
6659 char_u *line;
6660 char_u *p;
6661 char_u *s;
6662 pos_T *trypos;
6663 int i;
6664
6665 if (terminated != ';') /* there must be a ';' at the end */
6666 return FALSE;
6667
6668 p = line = ml_get_curline();
6669 while (*p != NUL)
6670 {
6671 p = cin_skipcomment(p);
6672 if (*p == ')')
6673 {
6674 s = skipwhite(p + 1);
6675 if (*s == ';' && cin_nocode(s + 1))
6676 {
6677 /* Found ");" at end of the line, now check there is "while"
6678 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006679 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006680 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006681 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006682 if (trypos != NULL)
6683 {
6684 s = cin_skipcomment(ml_get(trypos->lnum));
6685 if (*s == '}') /* accept "} while (cond);" */
6686 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006687 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006688 {
6689 curwin->w_cursor.lnum = trypos->lnum;
6690 return TRUE;
6691 }
6692 }
6693
6694 /* Searching may have made "line" invalid, get it again. */
6695 line = ml_get_curline();
6696 p = line + i;
6697 }
6698 }
6699 if (*p != NUL)
6700 ++p;
6701 }
6702 return FALSE;
6703}
6704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006706cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6709}
6710
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006711/*
6712 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 * constructor-initialization. eg:
6714 *
6715 * class MyClass :
6716 * baseClass <-- here
6717 * class MyClass : public baseClass,
6718 * anotherBaseClass <-- here (should probably lineup ??)
6719 * MyClass::MyClass(...) :
6720 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006721 *
6722 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 */
6724 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006725cin_is_cpp_baseclass(
6726 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006728 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 char_u *s;
6730 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006731 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006732 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006734 if (pos->lnum <= lnum)
6735 return cached->found; /* Use the cached result */
6736
6737 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006739 s = skipwhite(line);
6740 if (*s == '#') /* skip #define FOO x ? (x) : x */
6741 return FALSE;
6742 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 if (*s == NUL)
6744 return FALSE;
6745
6746 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6747
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006748 /* Search for a line starting with '#', empty, ending in ';' or containing
6749 * '{' or '}' and start below it. This handles the following situations:
6750 * a = cond ?
6751 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006752 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006753 * func::foo()
6754 * : something
6755 * {}
6756 * Foo::Foo (int one, int two)
6757 * : something(4),
6758 * somethingelse(3)
6759 * {}
6760 */
6761 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006763 line = ml_get(lnum - 1);
6764 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006765 if (*s == '#' || *s == NUL)
6766 break;
6767 while (*s != NUL)
6768 {
6769 s = cin_skipcomment(s);
6770 if (*s == '{' || *s == '}'
6771 || (*s == ';' && cin_nocode(s + 1)))
6772 break;
6773 if (*s != NUL)
6774 ++s;
6775 }
6776 if (*s != NUL)
6777 break;
6778 --lnum;
6779 }
6780
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006781 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006782 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006783 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006784 for (;;)
6785 {
6786 if (*s == NUL)
6787 {
6788 if (lnum == curwin->w_cursor.lnum)
6789 break;
6790 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006791 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006792 s = line;
6793 }
6794 if (s == line)
6795 {
6796 /* don't recognize "case (foo):" as a baseclass */
6797 if (cin_iscase(s, FALSE))
6798 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006799 s = cin_skipcomment(line);
6800 if (*s == NUL)
6801 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006802 }
6803
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006804 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006805 s = skip_string(s) + 1;
6806 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 if (s[1] == ':')
6809 {
6810 /* skip double colon. It can't be a constructor
6811 * initialization any more */
6812 lookfor_ctor_init = FALSE;
6813 s = cin_skipcomment(s + 2);
6814 }
6815 else if (lookfor_ctor_init || class_or_struct)
6816 {
6817 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006818 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 cpp_base_class = TRUE;
6820 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006821 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 s = cin_skipcomment(s + 1);
6823 }
6824 else
6825 s = cin_skipcomment(s + 1);
6826 }
6827 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6828 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6829 {
6830 class_or_struct = TRUE;
6831 lookfor_ctor_init = FALSE;
6832
6833 if (*s == 'c')
6834 s = cin_skipcomment(s + 5);
6835 else
6836 s = cin_skipcomment(s + 6);
6837 }
6838 else
6839 {
6840 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6841 {
6842 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6843 }
6844 else if (s[0] == ')')
6845 {
6846 /* Constructor-initialization is assumed if we come across
6847 * something like "):" */
6848 class_or_struct = FALSE;
6849 lookfor_ctor_init = TRUE;
6850 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006851 else if (s[0] == '?')
6852 {
6853 /* Avoid seeing '() :' after '?' as constructor init. */
6854 return FALSE;
6855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 else if (!vim_isIDc(s[0]))
6857 {
6858 /* if it is not an identifier, we are wrong */
6859 class_or_struct = FALSE;
6860 lookfor_ctor_init = FALSE;
6861 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006862 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
6864 /* it can't be a constructor-initialization any more */
6865 lookfor_ctor_init = FALSE;
6866
6867 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006868 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006869 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
6871
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006872 /* When the line ends in a comma don't align with it. */
6873 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006874 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 s = cin_skipcomment(s + 1);
6877 }
6878 }
6879
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006880 cached->found = cpp_base_class;
6881 if (cpp_base_class)
6882 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 return cpp_base_class;
6884}
6885
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006886 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006887get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006888{
6889 int amount;
6890 colnr_T vcol;
6891 pos_T *trypos;
6892
6893 if (col == 0)
6894 {
6895 amount = get_indent();
6896 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006897 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006898 amount = get_indent_lnum(trypos->lnum); /* XXX */
6899 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006900 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006901 }
6902 else
6903 {
6904 curwin->w_cursor.col = col;
6905 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6906 amount = (int)vcol;
6907 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006908 if (amount < curbuf->b_ind_cpp_baseclass)
6909 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006910 return amount;
6911}
6912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913/*
6914 * Return TRUE if string "s" ends with the string "find", possibly followed by
6915 * white space and comments. Skip strings and comments.
6916 * Ignore "ignore" after "find" if it's not NULL.
6917 */
6918 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006919cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 char_u *p = s;
6922 char_u *r;
6923 int len = (int)STRLEN(find);
6924
6925 while (*p != NUL)
6926 {
6927 p = cin_skipcomment(p);
6928 if (STRNCMP(p, find, len) == 0)
6929 {
6930 r = skipwhite(p + len);
6931 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6932 r = skipwhite(r + STRLEN(ignore));
6933 if (cin_nocode(r))
6934 return TRUE;
6935 }
6936 if (*p != NUL)
6937 ++p;
6938 }
6939 return FALSE;
6940}
6941
6942/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006943 * Return TRUE when "s" starts with "word" and then a non-ID character.
6944 */
6945 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006946cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006947{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006948 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006949
6950 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6951}
6952
6953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 * Skip strings, chars and comments until at or past "trypos".
6955 * Return the column found.
6956 */
6957 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006958cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 char_u *line;
6961 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006962 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964 p = line = ml_get(trypos->lnum);
6965 while (*p && (colnr_T)(p - line) < trypos->col)
6966 {
6967 if (cin_iscomment(p))
6968 p = cin_skipcomment(p);
6969 else
6970 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006971 new_p = skip_string(p);
6972 if (new_p == p)
6973 ++p;
6974 else
6975 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 }
6977 }
6978 return (int)(p - line);
6979}
6980
6981/*
6982 * Find the '{' at the start of the block we are in.
6983 * Return NULL if no match found.
6984 * Ignore a '{' that is in a comment, makes indenting the next three lines
6985 * work. */
6986/* foo() */
6987/* { */
6988/* } */
6989
6990 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006991find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 pos_T cursor_save;
6994 pos_T *trypos;
6995 pos_T *pos;
6996 static pos_T pos_copy;
6997
6998 cursor_save = curwin->w_cursor;
6999 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
7000 {
7001 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
7002 trypos = &pos_copy;
7003 curwin->w_cursor = *trypos;
7004 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007005 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02007007 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 break;
7009 if (pos != NULL)
7010 curwin->w_cursor.lnum = pos->lnum;
7011 }
7012 curwin->w_cursor = cursor_save;
7013 return trypos;
7014}
7015
7016/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007017 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007018 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 */
7020 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007021find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007023 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007024}
7025
7026 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007027find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007028{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 pos_T cursor_save;
7030 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007031 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007032 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007035 ind_maxp_wk = ind_maxparen;
7036retry:
7037 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 {
7039 /* check if the ( is in a // comment */
7040 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007041 {
7042 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7043 if (ind_maxp_wk > 0)
7044 {
7045 curwin->w_cursor = *trypos;
7046 curwin->w_cursor.col = 0; /* XXX */
7047 goto retry;
7048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 else
7052 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007053 pos_T *trypos_wk;
7054
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7056 trypos = &pos_copy;
7057 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007058 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007059 {
7060 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7061 - trypos_wk->lnum);
7062 if (ind_maxp_wk > 0)
7063 {
7064 curwin->w_cursor = *trypos_wk;
7065 goto retry;
7066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
7070 }
7071 curwin->w_cursor = cursor_save;
7072 return trypos;
7073}
7074
7075/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007076 * Find the matching '(', ignoring it if it is in a comment or before an
7077 * unmatched {.
7078 * Return NULL if no match found.
7079 */
7080 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007081find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007082{
7083 pos_T *trypos = find_match_paren(ind_maxparen);
7084
7085 if (trypos != NULL)
7086 {
7087 pos_T *tryposBrace = find_start_brace();
7088
7089 /* If both an unmatched '(' and '{' is found. Ignore the '('
7090 * position if the '{' is further down. */
7091 if (tryposBrace != NULL
7092 && (trypos->lnum != tryposBrace->lnum
7093 ? trypos->lnum < tryposBrace->lnum
7094 : trypos->col < tryposBrace->col))
7095 trypos = NULL;
7096 }
7097 return trypos;
7098}
7099
7100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 * Return ind_maxparen corrected for the difference in line number between the
7102 * cursor position and "startpos". This makes sure that searching for a
7103 * matching paren above the cursor line doesn't find a match because of
7104 * looking a few lines further.
7105 */
7106 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007107corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
7109 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7110
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007111 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7112 return curbuf->b_ind_maxparen - (int)n;
7113 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114}
7115
7116/*
7117 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007118 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 */
7120 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007121find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
7123 int i;
7124 int retval = FALSE;
7125 int open_count = 0;
7126
7127 curwin->w_cursor.col = 0; /* default is start of line */
7128
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007129 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {
7131 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7132 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7133 if (l[i] == start)
7134 ++open_count;
7135 else if (l[i] == end)
7136 {
7137 if (open_count > 0)
7138 --open_count;
7139 else
7140 {
7141 curwin->w_cursor.col = i;
7142 retval = TRUE;
7143 }
7144 }
7145 }
7146 return retval;
7147}
7148
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007149/*
7150 * Parse 'cinoptions' and set the values in "curbuf".
7151 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7152 */
7153 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007154parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007155{
7156 char_u *p;
7157 char_u *l;
7158 char_u *digits;
7159 int n;
7160 int divider;
7161 int fraction = 0;
7162 int sw = (int)get_sw_value(buf);
7163
7164 /*
7165 * Set the default values.
7166 */
7167 /* Spaces from a block's opening brace the prevailing indent for that
7168 * block should be. */
7169 buf->b_ind_level = sw;
7170
7171 /* Spaces from the edge of the line an open brace that's at the end of a
7172 * line is imagined to be. */
7173 buf->b_ind_open_imag = 0;
7174
7175 /* Spaces from the prevailing indent for a line that is not preceded by
7176 * an opening brace. */
7177 buf->b_ind_no_brace = 0;
7178
7179 /* Column where the first { of a function should be located }. */
7180 buf->b_ind_first_open = 0;
7181
7182 /* Spaces from the prevailing indent a leftmost open brace should be
7183 * located. */
7184 buf->b_ind_open_extra = 0;
7185
7186 /* Spaces from the matching open brace (real location for one at the left
7187 * edge; imaginary location from one that ends a line) the matching close
7188 * brace should be located. */
7189 buf->b_ind_close_extra = 0;
7190
7191 /* Spaces from the edge of the line an open brace sitting in the leftmost
7192 * column is imagined to be. */
7193 buf->b_ind_open_left_imag = 0;
7194
7195 /* Spaces jump labels should be shifted to the left if N is non-negative,
7196 * otherwise the jump label will be put to column 1. */
7197 buf->b_ind_jump_label = -1;
7198
7199 /* Spaces from the switch() indent a "case xx" label should be located. */
7200 buf->b_ind_case = sw;
7201
7202 /* Spaces from the "case xx:" code after a switch() should be located. */
7203 buf->b_ind_case_code = sw;
7204
7205 /* Lineup break at end of case in switch() with case label. */
7206 buf->b_ind_case_break = 0;
7207
7208 /* Spaces from the class declaration indent a scope declaration label
7209 * should be located. */
7210 buf->b_ind_scopedecl = sw;
7211
7212 /* Spaces from the scope declaration label code should be located. */
7213 buf->b_ind_scopedecl_code = sw;
7214
7215 /* Amount K&R-style parameters should be indented. */
7216 buf->b_ind_param = sw;
7217
7218 /* Amount a function type spec should be indented. */
7219 buf->b_ind_func_type = sw;
7220
7221 /* Amount a cpp base class declaration or constructor initialization
7222 * should be indented. */
7223 buf->b_ind_cpp_baseclass = sw;
7224
7225 /* additional spaces beyond the prevailing indent a continuation line
7226 * should be located. */
7227 buf->b_ind_continuation = sw;
7228
7229 /* Spaces from the indent of the line with an unclosed parentheses. */
7230 buf->b_ind_unclosed = sw * 2;
7231
7232 /* Spaces from the indent of the line with an unclosed parentheses, which
7233 * itself is also unclosed. */
7234 buf->b_ind_unclosed2 = sw;
7235
7236 /* Suppress ignoring spaces from the indent of a line starting with an
7237 * unclosed parentheses. */
7238 buf->b_ind_unclosed_noignore = 0;
7239
7240 /* If the opening paren is the last nonwhite character on the line, and
7241 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7242 * context (for very long lines). */
7243 buf->b_ind_unclosed_wrapped = 0;
7244
7245 /* Suppress ignoring white space when lining up with the character after
7246 * an unclosed parentheses. */
7247 buf->b_ind_unclosed_whiteok = 0;
7248
7249 /* Indent a closing parentheses under the line start of the matching
7250 * opening parentheses. */
7251 buf->b_ind_matching_paren = 0;
7252
7253 /* Indent a closing parentheses under the previous line. */
7254 buf->b_ind_paren_prev = 0;
7255
7256 /* Extra indent for comments. */
7257 buf->b_ind_comment = 0;
7258
7259 /* Spaces from the comment opener when there is nothing after it. */
7260 buf->b_ind_in_comment = 3;
7261
7262 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7263 * after the comment opener. */
7264 buf->b_ind_in_comment2 = 0;
7265
7266 /* Max lines to search for an open paren. */
7267 buf->b_ind_maxparen = 20;
7268
7269 /* Max lines to search for an open comment. */
7270 buf->b_ind_maxcomment = 70;
7271
7272 /* Handle braces for java code. */
7273 buf->b_ind_java = 0;
7274
7275 /* Not to confuse JS object properties with labels. */
7276 buf->b_ind_js = 0;
7277
7278 /* Handle blocked cases correctly. */
7279 buf->b_ind_keep_case_label = 0;
7280
7281 /* Handle C++ namespace. */
7282 buf->b_ind_cpp_namespace = 0;
7283
7284 /* Handle continuation lines containing conditions of if(), for() and
7285 * while(). */
7286 buf->b_ind_if_for_while = 0;
7287
Bram Moolenaar6b643942017-03-05 19:44:06 +01007288 /* indentation for # comments */
7289 buf->b_ind_hash_comment = 0;
7290
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007291 /* Handle C++ extern "C" or "C++" */
7292 buf->b_ind_cpp_extern_c = 0;
7293
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007294 for (p = buf->b_p_cino; *p; )
7295 {
7296 l = p++;
7297 if (*p == '-')
7298 ++p;
7299 digits = p; /* remember where the digits start */
7300 n = getdigits(&p);
7301 divider = 0;
7302 if (*p == '.') /* ".5s" means a fraction */
7303 {
7304 fraction = atol((char *)++p);
7305 while (VIM_ISDIGIT(*p))
7306 {
7307 ++p;
7308 if (divider)
7309 divider *= 10;
7310 else
7311 divider = 10;
7312 }
7313 }
7314 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7315 {
7316 if (p == digits)
7317 n = sw; /* just "s" is one 'shiftwidth' */
7318 else
7319 {
7320 n *= sw;
7321 if (divider)
7322 n += (sw * fraction + divider / 2) / divider;
7323 }
7324 ++p;
7325 }
7326 if (l[1] == '-')
7327 n = -n;
7328
7329 /* When adding an entry here, also update the default 'cinoptions' in
7330 * doc/indent.txt, and add explanation for it! */
7331 switch (*l)
7332 {
7333 case '>': buf->b_ind_level = n; break;
7334 case 'e': buf->b_ind_open_imag = n; break;
7335 case 'n': buf->b_ind_no_brace = n; break;
7336 case 'f': buf->b_ind_first_open = n; break;
7337 case '{': buf->b_ind_open_extra = n; break;
7338 case '}': buf->b_ind_close_extra = n; break;
7339 case '^': buf->b_ind_open_left_imag = n; break;
7340 case 'L': buf->b_ind_jump_label = n; break;
7341 case ':': buf->b_ind_case = n; break;
7342 case '=': buf->b_ind_case_code = n; break;
7343 case 'b': buf->b_ind_case_break = n; break;
7344 case 'p': buf->b_ind_param = n; break;
7345 case 't': buf->b_ind_func_type = n; break;
7346 case '/': buf->b_ind_comment = n; break;
7347 case 'c': buf->b_ind_in_comment = n; break;
7348 case 'C': buf->b_ind_in_comment2 = n; break;
7349 case 'i': buf->b_ind_cpp_baseclass = n; break;
7350 case '+': buf->b_ind_continuation = n; break;
7351 case '(': buf->b_ind_unclosed = n; break;
7352 case 'u': buf->b_ind_unclosed2 = n; break;
7353 case 'U': buf->b_ind_unclosed_noignore = n; break;
7354 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7355 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7356 case 'm': buf->b_ind_matching_paren = n; break;
7357 case 'M': buf->b_ind_paren_prev = n; break;
7358 case ')': buf->b_ind_maxparen = n; break;
7359 case '*': buf->b_ind_maxcomment = n; break;
7360 case 'g': buf->b_ind_scopedecl = n; break;
7361 case 'h': buf->b_ind_scopedecl_code = n; break;
7362 case 'j': buf->b_ind_java = n; break;
7363 case 'J': buf->b_ind_js = n; break;
7364 case 'l': buf->b_ind_keep_case_label = n; break;
7365 case '#': buf->b_ind_hash_comment = n; break;
7366 case 'N': buf->b_ind_cpp_namespace = n; break;
7367 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007368 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007369 }
7370 if (*p == ',')
7371 ++p;
7372 }
7373}
7374
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007375/*
7376 * Return the desired indent for C code.
7377 * Return -1 if the indent should be left alone (inside a raw string).
7378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007380get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 pos_T cur_curpos;
7383 int amount;
7384 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007385 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 colnr_T col;
7387 char_u *theline;
7388 char_u *linecopy;
7389 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007390 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007392 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 pos_T our_paren_pos;
7394 char_u *start;
7395 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007396#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397#define BRACE_AT_START 2 /* '{' is at start of line */
7398#define BRACE_AT_END 3 /* '{' is at end of line */
7399 linenr_T ourscope;
7400 char_u *l;
7401 char_u *look;
7402 char_u terminated;
7403 int lookfor;
7404#define LOOKFOR_INITIAL 0
7405#define LOOKFOR_IF 1
7406#define LOOKFOR_DO 2
7407#define LOOKFOR_CASE 3
7408#define LOOKFOR_ANY 4
7409#define LOOKFOR_TERM 5
7410#define LOOKFOR_UNTERM 6
7411#define LOOKFOR_SCOPEDECL 7
7412#define LOOKFOR_NOBREAK 8
7413#define LOOKFOR_CPP_BASECLASS 9
7414#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007415#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007416#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 int whilelevel;
7419 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 int n;
7421 int iscase;
7422 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007423 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007425 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007426 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007427 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007428 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007429 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007431 /* make a copy, value is changed below */
7432 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
7434 /* remember where the cursor was when we started */
7435 cur_curpos = curwin->w_cursor;
7436
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007437 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007438 if (cur_curpos.lnum == 1)
7439 return 0;
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 /* Get a copy of the current contents of the line.
7442 * This is required, because only the most recent line obtained with
7443 * ml_get is valid! */
7444 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7445 if (linecopy == NULL)
7446 return 0;
7447
7448 /*
7449 * In insert mode and the cursor is on a ')' truncate the line at the
7450 * cursor position. We don't want to line up with the matching '(' when
7451 * inserting new stuff.
7452 * For unknown reasons the cursor might be past the end of the line, thus
7453 * check for that.
7454 */
7455 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007456 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 && linecopy[curwin->w_cursor.col] == ')')
7458 linecopy[curwin->w_cursor.col] = NUL;
7459
7460 theline = skipwhite(linecopy);
7461
7462 /* move the cursor to the start of the line */
7463
7464 curwin->w_cursor.col = 0;
7465
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007466 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007467
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007469 * If we are inside a raw string don't change the indent.
7470 * Ignore a raw string inside a comment.
7471 */
7472 comment_pos = ind_find_start_comment();
7473 if (comment_pos != NULL)
7474 {
7475 /* findmatchlimit() static pos is overwritten, make a copy */
7476 tryposCopy = *comment_pos;
7477 comment_pos = &tryposCopy;
7478 }
7479 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007480 if (trypos != NULL && (comment_pos == NULL
7481 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007482 {
7483 amount = -1;
7484 goto laterend;
7485 }
7486
7487 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 * #defines and so on always go at the left when included in 'cinkeys'.
7489 */
7490 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007491 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007492 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007493 goto theend;
7494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
7496 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007497 * Is it a non-case label? Then that goes at the left margin too unless:
7498 * - JS flag is set.
7499 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007501 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007502 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 {
7504 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007505 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 }
7507
7508 /*
7509 * If we're inside a "//" comment and there is a "//" comment in a
7510 * previous line, lineup with that one.
7511 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007512 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 && (trypos = find_line_comment()) != NULL) /* XXX */
7514 {
7515 /* find how indented the line beginning the comment is */
7516 getvcol(curwin, trypos, &col, NULL, NULL);
7517 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007518 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 }
7520
7521 /*
7522 * If we're inside a comment and not looking at the start of the
7523 * comment, try using the 'comments' option.
7524 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007525 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 {
7527 int lead_start_len = 2;
7528 int lead_middle_len = 1;
7529 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7530 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7531 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7532 char_u *p;
7533 int start_align = 0;
7534 int start_off = 0;
7535 int done = FALSE;
7536
7537 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007538 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007540 *lead_start = NUL;
7541 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542
7543 p = curbuf->b_p_com;
7544 while (*p != NUL)
7545 {
7546 int align = 0;
7547 int off = 0;
7548 int what = 0;
7549
7550 while (*p != NUL && *p != ':')
7551 {
7552 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7553 what = *p++;
7554 else if (*p == COM_LEFT || *p == COM_RIGHT)
7555 align = *p++;
7556 else if (VIM_ISDIGIT(*p) || *p == '-')
7557 off = getdigits(&p);
7558 else
7559 ++p;
7560 }
7561
7562 if (*p == ':')
7563 ++p;
7564 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7565 if (what == COM_START)
7566 {
7567 STRCPY(lead_start, lead_end);
7568 lead_start_len = (int)STRLEN(lead_start);
7569 start_off = off;
7570 start_align = align;
7571 }
7572 else if (what == COM_MIDDLE)
7573 {
7574 STRCPY(lead_middle, lead_end);
7575 lead_middle_len = (int)STRLEN(lead_middle);
7576 }
7577 else if (what == COM_END)
7578 {
7579 /* If our line starts with the middle comment string, line it
7580 * up with the comment opener per the 'comments' option. */
7581 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7582 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7583 {
7584 done = TRUE;
7585 if (curwin->w_cursor.lnum > 1)
7586 {
7587 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007588 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 * the middle comment string matches in the previous
7590 * line, use the indent of that line. XXX */
7591 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7592 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7593 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7594 else if (STRNCMP(look, lead_middle,
7595 lead_middle_len) == 0)
7596 {
7597 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7598 break;
7599 }
7600 /* If the start comment string doesn't match with the
7601 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007602 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 lead_start, lead_start_len) != 0)
7604 continue;
7605 }
7606 if (start_off != 0)
7607 amount += start_off;
7608 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007609 amount += vim_strsize(lead_start)
7610 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 break;
7612 }
7613
7614 /* If our line starts with the end comment string, line it up
7615 * with the middle comment */
7616 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7617 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7618 {
7619 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7620 /* XXX */
7621 if (off != 0)
7622 amount += off;
7623 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007624 amount += vim_strsize(lead_start)
7625 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 done = TRUE;
7627 break;
7628 }
7629 }
7630 }
7631
7632 /* If our line starts with an asterisk, line up with the
7633 * asterisk in the comment opener; otherwise, line up
7634 * with the first character of the comment text.
7635 */
7636 if (done)
7637 ;
7638 else if (theline[0] == '*')
7639 amount += 1;
7640 else
7641 {
7642 /*
7643 * If we are more than one line away from the comment opener, take
7644 * the indent of the previous non-empty line. If 'cino' has "CO"
7645 * and we are just below the comment opener and there are any
7646 * white characters after it line up with the text after it;
7647 * otherwise, add the amount specified by "c" in 'cino'
7648 */
7649 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007650 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
7652 if (linewhite(lnum)) /* skip blank lines */
7653 continue;
7654 amount = get_indent_lnum(lnum); /* XXX */
7655 break;
7656 }
7657 if (amount == -1) /* use the comment opener */
7658 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007659 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007661 start = ml_get(comment_pos->lnum);
7662 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007664 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007666 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007668 if (curbuf->b_ind_in_comment2 || *look == NUL)
7669 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007672 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 }
7674
7675 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007676 * Are we looking at a ']' that has a match?
7677 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007678 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007679 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7680 {
7681 /* align with the line containing the '['. */
7682 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007683 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007684 }
7685
7686 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 * Are we inside parentheses or braces?
7688 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007689 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007690 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007691 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 || trypos != NULL)
7693 {
7694 if (trypos != NULL && tryposBrace != NULL)
7695 {
7696 /* Both an unmatched '(' and '{' is found. Use the one which is
7697 * closer to the current cursor position, set the other to NULL. */
7698 if (trypos->lnum != tryposBrace->lnum
7699 ? trypos->lnum < tryposBrace->lnum
7700 : trypos->col < tryposBrace->col)
7701 trypos = NULL;
7702 else
7703 tryposBrace = NULL;
7704 }
7705
7706 if (trypos != NULL)
7707 {
7708 /*
7709 * If the matching paren is more than one line away, use the indent of
7710 * a previous non-empty line that matches the same paren.
7711 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007712 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007714 /* Line up with the start of the matching paren line. */
7715 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7716 }
7717 else
7718 {
7719 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007720 our_paren_pos = *trypos;
7721 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007723 l = skipwhite(ml_get(lnum));
7724 if (cin_nocode(l)) /* skip comment lines */
7725 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007726 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007727 continue; /* ignore #define, #if, etc. */
7728 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007730 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007731 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007732 {
7733 lnum = trypos->lnum + 1;
7734 continue;
7735 }
7736
7737 /* XXX */
7738 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007739 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007740 && trypos->lnum == our_paren_pos.lnum
7741 && trypos->col == our_paren_pos.col)
7742 {
7743 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007745 if (theline[0] == ')')
7746 {
7747 if (our_paren_pos.lnum != lnum
7748 && cur_amount > amount)
7749 cur_amount = amount;
7750 amount = -1;
7751 }
7752 break;
7753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755 }
7756
7757 /*
7758 * Line up with line where the matching paren is. XXX
7759 * If the line starts with a '(' or the indent for unclosed
7760 * parentheses is zero, line up with the unclosed parentheses.
7761 */
7762 if (amount == -1)
7763 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007764 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007765 int is_if_for_while = 0;
7766
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007767 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007768 {
7769 /* Look for the outermost opening parenthesis on this line
7770 * and check whether it belongs to an "if", "for" or "while". */
7771
7772 pos_T cursor_save = curwin->w_cursor;
7773 pos_T outermost;
7774 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007775
7776 trypos = &our_paren_pos;
7777 do {
7778 outermost = *trypos;
7779 curwin->w_cursor.lnum = outermost.lnum;
7780 curwin->w_cursor.col = outermost.col;
7781
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007782 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007783 } while (trypos && trypos->lnum == outermost.lnum);
7784
7785 curwin->w_cursor = cursor_save;
7786
7787 line = ml_get(outermost.lnum);
7788
7789 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007790 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007791 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007792
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007793 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007794 look = skipwhite(look);
7795 if (*look == '(')
7796 {
7797 linenr_T save_lnum = curwin->w_cursor.lnum;
7798 char_u *line;
7799 int look_col;
7800
7801 /* Ignore a '(' in front of the line that has a match before
7802 * our matching '('. */
7803 curwin->w_cursor.lnum = our_paren_pos.lnum;
7804 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007805 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007806 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007807 if ((trypos = findmatchlimit(NULL, ')', 0,
7808 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007809 != NULL
7810 && trypos->lnum == our_paren_pos.lnum
7811 && trypos->col < our_paren_pos.col)
7812 ignore_paren_col = trypos->col + 1;
7813
7814 curwin->w_cursor.lnum = save_lnum;
7815 look = ml_get(our_paren_pos.lnum) + look_col;
7816 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007817 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7818 && is_if_for_while == 0)
7819 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007820 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {
7822 /*
7823 * If we're looking at a close paren, line up right there;
7824 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007825 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 * the last nonwhite character of the line, use either the
7827 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007828 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 * lines).
7830 */
7831 if (theline[0] != ')')
7832 {
7833 cur_amount = MAXCOL;
7834 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007835 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 && cin_ends_in(l, (char_u *)"(", NULL))
7837 {
7838 /* look for opening unmatched paren, indent one level
7839 * for each additional level */
7840 n = 1;
7841 for (col = 0; col < our_paren_pos.col; ++col)
7842 {
7843 switch (l[col])
7844 {
7845 case '(':
7846 case '{': ++n;
7847 break;
7848
7849 case ')':
7850 case '}': if (n > 1)
7851 --n;
7852 break;
7853 }
7854 }
7855
7856 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007857 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007859 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 our_paren_pos.col++;
7861 else
7862 {
7863 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007864 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 col++;
7866 if (l[col] != NUL) /* In case of trailing space */
7867 our_paren_pos.col = col;
7868 else
7869 our_paren_pos.col++;
7870 }
7871 }
7872
7873 /*
7874 * Find how indented the paren is, or the character after it
7875 * if we did the above "if".
7876 */
7877 if (our_paren_pos.col > 0)
7878 {
7879 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7880 if (cur_amount > (int)col)
7881 cur_amount = col;
7882 }
7883 }
7884
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007885 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 /* Line up with the start of the matching paren line. */
7888 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007889 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7890 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007891 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
7893 if (cur_amount != MAXCOL)
7894 amount = cur_amount;
7895 }
7896 else
7897 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007898 /* Add b_ind_unclosed2 for each '(' before our matching one,
7899 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007901 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {
7903 --our_paren_pos.col;
7904 switch (*ml_get_pos(&our_paren_pos))
7905 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007906 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 col = our_paren_pos.col;
7908 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007909 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 col = MAXCOL;
7911 break;
7912 }
7913 }
7914
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007915 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 * braces */
7917 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007918 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 else
7920 {
7921 curwin->w_cursor.lnum = our_paren_pos.lnum;
7922 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007923 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7924 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007925 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007927 {
7928 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007929 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007930 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007931 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 }
7934 /*
7935 * For a line starting with ')' use the minimum of the two
7936 * positions, to avoid giving it more indent than the previous
7937 * lines:
7938 * func_long_name( if (x
7939 * arg && yy
7940 * ) ^ not here ) ^ not here
7941 */
7942 if (cur_amount < amount)
7943 amount = cur_amount;
7944 }
7945 }
7946
7947 /* add extra indent for a comment */
7948 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007949 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 else
7952 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007953 /*
7954 * We are inside braces, there is a { before this line at the position
7955 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007956 * Make a copy of tryposBrace, it may point to pos_copy inside
7957 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007958 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007959 tryposCopy = *tryposBrace;
7960 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 ourscope = trypos->lnum;
7963 start = ml_get(ourscope);
7964
7965 /*
7966 * Now figure out how indented the line is in general.
7967 * If the brace was at the start of the line, we use that;
7968 * otherwise, check out the indentation of the line as
7969 * a whole and then add the "imaginary indent" to that.
7970 */
7971 look = skipwhite(start);
7972 if (*look == '{')
7973 {
7974 getvcol(curwin, trypos, &col, NULL, NULL);
7975 amount = col;
7976 if (*start == '{')
7977 start_brace = BRACE_IN_COL0;
7978 else
7979 start_brace = BRACE_AT_START;
7980 }
7981 else
7982 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007983 /* That opening brace might have been on a continuation
7984 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 curwin->w_cursor.lnum = ourscope;
7986
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007987 /* Position the cursor over the rightmost paren, so that
7988 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 lnum = ourscope;
7990 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007991 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7992 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 lnum = trypos->lnum;
7994
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007995 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 * case 1: if (asdf &&
7997 * ldfd) {
7998 * }
7999 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008000 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
8001 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008003 else if (curbuf->b_ind_js)
8004 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008006 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007
8008 start_brace = BRACE_AT_END;
8009 }
8010
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008011 /* For Javascript check if the line starts with "key:". */
8012 if (curbuf->b_ind_js)
8013 js_cur_has_key = cin_has_js_key(theline);
8014
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008016 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 * we want to be. otherwise, add the amount of room
8018 * that an indent is supposed to be.
8019 */
8020 if (theline[0] == '}')
8021 {
8022 /*
8023 * they may want closing braces to line up with something
8024 * other than the open brace. indulge them, if so.
8025 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008026 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 }
8028 else
8029 {
8030 /*
8031 * If we're looking at an "else", try to find an "if"
8032 * to match it with.
8033 * If we're looking at a "while", try to find a "do"
8034 * to match it with.
8035 */
8036 lookfor = LOOKFOR_INITIAL;
8037 if (cin_iselse(theline))
8038 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008039 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 lookfor = LOOKFOR_DO;
8041 if (lookfor != LOOKFOR_INITIAL)
8042 {
8043 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008044 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {
8046 amount = get_indent(); /* XXX */
8047 goto theend;
8048 }
8049 }
8050
8051 /*
8052 * We get here if we are not on an "while-of-do" or "else" (or
8053 * failed to find a matching "if").
8054 * Search backwards for something to line up with.
8055 * First set amount for when we don't find anything.
8056 */
8057
8058 /*
8059 * if the '{' is _really_ at the left margin, use the imaginary
8060 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008061 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 */
8063
8064 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8065 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008066 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008067 lookfor_cpp_namespace = TRUE;
8068 }
8069 else if (start_brace == BRACE_AT_START &&
8070 lookfor_cpp_namespace) /* '{' is at start */
8071 {
8072
8073 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 }
8075 else
8076 {
8077 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008078 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008079 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008080
8081 l = skipwhite(ml_get_curline());
8082 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008083 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008084 else if (cin_is_cpp_extern_c(l))
8085 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 else
8088 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008089 /* Compensate for adding b_ind_open_extra later. */
8090 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 if (amount < 0)
8092 amount = 0;
8093 }
8094 }
8095
8096 lookfor_break = FALSE;
8097
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008098 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {
8100 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008101 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103 else if (cin_isscopedecl(theline)) /* private:, ... */
8104 {
8105 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008106 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108 else
8109 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008110 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8111 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 lookfor_break = TRUE;
8113
8114 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008115 /* b_ind_level from start of block */
8116 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 }
8118 scope_amount = amount;
8119 whilelevel = 0;
8120
8121 /*
8122 * Search backwards. If we find something we recognize, line up
8123 * with that.
8124 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008125 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 * the usual amount relative to the conditional
8127 * that opens the block.
8128 */
8129 curwin->w_cursor = cur_curpos;
8130 for (;;)
8131 {
8132 curwin->w_cursor.lnum--;
8133 curwin->w_cursor.col = 0;
8134
8135 /*
8136 * If we went all the way back to the start of our scope, line
8137 * up with it.
8138 */
8139 if (curwin->w_cursor.lnum <= ourscope)
8140 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008141 /* We reached end of scope:
8142 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008144 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 * don't add ind_continuation, otherwise it is a variable
8146 * declaration:
8147 * int x,
8148 * here; <-- add ind_continuation
8149 */
8150 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8151 {
8152 if (curwin->w_cursor.lnum == 0
8153 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008154 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008156 /* nothing found (abuse curbuf->b_ind_maxparen as
8157 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 * initialization) */
8159 if (cont_amount > 0)
8160 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008161 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 amount += ind_continuation;
8163 break;
8164 }
8165
8166 l = ml_get_curline();
8167
8168 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008169 * If we're in a comment or raw string now, skip to
8170 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008172 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 if (trypos != NULL)
8174 {
8175 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008176 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 continue;
8178 }
8179
8180 /*
8181 * Skip preprocessor directives and blank lines.
8182 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008183 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8184 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 continue;
8186
8187 if (cin_nocode(l))
8188 continue;
8189
8190 terminated = cin_isterminated(l, FALSE, TRUE);
8191
8192 /*
8193 * If we are at top level and the line looks like a
8194 * function declaration, we are done
8195 * (it's a variable declaration).
8196 */
8197 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008198 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
8200 /* if the line is terminated with another ','
8201 * it is a continued variable initialization.
8202 * don't add extra indent.
8203 * TODO: does not work, if a function
8204 * declaration is split over multiple lines:
8205 * cin_isfuncdecl returns FALSE then.
8206 */
8207 if (terminated == ',')
8208 break;
8209
8210 /* if it es a enum declaration or an assignment,
8211 * we are done.
8212 */
8213 if (terminated != ';' && cin_isinit())
8214 break;
8215
8216 /* nothing useful found */
8217 if (terminated == 0 || terminated == '{')
8218 continue;
8219 }
8220
8221 if (terminated != ';')
8222 {
8223 /* Skip parens and braces. Position the cursor
8224 * over the rightmost paren, so that matching it
8225 * will take us back to the start of the line.
8226 */ /* XXX */
8227 trypos = NULL;
8228 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008229 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008230 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
8232 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008233 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
8235 if (trypos != NULL)
8236 {
8237 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008238 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 continue;
8240 }
8241 }
8242
8243 /* it's a variable declaration, add indentation
8244 * like in
8245 * int a,
8246 * b;
8247 */
8248 if (cont_amount > 0)
8249 amount = cont_amount;
8250 else
8251 amount += ind_continuation;
8252 }
8253 else if (lookfor == LOOKFOR_UNTERM)
8254 {
8255 if (cont_amount > 0)
8256 amount = cont_amount;
8257 else
8258 amount += ind_continuation;
8259 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008260 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008261 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008262 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008263 && lookfor != LOOKFOR_CPP_BASECLASS
8264 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008265 {
8266 amount = scope_amount;
8267 if (theline[0] == '{')
8268 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008269 amount += curbuf->b_ind_open_extra;
8270 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008271 }
8272 }
8273
8274 if (lookfor_cpp_namespace)
8275 {
8276 /*
8277 * Looking for C++ namespace, need to look further
8278 * back.
8279 */
8280 if (curwin->w_cursor.lnum == ourscope)
8281 continue;
8282
8283 if (curwin->w_cursor.lnum == 0
8284 || curwin->w_cursor.lnum
8285 < ourscope - FIND_NAMESPACE_LIM)
8286 break;
8287
8288 l = ml_get_curline();
8289
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008290 /* If we're in a comment or raw string now, skip
8291 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008292 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008293 if (trypos != NULL)
8294 {
8295 curwin->w_cursor.lnum = trypos->lnum + 1;
8296 curwin->w_cursor.col = 0;
8297 continue;
8298 }
8299
8300 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008301 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8302 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008303 continue;
8304
8305 /* Finally the actual check for "namespace". */
8306 if (cin_is_cpp_namespace(l))
8307 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008308 amount += curbuf->b_ind_cpp_namespace
8309 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008310 break;
8311 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008312 else if (cin_is_cpp_extern_c(l))
8313 {
8314 amount += curbuf->b_ind_cpp_extern_c
8315 - added_to_amount;
8316 break;
8317 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008318
8319 if (cin_nocode(l))
8320 continue;
8321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 }
8323 break;
8324 }
8325
8326 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008327 * If we're in a comment or raw string now, skip to the start
8328 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008330 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {
8332 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008333 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 continue;
8335 }
8336
8337 l = ml_get_curline();
8338
8339 /*
8340 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008341 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008343 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 if (iscase || cin_isscopedecl(l))
8345 {
8346 /* we are only looking for cpp base class
8347 * declaration/initialization any longer */
8348 if (lookfor == LOOKFOR_CPP_BASECLASS)
8349 break;
8350
8351 /* When looking for a "do" we are not interested in
8352 * labels. */
8353 if (whilelevel > 0)
8354 continue;
8355
8356 /*
8357 * case xx:
8358 * c = 99 + <- this indent plus continuation
8359 *-> here;
8360 */
8361 if (lookfor == LOOKFOR_UNTERM
8362 || lookfor == LOOKFOR_ENUM_OR_INIT)
8363 {
8364 if (cont_amount > 0)
8365 amount = cont_amount;
8366 else
8367 amount += ind_continuation;
8368 break;
8369 }
8370
8371 /*
8372 * case xx: <- line up with this case
8373 * x = 333;
8374 * case yy:
8375 */
8376 if ( (iscase && lookfor == LOOKFOR_CASE)
8377 || (iscase && lookfor_break)
8378 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8379 {
8380 /*
8381 * Check that this case label is not for another
8382 * switch()
8383 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008384 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008385 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {
8387 amount = get_indent(); /* XXX */
8388 break;
8389 }
8390 continue;
8391 }
8392
8393 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8394
8395 /*
8396 * case xx: if (cond) <- line up with this if
8397 * y = y + 1;
8398 * -> s = 99;
8399 *
8400 * case xx:
8401 * if (cond) <- line up with this line
8402 * y = y + 1;
8403 * -> s = 99;
8404 */
8405 if (lookfor == LOOKFOR_TERM)
8406 {
8407 if (n)
8408 amount = n;
8409
8410 if (!lookfor_break)
8411 break;
8412 }
8413
8414 /*
8415 * case xx: x = x + 1; <- line up with this x
8416 * -> y = y + 1;
8417 *
8418 * case xx: if (cond) <- line up with this if
8419 * -> y = y + 1;
8420 */
8421 if (n)
8422 {
8423 amount = n;
8424 l = after_label(ml_get_curline());
8425 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008426 {
8427 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008428 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008429 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008430 amount += curbuf->b_ind_level
8431 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 break;
8434 }
8435
8436 /*
8437 * Try to get the indent of a statement before the switch
8438 * label. If nothing is found, line up relative to the
8439 * switch label.
8440 * break; <- may line up with this line
8441 * case xx:
8442 * -> y = 1;
8443 */
8444 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008445 ? curbuf->b_ind_case_code
8446 : curbuf->b_ind_scopedecl_code);
8447 lookfor = curbuf->b_ind_case_break
8448 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 continue;
8450 }
8451
8452 /*
8453 * Looking for a switch() label or C++ scope declaration,
8454 * ignore other lines, skip {}-blocks.
8455 */
8456 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8457 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008458 if (find_last_paren(l, '{', '}')
8459 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008462 curwin->w_cursor.col = 0;
8463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 continue;
8465 }
8466
8467 /*
8468 * Ignore jump labels with nothing after them.
8469 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008470 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
8472 l = after_label(ml_get_curline());
8473 if (l == NULL || cin_nocode(l))
8474 continue;
8475 }
8476
8477 /*
8478 * Ignore #defines, #if, etc.
8479 * Ignore comment and empty lines.
8480 * (need to get the line again, cin_islabel() may have
8481 * unlocked it)
8482 */
8483 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008484 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 || cin_nocode(l))
8486 continue;
8487
8488 /*
8489 * Are we at the start of a cpp base class declaration or
8490 * constructor initialization?
8491 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008492 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008493 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008494 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008495 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008496 l = ml_get_curline();
8497 }
8498 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 {
8500 if (lookfor == LOOKFOR_UNTERM)
8501 {
8502 if (cont_amount > 0)
8503 amount = cont_amount;
8504 else
8505 amount += ind_continuation;
8506 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008507 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008509 /* Need to find start of the declaration. */
8510 lookfor = LOOKFOR_UNTERM;
8511 ind_continuation = 0;
8512 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 }
8514 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008515 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008516 amount = get_baseclass_amount(
8517 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 break;
8519 }
8520 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8521 {
8522 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008523 * declaration or initialization before the opening brace.
8524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 if (cin_isterminated(l, TRUE, FALSE))
8526 break;
8527 else
8528 continue;
8529 }
8530
8531 /*
8532 * What happens next depends on the line being terminated.
8533 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008534 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 * 123,
8536 * sizeof
8537 * here
8538 * Otherwise check whether it is a enumeration or structure
8539 * initialisation (not indented) or a variable declaration
8540 * (indented).
8541 */
8542 terminated = cin_isterminated(l, FALSE, TRUE);
8543
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008544 if (js_cur_has_key)
8545 {
8546 js_cur_has_key = 0; /* only check the first line */
8547 if (curbuf->b_ind_js && terminated == ',')
8548 {
8549 /* For Javascript we might be inside an object:
8550 * key: something, <- align with this
8551 * key: something
8552 * or:
8553 * key: something + <- align with this
8554 * something,
8555 * key: something
8556 */
8557 lookfor = LOOKFOR_JS_KEY;
8558 }
8559 }
8560 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8561 {
8562 amount = get_indent();
8563 break;
8564 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008565 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008566 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008567 if (tryposBrace != NULL && tryposBrace->lnum
8568 >= curwin->w_cursor.lnum)
8569 break;
8570 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008571 /* line below current line is the one that starts a
8572 * (possibly broken) line ending in a comma. */
8573 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008574 else
8575 {
8576 amount = get_indent();
8577 if (curwin->w_cursor.lnum - 1 == ourscope)
8578 /* line above is start of the scope, thus current
8579 * line is the one that stars a (possibly broken)
8580 * line ending in a comma. */
8581 break;
8582 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008583 }
8584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8586 && terminated == ','))
8587 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008588 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8589 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008590 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 /*
8592 * if we're in the middle of a paren thing,
8593 * go back to the line that starts it so
8594 * we can get the right prevailing indent
8595 * if ( foo &&
8596 * bar )
8597 */
8598 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008599 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008601 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 */
8603 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008604 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008605 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8606 || (trypos->lnum == tryposBrace->lnum
8607 && trypos->col < tryposBrace->col)))
8608 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
8610 /*
8611 * If we are looking for ',', we also look for matching
8612 * braces.
8613 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008614 if (trypos == NULL && terminated == ','
8615 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008616 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617
8618 if (trypos != NULL)
8619 {
8620 /*
8621 * Check if we are on a case label now. This is
8622 * handled above.
8623 * case xx: if ( asdf &&
8624 * asdf)
8625 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008626 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008628 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {
8630 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008631 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 continue;
8633 }
8634 }
8635
8636 /*
8637 * Skip over continuation lines to find the one to get the
8638 * indent from
8639 * char *usethis = "bla\
8640 * bla",
8641 * here;
8642 */
8643 if (terminated == ',')
8644 {
8645 while (curwin->w_cursor.lnum > 1)
8646 {
8647 l = ml_get(curwin->w_cursor.lnum - 1);
8648 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8649 break;
8650 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008651 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 }
8653 }
8654
8655 /*
8656 * Get indent and pointer to text for current line,
8657 * ignoring any jump label. XXX
8658 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008659 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008660 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008661 else
8662 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 /*
8664 * If this is just above the line we are indenting, and it
8665 * starts with a '{', line it up with this line.
8666 * while (not)
8667 * -> {
8668 * }
8669 */
8670 if (terminated != ',' && lookfor != LOOKFOR_TERM
8671 && theline[0] == '{')
8672 {
8673 amount = cur_amount;
8674 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008675 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 * doesn't start with a '{', which must have a match
8677 * in the same line (scope is the same). Probably:
8678 * { 1, 2 },
8679 * -> { 3, 4 }
8680 */
8681 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008682 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008684 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {
8686 /* have to look back, whether it is a cpp base
8687 * class declaration or initialization */
8688 lookfor = LOOKFOR_CPP_BASECLASS;
8689 continue;
8690 }
8691 break;
8692 }
8693
8694 /*
8695 * Check if we are after an "if", "while", etc.
8696 * Also allow " } else".
8697 */
8698 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8699 {
8700 /*
8701 * Found an unterminated line after an if (), line up
8702 * with the last one.
8703 * if (cond)
8704 * 100 +
8705 * -> here;
8706 */
8707 if (lookfor == LOOKFOR_UNTERM
8708 || lookfor == LOOKFOR_ENUM_OR_INIT)
8709 {
8710 if (cont_amount > 0)
8711 amount = cont_amount;
8712 else
8713 amount += ind_continuation;
8714 break;
8715 }
8716
8717 /*
8718 * If this is just above the line we are indenting, we
8719 * are finished.
8720 * while (not)
8721 * -> here;
8722 * Otherwise this indent can be used when the line
8723 * before this is terminated.
8724 * yyy;
8725 * if (stat)
8726 * while (not)
8727 * xxx;
8728 * -> here;
8729 */
8730 amount = cur_amount;
8731 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008732 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 if (lookfor != LOOKFOR_TERM)
8734 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008735 amount += curbuf->b_ind_level
8736 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 break;
8738 }
8739
8740 /*
8741 * Special trick: when expecting the while () after a
8742 * do, line up with the while()
8743 * do
8744 * x = 1;
8745 * -> here
8746 */
8747 l = skipwhite(ml_get_curline());
8748 if (cin_isdo(l))
8749 {
8750 if (whilelevel == 0)
8751 break;
8752 --whilelevel;
8753 }
8754
8755 /*
8756 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008757 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 * Need to use the scope of this "else". XXX
8759 * If whilelevel != 0 continue looking for a "do {".
8760 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008761 if (cin_iselse(l) && whilelevel == 0)
8762 {
8763 /* If we're looking at "} else", let's make sure we
8764 * find the opening brace of the enclosing scope,
8765 * not the one from "if () {". */
8766 if (*l == '}')
8767 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008768 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008769
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008770 if ((trypos = find_start_brace()) == NULL
8771 || find_match(LOOKFOR_IF, trypos->lnum)
8772 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008773 break;
8774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 }
8776
8777 /*
8778 * If we're below an unterminated line that is not an
8779 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008780 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 * the line before this one.
8782 */
8783 else
8784 {
8785 /*
8786 * Found two unterminated lines on a row, line up with
8787 * the last one.
8788 * c = 99 +
8789 * 100 +
8790 * -> here;
8791 */
8792 if (lookfor == LOOKFOR_UNTERM)
8793 {
8794 /* When line ends in a comma add extra indent */
8795 if (terminated == ',')
8796 amount += ind_continuation;
8797 break;
8798 }
8799
8800 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8801 {
8802 /* Found two lines ending in ',', lineup with the
8803 * lowest one, but check for cpp base class
8804 * declaration/initialization, if it is an
8805 * opening brace or we are looking just for
8806 * enumerations/initializations. */
8807 if (terminated == ',')
8808 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008809 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 break;
8811
8812 lookfor = LOOKFOR_CPP_BASECLASS;
8813 continue;
8814 }
8815
8816 /* Ignore unterminated lines in between, but
8817 * reduce indent. */
8818 if (amount > cur_amount)
8819 amount = cur_amount;
8820 }
8821 else
8822 {
8823 /*
8824 * Found first unterminated line on a row, may
8825 * line up with this line, remember its indent
8826 * 100 +
8827 * -> here;
8828 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008829 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008831
8832 n = (int)STRLEN(l);
8833 if (terminated == ',' && (*skipwhite(l) == ']'
8834 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
8837 /*
8838 * If previous line ends in ',', check whether we
8839 * are in an initialization or enum
8840 * struct xxx =
8841 * {
8842 * sizeof a,
8843 * 124 };
8844 * or a normal possible continuation line.
8845 * but only, of no other statement has been found
8846 * yet.
8847 */
8848 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8849 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008850 if (curbuf->b_ind_js)
8851 {
8852 /* Search for a line ending in a comma
8853 * and line up with the line below it
8854 * (could be the current line).
8855 * some = [
8856 * 1, <- line up here
8857 * 2,
8858 * some = [
8859 * 3 + <- line up here
8860 * 4 *
8861 * 5,
8862 * 6,
8863 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008864 if (cin_iscomment(skipwhite(l)))
8865 break;
8866 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008867 trypos = find_match_char('[',
8868 curbuf->b_ind_maxparen);
8869 if (trypos != NULL)
8870 {
8871 if (trypos->lnum
8872 == curwin->w_cursor.lnum - 1)
8873 {
8874 /* Current line is first inside
8875 * [], line up with it. */
8876 break;
8877 }
8878 ourscope = trypos->lnum;
8879 }
8880 }
8881 else
8882 {
8883 lookfor = LOOKFOR_ENUM_OR_INIT;
8884 cont_amount = cin_first_id_amount();
8885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 }
8887 else
8888 {
8889 if (lookfor == LOOKFOR_INITIAL
8890 && *l != NUL
8891 && l[STRLEN(l) - 1] == '\\')
8892 /* XXX */
8893 cont_amount = cin_get_equal_amount(
8894 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008895 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008896 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008897 && lookfor != LOOKFOR_COMMA
8898 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 lookfor = LOOKFOR_UNTERM;
8900 }
8901 }
8902 }
8903 }
8904
8905 /*
8906 * Check if we are after a while (cond);
8907 * If so: Ignore until the matching "do".
8908 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008909 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
8911 /*
8912 * Found an unterminated line after a while ();, line up
8913 * with the last one.
8914 * while (cond);
8915 * 100 + <- line up with this one
8916 * -> here;
8917 */
8918 if (lookfor == LOOKFOR_UNTERM
8919 || lookfor == LOOKFOR_ENUM_OR_INIT)
8920 {
8921 if (cont_amount > 0)
8922 amount = cont_amount;
8923 else
8924 amount += ind_continuation;
8925 break;
8926 }
8927
8928 if (whilelevel == 0)
8929 {
8930 lookfor = LOOKFOR_TERM;
8931 amount = get_indent(); /* XXX */
8932 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008933 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 }
8935 ++whilelevel;
8936 }
8937
8938 /*
8939 * We are after a "normal" statement.
8940 * If we had another statement we can stop now and use the
8941 * indent of that other statement.
8942 * Otherwise the indent of the current statement may be used,
8943 * search backwards for the next "normal" statement.
8944 */
8945 else
8946 {
8947 /*
8948 * Skip single break line, if before a switch label. It
8949 * may be lined up with the case label.
8950 */
8951 if (lookfor == LOOKFOR_NOBREAK
8952 && cin_isbreak(skipwhite(ml_get_curline())))
8953 {
8954 lookfor = LOOKFOR_ANY;
8955 continue;
8956 }
8957
8958 /*
8959 * Handle "do {" line.
8960 */
8961 if (whilelevel > 0)
8962 {
8963 l = cin_skipcomment(ml_get_curline());
8964 if (cin_isdo(l))
8965 {
8966 amount = get_indent(); /* XXX */
8967 --whilelevel;
8968 continue;
8969 }
8970 }
8971
8972 /*
8973 * Found a terminated line above an unterminated line. Add
8974 * the amount for a continuation line.
8975 * x = 1;
8976 * y = foo +
8977 * -> here;
8978 * or
8979 * int x = 1;
8980 * int foo,
8981 * -> here;
8982 */
8983 if (lookfor == LOOKFOR_UNTERM
8984 || lookfor == LOOKFOR_ENUM_OR_INIT)
8985 {
8986 if (cont_amount > 0)
8987 amount = cont_amount;
8988 else
8989 amount += ind_continuation;
8990 break;
8991 }
8992
8993 /*
8994 * Found a terminated line above a terminated line or "if"
8995 * etc. line. Use the amount of the line below us.
8996 * x = 1; x = 1;
8997 * if (asdf) y = 2;
8998 * while (asdf) ->here;
8999 * here;
9000 * ->foo;
9001 */
9002 if (lookfor == LOOKFOR_TERM)
9003 {
9004 if (!lookfor_break && whilelevel == 0)
9005 break;
9006 }
9007
9008 /*
9009 * First line above the one we're indenting is terminated.
9010 * To know what needs to be done look further backward for
9011 * a terminated line.
9012 */
9013 else
9014 {
9015 /*
9016 * position the cursor over the rightmost paren, so
9017 * that matching it will take us back to the start of
9018 * the line. Helps for:
9019 * func(asdr,
9020 * asdfasdf);
9021 * here;
9022 */
9023term_again:
9024 l = ml_get_curline();
9025 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009026 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009027 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 {
9029 /*
9030 * Check if we are on a case label now. This is
9031 * handled above.
9032 * case xx: if ( asdf &&
9033 * asdf)
9034 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009035 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009037 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 {
9039 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009040 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 continue;
9042 }
9043 }
9044
9045 /* When aligning with the case statement, don't align
9046 * with a statement after it.
9047 * case 1: { <-- don't use this { position
9048 * stat;
9049 * }
9050 * case 2:
9051 * stat;
9052 * }
9053 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009054 iscase = (curbuf->b_ind_keep_case_label
9055 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056
9057 /*
9058 * Get indent and pointer to text for current line,
9059 * ignoring any jump label.
9060 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009061 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
9063 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009064 amount += curbuf->b_ind_open_extra;
9065 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009066 l = skipwhite(l);
9067 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009068 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9070
9071 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009072 * When a terminated line starts with "else" skip to
9073 * the matching "if":
9074 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009075 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009076 * Need to use the scope of this "else". XXX
9077 * If whilelevel != 0 continue looking for a "do {".
9078 */
9079 if (lookfor == LOOKFOR_TERM
9080 && *l != '}'
9081 && cin_iselse(l)
9082 && whilelevel == 0)
9083 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009084 if ((trypos = find_start_brace()) == NULL
9085 || find_match(LOOKFOR_IF, trypos->lnum)
9086 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009087 break;
9088 continue;
9089 }
9090
9091 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 * If we're at the end of a block, skip to the start of
9093 * that block.
9094 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009095 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009096 if (find_last_paren(l, '{', '}') /* XXX */
9097 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009099 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 /* if not "else {" check for terminated again */
9101 /* but skip block for "} else {" */
9102 l = cin_skipcomment(ml_get_curline());
9103 if (*l == '}' || !cin_iselse(l))
9104 goto term_again;
9105 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009106 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 }
9108 }
9109 }
9110 }
9111 }
9112 }
9113
9114 /* add extra indent for a comment */
9115 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009116 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009117
9118 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009119 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9120 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009121
9122 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009124
9125 /*
9126 * ok -- we're not inside any sort of structure at all!
9127 *
9128 * This means we're at the top level, and everything should
9129 * basically just match where the previous line is, except
9130 * for the lines immediately following a function declaration,
9131 * which are K&R-style parameters and need to be indented.
9132 *
9133 * if our line starts with an open brace, forget about any
9134 * prevailing indent and make sure it looks like the start
9135 * of a function
9136 */
9137
9138 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009140 amount = curbuf->b_ind_first_open;
9141 goto theend;
9142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009144 /*
9145 * If the NEXT line is a function declaration, the current
9146 * line needs to be indented as a function type spec.
9147 * Don't do this if the current line looks like a comment or if the
9148 * current line is terminated, ie. ends in ';', or if the current line
9149 * contains { or }: "void f() {\n if (1)"
9150 */
9151 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9152 && !cin_nocode(theline)
9153 && vim_strchr(theline, '{') == NULL
9154 && vim_strchr(theline, '}') == NULL
9155 && !cin_ends_in(theline, (char_u *)":", NULL)
9156 && !cin_ends_in(theline, (char_u *)",", NULL)
9157 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9158 cur_curpos.lnum + 1)
9159 && !cin_isterminated(theline, FALSE, TRUE))
9160 {
9161 amount = curbuf->b_ind_func_type;
9162 goto theend;
9163 }
9164
9165 /* search backwards until we find something we recognize */
9166 amount = 0;
9167 curwin->w_cursor = cur_curpos;
9168 while (curwin->w_cursor.lnum > 1)
9169 {
9170 curwin->w_cursor.lnum--;
9171 curwin->w_cursor.col = 0;
9172
9173 l = ml_get_curline();
9174
9175 /*
9176 * If we're in a comment or raw string now, skip to the start
9177 * of it.
9178 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009179 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009181 curwin->w_cursor.lnum = trypos->lnum + 1;
9182 curwin->w_cursor.col = 0;
9183 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
9185
9186 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009187 * Are we at the start of a cpp base class declaration or
9188 * constructor initialization?
9189 */ /* XXX */
9190 n = FALSE;
9191 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009193 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9194 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009196 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009198 /* XXX */
9199 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9200 break;
9201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009203 /*
9204 * Skip preprocessor directives and blank lines.
9205 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009206 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009207 continue;
9208
9209 if (cin_nocode(l))
9210 continue;
9211
9212 /*
9213 * If the previous line ends in ',', use one level of
9214 * indentation:
9215 * int foo,
9216 * bar;
9217 * do this before checking for '}' in case of eg.
9218 * enum foobar
9219 * {
9220 * ...
9221 * } foo,
9222 * bar;
9223 */
9224 n = 0;
9225 if (cin_ends_in(l, (char_u *)",", NULL)
9226 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9227 {
9228 /* take us back to opening paren */
9229 if (find_last_paren(l, '(', ')')
9230 && (trypos = find_match_paren(
9231 curbuf->b_ind_maxparen)) != NULL)
9232 curwin->w_cursor = *trypos;
9233
9234 /* For a line ending in ',' that is a continuation line go
9235 * back to the first line with a backslash:
9236 * char *foo = "bla\
9237 * bla",
9238 * here;
9239 */
9240 while (n == 0 && curwin->w_cursor.lnum > 1)
9241 {
9242 l = ml_get(curwin->w_cursor.lnum - 1);
9243 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9244 break;
9245 --curwin->w_cursor.lnum;
9246 curwin->w_cursor.col = 0;
9247 }
9248
9249 amount = get_indent(); /* XXX */
9250
9251 if (amount == 0)
9252 amount = cin_first_id_amount();
9253 if (amount == 0)
9254 amount = ind_continuation;
9255 break;
9256 }
9257
9258 /*
9259 * If the line looks like a function declaration, and we're
9260 * not in a comment, put it the left margin.
9261 */
9262 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9263 break;
9264 l = ml_get_curline();
9265
9266 /*
9267 * Finding the closing '}' of a previous function. Put
9268 * current line at the left margin. For when 'cino' has "fs".
9269 */
9270 if (*skipwhite(l) == '}')
9271 break;
9272
9273 /* (matching {)
9274 * If the previous line ends on '};' (maybe followed by
9275 * comments) align at column 0. For example:
9276 * char *string_array[] = { "foo",
9277 * / * x * / "b};ar" }; / * foobar * /
9278 */
9279 if (cin_ends_in(l, (char_u *)"};", NULL))
9280 break;
9281
9282 /*
9283 * If the previous line ends on '[' we are probably in an
9284 * array constant:
9285 * something = [
9286 * 234, <- extra indent
9287 */
9288 if (cin_ends_in(l, (char_u *)"[", NULL))
9289 {
9290 amount = get_indent() + ind_continuation;
9291 break;
9292 }
9293
9294 /*
9295 * Find a line only has a semicolon that belongs to a previous
9296 * line ending in '}', e.g. before an #endif. Don't increase
9297 * indent then.
9298 */
9299 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9300 {
9301 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302
9303 while (curwin->w_cursor.lnum > 1)
9304 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009305 look = ml_get(--curwin->w_cursor.lnum);
9306 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009307 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009309 }
9310 if (curwin->w_cursor.lnum > 0
9311 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009314 curwin->w_cursor = curpos_save;
9315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009317 /*
9318 * If the PREVIOUS line is a function declaration, the current
9319 * line (and the ones that follow) needs to be indented as
9320 * parameters.
9321 */
9322 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9323 {
9324 amount = curbuf->b_ind_param;
9325 break;
9326 }
9327
9328 /*
9329 * If the previous line ends in ';' and the line before the
9330 * previous line ends in ',' or '\', ident to column zero:
9331 * int foo,
9332 * bar;
9333 * indent_to_0 here;
9334 */
9335 if (cin_ends_in(l, (char_u *)";", NULL))
9336 {
9337 l = ml_get(curwin->w_cursor.lnum - 1);
9338 if (cin_ends_in(l, (char_u *)",", NULL)
9339 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9340 break;
9341 l = ml_get_curline();
9342 }
9343
9344 /*
9345 * Doesn't look like anything interesting -- so just
9346 * use the indent of this line.
9347 *
9348 * Position the cursor over the rightmost paren, so that
9349 * matching it will take us back to the start of the line.
9350 */
9351 find_last_paren(l, '(', ')');
9352
9353 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9354 curwin->w_cursor = *trypos;
9355 amount = get_indent(); /* XXX */
9356 break;
9357 }
9358
9359 /* add extra indent for a comment */
9360 if (cin_iscomment(theline))
9361 amount += curbuf->b_ind_comment;
9362
9363 /* add extra indent if the previous line ended in a backslash:
9364 * "asdfasdf\
9365 * here";
9366 * char *foo = "asdf\
9367 * here";
9368 */
9369 if (cur_curpos.lnum > 1)
9370 {
9371 l = ml_get(cur_curpos.lnum - 1);
9372 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9373 {
9374 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9375 if (cur_amount > 0)
9376 amount = cur_amount;
9377 else if (cur_amount == 0)
9378 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
9380 }
9381
9382theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009383 if (amount < 0)
9384 amount = 0;
9385
9386laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 /* put the cursor back where it belongs */
9388 curwin->w_cursor = cur_curpos;
9389
9390 vim_free(linecopy);
9391
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 return amount;
9393}
9394
9395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009396find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 char_u *look;
9399 pos_T *theirscope;
9400 char_u *mightbeif;
9401 int elselevel;
9402 int whilelevel;
9403
9404 if (lookfor == LOOKFOR_IF)
9405 {
9406 elselevel = 1;
9407 whilelevel = 0;
9408 }
9409 else
9410 {
9411 elselevel = 0;
9412 whilelevel = 1;
9413 }
9414
9415 curwin->w_cursor.col = 0;
9416
9417 while (curwin->w_cursor.lnum > ourscope + 1)
9418 {
9419 curwin->w_cursor.lnum--;
9420 curwin->w_cursor.col = 0;
9421
9422 look = cin_skipcomment(ml_get_curline());
9423 if (cin_iselse(look)
9424 || cin_isif(look)
9425 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009426 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 {
9428 /*
9429 * if we've gone outside the braces entirely,
9430 * we must be out of scope...
9431 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009432 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 if (theirscope == NULL)
9434 break;
9435
9436 /*
9437 * and if the brace enclosing this is further
9438 * back than the one enclosing the else, we're
9439 * out of luck too.
9440 */
9441 if (theirscope->lnum < ourscope)
9442 break;
9443
9444 /*
9445 * and if they're enclosed in a *deeper* brace,
9446 * then we can ignore it because it's in a
9447 * different scope...
9448 */
9449 if (theirscope->lnum > ourscope)
9450 continue;
9451
9452 /*
9453 * if it was an "else" (that's not an "else if")
9454 * then we need to go back to another if, so
9455 * increment elselevel
9456 */
9457 look = cin_skipcomment(ml_get_curline());
9458 if (cin_iselse(look))
9459 {
9460 mightbeif = cin_skipcomment(look + 4);
9461 if (!cin_isif(mightbeif))
9462 ++elselevel;
9463 continue;
9464 }
9465
9466 /*
9467 * if it was a "while" then we need to go back to
9468 * another "do", so increment whilelevel. XXX
9469 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009470 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 {
9472 ++whilelevel;
9473 continue;
9474 }
9475
9476 /* If it's an "if" decrement elselevel */
9477 look = cin_skipcomment(ml_get_curline());
9478 if (cin_isif(look))
9479 {
9480 elselevel--;
9481 /*
9482 * When looking for an "if" ignore "while"s that
9483 * get in the way.
9484 */
9485 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9486 whilelevel = 0;
9487 }
9488
9489 /* If it's a "do" decrement whilelevel */
9490 if (cin_isdo(look))
9491 whilelevel--;
9492
9493 /*
9494 * if we've used up all the elses, then
9495 * this must be the if that we want!
9496 * match the indent level of that if.
9497 */
9498 if (elselevel <= 0 && whilelevel <= 0)
9499 {
9500 return OK;
9501 }
9502 }
9503 }
9504 return FAIL;
9505}
9506
9507# if defined(FEAT_EVAL) || defined(PROTO)
9508/*
9509 * Get indent level from 'indentexpr'.
9510 */
9511 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009512get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009514 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009515 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009516 pos_T save_pos;
9517 colnr_T save_curswant;
9518 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009520 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9521 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009523 /* Save and restore cursor position and curswant, in case it was changed
9524 * via :normal commands */
9525 save_pos = curwin->w_cursor;
9526 save_curswant = curwin->w_curswant;
9527 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009529 if (use_sandbox)
9530 ++sandbox;
9531 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009532
9533 /* Need to make a copy, the 'indentexpr' option could be changed while
9534 * evaluating it. */
9535 inde_copy = vim_strsave(curbuf->b_p_inde);
9536 if (inde_copy != NULL)
9537 {
9538 indent = (int)eval_to_number(inde_copy);
9539 vim_free(inde_copy);
9540 }
9541
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009542 if (use_sandbox)
9543 --sandbox;
9544 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9547 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9548 * command. */
9549 save_State = State;
9550 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009551 curwin->w_cursor = save_pos;
9552 curwin->w_curswant = save_curswant;
9553 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 check_cursor();
9555 State = save_State;
9556
9557 /* If there is an error, just keep the current indent. */
9558 if (indent < 0)
9559 indent = get_indent();
9560
9561 return indent;
9562}
9563# endif
9564
9565#endif /* FEAT_CINDENT */
9566
9567#if defined(FEAT_LISP) || defined(PROTO)
9568
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009569static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570
9571 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009572lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574 char_u buf[LSIZE];
9575 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009576 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577
9578 while (*word != NUL)
9579 {
9580 (void)copy_option_part(&word, buf, LSIZE, ",");
9581 len = (int)STRLEN(buf);
9582 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9583 return TRUE;
9584 }
9585 return FALSE;
9586}
9587
9588/*
9589 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9590 * The incompatible newer method is quite a bit better at indenting
9591 * code in lisp-like languages than the traditional one; it's still
9592 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9593 *
9594 * TODO:
9595 * Findmatch() should be adapted for lisp, also to make showmatch
9596 * work correctly: now (v5.3) it seems all C/C++ oriented:
9597 * - it does not recognize the #\( and #\) notations as character literals
9598 * - it doesn't know about comments starting with a semicolon
9599 * - it incorrectly interprets '(' as a character literal
9600 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009601 * Update from Sergey Khorev:
9602 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 */
9604 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009605get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009607 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 int amount;
9609 char_u *that;
9610 colnr_T col;
9611 colnr_T firsttry;
9612 int parencount, quotecount;
9613 int vi_lisp;
9614
9615 /* Set vi_lisp to use the vi-compatible method */
9616 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9617
9618 realpos = curwin->w_cursor;
9619 curwin->w_cursor.col = 0;
9620
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009621 if ((pos = findmatch(NULL, '(')) == NULL)
9622 pos = findmatch(NULL, '[');
9623 else
9624 {
9625 paren = *pos;
9626 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009627 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009628 pos = &paren;
9629 }
9630 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 {
9632 /* Extra trick: Take the indent of the first previous non-white
9633 * line that is at the same () level. */
9634 amount = -1;
9635 parencount = 0;
9636
9637 while (--curwin->w_cursor.lnum >= pos->lnum)
9638 {
9639 if (linewhite(curwin->w_cursor.lnum))
9640 continue;
9641 for (that = ml_get_curline(); *that != NUL; ++that)
9642 {
9643 if (*that == ';')
9644 {
9645 while (*(that + 1) != NUL)
9646 ++that;
9647 continue;
9648 }
9649 if (*that == '\\')
9650 {
9651 if (*(that + 1) != NUL)
9652 ++that;
9653 continue;
9654 }
9655 if (*that == '"' && *(that + 1) != NUL)
9656 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009657 while (*++that && *that != '"')
9658 {
9659 /* skipping escaped characters in the string */
9660 if (*that == '\\')
9661 {
9662 if (*++that == NUL)
9663 break;
9664 if (that[1] == NUL)
9665 {
9666 ++that;
9667 break;
9668 }
9669 }
9670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009672 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009674 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 --parencount;
9676 }
9677 if (parencount == 0)
9678 {
9679 amount = get_indent();
9680 break;
9681 }
9682 }
9683
9684 if (amount == -1)
9685 {
9686 curwin->w_cursor.lnum = pos->lnum;
9687 curwin->w_cursor.col = pos->col;
9688 col = pos->col;
9689
9690 that = ml_get_curline();
9691
9692 if (vi_lisp && get_indent() == 0)
9693 amount = 2;
9694 else
9695 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009696 char_u *line = that;
9697
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 amount = 0;
9699 while (*that && col)
9700 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009701 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 col--;
9703 }
9704
9705 /*
9706 * Some keywords require "body" indenting rules (the
9707 * non-standard-lisp ones are Scheme special forms):
9708 *
9709 * (let ((a 1)) instead (let ((a 1))
9710 * (...)) of (...))
9711 */
9712
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009713 if (!vi_lisp && (*that == '(' || *that == '[')
9714 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 amount += 2;
9716 else
9717 {
9718 that++;
9719 amount++;
9720 firsttry = amount;
9721
Bram Moolenaar1c465442017-03-12 20:10:05 +01009722 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009724 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 ++that;
9726 }
9727
9728 if (*that && *that != ';') /* not a comment line */
9729 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009730 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009732 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 firsttry++;
9734
9735 parencount = 0;
9736 quotecount = 0;
9737
9738 if (vi_lisp
9739 || (*that != '"'
9740 && *that != '\''
9741 && *that != '#'
9742 && (*that < '0' || *that > '9')))
9743 {
9744 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009745 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 || quotecount
9747 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009748 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 && !quotecount
9750 && !parencount
9751 && vi_lisp)))
9752 {
9753 if (*that == '"')
9754 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009755 if ((*that == '(' || *that == '[')
9756 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009758 if ((*that == ')' || *that == ']')
9759 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 --parencount;
9761 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009762 amount += lbr_chartabsize_adv(
9763 line, &that, (colnr_T)amount);
9764 amount += lbr_chartabsize_adv(
9765 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 }
9767 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009768 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009770 amount += lbr_chartabsize(
9771 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 that++;
9773 }
9774 if (!*that || *that == ';')
9775 amount = firsttry;
9776 }
9777 }
9778 }
9779 }
9780 }
9781 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009782 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783
9784 curwin->w_cursor = realpos;
9785
9786 return amount;
9787}
9788#endif /* FEAT_LISP */
9789
9790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009791prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009793#if defined(SIGHUP) && defined(SIG_IGN)
9794 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9795 * makes Vim exit and then handling SIGHUP causes various reentrance
9796 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009797 signal(SIGHUP, SIG_IGN);
9798#endif
9799
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800#ifdef FEAT_GUI
9801 if (gui.in_use)
9802 {
9803 gui.dying = TRUE;
9804 out_trash(); /* trash any pending output */
9805 }
9806 else
9807#endif
9808 {
9809 windgoto((int)Rows - 1, 0);
9810
9811 /*
9812 * Switch terminal mode back now, so messages end up on the "normal"
9813 * screen (if there are two screens).
9814 */
9815 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009816 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 out_flush();
9818 }
9819}
9820
9821/*
9822 * Preserve files and exit.
9823 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009824 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9825 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 */
9827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009828preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830 buf_T *buf;
9831
9832 prepare_to_exit();
9833
Bram Moolenaar4770d092006-01-12 23:22:24 +00009834 /* Setting this will prevent free() calls. That avoids calling free()
9835 * recursively when free() was invoked with a bad pointer. */
9836 really_exiting = TRUE;
9837
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 out_str(IObuff);
9839 screen_start(); /* don't know where cursor is now */
9840 out_flush();
9841
9842 ml_close_notmod(); /* close all not-modified buffers */
9843
Bram Moolenaar29323592016-07-24 22:04:11 +02009844 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 {
9846 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9847 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009848 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 screen_start(); /* don't know where cursor is now */
9850 out_flush();
9851 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9852 break;
9853 }
9854 }
9855
9856 ml_close_all(FALSE); /* close all memfiles, without deleting */
9857
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009858 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
9860 getout(1);
9861}
9862
9863/*
9864 * return TRUE if "fname" exists.
9865 */
9866 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009867vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009869 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870
9871 if (mch_stat((char *)fname, &st))
9872 return FALSE;
9873 return TRUE;
9874}
9875
9876/*
9877 * Check for CTRL-C pressed, but only once in a while.
9878 * Should be used instead of ui_breakcheck() for functions that check for
9879 * each line in the file. Calling ui_breakcheck() each time takes too much
9880 * time, because it can be a system call.
9881 */
9882
9883#ifndef BREAKCHECK_SKIP
9884# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9885# define BREAKCHECK_SKIP 200
9886# else
9887# define BREAKCHECK_SKIP 32
9888# endif
9889#endif
9890
9891static int breakcheck_count = 0;
9892
9893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009894line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
9896 if (++breakcheck_count >= BREAKCHECK_SKIP)
9897 {
9898 breakcheck_count = 0;
9899 ui_breakcheck();
9900 }
9901}
9902
9903/*
9904 * Like line_breakcheck() but check 10 times less often.
9905 */
9906 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009907fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908{
9909 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9910 {
9911 breakcheck_count = 0;
9912 ui_breakcheck();
9913 }
9914}
9915
9916/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009917 * Invoke expand_wildcards() for one pattern.
9918 * Expand items like "%:h" before the expansion.
9919 * Returns OK or FAIL.
9920 */
9921 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009922expand_wildcards_eval(
9923 char_u **pat, /* pointer to input pattern */
9924 int *num_file, /* resulting number of files */
9925 char_u ***file, /* array of resulting files */
9926 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009927{
9928 int ret = FAIL;
9929 char_u *eval_pat = NULL;
9930 char_u *exp_pat = *pat;
9931 char_u *ignored_msg;
9932 int usedlen;
9933
9934 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9935 {
9936 ++emsg_off;
9937 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9938 NULL, &ignored_msg, NULL);
9939 --emsg_off;
9940 if (eval_pat != NULL)
9941 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9942 }
9943
9944 if (exp_pat != NULL)
9945 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9946
9947 if (eval_pat != NULL)
9948 {
9949 vim_free(exp_pat);
9950 vim_free(eval_pat);
9951 }
9952
9953 return ret;
9954}
9955
9956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9958 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009959 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 */
9961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009962expand_wildcards(
9963 int num_pat, /* number of input patterns */
9964 char_u **pat, /* array of input patterns */
9965 int *num_files, /* resulting number of files */
9966 char_u ***files, /* array of resulting files */
9967 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968{
9969 int retval;
9970 int i, j;
9971 char_u *p;
9972 int non_suf_match; /* number without matching suffix */
9973
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009974 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975
9976 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009977 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 return retval;
9979
9980#ifdef FEAT_WILDIGN
9981 /*
9982 * Remove names that match 'wildignore'.
9983 */
9984 if (*p_wig)
9985 {
9986 char_u *ffname;
9987
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009988 /* check all files in (*files)[] */
9989 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009991 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 if (ffname == NULL) /* out of memory */
9993 break;
9994# ifdef VMS
9995 vms_remove_version(ffname);
9996# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009997 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009999 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010000 vim_free((*files)[i]);
10001 for (j = i; j + 1 < *num_files; ++j)
10002 (*files)[j] = (*files)[j + 1];
10003 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 --i;
10005 }
10006 vim_free(ffname);
10007 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010008
10009 /* If the number of matches is now zero, we fail. */
10010 if (*num_files == 0)
10011 {
Bram Moolenaard23a8232018-02-10 18:45:26 +010010012 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010013 return FAIL;
10014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 }
10016#endif
10017
10018 /*
10019 * Move the names where 'suffixes' match to the end.
10020 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010021 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 {
10023 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010024 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010026 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 {
10028 /*
10029 * Move the name without matching suffix to the front
10030 * of the list.
10031 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010032 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010034 (*files)[j] = (*files)[j - 1];
10035 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 }
10037 }
10038 }
10039
10040 return retval;
10041}
10042
10043/*
10044 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10045 */
10046 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010047match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048{
10049 int fnamelen, setsuflen;
10050 char_u *setsuf;
10051#define MAXSUFLEN 30 /* maximum length of a file suffix */
10052 char_u suf_buf[MAXSUFLEN];
10053
10054 fnamelen = (int)STRLEN(fname);
10055 setsuflen = 0;
10056 for (setsuf = p_su; *setsuf; )
10057 {
10058 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010059 if (setsuflen == 0)
10060 {
10061 char_u *tail = gettail(fname);
10062
10063 /* empty entry: match name without a '.' */
10064 if (vim_strchr(tail, '.') == NULL)
10065 {
10066 setsuflen = 1;
10067 break;
10068 }
10069 }
10070 else
10071 {
10072 if (fnamelen >= setsuflen
10073 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10074 (size_t)setsuflen) == 0)
10075 break;
10076 setsuflen = 0;
10077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079 return (setsuflen != 0);
10080}
10081
10082#if !defined(NO_EXPANDPATH) || defined(PROTO)
10083
10084# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010085static int vim_backtick(char_u *p);
10086static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087# endif
10088
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010089# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090/*
10091 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10092 * it's shared between these systems.
10093 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010094# if defined(PROTO)
10095# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096# else
10097# ifdef __BORLANDC__
10098# define _cdecl _RTLENTRYF
10099# endif
10100# endif
10101
10102/*
10103 * comparison function for qsort in dos_expandpath()
10104 */
10105 static int _cdecl
10106pstrcmp(const void *a, const void *b)
10107{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010108 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109}
10110
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010112 * Recursively expand one path component into all matching files and/or
10113 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 * Return the number of matches found.
10115 * "path" has backslashes before chars that are not to be expanded, starting
10116 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010117 * Return the number of matches found.
10118 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 */
10120 static int
10121dos_expandpath(
10122 garray_T *gap,
10123 char_u *path,
10124 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010125 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010126 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010128 char_u *buf;
10129 char_u *path_end;
10130 char_u *p, *s, *e;
10131 int start_len = gap->ga_len;
10132 char_u *pat;
10133 regmatch_T regmatch;
10134 int starts_with_dot;
10135 int matches;
10136 int len;
10137 int starstar = FALSE;
10138 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 WIN32_FIND_DATA fb;
10140 HANDLE hFind = (HANDLE)0;
10141# ifdef FEAT_MBYTE
10142 WIN32_FIND_DATAW wfb;
10143 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10144# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010146 int ok;
10147
10148 /* Expanding "**" may take a long time, check for CTRL-C. */
10149 if (stardepth > 0)
10150 {
10151 ui_breakcheck();
10152 if (got_int)
10153 return 0;
10154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010156 /* Make room for file name. When doing encoding conversion the actual
10157 * length may be quite a bit longer, thus use the maximum possible length. */
10158 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 if (buf == NULL)
10160 return 0;
10161
10162 /*
10163 * Find the first part in the path name that contains a wildcard or a ~1.
10164 * Copy it into buf, including the preceding characters.
10165 */
10166 p = buf;
10167 s = buf;
10168 e = NULL;
10169 path_end = path;
10170 while (*path_end != NUL)
10171 {
10172 /* May ignore a wildcard that has a backslash before it; it will
10173 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10174 if (path_end >= path + wildoff && rem_backslash(path_end))
10175 *p++ = *path_end++;
10176 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10177 {
10178 if (e != NULL)
10179 break;
10180 s = p + 1;
10181 }
10182 else if (path_end >= path + wildoff
10183 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10184 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010185# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 if (has_mbyte)
10187 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010188 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 STRNCPY(p, path_end, len);
10190 p += len;
10191 path_end += len;
10192 }
10193 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 *p++ = *path_end++;
10196 }
10197 e = p;
10198 *e = NUL;
10199
10200 /* now we have one wildcard component between s and e */
10201 /* Remove backslashes between "wildoff" and the start of the wildcard
10202 * component. */
10203 for (p = buf + wildoff; p < s; ++p)
10204 if (rem_backslash(p))
10205 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010206 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 --e;
10208 --s;
10209 }
10210
Bram Moolenaar231334e2005-07-25 20:46:57 +000010211 /* Check for "**" between "s" and "e". */
10212 for (p = s; p < e; ++p)
10213 if (p[0] == '*' && p[1] == '*')
10214 starstar = TRUE;
10215
Bram Moolenaard82103e2016-01-17 17:04:05 +010010216 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10218 if (pat == NULL)
10219 {
10220 vim_free(buf);
10221 return 0;
10222 }
10223
10224 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010225 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010226 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 regmatch.rm_ic = TRUE; /* Always ignore case */
10228 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010229 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010230 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 vim_free(pat);
10232
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010233 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 {
10235 vim_free(buf);
10236 return 0;
10237 }
10238
10239 /* remember the pattern or file name being looked for */
10240 matchname = vim_strsave(s);
10241
Bram Moolenaar231334e2005-07-25 20:46:57 +000010242 /* If "**" is by itself, this is the first time we encounter it and more
10243 * is following then find matches without any directory. */
10244 if (!didstar && stardepth < 100 && starstar && e - s == 2
10245 && *path_end == '/')
10246 {
10247 STRCPY(s, path_end + 1);
10248 ++stardepth;
10249 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10250 --stardepth;
10251 }
10252
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 /* Scan all files in the directory with "dir/ *.*" */
10254 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255# ifdef FEAT_MBYTE
10256 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10257 {
10258 /* The active codepage differs from 'encoding'. Attempt using the
10259 * wide function. If it fails because it is not implemented fall back
10260 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010261 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 if (wn != NULL)
10263 {
10264 hFind = FindFirstFileW(wn, &wfb);
10265 if (hFind == INVALID_HANDLE_VALUE
10266 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010267 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 }
10269 }
10270
10271 if (wn == NULL)
10272# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010273 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275
10276 while (ok)
10277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278# ifdef FEAT_MBYTE
10279 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010280 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 else
10282# endif
10283 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 /* Ignore entries starting with a dot, unless when asked for. Accept
10285 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010286 if ((p[0] != '.' || starts_with_dot
10287 || ((flags & EW_DODOT)
10288 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010290 || (regmatch.regprog != NULL
10291 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010292 || ((flags & EW_NOTWILD)
10293 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010297
10298 if (starstar && stardepth < 100)
10299 {
10300 /* For "**" in the pattern first go deeper in the tree to
10301 * find matches. */
10302 STRCPY(buf + len, "/**");
10303 STRCPY(buf + len + 3, path_end);
10304 ++stardepth;
10305 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10306 --stardepth;
10307 }
10308
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 STRCPY(buf + len, path_end);
10310 if (mch_has_exp_wildcard(path_end))
10311 {
10312 /* need to expand another component of the path */
10313 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010314 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 }
10316 else
10317 {
10318 /* no more wildcards, check if there is a match */
10319 /* remove backslashes for the remaining components only */
10320 if (*path_end != 0)
10321 backslash_halve(buf + len + 1);
10322 if (mch_getperm(buf) >= 0) /* add existing file */
10323 addfile(gap, buf, flags);
10324 }
10325 }
10326
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327# ifdef FEAT_MBYTE
10328 if (wn != NULL)
10329 {
10330 vim_free(p);
10331 ok = FindNextFileW(hFind, &wfb);
10332 }
10333 else
10334# endif
10335 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336
10337 /* If no more matches and no match was used, try expanding the name
10338 * itself. Finds the long name of a short filename. */
10339 if (!ok && matchname != NULL && gap->ga_len == start_len)
10340 {
10341 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 FindClose(hFind);
10343# ifdef FEAT_MBYTE
10344 if (wn != NULL)
10345 {
10346 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010347 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 if (wn != NULL)
10349 hFind = FindFirstFileW(wn, &wfb);
10350 }
10351 if (wn == NULL)
10352# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010353 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010355 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 }
10357 }
10358
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 FindClose(hFind);
10360# ifdef FEAT_MBYTE
10361 vim_free(wn);
10362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010364 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 vim_free(matchname);
10366
10367 matches = gap->ga_len - start_len;
10368 if (matches > 0)
10369 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10370 sizeof(char_u *), pstrcmp);
10371 return matches;
10372}
10373
10374 int
10375mch_expandpath(
10376 garray_T *gap,
10377 char_u *path,
10378 int flags) /* EW_* flags */
10379{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010380 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010382# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383
Bram Moolenaar231334e2005-07-25 20:46:57 +000010384#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10385 || defined(PROTO)
10386/*
10387 * Unix style wildcard expansion code.
10388 * It's here because it's used both for Unix and Mac.
10389 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010390static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010391
10392 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010393pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010394{
10395 return (pathcmp(*(char **)a, *(char **)b, -1));
10396}
10397
10398/*
10399 * Recursively expand one path component into all matching files and/or
10400 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10401 * "path" has backslashes before chars that are not to be expanded, starting
10402 * at "path + wildoff".
10403 * Return the number of matches found.
10404 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10405 */
10406 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010407unix_expandpath(
10408 garray_T *gap,
10409 char_u *path,
10410 int wildoff,
10411 int flags, /* EW_* flags */
10412 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010413{
10414 char_u *buf;
10415 char_u *path_end;
10416 char_u *p, *s, *e;
10417 int start_len = gap->ga_len;
10418 char_u *pat;
10419 regmatch_T regmatch;
10420 int starts_with_dot;
10421 int matches;
10422 int len;
10423 int starstar = FALSE;
10424 static int stardepth = 0; /* depth for "**" expansion */
10425
10426 DIR *dirp;
10427 struct dirent *dp;
10428
10429 /* Expanding "**" may take a long time, check for CTRL-C. */
10430 if (stardepth > 0)
10431 {
10432 ui_breakcheck();
10433 if (got_int)
10434 return 0;
10435 }
10436
10437 /* make room for file name */
10438 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10439 if (buf == NULL)
10440 return 0;
10441
10442 /*
10443 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010444 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010445 * Copy it into "buf", including the preceding characters.
10446 */
10447 p = buf;
10448 s = buf;
10449 e = NULL;
10450 path_end = path;
10451 while (*path_end != NUL)
10452 {
10453 /* May ignore a wildcard that has a backslash before it; it will
10454 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10455 if (path_end >= path + wildoff && rem_backslash(path_end))
10456 *p++ = *path_end++;
10457 else if (*path_end == '/')
10458 {
10459 if (e != NULL)
10460 break;
10461 s = p + 1;
10462 }
10463 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010464 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010465 || (!p_fic && (flags & EW_ICASE)
10466 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010467 e = p;
10468#ifdef FEAT_MBYTE
10469 if (has_mbyte)
10470 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010471 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010472 STRNCPY(p, path_end, len);
10473 p += len;
10474 path_end += len;
10475 }
10476 else
10477#endif
10478 *p++ = *path_end++;
10479 }
10480 e = p;
10481 *e = NUL;
10482
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010483 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010484 /* Remove backslashes between "wildoff" and the start of the wildcard
10485 * component. */
10486 for (p = buf + wildoff; p < s; ++p)
10487 if (rem_backslash(p))
10488 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010489 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010490 --e;
10491 --s;
10492 }
10493
10494 /* Check for "**" between "s" and "e". */
10495 for (p = s; p < e; ++p)
10496 if (p[0] == '*' && p[1] == '*')
10497 starstar = TRUE;
10498
10499 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010500 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010501 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10502 if (pat == NULL)
10503 {
10504 vim_free(buf);
10505 return 0;
10506 }
10507
10508 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010509 if (flags & EW_ICASE)
10510 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10511 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010512 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010513 if (flags & (EW_NOERROR | EW_NOTWILD))
10514 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010515 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010516 if (flags & (EW_NOERROR | EW_NOTWILD))
10517 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010518 vim_free(pat);
10519
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010520 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010521 {
10522 vim_free(buf);
10523 return 0;
10524 }
10525
10526 /* If "**" is by itself, this is the first time we encounter it and more
10527 * is following then find matches without any directory. */
10528 if (!didstar && stardepth < 100 && starstar && e - s == 2
10529 && *path_end == '/')
10530 {
10531 STRCPY(s, path_end + 1);
10532 ++stardepth;
10533 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10534 --stardepth;
10535 }
10536
10537 /* open the directory for scanning */
10538 *s = NUL;
10539 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10540
10541 /* Find all matching entries */
10542 if (dirp != NULL)
10543 {
10544 for (;;)
10545 {
10546 dp = readdir(dirp);
10547 if (dp == NULL)
10548 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010549 if ((dp->d_name[0] != '.' || starts_with_dot
10550 || ((flags & EW_DODOT)
10551 && dp->d_name[1] != NUL
10552 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010553 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10554 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010555 || ((flags & EW_NOTWILD)
10556 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010557 {
10558 STRCPY(s, dp->d_name);
10559 len = STRLEN(buf);
10560
10561 if (starstar && stardepth < 100)
10562 {
10563 /* For "**" in the pattern first go deeper in the tree to
10564 * find matches. */
10565 STRCPY(buf + len, "/**");
10566 STRCPY(buf + len + 3, path_end);
10567 ++stardepth;
10568 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10569 --stardepth;
10570 }
10571
10572 STRCPY(buf + len, path_end);
10573 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10574 {
10575 /* need to expand another component of the path */
10576 /* remove backslashes for the remaining components only */
10577 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10578 }
10579 else
10580 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010581 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010582
Bram Moolenaar231334e2005-07-25 20:46:57 +000010583 /* no more wildcards, check if there is a match */
10584 /* remove backslashes for the remaining components only */
10585 if (*path_end != NUL)
10586 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010587 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010588 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010589 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010590 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010591#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010592 size_t precomp_len = STRLEN(buf)+1;
10593 char_u *precomp_buf =
10594 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010595
Bram Moolenaar231334e2005-07-25 20:46:57 +000010596 if (precomp_buf)
10597 {
10598 mch_memmove(buf, precomp_buf, precomp_len);
10599 vim_free(precomp_buf);
10600 }
10601#endif
10602 addfile(gap, buf, flags);
10603 }
10604 }
10605 }
10606 }
10607
10608 closedir(dirp);
10609 }
10610
10611 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010612 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010613
10614 matches = gap->ga_len - start_len;
10615 if (matches > 0)
10616 qsort(((char_u **)gap->ga_data) + start_len, matches,
10617 sizeof(char_u *), pstrcmp);
10618 return matches;
10619}
10620#endif
10621
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010622#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010623static int find_previous_pathsep(char_u *path, char_u **psep);
10624static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10625static void expand_path_option(char_u *curdir, garray_T *gap);
10626static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10627static void uniquefy_paths(garray_T *gap, char_u *pattern);
10628static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010629
10630/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010631 * Moves "*psep" back to the previous path separator in "path".
10632 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010633 */
10634 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010635find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010636{
10637 /* skip the current separator */
10638 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010639 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010640
10641 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010642 while (*psep > path)
10643 {
10644 if (vim_ispathsep(**psep))
10645 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010646 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010647 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010648
10649 return FAIL;
10650}
10651
10652/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010653 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10654 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010655 */
10656 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010657is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010658{
10659 int j;
10660 int candidate_len;
10661 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010662 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010663 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010664
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010665 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010666 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010667 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010668 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010669
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010670 candidate_len = (int)STRLEN(maybe_unique);
10671 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010672 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010673 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010674
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010675 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010676 if (fnamecmp(maybe_unique, rival) == 0
10677 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010678 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010679 }
10680
Bram Moolenaar162bd912010-07-28 22:29:10 +020010681 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010682}
10683
10684/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010685 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010686 * paths are expanded to their equivalent fullpath. This includes the "."
10687 * (relative to current buffer directory) and empty path (relative to current
10688 * directory) notations.
10689 *
10690 * TODO: handle upward search (;) and path limiter (**N) notations by
10691 * expanding each into their equivalent path(s).
10692 */
10693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010694expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010695{
10696 char_u *path_option = *curbuf->b_p_path == NUL
10697 ? p_path : curbuf->b_p_path;
10698 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010699 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010700 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010701
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010702 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010703 return;
10704
10705 while (*path_option != NUL)
10706 {
10707 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10708
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010709 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010710 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010711 /* Relative to current buffer:
10712 * "/path/file" + "." -> "/path/"
10713 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010714 if (curbuf->b_ffname == NULL)
10715 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010716 p = gettail(curbuf->b_ffname);
10717 len = (int)(p - curbuf->b_ffname);
10718 if (len + (int)STRLEN(buf) >= MAXPATHL)
10719 continue;
10720 if (buf[1] == NUL)
10721 buf[len] = NUL;
10722 else
10723 STRMOVE(buf + len, buf + 2);
10724 mch_memmove(buf, curbuf->b_ffname, len);
10725 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010726 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010727 else if (buf[0] == NUL)
10728 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010729 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010730 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010731 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010732 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010733 else if (!mch_isFullName(buf))
10734 {
10735 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010736 len = (int)STRLEN(curdir);
10737 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010738 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010739 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010740 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010741 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010742 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010743 }
10744
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010745 if (ga_grow(gap, 1) == FAIL)
10746 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010747
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010748# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010749 /* Avoid the path ending in a backslash, it fails when a comma is
10750 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010751 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010752 if (buf[len - 1] == '\\')
10753 buf[len - 1] = '/';
10754# endif
10755
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010756 p = vim_strsave(buf);
10757 if (p == NULL)
10758 break;
10759 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010760 }
10761
10762 vim_free(buf);
10763}
10764
10765/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010766 * Returns a pointer to the file or directory name in "fname" that matches the
10767 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010768 *
10769 * path: /foo/bar/baz
10770 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010771 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010772 */
10773 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010774get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010775{
10776 int i;
10777 int maxlen = 0;
10778 char_u **path_part = (char_u **)gap->ga_data;
10779 char_u *cutoff = NULL;
10780
10781 for (i = 0; i < gap->ga_len; i++)
10782 {
10783 int j = 0;
10784
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010785 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010786# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010787 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10788#endif
10789 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010790 j++;
10791 if (j > maxlen)
10792 {
10793 maxlen = j;
10794 cutoff = &fname[j];
10795 }
10796 }
10797
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010798 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010799 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010800 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010801 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010802
10803 return cutoff;
10804}
10805
10806/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010807 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10808 * that they are unique with respect to each other while conserving the part
10809 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010810 */
10811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010812uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010813{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010814 int i;
10815 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010816 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010817 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010818 char_u *pat;
10819 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010820 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010821 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010822 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010823 char_u **in_curdir = NULL;
10824 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010825
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010826 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010827 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010828
10829 /*
10830 * We need to prepend a '*' at the beginning of file_pattern so that the
10831 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010832 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010833 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010834 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010835 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010836 if (file_pattern == NULL)
10837 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010838 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010839 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010840 STRCAT(file_pattern, pattern);
10841 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10842 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010843 if (pat == NULL)
10844 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010845
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010846 regmatch.rm_ic = TRUE; /* always ignore case */
10847 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10848 vim_free(pat);
10849 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010850 return;
10851
Bram Moolenaar162bd912010-07-28 22:29:10 +020010852 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010853 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010854 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010855 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010856
10857 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010858 if (in_curdir == NULL)
10859 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010860
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010861 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010862 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010863 char_u *path = fnames[i];
10864 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010865 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010866 char_u *pathsep_p;
10867 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010868
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010869 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010870 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010871 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010872 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010873 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010874
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010875 /* Shorten the filename while maintaining its uniqueness */
10876 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010877
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010878 /* Don't assume all files can be reached without path when search
10879 * pattern starts with star star slash, so only remove path_cutoff
10880 * when possible. */
10881 if (pattern[0] == '*' && pattern[1] == '*'
10882 && vim_ispathsep_nocolon(pattern[2])
10883 && path_cutoff != NULL
10884 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10885 && is_unique(path_cutoff, gap, i))
10886 {
10887 sort_again = TRUE;
10888 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10889 }
10890 else
10891 {
10892 /* Here all files can be reached without path, so get shortest
10893 * unique path. We start at the end of the path. */
10894 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010895
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010896 while (find_previous_pathsep(path, &pathsep_p))
10897 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10898 && is_unique(pathsep_p + 1, gap, i)
10899 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10900 {
10901 sort_again = TRUE;
10902 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10903 break;
10904 }
10905 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010906
10907 if (mch_isFullName(path))
10908 {
10909 /*
10910 * Last resort: shorten relative to curdir if possible.
10911 * 'possible' means:
10912 * 1. It is under the current directory.
10913 * 2. The result is actually shorter than the original.
10914 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010915 * Before curdir After
10916 * /foo/bar/file.txt /foo/bar ./file.txt
10917 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10918 * /file.txt / /file.txt
10919 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010920 */
10921 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010922 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010923#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010924 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010925 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010926 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010927 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010928 && !vim_ispathsep(*short_name)
10929#endif
10930 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010931 {
10932 STRCPY(path, ".");
10933 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010934 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010935 }
10936 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010937 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010938 }
10939
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010940 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010941 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010942 {
10943 char_u *rel_path;
10944 char_u *path = in_curdir[i];
10945
10946 if (path == NULL)
10947 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010948
10949 /* If the {filename} is not unique, change it to ./{filename}.
10950 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010951 short_name = shorten_fname(path, curdir);
10952 if (short_name == NULL)
10953 short_name = path;
10954 if (is_unique(short_name, gap, i))
10955 {
10956 STRCPY(fnames[i], short_name);
10957 continue;
10958 }
10959
10960 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10961 if (rel_path == NULL)
10962 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010963 STRCPY(rel_path, ".");
10964 add_pathsep(rel_path);
10965 STRCAT(rel_path, short_name);
10966
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010967 vim_free(fnames[i]);
10968 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010969 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010970 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010971 }
10972
Bram Moolenaar162bd912010-07-28 22:29:10 +020010973theend:
10974 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010975 if (in_curdir != NULL)
10976 {
10977 for (i = 0; i < gap->ga_len; i++)
10978 vim_free(in_curdir[i]);
10979 vim_free(in_curdir);
10980 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010981 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010982 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010983
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010984 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010985 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010986}
10987
10988/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010989 * Calls globpath() with 'path' values for the given pattern and stores the
10990 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010991 * Returns the total number of matches.
10992 */
10993 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010994expand_in_path(
10995 garray_T *gap,
10996 char_u *pattern,
10997 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010998{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010999 char_u *curdir;
11000 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011001 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011002 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011003
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011004 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020011005 return 0;
11006 mch_dirname(curdir, MAXPATHL);
11007
Bram Moolenaar0be992e2010-08-12 21:50:51 +020011008 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011009 expand_path_option(curdir, &path_ga);
11010 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020011011 if (path_ga.ga_len == 0)
11012 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011013
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020011014 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011015 ga_clear_strings(&path_ga);
11016 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011017 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020011018
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011019 if (flags & EW_ICASE)
11020 glob_flags |= WILD_ICASE;
11021 if (flags & EW_ADDSLASH)
11022 glob_flags |= WILD_ADD_SLASH;
11023 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011024 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011025
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011026 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011027}
11028#endif
11029
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011030#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11031/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011032 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11033 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011034 */
11035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011036remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011037{
11038 int i;
11039 int j;
11040 char_u **fnames = (char_u **)gap->ga_data;
11041
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011042 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011043 for (i = gap->ga_len - 1; i > 0; --i)
11044 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11045 {
11046 vim_free(fnames[i]);
11047 for (j = i + 1; j < gap->ga_len; ++j)
11048 fnames[j - 1] = fnames[j];
11049 --gap->ga_len;
11050 }
11051}
11052#endif
11053
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010011054static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011055
11056/*
11057 * Return TRUE if "p" contains what looks like an environment variable.
11058 * Allowing for escaping.
11059 */
11060 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011061has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011062{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011063 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011064 {
11065 if (*p == '\\' && p[1] != NUL)
11066 ++p;
11067 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011068#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011069 "$%"
11070#else
11071 "$"
11072#endif
11073 , *p) != NULL)
11074 return TRUE;
11075 }
11076 return FALSE;
11077}
11078
11079#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010011080static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011081
11082/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011083 * Return TRUE if "p" contains a special wildcard character, one that Vim
11084 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011085 */
11086 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011087has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011088{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011089 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011090 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011091 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011092 if (*p == '\\' && p[1] != NUL)
11093 ++p;
11094 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11095 return TRUE;
11096 }
11097 return FALSE;
11098}
11099#endif
11100
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101/*
11102 * Generic wildcard expansion code.
11103 *
11104 * Characters in "pat" that should not be expanded must be preceded with a
11105 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11106 *
11107 * Return FAIL when no single file was found. In this case "num_file" is not
11108 * set, and "file" may contain an error message.
11109 * Return OK when some files found. "num_file" is set to the number of
11110 * matches, "file" to the array of matches. Call FreeWild() later.
11111 */
11112 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011113gen_expand_wildcards(
11114 int num_pat, /* number of input patterns */
11115 char_u **pat, /* array of input patterns */
11116 int *num_file, /* resulting number of files */
11117 char_u ***file, /* array of resulting files */
11118 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119{
11120 int i;
11121 garray_T ga;
11122 char_u *p;
11123 static int recursive = FALSE;
11124 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011125 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011126#if defined(FEAT_SEARCHPATH)
11127 int did_expand_in_path = FALSE;
11128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129
11130 /*
11131 * expand_env() is called to expand things like "~user". If this fails,
11132 * it calls ExpandOne(), which brings us back here. In this case, always
11133 * call the machine specific expansion function, if possible. Otherwise,
11134 * return FAIL.
11135 */
11136 if (recursive)
11137#ifdef SPECIAL_WILDCHAR
11138 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11139#else
11140 return FAIL;
11141#endif
11142
11143#ifdef SPECIAL_WILDCHAR
11144 /*
11145 * If there are any special wildcard characters which we cannot handle
11146 * here, call machine specific function for all the expansion. This
11147 * avoids starting the shell for each argument separately.
11148 * For `=expr` do use the internal function.
11149 */
11150 for (i = 0; i < num_pat; i++)
11151 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011152 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153# ifdef VIM_BACKTICK
11154 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11155# endif
11156 )
11157 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11158 }
11159#endif
11160
11161 recursive = TRUE;
11162
11163 /*
11164 * The matching file names are stored in a growarray. Init it empty.
11165 */
11166 ga_init2(&ga, (int)sizeof(char_u *), 30);
11167
11168 for (i = 0; i < num_pat; ++i)
11169 {
11170 add_pat = -1;
11171 p = pat[i];
11172
11173#ifdef VIM_BACKTICK
11174 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011177 if (add_pat == -1)
11178 retval = FAIL;
11179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 else
11181#endif
11182 {
11183 /*
11184 * First expand environment variables, "~/" and "~user/".
11185 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011186 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011188 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 if (p == NULL)
11190 p = pat[i];
11191#ifdef UNIX
11192 /*
11193 * On Unix, if expand_env() can't expand an environment
11194 * variable, use the shell to do that. Discard previously
11195 * found file names and start all over again.
11196 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011197 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 {
11199 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011200 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011202 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 recursive = FALSE;
11204 return i;
11205 }
11206#endif
11207 }
11208
11209 /*
11210 * If there are wildcards: Expand file names and add each match to
11211 * the list. If there is no match, and EW_NOTFOUND is given, add
11212 * the pattern.
11213 * If there are no wildcards: Add the file name if it exists or
11214 * when EW_NOTFOUND is given.
11215 */
11216 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011217 {
11218#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011219 if ((flags & EW_PATH)
11220 && !mch_isFullName(p)
11221 && !(p[0] == '.'
11222 && (vim_ispathsep(p[1])
11223 || (p[1] == '.' && vim_ispathsep(p[2]))))
11224 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011225 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011226 /* :find completion where 'path' is used.
11227 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011228 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011229 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011230 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011231 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011232 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011233 else
11234#endif
11235 add_pat = mch_expandpath(&ga, p, flags);
11236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238
11239 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11240 {
11241 char_u *t = backslash_halve_save(p);
11242
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11244 * "vim c:/" work. */
11245 if (flags & EW_NOTFOUND)
11246 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011247 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 addfile(&ga, t, flags);
11249 vim_free(t);
11250 }
11251
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011252#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011253 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011254 uniquefy_paths(&ga, p);
11255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 if (p != pat[i])
11257 vim_free(p);
11258 }
11259
11260 *num_file = ga.ga_len;
11261 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11262
11263 recursive = FALSE;
11264
Bram Moolenaar336bd622016-01-17 18:23:58 +010011265 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266}
11267
11268# ifdef VIM_BACKTICK
11269
11270/*
11271 * Return TRUE if we can expand this backtick thing here.
11272 */
11273 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011274vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275{
11276 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11277}
11278
11279/*
11280 * Expand an item in `backticks` by executing it as a command.
11281 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011282 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 */
11284 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011285expand_backtick(
11286 garray_T *gap,
11287 char_u *pat,
11288 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289{
11290 char_u *p;
11291 char_u *cmd;
11292 char_u *buffer;
11293 int cnt = 0;
11294 int i;
11295
11296 /* Create the command: lop off the backticks. */
11297 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11298 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011299 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300
11301#ifdef FEAT_EVAL
11302 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011303 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 else
11305#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011306 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011307 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 vim_free(cmd);
11309 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011310 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
11312 cmd = buffer;
11313 while (*cmd != NUL)
11314 {
11315 cmd = skipwhite(cmd); /* skip over white space */
11316 p = cmd;
11317 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11318 ++p;
11319 /* add an entry if it is not empty */
11320 if (p > cmd)
11321 {
11322 i = *p;
11323 *p = NUL;
11324 addfile(gap, cmd, flags);
11325 *p = i;
11326 ++cnt;
11327 }
11328 cmd = p;
11329 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11330 ++cmd;
11331 }
11332
11333 vim_free(buffer);
11334 return cnt;
11335}
11336# endif /* VIM_BACKTICK */
11337
11338/*
11339 * Add a file to a file list. Accepted flags:
11340 * EW_DIR add directories
11341 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011342 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 * EW_NOTFOUND add even when it doesn't exist
11344 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011345 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 */
11347 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011348addfile(
11349 garray_T *gap,
11350 char_u *f, /* filename */
11351 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352{
11353 char_u *p;
11354 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011355 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011357 /* if the file/dir/link doesn't exist, may not add it */
11358 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011359 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 return;
11361
11362#ifdef FNAME_ILLEGAL
11363 /* if the file/dir contains illegal characters, don't add it */
11364 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11365 return;
11366#endif
11367
11368 isdir = mch_isdir(f);
11369 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11370 return;
11371
Bram Moolenaarb5971142015-03-21 17:32:19 +010011372 /* If the file isn't executable, may not add it. Do accept directories.
11373 * When invoked from expand_shellcmd() do not use $PATH. */
11374 if (!isdir && (flags & EW_EXEC)
11375 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011376 return;
11377
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 /* Make room for another item in the file list. */
11379 if (ga_grow(gap, 1) == FAIL)
11380 return;
11381
11382 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11383 if (p == NULL)
11384 return;
11385
11386 STRCPY(p, f);
11387#ifdef BACKSLASH_IN_FILENAME
11388 slash_adjust(p);
11389#endif
11390 /*
11391 * Append a slash or backslash after directory names if none is present.
11392 */
11393#ifndef DONT_ADD_PATHSEP_TO_DIR
11394 if (isdir && (flags & EW_ADDSLASH))
11395 add_pathsep(p);
11396#endif
11397 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398}
11399#endif /* !NO_EXPANDPATH */
11400
11401#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11402
11403#ifndef SEEK_SET
11404# define SEEK_SET 0
11405#endif
11406#ifndef SEEK_END
11407# define SEEK_END 2
11408#endif
11409
11410/*
11411 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011412 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11413 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 * Returns an allocated string, or NULL for error.
11415 */
11416 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011417get_cmd_output(
11418 char_u *cmd,
11419 char_u *infile, /* optional input file name */
11420 int flags, /* can be SHELL_SILENT */
11421 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422{
11423 char_u *tempname;
11424 char_u *command;
11425 char_u *buffer = NULL;
11426 int len;
11427 int i = 0;
11428 FILE *fd;
11429
11430 if (check_restricted() || check_secure())
11431 return NULL;
11432
11433 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011434 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 {
11436 EMSG(_(e_notmp));
11437 return NULL;
11438 }
11439
11440 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011441 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 if (command == NULL)
11443 goto done;
11444
11445 /*
11446 * Call the shell to execute the command (errors are ignored).
11447 * Don't check timestamps here.
11448 */
11449 ++no_check_timestamps;
11450 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11451 --no_check_timestamps;
11452
11453 vim_free(command);
11454
11455 /*
11456 * read the names from the file into memory
11457 */
11458# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011459 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 fd = mch_fopen((char *)tempname, "r");
11461# else
11462 fd = mch_fopen((char *)tempname, READBIN);
11463# endif
11464
11465 if (fd == NULL)
11466 {
11467 EMSG2(_(e_notopen), tempname);
11468 goto done;
11469 }
11470
11471 fseek(fd, 0L, SEEK_END);
11472 len = ftell(fd); /* get size of temp file */
11473 fseek(fd, 0L, SEEK_SET);
11474
11475 buffer = alloc(len + 1);
11476 if (buffer != NULL)
11477 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11478 fclose(fd);
11479 mch_remove(tempname);
11480 if (buffer == NULL)
11481 goto done;
11482#ifdef VMS
11483 len = i; /* VMS doesn't give us what we asked for... */
11484#endif
11485 if (i != len)
11486 {
11487 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011488 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011490 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011491 {
11492 /* Change NUL into SOH, otherwise the string is truncated. */
11493 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011494 if (buffer[i] == NUL)
11495 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011496
Bram Moolenaar162bd912010-07-28 22:29:10 +020011497 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011498 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011499 else
11500 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501
11502done:
11503 vim_free(tempname);
11504 return buffer;
11505}
11506#endif
11507
11508/*
11509 * Free the list of files returned by expand_wildcards() or other expansion
11510 * functions.
11511 */
11512 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011513FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011515 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517 while (count--)
11518 vim_free(files[count]);
11519 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520}
11521
11522/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011523 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 * Don't do this when still processing a command or a mapping.
11525 * Don't do this when inside a ":normal" command.
11526 */
11527 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011528goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529{
11530 return (p_im && stuff_empty() && typebuf_typed());
11531}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011532
11533/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011534 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011535 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11536 * - Remove any argument. E.g., "csh -f" -> "csh".
11537 * But don't allow a space in the path, so that this works:
11538 * "/usr/bin/csh --rcfile ~/.cshrc"
11539 * But don't do that for Windows, it's common to have a space in the path.
11540 */
11541 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011542get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011543{
11544 char_u *p;
11545
11546#ifdef WIN3264
11547 p = gettail(p_sh);
11548 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11549#else
11550 p = skiptowhite(p_sh);
11551 if (*p == NUL)
11552 {
11553 /* No white space, use the tail. */
11554 p = vim_strsave(gettail(p_sh));
11555 }
11556 else
11557 {
11558 char_u *p1, *p2;
11559
11560 /* Find the last path separator before the space. */
11561 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011562 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011563 if (vim_ispathsep(*p2))
11564 p1 = p2 + 1;
11565 p = vim_strnsave(p1, (int)(p - p1));
11566 }
11567#endif
11568 return p;
11569}