blob: 1647aa952413b95109648cb248bb902c463dca7b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar828c3d72018-06-19 18:58:07 +020017#if defined(FEAT_CMDL_COMPL) && defined(WIN3264)
18# include <lm.h>
19#endif
20
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static char_u *vim_version_dir(char_u *vimdir);
22static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020023#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020025#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
Bram Moolenaar24305862012-08-15 14:05:05 +020028/* All user names (for ~user completion as done by shell). */
29#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
30static garray_T ga_users;
31#endif
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033/*
34 * Count the size (in window cells) of the indent in the current line.
35 */
36 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010037get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000038{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020039#ifdef FEAT_VARTABS
40 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
41 curbuf->b_p_vts_array, FALSE);
42#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020043 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47/*
48 * Count the size (in window cells) of the indent in line "lnum".
49 */
50 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010051get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020053#ifdef FEAT_VARTABS
54 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
55 curbuf->b_p_vts_array, FALSE);
56#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020057 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000059}
60
61#if defined(FEAT_FOLDING) || defined(PROTO)
62/*
63 * Count the size (in window cells) of the indent in line "lnum" of buffer
64 * "buf".
65 */
66 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010067get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020069#ifdef FEAT_VARTABS
70 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
71 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
72#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000075}
76#endif
77
78/*
79 * count the size (in window cells) of the indent in line "ptr", with
80 * 'tabstop' at "ts"
81 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010083get_indent_str(
84 char_u *ptr,
85 int ts,
86 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087{
88 int count = 0;
89
90 for ( ; *ptr; ++ptr)
91 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020092 if (*ptr == TAB)
93 {
94 if (!list || lcs_tab1) /* count a tab for what it is worth */
95 count += ts - (count % ts);
96 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020097 /* In list mode, when tab is not set, count screen char width
98 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020099 count += ptr2cells(ptr);
100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 else if (*ptr == ' ')
102 ++count; /* count a space for one */
103 else
104 break;
105 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000106 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107}
108
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200109#ifdef FEAT_VARTABS
110/*
111 * Count the size (in window cells) of the indent in line "ptr", using
112 * variable tabstops.
113 * if "list" is TRUE, count only screen size for tabs.
114 */
115 int
116get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
117{
118 int count = 0;
119
120 for ( ; *ptr; ++ptr)
121 {
122 if (*ptr == TAB) /* count a tab for what it is worth */
123 {
124 if (!list || lcs_tab1)
125 count += tabstop_padding(count, ts, vts);
126 else
127 /* In list mode, when tab is not set, count screen char width
128 * for Tab, displays: ^I */
129 count += ptr2cells(ptr);
130 }
131 else if (*ptr == ' ')
132 ++count; /* count a space for one */
133 else
134 break;
135 }
136 return count;
137}
138#endif
139
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140/*
141 * Set the indent of the current line.
142 * Leaves the cursor on the first non-blank in the line.
143 * Caller must take care of undo.
144 * "flags":
145 * SIN_CHANGED: call changed_bytes() if the line was changed.
146 * SIN_INSERT: insert the indent in front of the line.
147 * SIN_UNDO: save line for undo before changing it.
148 * Returns TRUE if the line was changed.
149 */
150 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100151set_indent(
152 int size, /* measured in spaces */
153 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 char_u *p;
156 char_u *newline;
157 char_u *oldline;
158 char_u *s;
159 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000160 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 int line_len;
162 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000163 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200164#ifdef FEAT_VARTABS
165 int ind_col = 0;
166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000168 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000169 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000170 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
172 /*
173 * First check if there is anything to do and compute the number of
174 * characters needed for the indent.
175 */
176 todo = size;
177 ind_len = 0;
178 p = oldline = ml_get_curline();
179
180 /* Calculate the buffer size for the new indent, and check to see if it
181 * isn't already set */
182
Bram Moolenaar5002c292007-07-24 13:26:15 +0000183 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
184 * 'preserveindent' are set count the number of characters at the
185 * beginning of the line to be copied */
186 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {
188 /* If 'preserveindent' is set then reuse as much as possible of
189 * the existing indent structure for the new indent */
190 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
191 {
192 ind_done = 0;
193
194 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100195 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 {
197 if (*p == TAB)
198 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200199#ifdef FEAT_VARTABS
200 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
201 curbuf->b_p_vts_array);
202#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 tab_pad = (int)curbuf->b_p_ts
204 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 /* stop if this tab will overshoot the target */
207 if (todo < tab_pad)
208 break;
209 todo -= tab_pad;
210 ++ind_len;
211 ind_done += tab_pad;
212 }
213 else
214 {
215 --todo;
216 ++ind_len;
217 ++ind_done;
218 }
219 ++p;
220 }
221
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200222#ifdef FEAT_VARTABS
223 /* These diverge from this point. */
224 ind_col = ind_done;
225#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000226 /* Set initial number of whitespace chars to copy if we are
227 * preserving indent but expandtab is set */
228 if (curbuf->b_p_et)
229 orig_char_len = ind_len;
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200232#ifdef FEAT_VARTABS
233 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
234 curbuf->b_p_vts_array);
235#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200237#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000238 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {
240 doit = TRUE;
241 todo -= tab_pad;
242 ++ind_len;
243 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200244#ifdef FEAT_VARTABS
245 ind_col += tab_pad;
246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 }
248 }
249
250 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200251#ifdef FEAT_VARTABS
252 for (;;)
253 {
254 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
255 curbuf->b_p_vts_array);
256 if (todo < tab_pad)
257 break;
258 if (*p != TAB)
259 doit = TRUE;
260 else
261 ++p;
262 todo -= tab_pad;
263 ++ind_len;
264 ind_col += tab_pad;
265 }
266#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 while (todo >= (int)curbuf->b_p_ts)
268 {
269 if (*p != TAB)
270 doit = TRUE;
271 else
272 ++p;
273 todo -= (int)curbuf->b_p_ts;
274 ++ind_len;
275 /* ind_done += (int)curbuf->b_p_ts; */
276 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 }
279 /* count spaces required for indent */
280 while (todo > 0)
281 {
282 if (*p != ' ')
283 doit = TRUE;
284 else
285 ++p;
286 --todo;
287 ++ind_len;
288 /* ++ind_done; */
289 }
290
291 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100292 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 return FALSE;
294
295 /* Allocate memory for the new line. */
296 if (flags & SIN_INSERT)
297 p = oldline;
298 else
299 p = skipwhite(p);
300 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000301
302 /* If 'preserveindent' and 'expandtab' are both set keep the original
303 * characters and allocate accordingly. We will fill the rest with spaces
304 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000305 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000306 {
307 newline = alloc(orig_char_len + size - ind_done + line_len);
308 if (newline == NULL)
309 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000310 todo = size - ind_done;
311 ind_len = orig_char_len + todo; /* Set total length of indent in
312 * characters, which may have been
313 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000314 p = oldline;
315 s = newline;
316 while (orig_char_len > 0)
317 {
318 *s++ = *p++;
319 orig_char_len--;
320 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000321
Bram Moolenaar5002c292007-07-24 13:26:15 +0000322 /* Skip over any additional white space (useful when newindent is less
323 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100324 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000325 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000326
Bram Moolenaar5002c292007-07-24 13:26:15 +0000327 }
328 else
329 {
330 todo = size;
331 newline = alloc(ind_len + line_len);
332 if (newline == NULL)
333 return FALSE;
334 s = newline;
335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
337 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 /* if 'expandtab' isn't set: use TABs */
339 if (!curbuf->b_p_et)
340 {
341 /* If 'preserveindent' is set then reuse as much as possible of
342 * the existing indent structure for the new indent */
343 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
344 {
345 p = oldline;
346 ind_done = 0;
347
Bram Moolenaar1c465442017-03-12 20:10:05 +0100348 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {
350 if (*p == TAB)
351 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200352#ifdef FEAT_VARTABS
353 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
354 curbuf->b_p_vts_array);
355#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 tab_pad = (int)curbuf->b_p_ts
357 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 /* stop if this tab will overshoot the target */
360 if (todo < tab_pad)
361 break;
362 todo -= tab_pad;
363 ind_done += tab_pad;
364 }
365 else
366 {
367 --todo;
368 ++ind_done;
369 }
370 *s++ = *p++;
371 }
372
373 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200374#ifdef FEAT_VARTABS
375 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
376 curbuf->b_p_vts_array);
377#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (todo >= tab_pad)
381 {
382 *s++ = TAB;
383 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200384#ifdef FEAT_VARTABS
385 ind_done += tab_pad;
386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388
389 p = skipwhite(p);
390 }
391
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200392#ifdef FEAT_VARTABS
393 for (;;)
394 {
395 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
396 curbuf->b_p_vts_array);
397 if (todo < tab_pad)
398 break;
399 *s++ = TAB;
400 todo -= tab_pad;
401 ind_done += tab_pad;
402 }
403#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 while (todo >= (int)curbuf->b_p_ts)
405 {
406 *s++ = TAB;
407 todo -= (int)curbuf->b_p_ts;
408 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 }
411 while (todo > 0)
412 {
413 *s++ = ' ';
414 --todo;
415 }
416 mch_memmove(s, p, (size_t)line_len);
417
418 /* Replace the line (unless undo fails). */
419 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
420 {
421 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
422 if (flags & SIN_CHANGED)
423 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200424 /* Correct saved cursor position if it is in this line. */
425 if (saved_cursor.lnum == curwin->w_cursor.lnum)
426 {
427 if (saved_cursor.col >= (colnr_T)(p - oldline))
428 /* cursor was after the indent, adjust for the number of
429 * bytes added/removed */
430 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
431 else if (saved_cursor.col >= (colnr_T)(s - newline))
432 /* cursor was in the indent, and is now after it, put it back
433 * at the start of the indent (replacing spaces with TAB) */
434 saved_cursor.col = (colnr_T)(s - newline);
435 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000436 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 }
438 else
439 vim_free(newline);
440
441 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000442 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443}
444
445/*
446 * Copy the indent from ptr to the current line (and fill to size)
447 * Leaves the cursor on the first non-blank in the line.
448 * Returns TRUE if the line was changed.
449 */
450 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100451copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 char_u *p = NULL;
454 char_u *line = NULL;
455 char_u *s;
456 int todo;
457 int ind_len;
458 int line_len = 0;
459 int tab_pad;
460 int ind_done;
461 int round;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200462#ifdef FEAT_VARTABS
463 int ind_col;
464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
466 /* Round 1: compute the number of characters needed for the indent
467 * Round 2: copy the characters. */
468 for (round = 1; round <= 2; ++round)
469 {
470 todo = size;
471 ind_len = 0;
472 ind_done = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200473#ifdef FEAT_VARTABS
474 ind_col = 0;
475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 s = src;
477
478 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100479 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 {
481 if (*s == TAB)
482 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200483#ifdef FEAT_VARTABS
484 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
485 curbuf->b_p_vts_array);
486#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 tab_pad = (int)curbuf->b_p_ts
488 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 /* Stop if this tab will overshoot the target */
491 if (todo < tab_pad)
492 break;
493 todo -= tab_pad;
494 ind_done += tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200495#ifdef FEAT_VARTABS
496 ind_col += tab_pad;
497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 }
499 else
500 {
501 --todo;
502 ++ind_done;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200503#ifdef FEAT_VARTABS
504 ++ind_col;
505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 }
507 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000508 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 *p++ = *s;
510 ++s;
511 }
512
513 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200514#ifdef FEAT_VARTABS
515 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
516 curbuf->b_p_vts_array);
517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200519#endif
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200520 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 {
522 todo -= tab_pad;
523 ++ind_len;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200524#ifdef FEAT_VARTABS
525 ind_col += tab_pad;
526#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000527 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 *p++ = TAB;
529 }
530
531 /* Add tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200532 if (!curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200534#ifdef FEAT_VARTABS
535 for (;;)
536 {
537 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
538 curbuf->b_p_vts_array);
539 if (todo < tab_pad)
540 break;
541 todo -= tab_pad;
542 ++ind_len;
543 ind_col += tab_pad;
544 if (p != NULL)
545 *p++ = TAB;
546 }
547#else
548 while (todo >= (int)curbuf->b_p_ts)
549 {
550 todo -= (int)curbuf->b_p_ts;
551 ++ind_len;
552 if (p != NULL)
553 *p++ = TAB;
554 }
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 }
557
558 /* Count/add spaces required for indent */
559 while (todo > 0)
560 {
561 --todo;
562 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000563 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 *p++ = ' ';
565 }
566
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000567 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 /* Allocate memory for the result: the copied indent, new indent
570 * and the rest of the line. */
571 line_len = (int)STRLEN(ml_get_curline()) + 1;
572 line = alloc(ind_len + line_len);
573 if (line == NULL)
574 return FALSE;
575 p = line;
576 }
577 }
578
579 /* Append the original line */
580 mch_memmove(p, ml_get_curline(), (size_t)line_len);
581
582 /* Replace the line */
583 ml_replace(curwin->w_cursor.lnum, line, FALSE);
584
585 /* Put the cursor after the indent. */
586 curwin->w_cursor.col = ind_len;
587 return TRUE;
588}
589
590/*
591 * Return the indent of the current line after a number. Return -1 if no
592 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000593 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 */
595 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100596get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 colnr_T col;
599 pos_T pos;
600
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200601 regmatch_T regmatch;
602 int lead_len = 0; /* length of comment leader */
603
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (lnum > curbuf->b_ml.ml_line_count)
605 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000606 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200607
608#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200609 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
610 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200611 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000612#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200613 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
614 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200615 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200616 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200617
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200618 /* vim_regexec() expects a pointer to a line. This lets us
619 * start matching for the flp beyond any comment leader... */
620 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200621 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200622 pos.lnum = lnum;
623 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200624#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200625 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200626#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200627 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200628 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200629 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000630
631 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 getvcol(curwin, &pos, &col, NULL, NULL);
634 return (int)col;
635}
636
Bram Moolenaar597a4222014-06-25 14:39:50 +0200637#if defined(FEAT_LINEBREAK) || defined(PROTO)
638/*
639 * Return appropriate space number for breakindent, taking influencing
640 * parameters into account. Window must be specified, since it is not
641 * necessarily always the current one.
642 */
643 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100644get_breakindent_win(
645 win_T *wp,
646 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200647{
648 static int prev_indent = 0; /* cached indent value */
649 static long prev_ts = 0L; /* cached tabstop value */
650 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100651 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200652#ifdef FEAT_VARTABS
653 static int *prev_vts = NULL; /* cached vartabs values */
654#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200655 int bri = 0;
656 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200657 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200658 - ((wp->w_p_nu || wp->w_p_rnu)
659 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
660 ? number_width(wp) + 1 : 0);
661
662 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200663 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200664 || prev_tick != CHANGEDTICK(wp->w_buffer)
665#ifdef FEAT_VARTABS
666 || prev_vts != wp->w_buffer->b_p_vts_array
667#endif
668 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200669 {
670 prev_line = line;
671 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100672 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200673#ifdef FEAT_VARTABS
674 prev_vts = wp->w_buffer->b_p_vts_array;
675 prev_indent = get_indent_str_vtab(line,
676 (int)wp->w_buffer->b_p_ts,
677 wp->w_buffer->b_p_vts_array, wp->w_p_list);
678#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200679 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200680 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200681#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200682 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200683 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200684
685 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200686 if (wp->w_p_brisbr)
687 bri -= vim_strsize(p_sbr);
688
689 /* Add offset for number column, if 'n' is in 'cpoptions' */
690 bri += win_col_off2(wp);
691
692 /* never indent past left window margin */
693 if (bri < 0)
694 bri = 0;
695 /* always leave at least bri_min characters on the left,
696 * if text width is sufficient */
697 else if (bri > eff_wwidth - wp->w_p_brimin)
698 bri = (eff_wwidth - wp->w_p_brimin < 0)
699 ? 0 : eff_wwidth - wp->w_p_brimin;
700
701 return bri;
702}
703#endif
704
705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
707
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100708static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
710/*
711 * Return TRUE if the string "line" starts with a word from 'cinwords'.
712 */
713 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100714cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 char_u *cinw;
717 char_u *cinw_buf;
718 int cinw_len;
719 int retval = FALSE;
720 int len;
721
722 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
723 cinw_buf = alloc((unsigned)cinw_len);
724 if (cinw_buf != NULL)
725 {
726 line = skipwhite(line);
727 for (cinw = curbuf->b_p_cinw; *cinw; )
728 {
729 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
730 if (STRNCMP(line, cinw_buf, len) == 0
731 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
732 {
733 retval = TRUE;
734 break;
735 }
736 }
737 vim_free(cinw_buf);
738 }
739 return retval;
740}
741#endif
742
743/*
744 * open_line: Add a new line below or above the current line.
745 *
746 * For VREPLACE mode, we only add a new line when we get to the end of the
747 * file, otherwise we just start replacing the next line.
748 *
749 * Caller must take care of undo. Since VREPLACE may affect any number of
750 * lines however, it may call u_save_cursor() again when starting to change a
751 * new line.
752 * "flags": OPENLINE_DELSPACES delete spaces after cursor
753 * OPENLINE_DO_COM format comments
754 * OPENLINE_KEEPTRAIL keep trailing spaces
755 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200756 * OPENLINE_COM_LIST format comments with list or 2nd line indent
757 *
758 * "second_line_indent": indent for after ^^D in Insert mode or if flag
759 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200761 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
763 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100764open_line(
765 int dir, /* FORWARD or BACKWARD */
766 int flags,
767 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
769 char_u *saved_line; /* copy of the original line */
770 char_u *next_line = NULL; /* copy of the next line */
771 char_u *p_extra = NULL; /* what goes to next line */
772 int less_cols = 0; /* less columns for mark in new line */
773 int less_cols_off = 0; /* columns to skip for mark adjust */
774 pos_T old_cursor; /* old cursor position */
775 int newcol = 0; /* new cursor column */
776 int newindent = 0; /* auto-indent of the new line */
777 int n;
778 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200779 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#ifdef FEAT_COMMENTS
781 int extra_len = 0; /* length of p_extra string */
782 int lead_len; /* length of comment leader */
783 char_u *lead_flags; /* position in 'comments' for comment leader */
784 char_u *leader = NULL; /* copy of comment leader */
785#endif
786 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 int saved_char = NUL; /* init for GCC */
789#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
790 pos_T *pos;
791#endif
792#ifdef FEAT_SMARTINDENT
793 int do_si = (!p_paste && curbuf->b_p_si
794# ifdef FEAT_CINDENT
795 && !curbuf->b_p_cin
796# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200797# ifdef FEAT_EVAL
798 && *curbuf->b_p_inde == NUL
799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 );
801 int no_si = FALSE; /* reset did_si afterwards */
802 int first_char = NUL; /* init for GCC */
803#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200804#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 int vreplace_mode;
806#endif
807 int did_append; /* appended a new line */
808 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
809
810 /*
811 * make a copy of the current line so we can mess with it
812 */
813 saved_line = vim_strsave(ml_get_curline());
814 if (saved_line == NULL) /* out of memory! */
815 return FALSE;
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (State & VREPLACE_FLAG)
818 {
819 /*
820 * With VREPLACE we make a copy of the next line, which we will be
821 * starting to replace. First make the new line empty and let vim play
822 * with the indenting and comment leader to its heart's content. Then
823 * we grab what it ended up putting on the new line, put back the
824 * original line, and call ins_char() to put each new character onto
825 * the line, replacing what was there before and pushing the right
826 * stuff onto the replace stack. -- webb.
827 */
828 if (curwin->w_cursor.lnum < orig_line_count)
829 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
830 else
831 next_line = vim_strsave((char_u *)"");
832 if (next_line == NULL) /* out of memory! */
833 goto theend;
834
835 /*
836 * In VREPLACE mode, a NL replaces the rest of the line, and starts
837 * replacing the next line, so push all of the characters left on the
838 * line onto the replace stack. We'll push any other characters that
839 * might be replaced at the start of the next line (due to autoindent
840 * etc) a bit later.
841 */
842 replace_push(NUL); /* Call twice because BS over NL expects it */
843 replace_push(NUL);
844 p = saved_line + curwin->w_cursor.col;
845 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000846 {
847#ifdef FEAT_MBYTE
848 if (has_mbyte)
849 p += replace_push_mb(p);
850 else
851#endif
852 replace_push(*p++);
853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 saved_line[curwin->w_cursor.col] = NUL;
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200857 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859 p_extra = saved_line + curwin->w_cursor.col;
860#ifdef FEAT_SMARTINDENT
861 if (do_si) /* need first char after new line break */
862 {
863 p = skipwhite(p_extra);
864 first_char = *p;
865 }
866#endif
867#ifdef FEAT_COMMENTS
868 extra_len = (int)STRLEN(p_extra);
869#endif
870 saved_char = *p_extra;
871 *p_extra = NUL;
872 }
873
874 u_clearline(); /* cannot do "U" command when adding lines */
875#ifdef FEAT_SMARTINDENT
876 did_si = FALSE;
877#endif
878 ai_col = 0;
879
880 /*
881 * If we just did an auto-indent, then we didn't type anything on
882 * the prior line, and it should be truncated. Do this even if 'ai' is not
883 * set because automatically inserting a comment leader also sets did_ai.
884 */
885 if (dir == FORWARD && did_ai)
886 trunc_line = TRUE;
887
888 /*
889 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
890 * indent to use for the new line.
891 */
892 if (curbuf->b_p_ai
893#ifdef FEAT_SMARTINDENT
894 || do_si
895#endif
896 )
897 {
898 /*
899 * count white space on current line
900 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200901#ifdef FEAT_VARTABS
902 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
903 curbuf->b_p_vts_array, FALSE);
904#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200905 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200906#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200907 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
908 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
910#ifdef FEAT_SMARTINDENT
911 /*
912 * Do smart indenting.
913 * In insert/replace mode (only when dir == FORWARD)
914 * we may move some text to the next line. If it starts with '{'
915 * don't add an indent. Fixes inserting a NL before '{' in line
916 * "if (condition) {"
917 */
918 if (!trunc_line && do_si && *saved_line != NUL
919 && (p_extra == NULL || first_char != '{'))
920 {
921 char_u *ptr;
922 char_u last_char;
923
924 old_cursor = curwin->w_cursor;
925 ptr = saved_line;
926# ifdef FEAT_COMMENTS
927 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200928 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 else
930 lead_len = 0;
931# endif
932 if (dir == FORWARD)
933 {
934 /*
935 * Skip preprocessor directives, unless they are
936 * recognised as comments.
937 */
938 if (
939# ifdef FEAT_COMMENTS
940 lead_len == 0 &&
941# endif
942 ptr[0] == '#')
943 {
944 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
945 ptr = ml_get(--curwin->w_cursor.lnum);
946 newindent = get_indent();
947 }
948# ifdef FEAT_COMMENTS
949 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200950 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else
952 lead_len = 0;
953 if (lead_len > 0)
954 {
955 /*
956 * This case gets the following right:
957 * \*
958 * * A comment (read '\' as '/').
959 * *\
960 * #define IN_THE_WAY
961 * This should line up here;
962 */
963 p = skipwhite(ptr);
964 if (p[0] == '/' && p[1] == '*')
965 p++;
966 if (p[0] == '*')
967 {
968 for (p++; *p; p++)
969 {
970 if (p[0] == '/' && p[-1] == '*')
971 {
972 /*
973 * End of C comment, indent should line up
974 * with the line containing the start of
975 * the comment
976 */
977 curwin->w_cursor.col = (colnr_T)(p - ptr);
978 if ((pos = findmatch(NULL, NUL)) != NULL)
979 {
980 curwin->w_cursor.lnum = pos->lnum;
981 newindent = get_indent();
982 }
983 }
984 }
985 }
986 }
987 else /* Not a comment line */
988# endif
989 {
990 /* Find last non-blank in line */
991 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100992 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 --p;
994 last_char = *p;
995
996 /*
997 * find the character just before the '{' or ';'
998 */
999 if (last_char == '{' || last_char == ';')
1000 {
1001 if (p > ptr)
1002 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001003 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 --p;
1005 }
1006 /*
1007 * Try to catch lines that are split over multiple
1008 * lines. eg:
1009 * if (condition &&
1010 * condition) {
1011 * Should line up here!
1012 * }
1013 */
1014 if (*p == ')')
1015 {
1016 curwin->w_cursor.col = (colnr_T)(p - ptr);
1017 if ((pos = findmatch(NULL, '(')) != NULL)
1018 {
1019 curwin->w_cursor.lnum = pos->lnum;
1020 newindent = get_indent();
1021 ptr = ml_get_curline();
1022 }
1023 }
1024 /*
1025 * If last character is '{' do indent, without
1026 * checking for "if" and the like.
1027 */
1028 if (last_char == '{')
1029 {
1030 did_si = TRUE; /* do indent */
1031 no_si = TRUE; /* don't delete it when '{' typed */
1032 }
1033 /*
1034 * Look for "if" and the like, use 'cinwords'.
1035 * Don't do this if the previous line ended in ';' or
1036 * '}'.
1037 */
1038 else if (last_char != ';' && last_char != '}'
1039 && cin_is_cinword(ptr))
1040 did_si = TRUE;
1041 }
1042 }
1043 else /* dir == BACKWARD */
1044 {
1045 /*
1046 * Skip preprocessor directives, unless they are
1047 * recognised as comments.
1048 */
1049 if (
1050# ifdef FEAT_COMMENTS
1051 lead_len == 0 &&
1052# endif
1053 ptr[0] == '#')
1054 {
1055 int was_backslashed = FALSE;
1056
1057 while ((ptr[0] == '#' || was_backslashed) &&
1058 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1059 {
1060 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1061 was_backslashed = TRUE;
1062 else
1063 was_backslashed = FALSE;
1064 ptr = ml_get(++curwin->w_cursor.lnum);
1065 }
1066 if (was_backslashed)
1067 newindent = 0; /* Got to end of file */
1068 else
1069 newindent = get_indent();
1070 }
1071 p = skipwhite(ptr);
1072 if (*p == '}') /* if line starts with '}': do indent */
1073 did_si = TRUE;
1074 else /* can delete indent when '{' typed */
1075 can_si_back = TRUE;
1076 }
1077 curwin->w_cursor = old_cursor;
1078 }
1079 if (do_si)
1080 can_si = TRUE;
1081#endif /* FEAT_SMARTINDENT */
1082
1083 did_ai = TRUE;
1084 }
1085
1086#ifdef FEAT_COMMENTS
1087 /*
1088 * Find out if the current line starts with a comment leader.
1089 * This may then be inserted in front of the new line.
1090 */
1091 end_comment_pending = NUL;
1092 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001093 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 else
1095 lead_len = 0;
1096 if (lead_len > 0)
1097 {
1098 char_u *lead_repl = NULL; /* replaces comment leader */
1099 int lead_repl_len = 0; /* length of *lead_repl */
1100 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1101 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1102 char_u *comment_end = NULL; /* where lead_end has been found */
1103 int extra_space = FALSE; /* append extra space */
1104 int current_flag;
1105 int require_blank = FALSE; /* requires blank after middle */
1106 char_u *p2;
1107
1108 /*
1109 * If the comment leader has the start, middle or end flag, it may not
1110 * be used or may be replaced with the middle leader.
1111 */
1112 for (p = lead_flags; *p && *p != ':'; ++p)
1113 {
1114 if (*p == COM_BLANK)
1115 {
1116 require_blank = TRUE;
1117 continue;
1118 }
1119 if (*p == COM_START || *p == COM_MIDDLE)
1120 {
1121 current_flag = *p;
1122 if (*p == COM_START)
1123 {
1124 /*
1125 * Doing "O" on a start of comment does not insert leader.
1126 */
1127 if (dir == BACKWARD)
1128 {
1129 lead_len = 0;
1130 break;
1131 }
1132
1133 /* find start of middle part */
1134 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1135 require_blank = FALSE;
1136 }
1137
1138 /*
1139 * Isolate the strings of the middle and end leader.
1140 */
1141 while (*p && p[-1] != ':') /* find end of middle flags */
1142 {
1143 if (*p == COM_BLANK)
1144 require_blank = TRUE;
1145 ++p;
1146 }
1147 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1148
1149 while (*p && p[-1] != ':') /* find end of end flags */
1150 {
1151 /* Check whether we allow automatic ending of comments */
1152 if (*p == COM_AUTO_END)
1153 end_comment_pending = -1; /* means we want to set it */
1154 ++p;
1155 }
1156 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1157
1158 if (end_comment_pending == -1) /* we can set it now */
1159 end_comment_pending = lead_end[n - 1];
1160
1161 /*
1162 * If the end of the comment is in the same line, don't use
1163 * the comment leader.
1164 */
1165 if (dir == FORWARD)
1166 {
1167 for (p = saved_line + lead_len; *p; ++p)
1168 if (STRNCMP(p, lead_end, n) == 0)
1169 {
1170 comment_end = p;
1171 lead_len = 0;
1172 break;
1173 }
1174 }
1175
1176 /*
1177 * Doing "o" on a start of comment inserts the middle leader.
1178 */
1179 if (lead_len > 0)
1180 {
1181 if (current_flag == COM_START)
1182 {
1183 lead_repl = lead_middle;
1184 lead_repl_len = (int)STRLEN(lead_middle);
1185 }
1186
1187 /*
1188 * If we have hit RETURN immediately after the start
1189 * comment leader, then put a space after the middle
1190 * comment leader on the next line.
1191 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001192 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && ((p_extra != NULL
1194 && (int)curwin->w_cursor.col == lead_len)
1195 || (p_extra == NULL
1196 && saved_line[lead_len] == NUL)
1197 || require_blank))
1198 extra_space = TRUE;
1199 }
1200 break;
1201 }
1202 if (*p == COM_END)
1203 {
1204 /*
1205 * Doing "o" on the end of a comment does not insert leader.
1206 * Remember where the end is, might want to use it to find the
1207 * start (for C-comments).
1208 */
1209 if (dir == FORWARD)
1210 {
1211 comment_end = skipwhite(saved_line);
1212 lead_len = 0;
1213 break;
1214 }
1215
1216 /*
1217 * Doing "O" on the end of a comment inserts the middle leader.
1218 * Find the string for the middle leader, searching backwards.
1219 */
1220 while (p > curbuf->b_p_com && *p != ',')
1221 --p;
1222 for (lead_repl = p; lead_repl > curbuf->b_p_com
1223 && lead_repl[-1] != ':'; --lead_repl)
1224 ;
1225 lead_repl_len = (int)(p - lead_repl);
1226
1227 /* We can probably always add an extra space when doing "O" on
1228 * the comment-end */
1229 extra_space = TRUE;
1230
1231 /* Check whether we allow automatic ending of comments */
1232 for (p2 = p; *p2 && *p2 != ':'; p2++)
1233 {
1234 if (*p2 == COM_AUTO_END)
1235 end_comment_pending = -1; /* means we want to set it */
1236 }
1237 if (end_comment_pending == -1)
1238 {
1239 /* Find last character in end-comment string */
1240 while (*p2 && *p2 != ',')
1241 p2++;
1242 end_comment_pending = p2[-1];
1243 }
1244 break;
1245 }
1246 if (*p == COM_FIRST)
1247 {
1248 /*
1249 * Comment leader for first line only: Don't repeat leader
1250 * when using "O", blank out leader when using "o".
1251 */
1252 if (dir == BACKWARD)
1253 lead_len = 0;
1254 else
1255 {
1256 lead_repl = (char_u *)"";
1257 lead_repl_len = 0;
1258 }
1259 break;
1260 }
1261 }
1262 if (lead_len)
1263 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001264 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001265 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001266 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 allocated = leader; /* remember to free it later */
1268
1269 if (leader == NULL)
1270 lead_len = 0;
1271 else
1272 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001273 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275 /*
1276 * Replace leader with lead_repl, right or left adjusted
1277 */
1278 if (lead_repl != NULL)
1279 {
1280 int c = 0;
1281 int off = 0;
1282
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001283 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {
1285 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001286 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else if (VIM_ISDIGIT(*p) || *p == '-')
1288 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001289 else
1290 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 if (c == COM_RIGHT) /* right adjusted leader */
1293 {
1294 /* find last non-white in the leader to line up with */
1295 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001296 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001299
1300#ifdef FEAT_MBYTE
1301 /* Compute the length of the replaced characters in
1302 * screen characters, not bytes. */
1303 {
1304 int repl_size = vim_strnsize(lead_repl,
1305 lead_repl_len);
1306 int old_size = 0;
1307 char_u *endp = p;
1308 int l;
1309
1310 while (old_size < repl_size && p > leader)
1311 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001312 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001313 old_size += ptr2cells(p);
1314 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001315 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001316 if (l != 0)
1317 mch_memmove(endp + l, endp,
1318 (size_t)((leader + lead_len) - endp));
1319 lead_len += l;
1320 }
1321#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (p < leader + lead_repl_len)
1323 p = leader;
1324 else
1325 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1328 if (p + lead_repl_len > leader + lead_len)
1329 p[lead_repl_len] = NUL;
1330
1331 /* blank-out any other chars from the old leader. */
1332 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001333 {
1334#ifdef FEAT_MBYTE
1335 int l = mb_head_off(leader, p);
1336
1337 if (l > 1)
1338 {
1339 p -= l;
1340 if (ptr2cells(p) > 1)
1341 {
1342 p[1] = ' ';
1343 --l;
1344 }
1345 mch_memmove(p + 1, p + l + 1,
1346 (size_t)((leader + lead_len) - (p + l + 1)));
1347 lead_len -= l;
1348 *p = ' ';
1349 }
1350 else
1351#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001352 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 else /* left adjusted leader */
1357 {
1358 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001359#ifdef FEAT_MBYTE
1360 /* Compute the length of the replaced characters in
1361 * screen characters, not bytes. Move the part that is
1362 * not to be overwritten. */
1363 {
1364 int repl_size = vim_strnsize(lead_repl,
1365 lead_repl_len);
1366 int i;
1367 int l;
1368
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001369 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001370 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001371 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001372 if (vim_strnsize(p, i + l) > repl_size)
1373 break;
1374 }
1375 if (i != lead_repl_len)
1376 {
1377 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001378 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001379 lead_len += lead_repl_len - i;
1380 }
1381 }
1382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1384
1385 /* Replace any remaining non-white chars in the old
1386 * leader by spaces. Keep Tabs, the indent must
1387 * remain the same. */
1388 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001389 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
1391 /* Don't put a space before a TAB. */
1392 if (p + 1 < leader + lead_len && p[1] == TAB)
1393 {
1394 --lead_len;
1395 mch_memmove(p, p + 1,
1396 (leader + lead_len) - p);
1397 }
1398 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001399 {
1400#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001401 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001402
1403 if (l > 1)
1404 {
1405 if (ptr2cells(p) > 1)
1406 {
1407 /* Replace a double-wide char with
1408 * two spaces */
1409 --l;
1410 *p++ = ' ';
1411 }
1412 mch_memmove(p + 1, p + l,
1413 (leader + lead_len) - p);
1414 lead_len -= l - 1;
1415 }
1416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420 *p = NUL;
1421 }
1422
1423 /* Recompute the indent, it may have changed. */
1424 if (curbuf->b_p_ai
1425#ifdef FEAT_SMARTINDENT
1426 || do_si
1427#endif
1428 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001429#ifdef FEAT_VARTABS
1430 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1431 curbuf->b_p_vts_array, FALSE);
1432#else
1433 newindent = get_indent_str(leader,
1434 (int)curbuf->b_p_ts, FALSE);
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 /* Add the indent offset */
1438 if (newindent + off < 0)
1439 {
1440 off = -newindent;
1441 newindent = 0;
1442 }
1443 else
1444 newindent += off;
1445
1446 /* Correct trailing spaces for the shift, so that
1447 * alignment remains equal. */
1448 while (off > 0 && lead_len > 0
1449 && leader[lead_len - 1] == ' ')
1450 {
1451 /* Don't do it when there is a tab before the space */
1452 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1453 break;
1454 --lead_len;
1455 --off;
1456 }
1457
1458 /* If the leader ends in white space, don't add an
1459 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001460 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 extra_space = FALSE;
1462 leader[lead_len] = NUL;
1463 }
1464
1465 if (extra_space)
1466 {
1467 leader[lead_len++] = ' ';
1468 leader[lead_len] = NUL;
1469 }
1470
1471 newcol = lead_len;
1472
1473 /*
1474 * if a new indent will be set below, remove the indent that
1475 * is in the comment leader
1476 */
1477 if (newindent
1478#ifdef FEAT_SMARTINDENT
1479 || did_si
1480#endif
1481 )
1482 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001483 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 --lead_len;
1486 --newcol;
1487 ++leader;
1488 }
1489 }
1490
1491 }
1492#ifdef FEAT_SMARTINDENT
1493 did_si = can_si = FALSE;
1494#endif
1495 }
1496 else if (comment_end != NULL)
1497 {
1498 /*
1499 * We have finished a comment, so we don't use the leader.
1500 * If this was a C-comment and 'ai' or 'si' is set do a normal
1501 * indent to align with the line containing the start of the
1502 * comment.
1503 */
1504 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1505 (curbuf->b_p_ai
1506#ifdef FEAT_SMARTINDENT
1507 || do_si
1508#endif
1509 ))
1510 {
1511 old_cursor = curwin->w_cursor;
1512 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1513 if ((pos = findmatch(NULL, NUL)) != NULL)
1514 {
1515 curwin->w_cursor.lnum = pos->lnum;
1516 newindent = get_indent();
1517 }
1518 curwin->w_cursor = old_cursor;
1519 }
1520 }
1521 }
1522#endif
1523
1524 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1525 if (p_extra != NULL)
1526 {
1527 *p_extra = saved_char; /* restore char that NUL replaced */
1528
1529 /*
1530 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1531 * non-blank.
1532 *
1533 * When in REPLACE mode, put the deleted blanks on the replace stack,
1534 * preceded by a NUL, so they can be put back when a BS is entered.
1535 */
1536 if (REPLACE_NORMAL(State))
1537 replace_push(NUL); /* end of extra blanks */
1538 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1539 {
1540 while ((*p_extra == ' ' || *p_extra == '\t')
1541#ifdef FEAT_MBYTE
1542 && (!enc_utf8
1543 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1544#endif
1545 )
1546 {
1547 if (REPLACE_NORMAL(State))
1548 replace_push(*p_extra);
1549 ++p_extra;
1550 ++less_cols_off;
1551 }
1552 }
1553 if (*p_extra != NUL)
1554 did_ai = FALSE; /* append some text, don't truncate now */
1555
1556 /* columns for marks adjusted for removed columns */
1557 less_cols = (int)(p_extra - saved_line);
1558 }
1559
1560 if (p_extra == NULL)
1561 p_extra = (char_u *)""; /* append empty line */
1562
1563#ifdef FEAT_COMMENTS
1564 /* concatenate leader and p_extra, if there is a leader */
1565 if (lead_len)
1566 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001567 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1568 {
1569 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001570 int padding = second_line_indent
1571 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001572
1573 /* Here whitespace is inserted after the comment char.
1574 * Below, set_indent(newindent, SIN_INSERT) will insert the
1575 * whitespace needed before the comment char. */
1576 for (i = 0; i < padding; i++)
1577 {
1578 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001579 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001580 newcol++;
1581 }
1582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 STRCAT(leader, p_extra);
1584 p_extra = leader;
1585 did_ai = TRUE; /* So truncating blanks works with comments */
1586 less_cols -= lead_len;
1587 }
1588 else
1589 end_comment_pending = NUL; /* turns out there was no leader */
1590#endif
1591
1592 old_cursor = curwin->w_cursor;
1593 if (dir == BACKWARD)
1594 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1598 == FAIL)
1599 goto theend;
1600 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001601 * with markers.
1602 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001603 * be marks there. But still needed in diff mode. */
1604 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1605#ifdef FEAT_DIFF
1606 || curwin->w_p_diff
1607#endif
1608 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001609 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 did_append = TRUE;
1611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 else
1613 {
1614 /*
1615 * In VREPLACE mode we are starting to replace the next line.
1616 */
1617 curwin->w_cursor.lnum++;
1618 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1619 {
1620 /* In case we NL to a new line, BS to the previous one, and NL
1621 * again, we don't want to save the new line for undo twice.
1622 */
1623 (void)u_save_cursor(); /* errors are ignored! */
1624 vr_lines_changed++;
1625 }
1626 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1627 changed_bytes(curwin->w_cursor.lnum, 0);
1628 curwin->w_cursor.lnum--;
1629 did_append = FALSE;
1630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 if (newindent
1633#ifdef FEAT_SMARTINDENT
1634 || did_si
1635#endif
1636 )
1637 {
1638 ++curwin->w_cursor.lnum;
1639#ifdef FEAT_SMARTINDENT
1640 if (did_si)
1641 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001642 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001645 newindent -= newindent % sw;
1646 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 }
1648#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001649 /* Copy the indent */
1650 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
1652 (void)copy_indent(newindent, saved_line);
1653
1654 /*
1655 * Set the 'preserveindent' option so that any further screwing
1656 * with the line doesn't entirely destroy our efforts to preserve
1657 * it. It gets restored at the function end.
1658 */
1659 curbuf->b_p_pi = TRUE;
1660 }
1661 else
1662 (void)set_indent(newindent, SIN_INSERT);
1663 less_cols -= curwin->w_cursor.col;
1664
1665 ai_col = curwin->w_cursor.col;
1666
1667 /*
1668 * In REPLACE mode, for each character in the new indent, there must
1669 * be a NUL on the replace stack, for when it is deleted with BS
1670 */
1671 if (REPLACE_NORMAL(State))
1672 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1673 replace_push(NUL);
1674 newcol += curwin->w_cursor.col;
1675#ifdef FEAT_SMARTINDENT
1676 if (no_si)
1677 did_si = FALSE;
1678#endif
1679 }
1680
1681#ifdef FEAT_COMMENTS
1682 /*
1683 * In REPLACE mode, for each character in the extra leader, there must be
1684 * a NUL on the replace stack, for when it is deleted with BS.
1685 */
1686 if (REPLACE_NORMAL(State))
1687 while (lead_len-- > 0)
1688 replace_push(NUL);
1689#endif
1690
1691 curwin->w_cursor = old_cursor;
1692
1693 if (dir == FORWARD)
1694 {
1695 if (trunc_line || (State & INSERT))
1696 {
1697 /* truncate current line at cursor */
1698 saved_line[curwin->w_cursor.col] = NUL;
1699 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1700 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1701 truncate_spaces(saved_line);
1702 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1703 saved_line = NULL;
1704 if (did_append)
1705 {
1706 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1707 curwin->w_cursor.lnum + 1, 1L);
1708 did_append = FALSE;
1709
1710 /* Move marks after the line break to the new line. */
1711 if (flags & OPENLINE_MARKFIX)
1712 mark_col_adjust(curwin->w_cursor.lnum,
1713 curwin->w_cursor.col + less_cols_off,
1714 1L, (long)-less_cols);
1715 }
1716 else
1717 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1718 }
1719
1720 /*
1721 * Put the cursor on the new line. Careful: the scrollup() above may
1722 * have moved w_cursor, we must use old_cursor.
1723 */
1724 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1725 }
1726 if (did_append)
1727 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1728
1729 curwin->w_cursor.col = newcol;
1730#ifdef FEAT_VIRTUALEDIT
1731 curwin->w_cursor.coladd = 0;
1732#endif
1733
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001734#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 /*
1736 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1737 * fixthisline() from doing it (via change_indent()) by telling it we're in
1738 * normal INSERT mode.
1739 */
1740 if (State & VREPLACE_FLAG)
1741 {
1742 vreplace_mode = State; /* So we know to put things right later */
1743 State = INSERT;
1744 }
1745 else
1746 vreplace_mode = 0;
1747#endif
1748#ifdef FEAT_LISP
1749 /*
1750 * May do lisp indenting.
1751 */
1752 if (!p_paste
1753# ifdef FEAT_COMMENTS
1754 && leader == NULL
1755# endif
1756 && curbuf->b_p_lisp
1757 && curbuf->b_p_ai)
1758 {
1759 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001760 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762#endif
1763#ifdef FEAT_CINDENT
1764 /*
1765 * May do indenting after opening a new line.
1766 */
1767 if (!p_paste
1768 && (curbuf->b_p_cin
1769# ifdef FEAT_EVAL
1770 || *curbuf->b_p_inde != NUL
1771# endif
1772 )
1773 && in_cinkeys(dir == FORWARD
1774 ? KEY_OPEN_FORW
1775 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1776 {
1777 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001778 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 }
1780#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001781#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (vreplace_mode != 0)
1783 State = vreplace_mode;
1784#endif
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
1787 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1788 * original line, and inserts the new stuff char by char, pushing old stuff
1789 * onto the replace stack (via ins_char()).
1790 */
1791 if (State & VREPLACE_FLAG)
1792 {
1793 /* Put new line in p_extra */
1794 p_extra = vim_strsave(ml_get_curline());
1795 if (p_extra == NULL)
1796 goto theend;
1797
1798 /* Put back original line */
1799 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1800
1801 /* Insert new stuff into line again */
1802 curwin->w_cursor.col = 0;
1803#ifdef FEAT_VIRTUALEDIT
1804 curwin->w_cursor.coladd = 0;
1805#endif
1806 ins_bytes(p_extra); /* will call changed_bytes() */
1807 vim_free(p_extra);
1808 next_line = NULL;
1809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001811 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812theend:
1813 curbuf->b_p_pi = saved_pi;
1814 vim_free(saved_line);
1815 vim_free(next_line);
1816 vim_free(allocated);
1817 return retval;
1818}
1819
1820#if defined(FEAT_COMMENTS) || defined(PROTO)
1821/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001822 * get_leader_len() returns the length in bytes of the prefix of the given
1823 * string which introduces a comment. If this string is not a comment then
1824 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 * When "flags" is not NULL, it is set to point to the flags of the recognized
1826 * comment leader.
1827 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001828 * If "include_space" is set, include trailing whitespace while calculating the
1829 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001832get_leader_len(
1833 char_u *line,
1834 char_u **flags,
1835 int backward,
1836 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001839 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int got_com = FALSE;
1841 int found_one;
1842 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1843 char_u *string; /* pointer to comment string */
1844 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001845 int middle_match_len = 0;
1846 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001847 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaar81340392012-06-06 16:12:59 +02001849 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001850 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 ++i;
1852
1853 /*
1854 * Repeat to match several nested comment strings.
1855 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001856 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 /*
1859 * scan through the 'comments' option for a match
1860 */
1861 found_one = FALSE;
1862 for (list = curbuf->b_p_com; *list; )
1863 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001864 /* Get one option part into part_buf[]. Advance "list" to next
1865 * one. Put "string" at start of string. */
1866 if (!got_com && flags != NULL)
1867 *flags = list; /* remember where flags started */
1868 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1870 string = vim_strchr(part_buf, ':');
1871 if (string == NULL) /* missing ':', ignore this part */
1872 continue;
1873 *string++ = NUL; /* isolate flags from string */
1874
Bram Moolenaara4271d52011-05-10 13:38:27 +02001875 /* If we found a middle match previously, use that match when this
1876 * is not a middle or end. */
1877 if (middle_match_len != 0
1878 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1879 && vim_strchr(part_buf, COM_END) == NULL)
1880 break;
1881
1882 /* When we already found a nested comment, only accept further
1883 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1885 continue;
1886
Bram Moolenaara4271d52011-05-10 13:38:27 +02001887 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1889 continue;
1890
Bram Moolenaara4271d52011-05-10 13:38:27 +02001891 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 * When string starts with white space, must have some white space
1893 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001894 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001895 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001897 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001898 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001899 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 ++string;
1901 }
1902 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1903 ;
1904 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001905 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaara4271d52011-05-10 13:38:27 +02001907 /* When 'b' flag used, there must be white space or an
1908 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001910 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 continue;
1912
Bram Moolenaara4271d52011-05-10 13:38:27 +02001913 /* We have found a match, stop searching unless this is a middle
1914 * comment. The middle comment can be a substring of the end
1915 * comment in which case it's better to return the length of the
1916 * end comment and its flags. Thus we keep searching with middle
1917 * and end matches and use an end match if it matches better. */
1918 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1919 {
1920 if (middle_match_len == 0)
1921 {
1922 middle_match_len = j;
1923 saved_flags = prev_list;
1924 }
1925 continue;
1926 }
1927 if (middle_match_len != 0 && j > middle_match_len)
1928 /* Use this match instead of the middle match, since it's a
1929 * longer thus better match. */
1930 middle_match_len = 0;
1931
1932 if (middle_match_len == 0)
1933 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 found_one = TRUE;
1935 break;
1936 }
1937
Bram Moolenaara4271d52011-05-10 13:38:27 +02001938 if (middle_match_len != 0)
1939 {
1940 /* Use the previously found middle match after failing to find a
1941 * match with an end. */
1942 if (!got_com && flags != NULL)
1943 *flags = saved_flags;
1944 i += middle_match_len;
1945 found_one = TRUE;
1946 }
1947
1948 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (!found_one)
1950 break;
1951
Bram Moolenaar81340392012-06-06 16:12:59 +02001952 result = i;
1953
Bram Moolenaara4271d52011-05-10 13:38:27 +02001954 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001955 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 ++i;
1957
Bram Moolenaar81340392012-06-06 16:12:59 +02001958 if (include_space)
1959 result = i;
1960
Bram Moolenaara4271d52011-05-10 13:38:27 +02001961 /* If this comment doesn't nest, stop here. */
1962 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (vim_strchr(part_buf, COM_NEST) == NULL)
1964 break;
1965 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001966 return result;
1967}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001968
Bram Moolenaar81340392012-06-06 16:12:59 +02001969/*
1970 * Return the offset at which the last comment in line starts. If there is no
1971 * comment in the whole line, -1 is returned.
1972 *
1973 * When "flags" is not null, it is set to point to the flags describing the
1974 * recognized comment leader.
1975 */
1976 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001977get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001978{
1979 int result = -1;
1980 int i, j;
1981 int lower_check_bound = 0;
1982 char_u *string;
1983 char_u *com_leader;
1984 char_u *com_flags;
1985 char_u *list;
1986 int found_one;
1987 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1988
1989 /*
1990 * Repeat to match several nested comment strings.
1991 */
1992 i = (int)STRLEN(line);
1993 while (--i >= lower_check_bound)
1994 {
1995 /*
1996 * scan through the 'comments' option for a match
1997 */
1998 found_one = FALSE;
1999 for (list = curbuf->b_p_com; *list; )
2000 {
2001 char_u *flags_save = list;
2002
2003 /*
2004 * Get one option part into part_buf[]. Advance list to next one.
2005 * put string at start of string.
2006 */
2007 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2008 string = vim_strchr(part_buf, ':');
2009 if (string == NULL) /* If everything is fine, this cannot actually
2010 * happen. */
2011 {
2012 continue;
2013 }
2014 *string++ = NUL; /* Isolate flags from string. */
2015 com_leader = string;
2016
2017 /*
2018 * Line contents and string must match.
2019 * When string starts with white space, must have some white space
2020 * (but the amount does not need to match, there might be a mix of
2021 * TABs and spaces).
2022 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002023 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002024 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002025 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002026 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002027 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002028 ++string;
2029 }
2030 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2031 /* do nothing */;
2032 if (string[j] != NUL)
2033 continue;
2034
2035 /*
2036 * When 'b' flag used, there must be white space or an
2037 * end-of-line after the string in the line.
2038 */
2039 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002040 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002041 {
2042 continue;
2043 }
2044
2045 /*
2046 * We have found a match, stop searching.
2047 */
2048 found_one = TRUE;
2049
2050 if (flags)
2051 *flags = flags_save;
2052 com_flags = flags_save;
2053
2054 break;
2055 }
2056
2057 if (found_one)
2058 {
2059 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2060 int len1, len2, off;
2061
2062 result = i;
2063 /*
2064 * If this comment nests, continue searching.
2065 */
2066 if (vim_strchr(part_buf, COM_NEST) != NULL)
2067 continue;
2068
2069 lower_check_bound = i;
2070
2071 /* Let's verify whether the comment leader found is a substring
2072 * of other comment leaders. If it is, let's adjust the
2073 * lower_check_bound so that we make sure that we have determined
2074 * the comment leader correctly.
2075 */
2076
Bram Moolenaar1c465442017-03-12 20:10:05 +01002077 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002078 ++com_leader;
2079 len1 = (int)STRLEN(com_leader);
2080
2081 for (list = curbuf->b_p_com; *list; )
2082 {
2083 char_u *flags_save = list;
2084
2085 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2086 if (flags_save == com_flags)
2087 continue;
2088 string = vim_strchr(part_buf2, ':');
2089 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002090 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002091 ++string;
2092 len2 = (int)STRLEN(string);
2093 if (len2 == 0)
2094 continue;
2095
2096 /* Now we have to verify whether string ends with a substring
2097 * beginning the com_leader. */
2098 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2099 {
2100 --off;
2101 if (!STRNCMP(string + off, com_leader, len2 - off))
2102 {
2103 if (i - off < lower_check_bound)
2104 lower_check_bound = i - off;
2105 }
2106 }
2107 }
2108 }
2109 }
2110 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112#endif
2113
2114/*
2115 * Return the number of window lines occupied by buffer line "lnum".
2116 */
2117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 return plines_win(curwin, lnum, TRUE);
2121}
2122
2123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124plines_win(
2125 win_T *wp,
2126 linenr_T lnum,
2127 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
2129#if defined(FEAT_DIFF) || defined(PROTO)
2130 /* Check for filler lines above this buffer line. When folded the result
2131 * is one line anyway. */
2132 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2133}
2134
2135 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002136plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 return plines_win_nofill(curwin, lnum, TRUE);
2139}
2140
2141 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002142plines_win_nofill(
2143 win_T *wp,
2144 linenr_T lnum,
2145 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
2147#endif
2148 int lines;
2149
2150 if (!wp->w_p_wrap)
2151 return 1;
2152
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (wp->w_width == 0)
2154 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156#ifdef FEAT_FOLDING
2157 /* A folded lines is handled just like an empty line. */
2158 /* NOTE: Caller must handle lines that are MAYBE folded. */
2159 if (lineFolded(wp, lnum) == TRUE)
2160 return 1;
2161#endif
2162
2163 lines = plines_win_nofold(wp, lnum);
2164 if (winheight > 0 && lines > wp->w_height)
2165 return (int)wp->w_height;
2166 return lines;
2167}
2168
2169/*
2170 * Return number of window lines physical line "lnum" will occupy in window
2171 * "wp". Does not care about folding, 'wrap' or 'diff'.
2172 */
2173 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002174plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 char_u *s;
2177 long col;
2178 int width;
2179
2180 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2181 if (*s == NUL) /* empty line */
2182 return 1;
2183 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2184
2185 /*
2186 * If list mode is on, then the '$' at the end of the line may take up one
2187 * extra column.
2188 */
2189 if (wp->w_p_list && lcs_eol != NUL)
2190 col += 1;
2191
2192 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002193 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002195 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if (width <= 0)
2197 return 32000;
2198 if (col <= width)
2199 return 1;
2200 col -= width;
2201 width += win_col_off2(wp);
2202 return (col + (width - 1)) / width + 1;
2203}
2204
2205/*
2206 * Like plines_win(), but only reports the number of physical screen lines
2207 * used from the start of the line to the given column number.
2208 */
2209 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002210plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
2212 long col;
2213 char_u *s;
2214 int lines = 0;
2215 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002216 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218#ifdef FEAT_DIFF
2219 /* Check for filler lines above this buffer line. When folded the result
2220 * is one line anyway. */
2221 lines = diff_check_fill(wp, lnum);
2222#endif
2223
2224 if (!wp->w_p_wrap)
2225 return lines + 1;
2226
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (wp->w_width == 0)
2228 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
Bram Moolenaar597a4222014-06-25 14:39:50 +02002230 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 col = 0;
2233 while (*s != NUL && --column >= 0)
2234 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002235 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002236 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238
2239 /*
2240 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2241 * INSERT mode, then col must be adjusted so that it represents the last
2242 * screen position of the TAB. This only fixes an error when the TAB wraps
2243 * from one screen line to the next (when 'columns' is not a multiple of
2244 * 'ts') -- webb.
2245 */
2246 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002247 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002250 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002252 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002253 if (width <= 0)
2254 return 9999;
2255
2256 lines += 1;
2257 if (col > width)
2258 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2259 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260}
2261
2262 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002263plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 int count = 0;
2266
2267 while (first <= last)
2268 {
2269#ifdef FEAT_FOLDING
2270 int x;
2271
2272 /* Check if there are any really folded lines, but also included lines
2273 * that are maybe folded. */
2274 x = foldedCount(wp, first, NULL);
2275 if (x > 0)
2276 {
2277 ++count; /* count 1 for "+-- folded" line */
2278 first += x;
2279 }
2280 else
2281#endif
2282 {
2283#ifdef FEAT_DIFF
2284 if (first == wp->w_topline)
2285 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2286 else
2287#endif
2288 count += plines_win(wp, first, TRUE);
2289 ++first;
2290 }
2291 }
2292 return (count);
2293}
2294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295/*
2296 * Insert string "p" at the cursor position. Stops at a NUL byte.
2297 * Handles Replace mode and multi-byte characters.
2298 */
2299 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002300ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 ins_bytes_len(p, (int)STRLEN(p));
2303}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
2306 * Insert string "p" with length "len" at the cursor position.
2307 * Handles Replace mode and multi-byte characters.
2308 */
2309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002310ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002313#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 int n;
2315
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002316 if (has_mbyte)
2317 for (i = 0; i < len; i += n)
2318 {
2319 if (enc_utf8)
2320 /* avoid reading past p[len] */
2321 n = utfc_ptr2len_len(p + i, len - i);
2322 else
2323 n = (*mb_ptr2len)(p + i);
2324 ins_char_bytes(p + i, n);
2325 }
2326 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002327#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002328 for (i = 0; i < len; ++i)
2329 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
2332/*
2333 * Insert or replace a single character at the cursor position.
2334 * When in REPLACE or VREPLACE mode, replace any existing character.
2335 * Caller must have prepared for undo.
2336 * For multi-byte characters we get the whole character, the caller must
2337 * convert bytes to a character.
2338 */
2339 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002340ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002342 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002343 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002345#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 n = (*mb_char2bytes)(c, buf);
2347
2348 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2349 * Happens for CTRL-Vu9900. */
2350 if (buf[0] == 0)
2351 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002352#else
2353 buf[0] = c;
2354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356 ins_char_bytes(buf, n);
2357}
2358
2359 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002360ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 int newlen; /* nr of bytes inserted */
2364 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2365 char_u *p;
2366 char_u *newp;
2367 char_u *oldp;
2368 int linelen; /* length of old line including NUL */
2369 colnr_T col;
2370 linenr_T lnum = curwin->w_cursor.lnum;
2371 int i;
2372
2373#ifdef FEAT_VIRTUALEDIT
2374 /* Break tabs if needed. */
2375 if (virtual_active() && curwin->w_cursor.coladd > 0)
2376 coladvance_force(getviscol());
2377#endif
2378
2379 col = curwin->w_cursor.col;
2380 oldp = ml_get(lnum);
2381 linelen = (int)STRLEN(oldp) + 1;
2382
2383 /* The lengths default to the values for when not replacing. */
2384 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 if (State & REPLACE_FLAG)
2388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (State & VREPLACE_FLAG)
2390 {
2391 colnr_T new_vcol = 0; /* init for GCC */
2392 colnr_T vcol;
2393 int old_list;
2394#ifndef FEAT_MBYTE
2395 char_u buf[2];
2396#endif
2397
2398 /*
2399 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2400 * Returns the old value of list, so when finished,
2401 * curwin->w_p_list should be set back to this.
2402 */
2403 old_list = curwin->w_p_list;
2404 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2405 curwin->w_p_list = FALSE;
2406
2407 /*
2408 * In virtual replace mode each character may replace one or more
2409 * characters (zero if it's a TAB). Count the number of bytes to
2410 * be deleted to make room for the new character, counting screen
2411 * cells. May result in adding spaces to fill a gap.
2412 */
2413 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2414#ifndef FEAT_MBYTE
2415 buf[0] = c;
2416 buf[1] = NUL;
2417#endif
2418 new_vcol = vcol + chartabsize(buf, vcol);
2419 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2420 {
2421 vcol += chartabsize(oldp + col + oldlen, vcol);
2422 /* Don't need to remove a TAB that takes us to the right
2423 * position. */
2424 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2425 break;
2426#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002427 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428#else
2429 ++oldlen;
2430#endif
2431 /* Deleted a bit too much, insert spaces. */
2432 if (vcol > new_vcol)
2433 newlen += vcol - new_vcol;
2434 }
2435 curwin->w_p_list = old_list;
2436 }
2437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (oldp[col] != NUL)
2439 {
2440 /* normal replace */
2441#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002442 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443#else
2444 oldlen = 1;
2445#endif
2446 }
2447
2448
2449 /* Push the replaced bytes onto the replace stack, so that they can be
2450 * put back when BS is used. The bytes of a multi-byte character are
2451 * done the other way around, so that the first byte is popped off
2452 * first (it tells the byte length of the character). */
2453 replace_push(NUL);
2454 for (i = 0; i < oldlen; ++i)
2455 {
2456#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002457 if (has_mbyte)
2458 i += replace_push_mb(oldp + col + i) - 1;
2459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002461 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 }
2464
2465 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2466 if (newp == NULL)
2467 return;
2468
2469 /* Copy bytes before the cursor. */
2470 if (col > 0)
2471 mch_memmove(newp, oldp, (size_t)col);
2472
2473 /* Copy bytes after the changed character(s). */
2474 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002475 if (linelen > col + oldlen)
2476 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 (size_t)(linelen - col - oldlen));
2478
2479 /* Insert or overwrite the new character. */
2480#ifdef FEAT_MBYTE
2481 mch_memmove(p, buf, charlen);
2482 i = charlen;
2483#else
2484 *p = c;
2485 i = 1;
2486#endif
2487
2488 /* Fill with spaces when necessary. */
2489 while (i < newlen)
2490 p[i++] = ' ';
2491
2492 /* Replace the line in the buffer. */
2493 ml_replace(lnum, newp, FALSE);
2494
2495 /* mark the buffer as changed and prepare for displaying */
2496 changed_bytes(lnum, col);
2497
2498 /*
2499 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2500 * show the match for right parens and braces.
2501 */
2502 if (p_sm && (State & INSERT)
2503 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002504#ifdef FEAT_INS_EXPAND
2505 && !ins_compl_active()
2506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002508 {
2509#ifdef FEAT_MBYTE
2510 if (has_mbyte)
2511 showmatch(mb_ptr2char(buf));
2512 else
2513#endif
2514 showmatch(c);
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517#ifdef FEAT_RIGHTLEFT
2518 if (!p_ri || (State & REPLACE_FLAG))
2519#endif
2520 {
2521 /* Normal insert: move cursor right */
2522#ifdef FEAT_MBYTE
2523 curwin->w_cursor.col += charlen;
2524#else
2525 ++curwin->w_cursor.col;
2526#endif
2527 }
2528 /*
2529 * TODO: should try to update w_row here, to avoid recomputing it later.
2530 */
2531}
2532
2533/*
2534 * Insert a string at the cursor position.
2535 * Note: Does NOT handle Replace mode.
2536 * Caller must have prepared for undo.
2537 */
2538 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002539ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 char_u *oldp, *newp;
2542 int newlen = (int)STRLEN(s);
2543 int oldlen;
2544 colnr_T col;
2545 linenr_T lnum = curwin->w_cursor.lnum;
2546
2547#ifdef FEAT_VIRTUALEDIT
2548 if (virtual_active() && curwin->w_cursor.coladd > 0)
2549 coladvance_force(getviscol());
2550#endif
2551
2552 col = curwin->w_cursor.col;
2553 oldp = ml_get(lnum);
2554 oldlen = (int)STRLEN(oldp);
2555
2556 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2557 if (newp == NULL)
2558 return;
2559 if (col > 0)
2560 mch_memmove(newp, oldp, (size_t)col);
2561 mch_memmove(newp + col, s, (size_t)newlen);
2562 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2563 ml_replace(lnum, newp, FALSE);
2564 changed_bytes(lnum, col);
2565 curwin->w_cursor.col += newlen;
2566}
2567
2568/*
2569 * Delete one character under the cursor.
2570 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2571 * Caller must have prepared for undo.
2572 *
2573 * return FAIL for failure, OK otherwise
2574 */
2575 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002576del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578#ifdef FEAT_MBYTE
2579 if (has_mbyte)
2580 {
2581 /* Make sure the cursor is at the start of a character. */
2582 mb_adjust_cursor();
2583 if (*ml_get_cursor() == NUL)
2584 return FAIL;
2585 return del_chars(1L, fixpos);
2586 }
2587#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002588 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589}
2590
2591#if defined(FEAT_MBYTE) || defined(PROTO)
2592/*
2593 * Like del_bytes(), but delete characters instead of bytes.
2594 */
2595 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002596del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 long bytes = 0;
2599 long i;
2600 char_u *p;
2601 int l;
2602
2603 p = ml_get_cursor();
2604 for (i = 0; i < count && *p != NUL; ++i)
2605 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002606 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 bytes += l;
2608 p += l;
2609 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002610 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611}
2612#endif
2613
2614/*
2615 * Delete "count" bytes under the cursor.
2616 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2617 * Caller must have prepared for undo.
2618 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002619 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
2621 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002622del_bytes(
2623 long count,
2624 int fixpos_arg,
2625 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 char_u *oldp, *newp;
2628 colnr_T oldlen;
2629 linenr_T lnum = curwin->w_cursor.lnum;
2630 colnr_T col = curwin->w_cursor.col;
2631 int was_alloced;
2632 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002633 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 oldp = ml_get(lnum);
2636 oldlen = (int)STRLEN(oldp);
2637
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002638 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (col >= oldlen)
2640 return FAIL;
2641
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002642 /* If "count" is zero there is nothing to do. */
2643 if (count == 0)
2644 return OK;
2645
2646 /* If "count" is negative the caller must be doing something wrong. */
2647 if (count < 1)
2648 {
2649 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2650 return FAIL;
2651 }
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653#ifdef FEAT_MBYTE
2654 /* If 'delcombine' is set and deleting (less than) one character, only
2655 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002656 if (p_deco && use_delcombine && enc_utf8
2657 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002659 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 int n;
2661
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002662 (void)utfc_ptr2char(oldp + col, cc);
2663 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
2665 /* Find the last composing char, there can be several. */
2666 n = col;
2667 do
2668 {
2669 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002670 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 n += count;
2672 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2673 fixpos = 0;
2674 }
2675 }
2676#endif
2677
2678 /*
2679 * When count is too big, reduce it.
2680 */
2681 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2682 if (movelen <= 1)
2683 {
2684 /*
2685 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002686 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2687 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002689 if (col > 0 && fixpos && restart_edit == 0
2690#ifdef FEAT_VIRTUALEDIT
2691 && (ve_flags & VE_ONEMORE) == 0
2692#endif
2693 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
2695 --curwin->w_cursor.col;
2696#ifdef FEAT_VIRTUALEDIT
2697 curwin->w_cursor.coladd = 0;
2698#endif
2699#ifdef FEAT_MBYTE
2700 if (has_mbyte)
2701 curwin->w_cursor.col -=
2702 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2703#endif
2704 }
2705 count = oldlen - col;
2706 movelen = 1;
2707 }
2708
2709 /*
2710 * If the old line has been allocated the deletion can be done in the
2711 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002712 * Can't do this when using Netbeans, because we would need to invoke
2713 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002714 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002717 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002718 was_alloced = FALSE;
2719 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002721 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (was_alloced)
2723 newp = oldp; /* use same allocated memory */
2724 else
2725 { /* need to allocate a new line */
2726 newp = alloc((unsigned)(oldlen + 1 - count));
2727 if (newp == NULL)
2728 return FAIL;
2729 mch_memmove(newp, oldp, (size_t)col);
2730 }
2731 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2732 if (!was_alloced)
2733 ml_replace(lnum, newp, FALSE);
2734
2735 /* mark the buffer as changed and prepare for displaying */
2736 changed_bytes(lnum, curwin->w_cursor.col);
2737
2738 return OK;
2739}
2740
2741/*
2742 * Delete from cursor to end of line.
2743 * Caller must have prepared for undo.
2744 *
2745 * return FAIL for failure, OK otherwise
2746 */
2747 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002748truncate_line(
2749 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
2751 char_u *newp;
2752 linenr_T lnum = curwin->w_cursor.lnum;
2753 colnr_T col = curwin->w_cursor.col;
2754
2755 if (col == 0)
2756 newp = vim_strsave((char_u *)"");
2757 else
2758 newp = vim_strnsave(ml_get(lnum), col);
2759
2760 if (newp == NULL)
2761 return FAIL;
2762
2763 ml_replace(lnum, newp, FALSE);
2764
2765 /* mark the buffer as changed and prepare for displaying */
2766 changed_bytes(lnum, curwin->w_cursor.col);
2767
2768 /*
2769 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2770 */
2771 if (fixpos && curwin->w_cursor.col > 0)
2772 --curwin->w_cursor.col;
2773
2774 return OK;
2775}
2776
2777/*
2778 * Delete "nlines" lines at the cursor.
2779 * Saves the lines for undo first if "undo" is TRUE.
2780 */
2781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002782del_lines(
2783 long nlines, /* number of lines to delete */
2784 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
2786 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002787 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 if (nlines <= 0)
2790 return;
2791
2792 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002793 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 return;
2795
2796 for (n = 0; n < nlines; )
2797 {
2798 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2799 break;
2800
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002801 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 ++n;
2803
2804 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002805 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 break;
2807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002809 /* Correct the cursor position before calling deleted_lines_mark(), it may
2810 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 curwin->w_cursor.col = 0;
2812 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002813
2814 /* adjust marks, mark the buffer as changed and prepare for displaying */
2815 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816}
2817
2818 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002819gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002821 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002823 /* When searching columns is sometimes put at the end of a line. */
2824 if (pos->col == MAXCOL)
2825 return NUL;
2826 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827#ifdef FEAT_MBYTE
2828 if (has_mbyte)
2829 return (*mb_ptr2char)(ptr);
2830#endif
2831 return (int)*ptr;
2832}
2833
2834 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002835gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837#ifdef FEAT_MBYTE
2838 if (has_mbyte)
2839 return (*mb_ptr2char)(ml_get_cursor());
2840#endif
2841 return (int)*ml_get_cursor();
2842}
2843
2844/*
2845 * Write a character at the current cursor position.
2846 * It is directly written into the block.
2847 */
2848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002849pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
2851 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2852 + curwin->w_cursor.col) = c;
2853}
2854
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855/*
2856 * When extra == 0: Return TRUE if the cursor is before or on the first
2857 * non-blank in the line.
2858 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2859 * the line.
2860 */
2861 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002862inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 char_u *ptr;
2865 colnr_T col;
2866
Bram Moolenaar1c465442017-03-12 20:10:05 +01002867 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 ++ptr;
2869 if (col >= curwin->w_cursor.col + extra)
2870 return TRUE;
2871 else
2872 return FALSE;
2873}
2874
2875/*
2876 * Skip to next part of an option argument: Skip space and comma.
2877 */
2878 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002879skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 if (*p == ',')
2882 ++p;
2883 while (*p == ' ')
2884 ++p;
2885 return p;
2886}
2887
2888/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002889 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 *
2891 * Most often called through changed_bytes() and changed_lines(), which also
2892 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002893 *
2894 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 */
2896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002897changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002900 if (p_imst == IM_ON_THE_SPOT)
2901 {
2902 /* The text of the preediting area is inserted, but this doesn't
2903 * mean a change of the buffer yet. That is delayed until the
2904 * text is committed. (this means preedit becomes empty) */
2905 if (im_is_preediting() && !xim_changed_while_preediting)
2906 return;
2907 xim_changed_while_preediting = FALSE;
2908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#endif
2910
2911 if (!curbuf->b_changed)
2912 {
2913 int save_msg_scroll = msg_scroll;
2914
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002915 /* Give a warning about changing a read-only file. This may also
2916 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /* Create a swap file if that is wanted.
2920 * Don't do this for "nofile" and "nowrite" buffer types. */
2921 if (curbuf->b_may_swap
2922#ifdef FEAT_QUICKFIX
2923 && !bt_dontwrite(curbuf)
2924#endif
2925 )
2926 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002927 int save_need_wait_return = need_wait_return;
2928
2929 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 ml_open_file(curbuf);
2931
2932 /* The ml_open_file() can cause an ATTENTION message.
2933 * Wait two seconds, to make sure the user reads this unexpected
2934 * message. Since we could be anywhere, call wait_return() now,
2935 * and don't let the emsg() set msg_scroll. */
2936 if (need_wait_return && emsg_silent == 0)
2937 {
2938 out_flush();
2939 ui_delay(2000L, TRUE);
2940 wait_return(TRUE);
2941 msg_scroll = save_msg_scroll;
2942 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002943 else
2944 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002946 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002948 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949}
2950
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002951/*
2952 * Internal part of changed(), no user interaction.
2953 */
2954 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002955changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002956{
2957 curbuf->b_changed = TRUE;
2958 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002959 check_status(curbuf);
2960 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002961#ifdef FEAT_TITLE
2962 need_maketitle = TRUE; /* set window title later */
2963#endif
2964}
2965
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002966static void changedOneline(buf_T *buf, linenr_T lnum);
2967static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2968static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
2970/*
2971 * Changed bytes within a single line for the current buffer.
2972 * - marks the windows on this buffer to be redisplayed
2973 * - marks the buffer changed by calling changed()
2974 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002975 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 */
2977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002978changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002980 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002982
2983#ifdef FEAT_DIFF
2984 /* Diff highlighting in other diff windows may need to be updated too. */
2985 if (curwin->w_p_diff)
2986 {
2987 win_T *wp;
2988 linenr_T wlnum;
2989
Bram Moolenaar29323592016-07-24 22:04:11 +02002990 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002991 if (wp->w_p_diff && wp != curwin)
2992 {
2993 redraw_win_later(wp, VALID);
2994 wlnum = diff_lnum_win(lnum, wp);
2995 if (wlnum > 0)
2996 changedOneline(wp->w_buffer, wlnum);
2997 }
2998 }
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000}
3001
3002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003003changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003005 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
3007 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003008 if (lnum < buf->b_mod_top)
3009 buf->b_mod_top = lnum;
3010 else if (lnum >= buf->b_mod_bot)
3011 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013 else
3014 {
3015 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003016 buf->b_mod_set = TRUE;
3017 buf->b_mod_top = lnum;
3018 buf->b_mod_bot = lnum + 1;
3019 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021}
3022
3023/*
3024 * Appended "count" lines below line "lnum" in the current buffer.
3025 * Must be called AFTER the change and after mark_adjust().
3026 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3027 */
3028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003029appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 changed_lines(lnum + 1, 0, lnum + 1, count);
3032}
3033
3034/*
3035 * Like appended_lines(), but adjust marks first.
3036 */
3037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003038appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003040 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003041 * be marks there. But it's still needed in diff mode. */
3042 if (lnum + count < curbuf->b_ml.ml_line_count
3043#ifdef FEAT_DIFF
3044 || curwin->w_p_diff
3045#endif
3046 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003047 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 changed_lines(lnum + 1, 0, lnum + 1, count);
3049}
3050
3051/*
3052 * Deleted "count" lines at line "lnum" in the current buffer.
3053 * Must be called AFTER the change and after mark_adjust().
3054 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3055 */
3056 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003057deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 changed_lines(lnum, 0, lnum + count, -count);
3060}
3061
3062/*
3063 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003064 * Make sure the cursor is on a valid line before calling, a GUI callback may
3065 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 */
3067 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003068deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3071 changed_lines(lnum, 0, lnum + count, -count);
3072}
3073
3074/*
3075 * Changed lines for the current buffer.
3076 * Must be called AFTER the change and after mark_adjust().
3077 * - mark the buffer changed by calling changed()
3078 * - mark the windows on this buffer to be redisplayed
3079 * - invalidate cached values
3080 * "lnum" is the first line that needs displaying, "lnume" the first line
3081 * below the changed lines (BEFORE the change).
3082 * When only inserting lines, "lnum" and "lnume" are equal.
3083 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003084 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 */
3086 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003087changed_lines(
3088 linenr_T lnum, /* first line with change */
3089 colnr_T col, /* column in first line with change */
3090 linenr_T lnume, /* line below last changed line */
3091 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003093 changed_lines_buf(curbuf, lnum, lnume, xtra);
3094
3095#ifdef FEAT_DIFF
3096 if (xtra == 0 && curwin->w_p_diff)
3097 {
3098 /* When the number of lines doesn't change then mark_adjust() isn't
3099 * called and other diff buffers still need to be marked for
3100 * displaying. */
3101 win_T *wp;
3102 linenr_T wlnum;
3103
Bram Moolenaar29323592016-07-24 22:04:11 +02003104 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003105 if (wp->w_p_diff && wp != curwin)
3106 {
3107 redraw_win_later(wp, VALID);
3108 wlnum = diff_lnum_win(lnum, wp);
3109 if (wlnum > 0)
3110 changed_lines_buf(wp->w_buffer, wlnum,
3111 lnume - lnum + wlnum, 0L);
3112 }
3113 }
3114#endif
3115
3116 changed_common(lnum, col, lnume, xtra);
3117}
3118
3119 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003120changed_lines_buf(
3121 buf_T *buf,
3122 linenr_T lnum, /* first line with change */
3123 linenr_T lnume, /* line below last changed line */
3124 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003125{
3126 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003129 if (lnum < buf->b_mod_top)
3130 buf->b_mod_top = lnum;
3131 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003134 buf->b_mod_bot += xtra;
3135 if (buf->b_mod_bot < lnum)
3136 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003138 if (lnume + xtra > buf->b_mod_bot)
3139 buf->b_mod_bot = lnume + xtra;
3140 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
3142 else
3143 {
3144 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003145 buf->b_mod_set = TRUE;
3146 buf->b_mod_top = lnum;
3147 buf->b_mod_bot = lnume + xtra;
3148 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150}
3151
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003152/*
3153 * Common code for when a change is was made.
3154 * See changed_lines() for the arguments.
3155 * Careful: may trigger autocommands that reload the buffer.
3156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003158changed_common(
3159 linenr_T lnum,
3160 colnr_T col,
3161 linenr_T lnume,
3162 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003165 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 int i;
3167#ifdef FEAT_JUMPLIST
3168 int cols;
3169 pos_T *p;
3170 int add;
3171#endif
3172
3173 /* mark the buffer as modified */
3174 changed();
3175
3176 /* set the '. mark */
3177 if (!cmdmod.keepjumps)
3178 {
3179 curbuf->b_last_change.lnum = lnum;
3180 curbuf->b_last_change.col = col;
3181
3182#ifdef FEAT_JUMPLIST
3183 /* Create a new entry if a new undo-able change was started or we
3184 * don't have an entry yet. */
3185 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3186 {
3187 if (curbuf->b_changelistlen == 0)
3188 add = TRUE;
3189 else
3190 {
3191 /* Don't create a new entry when the line number is the same
3192 * as the last one and the column is not too far away. Avoids
3193 * creating many entries for typing "xxxxx". */
3194 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3195 if (p->lnum != lnum)
3196 add = TRUE;
3197 else
3198 {
3199 cols = comp_textwidth(FALSE);
3200 if (cols == 0)
3201 cols = 79;
3202 add = (p->col + cols < col || col + cols < p->col);
3203 }
3204 }
3205 if (add)
3206 {
3207 /* This is the first of a new sequence of undo-able changes
3208 * and it's at some distance of the last change. Use a new
3209 * position in the changelist. */
3210 curbuf->b_new_change = FALSE;
3211
3212 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3213 {
3214 /* changelist is full: remove oldest entry */
3215 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3216 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3217 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003218 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
3220 /* Correct position in changelist for other windows on
3221 * this buffer. */
3222 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3223 --wp->w_changelistidx;
3224 }
3225 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003226 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
3228 /* For other windows, if the position in the changelist is
3229 * at the end it stays at the end. */
3230 if (wp->w_buffer == curbuf
3231 && wp->w_changelistidx == curbuf->b_changelistlen)
3232 ++wp->w_changelistidx;
3233 }
3234 ++curbuf->b_changelistlen;
3235 }
3236 }
3237 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3238 curbuf->b_last_change;
3239 /* The current window is always after the last change, so that "g,"
3240 * takes you back to it. */
3241 curwin->w_changelistidx = curbuf->b_changelistlen;
3242#endif
3243 }
3244
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003245 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 if (wp->w_buffer == curbuf)
3248 {
3249 /* Mark this window to be redrawn later. */
3250 if (wp->w_redr_type < VALID)
3251 wp->w_redr_type = VALID;
3252
3253 /* Check if a change in the buffer has invalidated the cached
3254 * values for the cursor. */
3255#ifdef FEAT_FOLDING
3256 /*
3257 * Update the folds for this window. Can't postpone this, because
3258 * a following operator might work on the whole fold: ">>dd".
3259 */
3260 foldUpdate(wp, lnum, lnume + xtra - 1);
3261
3262 /* The change may cause lines above or below the change to become
3263 * included in a fold. Set lnum/lnume to the first/last line that
3264 * might be displayed differently.
3265 * Set w_cline_folded here as an efficient way to update it when
3266 * inserting lines just above a closed fold. */
3267 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3268 if (wp->w_cursor.lnum == lnum)
3269 wp->w_cline_folded = i;
3270 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3271 if (wp->w_cursor.lnum == lnume)
3272 wp->w_cline_folded = i;
3273
3274 /* If the changed line is in a range of previously folded lines,
3275 * compare with the first line in that range. */
3276 if (wp->w_cursor.lnum <= lnum)
3277 {
3278 i = find_wl_entry(wp, lnum);
3279 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3280 changed_line_abv_curs_win(wp);
3281 }
3282#endif
3283
3284 if (wp->w_cursor.lnum > lnum)
3285 changed_line_abv_curs_win(wp);
3286 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3287 changed_cline_bef_curs_win(wp);
3288 if (wp->w_botline >= lnum)
3289 {
3290 /* Assume that botline doesn't change (inserted lines make
3291 * other lines scroll down below botline). */
3292 approximate_botline_win(wp);
3293 }
3294
3295 /* Check if any w_lines[] entries have become invalid.
3296 * For entries below the change: Correct the lnums for
3297 * inserted/deleted lines. Makes it possible to stop displaying
3298 * after the change. */
3299 for (i = 0; i < wp->w_lines_valid; ++i)
3300 if (wp->w_lines[i].wl_valid)
3301 {
3302 if (wp->w_lines[i].wl_lnum >= lnum)
3303 {
3304 if (wp->w_lines[i].wl_lnum < lnume)
3305 {
3306 /* line included in change */
3307 wp->w_lines[i].wl_valid = FALSE;
3308 }
3309 else if (xtra != 0)
3310 {
3311 /* line below change */
3312 wp->w_lines[i].wl_lnum += xtra;
3313#ifdef FEAT_FOLDING
3314 wp->w_lines[i].wl_lastlnum += xtra;
3315#endif
3316 }
3317 }
3318#ifdef FEAT_FOLDING
3319 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3320 {
3321 /* change somewhere inside this range of folded lines,
3322 * may need to be redrawn */
3323 wp->w_lines[i].wl_valid = FALSE;
3324 }
3325#endif
3326 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003327
3328#ifdef FEAT_FOLDING
3329 /* Take care of side effects for setting w_topline when folds have
3330 * changed. Esp. when the buffer was changed in another window. */
3331 if (hasAnyFolding(wp))
3332 set_topline(wp, wp->w_topline);
3333#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003334 /* relative numbering may require updating more */
3335 if (wp->w_p_rnu)
3336 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338 }
3339
3340 /* Call update_screen() later, which checks out what needs to be redrawn,
3341 * since it notices b_mod_set and then uses b_mod_*. */
3342 if (must_redraw < VALID)
3343 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003344
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003345 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003346 if (lnum <= curwin->w_cursor.lnum
3347 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003348 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349}
3350
3351/*
3352 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3353 */
3354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003355unchanged(
3356 buf_T *buf,
3357 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003359 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
3361 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003362 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (ff)
3364 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003366 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#ifdef FEAT_TITLE
3368 need_maketitle = TRUE; /* set window title later */
3369#endif
3370 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003371 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#ifdef FEAT_NETBEANS_INTG
3373 netbeans_unmodified(buf);
3374#endif
3375}
3376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377/*
3378 * check_status: called when the status bars for the buffer 'buf'
3379 * need to be updated
3380 */
3381 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003382check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 win_T *wp;
3385
Bram Moolenaar29323592016-07-24 22:04:11 +02003386 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (wp->w_buffer == buf && wp->w_status_height)
3388 {
3389 wp->w_redr_status = TRUE;
3390 if (must_redraw < VALID)
3391 must_redraw = VALID;
3392 }
3393}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395/*
3396 * If the file is readonly, give a warning message with the first change.
3397 * Don't do this for autocommands.
3398 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003399 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003401 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 */
3403 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003404change_warning(
3405 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 mode and 'showmode' is on */
3407{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003408 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (curbuf->b_did_warn == FALSE
3411 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 && curbuf->b_p_ro)
3414 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003415 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003417 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (!curbuf->b_p_ro)
3419 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 /*
3421 * Do what msg() does, but with a column offset if the warning should
3422 * be after the mode message.
3423 */
3424 msg_start();
3425 if (msg_row == Rows - 1)
3426 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003427 msg_source(HL_ATTR(HLF_W));
3428 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003429#ifdef FEAT_EVAL
3430 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 msg_clr_eos();
3433 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003434 if (msg_silent == 0 && !silent_mode
3435#ifdef FEAT_EVAL
3436 && time_for_testing != 1
3437#endif
3438 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 out_flush();
3441 ui_delay(1000L, TRUE); /* give the user time to think about it */
3442 }
3443 curbuf->b_did_warn = TRUE;
3444 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3445 if (msg_row < Rows - 1)
3446 showmode();
3447 }
3448}
3449
3450/*
3451 * Ask for a reply from the user, a 'y' or a 'n'.
3452 * No other characters are accepted, the message is repeated until a valid
3453 * reply is entered or CTRL-C is hit.
3454 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3455 * from any buffers but directly from the user.
3456 *
3457 * return the 'y' or 'n'
3458 */
3459 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003460ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 int r = ' ';
3463 int save_State = State;
3464
3465 if (exiting) /* put terminal in raw mode for this question */
3466 settmode(TMODE_RAW);
3467 ++no_wait_return;
3468#ifdef USE_ON_FLY_SCROLL
3469 dont_scroll = TRUE; /* disallow scrolling here */
3470#endif
3471 State = CONFIRM; /* mouse behaves like with :confirm */
3472#ifdef FEAT_MOUSE
3473 setmouse(); /* disables mouse for xterm */
3474#endif
3475 ++no_mapping;
3476 ++allow_keys; /* no mapping here, but recognize keys */
3477
3478 while (r != 'y' && r != 'n')
3479 {
3480 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003481 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (direct)
3483 r = get_keystroke();
3484 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003485 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (r == Ctrl_C || r == ESC)
3487 r = 'n';
3488 msg_putchar(r); /* show what you typed */
3489 out_flush();
3490 }
3491 --no_wait_return;
3492 State = save_State;
3493#ifdef FEAT_MOUSE
3494 setmouse();
3495#endif
3496 --no_mapping;
3497 --allow_keys;
3498
3499 return r;
3500}
3501
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003502#if defined(FEAT_MOUSE) || defined(PROTO)
3503/*
3504 * Return TRUE if "c" is a mouse key.
3505 */
3506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003507is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003508{
3509 return c == K_LEFTMOUSE
3510 || c == K_LEFTMOUSE_NM
3511 || c == K_LEFTDRAG
3512 || c == K_LEFTRELEASE
3513 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003514 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003515 || c == K_MIDDLEMOUSE
3516 || c == K_MIDDLEDRAG
3517 || c == K_MIDDLERELEASE
3518 || c == K_RIGHTMOUSE
3519 || c == K_RIGHTDRAG
3520 || c == K_RIGHTRELEASE
3521 || c == K_MOUSEDOWN
3522 || c == K_MOUSEUP
3523 || c == K_MOUSELEFT
3524 || c == K_MOUSERIGHT
3525 || c == K_X1MOUSE
3526 || c == K_X1DRAG
3527 || c == K_X1RELEASE
3528 || c == K_X2MOUSE
3529 || c == K_X2DRAG
3530 || c == K_X2RELEASE;
3531}
3532#endif
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534/*
3535 * Get a key stroke directly from the user.
3536 * Ignores mouse clicks and scrollbar events, except a click for the left
3537 * button (used at the more prompt).
3538 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3539 * Disadvantage: typeahead is ignored.
3540 * Translates the interrupt character for unix to ESC.
3541 */
3542 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003543get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003545 char_u *buf = NULL;
3546 int buflen = 150;
3547 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 int len = 0;
3549 int n;
3550 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003551 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
3553 mapped_ctrl_c = FALSE; /* mappings are not used here */
3554 for (;;)
3555 {
3556 cursor_on();
3557 out_flush();
3558
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003559 /* Leave some room for check_termcode() to insert a key code into (max
3560 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3561 * bytes. */
3562 maxlen = (buflen - 6 - len) / 3;
3563 if (buf == NULL)
3564 buf = alloc(buflen);
3565 else if (maxlen < 10)
3566 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003567 char_u *t_buf = buf;
3568
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003569 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003570 * escape sequence. */
3571 buflen += 100;
3572 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003573 if (buf == NULL)
3574 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003575 maxlen = (buflen - 6 - len) / 3;
3576 }
3577 if (buf == NULL)
3578 {
3579 do_outofmem_msg((long_u)buflen);
3580 return ESC; /* panic! */
3581 }
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003584 * terminal code to complete. */
3585 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (n > 0)
3587 {
3588 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003589 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003591 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003593 else if (len > 0)
3594 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar4395a712006-09-05 18:57:57 +00003596 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003597 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003598 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003600
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003601 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003602 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003603 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003604 {
3605 /* Redrawing was postponed, do it now. */
3606 update_screen(0);
3607 setcursor(); /* put cursor back where it belongs */
3608 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003609 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003610 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003611 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003613 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 continue;
3615
3616 /* Handle modifier and/or special key code. */
3617 n = buf[0];
3618 if (n == K_SPECIAL)
3619 {
3620 n = TO_SPECIAL(buf[1], buf[2]);
3621 if (buf[1] == KS_MODIFIER
3622 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003623#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003624 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003625#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003626#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 || n == K_VER_SCROLLBAR
3628 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629#endif
3630 )
3631 {
3632 if (buf[1] == KS_MODIFIER)
3633 mod_mask = buf[2];
3634 len -= 3;
3635 if (len > 0)
3636 mch_memmove(buf, buf + 3, (size_t)len);
3637 continue;
3638 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003639 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
3641#ifdef FEAT_MBYTE
3642 if (has_mbyte)
3643 {
3644 if (MB_BYTE2LEN(n) > len)
3645 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003646 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 n = (*mb_ptr2char)(buf);
3648 }
3649#endif
3650#ifdef UNIX
3651 if (n == intr_char)
3652 n = ESC;
3653#endif
3654 break;
3655 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003656 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
3658 mapped_ctrl_c = save_mapped_ctrl_c;
3659 return n;
3660}
3661
3662/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003663 * Get a number from the user.
3664 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
3666 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003667get_number(
3668 int colon, /* allow colon to abort */
3669 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
3671 int n = 0;
3672 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003673 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003675 if (mouse_used != NULL)
3676 *mouse_used = FALSE;
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 /* When not printing messages, the user won't know what to type, return a
3679 * zero (as if CR was hit). */
3680 if (msg_silent != 0)
3681 return 0;
3682
3683#ifdef USE_ON_FLY_SCROLL
3684 dont_scroll = TRUE; /* disallow scrolling here */
3685#endif
3686 ++no_mapping;
3687 ++allow_keys; /* no mapping here, but recognize keys */
3688 for (;;)
3689 {
3690 windgoto(msg_row, msg_col);
3691 c = safe_vgetc();
3692 if (VIM_ISDIGIT(c))
3693 {
3694 n = n * 10 + c - '0';
3695 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003696 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3699 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003700 if (typed > 0)
3701 {
3702 MSG_PUTS("\b \b");
3703 --typed;
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003707#ifdef FEAT_MOUSE
3708 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3709 {
3710 *mouse_used = TRUE;
3711 n = mouse_row + 1;
3712 break;
3713 }
3714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 else if (n == 0 && c == ':' && colon)
3716 {
3717 stuffcharReadbuff(':');
3718 if (!exmode_active)
3719 cmdline_row = msg_row;
3720 skip_redraw = TRUE; /* skip redraw once */
3721 do_redraw = FALSE;
3722 break;
3723 }
3724 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3725 break;
3726 }
3727 --no_mapping;
3728 --allow_keys;
3729 return n;
3730}
3731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003732/*
3733 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003734 * When "mouse_used" is not NULL allow using the mouse and in that case return
3735 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 */
3737 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003738prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003739{
3740 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003741 int save_cmdline_row;
3742 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743
3744 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003745 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003746 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003747 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003748 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003749
Bram Moolenaar203335e2006-09-03 14:35:42 +00003750 /* Set the state such that text can be selected/copied/pasted and we still
3751 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003752 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003753 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003754 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003755 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaar73658312018-04-24 17:41:57 +02003756#ifdef FEAT_MOUSE
3757 /* May show different mouse shape. */
3758 setmouse();
3759#endif
3760
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003761
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003762 i = get_number(TRUE, mouse_used);
3763 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003765 /* don't call wait_return() now */
3766 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003767 cmdline_row = msg_row - 1;
3768 need_wait_return = FALSE;
3769 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003770 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003771 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003772 else
3773 cmdline_row = save_cmdline_row;
3774 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003775#ifdef FEAT_MOUSE
3776 /* May need to restore mouse shape. */
3777 setmouse();
3778#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 return i;
3781}
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003784msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786 long pn;
3787
3788 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3790 return;
3791
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003792 /* We don't want to overwrite another important message, but do overwrite
3793 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3794 * then "put" reports the last action. */
3795 if (keep_msg != NULL && !keep_msg_more)
3796 return;
3797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (n > 0)
3799 pn = n;
3800 else
3801 pn = -n;
3802
3803 if (pn > p_report)
3804 {
3805 if (pn == 1)
3806 {
3807 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003808 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3809 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003811 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3812 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814 else
3815 {
3816 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003817 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3818 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003820 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3821 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003824 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (msg(msg_buf))
3826 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003827 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003828 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 }
3831}
3832
3833/*
3834 * flush map and typeahead buffers and give a warning for an error
3835 */
3836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003837beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838{
3839 if (emsg_silent == 0)
3840 {
3841 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003842 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844}
3845
3846/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003847 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 */
3849 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003850vim_beep(
3851 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003853#ifdef FEAT_EVAL
3854 called_vim_beep = TRUE;
3855#endif
3856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (emsg_silent == 0)
3858 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003859 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3860 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003861#ifdef ELAPSED_FUNC
3862 static int did_init = FALSE;
3863 static ELAPSED_TYPE start_tv;
3864
3865 /* Only beep once per half a second, otherwise a sequence of beeps
3866 * would freeze Vim. */
3867 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3868 {
3869 did_init = TRUE;
3870 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003872 if (p_vb
3873#ifdef FEAT_GUI
3874 /* While the GUI is starting up the termcap is set for
3875 * the GUI but the output still goes to a terminal. */
3876 && !(gui.in_use && gui.starting)
3877#endif
3878 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003879 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003880 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003881#ifdef FEAT_VTP
3882 /* No restore color information, refresh the screen. */
3883 if (has_vtp_working() != 0
3884# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003885 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003886# endif
3887 )
3888 {
3889 redraw_later(CLEAR);
3890 update_screen(0);
3891 redrawcmd();
3892 }
3893#endif
3894 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003895 else
3896 out_char(BELL);
3897#ifdef ELAPSED_FUNC
3898 }
3899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003901
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003902 /* When 'debug' contains "beep" produce a message. If we are sourcing
3903 * a script or executing a function give the user a hint where the beep
3904 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003905 if (vim_strchr(p_debug, 'e') != NULL)
3906 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003907 msg_source(HL_ATTR(HLF_W));
3908 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911}
3912
3913/*
3914 * To get the "real" home directory:
3915 * - get value of $HOME
3916 * For Unix:
3917 * - go to that directory
3918 * - do mch_dirname() to get the real name of that directory.
3919 * This also works with mounts and links.
3920 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3921 */
3922static char_u *homedir = NULL;
3923
3924 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003925init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
3927 char_u *var;
3928
Bram Moolenaar05159a02005-02-26 23:04:13 +00003929 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003930 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932#ifdef VMS
3933 var = mch_getenv((char_u *)"SYS$LOGIN");
3934#else
3935 var = mch_getenv((char_u *)"HOME");
3936#endif
3937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938#ifdef WIN3264
3939 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003940 * Typically, $HOME is not defined on Windows, unless the user has
3941 * specifically defined it for Vim's sake. However, on Windows NT
3942 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3943 * each user. Try constructing $HOME from these.
3944 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003945 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003946 {
3947 char_u *homedrive, *homepath;
3948
3949 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3950 homepath = mch_getenv((char_u *)"HOMEPATH");
3951 if (homepath == NULL || *homepath == NUL)
3952 homepath = (char_u *)"\\";
3953 if (homedrive != NULL
3954 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3955 {
3956 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3957 if (NameBuff[0] != NUL)
3958 var = NameBuff;
3959 }
3960 }
3961
3962 if (var == NULL)
3963 var = mch_getenv((char_u *)"USERPROFILE");
3964
3965 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 * Weird but true: $HOME may contain an indirect reference to another
3967 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3968 * when $HOME is being set.
3969 */
3970 if (var != NULL && *var == '%')
3971 {
3972 char_u *p;
3973 char_u *exp;
3974
3975 p = vim_strchr(var + 1, '%');
3976 if (p != NULL)
3977 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003978 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 exp = mch_getenv(NameBuff);
3980 if (exp != NULL && *exp != NUL
3981 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3982 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003983 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986 }
3987 }
3988
Bram Moolenaar48340b62017-08-29 22:08:53 +02003989 if (var != NULL && *var == NUL) /* empty is same as not set */
3990 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar48340b62017-08-29 22:08:53 +02003992# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003993 if (enc_utf8 && var != NULL)
3994 {
3995 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003996 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003997
3998 /* Convert from active codepage to UTF-8. Other conversions are
3999 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004000 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004001 if (pp != NULL)
4002 {
4003 homedir = pp;
4004 return;
4005 }
4006 }
4007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 /*
4010 * Default home dir is C:/
4011 * Best assumption we can make in such a situation.
4012 */
4013 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004014 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if (var != NULL)
4018 {
4019#ifdef UNIX
4020 /*
4021 * Change to the directory and get the actual path. This resolves
4022 * links. Don't do it when we can't return.
4023 */
4024 if (mch_dirname(NameBuff, MAXPATHL) == OK
4025 && mch_chdir((char *)NameBuff) == 0)
4026 {
4027 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4028 var = IObuff;
4029 if (mch_chdir((char *)NameBuff) != 0)
4030 EMSG(_(e_prev_dir));
4031 }
4032#endif
4033 homedir = vim_strsave(var);
4034 }
4035}
4036
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004037#if defined(EXITFREE) || defined(PROTO)
4038 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004039free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004040{
4041 vim_free(homedir);
4042}
Bram Moolenaar24305862012-08-15 14:05:05 +02004043
4044# ifdef FEAT_CMDL_COMPL
4045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004046free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004047{
4048 ga_clear_strings(&ga_users);
4049}
4050# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004051#endif
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004054 * Call expand_env() and store the result in an allocated string.
4055 * This is not very memory efficient, this expects the result to be freed
4056 * again soon.
4057 */
4058 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004059expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004060{
4061 return expand_env_save_opt(src, FALSE);
4062}
4063
4064/*
4065 * Idem, but when "one" is TRUE handle the string as one file name, only
4066 * expand "~" at the start.
4067 */
4068 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004069expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004070{
4071 char_u *p;
4072
4073 p = alloc(MAXPATHL);
4074 if (p != NULL)
4075 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4076 return p;
4077}
4078
4079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 * Expand environment variable with path name.
4081 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004082 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * If anything fails no expansion is done and dst equals src.
4084 */
4085 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004086expand_env(
4087 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4088 char_u *dst, /* where to put the result */
4089 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004091 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092}
4093
4094 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004095expand_env_esc(
4096 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4097 char_u *dst, /* where to put the result */
4098 int dstlen, /* maximum length of the result */
4099 int esc, /* escape spaces in expanded variables */
4100 int one, /* "srcp" is one file name */
4101 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004103 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 char_u *tail;
4105 int c;
4106 char_u *var;
4107 int copy_char;
4108 int mustfree; /* var was allocated, need to free it later */
4109 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004110 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004112 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004113 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004114
4115 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 --dstlen; /* leave one char space for "\," */
4117 while (*src && dstlen > 0)
4118 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004119#ifdef FEAT_EVAL
4120 /* Skip over `=expr`. */
4121 if (src[0] == '`' && src[1] == '=')
4122 {
4123 size_t len;
4124
4125 var = src;
4126 src += 2;
4127 (void)skip_expr(&src);
4128 if (*src == '`')
4129 ++src;
4130 len = src - var;
4131 if (len > (size_t)dstlen)
4132 len = dstlen;
4133 vim_strncpy(dst, var, len);
4134 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004135 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004136 continue;
4137 }
4138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004140 if ((*src == '$'
4141#ifdef VMS
4142 && at_start
4143#endif
4144 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004145#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 || *src == '%'
4147#endif
4148 || (*src == '~' && at_start))
4149 {
4150 mustfree = FALSE;
4151
4152 /*
4153 * The variable name is copied into dst temporarily, because it may
4154 * be a string in read-only memory and a NUL needs to be appended.
4155 */
4156 if (*src != '~') /* environment var */
4157 {
4158 tail = src + 1;
4159 var = dst;
4160 c = dstlen - 1;
4161
4162#ifdef UNIX
4163 /* Unix has ${var-name} type environment vars */
4164 if (*tail == '{' && !vim_isIDc('{'))
4165 {
4166 tail++; /* ignore '{' */
4167 while (c-- > 0 && *tail && *tail != '}')
4168 *var++ = *tail++;
4169 }
4170 else
4171#endif
4172 {
4173 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004174#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 || (*src == '%' && *tail != '%')
4176#endif
4177 ))
4178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 }
4182
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004183#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184# ifdef UNIX
4185 if (src[1] == '{' && *tail != '}')
4186# else
4187 if (*src == '%' && *tail != '%')
4188# endif
4189 var = NULL;
4190 else
4191 {
4192# ifdef UNIX
4193 if (src[1] == '{')
4194# else
4195 if (*src == '%')
4196#endif
4197 ++tail;
4198#endif
4199 *var = NUL;
4200 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004201#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203#endif
4204 }
4205 /* home directory */
4206 else if ( src[1] == NUL
4207 || vim_ispathsep(src[1])
4208 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4209 {
4210 var = homedir;
4211 tail = src + 1;
4212 }
4213 else /* user directory */
4214 {
4215#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4216 /*
4217 * Copy ~user to dst[], so we can put a NUL after it.
4218 */
4219 tail = src;
4220 var = dst;
4221 c = dstlen - 1;
4222 while ( c-- > 0
4223 && *tail
4224 && vim_isfilec(*tail)
4225 && !vim_ispathsep(*tail))
4226 *var++ = *tail++;
4227 *var = NUL;
4228# ifdef UNIX
4229 /*
4230 * If the system supports getpwnam(), use it.
4231 * Otherwise, or if getpwnam() fails, the shell is used to
4232 * expand ~user. This is slower and may fail if the shell
4233 * does not support ~user (old versions of /bin/sh).
4234 */
4235# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4236 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004237 /* Note: memory allocated by getpwnam() is never freed.
4238 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004239 struct passwd *pw = (*dst == NUL)
4240 ? NULL : getpwnam((char *)dst + 1);
4241
4242 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244 if (var == NULL)
4245# endif
4246 {
4247 expand_T xpc;
4248
4249 ExpandInit(&xpc);
4250 xpc.xp_context = EXPAND_FILES;
4251 var = ExpandOne(&xpc, dst, NULL,
4252 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 mustfree = TRUE;
4254 }
4255
4256# else /* !UNIX, thus VMS */
4257 /*
4258 * USER_HOME is a comma-separated list of
4259 * directories to search for the user account in.
4260 */
4261 {
4262 char_u test[MAXPATHL], paths[MAXPATHL];
4263 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004264 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
4266 STRCPY(paths, USER_HOME);
4267 next_path = paths;
4268 while (*next_path)
4269 {
4270 for (path = next_path; *next_path && *next_path != ',';
4271 next_path++);
4272 if (*next_path)
4273 *next_path++ = NUL;
4274 STRCPY(test, path);
4275 STRCAT(test, "/");
4276 STRCAT(test, dst + 1);
4277 if (mch_stat(test, &st) == 0)
4278 {
4279 var = alloc(STRLEN(test) + 1);
4280 STRCPY(var, test);
4281 mustfree = TRUE;
4282 break;
4283 }
4284 }
4285 }
4286# endif /* UNIX */
4287#else
4288 /* cannot expand user's home directory, so don't try */
4289 var = NULL;
4290 tail = (char_u *)""; /* for gcc */
4291#endif /* UNIX || VMS */
4292 }
4293
4294#ifdef BACKSLASH_IN_FILENAME
4295 /* If 'shellslash' is set change backslashes to forward slashes.
4296 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4297 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4298 {
4299 char_u *p = vim_strsave(var);
4300
4301 if (p != NULL)
4302 {
4303 if (mustfree)
4304 vim_free(var);
4305 var = p;
4306 mustfree = TRUE;
4307 forward_slash(var);
4308 }
4309 }
4310#endif
4311
4312 /* If "var" contains white space, escape it with a backslash.
4313 * Required for ":e ~/tt" when $HOME includes a space. */
4314 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4315 {
4316 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4317
4318 if (p != NULL)
4319 {
4320 if (mustfree)
4321 vim_free(var);
4322 var = p;
4323 mustfree = TRUE;
4324 }
4325 }
4326
4327 if (var != NULL && *var != NUL
4328 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4329 {
4330 STRCPY(dst, var);
4331 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004332 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 /* if var[] ends in a path separator and tail[] starts
4334 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004335 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4337 && dst[-1] != ':'
4338#endif
4339 && vim_ispathsep(*tail))
4340 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004341 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 src = tail;
4343 copy_char = FALSE;
4344 }
4345 if (mustfree)
4346 vim_free(var);
4347 }
4348
4349 if (copy_char) /* copy at least one char */
4350 {
4351 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004352 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004353 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4354 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 */
4356 at_start = FALSE;
4357 if (src[0] == '\\' && src[1] != NUL)
4358 {
4359 *dst++ = *src++;
4360 --dstlen;
4361 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004362 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004364 if (dstlen > 0)
4365 {
4366 *dst++ = *src++;
4367 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004368
Bram Moolenaar1c864092017-08-06 18:15:45 +02004369 if (startstr != NULL && src - startstr_len >= srcp
4370 && STRNCMP(src - startstr_len, startstr,
4371 startstr_len) == 0)
4372 at_start = TRUE;
4373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377 *dst = NUL;
4378}
4379
4380/*
4381 * Vim's version of getenv().
4382 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004383 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004384 * "mustfree" is set to TRUE when returned is allocated, it must be
4385 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 */
4387 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004388vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 char_u *p;
4391 char_u *pend;
4392 int vimruntime;
4393
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004394#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 /* use "C:/" when $HOME is not set */
4396 if (STRCMP(name, "HOME") == 0)
4397 return homedir;
4398#endif
4399
4400 p = mch_getenv(name);
4401 if (p != NULL && *p == NUL) /* empty is the same as not set */
4402 p = NULL;
4403
4404 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004405 {
4406#if defined(FEAT_MBYTE) && defined(WIN3264)
4407 if (enc_utf8)
4408 {
4409 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004410 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004411
4412 /* Convert from active codepage to UTF-8. Other conversions are
4413 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004414 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004415 if (pp != NULL)
4416 {
4417 p = pp;
4418 *mustfree = TRUE;
4419 }
4420 }
4421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4426 if (!vimruntime && STRCMP(name, "VIM") != 0)
4427 return NULL;
4428
4429 /*
4430 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4431 * Don't do this when default_vimruntime_dir is non-empty.
4432 */
4433 if (vimruntime
4434#ifdef HAVE_PATHDEF
4435 && *default_vimruntime_dir == NUL
4436#endif
4437 )
4438 {
4439 p = mch_getenv((char_u *)"VIM");
4440 if (p != NULL && *p == NUL) /* empty is the same as not set */
4441 p = NULL;
4442 if (p != NULL)
4443 {
4444 p = vim_version_dir(p);
4445 if (p != NULL)
4446 *mustfree = TRUE;
4447 else
4448 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004449
4450#if defined(FEAT_MBYTE) && defined(WIN3264)
4451 if (enc_utf8)
4452 {
4453 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004454 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004455
4456 /* Convert from active codepage to UTF-8. Other conversions
4457 * are not done, because they would fail for non-ASCII
4458 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004459 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004460 if (pp != NULL)
4461 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004462 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004463 vim_free(p);
4464 p = pp;
4465 *mustfree = TRUE;
4466 }
4467 }
4468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 }
4471
4472 /*
4473 * When expanding $VIM or $VIMRUNTIME fails, try using:
4474 * - the directory name from 'helpfile' (unless it contains '$')
4475 * - the executable name from argv[0]
4476 */
4477 if (p == NULL)
4478 {
4479 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4480 p = p_hf;
4481#ifdef USE_EXE_NAME
4482 /*
4483 * Use the name of the executable, obtained from argv[0].
4484 */
4485 else
4486 p = exe_name;
4487#endif
4488 if (p != NULL)
4489 {
4490 /* remove the file name */
4491 pend = gettail(p);
4492
4493 /* remove "doc/" from 'helpfile', if present */
4494 if (p == p_hf)
4495 pend = remove_tail(p, pend, (char_u *)"doc");
4496
4497#ifdef USE_EXE_NAME
4498# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004499 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (p == exe_name)
4501 {
4502 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004503 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004505 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4506 if (pend1 != pend)
4507 {
4508 pnew = alloc((unsigned)(pend1 - p) + 15);
4509 if (pnew != NULL)
4510 {
4511 STRNCPY(pnew, p, (pend1 - p));
4512 STRCPY(pnew + (pend1 - p), "Resources/vim");
4513 p = pnew;
4514 pend = p + STRLEN(p);
4515 }
4516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518# endif
4519 /* remove "src/" from exe_name, if present */
4520 if (p == exe_name)
4521 pend = remove_tail(p, pend, (char_u *)"src");
4522#endif
4523
4524 /* for $VIM, remove "runtime/" or "vim54/", if present */
4525 if (!vimruntime)
4526 {
4527 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4528 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4529 }
4530
4531 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004532 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004535#ifdef MACOS_X
4536 if (p == exe_name || p == p_hf)
4537#endif
4538 /* check that the result is a directory name */
4539 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
4541 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004542 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 else
4544 {
4545#ifdef USE_EXE_NAME
4546 /* may add "/vim54" or "/runtime" if it exists */
4547 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4548 {
4549 vim_free(p);
4550 p = pend;
4551 }
4552#endif
4553 *mustfree = TRUE;
4554 }
4555 }
4556 }
4557
4558#ifdef HAVE_PATHDEF
4559 /* When there is a pathdef.c file we can use default_vim_dir and
4560 * default_vimruntime_dir */
4561 if (p == NULL)
4562 {
4563 /* Only use default_vimruntime_dir when it is not empty */
4564 if (vimruntime && *default_vimruntime_dir != NUL)
4565 {
4566 p = default_vimruntime_dir;
4567 *mustfree = FALSE;
4568 }
4569 else if (*default_vim_dir != NUL)
4570 {
4571 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4572 *mustfree = TRUE;
4573 else
4574 {
4575 p = default_vim_dir;
4576 *mustfree = FALSE;
4577 }
4578 }
4579 }
4580#endif
4581
4582 /*
4583 * Set the environment variable, so that the new value can be found fast
4584 * next time, and others can also use it (e.g. Perl).
4585 */
4586 if (p != NULL)
4587 {
4588 if (vimruntime)
4589 {
4590 vim_setenv((char_u *)"VIMRUNTIME", p);
4591 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 else
4594 {
4595 vim_setenv((char_u *)"VIM", p);
4596 didset_vim = TRUE;
4597 }
4598 }
4599 return p;
4600}
4601
4602/*
4603 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4604 * Return NULL if not, return its name in allocated memory otherwise.
4605 */
4606 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004607vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
4609 char_u *p;
4610
4611 if (vimdir == NULL || *vimdir == NUL)
4612 return NULL;
4613 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4614 if (p != NULL && mch_isdir(p))
4615 return p;
4616 vim_free(p);
4617 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4618 if (p != NULL && mch_isdir(p))
4619 return p;
4620 vim_free(p);
4621 return NULL;
4622}
4623
4624/*
4625 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4626 * the length of "name/". Otherwise return "pend".
4627 */
4628 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004629remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 int len = (int)STRLEN(name) + 1;
4632 char_u *newend = pend - len;
4633
4634 if (newend >= p
4635 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004636 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 return newend;
4638 return pend;
4639}
4640
Bram Moolenaar137374f2018-05-13 15:59:50 +02004641 void
4642vim_unsetenv(char_u *var)
4643{
4644#ifdef HAVE_UNSETENV
4645 unsetenv((char *)var);
4646#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004647 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004648#endif
4649}
4650
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 * Our portable version of setenv.
4654 */
4655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004656vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658#ifdef HAVE_SETENV
4659 mch_setenv((char *)name, (char *)val, 1);
4660#else
4661 char_u *envbuf;
4662
4663 /*
4664 * Putenv does not copy the string, it has to remain
4665 * valid. The allocated memory will never be freed.
4666 */
4667 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4668 if (envbuf != NULL)
4669 {
4670 sprintf((char *)envbuf, "%s=%s", name, val);
4671 putenv((char *)envbuf);
4672 }
4673#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004674#ifdef FEAT_GETTEXT
4675 /*
4676 * When setting $VIMRUNTIME adjust the directory to find message
4677 * translations to $VIMRUNTIME/lang.
4678 */
4679 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4680 {
4681 char_u *buf = concat_str(val, (char_u *)"/lang");
4682
4683 if (buf != NULL)
4684 {
4685 bindtextdomain(VIMPACKAGE, (char *)buf);
4686 vim_free(buf);
4687 }
4688 }
4689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690}
4691
4692#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4693/*
4694 * Function given to ExpandGeneric() to obtain an environment variable name.
4695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004697get_env_name(
4698 expand_T *xp UNUSED,
4699 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
Bram Moolenaard0573012017-10-28 21:11:06 +02004701# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004703 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 */
4705 return NULL;
4706# else
4707# ifndef __WIN32__
4708 /* Borland C++ 5.2 has this in a header file. */
4709 extern char **environ;
4710# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004711# define ENVNAMELEN 100
4712 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 char_u *str;
4714 int n;
4715
4716 str = (char_u *)environ[idx];
4717 if (str == NULL)
4718 return NULL;
4719
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004720 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
4722 if (str[n] == '=' || str[n] == NUL)
4723 break;
4724 name[n] = str[n];
4725 }
4726 name[n] = NUL;
4727 return name;
4728# endif
4729}
Bram Moolenaar24305862012-08-15 14:05:05 +02004730
4731/*
4732 * Find all user names for user completion.
4733 * Done only once and then cached.
4734 */
4735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004736init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004737{
Bram Moolenaar24305862012-08-15 14:05:05 +02004738 static int lazy_init_done = FALSE;
4739
4740 if (lazy_init_done)
4741 return;
4742
4743 lazy_init_done = TRUE;
4744 ga_init2(&ga_users, sizeof(char_u *), 20);
4745
4746# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4747 {
4748 char_u* user;
4749 struct passwd* pw;
4750
4751 setpwent();
4752 while ((pw = getpwent()) != NULL)
4753 /* pw->pw_name shouldn't be NULL but just in case... */
4754 if (pw->pw_name != NULL)
4755 {
4756 if (ga_grow(&ga_users, 1) == FAIL)
4757 break;
4758 user = vim_strsave((char_u*)pw->pw_name);
4759 if (user == NULL)
4760 break;
4761 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4762 }
4763 endpwent();
4764 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004765# elif defined(WIN3264)
4766 {
4767 char_u* user;
4768 DWORD nusers = 0, ntotal = 0, i;
4769 PUSER_INFO_0 uinfo;
4770
4771 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4772 &nusers, &ntotal, NULL) == NERR_Success)
4773 {
4774 for (i = 0; i < nusers; i++)
4775 {
4776 if (ga_grow(&ga_users, 1) == FAIL)
4777 break;
4778 user = utf16_to_enc(uinfo[i].usri0_name, NULL);
4779 if (user == NULL)
4780 break;
4781 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4782 }
4783
4784 NetApiBufferFree(uinfo);
4785 }
4786 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004787# endif
4788}
4789
4790/*
4791 * Function given to ExpandGeneric() to obtain an user names.
4792 */
4793 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004794get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004795{
4796 init_users();
4797 if (idx < ga_users.ga_len)
4798 return ((char_u **)ga_users.ga_data)[idx];
4799 return NULL;
4800}
4801
4802/*
4803 * Check whether name matches a user name. Return:
4804 * 0 if name does not match any user name.
4805 * 1 if name partially matches the beginning of a user name.
4806 * 2 is name fully matches a user name.
4807 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004808 int
4809match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004810{
4811 int i;
4812 int n = (int)STRLEN(name);
4813 int result = 0;
4814
4815 init_users();
4816 for (i = 0; i < ga_users.ga_len; i++)
4817 {
4818 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4819 return 2; /* full match */
4820 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4821 result = 1; /* partial match */
4822 }
4823 return result;
4824}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825#endif
4826
4827/*
4828 * Replace home directory by "~" in each space or comma separated file name in
4829 * 'src'.
4830 * If anything fails (except when out of space) dst equals src.
4831 */
4832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004833home_replace(
4834 buf_T *buf, /* when not NULL, check for help files */
4835 char_u *src, /* input file name */
4836 char_u *dst, /* where to put the result */
4837 int dstlen, /* maximum length of the result */
4838 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 spaces and commas in the file name. */
4840{
4841 size_t dirlen = 0, envlen = 0;
4842 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004843 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 char_u *p;
4845
4846 if (src == NULL)
4847 {
4848 *dst = NUL;
4849 return;
4850 }
4851
4852 /*
4853 * If the file is a help file, remove the path completely.
4854 */
4855 if (buf != NULL && buf->b_help)
4856 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004857 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 return;
4859 }
4860
4861 /*
4862 * We check both the value of the $HOME environment variable and the
4863 * "real" home directory.
4864 */
4865 if (homedir != NULL)
4866 dirlen = STRLEN(homedir);
4867
4868#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004869 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004871 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4872#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004873#ifdef WIN3264
4874 if (homedir_env == NULL)
4875 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4876#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004877 /* Empty is the same as not set. */
4878 if (homedir_env != NULL && *homedir_env == NUL)
4879 homedir_env = NULL;
4880
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004881#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004882 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004883 {
4884 int usedlen = 0;
4885 int flen;
4886 char_u *fbuf = NULL;
4887
4888 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004889 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004890 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004891 flen = (int)STRLEN(homedir_env);
4892 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4893 /* Remove the trailing / that is added to a directory. */
4894 homedir_env[flen - 1] = NUL;
4895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896#endif
4897
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (homedir_env != NULL)
4899 envlen = STRLEN(homedir_env);
4900
4901 if (!one)
4902 src = skipwhite(src);
4903 while (*src && dstlen > 0)
4904 {
4905 /*
4906 * Here we are at the beginning of a file name.
4907 * First, check to see if the beginning of the file name matches
4908 * $HOME or the "real" home directory. Check that there is a '/'
4909 * after the match (so that if e.g. the file is "/home/pieter/bla",
4910 * and the home directory is "/home/piet", the file does not end up
4911 * as "~er/bla" (which would seem to indicate the file "bla" in user
4912 * er's home directory)).
4913 */
4914 p = homedir;
4915 len = dirlen;
4916 for (;;)
4917 {
4918 if ( len
4919 && fnamencmp(src, p, len) == 0
4920 && (vim_ispathsep(src[len])
4921 || (!one && (src[len] == ',' || src[len] == ' '))
4922 || src[len] == NUL))
4923 {
4924 src += len;
4925 if (--dstlen > 0)
4926 *dst++ = '~';
4927
4928 /*
4929 * If it's just the home directory, add "/".
4930 */
4931 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4932 *dst++ = '/';
4933 break;
4934 }
4935 if (p == homedir_env)
4936 break;
4937 p = homedir_env;
4938 len = envlen;
4939 }
4940
4941 /* if (!one) skip to separator: space or comma */
4942 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4943 *dst++ = *src++;
4944 /* skip separator */
4945 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4946 *dst++ = *src++;
4947 }
4948 /* if (dstlen == 0) out of space, what to do??? */
4949
4950 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004951
4952 if (homedir_env != homedir_env_orig)
4953 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954}
4955
4956/*
4957 * Like home_replace, store the replaced string in allocated memory.
4958 * When something fails, NULL is returned.
4959 */
4960 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004961home_replace_save(
4962 buf_T *buf, /* when not NULL, check for help files */
4963 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
4965 char_u *dst;
4966 unsigned len;
4967
4968 len = 3; /* space for "~/" and trailing NUL */
4969 if (src != NULL) /* just in case */
4970 len += (unsigned)STRLEN(src);
4971 dst = alloc(len);
4972 if (dst != NULL)
4973 home_replace(buf, src, dst, len, TRUE);
4974 return dst;
4975}
4976
4977/*
4978 * Compare two file names and return:
4979 * FPC_SAME if they both exist and are the same file.
4980 * FPC_SAMEX if they both don't exist and have the same file name.
4981 * FPC_DIFF if they both exist and are different files.
4982 * FPC_NOTX if they both don't exist.
4983 * FPC_DIFFX if one of them doesn't exist.
4984 * For the first name environment variables are expanded
4985 */
4986 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004987fullpathcmp(
4988 char_u *s1,
4989 char_u *s2,
4990 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
4992#ifdef UNIX
4993 char_u exp1[MAXPATHL];
4994 char_u full1[MAXPATHL];
4995 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004996 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 int r1, r2;
4998
4999 expand_env(s1, exp1, MAXPATHL);
5000 r1 = mch_stat((char *)exp1, &st1);
5001 r2 = mch_stat((char *)s2, &st2);
5002 if (r1 != 0 && r2 != 0)
5003 {
5004 /* if mch_stat() doesn't work, may compare the names */
5005 if (checkname)
5006 {
5007 if (fnamecmp(exp1, s2) == 0)
5008 return FPC_SAMEX;
5009 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5010 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5011 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5012 return FPC_SAMEX;
5013 }
5014 return FPC_NOTX;
5015 }
5016 if (r1 != 0 || r2 != 0)
5017 return FPC_DIFFX;
5018 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5019 return FPC_SAME;
5020 return FPC_DIFF;
5021#else
5022 char_u *exp1; /* expanded s1 */
5023 char_u *full1; /* full path of s1 */
5024 char_u *full2; /* full path of s2 */
5025 int retval = FPC_DIFF;
5026 int r1, r2;
5027
5028 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5029 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5030 {
5031 full1 = exp1 + MAXPATHL;
5032 full2 = full1 + MAXPATHL;
5033
5034 expand_env(s1, exp1, MAXPATHL);
5035 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5036 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5037
5038 /* If vim_FullName() fails, the file probably doesn't exist. */
5039 if (r1 != OK && r2 != OK)
5040 {
5041 if (checkname && fnamecmp(exp1, s2) == 0)
5042 retval = FPC_SAMEX;
5043 else
5044 retval = FPC_NOTX;
5045 }
5046 else if (r1 != OK || r2 != OK)
5047 retval = FPC_DIFFX;
5048 else if (fnamecmp(full1, full2))
5049 retval = FPC_DIFF;
5050 else
5051 retval = FPC_SAME;
5052 vim_free(exp1);
5053 }
5054 return retval;
5055#endif
5056}
5057
5058/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005059 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005060 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005061 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 */
5063 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005064gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 char_u *p1, *p2;
5067
5068 if (fname == NULL)
5069 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005070 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005072 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005074 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 return p1;
5077}
5078
Bram Moolenaar31710262010-08-13 13:36:15 +02005079#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005080static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02005081
5082/*
5083 * Return the end of the directory name, on the first path
5084 * separator:
5085 * "/path/file", "/path/dir/", "/path//dir", "/file"
5086 * ^ ^ ^ ^
5087 */
5088 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005089gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005090{
5091 char_u *dir_end = fname;
5092 char_u *next_dir_end = fname;
5093 int look_for_sep = TRUE;
5094 char_u *p;
5095
5096 for (p = fname; *p != NUL; )
5097 {
5098 if (vim_ispathsep(*p))
5099 {
5100 if (look_for_sep)
5101 {
5102 next_dir_end = p;
5103 look_for_sep = FALSE;
5104 }
5105 }
5106 else
5107 {
5108 if (!look_for_sep)
5109 dir_end = next_dir_end;
5110 look_for_sep = TRUE;
5111 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005112 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005113 }
5114 return dir_end;
5115}
5116#endif
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005119 * Get pointer to tail of "fname", including path separators. Putting a NUL
5120 * here leaves the directory name. Takes care of "c:/" and "//".
5121 * Always returns a valid pointer.
5122 */
5123 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005124gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005125{
5126 char_u *p;
5127 char_u *t;
5128
5129 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5130 t = gettail(fname);
5131 while (t > p && after_pathsep(fname, t))
5132 --t;
5133#ifdef VMS
5134 /* path separator is part of the path */
5135 ++t;
5136#endif
5137 return t;
5138}
5139
5140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 * get the next path component (just after the next path separator).
5142 */
5143 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005144getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005147 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (*fname)
5149 ++fname;
5150 return fname;
5151}
5152
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153/*
5154 * Get a pointer to one character past the head of a path name.
5155 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5156 * If there is no head, path is returned.
5157 */
5158 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005159get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
5161 char_u *retval;
5162
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005163#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 /* may skip "c:" */
5165 if (isalpha(path[0]) && path[1] == ':')
5166 retval = path + 2;
5167 else
5168 retval = path;
5169#else
5170# if defined(AMIGA)
5171 /* may skip "label:" */
5172 retval = vim_strchr(path, ':');
5173 if (retval == NULL)
5174 retval = path;
5175# else /* Unix */
5176 retval = path;
5177# endif
5178#endif
5179
5180 while (vim_ispathsep(*retval))
5181 ++retval;
5182
5183 return retval;
5184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005187 * Return TRUE if 'c' is a path separator.
5188 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 */
5190 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005193#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005195#else
5196# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005198# else
5199# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5201 return (c == ':' || c == '[' || c == ']' || c == '/'
5202 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005203# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005205# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208}
5209
Bram Moolenaar69c35002013-11-04 02:54:12 +01005210/*
5211 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5212 */
5213 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005214vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005215{
5216 return vim_ispathsep(c)
5217#ifdef BACKSLASH_IN_FILENAME
5218 && c != ':'
5219#endif
5220 ;
5221}
5222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5224/*
5225 * return TRUE if 'c' is a path list separator.
5226 */
5227 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005228vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
5230#ifdef UNIX
5231 return (c == ':');
5232#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005233 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234#endif
5235}
5236#endif
5237
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005238/*
5239 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5240 * It's done in-place.
5241 */
5242 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005243shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005244{
5245 char_u *tail, *s, *d;
5246 int skip = FALSE;
5247
5248 tail = gettail(str);
5249 d = str;
5250 for (s = str; ; ++s)
5251 {
5252 if (s >= tail) /* copy the whole tail */
5253 {
5254 *d++ = *s;
5255 if (*s == NUL)
5256 break;
5257 }
5258 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5259 {
5260 *d++ = *s;
5261 skip = FALSE;
5262 }
5263 else if (!skip)
5264 {
5265 *d++ = *s; /* copy next char */
5266 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5267 skip = TRUE;
5268# ifdef FEAT_MBYTE
5269 if (has_mbyte)
5270 {
5271 int l = mb_ptr2len(s);
5272
5273 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005274 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005275 }
5276# endif
5277 }
5278 }
5279}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005280
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005281/*
5282 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5283 * Also returns TRUE if there is no directory name.
5284 * "fname" must be writable!.
5285 */
5286 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005287dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005288{
5289 char_u *p;
5290 int c;
5291 int retval;
5292
5293 p = gettail_sep(fname);
5294 if (p == fname)
5295 return TRUE;
5296 c = *p;
5297 *p = NUL;
5298 retval = mch_isdir(fname);
5299 *p = c;
5300 return retval;
5301}
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005304 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5305 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 */
5307 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005308vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005310#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005312#else
5313 if (p_fic)
5314 return MB_STRICMP(x, y);
5315 return STRCMP(x, y);
5316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317}
5318
5319 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005320vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005322#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005323 char_u *px = x;
5324 char_u *py = y;
5325 int cx = NUL;
5326 int cy = NUL;
5327
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005328 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005330 cx = PTR2CHAR(px);
5331 cy = PTR2CHAR(py);
5332 if (cx == NUL || cy == NUL
5333 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5334 && !(cx == '/' && cy == '\\')
5335 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005337 len -= MB_PTR2LEN(px);
5338 px += MB_PTR2LEN(px);
5339 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341 if (len == 0)
5342 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005343 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005344#else
5345 if (p_fic)
5346 return MB_STRNICMP(x, y, len);
5347 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
5351/*
5352 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005353 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 */
5355 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005356concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357{
5358 char_u *dest;
5359
5360 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5361 if (dest != NULL)
5362 {
5363 STRCPY(dest, fname1);
5364 if (sep)
5365 add_pathsep(dest);
5366 STRCAT(dest, fname2);
5367 }
5368 return dest;
5369}
5370
Bram Moolenaard6754642005-01-17 22:18:45 +00005371/*
5372 * Concatenate two strings and return the result in allocated memory.
5373 * Returns NULL when out of memory.
5374 */
5375 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005376concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005377{
5378 char_u *dest;
5379 size_t l = STRLEN(str1);
5380
5381 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5382 if (dest != NULL)
5383 {
5384 STRCPY(dest, str1);
5385 STRCPY(dest + l, str2);
5386 }
5387 return dest;
5388}
Bram Moolenaard6754642005-01-17 22:18:45 +00005389
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390/*
5391 * Add a path separator to a file name, unless it already ends in a path
5392 * separator.
5393 */
5394 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005395add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005397 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 STRCAT(p, PATHSEPSTR);
5399}
5400
5401/*
5402 * FullName_save - Make an allocated copy of a full file name.
5403 * Returns NULL when out of memory.
5404 */
5405 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005406FullName_save(
5407 char_u *fname,
5408 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005409 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410{
5411 char_u *buf;
5412 char_u *new_fname = NULL;
5413
5414 if (fname == NULL)
5415 return NULL;
5416
5417 buf = alloc((unsigned)MAXPATHL);
5418 if (buf != NULL)
5419 {
5420 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5421 new_fname = vim_strsave(buf);
5422 else
5423 new_fname = vim_strsave(fname);
5424 vim_free(buf);
5425 }
5426 return new_fname;
5427}
5428
5429#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5430
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005431static char_u *skip_string(char_u *p);
5432static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005433static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005434static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436/*
5437 * Find the start of a comment, not knowing if we are in a comment right now.
5438 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005439 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005441 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005442ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005443{
5444 return find_start_comment(curbuf->b_ind_maxcomment);
5445}
5446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005448find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 pos_T *pos;
5451 char_u *line;
5452 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005453 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005455 for (;;)
5456 {
5457 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5458 if (pos == NULL)
5459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005461 /*
5462 * Check if the comment start we found is inside a string.
5463 * If it is then restrict the search to below this line and try again.
5464 */
5465 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005466 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005467 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005468 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005469 break;
5470 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5471 if (cur_maxcomment <= 0)
5472 {
5473 pos = NULL;
5474 break;
5475 }
5476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 return pos;
5478}
5479
5480/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005481 * Find the start of a comment or raw string, not knowing if we are in a
5482 * comment or raw string right now.
5483 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005484 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005485 * Return NULL when not inside a comment or raw string.
5486 * "CORS" -> Comment Or Raw String
5487 */
5488 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005489ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005490{
Bram Moolenaar089af182015-10-07 11:41:49 +02005491 static pos_T comment_pos_copy;
5492 pos_T *comment_pos;
5493 pos_T *rs_pos;
5494
5495 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5496 if (comment_pos != NULL)
5497 {
5498 /* Need to make a copy of the static pos in findmatchlimit(),
5499 * calling find_start_rawstring() may change it. */
5500 comment_pos_copy = *comment_pos;
5501 comment_pos = &comment_pos_copy;
5502 }
5503 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005504
5505 /* If comment_pos is before rs_pos the raw string is inside the comment.
5506 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005507 if (comment_pos == NULL || (rs_pos != NULL
5508 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005509 {
5510 if (is_raw != NULL && rs_pos != NULL)
5511 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005512 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005513 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005514 return comment_pos;
5515}
5516
5517/*
5518 * Find the start of a raw string, not knowing if we are in one right now.
5519 * Search starts at w_cursor.lnum and goes backwards.
5520 * Return NULL when not inside a raw string.
5521 */
5522 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005524{
5525 pos_T *pos;
5526 char_u *line;
5527 char_u *p;
5528 int cur_maxcomment = ind_maxcomment;
5529
5530 for (;;)
5531 {
5532 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5533 if (pos == NULL)
5534 break;
5535
5536 /*
5537 * Check if the raw string start we found is inside a string.
5538 * If it is then restrict the search to below this line and try again.
5539 */
5540 line = ml_get(pos->lnum);
5541 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5542 p = skip_string(p);
5543 if ((colnr_T)(p - line) <= pos->col)
5544 break;
5545 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5546 if (cur_maxcomment <= 0)
5547 {
5548 pos = NULL;
5549 break;
5550 }
5551 }
5552 return pos;
5553}
5554
5555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 * Skip to the end of a "string" and a 'c' character.
5557 * If there is no string or character, return argument unmodified.
5558 */
5559 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005560skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 int i;
5563
5564 /*
5565 * We loop, because strings may be concatenated: "date""time".
5566 */
5567 for ( ; ; ++p)
5568 {
5569 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5570 {
5571 if (!p[1]) /* ' at end of line */
5572 break;
5573 i = 2;
5574 if (p[1] == '\\') /* '\n' or '\000' */
5575 {
5576 ++i;
5577 while (vim_isdigit(p[i - 1])) /* '\000' */
5578 ++i;
5579 }
5580 if (p[i] == '\'') /* check for trailing ' */
5581 {
5582 p += i;
5583 continue;
5584 }
5585 }
5586 else if (p[0] == '"') /* start of string */
5587 {
5588 for (++p; p[0]; ++p)
5589 {
5590 if (p[0] == '\\' && p[1] != NUL)
5591 ++p;
5592 else if (p[0] == '"') /* end of string */
5593 break;
5594 }
5595 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005596 continue; /* continue for another string */
5597 }
5598 else if (p[0] == 'R' && p[1] == '"')
5599 {
5600 /* Raw string: R"[delim](...)[delim]" */
5601 char_u *delim = p + 2;
5602 char_u *paren = vim_strchr(delim, '(');
5603
5604 if (paren != NULL)
5605 {
5606 size_t delim_len = paren - delim;
5607
5608 for (p += 3; *p; ++p)
5609 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5610 && p[delim_len + 1] == '"')
5611 {
5612 p += delim_len + 1;
5613 break;
5614 }
5615 if (p[0] == '"')
5616 continue; /* continue for another string */
5617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 break; /* no string found */
5620 }
5621 if (!*p)
5622 --p; /* backup from NUL */
5623 return p;
5624}
5625#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5626
5627#if defined(FEAT_CINDENT) || defined(PROTO)
5628
5629/*
5630 * Do C or expression indenting on the current line.
5631 */
5632 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005633do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635# ifdef FEAT_EVAL
5636 if (*curbuf->b_p_inde != NUL)
5637 fixthisline(get_expr_indent);
5638 else
5639# endif
5640 fixthisline(get_c_indent);
5641}
5642
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005643/* Find result cache for cpp_baseclass */
5644typedef struct {
5645 int found;
5646 lpos_T lpos;
5647} cpp_baseclass_cache_T;
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649/*
5650 * Functions for C-indenting.
5651 * Most of this originally comes from Eric Fischer.
5652 */
5653/*
5654 * Below "XXX" means that this function may unlock the current line.
5655 */
5656
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005657static char_u *cin_skipcomment(char_u *);
5658static int cin_nocode(char_u *);
5659static pos_T *find_line_comment(void);
5660static int cin_has_js_key(char_u *text);
5661static int cin_islabel_skip(char_u **);
5662static int cin_isdefault(char_u *);
5663static char_u *after_label(char_u *l);
5664static int get_indent_nolabel(linenr_T lnum);
5665static int skip_label(linenr_T, char_u **pp);
5666static int cin_first_id_amount(void);
5667static int cin_get_equal_amount(linenr_T lnum);
5668static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005669static int cin_iscomment(char_u *);
5670static int cin_islinecomment(char_u *);
5671static int cin_isterminated(char_u *, int, int);
5672static int cin_isinit(void);
5673static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5674static int cin_isif(char_u *);
5675static int cin_iselse(char_u *);
5676static int cin_isdo(char_u *);
5677static int cin_iswhileofdo(char_u *, linenr_T);
5678static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5679static int cin_iswhileofdo_end(int terminated);
5680static int cin_isbreak(char_u *);
5681static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5682static int get_baseclass_amount(int col);
5683static int cin_ends_in(char_u *, char_u *, char_u *);
5684static int cin_starts_with(char_u *s, char *word);
5685static int cin_skip2pos(pos_T *trypos);
5686static pos_T *find_start_brace(void);
5687static pos_T *find_match_paren(int);
5688static pos_T *find_match_char(int c, int ind_maxparen);
5689static int corr_ind_maxparen(pos_T *startpos);
5690static int find_last_paren(char_u *l, int start, int end);
5691static int find_match(int lookfor, linenr_T ourscope);
5692static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694/*
5695 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005696 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 */
5698 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005699cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 while (*s)
5702 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005703 char_u *prev_s = s;
5704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005706
5707 /* Perl/shell # comment comment continues until eol. Require a space
5708 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005709 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005710 {
5711 s += STRLEN(s);
5712 break;
5713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 if (*s != '/')
5715 break;
5716 ++s;
5717 if (*s == '/') /* slash-slash comment continues till eol */
5718 {
5719 s += STRLEN(s);
5720 break;
5721 }
5722 if (*s != '*')
5723 break;
5724 for (++s; *s; ++s) /* skip slash-star comment */
5725 if (s[0] == '*' && s[1] == '/')
5726 {
5727 s += 2;
5728 break;
5729 }
5730 }
5731 return s;
5732}
5733
5734/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005735 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 * not considered code.
5737 */
5738 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005739cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 return *cin_skipcomment(s) == NUL;
5742}
5743
5744/*
5745 * Check previous lines for a "//" line comment, skipping over blank lines.
5746 */
5747 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005748find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 static pos_T pos;
5751 char_u *line;
5752 char_u *p;
5753
5754 pos = curwin->w_cursor;
5755 while (--pos.lnum > 0)
5756 {
5757 line = ml_get(pos.lnum);
5758 p = skipwhite(line);
5759 if (cin_islinecomment(p))
5760 {
5761 pos.col = (int)(p - line);
5762 return &pos;
5763 }
5764 if (*p != NUL)
5765 break;
5766 }
5767 return NULL;
5768}
5769
5770/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005771 * Return TRUE if "text" starts with "key:".
5772 */
5773 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005774cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005775{
5776 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005777 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005778
5779 if (*s == '\'' || *s == '"')
5780 {
5781 /* can be 'key': or "key": */
5782 quote = *s;
5783 ++s;
5784 }
5785 if (!vim_isIDc(*s)) /* need at least one ID character */
5786 return FALSE;
5787
5788 while (vim_isIDc(*s))
5789 ++s;
5790 if (*s == quote)
5791 ++s;
5792
5793 s = cin_skipcomment(s);
5794
5795 /* "::" is not a label, it's C++ */
5796 return (*s == ':' && s[1] != ':');
5797}
5798
5799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005801 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 */
5803 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005804cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 if (!vim_isIDc(**s)) /* need at least one ID character */
5807 return FALSE;
5808
5809 while (vim_isIDc(**s))
5810 (*s)++;
5811
5812 *s = cin_skipcomment(*s);
5813
5814 /* "::" is not a label, it's C++ */
5815 return (**s == ':' && *++*s != ':');
5816}
5817
5818/*
5819 * Recognize a label: "label:".
5820 * Note: curwin->w_cursor must be where we are looking for the label.
5821 */
5822 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005823cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 char_u *s;
5826
5827 s = cin_skipcomment(ml_get_curline());
5828
5829 /*
5830 * Exclude "default" from labels, since it should be indented
5831 * like a switch label. Same for C++ scope declarations.
5832 */
5833 if (cin_isdefault(s))
5834 return FALSE;
5835 if (cin_isscopedecl(s))
5836 return FALSE;
5837
5838 if (cin_islabel_skip(&s))
5839 {
5840 /*
5841 * Only accept a label if the previous line is terminated or is a case
5842 * label.
5843 */
5844 pos_T cursor_save;
5845 pos_T *trypos;
5846 char_u *line;
5847
5848 cursor_save = curwin->w_cursor;
5849 while (curwin->w_cursor.lnum > 1)
5850 {
5851 --curwin->w_cursor.lnum;
5852
5853 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005854 * If we're in a comment or raw string now, skip to the start of
5855 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 */
5857 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005858 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 curwin->w_cursor = *trypos;
5860
5861 line = ml_get_curline();
5862 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5863 continue;
5864 if (*(line = cin_skipcomment(line)) == NUL)
5865 continue;
5866
5867 curwin->w_cursor = cursor_save;
5868 if (cin_isterminated(line, TRUE, FALSE)
5869 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005870 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 || (cin_islabel_skip(&line) && cin_nocode(line)))
5872 return TRUE;
5873 return FALSE;
5874 }
5875 curwin->w_cursor = cursor_save;
5876 return TRUE; /* label at start of file??? */
5877 }
5878 return FALSE;
5879}
5880
5881/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005882 * Recognize structure initialization and enumerations:
5883 * "[typedef] [static|public|protected|private] enum"
5884 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 */
5886 static int
5887cin_isinit(void)
5888{
5889 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005890 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
5892 s = cin_skipcomment(ml_get_curline());
5893
Bram Moolenaar75342212013-03-07 13:13:52 +01005894 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 s = cin_skipcomment(s + 7);
5896
Bram Moolenaar75342212013-03-07 13:13:52 +01005897 for (;;)
5898 {
5899 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005900
Bram Moolenaar75342212013-03-07 13:13:52 +01005901 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5902 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005903 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005904 if (cin_starts_with(s, skip[i]))
5905 {
5906 s = cin_skipcomment(s + l);
5907 l = 0;
5908 break;
5909 }
5910 }
5911 if (l != 0)
5912 break;
5913 }
5914
5915 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 return TRUE;
5917
5918 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5919 return TRUE;
5920
5921 return FALSE;
5922}
5923
5924/*
5925 * Recognize a switch label: "case .*:" or "default:".
5926 */
5927 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005928cin_iscase(
5929 char_u *s,
5930 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005933 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {
5935 for (s += 4; *s; ++s)
5936 {
5937 s = cin_skipcomment(s);
5938 if (*s == ':')
5939 {
5940 if (s[1] == ':') /* skip over "::" for C++ */
5941 ++s;
5942 else
5943 return TRUE;
5944 }
5945 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005946 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5948 return FALSE; /* stop at comment */
5949 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005950 {
5951 /* JS etc. */
5952 if (strict)
5953 return FALSE; /* stop at string */
5954 else
5955 return TRUE;
5956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
5958 return FALSE;
5959 }
5960
5961 if (cin_isdefault(s))
5962 return TRUE;
5963 return FALSE;
5964}
5965
5966/*
5967 * Recognize a "default" switch label.
5968 */
5969 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005970cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
5972 return (STRNCMP(s, "default", 7) == 0
5973 && *(s = cin_skipcomment(s + 7)) == ':'
5974 && s[1] != ':');
5975}
5976
5977/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005978 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 */
5980 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005981cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
5983 int i;
5984
5985 s = cin_skipcomment(s);
5986 if (STRNCMP(s, "public", 6) == 0)
5987 i = 6;
5988 else if (STRNCMP(s, "protected", 9) == 0)
5989 i = 9;
5990 else if (STRNCMP(s, "private", 7) == 0)
5991 i = 7;
5992 else
5993 return FALSE;
5994 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5995}
5996
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005997/* Maximum number of lines to search back for a "namespace" line. */
5998#define FIND_NAMESPACE_LIM 20
5999
6000/*
6001 * Recognize a "namespace" scope declaration.
6002 */
6003 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006004cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006005{
6006 char_u *p;
6007 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006008 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006009
6010 s = cin_skipcomment(s);
6011 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6012 {
6013 p = cin_skipcomment(skipwhite(s + 9));
6014 while (*p != NUL)
6015 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006016 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006017 {
6018 has_name = TRUE; /* found end of a name */
6019 p = cin_skipcomment(skipwhite(p));
6020 }
6021 else if (*p == '{')
6022 {
6023 break;
6024 }
6025 else if (vim_iswordc(*p))
6026 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006027 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006028 if (has_name)
6029 return FALSE; /* word character after skipping past name */
6030 ++p;
6031 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006032 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6033 {
6034 if (!has_name_start || has_name)
6035 return FALSE;
6036 /* C++ 17 nested namespace */
6037 p += 3;
6038 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006039 else
6040 {
6041 return FALSE;
6042 }
6043 }
6044 return TRUE;
6045 }
6046 return FALSE;
6047}
6048
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006050 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6051 */
6052 static int
6053cin_is_cpp_extern_c(char_u *s)
6054{
6055 char_u *p;
6056 int has_string_literal = FALSE;
6057
6058 s = cin_skipcomment(s);
6059 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6060 {
6061 p = cin_skipcomment(skipwhite(s + 6));
6062 while (*p != NUL)
6063 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006064 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006065 {
6066 p = cin_skipcomment(skipwhite(p));
6067 }
6068 else if (*p == '{')
6069 {
6070 break;
6071 }
6072 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6073 {
6074 if (has_string_literal)
6075 return FALSE;
6076 has_string_literal = TRUE;
6077 p += 3;
6078 }
6079 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6080 && p[4] == '"')
6081 {
6082 if (has_string_literal)
6083 return FALSE;
6084 has_string_literal = TRUE;
6085 p += 5;
6086 }
6087 else
6088 {
6089 return FALSE;
6090 }
6091 }
6092 return has_string_literal ? TRUE : FALSE;
6093 }
6094 return FALSE;
6095}
6096
6097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 * Return a pointer to the first non-empty non-comment character after a ':'.
6099 * Return NULL if not found.
6100 * case 234: a = b;
6101 * ^
6102 */
6103 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006104after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105{
6106 for ( ; *l; ++l)
6107 {
6108 if (*l == ':')
6109 {
6110 if (l[1] == ':') /* skip over "::" for C++ */
6111 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006112 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 break;
6114 }
6115 else if (*l == '\'' && l[1] && l[2] == '\'')
6116 l += 2; /* skip over 'x' */
6117 }
6118 if (*l == NUL)
6119 return NULL;
6120 l = cin_skipcomment(l + 1);
6121 if (*l == NUL)
6122 return NULL;
6123 return l;
6124}
6125
6126/*
6127 * Get indent of line "lnum", skipping a label.
6128 * Return 0 if there is nothing after the label.
6129 */
6130 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006131get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133 char_u *l;
6134 pos_T fp;
6135 colnr_T col;
6136 char_u *p;
6137
6138 l = ml_get(lnum);
6139 p = after_label(l);
6140 if (p == NULL)
6141 return 0;
6142
6143 fp.col = (colnr_T)(p - l);
6144 fp.lnum = lnum;
6145 getvcol(curwin, &fp, &col, NULL, NULL);
6146 return (int)col;
6147}
6148
6149/*
6150 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006151 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 * label: if (asdf && asdfasdf)
6153 * ^
6154 */
6155 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006156skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 char_u *l;
6159 int amount;
6160 pos_T cursor_save;
6161
6162 cursor_save = curwin->w_cursor;
6163 curwin->w_cursor.lnum = lnum;
6164 l = ml_get_curline();
6165 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006166 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 {
6168 amount = get_indent_nolabel(lnum);
6169 l = after_label(ml_get_curline());
6170 if (l == NULL) /* just in case */
6171 l = ml_get_curline();
6172 }
6173 else
6174 {
6175 amount = get_indent();
6176 l = ml_get_curline();
6177 }
6178 *pp = l;
6179
6180 curwin->w_cursor = cursor_save;
6181 return amount;
6182}
6183
6184/*
6185 * Return the indent of the first variable name after a type in a declaration.
6186 * int a, indent of "a"
6187 * static struct foo b, indent of "b"
6188 * enum bla c, indent of "c"
6189 * Returns zero when it doesn't look like a declaration.
6190 */
6191 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006192cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 char_u *line, *p, *s;
6195 int len;
6196 pos_T fp;
6197 colnr_T col;
6198
6199 line = ml_get_curline();
6200 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006201 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6203 {
6204 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006205 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 }
6207 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6208 p = skipwhite(p + 6);
6209 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6210 p = skipwhite(p + 4);
6211 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6212 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6213 {
6214 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006215 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6216 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6217 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6218 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 p = s;
6220 }
6221 for (len = 0; vim_isIDc(p[len]); ++len)
6222 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006223 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 return 0;
6225
6226 p = skipwhite(p + len);
6227 fp.lnum = curwin->w_cursor.lnum;
6228 fp.col = (colnr_T)(p - line);
6229 getvcol(curwin, &fp, &col, NULL, NULL);
6230 return (int)col;
6231}
6232
6233/*
6234 * Return the indent of the first non-blank after an equal sign.
6235 * char *foo = "here";
6236 * Return zero if no (useful) equal sign found.
6237 * Return -1 if the line above "lnum" ends in a backslash.
6238 * foo = "asdf\
6239 * asdf\
6240 * here";
6241 */
6242 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006243cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
6245 char_u *line;
6246 char_u *s;
6247 colnr_T col;
6248 pos_T fp;
6249
6250 if (lnum > 1)
6251 {
6252 line = ml_get(lnum - 1);
6253 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6254 return -1;
6255 }
6256
6257 line = s = ml_get(lnum);
6258 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6259 {
6260 if (cin_iscomment(s)) /* ignore comments */
6261 s = cin_skipcomment(s);
6262 else
6263 ++s;
6264 }
6265 if (*s != '=')
6266 return 0;
6267
6268 s = skipwhite(s + 1);
6269 if (cin_nocode(s))
6270 return 0;
6271
6272 if (*s == '"') /* nice alignment for continued strings */
6273 ++s;
6274
6275 fp.lnum = lnum;
6276 fp.col = (colnr_T)(s - line);
6277 getvcol(curwin, &fp, &col, NULL, NULL);
6278 return (int)col;
6279}
6280
6281/*
6282 * Recognize a preprocessor statement: Any line that starts with '#'.
6283 */
6284 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006285cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006287 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 return TRUE;
6289 return FALSE;
6290}
6291
6292/*
6293 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6294 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6295 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006296 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 */
6298 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006299cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 char_u *line = *pp;
6302 linenr_T lnum = *lnump;
6303 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006304 int candidate_amount = *amount;
6305
6306 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6307 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006309 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 {
6311 if (cin_ispreproc(line))
6312 {
6313 retval = TRUE;
6314 *lnump = lnum;
6315 break;
6316 }
6317 if (lnum == 1)
6318 break;
6319 line = ml_get(--lnum);
6320 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6321 break;
6322 }
6323
6324 if (lnum != *lnump)
6325 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006326 if (retval)
6327 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 return retval;
6329}
6330
6331/*
6332 * Recognize the start of a C or C++ comment.
6333 */
6334 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006335cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6338}
6339
6340/*
6341 * Recognize the start of a "//" comment.
6342 */
6343 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006344cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 return (p[0] == '/' && p[1] == '/');
6347}
6348
6349/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006350 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6351 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006353 * If a line begins with an "else", only consider it terminated if no unmatched
6354 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 * Return the character terminating the line (ending char's have precedence if
6356 * both apply in order to determine initializations).
6357 */
6358 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006359cin_isterminated(
6360 char_u *s,
6361 int incl_open, /* include '{' at the end as terminator */
6362 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006364 char_u found_start = 0;
6365 unsigned n_open = 0;
6366 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
6368 s = cin_skipcomment(s);
6369
6370 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6371 found_start = *s;
6372
Bram Moolenaar496f9512011-05-19 16:35:09 +02006373 if (!found_start)
6374 is_else = cin_iselse(s);
6375
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 while (*s)
6377 {
6378 /* skip over comments, "" strings and 'c'haracters */
6379 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006380 if (*s == '}' && n_open > 0)
6381 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006382 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006383 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 && cin_nocode(s + 1))
6385 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006386 else if (*s == '{')
6387 {
6388 if (incl_open && cin_nocode(s + 1))
6389 return *s;
6390 else
6391 ++n_open;
6392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
6394 if (*s)
6395 s++;
6396 }
6397 return found_start;
6398}
6399
6400/*
6401 * Recognize the basic picture of a function declaration -- it needs to
6402 * have an open paren somewhere and a close paren at the end of the line and
6403 * no semicolons anywhere.
6404 * When a line ends in a comma we continue looking in the next line.
6405 * "sp" points to a string with the line. When looking at other lines it must
6406 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006407 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006408 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 */
6410 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006411cin_isfuncdecl(
6412 char_u **sp,
6413 linenr_T first_lnum,
6414 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 char_u *s;
6417 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006418 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006420 pos_T *trypos;
6421 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
6423 if (sp == NULL)
6424 s = ml_get(lnum);
6425 else
6426 s = *sp;
6427
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006428 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006429 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006430 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006431 {
6432 lnum = trypos->lnum;
6433 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006434 {
6435 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006436 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006437 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006438
6439 s = ml_get(lnum);
6440 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006441 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006442
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006443 /* Ignore line starting with #. */
6444 if (cin_ispreproc(s))
6445 return FALSE;
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6448 {
6449 if (cin_iscomment(s)) /* ignore comments */
6450 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006451 else if (*s == ':')
6452 {
6453 if (*(s + 1) == ':')
6454 s += 2;
6455 else
6456 /* To avoid a mistake in the following situation:
6457 * A::A(int a, int b)
6458 * : a(0) // <--not a function decl
6459 * , b(0)
6460 * {...
6461 */
6462 return FALSE;
6463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 else
6465 ++s;
6466 }
6467 if (*s != '(')
6468 return FALSE; /* ';', ' or " before any () or no '(' */
6469
6470 while (*s && *s != ';' && *s != '\'' && *s != '"')
6471 {
6472 if (*s == ')' && cin_nocode(s + 1))
6473 {
6474 /* ')' at the end: may have found a match
6475 * Check for he previous line not to end in a backslash:
6476 * #if defined(x) && \
6477 * defined(y)
6478 */
6479 lnum = first_lnum - 1;
6480 s = ml_get(lnum);
6481 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6482 retval = TRUE;
6483 goto done;
6484 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006485 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006487 int comma = (*s == ',');
6488
6489 /* ',' at the end: continue looking in the next line.
6490 * At the end: check for ',' in the next line, for this style:
6491 * func(arg1
6492 * , arg2) */
6493 for (;;)
6494 {
6495 if (lnum >= curbuf->b_ml.ml_line_count)
6496 break;
6497 s = ml_get(++lnum);
6498 if (!cin_ispreproc(s))
6499 break;
6500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 if (lnum >= curbuf->b_ml.ml_line_count)
6502 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006503 /* Require a comma at end of the line or a comma or ')' at the
6504 * start of next line. */
6505 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006506 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006507 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006508 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 }
6510 else if (cin_iscomment(s)) /* ignore comments */
6511 s = cin_skipcomment(s);
6512 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006515 just_started = FALSE;
6516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
6518
6519done:
6520 if (lnum != first_lnum && sp != NULL)
6521 *sp = ml_get(first_lnum);
6522
6523 return retval;
6524}
6525
6526 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006527cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006529 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530}
6531
6532 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006533cin_iselse(
6534 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 if (*p == '}') /* accept "} else" */
6537 p = cin_skipcomment(p + 1);
6538 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6539}
6540
6541 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006542cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543{
6544 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6545}
6546
6547/*
6548 * Check if this is a "while" that should have a matching "do".
6549 * We only accept a "while (condition) ;", with only white space between the
6550 * ')' and ';'. The condition may be spread over several lines.
6551 */
6552 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006553cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 pos_T cursor_save;
6556 pos_T *trypos;
6557 int retval = FALSE;
6558
6559 p = cin_skipcomment(p);
6560 if (*p == '}') /* accept "} while (cond);" */
6561 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006562 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
6564 cursor_save = curwin->w_cursor;
6565 curwin->w_cursor.lnum = lnum;
6566 curwin->w_cursor.col = 0;
6567 p = ml_get_curline();
6568 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6569 {
6570 ++p;
6571 ++curwin->w_cursor.col;
6572 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006573 if ((trypos = findmatchlimit(NULL, 0, 0,
6574 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6576 retval = TRUE;
6577 curwin->w_cursor = cursor_save;
6578 }
6579 return retval;
6580}
6581
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006582/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006583 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006584 * Return 0 if there is none.
6585 * Otherwise return !0 and update "*poffset" to point to the place where the
6586 * string was found.
6587 */
6588 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006589cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006590{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006591 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006592
6593 if (offset-- < 2)
6594 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006595 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006596 --offset;
6597
6598 offset -= 1;
6599 if (!STRNCMP(line + offset, "if", 2))
6600 goto probablyFound;
6601
6602 if (offset >= 1)
6603 {
6604 offset -= 1;
6605 if (!STRNCMP(line + offset, "for", 3))
6606 goto probablyFound;
6607
6608 if (offset >= 2)
6609 {
6610 offset -= 2;
6611 if (!STRNCMP(line + offset, "while", 5))
6612 goto probablyFound;
6613 }
6614 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006615 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006616
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006617probablyFound:
6618 if (!offset || !vim_isIDc(line[offset - 1]))
6619 {
6620 *poffset = offset;
6621 return 1;
6622 }
6623 return 0;
6624}
6625
6626/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006627 * Return TRUE if we are at the end of a do-while.
6628 * do
6629 * nothing;
6630 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006631 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006632 * Adjust the cursor to the line with "while".
6633 */
6634 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006635cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006636{
6637 char_u *line;
6638 char_u *p;
6639 char_u *s;
6640 pos_T *trypos;
6641 int i;
6642
6643 if (terminated != ';') /* there must be a ';' at the end */
6644 return FALSE;
6645
6646 p = line = ml_get_curline();
6647 while (*p != NUL)
6648 {
6649 p = cin_skipcomment(p);
6650 if (*p == ')')
6651 {
6652 s = skipwhite(p + 1);
6653 if (*s == ';' && cin_nocode(s + 1))
6654 {
6655 /* Found ");" at end of the line, now check there is "while"
6656 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006657 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006658 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006659 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006660 if (trypos != NULL)
6661 {
6662 s = cin_skipcomment(ml_get(trypos->lnum));
6663 if (*s == '}') /* accept "} while (cond);" */
6664 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006665 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006666 {
6667 curwin->w_cursor.lnum = trypos->lnum;
6668 return TRUE;
6669 }
6670 }
6671
6672 /* Searching may have made "line" invalid, get it again. */
6673 line = ml_get_curline();
6674 p = line + i;
6675 }
6676 }
6677 if (*p != NUL)
6678 ++p;
6679 }
6680 return FALSE;
6681}
6682
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006684cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6687}
6688
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006689/*
6690 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 * constructor-initialization. eg:
6692 *
6693 * class MyClass :
6694 * baseClass <-- here
6695 * class MyClass : public baseClass,
6696 * anotherBaseClass <-- here (should probably lineup ??)
6697 * MyClass::MyClass(...) :
6698 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006699 *
6700 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
6702 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006703cin_is_cpp_baseclass(
6704 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006706 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 char_u *s;
6708 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006709 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006710 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006712 if (pos->lnum <= lnum)
6713 return cached->found; /* Use the cached result */
6714
6715 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006717 s = skipwhite(line);
6718 if (*s == '#') /* skip #define FOO x ? (x) : x */
6719 return FALSE;
6720 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if (*s == NUL)
6722 return FALSE;
6723
6724 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6725
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006726 /* Search for a line starting with '#', empty, ending in ';' or containing
6727 * '{' or '}' and start below it. This handles the following situations:
6728 * a = cond ?
6729 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006730 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006731 * func::foo()
6732 * : something
6733 * {}
6734 * Foo::Foo (int one, int two)
6735 * : something(4),
6736 * somethingelse(3)
6737 * {}
6738 */
6739 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006741 line = ml_get(lnum - 1);
6742 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006743 if (*s == '#' || *s == NUL)
6744 break;
6745 while (*s != NUL)
6746 {
6747 s = cin_skipcomment(s);
6748 if (*s == '{' || *s == '}'
6749 || (*s == ';' && cin_nocode(s + 1)))
6750 break;
6751 if (*s != NUL)
6752 ++s;
6753 }
6754 if (*s != NUL)
6755 break;
6756 --lnum;
6757 }
6758
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006759 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006760 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006761 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006762 for (;;)
6763 {
6764 if (*s == NUL)
6765 {
6766 if (lnum == curwin->w_cursor.lnum)
6767 break;
6768 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006769 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006770 s = line;
6771 }
6772 if (s == line)
6773 {
6774 /* don't recognize "case (foo):" as a baseclass */
6775 if (cin_iscase(s, FALSE))
6776 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006777 s = cin_skipcomment(line);
6778 if (*s == NUL)
6779 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006780 }
6781
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006782 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006783 s = skip_string(s) + 1;
6784 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {
6786 if (s[1] == ':')
6787 {
6788 /* skip double colon. It can't be a constructor
6789 * initialization any more */
6790 lookfor_ctor_init = FALSE;
6791 s = cin_skipcomment(s + 2);
6792 }
6793 else if (lookfor_ctor_init || class_or_struct)
6794 {
6795 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006796 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 cpp_base_class = TRUE;
6798 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006799 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 s = cin_skipcomment(s + 1);
6801 }
6802 else
6803 s = cin_skipcomment(s + 1);
6804 }
6805 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6806 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6807 {
6808 class_or_struct = TRUE;
6809 lookfor_ctor_init = FALSE;
6810
6811 if (*s == 'c')
6812 s = cin_skipcomment(s + 5);
6813 else
6814 s = cin_skipcomment(s + 6);
6815 }
6816 else
6817 {
6818 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6819 {
6820 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6821 }
6822 else if (s[0] == ')')
6823 {
6824 /* Constructor-initialization is assumed if we come across
6825 * something like "):" */
6826 class_or_struct = FALSE;
6827 lookfor_ctor_init = TRUE;
6828 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006829 else if (s[0] == '?')
6830 {
6831 /* Avoid seeing '() :' after '?' as constructor init. */
6832 return FALSE;
6833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 else if (!vim_isIDc(s[0]))
6835 {
6836 /* if it is not an identifier, we are wrong */
6837 class_or_struct = FALSE;
6838 lookfor_ctor_init = FALSE;
6839 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006840 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {
6842 /* it can't be a constructor-initialization any more */
6843 lookfor_ctor_init = FALSE;
6844
6845 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006846 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006847 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006850 /* When the line ends in a comma don't align with it. */
6851 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006852 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006853
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 s = cin_skipcomment(s + 1);
6855 }
6856 }
6857
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006858 cached->found = cpp_base_class;
6859 if (cpp_base_class)
6860 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 return cpp_base_class;
6862}
6863
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006864 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006865get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006866{
6867 int amount;
6868 colnr_T vcol;
6869 pos_T *trypos;
6870
6871 if (col == 0)
6872 {
6873 amount = get_indent();
6874 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006875 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006876 amount = get_indent_lnum(trypos->lnum); /* XXX */
6877 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006878 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006879 }
6880 else
6881 {
6882 curwin->w_cursor.col = col;
6883 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6884 amount = (int)vcol;
6885 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006886 if (amount < curbuf->b_ind_cpp_baseclass)
6887 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006888 return amount;
6889}
6890
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891/*
6892 * Return TRUE if string "s" ends with the string "find", possibly followed by
6893 * white space and comments. Skip strings and comments.
6894 * Ignore "ignore" after "find" if it's not NULL.
6895 */
6896 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006897cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898{
6899 char_u *p = s;
6900 char_u *r;
6901 int len = (int)STRLEN(find);
6902
6903 while (*p != NUL)
6904 {
6905 p = cin_skipcomment(p);
6906 if (STRNCMP(p, find, len) == 0)
6907 {
6908 r = skipwhite(p + len);
6909 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6910 r = skipwhite(r + STRLEN(ignore));
6911 if (cin_nocode(r))
6912 return TRUE;
6913 }
6914 if (*p != NUL)
6915 ++p;
6916 }
6917 return FALSE;
6918}
6919
6920/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006921 * Return TRUE when "s" starts with "word" and then a non-ID character.
6922 */
6923 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006924cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006925{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006926 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006927
6928 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6929}
6930
6931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 * Skip strings, chars and comments until at or past "trypos".
6933 * Return the column found.
6934 */
6935 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006936cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 char_u *line;
6939 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006940 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942 p = line = ml_get(trypos->lnum);
6943 while (*p && (colnr_T)(p - line) < trypos->col)
6944 {
6945 if (cin_iscomment(p))
6946 p = cin_skipcomment(p);
6947 else
6948 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006949 new_p = skip_string(p);
6950 if (new_p == p)
6951 ++p;
6952 else
6953 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955 }
6956 return (int)(p - line);
6957}
6958
6959/*
6960 * Find the '{' at the start of the block we are in.
6961 * Return NULL if no match found.
6962 * Ignore a '{' that is in a comment, makes indenting the next three lines
6963 * work. */
6964/* foo() */
6965/* { */
6966/* } */
6967
6968 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006969find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970{
6971 pos_T cursor_save;
6972 pos_T *trypos;
6973 pos_T *pos;
6974 static pos_T pos_copy;
6975
6976 cursor_save = curwin->w_cursor;
6977 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6978 {
6979 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6980 trypos = &pos_copy;
6981 curwin->w_cursor = *trypos;
6982 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006983 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006985 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 break;
6987 if (pos != NULL)
6988 curwin->w_cursor.lnum = pos->lnum;
6989 }
6990 curwin->w_cursor = cursor_save;
6991 return trypos;
6992}
6993
6994/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006995 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006996 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 */
6998 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006999find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007001 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007002}
7003
7004 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007005find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007006{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 pos_T cursor_save;
7008 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007009 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007010 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007013 ind_maxp_wk = ind_maxparen;
7014retry:
7015 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {
7017 /* check if the ( is in a // comment */
7018 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007019 {
7020 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7021 if (ind_maxp_wk > 0)
7022 {
7023 curwin->w_cursor = *trypos;
7024 curwin->w_cursor.col = 0; /* XXX */
7025 goto retry;
7026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 else
7030 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007031 pos_T *trypos_wk;
7032
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7034 trypos = &pos_copy;
7035 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007036 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007037 {
7038 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7039 - trypos_wk->lnum);
7040 if (ind_maxp_wk > 0)
7041 {
7042 curwin->w_cursor = *trypos_wk;
7043 goto retry;
7044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 }
7048 }
7049 curwin->w_cursor = cursor_save;
7050 return trypos;
7051}
7052
7053/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007054 * Find the matching '(', ignoring it if it is in a comment or before an
7055 * unmatched {.
7056 * Return NULL if no match found.
7057 */
7058 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007059find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007060{
7061 pos_T *trypos = find_match_paren(ind_maxparen);
7062
7063 if (trypos != NULL)
7064 {
7065 pos_T *tryposBrace = find_start_brace();
7066
7067 /* If both an unmatched '(' and '{' is found. Ignore the '('
7068 * position if the '{' is further down. */
7069 if (tryposBrace != NULL
7070 && (trypos->lnum != tryposBrace->lnum
7071 ? trypos->lnum < tryposBrace->lnum
7072 : trypos->col < tryposBrace->col))
7073 trypos = NULL;
7074 }
7075 return trypos;
7076}
7077
7078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 * Return ind_maxparen corrected for the difference in line number between the
7080 * cursor position and "startpos". This makes sure that searching for a
7081 * matching paren above the cursor line doesn't find a match because of
7082 * looking a few lines further.
7083 */
7084 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007085corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7088
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007089 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7090 return curbuf->b_ind_maxparen - (int)n;
7091 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092}
7093
7094/*
7095 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007096 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 */
7098 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007099find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 int i;
7102 int retval = FALSE;
7103 int open_count = 0;
7104
7105 curwin->w_cursor.col = 0; /* default is start of line */
7106
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007107 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {
7109 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7110 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7111 if (l[i] == start)
7112 ++open_count;
7113 else if (l[i] == end)
7114 {
7115 if (open_count > 0)
7116 --open_count;
7117 else
7118 {
7119 curwin->w_cursor.col = i;
7120 retval = TRUE;
7121 }
7122 }
7123 }
7124 return retval;
7125}
7126
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007127/*
7128 * Parse 'cinoptions' and set the values in "curbuf".
7129 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7130 */
7131 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007132parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007133{
7134 char_u *p;
7135 char_u *l;
7136 char_u *digits;
7137 int n;
7138 int divider;
7139 int fraction = 0;
7140 int sw = (int)get_sw_value(buf);
7141
7142 /*
7143 * Set the default values.
7144 */
7145 /* Spaces from a block's opening brace the prevailing indent for that
7146 * block should be. */
7147 buf->b_ind_level = sw;
7148
7149 /* Spaces from the edge of the line an open brace that's at the end of a
7150 * line is imagined to be. */
7151 buf->b_ind_open_imag = 0;
7152
7153 /* Spaces from the prevailing indent for a line that is not preceded by
7154 * an opening brace. */
7155 buf->b_ind_no_brace = 0;
7156
7157 /* Column where the first { of a function should be located }. */
7158 buf->b_ind_first_open = 0;
7159
7160 /* Spaces from the prevailing indent a leftmost open brace should be
7161 * located. */
7162 buf->b_ind_open_extra = 0;
7163
7164 /* Spaces from the matching open brace (real location for one at the left
7165 * edge; imaginary location from one that ends a line) the matching close
7166 * brace should be located. */
7167 buf->b_ind_close_extra = 0;
7168
7169 /* Spaces from the edge of the line an open brace sitting in the leftmost
7170 * column is imagined to be. */
7171 buf->b_ind_open_left_imag = 0;
7172
7173 /* Spaces jump labels should be shifted to the left if N is non-negative,
7174 * otherwise the jump label will be put to column 1. */
7175 buf->b_ind_jump_label = -1;
7176
7177 /* Spaces from the switch() indent a "case xx" label should be located. */
7178 buf->b_ind_case = sw;
7179
7180 /* Spaces from the "case xx:" code after a switch() should be located. */
7181 buf->b_ind_case_code = sw;
7182
7183 /* Lineup break at end of case in switch() with case label. */
7184 buf->b_ind_case_break = 0;
7185
7186 /* Spaces from the class declaration indent a scope declaration label
7187 * should be located. */
7188 buf->b_ind_scopedecl = sw;
7189
7190 /* Spaces from the scope declaration label code should be located. */
7191 buf->b_ind_scopedecl_code = sw;
7192
7193 /* Amount K&R-style parameters should be indented. */
7194 buf->b_ind_param = sw;
7195
7196 /* Amount a function type spec should be indented. */
7197 buf->b_ind_func_type = sw;
7198
7199 /* Amount a cpp base class declaration or constructor initialization
7200 * should be indented. */
7201 buf->b_ind_cpp_baseclass = sw;
7202
7203 /* additional spaces beyond the prevailing indent a continuation line
7204 * should be located. */
7205 buf->b_ind_continuation = sw;
7206
7207 /* Spaces from the indent of the line with an unclosed parentheses. */
7208 buf->b_ind_unclosed = sw * 2;
7209
7210 /* Spaces from the indent of the line with an unclosed parentheses, which
7211 * itself is also unclosed. */
7212 buf->b_ind_unclosed2 = sw;
7213
7214 /* Suppress ignoring spaces from the indent of a line starting with an
7215 * unclosed parentheses. */
7216 buf->b_ind_unclosed_noignore = 0;
7217
7218 /* If the opening paren is the last nonwhite character on the line, and
7219 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7220 * context (for very long lines). */
7221 buf->b_ind_unclosed_wrapped = 0;
7222
7223 /* Suppress ignoring white space when lining up with the character after
7224 * an unclosed parentheses. */
7225 buf->b_ind_unclosed_whiteok = 0;
7226
7227 /* Indent a closing parentheses under the line start of the matching
7228 * opening parentheses. */
7229 buf->b_ind_matching_paren = 0;
7230
7231 /* Indent a closing parentheses under the previous line. */
7232 buf->b_ind_paren_prev = 0;
7233
7234 /* Extra indent for comments. */
7235 buf->b_ind_comment = 0;
7236
7237 /* Spaces from the comment opener when there is nothing after it. */
7238 buf->b_ind_in_comment = 3;
7239
7240 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7241 * after the comment opener. */
7242 buf->b_ind_in_comment2 = 0;
7243
7244 /* Max lines to search for an open paren. */
7245 buf->b_ind_maxparen = 20;
7246
7247 /* Max lines to search for an open comment. */
7248 buf->b_ind_maxcomment = 70;
7249
7250 /* Handle braces for java code. */
7251 buf->b_ind_java = 0;
7252
7253 /* Not to confuse JS object properties with labels. */
7254 buf->b_ind_js = 0;
7255
7256 /* Handle blocked cases correctly. */
7257 buf->b_ind_keep_case_label = 0;
7258
7259 /* Handle C++ namespace. */
7260 buf->b_ind_cpp_namespace = 0;
7261
7262 /* Handle continuation lines containing conditions of if(), for() and
7263 * while(). */
7264 buf->b_ind_if_for_while = 0;
7265
Bram Moolenaar6b643942017-03-05 19:44:06 +01007266 /* indentation for # comments */
7267 buf->b_ind_hash_comment = 0;
7268
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007269 /* Handle C++ extern "C" or "C++" */
7270 buf->b_ind_cpp_extern_c = 0;
7271
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007272 for (p = buf->b_p_cino; *p; )
7273 {
7274 l = p++;
7275 if (*p == '-')
7276 ++p;
7277 digits = p; /* remember where the digits start */
7278 n = getdigits(&p);
7279 divider = 0;
7280 if (*p == '.') /* ".5s" means a fraction */
7281 {
7282 fraction = atol((char *)++p);
7283 while (VIM_ISDIGIT(*p))
7284 {
7285 ++p;
7286 if (divider)
7287 divider *= 10;
7288 else
7289 divider = 10;
7290 }
7291 }
7292 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7293 {
7294 if (p == digits)
7295 n = sw; /* just "s" is one 'shiftwidth' */
7296 else
7297 {
7298 n *= sw;
7299 if (divider)
7300 n += (sw * fraction + divider / 2) / divider;
7301 }
7302 ++p;
7303 }
7304 if (l[1] == '-')
7305 n = -n;
7306
7307 /* When adding an entry here, also update the default 'cinoptions' in
7308 * doc/indent.txt, and add explanation for it! */
7309 switch (*l)
7310 {
7311 case '>': buf->b_ind_level = n; break;
7312 case 'e': buf->b_ind_open_imag = n; break;
7313 case 'n': buf->b_ind_no_brace = n; break;
7314 case 'f': buf->b_ind_first_open = n; break;
7315 case '{': buf->b_ind_open_extra = n; break;
7316 case '}': buf->b_ind_close_extra = n; break;
7317 case '^': buf->b_ind_open_left_imag = n; break;
7318 case 'L': buf->b_ind_jump_label = n; break;
7319 case ':': buf->b_ind_case = n; break;
7320 case '=': buf->b_ind_case_code = n; break;
7321 case 'b': buf->b_ind_case_break = n; break;
7322 case 'p': buf->b_ind_param = n; break;
7323 case 't': buf->b_ind_func_type = n; break;
7324 case '/': buf->b_ind_comment = n; break;
7325 case 'c': buf->b_ind_in_comment = n; break;
7326 case 'C': buf->b_ind_in_comment2 = n; break;
7327 case 'i': buf->b_ind_cpp_baseclass = n; break;
7328 case '+': buf->b_ind_continuation = n; break;
7329 case '(': buf->b_ind_unclosed = n; break;
7330 case 'u': buf->b_ind_unclosed2 = n; break;
7331 case 'U': buf->b_ind_unclosed_noignore = n; break;
7332 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7333 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7334 case 'm': buf->b_ind_matching_paren = n; break;
7335 case 'M': buf->b_ind_paren_prev = n; break;
7336 case ')': buf->b_ind_maxparen = n; break;
7337 case '*': buf->b_ind_maxcomment = n; break;
7338 case 'g': buf->b_ind_scopedecl = n; break;
7339 case 'h': buf->b_ind_scopedecl_code = n; break;
7340 case 'j': buf->b_ind_java = n; break;
7341 case 'J': buf->b_ind_js = n; break;
7342 case 'l': buf->b_ind_keep_case_label = n; break;
7343 case '#': buf->b_ind_hash_comment = n; break;
7344 case 'N': buf->b_ind_cpp_namespace = n; break;
7345 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007346 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007347 }
7348 if (*p == ',')
7349 ++p;
7350 }
7351}
7352
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007353/*
7354 * Return the desired indent for C code.
7355 * Return -1 if the indent should be left alone (inside a raw string).
7356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007358get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 pos_T cur_curpos;
7361 int amount;
7362 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007363 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 colnr_T col;
7365 char_u *theline;
7366 char_u *linecopy;
7367 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007368 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007370 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 pos_T our_paren_pos;
7372 char_u *start;
7373 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007374#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375#define BRACE_AT_START 2 /* '{' is at start of line */
7376#define BRACE_AT_END 3 /* '{' is at end of line */
7377 linenr_T ourscope;
7378 char_u *l;
7379 char_u *look;
7380 char_u terminated;
7381 int lookfor;
7382#define LOOKFOR_INITIAL 0
7383#define LOOKFOR_IF 1
7384#define LOOKFOR_DO 2
7385#define LOOKFOR_CASE 3
7386#define LOOKFOR_ANY 4
7387#define LOOKFOR_TERM 5
7388#define LOOKFOR_UNTERM 6
7389#define LOOKFOR_SCOPEDECL 7
7390#define LOOKFOR_NOBREAK 8
7391#define LOOKFOR_CPP_BASECLASS 9
7392#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007393#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007394#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
7396 int whilelevel;
7397 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 int n;
7399 int iscase;
7400 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007401 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007403 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007404 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007405 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007406 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007407 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007409 /* make a copy, value is changed below */
7410 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411
7412 /* remember where the cursor was when we started */
7413 cur_curpos = curwin->w_cursor;
7414
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007415 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007416 if (cur_curpos.lnum == 1)
7417 return 0;
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 /* Get a copy of the current contents of the line.
7420 * This is required, because only the most recent line obtained with
7421 * ml_get is valid! */
7422 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7423 if (linecopy == NULL)
7424 return 0;
7425
7426 /*
7427 * In insert mode and the cursor is on a ')' truncate the line at the
7428 * cursor position. We don't want to line up with the matching '(' when
7429 * inserting new stuff.
7430 * For unknown reasons the cursor might be past the end of the line, thus
7431 * check for that.
7432 */
7433 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007434 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 && linecopy[curwin->w_cursor.col] == ')')
7436 linecopy[curwin->w_cursor.col] = NUL;
7437
7438 theline = skipwhite(linecopy);
7439
7440 /* move the cursor to the start of the line */
7441
7442 curwin->w_cursor.col = 0;
7443
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007444 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007445
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007447 * If we are inside a raw string don't change the indent.
7448 * Ignore a raw string inside a comment.
7449 */
7450 comment_pos = ind_find_start_comment();
7451 if (comment_pos != NULL)
7452 {
7453 /* findmatchlimit() static pos is overwritten, make a copy */
7454 tryposCopy = *comment_pos;
7455 comment_pos = &tryposCopy;
7456 }
7457 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007458 if (trypos != NULL && (comment_pos == NULL
7459 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007460 {
7461 amount = -1;
7462 goto laterend;
7463 }
7464
7465 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 * #defines and so on always go at the left when included in 'cinkeys'.
7467 */
7468 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007469 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007470 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007471 goto theend;
7472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
7474 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007475 * Is it a non-case label? Then that goes at the left margin too unless:
7476 * - JS flag is set.
7477 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007479 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007480 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 {
7482 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007483 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485
7486 /*
7487 * If we're inside a "//" comment and there is a "//" comment in a
7488 * previous line, lineup with that one.
7489 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007490 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 && (trypos = find_line_comment()) != NULL) /* XXX */
7492 {
7493 /* find how indented the line beginning the comment is */
7494 getvcol(curwin, trypos, &col, NULL, NULL);
7495 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007496 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 }
7498
7499 /*
7500 * If we're inside a comment and not looking at the start of the
7501 * comment, try using the 'comments' option.
7502 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007503 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 {
7505 int lead_start_len = 2;
7506 int lead_middle_len = 1;
7507 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7508 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7509 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7510 char_u *p;
7511 int start_align = 0;
7512 int start_off = 0;
7513 int done = FALSE;
7514
7515 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007516 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007518 *lead_start = NUL;
7519 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
7521 p = curbuf->b_p_com;
7522 while (*p != NUL)
7523 {
7524 int align = 0;
7525 int off = 0;
7526 int what = 0;
7527
7528 while (*p != NUL && *p != ':')
7529 {
7530 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7531 what = *p++;
7532 else if (*p == COM_LEFT || *p == COM_RIGHT)
7533 align = *p++;
7534 else if (VIM_ISDIGIT(*p) || *p == '-')
7535 off = getdigits(&p);
7536 else
7537 ++p;
7538 }
7539
7540 if (*p == ':')
7541 ++p;
7542 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7543 if (what == COM_START)
7544 {
7545 STRCPY(lead_start, lead_end);
7546 lead_start_len = (int)STRLEN(lead_start);
7547 start_off = off;
7548 start_align = align;
7549 }
7550 else if (what == COM_MIDDLE)
7551 {
7552 STRCPY(lead_middle, lead_end);
7553 lead_middle_len = (int)STRLEN(lead_middle);
7554 }
7555 else if (what == COM_END)
7556 {
7557 /* If our line starts with the middle comment string, line it
7558 * up with the comment opener per the 'comments' option. */
7559 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7560 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7561 {
7562 done = TRUE;
7563 if (curwin->w_cursor.lnum > 1)
7564 {
7565 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007566 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 * the middle comment string matches in the previous
7568 * line, use the indent of that line. XXX */
7569 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7570 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7571 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7572 else if (STRNCMP(look, lead_middle,
7573 lead_middle_len) == 0)
7574 {
7575 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7576 break;
7577 }
7578 /* If the start comment string doesn't match with the
7579 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007580 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 lead_start, lead_start_len) != 0)
7582 continue;
7583 }
7584 if (start_off != 0)
7585 amount += start_off;
7586 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007587 amount += vim_strsize(lead_start)
7588 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 break;
7590 }
7591
7592 /* If our line starts with the end comment string, line it up
7593 * with the middle comment */
7594 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7595 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7596 {
7597 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7598 /* XXX */
7599 if (off != 0)
7600 amount += off;
7601 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007602 amount += vim_strsize(lead_start)
7603 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 done = TRUE;
7605 break;
7606 }
7607 }
7608 }
7609
7610 /* If our line starts with an asterisk, line up with the
7611 * asterisk in the comment opener; otherwise, line up
7612 * with the first character of the comment text.
7613 */
7614 if (done)
7615 ;
7616 else if (theline[0] == '*')
7617 amount += 1;
7618 else
7619 {
7620 /*
7621 * If we are more than one line away from the comment opener, take
7622 * the indent of the previous non-empty line. If 'cino' has "CO"
7623 * and we are just below the comment opener and there are any
7624 * white characters after it line up with the text after it;
7625 * otherwise, add the amount specified by "c" in 'cino'
7626 */
7627 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007628 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
7630 if (linewhite(lnum)) /* skip blank lines */
7631 continue;
7632 amount = get_indent_lnum(lnum); /* XXX */
7633 break;
7634 }
7635 if (amount == -1) /* use the comment opener */
7636 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007637 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007639 start = ml_get(comment_pos->lnum);
7640 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007642 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007644 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007646 if (curbuf->b_ind_in_comment2 || *look == NUL)
7647 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007650 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 }
7652
7653 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007654 * Are we looking at a ']' that has a match?
7655 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007656 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007657 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7658 {
7659 /* align with the line containing the '['. */
7660 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007661 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007662 }
7663
7664 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 * Are we inside parentheses or braces?
7666 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007667 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007668 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007669 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 || trypos != NULL)
7671 {
7672 if (trypos != NULL && tryposBrace != NULL)
7673 {
7674 /* Both an unmatched '(' and '{' is found. Use the one which is
7675 * closer to the current cursor position, set the other to NULL. */
7676 if (trypos->lnum != tryposBrace->lnum
7677 ? trypos->lnum < tryposBrace->lnum
7678 : trypos->col < tryposBrace->col)
7679 trypos = NULL;
7680 else
7681 tryposBrace = NULL;
7682 }
7683
7684 if (trypos != NULL)
7685 {
7686 /*
7687 * If the matching paren is more than one line away, use the indent of
7688 * a previous non-empty line that matches the same paren.
7689 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007690 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007692 /* Line up with the start of the matching paren line. */
7693 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7694 }
7695 else
7696 {
7697 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007698 our_paren_pos = *trypos;
7699 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007701 l = skipwhite(ml_get(lnum));
7702 if (cin_nocode(l)) /* skip comment lines */
7703 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007704 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007705 continue; /* ignore #define, #if, etc. */
7706 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007708 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007709 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007710 {
7711 lnum = trypos->lnum + 1;
7712 continue;
7713 }
7714
7715 /* XXX */
7716 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007717 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007718 && trypos->lnum == our_paren_pos.lnum
7719 && trypos->col == our_paren_pos.col)
7720 {
7721 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007723 if (theline[0] == ')')
7724 {
7725 if (our_paren_pos.lnum != lnum
7726 && cur_amount > amount)
7727 cur_amount = amount;
7728 amount = -1;
7729 }
7730 break;
7731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 }
7733 }
7734
7735 /*
7736 * Line up with line where the matching paren is. XXX
7737 * If the line starts with a '(' or the indent for unclosed
7738 * parentheses is zero, line up with the unclosed parentheses.
7739 */
7740 if (amount == -1)
7741 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007742 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007743 int is_if_for_while = 0;
7744
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007745 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007746 {
7747 /* Look for the outermost opening parenthesis on this line
7748 * and check whether it belongs to an "if", "for" or "while". */
7749
7750 pos_T cursor_save = curwin->w_cursor;
7751 pos_T outermost;
7752 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007753
7754 trypos = &our_paren_pos;
7755 do {
7756 outermost = *trypos;
7757 curwin->w_cursor.lnum = outermost.lnum;
7758 curwin->w_cursor.col = outermost.col;
7759
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007760 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007761 } while (trypos && trypos->lnum == outermost.lnum);
7762
7763 curwin->w_cursor = cursor_save;
7764
7765 line = ml_get(outermost.lnum);
7766
7767 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007768 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007769 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007770
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007771 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007772 look = skipwhite(look);
7773 if (*look == '(')
7774 {
7775 linenr_T save_lnum = curwin->w_cursor.lnum;
7776 char_u *line;
7777 int look_col;
7778
7779 /* Ignore a '(' in front of the line that has a match before
7780 * our matching '('. */
7781 curwin->w_cursor.lnum = our_paren_pos.lnum;
7782 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007783 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007784 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007785 if ((trypos = findmatchlimit(NULL, ')', 0,
7786 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007787 != NULL
7788 && trypos->lnum == our_paren_pos.lnum
7789 && trypos->col < our_paren_pos.col)
7790 ignore_paren_col = trypos->col + 1;
7791
7792 curwin->w_cursor.lnum = save_lnum;
7793 look = ml_get(our_paren_pos.lnum) + look_col;
7794 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007795 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7796 && is_if_for_while == 0)
7797 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007798 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {
7800 /*
7801 * If we're looking at a close paren, line up right there;
7802 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007803 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 * the last nonwhite character of the line, use either the
7805 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007806 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 * lines).
7808 */
7809 if (theline[0] != ')')
7810 {
7811 cur_amount = MAXCOL;
7812 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007813 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 && cin_ends_in(l, (char_u *)"(", NULL))
7815 {
7816 /* look for opening unmatched paren, indent one level
7817 * for each additional level */
7818 n = 1;
7819 for (col = 0; col < our_paren_pos.col; ++col)
7820 {
7821 switch (l[col])
7822 {
7823 case '(':
7824 case '{': ++n;
7825 break;
7826
7827 case ')':
7828 case '}': if (n > 1)
7829 --n;
7830 break;
7831 }
7832 }
7833
7834 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007835 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007837 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 our_paren_pos.col++;
7839 else
7840 {
7841 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007842 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 col++;
7844 if (l[col] != NUL) /* In case of trailing space */
7845 our_paren_pos.col = col;
7846 else
7847 our_paren_pos.col++;
7848 }
7849 }
7850
7851 /*
7852 * Find how indented the paren is, or the character after it
7853 * if we did the above "if".
7854 */
7855 if (our_paren_pos.col > 0)
7856 {
7857 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7858 if (cur_amount > (int)col)
7859 cur_amount = col;
7860 }
7861 }
7862
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007863 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {
7865 /* Line up with the start of the matching paren line. */
7866 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007867 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7868 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007869 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
7871 if (cur_amount != MAXCOL)
7872 amount = cur_amount;
7873 }
7874 else
7875 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007876 /* Add b_ind_unclosed2 for each '(' before our matching one,
7877 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007879 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {
7881 --our_paren_pos.col;
7882 switch (*ml_get_pos(&our_paren_pos))
7883 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007884 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 col = our_paren_pos.col;
7886 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007887 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 col = MAXCOL;
7889 break;
7890 }
7891 }
7892
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007893 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 * braces */
7895 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007896 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 else
7898 {
7899 curwin->w_cursor.lnum = our_paren_pos.lnum;
7900 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007901 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7902 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007903 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007905 {
7906 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007907 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007908 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007909 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 }
7912 /*
7913 * For a line starting with ')' use the minimum of the two
7914 * positions, to avoid giving it more indent than the previous
7915 * lines:
7916 * func_long_name( if (x
7917 * arg && yy
7918 * ) ^ not here ) ^ not here
7919 */
7920 if (cur_amount < amount)
7921 amount = cur_amount;
7922 }
7923 }
7924
7925 /* add extra indent for a comment */
7926 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007927 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 else
7930 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007931 /*
7932 * We are inside braces, there is a { before this line at the position
7933 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007934 * Make a copy of tryposBrace, it may point to pos_copy inside
7935 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007936 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007937 tryposCopy = *tryposBrace;
7938 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 ourscope = trypos->lnum;
7941 start = ml_get(ourscope);
7942
7943 /*
7944 * Now figure out how indented the line is in general.
7945 * If the brace was at the start of the line, we use that;
7946 * otherwise, check out the indentation of the line as
7947 * a whole and then add the "imaginary indent" to that.
7948 */
7949 look = skipwhite(start);
7950 if (*look == '{')
7951 {
7952 getvcol(curwin, trypos, &col, NULL, NULL);
7953 amount = col;
7954 if (*start == '{')
7955 start_brace = BRACE_IN_COL0;
7956 else
7957 start_brace = BRACE_AT_START;
7958 }
7959 else
7960 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007961 /* That opening brace might have been on a continuation
7962 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 curwin->w_cursor.lnum = ourscope;
7964
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007965 /* Position the cursor over the rightmost paren, so that
7966 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 lnum = ourscope;
7968 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007969 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7970 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 lnum = trypos->lnum;
7972
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007973 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 * case 1: if (asdf &&
7975 * ldfd) {
7976 * }
7977 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007978 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7979 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007981 else if (curbuf->b_ind_js)
7982 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007984 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
7986 start_brace = BRACE_AT_END;
7987 }
7988
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007989 /* For Javascript check if the line starts with "key:". */
7990 if (curbuf->b_ind_js)
7991 js_cur_has_key = cin_has_js_key(theline);
7992
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007994 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 * we want to be. otherwise, add the amount of room
7996 * that an indent is supposed to be.
7997 */
7998 if (theline[0] == '}')
7999 {
8000 /*
8001 * they may want closing braces to line up with something
8002 * other than the open brace. indulge them, if so.
8003 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008004 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006 else
8007 {
8008 /*
8009 * If we're looking at an "else", try to find an "if"
8010 * to match it with.
8011 * If we're looking at a "while", try to find a "do"
8012 * to match it with.
8013 */
8014 lookfor = LOOKFOR_INITIAL;
8015 if (cin_iselse(theline))
8016 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008017 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 lookfor = LOOKFOR_DO;
8019 if (lookfor != LOOKFOR_INITIAL)
8020 {
8021 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008022 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {
8024 amount = get_indent(); /* XXX */
8025 goto theend;
8026 }
8027 }
8028
8029 /*
8030 * We get here if we are not on an "while-of-do" or "else" (or
8031 * failed to find a matching "if").
8032 * Search backwards for something to line up with.
8033 * First set amount for when we don't find anything.
8034 */
8035
8036 /*
8037 * if the '{' is _really_ at the left margin, use the imaginary
8038 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008039 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 */
8041
8042 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8043 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008044 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008045 lookfor_cpp_namespace = TRUE;
8046 }
8047 else if (start_brace == BRACE_AT_START &&
8048 lookfor_cpp_namespace) /* '{' is at start */
8049 {
8050
8051 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 else
8054 {
8055 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008056 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008057 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008058
8059 l = skipwhite(ml_get_curline());
8060 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008061 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008062 else if (cin_is_cpp_extern_c(l))
8063 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 else
8066 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008067 /* Compensate for adding b_ind_open_extra later. */
8068 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 if (amount < 0)
8070 amount = 0;
8071 }
8072 }
8073
8074 lookfor_break = FALSE;
8075
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008076 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {
8078 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008079 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 }
8081 else if (cin_isscopedecl(theline)) /* private:, ... */
8082 {
8083 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008084 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 }
8086 else
8087 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008088 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8089 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 lookfor_break = TRUE;
8091
8092 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008093 /* b_ind_level from start of block */
8094 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 }
8096 scope_amount = amount;
8097 whilelevel = 0;
8098
8099 /*
8100 * Search backwards. If we find something we recognize, line up
8101 * with that.
8102 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008103 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 * the usual amount relative to the conditional
8105 * that opens the block.
8106 */
8107 curwin->w_cursor = cur_curpos;
8108 for (;;)
8109 {
8110 curwin->w_cursor.lnum--;
8111 curwin->w_cursor.col = 0;
8112
8113 /*
8114 * If we went all the way back to the start of our scope, line
8115 * up with it.
8116 */
8117 if (curwin->w_cursor.lnum <= ourscope)
8118 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008119 /* We reached end of scope:
8120 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008122 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 * don't add ind_continuation, otherwise it is a variable
8124 * declaration:
8125 * int x,
8126 * here; <-- add ind_continuation
8127 */
8128 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8129 {
8130 if (curwin->w_cursor.lnum == 0
8131 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008132 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008134 /* nothing found (abuse curbuf->b_ind_maxparen as
8135 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 * initialization) */
8137 if (cont_amount > 0)
8138 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008139 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 amount += ind_continuation;
8141 break;
8142 }
8143
8144 l = ml_get_curline();
8145
8146 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008147 * If we're in a comment or raw string now, skip to
8148 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008150 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 if (trypos != NULL)
8152 {
8153 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008154 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 continue;
8156 }
8157
8158 /*
8159 * Skip preprocessor directives and blank lines.
8160 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008161 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8162 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 continue;
8164
8165 if (cin_nocode(l))
8166 continue;
8167
8168 terminated = cin_isterminated(l, FALSE, TRUE);
8169
8170 /*
8171 * If we are at top level and the line looks like a
8172 * function declaration, we are done
8173 * (it's a variable declaration).
8174 */
8175 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008176 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {
8178 /* if the line is terminated with another ','
8179 * it is a continued variable initialization.
8180 * don't add extra indent.
8181 * TODO: does not work, if a function
8182 * declaration is split over multiple lines:
8183 * cin_isfuncdecl returns FALSE then.
8184 */
8185 if (terminated == ',')
8186 break;
8187
8188 /* if it es a enum declaration or an assignment,
8189 * we are done.
8190 */
8191 if (terminated != ';' && cin_isinit())
8192 break;
8193
8194 /* nothing useful found */
8195 if (terminated == 0 || terminated == '{')
8196 continue;
8197 }
8198
8199 if (terminated != ';')
8200 {
8201 /* Skip parens and braces. Position the cursor
8202 * over the rightmost paren, so that matching it
8203 * will take us back to the start of the line.
8204 */ /* XXX */
8205 trypos = NULL;
8206 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008207 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008208 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209
8210 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008211 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212
8213 if (trypos != NULL)
8214 {
8215 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008216 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 continue;
8218 }
8219 }
8220
8221 /* it's a variable declaration, add indentation
8222 * like in
8223 * int a,
8224 * b;
8225 */
8226 if (cont_amount > 0)
8227 amount = cont_amount;
8228 else
8229 amount += ind_continuation;
8230 }
8231 else if (lookfor == LOOKFOR_UNTERM)
8232 {
8233 if (cont_amount > 0)
8234 amount = cont_amount;
8235 else
8236 amount += ind_continuation;
8237 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008238 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008239 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008240 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008241 && lookfor != LOOKFOR_CPP_BASECLASS
8242 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008243 {
8244 amount = scope_amount;
8245 if (theline[0] == '{')
8246 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008247 amount += curbuf->b_ind_open_extra;
8248 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008249 }
8250 }
8251
8252 if (lookfor_cpp_namespace)
8253 {
8254 /*
8255 * Looking for C++ namespace, need to look further
8256 * back.
8257 */
8258 if (curwin->w_cursor.lnum == ourscope)
8259 continue;
8260
8261 if (curwin->w_cursor.lnum == 0
8262 || curwin->w_cursor.lnum
8263 < ourscope - FIND_NAMESPACE_LIM)
8264 break;
8265
8266 l = ml_get_curline();
8267
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008268 /* If we're in a comment or raw string now, skip
8269 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008270 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008271 if (trypos != NULL)
8272 {
8273 curwin->w_cursor.lnum = trypos->lnum + 1;
8274 curwin->w_cursor.col = 0;
8275 continue;
8276 }
8277
8278 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008279 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8280 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008281 continue;
8282
8283 /* Finally the actual check for "namespace". */
8284 if (cin_is_cpp_namespace(l))
8285 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008286 amount += curbuf->b_ind_cpp_namespace
8287 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008288 break;
8289 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008290 else if (cin_is_cpp_extern_c(l))
8291 {
8292 amount += curbuf->b_ind_cpp_extern_c
8293 - added_to_amount;
8294 break;
8295 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008296
8297 if (cin_nocode(l))
8298 continue;
8299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
8301 break;
8302 }
8303
8304 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008305 * If we're in a comment or raw string now, skip to the start
8306 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008308 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
8310 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008311 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 continue;
8313 }
8314
8315 l = ml_get_curline();
8316
8317 /*
8318 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008319 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008321 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 if (iscase || cin_isscopedecl(l))
8323 {
8324 /* we are only looking for cpp base class
8325 * declaration/initialization any longer */
8326 if (lookfor == LOOKFOR_CPP_BASECLASS)
8327 break;
8328
8329 /* When looking for a "do" we are not interested in
8330 * labels. */
8331 if (whilelevel > 0)
8332 continue;
8333
8334 /*
8335 * case xx:
8336 * c = 99 + <- this indent plus continuation
8337 *-> here;
8338 */
8339 if (lookfor == LOOKFOR_UNTERM
8340 || lookfor == LOOKFOR_ENUM_OR_INIT)
8341 {
8342 if (cont_amount > 0)
8343 amount = cont_amount;
8344 else
8345 amount += ind_continuation;
8346 break;
8347 }
8348
8349 /*
8350 * case xx: <- line up with this case
8351 * x = 333;
8352 * case yy:
8353 */
8354 if ( (iscase && lookfor == LOOKFOR_CASE)
8355 || (iscase && lookfor_break)
8356 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8357 {
8358 /*
8359 * Check that this case label is not for another
8360 * switch()
8361 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008362 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008363 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {
8365 amount = get_indent(); /* XXX */
8366 break;
8367 }
8368 continue;
8369 }
8370
8371 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8372
8373 /*
8374 * case xx: if (cond) <- line up with this if
8375 * y = y + 1;
8376 * -> s = 99;
8377 *
8378 * case xx:
8379 * if (cond) <- line up with this line
8380 * y = y + 1;
8381 * -> s = 99;
8382 */
8383 if (lookfor == LOOKFOR_TERM)
8384 {
8385 if (n)
8386 amount = n;
8387
8388 if (!lookfor_break)
8389 break;
8390 }
8391
8392 /*
8393 * case xx: x = x + 1; <- line up with this x
8394 * -> y = y + 1;
8395 *
8396 * case xx: if (cond) <- line up with this if
8397 * -> y = y + 1;
8398 */
8399 if (n)
8400 {
8401 amount = n;
8402 l = after_label(ml_get_curline());
8403 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008404 {
8405 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008406 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008407 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008408 amount += curbuf->b_ind_level
8409 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 break;
8412 }
8413
8414 /*
8415 * Try to get the indent of a statement before the switch
8416 * label. If nothing is found, line up relative to the
8417 * switch label.
8418 * break; <- may line up with this line
8419 * case xx:
8420 * -> y = 1;
8421 */
8422 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008423 ? curbuf->b_ind_case_code
8424 : curbuf->b_ind_scopedecl_code);
8425 lookfor = curbuf->b_ind_case_break
8426 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 continue;
8428 }
8429
8430 /*
8431 * Looking for a switch() label or C++ scope declaration,
8432 * ignore other lines, skip {}-blocks.
8433 */
8434 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8435 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008436 if (find_last_paren(l, '{', '}')
8437 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008440 curwin->w_cursor.col = 0;
8441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 continue;
8443 }
8444
8445 /*
8446 * Ignore jump labels with nothing after them.
8447 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008448 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {
8450 l = after_label(ml_get_curline());
8451 if (l == NULL || cin_nocode(l))
8452 continue;
8453 }
8454
8455 /*
8456 * Ignore #defines, #if, etc.
8457 * Ignore comment and empty lines.
8458 * (need to get the line again, cin_islabel() may have
8459 * unlocked it)
8460 */
8461 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008462 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 || cin_nocode(l))
8464 continue;
8465
8466 /*
8467 * Are we at the start of a cpp base class declaration or
8468 * constructor initialization?
8469 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008470 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008471 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008472 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008473 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008474 l = ml_get_curline();
8475 }
8476 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {
8478 if (lookfor == LOOKFOR_UNTERM)
8479 {
8480 if (cont_amount > 0)
8481 amount = cont_amount;
8482 else
8483 amount += ind_continuation;
8484 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008485 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008487 /* Need to find start of the declaration. */
8488 lookfor = LOOKFOR_UNTERM;
8489 ind_continuation = 0;
8490 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 }
8492 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008493 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008494 amount = get_baseclass_amount(
8495 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 break;
8497 }
8498 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8499 {
8500 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008501 * declaration or initialization before the opening brace.
8502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if (cin_isterminated(l, TRUE, FALSE))
8504 break;
8505 else
8506 continue;
8507 }
8508
8509 /*
8510 * What happens next depends on the line being terminated.
8511 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008512 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 * 123,
8514 * sizeof
8515 * here
8516 * Otherwise check whether it is a enumeration or structure
8517 * initialisation (not indented) or a variable declaration
8518 * (indented).
8519 */
8520 terminated = cin_isterminated(l, FALSE, TRUE);
8521
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008522 if (js_cur_has_key)
8523 {
8524 js_cur_has_key = 0; /* only check the first line */
8525 if (curbuf->b_ind_js && terminated == ',')
8526 {
8527 /* For Javascript we might be inside an object:
8528 * key: something, <- align with this
8529 * key: something
8530 * or:
8531 * key: something + <- align with this
8532 * something,
8533 * key: something
8534 */
8535 lookfor = LOOKFOR_JS_KEY;
8536 }
8537 }
8538 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8539 {
8540 amount = get_indent();
8541 break;
8542 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008543 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008544 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008545 if (tryposBrace != NULL && tryposBrace->lnum
8546 >= curwin->w_cursor.lnum)
8547 break;
8548 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008549 /* line below current line is the one that starts a
8550 * (possibly broken) line ending in a comma. */
8551 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008552 else
8553 {
8554 amount = get_indent();
8555 if (curwin->w_cursor.lnum - 1 == ourscope)
8556 /* line above is start of the scope, thus current
8557 * line is the one that stars a (possibly broken)
8558 * line ending in a comma. */
8559 break;
8560 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008561 }
8562
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8564 && terminated == ','))
8565 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008566 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8567 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008568 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 /*
8570 * if we're in the middle of a paren thing,
8571 * go back to the line that starts it so
8572 * we can get the right prevailing indent
8573 * if ( foo &&
8574 * bar )
8575 */
8576 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008577 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008579 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 */
8581 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008582 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008583 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8584 || (trypos->lnum == tryposBrace->lnum
8585 && trypos->col < tryposBrace->col)))
8586 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
8588 /*
8589 * If we are looking for ',', we also look for matching
8590 * braces.
8591 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008592 if (trypos == NULL && terminated == ','
8593 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008594 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
8596 if (trypos != NULL)
8597 {
8598 /*
8599 * Check if we are on a case label now. This is
8600 * handled above.
8601 * case xx: if ( asdf &&
8602 * asdf)
8603 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008604 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008606 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {
8608 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008609 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 continue;
8611 }
8612 }
8613
8614 /*
8615 * Skip over continuation lines to find the one to get the
8616 * indent from
8617 * char *usethis = "bla\
8618 * bla",
8619 * here;
8620 */
8621 if (terminated == ',')
8622 {
8623 while (curwin->w_cursor.lnum > 1)
8624 {
8625 l = ml_get(curwin->w_cursor.lnum - 1);
8626 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8627 break;
8628 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008629 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 }
8631 }
8632
8633 /*
8634 * Get indent and pointer to text for current line,
8635 * ignoring any jump label. XXX
8636 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008637 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008638 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008639 else
8640 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 /*
8642 * If this is just above the line we are indenting, and it
8643 * starts with a '{', line it up with this line.
8644 * while (not)
8645 * -> {
8646 * }
8647 */
8648 if (terminated != ',' && lookfor != LOOKFOR_TERM
8649 && theline[0] == '{')
8650 {
8651 amount = cur_amount;
8652 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008653 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 * doesn't start with a '{', which must have a match
8655 * in the same line (scope is the same). Probably:
8656 * { 1, 2 },
8657 * -> { 3, 4 }
8658 */
8659 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008660 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008662 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {
8664 /* have to look back, whether it is a cpp base
8665 * class declaration or initialization */
8666 lookfor = LOOKFOR_CPP_BASECLASS;
8667 continue;
8668 }
8669 break;
8670 }
8671
8672 /*
8673 * Check if we are after an "if", "while", etc.
8674 * Also allow " } else".
8675 */
8676 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8677 {
8678 /*
8679 * Found an unterminated line after an if (), line up
8680 * with the last one.
8681 * if (cond)
8682 * 100 +
8683 * -> here;
8684 */
8685 if (lookfor == LOOKFOR_UNTERM
8686 || lookfor == LOOKFOR_ENUM_OR_INIT)
8687 {
8688 if (cont_amount > 0)
8689 amount = cont_amount;
8690 else
8691 amount += ind_continuation;
8692 break;
8693 }
8694
8695 /*
8696 * If this is just above the line we are indenting, we
8697 * are finished.
8698 * while (not)
8699 * -> here;
8700 * Otherwise this indent can be used when the line
8701 * before this is terminated.
8702 * yyy;
8703 * if (stat)
8704 * while (not)
8705 * xxx;
8706 * -> here;
8707 */
8708 amount = cur_amount;
8709 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008710 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 if (lookfor != LOOKFOR_TERM)
8712 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008713 amount += curbuf->b_ind_level
8714 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 break;
8716 }
8717
8718 /*
8719 * Special trick: when expecting the while () after a
8720 * do, line up with the while()
8721 * do
8722 * x = 1;
8723 * -> here
8724 */
8725 l = skipwhite(ml_get_curline());
8726 if (cin_isdo(l))
8727 {
8728 if (whilelevel == 0)
8729 break;
8730 --whilelevel;
8731 }
8732
8733 /*
8734 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008735 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 * Need to use the scope of this "else". XXX
8737 * If whilelevel != 0 continue looking for a "do {".
8738 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008739 if (cin_iselse(l) && whilelevel == 0)
8740 {
8741 /* If we're looking at "} else", let's make sure we
8742 * find the opening brace of the enclosing scope,
8743 * not the one from "if () {". */
8744 if (*l == '}')
8745 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008746 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008747
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008748 if ((trypos = find_start_brace()) == NULL
8749 || find_match(LOOKFOR_IF, trypos->lnum)
8750 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008751 break;
8752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 }
8754
8755 /*
8756 * If we're below an unterminated line that is not an
8757 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008758 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 * the line before this one.
8760 */
8761 else
8762 {
8763 /*
8764 * Found two unterminated lines on a row, line up with
8765 * the last one.
8766 * c = 99 +
8767 * 100 +
8768 * -> here;
8769 */
8770 if (lookfor == LOOKFOR_UNTERM)
8771 {
8772 /* When line ends in a comma add extra indent */
8773 if (terminated == ',')
8774 amount += ind_continuation;
8775 break;
8776 }
8777
8778 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8779 {
8780 /* Found two lines ending in ',', lineup with the
8781 * lowest one, but check for cpp base class
8782 * declaration/initialization, if it is an
8783 * opening brace or we are looking just for
8784 * enumerations/initializations. */
8785 if (terminated == ',')
8786 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008787 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 break;
8789
8790 lookfor = LOOKFOR_CPP_BASECLASS;
8791 continue;
8792 }
8793
8794 /* Ignore unterminated lines in between, but
8795 * reduce indent. */
8796 if (amount > cur_amount)
8797 amount = cur_amount;
8798 }
8799 else
8800 {
8801 /*
8802 * Found first unterminated line on a row, may
8803 * line up with this line, remember its indent
8804 * 100 +
8805 * -> here;
8806 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008807 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008809
8810 n = (int)STRLEN(l);
8811 if (terminated == ',' && (*skipwhite(l) == ']'
8812 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814
8815 /*
8816 * If previous line ends in ',', check whether we
8817 * are in an initialization or enum
8818 * struct xxx =
8819 * {
8820 * sizeof a,
8821 * 124 };
8822 * or a normal possible continuation line.
8823 * but only, of no other statement has been found
8824 * yet.
8825 */
8826 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8827 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008828 if (curbuf->b_ind_js)
8829 {
8830 /* Search for a line ending in a comma
8831 * and line up with the line below it
8832 * (could be the current line).
8833 * some = [
8834 * 1, <- line up here
8835 * 2,
8836 * some = [
8837 * 3 + <- line up here
8838 * 4 *
8839 * 5,
8840 * 6,
8841 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008842 if (cin_iscomment(skipwhite(l)))
8843 break;
8844 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008845 trypos = find_match_char('[',
8846 curbuf->b_ind_maxparen);
8847 if (trypos != NULL)
8848 {
8849 if (trypos->lnum
8850 == curwin->w_cursor.lnum - 1)
8851 {
8852 /* Current line is first inside
8853 * [], line up with it. */
8854 break;
8855 }
8856 ourscope = trypos->lnum;
8857 }
8858 }
8859 else
8860 {
8861 lookfor = LOOKFOR_ENUM_OR_INIT;
8862 cont_amount = cin_first_id_amount();
8863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 }
8865 else
8866 {
8867 if (lookfor == LOOKFOR_INITIAL
8868 && *l != NUL
8869 && l[STRLEN(l) - 1] == '\\')
8870 /* XXX */
8871 cont_amount = cin_get_equal_amount(
8872 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008873 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008874 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008875 && lookfor != LOOKFOR_COMMA
8876 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 lookfor = LOOKFOR_UNTERM;
8878 }
8879 }
8880 }
8881 }
8882
8883 /*
8884 * Check if we are after a while (cond);
8885 * If so: Ignore until the matching "do".
8886 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008887 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 {
8889 /*
8890 * Found an unterminated line after a while ();, line up
8891 * with the last one.
8892 * while (cond);
8893 * 100 + <- line up with this one
8894 * -> here;
8895 */
8896 if (lookfor == LOOKFOR_UNTERM
8897 || lookfor == LOOKFOR_ENUM_OR_INIT)
8898 {
8899 if (cont_amount > 0)
8900 amount = cont_amount;
8901 else
8902 amount += ind_continuation;
8903 break;
8904 }
8905
8906 if (whilelevel == 0)
8907 {
8908 lookfor = LOOKFOR_TERM;
8909 amount = get_indent(); /* XXX */
8910 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008911 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
8913 ++whilelevel;
8914 }
8915
8916 /*
8917 * We are after a "normal" statement.
8918 * If we had another statement we can stop now and use the
8919 * indent of that other statement.
8920 * Otherwise the indent of the current statement may be used,
8921 * search backwards for the next "normal" statement.
8922 */
8923 else
8924 {
8925 /*
8926 * Skip single break line, if before a switch label. It
8927 * may be lined up with the case label.
8928 */
8929 if (lookfor == LOOKFOR_NOBREAK
8930 && cin_isbreak(skipwhite(ml_get_curline())))
8931 {
8932 lookfor = LOOKFOR_ANY;
8933 continue;
8934 }
8935
8936 /*
8937 * Handle "do {" line.
8938 */
8939 if (whilelevel > 0)
8940 {
8941 l = cin_skipcomment(ml_get_curline());
8942 if (cin_isdo(l))
8943 {
8944 amount = get_indent(); /* XXX */
8945 --whilelevel;
8946 continue;
8947 }
8948 }
8949
8950 /*
8951 * Found a terminated line above an unterminated line. Add
8952 * the amount for a continuation line.
8953 * x = 1;
8954 * y = foo +
8955 * -> here;
8956 * or
8957 * int x = 1;
8958 * int foo,
8959 * -> here;
8960 */
8961 if (lookfor == LOOKFOR_UNTERM
8962 || lookfor == LOOKFOR_ENUM_OR_INIT)
8963 {
8964 if (cont_amount > 0)
8965 amount = cont_amount;
8966 else
8967 amount += ind_continuation;
8968 break;
8969 }
8970
8971 /*
8972 * Found a terminated line above a terminated line or "if"
8973 * etc. line. Use the amount of the line below us.
8974 * x = 1; x = 1;
8975 * if (asdf) y = 2;
8976 * while (asdf) ->here;
8977 * here;
8978 * ->foo;
8979 */
8980 if (lookfor == LOOKFOR_TERM)
8981 {
8982 if (!lookfor_break && whilelevel == 0)
8983 break;
8984 }
8985
8986 /*
8987 * First line above the one we're indenting is terminated.
8988 * To know what needs to be done look further backward for
8989 * a terminated line.
8990 */
8991 else
8992 {
8993 /*
8994 * position the cursor over the rightmost paren, so
8995 * that matching it will take us back to the start of
8996 * the line. Helps for:
8997 * func(asdr,
8998 * asdfasdf);
8999 * here;
9000 */
9001term_again:
9002 l = ml_get_curline();
9003 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009004 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009005 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 {
9007 /*
9008 * Check if we are on a case label now. This is
9009 * handled above.
9010 * case xx: if ( asdf &&
9011 * asdf)
9012 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009013 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009015 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 {
9017 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009018 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 continue;
9020 }
9021 }
9022
9023 /* When aligning with the case statement, don't align
9024 * with a statement after it.
9025 * case 1: { <-- don't use this { position
9026 * stat;
9027 * }
9028 * case 2:
9029 * stat;
9030 * }
9031 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009032 iscase = (curbuf->b_ind_keep_case_label
9033 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034
9035 /*
9036 * Get indent and pointer to text for current line,
9037 * ignoring any jump label.
9038 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009039 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
9041 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009042 amount += curbuf->b_ind_open_extra;
9043 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009044 l = skipwhite(l);
9045 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009046 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9048
9049 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009050 * When a terminated line starts with "else" skip to
9051 * the matching "if":
9052 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009053 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009054 * Need to use the scope of this "else". XXX
9055 * If whilelevel != 0 continue looking for a "do {".
9056 */
9057 if (lookfor == LOOKFOR_TERM
9058 && *l != '}'
9059 && cin_iselse(l)
9060 && whilelevel == 0)
9061 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009062 if ((trypos = find_start_brace()) == NULL
9063 || find_match(LOOKFOR_IF, trypos->lnum)
9064 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009065 break;
9066 continue;
9067 }
9068
9069 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 * If we're at the end of a block, skip to the start of
9071 * that block.
9072 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009073 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009074 if (find_last_paren(l, '{', '}') /* XXX */
9075 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009077 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 /* if not "else {" check for terminated again */
9079 /* but skip block for "} else {" */
9080 l = cin_skipcomment(ml_get_curline());
9081 if (*l == '}' || !cin_iselse(l))
9082 goto term_again;
9083 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009084 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 }
9086 }
9087 }
9088 }
9089 }
9090 }
9091
9092 /* add extra indent for a comment */
9093 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009094 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009095
9096 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009097 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9098 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009099
9100 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009102
9103 /*
9104 * ok -- we're not inside any sort of structure at all!
9105 *
9106 * This means we're at the top level, and everything should
9107 * basically just match where the previous line is, except
9108 * for the lines immediately following a function declaration,
9109 * which are K&R-style parameters and need to be indented.
9110 *
9111 * if our line starts with an open brace, forget about any
9112 * prevailing indent and make sure it looks like the start
9113 * of a function
9114 */
9115
9116 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009118 amount = curbuf->b_ind_first_open;
9119 goto theend;
9120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009122 /*
9123 * If the NEXT line is a function declaration, the current
9124 * line needs to be indented as a function type spec.
9125 * Don't do this if the current line looks like a comment or if the
9126 * current line is terminated, ie. ends in ';', or if the current line
9127 * contains { or }: "void f() {\n if (1)"
9128 */
9129 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9130 && !cin_nocode(theline)
9131 && vim_strchr(theline, '{') == NULL
9132 && vim_strchr(theline, '}') == NULL
9133 && !cin_ends_in(theline, (char_u *)":", NULL)
9134 && !cin_ends_in(theline, (char_u *)",", NULL)
9135 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9136 cur_curpos.lnum + 1)
9137 && !cin_isterminated(theline, FALSE, TRUE))
9138 {
9139 amount = curbuf->b_ind_func_type;
9140 goto theend;
9141 }
9142
9143 /* search backwards until we find something we recognize */
9144 amount = 0;
9145 curwin->w_cursor = cur_curpos;
9146 while (curwin->w_cursor.lnum > 1)
9147 {
9148 curwin->w_cursor.lnum--;
9149 curwin->w_cursor.col = 0;
9150
9151 l = ml_get_curline();
9152
9153 /*
9154 * If we're in a comment or raw string now, skip to the start
9155 * of it.
9156 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009157 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009159 curwin->w_cursor.lnum = trypos->lnum + 1;
9160 curwin->w_cursor.col = 0;
9161 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163
9164 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009165 * Are we at the start of a cpp base class declaration or
9166 * constructor initialization?
9167 */ /* XXX */
9168 n = FALSE;
9169 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009171 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9172 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009174 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009176 /* XXX */
9177 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9178 break;
9179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009181 /*
9182 * Skip preprocessor directives and blank lines.
9183 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009184 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009185 continue;
9186
9187 if (cin_nocode(l))
9188 continue;
9189
9190 /*
9191 * If the previous line ends in ',', use one level of
9192 * indentation:
9193 * int foo,
9194 * bar;
9195 * do this before checking for '}' in case of eg.
9196 * enum foobar
9197 * {
9198 * ...
9199 * } foo,
9200 * bar;
9201 */
9202 n = 0;
9203 if (cin_ends_in(l, (char_u *)",", NULL)
9204 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9205 {
9206 /* take us back to opening paren */
9207 if (find_last_paren(l, '(', ')')
9208 && (trypos = find_match_paren(
9209 curbuf->b_ind_maxparen)) != NULL)
9210 curwin->w_cursor = *trypos;
9211
9212 /* For a line ending in ',' that is a continuation line go
9213 * back to the first line with a backslash:
9214 * char *foo = "bla\
9215 * bla",
9216 * here;
9217 */
9218 while (n == 0 && curwin->w_cursor.lnum > 1)
9219 {
9220 l = ml_get(curwin->w_cursor.lnum - 1);
9221 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9222 break;
9223 --curwin->w_cursor.lnum;
9224 curwin->w_cursor.col = 0;
9225 }
9226
9227 amount = get_indent(); /* XXX */
9228
9229 if (amount == 0)
9230 amount = cin_first_id_amount();
9231 if (amount == 0)
9232 amount = ind_continuation;
9233 break;
9234 }
9235
9236 /*
9237 * If the line looks like a function declaration, and we're
9238 * not in a comment, put it the left margin.
9239 */
9240 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9241 break;
9242 l = ml_get_curline();
9243
9244 /*
9245 * Finding the closing '}' of a previous function. Put
9246 * current line at the left margin. For when 'cino' has "fs".
9247 */
9248 if (*skipwhite(l) == '}')
9249 break;
9250
9251 /* (matching {)
9252 * If the previous line ends on '};' (maybe followed by
9253 * comments) align at column 0. For example:
9254 * char *string_array[] = { "foo",
9255 * / * x * / "b};ar" }; / * foobar * /
9256 */
9257 if (cin_ends_in(l, (char_u *)"};", NULL))
9258 break;
9259
9260 /*
9261 * If the previous line ends on '[' we are probably in an
9262 * array constant:
9263 * something = [
9264 * 234, <- extra indent
9265 */
9266 if (cin_ends_in(l, (char_u *)"[", NULL))
9267 {
9268 amount = get_indent() + ind_continuation;
9269 break;
9270 }
9271
9272 /*
9273 * Find a line only has a semicolon that belongs to a previous
9274 * line ending in '}', e.g. before an #endif. Don't increase
9275 * indent then.
9276 */
9277 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9278 {
9279 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280
9281 while (curwin->w_cursor.lnum > 1)
9282 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009283 look = ml_get(--curwin->w_cursor.lnum);
9284 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009285 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009287 }
9288 if (curwin->w_cursor.lnum > 0
9289 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009292 curwin->w_cursor = curpos_save;
9293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009295 /*
9296 * If the PREVIOUS line is a function declaration, the current
9297 * line (and the ones that follow) needs to be indented as
9298 * parameters.
9299 */
9300 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9301 {
9302 amount = curbuf->b_ind_param;
9303 break;
9304 }
9305
9306 /*
9307 * If the previous line ends in ';' and the line before the
9308 * previous line ends in ',' or '\', ident to column zero:
9309 * int foo,
9310 * bar;
9311 * indent_to_0 here;
9312 */
9313 if (cin_ends_in(l, (char_u *)";", NULL))
9314 {
9315 l = ml_get(curwin->w_cursor.lnum - 1);
9316 if (cin_ends_in(l, (char_u *)",", NULL)
9317 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9318 break;
9319 l = ml_get_curline();
9320 }
9321
9322 /*
9323 * Doesn't look like anything interesting -- so just
9324 * use the indent of this line.
9325 *
9326 * Position the cursor over the rightmost paren, so that
9327 * matching it will take us back to the start of the line.
9328 */
9329 find_last_paren(l, '(', ')');
9330
9331 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9332 curwin->w_cursor = *trypos;
9333 amount = get_indent(); /* XXX */
9334 break;
9335 }
9336
9337 /* add extra indent for a comment */
9338 if (cin_iscomment(theline))
9339 amount += curbuf->b_ind_comment;
9340
9341 /* add extra indent if the previous line ended in a backslash:
9342 * "asdfasdf\
9343 * here";
9344 * char *foo = "asdf\
9345 * here";
9346 */
9347 if (cur_curpos.lnum > 1)
9348 {
9349 l = ml_get(cur_curpos.lnum - 1);
9350 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9351 {
9352 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9353 if (cur_amount > 0)
9354 amount = cur_amount;
9355 else if (cur_amount == 0)
9356 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 }
9358 }
9359
9360theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009361 if (amount < 0)
9362 amount = 0;
9363
9364laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 /* put the cursor back where it belongs */
9366 curwin->w_cursor = cur_curpos;
9367
9368 vim_free(linecopy);
9369
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 return amount;
9371}
9372
9373 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009374find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376 char_u *look;
9377 pos_T *theirscope;
9378 char_u *mightbeif;
9379 int elselevel;
9380 int whilelevel;
9381
9382 if (lookfor == LOOKFOR_IF)
9383 {
9384 elselevel = 1;
9385 whilelevel = 0;
9386 }
9387 else
9388 {
9389 elselevel = 0;
9390 whilelevel = 1;
9391 }
9392
9393 curwin->w_cursor.col = 0;
9394
9395 while (curwin->w_cursor.lnum > ourscope + 1)
9396 {
9397 curwin->w_cursor.lnum--;
9398 curwin->w_cursor.col = 0;
9399
9400 look = cin_skipcomment(ml_get_curline());
9401 if (cin_iselse(look)
9402 || cin_isif(look)
9403 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009404 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 {
9406 /*
9407 * if we've gone outside the braces entirely,
9408 * we must be out of scope...
9409 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009410 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 if (theirscope == NULL)
9412 break;
9413
9414 /*
9415 * and if the brace enclosing this is further
9416 * back than the one enclosing the else, we're
9417 * out of luck too.
9418 */
9419 if (theirscope->lnum < ourscope)
9420 break;
9421
9422 /*
9423 * and if they're enclosed in a *deeper* brace,
9424 * then we can ignore it because it's in a
9425 * different scope...
9426 */
9427 if (theirscope->lnum > ourscope)
9428 continue;
9429
9430 /*
9431 * if it was an "else" (that's not an "else if")
9432 * then we need to go back to another if, so
9433 * increment elselevel
9434 */
9435 look = cin_skipcomment(ml_get_curline());
9436 if (cin_iselse(look))
9437 {
9438 mightbeif = cin_skipcomment(look + 4);
9439 if (!cin_isif(mightbeif))
9440 ++elselevel;
9441 continue;
9442 }
9443
9444 /*
9445 * if it was a "while" then we need to go back to
9446 * another "do", so increment whilelevel. XXX
9447 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009448 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 {
9450 ++whilelevel;
9451 continue;
9452 }
9453
9454 /* If it's an "if" decrement elselevel */
9455 look = cin_skipcomment(ml_get_curline());
9456 if (cin_isif(look))
9457 {
9458 elselevel--;
9459 /*
9460 * When looking for an "if" ignore "while"s that
9461 * get in the way.
9462 */
9463 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9464 whilelevel = 0;
9465 }
9466
9467 /* If it's a "do" decrement whilelevel */
9468 if (cin_isdo(look))
9469 whilelevel--;
9470
9471 /*
9472 * if we've used up all the elses, then
9473 * this must be the if that we want!
9474 * match the indent level of that if.
9475 */
9476 if (elselevel <= 0 && whilelevel <= 0)
9477 {
9478 return OK;
9479 }
9480 }
9481 }
9482 return FAIL;
9483}
9484
9485# if defined(FEAT_EVAL) || defined(PROTO)
9486/*
9487 * Get indent level from 'indentexpr'.
9488 */
9489 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009490get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009492 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009493 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009494 pos_T save_pos;
9495 colnr_T save_curswant;
9496 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009498 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9499 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009501 /* Save and restore cursor position and curswant, in case it was changed
9502 * via :normal commands */
9503 save_pos = curwin->w_cursor;
9504 save_curswant = curwin->w_curswant;
9505 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009507 if (use_sandbox)
9508 ++sandbox;
9509 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009510
9511 /* Need to make a copy, the 'indentexpr' option could be changed while
9512 * evaluating it. */
9513 inde_copy = vim_strsave(curbuf->b_p_inde);
9514 if (inde_copy != NULL)
9515 {
9516 indent = (int)eval_to_number(inde_copy);
9517 vim_free(inde_copy);
9518 }
9519
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009520 if (use_sandbox)
9521 --sandbox;
9522 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
9524 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9525 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9526 * command. */
9527 save_State = State;
9528 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009529 curwin->w_cursor = save_pos;
9530 curwin->w_curswant = save_curswant;
9531 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 check_cursor();
9533 State = save_State;
9534
9535 /* If there is an error, just keep the current indent. */
9536 if (indent < 0)
9537 indent = get_indent();
9538
9539 return indent;
9540}
9541# endif
9542
9543#endif /* FEAT_CINDENT */
9544
9545#if defined(FEAT_LISP) || defined(PROTO)
9546
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009547static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
9549 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009550lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552 char_u buf[LSIZE];
9553 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009554 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
9556 while (*word != NUL)
9557 {
9558 (void)copy_option_part(&word, buf, LSIZE, ",");
9559 len = (int)STRLEN(buf);
9560 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9561 return TRUE;
9562 }
9563 return FALSE;
9564}
9565
9566/*
9567 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9568 * The incompatible newer method is quite a bit better at indenting
9569 * code in lisp-like languages than the traditional one; it's still
9570 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9571 *
9572 * TODO:
9573 * Findmatch() should be adapted for lisp, also to make showmatch
9574 * work correctly: now (v5.3) it seems all C/C++ oriented:
9575 * - it does not recognize the #\( and #\) notations as character literals
9576 * - it doesn't know about comments starting with a semicolon
9577 * - it incorrectly interprets '(' as a character literal
9578 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009579 * Update from Sergey Khorev:
9580 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 */
9582 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009583get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009585 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 int amount;
9587 char_u *that;
9588 colnr_T col;
9589 colnr_T firsttry;
9590 int parencount, quotecount;
9591 int vi_lisp;
9592
9593 /* Set vi_lisp to use the vi-compatible method */
9594 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9595
9596 realpos = curwin->w_cursor;
9597 curwin->w_cursor.col = 0;
9598
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009599 if ((pos = findmatch(NULL, '(')) == NULL)
9600 pos = findmatch(NULL, '[');
9601 else
9602 {
9603 paren = *pos;
9604 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009605 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009606 pos = &paren;
9607 }
9608 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 {
9610 /* Extra trick: Take the indent of the first previous non-white
9611 * line that is at the same () level. */
9612 amount = -1;
9613 parencount = 0;
9614
9615 while (--curwin->w_cursor.lnum >= pos->lnum)
9616 {
9617 if (linewhite(curwin->w_cursor.lnum))
9618 continue;
9619 for (that = ml_get_curline(); *that != NUL; ++that)
9620 {
9621 if (*that == ';')
9622 {
9623 while (*(that + 1) != NUL)
9624 ++that;
9625 continue;
9626 }
9627 if (*that == '\\')
9628 {
9629 if (*(that + 1) != NUL)
9630 ++that;
9631 continue;
9632 }
9633 if (*that == '"' && *(that + 1) != NUL)
9634 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009635 while (*++that && *that != '"')
9636 {
9637 /* skipping escaped characters in the string */
9638 if (*that == '\\')
9639 {
9640 if (*++that == NUL)
9641 break;
9642 if (that[1] == NUL)
9643 {
9644 ++that;
9645 break;
9646 }
9647 }
9648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009650 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009652 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 --parencount;
9654 }
9655 if (parencount == 0)
9656 {
9657 amount = get_indent();
9658 break;
9659 }
9660 }
9661
9662 if (amount == -1)
9663 {
9664 curwin->w_cursor.lnum = pos->lnum;
9665 curwin->w_cursor.col = pos->col;
9666 col = pos->col;
9667
9668 that = ml_get_curline();
9669
9670 if (vi_lisp && get_indent() == 0)
9671 amount = 2;
9672 else
9673 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009674 char_u *line = that;
9675
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 amount = 0;
9677 while (*that && col)
9678 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009679 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 col--;
9681 }
9682
9683 /*
9684 * Some keywords require "body" indenting rules (the
9685 * non-standard-lisp ones are Scheme special forms):
9686 *
9687 * (let ((a 1)) instead (let ((a 1))
9688 * (...)) of (...))
9689 */
9690
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009691 if (!vi_lisp && (*that == '(' || *that == '[')
9692 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 amount += 2;
9694 else
9695 {
9696 that++;
9697 amount++;
9698 firsttry = amount;
9699
Bram Moolenaar1c465442017-03-12 20:10:05 +01009700 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009702 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 ++that;
9704 }
9705
9706 if (*that && *that != ';') /* not a comment line */
9707 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009708 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009710 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 firsttry++;
9712
9713 parencount = 0;
9714 quotecount = 0;
9715
9716 if (vi_lisp
9717 || (*that != '"'
9718 && *that != '\''
9719 && *that != '#'
9720 && (*that < '0' || *that > '9')))
9721 {
9722 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009723 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 || quotecount
9725 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009726 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 && !quotecount
9728 && !parencount
9729 && vi_lisp)))
9730 {
9731 if (*that == '"')
9732 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009733 if ((*that == '(' || *that == '[')
9734 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009736 if ((*that == ')' || *that == ']')
9737 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 --parencount;
9739 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009740 amount += lbr_chartabsize_adv(
9741 line, &that, (colnr_T)amount);
9742 amount += lbr_chartabsize_adv(
9743 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 }
9745 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009746 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009748 amount += lbr_chartabsize(
9749 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 that++;
9751 }
9752 if (!*that || *that == ';')
9753 amount = firsttry;
9754 }
9755 }
9756 }
9757 }
9758 }
9759 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009760 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761
9762 curwin->w_cursor = realpos;
9763
9764 return amount;
9765}
9766#endif /* FEAT_LISP */
9767
9768 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009769prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009771#if defined(SIGHUP) && defined(SIG_IGN)
9772 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9773 * makes Vim exit and then handling SIGHUP causes various reentrance
9774 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009775 signal(SIGHUP, SIG_IGN);
9776#endif
9777
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778#ifdef FEAT_GUI
9779 if (gui.in_use)
9780 {
9781 gui.dying = TRUE;
9782 out_trash(); /* trash any pending output */
9783 }
9784 else
9785#endif
9786 {
9787 windgoto((int)Rows - 1, 0);
9788
9789 /*
9790 * Switch terminal mode back now, so messages end up on the "normal"
9791 * screen (if there are two screens).
9792 */
9793 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009794 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 out_flush();
9796 }
9797}
9798
9799/*
9800 * Preserve files and exit.
9801 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009802 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9803 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 */
9805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009806preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
9808 buf_T *buf;
9809
9810 prepare_to_exit();
9811
Bram Moolenaar4770d092006-01-12 23:22:24 +00009812 /* Setting this will prevent free() calls. That avoids calling free()
9813 * recursively when free() was invoked with a bad pointer. */
9814 really_exiting = TRUE;
9815
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 out_str(IObuff);
9817 screen_start(); /* don't know where cursor is now */
9818 out_flush();
9819
9820 ml_close_notmod(); /* close all not-modified buffers */
9821
Bram Moolenaar29323592016-07-24 22:04:11 +02009822 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 {
9824 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9825 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009826 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 screen_start(); /* don't know where cursor is now */
9828 out_flush();
9829 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9830 break;
9831 }
9832 }
9833
9834 ml_close_all(FALSE); /* close all memfiles, without deleting */
9835
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009836 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837
9838 getout(1);
9839}
9840
9841/*
9842 * return TRUE if "fname" exists.
9843 */
9844 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009845vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009847 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848
9849 if (mch_stat((char *)fname, &st))
9850 return FALSE;
9851 return TRUE;
9852}
9853
9854/*
9855 * Check for CTRL-C pressed, but only once in a while.
9856 * Should be used instead of ui_breakcheck() for functions that check for
9857 * each line in the file. Calling ui_breakcheck() each time takes too much
9858 * time, because it can be a system call.
9859 */
9860
9861#ifndef BREAKCHECK_SKIP
9862# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9863# define BREAKCHECK_SKIP 200
9864# else
9865# define BREAKCHECK_SKIP 32
9866# endif
9867#endif
9868
9869static int breakcheck_count = 0;
9870
9871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009872line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873{
9874 if (++breakcheck_count >= BREAKCHECK_SKIP)
9875 {
9876 breakcheck_count = 0;
9877 ui_breakcheck();
9878 }
9879}
9880
9881/*
9882 * Like line_breakcheck() but check 10 times less often.
9883 */
9884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009885fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886{
9887 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9888 {
9889 breakcheck_count = 0;
9890 ui_breakcheck();
9891 }
9892}
9893
9894/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009895 * Invoke expand_wildcards() for one pattern.
9896 * Expand items like "%:h" before the expansion.
9897 * Returns OK or FAIL.
9898 */
9899 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009900expand_wildcards_eval(
9901 char_u **pat, /* pointer to input pattern */
9902 int *num_file, /* resulting number of files */
9903 char_u ***file, /* array of resulting files */
9904 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009905{
9906 int ret = FAIL;
9907 char_u *eval_pat = NULL;
9908 char_u *exp_pat = *pat;
9909 char_u *ignored_msg;
9910 int usedlen;
9911
9912 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9913 {
9914 ++emsg_off;
9915 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9916 NULL, &ignored_msg, NULL);
9917 --emsg_off;
9918 if (eval_pat != NULL)
9919 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9920 }
9921
9922 if (exp_pat != NULL)
9923 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9924
9925 if (eval_pat != NULL)
9926 {
9927 vim_free(exp_pat);
9928 vim_free(eval_pat);
9929 }
9930
9931 return ret;
9932}
9933
9934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9936 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009937 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 */
9939 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009940expand_wildcards(
9941 int num_pat, /* number of input patterns */
9942 char_u **pat, /* array of input patterns */
9943 int *num_files, /* resulting number of files */
9944 char_u ***files, /* array of resulting files */
9945 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946{
9947 int retval;
9948 int i, j;
9949 char_u *p;
9950 int non_suf_match; /* number without matching suffix */
9951
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009952 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953
9954 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009955 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 return retval;
9957
9958#ifdef FEAT_WILDIGN
9959 /*
9960 * Remove names that match 'wildignore'.
9961 */
9962 if (*p_wig)
9963 {
9964 char_u *ffname;
9965
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009966 /* check all files in (*files)[] */
9967 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009969 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 if (ffname == NULL) /* out of memory */
9971 break;
9972# ifdef VMS
9973 vms_remove_version(ffname);
9974# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009975 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009977 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009978 vim_free((*files)[i]);
9979 for (j = i; j + 1 < *num_files; ++j)
9980 (*files)[j] = (*files)[j + 1];
9981 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 --i;
9983 }
9984 vim_free(ffname);
9985 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009986
9987 /* If the number of matches is now zero, we fail. */
9988 if (*num_files == 0)
9989 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009990 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009991 return FAIL;
9992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 }
9994#endif
9995
9996 /*
9997 * Move the names where 'suffixes' match to the end.
9998 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009999 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 {
10001 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010002 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010004 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
10006 /*
10007 * Move the name without matching suffix to the front
10008 * of the list.
10009 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010010 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010012 (*files)[j] = (*files)[j - 1];
10013 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 }
10015 }
10016 }
10017
10018 return retval;
10019}
10020
10021/*
10022 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10023 */
10024 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010025match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 int fnamelen, setsuflen;
10028 char_u *setsuf;
10029#define MAXSUFLEN 30 /* maximum length of a file suffix */
10030 char_u suf_buf[MAXSUFLEN];
10031
10032 fnamelen = (int)STRLEN(fname);
10033 setsuflen = 0;
10034 for (setsuf = p_su; *setsuf; )
10035 {
10036 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010037 if (setsuflen == 0)
10038 {
10039 char_u *tail = gettail(fname);
10040
10041 /* empty entry: match name without a '.' */
10042 if (vim_strchr(tail, '.') == NULL)
10043 {
10044 setsuflen = 1;
10045 break;
10046 }
10047 }
10048 else
10049 {
10050 if (fnamelen >= setsuflen
10051 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10052 (size_t)setsuflen) == 0)
10053 break;
10054 setsuflen = 0;
10055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 }
10057 return (setsuflen != 0);
10058}
10059
10060#if !defined(NO_EXPANDPATH) || defined(PROTO)
10061
10062# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010063static int vim_backtick(char_u *p);
10064static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065# endif
10066
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010067# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068/*
10069 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10070 * it's shared between these systems.
10071 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010072# if defined(PROTO)
10073# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074# else
10075# ifdef __BORLANDC__
10076# define _cdecl _RTLENTRYF
10077# endif
10078# endif
10079
10080/*
10081 * comparison function for qsort in dos_expandpath()
10082 */
10083 static int _cdecl
10084pstrcmp(const void *a, const void *b)
10085{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010086 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087}
10088
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010090 * Recursively expand one path component into all matching files and/or
10091 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 * Return the number of matches found.
10093 * "path" has backslashes before chars that are not to be expanded, starting
10094 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010095 * Return the number of matches found.
10096 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 */
10098 static int
10099dos_expandpath(
10100 garray_T *gap,
10101 char_u *path,
10102 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010103 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010104 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010106 char_u *buf;
10107 char_u *path_end;
10108 char_u *p, *s, *e;
10109 int start_len = gap->ga_len;
10110 char_u *pat;
10111 regmatch_T regmatch;
10112 int starts_with_dot;
10113 int matches;
10114 int len;
10115 int starstar = FALSE;
10116 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 WIN32_FIND_DATA fb;
10118 HANDLE hFind = (HANDLE)0;
10119# ifdef FEAT_MBYTE
10120 WIN32_FIND_DATAW wfb;
10121 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010124 int ok;
10125
10126 /* Expanding "**" may take a long time, check for CTRL-C. */
10127 if (stardepth > 0)
10128 {
10129 ui_breakcheck();
10130 if (got_int)
10131 return 0;
10132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010134 /* Make room for file name. When doing encoding conversion the actual
10135 * length may be quite a bit longer, thus use the maximum possible length. */
10136 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 if (buf == NULL)
10138 return 0;
10139
10140 /*
10141 * Find the first part in the path name that contains a wildcard or a ~1.
10142 * Copy it into buf, including the preceding characters.
10143 */
10144 p = buf;
10145 s = buf;
10146 e = NULL;
10147 path_end = path;
10148 while (*path_end != NUL)
10149 {
10150 /* May ignore a wildcard that has a backslash before it; it will
10151 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10152 if (path_end >= path + wildoff && rem_backslash(path_end))
10153 *p++ = *path_end++;
10154 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10155 {
10156 if (e != NULL)
10157 break;
10158 s = p + 1;
10159 }
10160 else if (path_end >= path + wildoff
10161 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10162 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010163# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 if (has_mbyte)
10165 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010166 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 STRNCPY(p, path_end, len);
10168 p += len;
10169 path_end += len;
10170 }
10171 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 *p++ = *path_end++;
10174 }
10175 e = p;
10176 *e = NUL;
10177
10178 /* now we have one wildcard component between s and e */
10179 /* Remove backslashes between "wildoff" and the start of the wildcard
10180 * component. */
10181 for (p = buf + wildoff; p < s; ++p)
10182 if (rem_backslash(p))
10183 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010184 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 --e;
10186 --s;
10187 }
10188
Bram Moolenaar231334e2005-07-25 20:46:57 +000010189 /* Check for "**" between "s" and "e". */
10190 for (p = s; p < e; ++p)
10191 if (p[0] == '*' && p[1] == '*')
10192 starstar = TRUE;
10193
Bram Moolenaard82103e2016-01-17 17:04:05 +010010194 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10196 if (pat == NULL)
10197 {
10198 vim_free(buf);
10199 return 0;
10200 }
10201
10202 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010203 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010204 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 regmatch.rm_ic = TRUE; /* Always ignore case */
10206 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010207 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010208 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 vim_free(pat);
10210
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010211 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 {
10213 vim_free(buf);
10214 return 0;
10215 }
10216
10217 /* remember the pattern or file name being looked for */
10218 matchname = vim_strsave(s);
10219
Bram Moolenaar231334e2005-07-25 20:46:57 +000010220 /* If "**" is by itself, this is the first time we encounter it and more
10221 * is following then find matches without any directory. */
10222 if (!didstar && stardepth < 100 && starstar && e - s == 2
10223 && *path_end == '/')
10224 {
10225 STRCPY(s, path_end + 1);
10226 ++stardepth;
10227 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10228 --stardepth;
10229 }
10230
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 /* Scan all files in the directory with "dir/ *.*" */
10232 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233# ifdef FEAT_MBYTE
10234 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10235 {
10236 /* The active codepage differs from 'encoding'. Attempt using the
10237 * wide function. If it fails because it is not implemented fall back
10238 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010239 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 if (wn != NULL)
10241 {
10242 hFind = FindFirstFileW(wn, &wfb);
10243 if (hFind == INVALID_HANDLE_VALUE
10244 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010245 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 }
10247 }
10248
10249 if (wn == NULL)
10250# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010251 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253
10254 while (ok)
10255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256# ifdef FEAT_MBYTE
10257 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010258 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 else
10260# endif
10261 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 /* Ignore entries starting with a dot, unless when asked for. Accept
10263 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010264 if ((p[0] != '.' || starts_with_dot
10265 || ((flags & EW_DODOT)
10266 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010268 || (regmatch.regprog != NULL
10269 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010270 || ((flags & EW_NOTWILD)
10271 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010275
10276 if (starstar && stardepth < 100)
10277 {
10278 /* For "**" in the pattern first go deeper in the tree to
10279 * find matches. */
10280 STRCPY(buf + len, "/**");
10281 STRCPY(buf + len + 3, path_end);
10282 ++stardepth;
10283 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10284 --stardepth;
10285 }
10286
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 STRCPY(buf + len, path_end);
10288 if (mch_has_exp_wildcard(path_end))
10289 {
10290 /* need to expand another component of the path */
10291 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010292 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 }
10294 else
10295 {
10296 /* no more wildcards, check if there is a match */
10297 /* remove backslashes for the remaining components only */
10298 if (*path_end != 0)
10299 backslash_halve(buf + len + 1);
10300 if (mch_getperm(buf) >= 0) /* add existing file */
10301 addfile(gap, buf, flags);
10302 }
10303 }
10304
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305# ifdef FEAT_MBYTE
10306 if (wn != NULL)
10307 {
10308 vim_free(p);
10309 ok = FindNextFileW(hFind, &wfb);
10310 }
10311 else
10312# endif
10313 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314
10315 /* If no more matches and no match was used, try expanding the name
10316 * itself. Finds the long name of a short filename. */
10317 if (!ok && matchname != NULL && gap->ga_len == start_len)
10318 {
10319 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 FindClose(hFind);
10321# ifdef FEAT_MBYTE
10322 if (wn != NULL)
10323 {
10324 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010325 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 if (wn != NULL)
10327 hFind = FindFirstFileW(wn, &wfb);
10328 }
10329 if (wn == NULL)
10330# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010331 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010333 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 }
10335 }
10336
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 FindClose(hFind);
10338# ifdef FEAT_MBYTE
10339 vim_free(wn);
10340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010342 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 vim_free(matchname);
10344
10345 matches = gap->ga_len - start_len;
10346 if (matches > 0)
10347 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10348 sizeof(char_u *), pstrcmp);
10349 return matches;
10350}
10351
10352 int
10353mch_expandpath(
10354 garray_T *gap,
10355 char_u *path,
10356 int flags) /* EW_* flags */
10357{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010358 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010360# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361
Bram Moolenaar231334e2005-07-25 20:46:57 +000010362#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10363 || defined(PROTO)
10364/*
10365 * Unix style wildcard expansion code.
10366 * It's here because it's used both for Unix and Mac.
10367 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010368static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010369
10370 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010371pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010372{
10373 return (pathcmp(*(char **)a, *(char **)b, -1));
10374}
10375
10376/*
10377 * Recursively expand one path component into all matching files and/or
10378 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10379 * "path" has backslashes before chars that are not to be expanded, starting
10380 * at "path + wildoff".
10381 * Return the number of matches found.
10382 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10383 */
10384 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010385unix_expandpath(
10386 garray_T *gap,
10387 char_u *path,
10388 int wildoff,
10389 int flags, /* EW_* flags */
10390 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010391{
10392 char_u *buf;
10393 char_u *path_end;
10394 char_u *p, *s, *e;
10395 int start_len = gap->ga_len;
10396 char_u *pat;
10397 regmatch_T regmatch;
10398 int starts_with_dot;
10399 int matches;
10400 int len;
10401 int starstar = FALSE;
10402 static int stardepth = 0; /* depth for "**" expansion */
10403
10404 DIR *dirp;
10405 struct dirent *dp;
10406
10407 /* Expanding "**" may take a long time, check for CTRL-C. */
10408 if (stardepth > 0)
10409 {
10410 ui_breakcheck();
10411 if (got_int)
10412 return 0;
10413 }
10414
10415 /* make room for file name */
10416 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10417 if (buf == NULL)
10418 return 0;
10419
10420 /*
10421 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010422 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010423 * Copy it into "buf", including the preceding characters.
10424 */
10425 p = buf;
10426 s = buf;
10427 e = NULL;
10428 path_end = path;
10429 while (*path_end != NUL)
10430 {
10431 /* May ignore a wildcard that has a backslash before it; it will
10432 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10433 if (path_end >= path + wildoff && rem_backslash(path_end))
10434 *p++ = *path_end++;
10435 else if (*path_end == '/')
10436 {
10437 if (e != NULL)
10438 break;
10439 s = p + 1;
10440 }
10441 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010442 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010443 || (!p_fic && (flags & EW_ICASE)
10444 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010445 e = p;
10446#ifdef FEAT_MBYTE
10447 if (has_mbyte)
10448 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010449 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010450 STRNCPY(p, path_end, len);
10451 p += len;
10452 path_end += len;
10453 }
10454 else
10455#endif
10456 *p++ = *path_end++;
10457 }
10458 e = p;
10459 *e = NUL;
10460
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010461 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010462 /* Remove backslashes between "wildoff" and the start of the wildcard
10463 * component. */
10464 for (p = buf + wildoff; p < s; ++p)
10465 if (rem_backslash(p))
10466 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010467 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010468 --e;
10469 --s;
10470 }
10471
10472 /* Check for "**" between "s" and "e". */
10473 for (p = s; p < e; ++p)
10474 if (p[0] == '*' && p[1] == '*')
10475 starstar = TRUE;
10476
10477 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010478 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010479 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10480 if (pat == NULL)
10481 {
10482 vim_free(buf);
10483 return 0;
10484 }
10485
10486 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010487 if (flags & EW_ICASE)
10488 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10489 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010490 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010491 if (flags & (EW_NOERROR | EW_NOTWILD))
10492 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010493 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010494 if (flags & (EW_NOERROR | EW_NOTWILD))
10495 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010496 vim_free(pat);
10497
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010498 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010499 {
10500 vim_free(buf);
10501 return 0;
10502 }
10503
10504 /* If "**" is by itself, this is the first time we encounter it and more
10505 * is following then find matches without any directory. */
10506 if (!didstar && stardepth < 100 && starstar && e - s == 2
10507 && *path_end == '/')
10508 {
10509 STRCPY(s, path_end + 1);
10510 ++stardepth;
10511 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10512 --stardepth;
10513 }
10514
10515 /* open the directory for scanning */
10516 *s = NUL;
10517 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10518
10519 /* Find all matching entries */
10520 if (dirp != NULL)
10521 {
10522 for (;;)
10523 {
10524 dp = readdir(dirp);
10525 if (dp == NULL)
10526 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010527 if ((dp->d_name[0] != '.' || starts_with_dot
10528 || ((flags & EW_DODOT)
10529 && dp->d_name[1] != NUL
10530 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010531 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10532 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010533 || ((flags & EW_NOTWILD)
10534 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010535 {
10536 STRCPY(s, dp->d_name);
10537 len = STRLEN(buf);
10538
10539 if (starstar && stardepth < 100)
10540 {
10541 /* For "**" in the pattern first go deeper in the tree to
10542 * find matches. */
10543 STRCPY(buf + len, "/**");
10544 STRCPY(buf + len + 3, path_end);
10545 ++stardepth;
10546 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10547 --stardepth;
10548 }
10549
10550 STRCPY(buf + len, path_end);
10551 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10552 {
10553 /* need to expand another component of the path */
10554 /* remove backslashes for the remaining components only */
10555 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10556 }
10557 else
10558 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010559 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010560
Bram Moolenaar231334e2005-07-25 20:46:57 +000010561 /* no more wildcards, check if there is a match */
10562 /* remove backslashes for the remaining components only */
10563 if (*path_end != NUL)
10564 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010565 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010566 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010567 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010568 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010569#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010570 size_t precomp_len = STRLEN(buf)+1;
10571 char_u *precomp_buf =
10572 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010573
Bram Moolenaar231334e2005-07-25 20:46:57 +000010574 if (precomp_buf)
10575 {
10576 mch_memmove(buf, precomp_buf, precomp_len);
10577 vim_free(precomp_buf);
10578 }
10579#endif
10580 addfile(gap, buf, flags);
10581 }
10582 }
10583 }
10584 }
10585
10586 closedir(dirp);
10587 }
10588
10589 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010590 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010591
10592 matches = gap->ga_len - start_len;
10593 if (matches > 0)
10594 qsort(((char_u **)gap->ga_data) + start_len, matches,
10595 sizeof(char_u *), pstrcmp);
10596 return matches;
10597}
10598#endif
10599
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010600#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010601static int find_previous_pathsep(char_u *path, char_u **psep);
10602static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10603static void expand_path_option(char_u *curdir, garray_T *gap);
10604static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10605static void uniquefy_paths(garray_T *gap, char_u *pattern);
10606static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010607
10608/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010609 * Moves "*psep" back to the previous path separator in "path".
10610 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010611 */
10612 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010613find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010614{
10615 /* skip the current separator */
10616 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010617 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010618
10619 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010620 while (*psep > path)
10621 {
10622 if (vim_ispathsep(**psep))
10623 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010624 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010625 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010626
10627 return FAIL;
10628}
10629
10630/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010631 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10632 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010633 */
10634 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010635is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010636{
10637 int j;
10638 int candidate_len;
10639 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010640 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010641 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010642
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010643 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010645 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010646 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010647
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010648 candidate_len = (int)STRLEN(maybe_unique);
10649 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010650 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010651 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010652
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010653 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010654 if (fnamecmp(maybe_unique, rival) == 0
10655 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010656 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010657 }
10658
Bram Moolenaar162bd912010-07-28 22:29:10 +020010659 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010660}
10661
10662/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010663 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010664 * paths are expanded to their equivalent fullpath. This includes the "."
10665 * (relative to current buffer directory) and empty path (relative to current
10666 * directory) notations.
10667 *
10668 * TODO: handle upward search (;) and path limiter (**N) notations by
10669 * expanding each into their equivalent path(s).
10670 */
10671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010672expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010673{
10674 char_u *path_option = *curbuf->b_p_path == NUL
10675 ? p_path : curbuf->b_p_path;
10676 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010677 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010678 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010679
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010680 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010681 return;
10682
10683 while (*path_option != NUL)
10684 {
10685 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10686
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010687 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010688 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010689 /* Relative to current buffer:
10690 * "/path/file" + "." -> "/path/"
10691 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010692 if (curbuf->b_ffname == NULL)
10693 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010694 p = gettail(curbuf->b_ffname);
10695 len = (int)(p - curbuf->b_ffname);
10696 if (len + (int)STRLEN(buf) >= MAXPATHL)
10697 continue;
10698 if (buf[1] == NUL)
10699 buf[len] = NUL;
10700 else
10701 STRMOVE(buf + len, buf + 2);
10702 mch_memmove(buf, curbuf->b_ffname, len);
10703 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010704 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010705 else if (buf[0] == NUL)
10706 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010707 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010708 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010709 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010710 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010711 else if (!mch_isFullName(buf))
10712 {
10713 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010714 len = (int)STRLEN(curdir);
10715 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010716 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010717 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010718 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010719 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010720 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010721 }
10722
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010723 if (ga_grow(gap, 1) == FAIL)
10724 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010725
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010726# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010727 /* Avoid the path ending in a backslash, it fails when a comma is
10728 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010729 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010730 if (buf[len - 1] == '\\')
10731 buf[len - 1] = '/';
10732# endif
10733
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010734 p = vim_strsave(buf);
10735 if (p == NULL)
10736 break;
10737 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010738 }
10739
10740 vim_free(buf);
10741}
10742
10743/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010744 * Returns a pointer to the file or directory name in "fname" that matches the
10745 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010746 *
10747 * path: /foo/bar/baz
10748 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010749 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010750 */
10751 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010752get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010753{
10754 int i;
10755 int maxlen = 0;
10756 char_u **path_part = (char_u **)gap->ga_data;
10757 char_u *cutoff = NULL;
10758
10759 for (i = 0; i < gap->ga_len; i++)
10760 {
10761 int j = 0;
10762
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010763 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010764# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010765 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10766#endif
10767 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010768 j++;
10769 if (j > maxlen)
10770 {
10771 maxlen = j;
10772 cutoff = &fname[j];
10773 }
10774 }
10775
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010776 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010777 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010778 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010779 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010780
10781 return cutoff;
10782}
10783
10784/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010785 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10786 * that they are unique with respect to each other while conserving the part
10787 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010788 */
10789 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010790uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010791{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010792 int i;
10793 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010794 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010795 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010796 char_u *pat;
10797 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010798 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010799 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010800 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010801 char_u **in_curdir = NULL;
10802 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010803
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010804 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010805 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010806
10807 /*
10808 * We need to prepend a '*' at the beginning of file_pattern so that the
10809 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010810 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010811 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010812 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010813 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010814 if (file_pattern == NULL)
10815 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010816 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010817 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010818 STRCAT(file_pattern, pattern);
10819 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10820 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010821 if (pat == NULL)
10822 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010823
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010824 regmatch.rm_ic = TRUE; /* always ignore case */
10825 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10826 vim_free(pat);
10827 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010828 return;
10829
Bram Moolenaar162bd912010-07-28 22:29:10 +020010830 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010831 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010832 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010833 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010834
10835 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010836 if (in_curdir == NULL)
10837 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010838
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010839 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010840 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010841 char_u *path = fnames[i];
10842 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010843 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010844 char_u *pathsep_p;
10845 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010846
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010847 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010848 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010849 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010850 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010851 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010852
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010853 /* Shorten the filename while maintaining its uniqueness */
10854 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010855
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010856 /* Don't assume all files can be reached without path when search
10857 * pattern starts with star star slash, so only remove path_cutoff
10858 * when possible. */
10859 if (pattern[0] == '*' && pattern[1] == '*'
10860 && vim_ispathsep_nocolon(pattern[2])
10861 && path_cutoff != NULL
10862 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10863 && is_unique(path_cutoff, gap, i))
10864 {
10865 sort_again = TRUE;
10866 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10867 }
10868 else
10869 {
10870 /* Here all files can be reached without path, so get shortest
10871 * unique path. We start at the end of the path. */
10872 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010873
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010874 while (find_previous_pathsep(path, &pathsep_p))
10875 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10876 && is_unique(pathsep_p + 1, gap, i)
10877 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10878 {
10879 sort_again = TRUE;
10880 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10881 break;
10882 }
10883 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010884
10885 if (mch_isFullName(path))
10886 {
10887 /*
10888 * Last resort: shorten relative to curdir if possible.
10889 * 'possible' means:
10890 * 1. It is under the current directory.
10891 * 2. The result is actually shorter than the original.
10892 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010893 * Before curdir After
10894 * /foo/bar/file.txt /foo/bar ./file.txt
10895 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10896 * /file.txt / /file.txt
10897 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010898 */
10899 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010900 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010901#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010902 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010903 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010904 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010905 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010906 && !vim_ispathsep(*short_name)
10907#endif
10908 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010909 {
10910 STRCPY(path, ".");
10911 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010912 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010913 }
10914 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010915 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010916 }
10917
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010918 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010919 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010920 {
10921 char_u *rel_path;
10922 char_u *path = in_curdir[i];
10923
10924 if (path == NULL)
10925 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010926
10927 /* If the {filename} is not unique, change it to ./{filename}.
10928 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010929 short_name = shorten_fname(path, curdir);
10930 if (short_name == NULL)
10931 short_name = path;
10932 if (is_unique(short_name, gap, i))
10933 {
10934 STRCPY(fnames[i], short_name);
10935 continue;
10936 }
10937
10938 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10939 if (rel_path == NULL)
10940 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010941 STRCPY(rel_path, ".");
10942 add_pathsep(rel_path);
10943 STRCAT(rel_path, short_name);
10944
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010945 vim_free(fnames[i]);
10946 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010947 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010948 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010949 }
10950
Bram Moolenaar162bd912010-07-28 22:29:10 +020010951theend:
10952 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010953 if (in_curdir != NULL)
10954 {
10955 for (i = 0; i < gap->ga_len; i++)
10956 vim_free(in_curdir[i]);
10957 vim_free(in_curdir);
10958 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010959 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010960 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010961
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010962 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010963 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010964}
10965
10966/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010967 * Calls globpath() with 'path' values for the given pattern and stores the
10968 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010969 * Returns the total number of matches.
10970 */
10971 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010972expand_in_path(
10973 garray_T *gap,
10974 char_u *pattern,
10975 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010976{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010977 char_u *curdir;
10978 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010979 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010980 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010981
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010982 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010983 return 0;
10984 mch_dirname(curdir, MAXPATHL);
10985
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010986 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010987 expand_path_option(curdir, &path_ga);
10988 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010989 if (path_ga.ga_len == 0)
10990 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010991
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010992 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010993 ga_clear_strings(&path_ga);
10994 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010995 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010996
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010997 if (flags & EW_ICASE)
10998 glob_flags |= WILD_ICASE;
10999 if (flags & EW_ADDSLASH)
11000 glob_flags |= WILD_ADD_SLASH;
11001 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011002 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011003
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011004 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011005}
11006#endif
11007
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011008#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11009/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011010 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11011 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011012 */
11013 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011014remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011015{
11016 int i;
11017 int j;
11018 char_u **fnames = (char_u **)gap->ga_data;
11019
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011020 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011021 for (i = gap->ga_len - 1; i > 0; --i)
11022 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11023 {
11024 vim_free(fnames[i]);
11025 for (j = i + 1; j < gap->ga_len; ++j)
11026 fnames[j - 1] = fnames[j];
11027 --gap->ga_len;
11028 }
11029}
11030#endif
11031
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010011032static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011033
11034/*
11035 * Return TRUE if "p" contains what looks like an environment variable.
11036 * Allowing for escaping.
11037 */
11038 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011039has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011040{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011041 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011042 {
11043 if (*p == '\\' && p[1] != NUL)
11044 ++p;
11045 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011046#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011047 "$%"
11048#else
11049 "$"
11050#endif
11051 , *p) != NULL)
11052 return TRUE;
11053 }
11054 return FALSE;
11055}
11056
11057#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010011058static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011059
11060/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011061 * Return TRUE if "p" contains a special wildcard character, one that Vim
11062 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011063 */
11064 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011065has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011066{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011067 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011068 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011069 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011070 if (*p == '\\' && p[1] != NUL)
11071 ++p;
11072 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11073 return TRUE;
11074 }
11075 return FALSE;
11076}
11077#endif
11078
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079/*
11080 * Generic wildcard expansion code.
11081 *
11082 * Characters in "pat" that should not be expanded must be preceded with a
11083 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11084 *
11085 * Return FAIL when no single file was found. In this case "num_file" is not
11086 * set, and "file" may contain an error message.
11087 * Return OK when some files found. "num_file" is set to the number of
11088 * matches, "file" to the array of matches. Call FreeWild() later.
11089 */
11090 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011091gen_expand_wildcards(
11092 int num_pat, /* number of input patterns */
11093 char_u **pat, /* array of input patterns */
11094 int *num_file, /* resulting number of files */
11095 char_u ***file, /* array of resulting files */
11096 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097{
11098 int i;
11099 garray_T ga;
11100 char_u *p;
11101 static int recursive = FALSE;
11102 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011103 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011104#if defined(FEAT_SEARCHPATH)
11105 int did_expand_in_path = FALSE;
11106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
11108 /*
11109 * expand_env() is called to expand things like "~user". If this fails,
11110 * it calls ExpandOne(), which brings us back here. In this case, always
11111 * call the machine specific expansion function, if possible. Otherwise,
11112 * return FAIL.
11113 */
11114 if (recursive)
11115#ifdef SPECIAL_WILDCHAR
11116 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11117#else
11118 return FAIL;
11119#endif
11120
11121#ifdef SPECIAL_WILDCHAR
11122 /*
11123 * If there are any special wildcard characters which we cannot handle
11124 * here, call machine specific function for all the expansion. This
11125 * avoids starting the shell for each argument separately.
11126 * For `=expr` do use the internal function.
11127 */
11128 for (i = 0; i < num_pat; i++)
11129 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011130 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131# ifdef VIM_BACKTICK
11132 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11133# endif
11134 )
11135 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11136 }
11137#endif
11138
11139 recursive = TRUE;
11140
11141 /*
11142 * The matching file names are stored in a growarray. Init it empty.
11143 */
11144 ga_init2(&ga, (int)sizeof(char_u *), 30);
11145
11146 for (i = 0; i < num_pat; ++i)
11147 {
11148 add_pat = -1;
11149 p = pat[i];
11150
11151#ifdef VIM_BACKTICK
11152 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011155 if (add_pat == -1)
11156 retval = FAIL;
11157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 else
11159#endif
11160 {
11161 /*
11162 * First expand environment variables, "~/" and "~user/".
11163 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011164 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011166 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 if (p == NULL)
11168 p = pat[i];
11169#ifdef UNIX
11170 /*
11171 * On Unix, if expand_env() can't expand an environment
11172 * variable, use the shell to do that. Discard previously
11173 * found file names and start all over again.
11174 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011175 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 {
11177 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011178 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011180 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 recursive = FALSE;
11182 return i;
11183 }
11184#endif
11185 }
11186
11187 /*
11188 * If there are wildcards: Expand file names and add each match to
11189 * the list. If there is no match, and EW_NOTFOUND is given, add
11190 * the pattern.
11191 * If there are no wildcards: Add the file name if it exists or
11192 * when EW_NOTFOUND is given.
11193 */
11194 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011195 {
11196#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011197 if ((flags & EW_PATH)
11198 && !mch_isFullName(p)
11199 && !(p[0] == '.'
11200 && (vim_ispathsep(p[1])
11201 || (p[1] == '.' && vim_ispathsep(p[2]))))
11202 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011203 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011204 /* :find completion where 'path' is used.
11205 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011206 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011207 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011208 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011209 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011210 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011211 else
11212#endif
11213 add_pat = mch_expandpath(&ga, p, flags);
11214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 }
11216
11217 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11218 {
11219 char_u *t = backslash_halve_save(p);
11220
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11222 * "vim c:/" work. */
11223 if (flags & EW_NOTFOUND)
11224 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011225 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 addfile(&ga, t, flags);
11227 vim_free(t);
11228 }
11229
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011230#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011231 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011232 uniquefy_paths(&ga, p);
11233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 if (p != pat[i])
11235 vim_free(p);
11236 }
11237
11238 *num_file = ga.ga_len;
11239 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11240
11241 recursive = FALSE;
11242
Bram Moolenaar336bd622016-01-17 18:23:58 +010011243 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244}
11245
11246# ifdef VIM_BACKTICK
11247
11248/*
11249 * Return TRUE if we can expand this backtick thing here.
11250 */
11251 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011252vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253{
11254 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11255}
11256
11257/*
11258 * Expand an item in `backticks` by executing it as a command.
11259 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011260 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 */
11262 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011263expand_backtick(
11264 garray_T *gap,
11265 char_u *pat,
11266 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267{
11268 char_u *p;
11269 char_u *cmd;
11270 char_u *buffer;
11271 int cnt = 0;
11272 int i;
11273
11274 /* Create the command: lop off the backticks. */
11275 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11276 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011277 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278
11279#ifdef FEAT_EVAL
11280 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011281 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 else
11283#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011284 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011285 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 vim_free(cmd);
11287 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011288 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289
11290 cmd = buffer;
11291 while (*cmd != NUL)
11292 {
11293 cmd = skipwhite(cmd); /* skip over white space */
11294 p = cmd;
11295 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11296 ++p;
11297 /* add an entry if it is not empty */
11298 if (p > cmd)
11299 {
11300 i = *p;
11301 *p = NUL;
11302 addfile(gap, cmd, flags);
11303 *p = i;
11304 ++cnt;
11305 }
11306 cmd = p;
11307 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11308 ++cmd;
11309 }
11310
11311 vim_free(buffer);
11312 return cnt;
11313}
11314# endif /* VIM_BACKTICK */
11315
11316/*
11317 * Add a file to a file list. Accepted flags:
11318 * EW_DIR add directories
11319 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011320 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 * EW_NOTFOUND add even when it doesn't exist
11322 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011323 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 */
11325 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011326addfile(
11327 garray_T *gap,
11328 char_u *f, /* filename */
11329 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330{
11331 char_u *p;
11332 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011333 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011335 /* if the file/dir/link doesn't exist, may not add it */
11336 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011337 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 return;
11339
11340#ifdef FNAME_ILLEGAL
11341 /* if the file/dir contains illegal characters, don't add it */
11342 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11343 return;
11344#endif
11345
11346 isdir = mch_isdir(f);
11347 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11348 return;
11349
Bram Moolenaarb5971142015-03-21 17:32:19 +010011350 /* If the file isn't executable, may not add it. Do accept directories.
11351 * When invoked from expand_shellcmd() do not use $PATH. */
11352 if (!isdir && (flags & EW_EXEC)
11353 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011354 return;
11355
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 /* Make room for another item in the file list. */
11357 if (ga_grow(gap, 1) == FAIL)
11358 return;
11359
11360 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11361 if (p == NULL)
11362 return;
11363
11364 STRCPY(p, f);
11365#ifdef BACKSLASH_IN_FILENAME
11366 slash_adjust(p);
11367#endif
11368 /*
11369 * Append a slash or backslash after directory names if none is present.
11370 */
11371#ifndef DONT_ADD_PATHSEP_TO_DIR
11372 if (isdir && (flags & EW_ADDSLASH))
11373 add_pathsep(p);
11374#endif
11375 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376}
11377#endif /* !NO_EXPANDPATH */
11378
11379#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11380
11381#ifndef SEEK_SET
11382# define SEEK_SET 0
11383#endif
11384#ifndef SEEK_END
11385# define SEEK_END 2
11386#endif
11387
11388/*
11389 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011390 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11391 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 * Returns an allocated string, or NULL for error.
11393 */
11394 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011395get_cmd_output(
11396 char_u *cmd,
11397 char_u *infile, /* optional input file name */
11398 int flags, /* can be SHELL_SILENT */
11399 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400{
11401 char_u *tempname;
11402 char_u *command;
11403 char_u *buffer = NULL;
11404 int len;
11405 int i = 0;
11406 FILE *fd;
11407
11408 if (check_restricted() || check_secure())
11409 return NULL;
11410
11411 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011412 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 {
11414 EMSG(_(e_notmp));
11415 return NULL;
11416 }
11417
11418 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011419 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 if (command == NULL)
11421 goto done;
11422
11423 /*
11424 * Call the shell to execute the command (errors are ignored).
11425 * Don't check timestamps here.
11426 */
11427 ++no_check_timestamps;
11428 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11429 --no_check_timestamps;
11430
11431 vim_free(command);
11432
11433 /*
11434 * read the names from the file into memory
11435 */
11436# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011437 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 fd = mch_fopen((char *)tempname, "r");
11439# else
11440 fd = mch_fopen((char *)tempname, READBIN);
11441# endif
11442
11443 if (fd == NULL)
11444 {
11445 EMSG2(_(e_notopen), tempname);
11446 goto done;
11447 }
11448
11449 fseek(fd, 0L, SEEK_END);
11450 len = ftell(fd); /* get size of temp file */
11451 fseek(fd, 0L, SEEK_SET);
11452
11453 buffer = alloc(len + 1);
11454 if (buffer != NULL)
11455 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11456 fclose(fd);
11457 mch_remove(tempname);
11458 if (buffer == NULL)
11459 goto done;
11460#ifdef VMS
11461 len = i; /* VMS doesn't give us what we asked for... */
11462#endif
11463 if (i != len)
11464 {
11465 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011466 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011468 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011469 {
11470 /* Change NUL into SOH, otherwise the string is truncated. */
11471 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011472 if (buffer[i] == NUL)
11473 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011474
Bram Moolenaar162bd912010-07-28 22:29:10 +020011475 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011476 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011477 else
11478 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479
11480done:
11481 vim_free(tempname);
11482 return buffer;
11483}
11484#endif
11485
11486/*
11487 * Free the list of files returned by expand_wildcards() or other expansion
11488 * functions.
11489 */
11490 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011491FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011493 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 while (count--)
11496 vim_free(files[count]);
11497 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498}
11499
11500/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011501 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 * Don't do this when still processing a command or a mapping.
11503 * Don't do this when inside a ":normal" command.
11504 */
11505 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011506goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507{
11508 return (p_im && stuff_empty() && typebuf_typed());
11509}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011510
11511/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011512 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011513 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11514 * - Remove any argument. E.g., "csh -f" -> "csh".
11515 * But don't allow a space in the path, so that this works:
11516 * "/usr/bin/csh --rcfile ~/.cshrc"
11517 * But don't do that for Windows, it's common to have a space in the path.
11518 */
11519 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011520get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011521{
11522 char_u *p;
11523
11524#ifdef WIN3264
11525 p = gettail(p_sh);
11526 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11527#else
11528 p = skiptowhite(p_sh);
11529 if (*p == NUL)
11530 {
11531 /* No white space, use the tail. */
11532 p = vim_strsave(gettail(p_sh));
11533 }
11534 else
11535 {
11536 char_u *p1, *p2;
11537
11538 /* Find the last path separator before the space. */
11539 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011540 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011541 if (vim_ispathsep(*p2))
11542 p1 = p2 + 1;
11543 p = vim_strnsave(p1, (int)(p - p1));
11544 }
11545#endif
11546 return p;
11547}