blob: bd1fe255b1d4528928b2f4ae3fcb06e27cd5fd71 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar071d4272004-06-13 20:20:40 +000017static char_u *vim_version_dir __ARGS((char_u *vimdir));
18static char_u *remove_tail __ARGS((char_u *p, char_u *pend, char_u *name));
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar01b626c2013-06-16 22:49:14 +020020static void init_users __ARGS((void));
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022static int copy_indent __ARGS((int size, char_u *src));
23
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
33get_indent()
34{
Bram Moolenaar597a4222014-06-25 14:39:50 +020035 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Count the size (in window cells) of the indent in line "lnum".
40 */
41 int
42get_indent_lnum(lnum)
43 linenr_T lnum;
44{
Bram Moolenaar597a4222014-06-25 14:39:50 +020045 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046}
47
48#if defined(FEAT_FOLDING) || defined(PROTO)
49/*
50 * Count the size (in window cells) of the indent in line "lnum" of buffer
51 * "buf".
52 */
53 int
54get_indent_buf(buf, lnum)
55 buf_T *buf;
56 linenr_T lnum;
57{
Bram Moolenaar597a4222014-06-25 14:39:50 +020058 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059}
60#endif
61
62/*
63 * count the size (in window cells) of the indent in line "ptr", with
64 * 'tabstop' at "ts"
65 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000066 int
Bram Moolenaar597a4222014-06-25 14:39:50 +020067get_indent_str(ptr, ts, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 char_u *ptr;
69 int ts;
Bram Moolenaar597a4222014-06-25 14:39:50 +020070 int list; /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
72 int count = 0;
73
74 for ( ; *ptr; ++ptr)
75 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020076 if (*ptr == TAB)
77 {
78 if (!list || lcs_tab1) /* count a tab for what it is worth */
79 count += ts - (count % ts);
80 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020081 /* In list mode, when tab is not set, count screen char width
82 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020083 count += ptr2cells(ptr);
84 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 else if (*ptr == ' ')
86 ++count; /* count a space for one */
87 else
88 break;
89 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000090 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000091}
92
93/*
94 * Set the indent of the current line.
95 * Leaves the cursor on the first non-blank in the line.
96 * Caller must take care of undo.
97 * "flags":
98 * SIN_CHANGED: call changed_bytes() if the line was changed.
99 * SIN_INSERT: insert the indent in front of the line.
100 * SIN_UNDO: save line for undo before changing it.
101 * Returns TRUE if the line was changed.
102 */
103 int
104set_indent(size, flags)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000105 int size; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 int flags;
107{
108 char_u *p;
109 char_u *newline;
110 char_u *oldline;
111 char_u *s;
112 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int line_len;
115 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000116 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000118 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000119 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000120 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122 /*
123 * First check if there is anything to do and compute the number of
124 * characters needed for the indent.
125 */
126 todo = size;
127 ind_len = 0;
128 p = oldline = ml_get_curline();
129
130 /* Calculate the buffer size for the new indent, and check to see if it
131 * isn't already set */
132
Bram Moolenaar5002c292007-07-24 13:26:15 +0000133 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
134 * 'preserveindent' are set count the number of characters at the
135 * beginning of the line to be copied */
136 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {
138 /* If 'preserveindent' is set then reuse as much as possible of
139 * the existing indent structure for the new indent */
140 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
141 {
142 ind_done = 0;
143
144 /* count as many characters as we can use */
145 while (todo > 0 && vim_iswhite(*p))
146 {
147 if (*p == TAB)
148 {
149 tab_pad = (int)curbuf->b_p_ts
150 - (ind_done % (int)curbuf->b_p_ts);
151 /* stop if this tab will overshoot the target */
152 if (todo < tab_pad)
153 break;
154 todo -= tab_pad;
155 ++ind_len;
156 ind_done += tab_pad;
157 }
158 else
159 {
160 --todo;
161 ++ind_len;
162 ++ind_done;
163 }
164 ++p;
165 }
166
Bram Moolenaar5002c292007-07-24 13:26:15 +0000167 /* Set initial number of whitespace chars to copy if we are
168 * preserving indent but expandtab is set */
169 if (curbuf->b_p_et)
170 orig_char_len = ind_len;
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 /* Fill to next tabstop with a tab, if possible */
173 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000174 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 {
176 doit = TRUE;
177 todo -= tab_pad;
178 ++ind_len;
179 /* ind_done += tab_pad; */
180 }
181 }
182
183 /* count tabs required for indent */
184 while (todo >= (int)curbuf->b_p_ts)
185 {
186 if (*p != TAB)
187 doit = TRUE;
188 else
189 ++p;
190 todo -= (int)curbuf->b_p_ts;
191 ++ind_len;
192 /* ind_done += (int)curbuf->b_p_ts; */
193 }
194 }
195 /* count spaces required for indent */
196 while (todo > 0)
197 {
198 if (*p != ' ')
199 doit = TRUE;
200 else
201 ++p;
202 --todo;
203 ++ind_len;
204 /* ++ind_done; */
205 }
206
207 /* Return if the indent is OK already. */
208 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
209 return FALSE;
210
211 /* Allocate memory for the new line. */
212 if (flags & SIN_INSERT)
213 p = oldline;
214 else
215 p = skipwhite(p);
216 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000217
218 /* If 'preserveindent' and 'expandtab' are both set keep the original
219 * characters and allocate accordingly. We will fill the rest with spaces
220 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000221 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000222 {
223 newline = alloc(orig_char_len + size - ind_done + line_len);
224 if (newline == NULL)
225 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000226 todo = size - ind_done;
227 ind_len = orig_char_len + todo; /* Set total length of indent in
228 * characters, which may have been
229 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000230 p = oldline;
231 s = newline;
232 while (orig_char_len > 0)
233 {
234 *s++ = *p++;
235 orig_char_len--;
236 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000237
Bram Moolenaar5002c292007-07-24 13:26:15 +0000238 /* Skip over any additional white space (useful when newindent is less
239 * than old) */
240 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000241 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000242
Bram Moolenaar5002c292007-07-24 13:26:15 +0000243 }
244 else
245 {
246 todo = size;
247 newline = alloc(ind_len + line_len);
248 if (newline == NULL)
249 return FALSE;
250 s = newline;
251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
253 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 /* if 'expandtab' isn't set: use TABs */
255 if (!curbuf->b_p_et)
256 {
257 /* If 'preserveindent' is set then reuse as much as possible of
258 * the existing indent structure for the new indent */
259 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
260 {
261 p = oldline;
262 ind_done = 0;
263
264 while (todo > 0 && vim_iswhite(*p))
265 {
266 if (*p == TAB)
267 {
268 tab_pad = (int)curbuf->b_p_ts
269 - (ind_done % (int)curbuf->b_p_ts);
270 /* stop if this tab will overshoot the target */
271 if (todo < tab_pad)
272 break;
273 todo -= tab_pad;
274 ind_done += tab_pad;
275 }
276 else
277 {
278 --todo;
279 ++ind_done;
280 }
281 *s++ = *p++;
282 }
283
284 /* Fill to next tabstop with a tab, if possible */
285 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
286 if (todo >= tab_pad)
287 {
288 *s++ = TAB;
289 todo -= tab_pad;
290 }
291
292 p = skipwhite(p);
293 }
294
295 while (todo >= (int)curbuf->b_p_ts)
296 {
297 *s++ = TAB;
298 todo -= (int)curbuf->b_p_ts;
299 }
300 }
301 while (todo > 0)
302 {
303 *s++ = ' ';
304 --todo;
305 }
306 mch_memmove(s, p, (size_t)line_len);
307
308 /* Replace the line (unless undo fails). */
309 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
310 {
311 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
312 if (flags & SIN_CHANGED)
313 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200314 /* Correct saved cursor position if it is in this line. */
315 if (saved_cursor.lnum == curwin->w_cursor.lnum)
316 {
317 if (saved_cursor.col >= (colnr_T)(p - oldline))
318 /* cursor was after the indent, adjust for the number of
319 * bytes added/removed */
320 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
321 else if (saved_cursor.col >= (colnr_T)(s - newline))
322 /* cursor was in the indent, and is now after it, put it back
323 * at the start of the indent (replacing spaces with TAB) */
324 saved_cursor.col = (colnr_T)(s - newline);
325 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000326 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 }
328 else
329 vim_free(newline);
330
331 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000332 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333}
334
335/*
336 * Copy the indent from ptr to the current line (and fill to size)
337 * Leaves the cursor on the first non-blank in the line.
338 * Returns TRUE if the line was changed.
339 */
340 static int
341copy_indent(size, src)
342 int size;
343 char_u *src;
344{
345 char_u *p = NULL;
346 char_u *line = NULL;
347 char_u *s;
348 int todo;
349 int ind_len;
350 int line_len = 0;
351 int tab_pad;
352 int ind_done;
353 int round;
354
355 /* Round 1: compute the number of characters needed for the indent
356 * Round 2: copy the characters. */
357 for (round = 1; round <= 2; ++round)
358 {
359 todo = size;
360 ind_len = 0;
361 ind_done = 0;
362 s = src;
363
364 /* Count/copy the usable portion of the source line */
365 while (todo > 0 && vim_iswhite(*s))
366 {
367 if (*s == TAB)
368 {
369 tab_pad = (int)curbuf->b_p_ts
370 - (ind_done % (int)curbuf->b_p_ts);
371 /* Stop if this tab will overshoot the target */
372 if (todo < tab_pad)
373 break;
374 todo -= tab_pad;
375 ind_done += tab_pad;
376 }
377 else
378 {
379 --todo;
380 ++ind_done;
381 }
382 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000383 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 *p++ = *s;
385 ++s;
386 }
387
388 /* Fill to next tabstop with a tab, if possible */
389 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200390 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {
392 todo -= tab_pad;
393 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000394 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 *p++ = TAB;
396 }
397
398 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200399 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {
401 todo -= (int)curbuf->b_p_ts;
402 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000403 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 *p++ = TAB;
405 }
406
407 /* Count/add spaces required for indent */
408 while (todo > 0)
409 {
410 --todo;
411 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000412 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 *p++ = ' ';
414 }
415
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000416 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {
418 /* Allocate memory for the result: the copied indent, new indent
419 * and the rest of the line. */
420 line_len = (int)STRLEN(ml_get_curline()) + 1;
421 line = alloc(ind_len + line_len);
422 if (line == NULL)
423 return FALSE;
424 p = line;
425 }
426 }
427
428 /* Append the original line */
429 mch_memmove(p, ml_get_curline(), (size_t)line_len);
430
431 /* Replace the line */
432 ml_replace(curwin->w_cursor.lnum, line, FALSE);
433
434 /* Put the cursor after the indent. */
435 curwin->w_cursor.col = ind_len;
436 return TRUE;
437}
438
439/*
440 * Return the indent of the current line after a number. Return -1 if no
441 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000442 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 */
444 int
445get_number_indent(lnum)
446 linenr_T lnum;
447{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 colnr_T col;
449 pos_T pos;
450
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200451 regmatch_T regmatch;
452 int lead_len = 0; /* length of comment leader */
453
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (lnum > curbuf->b_ml.ml_line_count)
455 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200457
458#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200459 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
460 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000462#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200463 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
464 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200467
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200468 /* vim_regexec() expects a pointer to a line. This lets us
469 * start matching for the flp beyond any comment leader... */
470 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200472 pos.lnum = lnum;
473 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200474#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200475 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200476#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200477 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200478 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200479 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000480
481 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 getvcol(curwin, &pos, &col, NULL, NULL);
484 return (int)col;
485}
486
Bram Moolenaar597a4222014-06-25 14:39:50 +0200487#if defined(FEAT_LINEBREAK) || defined(PROTO)
488/*
489 * Return appropriate space number for breakindent, taking influencing
490 * parameters into account. Window must be specified, since it is not
491 * necessarily always the current one.
492 */
493 int
494get_breakindent_win(wp, line)
495 win_T *wp;
496 char_u *line; /* start of the line */
497{
498 static int prev_indent = 0; /* cached indent value */
499 static long prev_ts = 0L; /* cached tabstop value */
500 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200501 static int prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200502 int bri = 0;
503 /* window width minus window margin space, i.e. what rests for text */
504 const int eff_wwidth = W_WIDTH(wp)
505 - ((wp->w_p_nu || wp->w_p_rnu)
506 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
507 ? number_width(wp) + 1 : 0);
508
509 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200510 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
511 || prev_tick != wp->w_buffer->b_changedtick)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 {
513 prev_line = line;
514 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaara40aa762014-06-25 22:55:38 +0200515 prev_tick = wp->w_buffer->b_changedtick;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200517 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200518 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200519 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200520
521 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200522 if (wp->w_p_brisbr)
523 bri -= vim_strsize(p_sbr);
524
525 /* Add offset for number column, if 'n' is in 'cpoptions' */
526 bri += win_col_off2(wp);
527
528 /* never indent past left window margin */
529 if (bri < 0)
530 bri = 0;
531 /* always leave at least bri_min characters on the left,
532 * if text width is sufficient */
533 else if (bri > eff_wwidth - wp->w_p_brimin)
534 bri = (eff_wwidth - wp->w_p_brimin < 0)
535 ? 0 : eff_wwidth - wp->w_p_brimin;
536
537 return bri;
538}
539#endif
540
541
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
543
544static int cin_is_cinword __ARGS((char_u *line));
545
546/*
547 * Return TRUE if the string "line" starts with a word from 'cinwords'.
548 */
549 static int
550cin_is_cinword(line)
551 char_u *line;
552{
553 char_u *cinw;
554 char_u *cinw_buf;
555 int cinw_len;
556 int retval = FALSE;
557 int len;
558
559 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
560 cinw_buf = alloc((unsigned)cinw_len);
561 if (cinw_buf != NULL)
562 {
563 line = skipwhite(line);
564 for (cinw = curbuf->b_p_cinw; *cinw; )
565 {
566 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
567 if (STRNCMP(line, cinw_buf, len) == 0
568 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
569 {
570 retval = TRUE;
571 break;
572 }
573 }
574 vim_free(cinw_buf);
575 }
576 return retval;
577}
578#endif
579
580/*
581 * open_line: Add a new line below or above the current line.
582 *
583 * For VREPLACE mode, we only add a new line when we get to the end of the
584 * file, otherwise we just start replacing the next line.
585 *
586 * Caller must take care of undo. Since VREPLACE may affect any number of
587 * lines however, it may call u_save_cursor() again when starting to change a
588 * new line.
589 * "flags": OPENLINE_DELSPACES delete spaces after cursor
590 * OPENLINE_DO_COM format comments
591 * OPENLINE_KEEPTRAIL keep trailing spaces
592 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200593 * OPENLINE_COM_LIST format comments with list or 2nd line indent
594 *
595 * "second_line_indent": indent for after ^^D in Insert mode or if flag
596 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 *
598 * Return TRUE for success, FALSE for failure
599 */
600 int
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200601open_line(dir, flags, second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 int dir; /* FORWARD or BACKWARD */
603 int flags;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200604 int second_line_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 char_u *saved_line; /* copy of the original line */
607 char_u *next_line = NULL; /* copy of the next line */
608 char_u *p_extra = NULL; /* what goes to next line */
609 int less_cols = 0; /* less columns for mark in new line */
610 int less_cols_off = 0; /* columns to skip for mark adjust */
611 pos_T old_cursor; /* old cursor position */
612 int newcol = 0; /* new cursor column */
613 int newindent = 0; /* auto-indent of the new line */
614 int n;
615 int trunc_line = FALSE; /* truncate current line afterwards */
616 int retval = FALSE; /* return value, default is FAIL */
617#ifdef FEAT_COMMENTS
618 int extra_len = 0; /* length of p_extra string */
619 int lead_len; /* length of comment leader */
620 char_u *lead_flags; /* position in 'comments' for comment leader */
621 char_u *leader = NULL; /* copy of comment leader */
622#endif
623 char_u *allocated = NULL; /* allocated memory */
624#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
625 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
626 char_u *p;
627#endif
628 int saved_char = NUL; /* init for GCC */
629#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
630 pos_T *pos;
631#endif
632#ifdef FEAT_SMARTINDENT
633 int do_si = (!p_paste && curbuf->b_p_si
634# ifdef FEAT_CINDENT
635 && !curbuf->b_p_cin
636# endif
637 );
638 int no_si = FALSE; /* reset did_si afterwards */
639 int first_char = NUL; /* init for GCC */
640#endif
641#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
642 int vreplace_mode;
643#endif
644 int did_append; /* appended a new line */
645 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
646
647 /*
648 * make a copy of the current line so we can mess with it
649 */
650 saved_line = vim_strsave(ml_get_curline());
651 if (saved_line == NULL) /* out of memory! */
652 return FALSE;
653
654#ifdef FEAT_VREPLACE
655 if (State & VREPLACE_FLAG)
656 {
657 /*
658 * With VREPLACE we make a copy of the next line, which we will be
659 * starting to replace. First make the new line empty and let vim play
660 * with the indenting and comment leader to its heart's content. Then
661 * we grab what it ended up putting on the new line, put back the
662 * original line, and call ins_char() to put each new character onto
663 * the line, replacing what was there before and pushing the right
664 * stuff onto the replace stack. -- webb.
665 */
666 if (curwin->w_cursor.lnum < orig_line_count)
667 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
668 else
669 next_line = vim_strsave((char_u *)"");
670 if (next_line == NULL) /* out of memory! */
671 goto theend;
672
673 /*
674 * In VREPLACE mode, a NL replaces the rest of the line, and starts
675 * replacing the next line, so push all of the characters left on the
676 * line onto the replace stack. We'll push any other characters that
677 * might be replaced at the start of the next line (due to autoindent
678 * etc) a bit later.
679 */
680 replace_push(NUL); /* Call twice because BS over NL expects it */
681 replace_push(NUL);
682 p = saved_line + curwin->w_cursor.col;
683 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000684 {
685#ifdef FEAT_MBYTE
686 if (has_mbyte)
687 p += replace_push_mb(p);
688 else
689#endif
690 replace_push(*p++);
691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 saved_line[curwin->w_cursor.col] = NUL;
693 }
694#endif
695
696 if ((State & INSERT)
697#ifdef FEAT_VREPLACE
698 && !(State & VREPLACE_FLAG)
699#endif
700 )
701 {
702 p_extra = saved_line + curwin->w_cursor.col;
703#ifdef FEAT_SMARTINDENT
704 if (do_si) /* need first char after new line break */
705 {
706 p = skipwhite(p_extra);
707 first_char = *p;
708 }
709#endif
710#ifdef FEAT_COMMENTS
711 extra_len = (int)STRLEN(p_extra);
712#endif
713 saved_char = *p_extra;
714 *p_extra = NUL;
715 }
716
717 u_clearline(); /* cannot do "U" command when adding lines */
718#ifdef FEAT_SMARTINDENT
719 did_si = FALSE;
720#endif
721 ai_col = 0;
722
723 /*
724 * If we just did an auto-indent, then we didn't type anything on
725 * the prior line, and it should be truncated. Do this even if 'ai' is not
726 * set because automatically inserting a comment leader also sets did_ai.
727 */
728 if (dir == FORWARD && did_ai)
729 trunc_line = TRUE;
730
731 /*
732 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
733 * indent to use for the new line.
734 */
735 if (curbuf->b_p_ai
736#ifdef FEAT_SMARTINDENT
737 || do_si
738#endif
739 )
740 {
741 /*
742 * count white space on current line
743 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200744 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200745 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
746 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
748#ifdef FEAT_SMARTINDENT
749 /*
750 * Do smart indenting.
751 * In insert/replace mode (only when dir == FORWARD)
752 * we may move some text to the next line. If it starts with '{'
753 * don't add an indent. Fixes inserting a NL before '{' in line
754 * "if (condition) {"
755 */
756 if (!trunc_line && do_si && *saved_line != NUL
757 && (p_extra == NULL || first_char != '{'))
758 {
759 char_u *ptr;
760 char_u last_char;
761
762 old_cursor = curwin->w_cursor;
763 ptr = saved_line;
764# ifdef FEAT_COMMENTS
765 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200766 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 else
768 lead_len = 0;
769# endif
770 if (dir == FORWARD)
771 {
772 /*
773 * Skip preprocessor directives, unless they are
774 * recognised as comments.
775 */
776 if (
777# ifdef FEAT_COMMENTS
778 lead_len == 0 &&
779# endif
780 ptr[0] == '#')
781 {
782 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
783 ptr = ml_get(--curwin->w_cursor.lnum);
784 newindent = get_indent();
785 }
786# ifdef FEAT_COMMENTS
787 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200788 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 else
790 lead_len = 0;
791 if (lead_len > 0)
792 {
793 /*
794 * This case gets the following right:
795 * \*
796 * * A comment (read '\' as '/').
797 * *\
798 * #define IN_THE_WAY
799 * This should line up here;
800 */
801 p = skipwhite(ptr);
802 if (p[0] == '/' && p[1] == '*')
803 p++;
804 if (p[0] == '*')
805 {
806 for (p++; *p; p++)
807 {
808 if (p[0] == '/' && p[-1] == '*')
809 {
810 /*
811 * End of C comment, indent should line up
812 * with the line containing the start of
813 * the comment
814 */
815 curwin->w_cursor.col = (colnr_T)(p - ptr);
816 if ((pos = findmatch(NULL, NUL)) != NULL)
817 {
818 curwin->w_cursor.lnum = pos->lnum;
819 newindent = get_indent();
820 }
821 }
822 }
823 }
824 }
825 else /* Not a comment line */
826# endif
827 {
828 /* Find last non-blank in line */
829 p = ptr + STRLEN(ptr) - 1;
830 while (p > ptr && vim_iswhite(*p))
831 --p;
832 last_char = *p;
833
834 /*
835 * find the character just before the '{' or ';'
836 */
837 if (last_char == '{' || last_char == ';')
838 {
839 if (p > ptr)
840 --p;
841 while (p > ptr && vim_iswhite(*p))
842 --p;
843 }
844 /*
845 * Try to catch lines that are split over multiple
846 * lines. eg:
847 * if (condition &&
848 * condition) {
849 * Should line up here!
850 * }
851 */
852 if (*p == ')')
853 {
854 curwin->w_cursor.col = (colnr_T)(p - ptr);
855 if ((pos = findmatch(NULL, '(')) != NULL)
856 {
857 curwin->w_cursor.lnum = pos->lnum;
858 newindent = get_indent();
859 ptr = ml_get_curline();
860 }
861 }
862 /*
863 * If last character is '{' do indent, without
864 * checking for "if" and the like.
865 */
866 if (last_char == '{')
867 {
868 did_si = TRUE; /* do indent */
869 no_si = TRUE; /* don't delete it when '{' typed */
870 }
871 /*
872 * Look for "if" and the like, use 'cinwords'.
873 * Don't do this if the previous line ended in ';' or
874 * '}'.
875 */
876 else if (last_char != ';' && last_char != '}'
877 && cin_is_cinword(ptr))
878 did_si = TRUE;
879 }
880 }
881 else /* dir == BACKWARD */
882 {
883 /*
884 * Skip preprocessor directives, unless they are
885 * recognised as comments.
886 */
887 if (
888# ifdef FEAT_COMMENTS
889 lead_len == 0 &&
890# endif
891 ptr[0] == '#')
892 {
893 int was_backslashed = FALSE;
894
895 while ((ptr[0] == '#' || was_backslashed) &&
896 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
897 {
898 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
899 was_backslashed = TRUE;
900 else
901 was_backslashed = FALSE;
902 ptr = ml_get(++curwin->w_cursor.lnum);
903 }
904 if (was_backslashed)
905 newindent = 0; /* Got to end of file */
906 else
907 newindent = get_indent();
908 }
909 p = skipwhite(ptr);
910 if (*p == '}') /* if line starts with '}': do indent */
911 did_si = TRUE;
912 else /* can delete indent when '{' typed */
913 can_si_back = TRUE;
914 }
915 curwin->w_cursor = old_cursor;
916 }
917 if (do_si)
918 can_si = TRUE;
919#endif /* FEAT_SMARTINDENT */
920
921 did_ai = TRUE;
922 }
923
924#ifdef FEAT_COMMENTS
925 /*
926 * Find out if the current line starts with a comment leader.
927 * This may then be inserted in front of the new line.
928 */
929 end_comment_pending = NUL;
930 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200931 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 else
933 lead_len = 0;
934 if (lead_len > 0)
935 {
936 char_u *lead_repl = NULL; /* replaces comment leader */
937 int lead_repl_len = 0; /* length of *lead_repl */
938 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
939 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
940 char_u *comment_end = NULL; /* where lead_end has been found */
941 int extra_space = FALSE; /* append extra space */
942 int current_flag;
943 int require_blank = FALSE; /* requires blank after middle */
944 char_u *p2;
945
946 /*
947 * If the comment leader has the start, middle or end flag, it may not
948 * be used or may be replaced with the middle leader.
949 */
950 for (p = lead_flags; *p && *p != ':'; ++p)
951 {
952 if (*p == COM_BLANK)
953 {
954 require_blank = TRUE;
955 continue;
956 }
957 if (*p == COM_START || *p == COM_MIDDLE)
958 {
959 current_flag = *p;
960 if (*p == COM_START)
961 {
962 /*
963 * Doing "O" on a start of comment does not insert leader.
964 */
965 if (dir == BACKWARD)
966 {
967 lead_len = 0;
968 break;
969 }
970
971 /* find start of middle part */
972 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
973 require_blank = FALSE;
974 }
975
976 /*
977 * Isolate the strings of the middle and end leader.
978 */
979 while (*p && p[-1] != ':') /* find end of middle flags */
980 {
981 if (*p == COM_BLANK)
982 require_blank = TRUE;
983 ++p;
984 }
985 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
986
987 while (*p && p[-1] != ':') /* find end of end flags */
988 {
989 /* Check whether we allow automatic ending of comments */
990 if (*p == COM_AUTO_END)
991 end_comment_pending = -1; /* means we want to set it */
992 ++p;
993 }
994 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
995
996 if (end_comment_pending == -1) /* we can set it now */
997 end_comment_pending = lead_end[n - 1];
998
999 /*
1000 * If the end of the comment is in the same line, don't use
1001 * the comment leader.
1002 */
1003 if (dir == FORWARD)
1004 {
1005 for (p = saved_line + lead_len; *p; ++p)
1006 if (STRNCMP(p, lead_end, n) == 0)
1007 {
1008 comment_end = p;
1009 lead_len = 0;
1010 break;
1011 }
1012 }
1013
1014 /*
1015 * Doing "o" on a start of comment inserts the middle leader.
1016 */
1017 if (lead_len > 0)
1018 {
1019 if (current_flag == COM_START)
1020 {
1021 lead_repl = lead_middle;
1022 lead_repl_len = (int)STRLEN(lead_middle);
1023 }
1024
1025 /*
1026 * If we have hit RETURN immediately after the start
1027 * comment leader, then put a space after the middle
1028 * comment leader on the next line.
1029 */
1030 if (!vim_iswhite(saved_line[lead_len - 1])
1031 && ((p_extra != NULL
1032 && (int)curwin->w_cursor.col == lead_len)
1033 || (p_extra == NULL
1034 && saved_line[lead_len] == NUL)
1035 || require_blank))
1036 extra_space = TRUE;
1037 }
1038 break;
1039 }
1040 if (*p == COM_END)
1041 {
1042 /*
1043 * Doing "o" on the end of a comment does not insert leader.
1044 * Remember where the end is, might want to use it to find the
1045 * start (for C-comments).
1046 */
1047 if (dir == FORWARD)
1048 {
1049 comment_end = skipwhite(saved_line);
1050 lead_len = 0;
1051 break;
1052 }
1053
1054 /*
1055 * Doing "O" on the end of a comment inserts the middle leader.
1056 * Find the string for the middle leader, searching backwards.
1057 */
1058 while (p > curbuf->b_p_com && *p != ',')
1059 --p;
1060 for (lead_repl = p; lead_repl > curbuf->b_p_com
1061 && lead_repl[-1] != ':'; --lead_repl)
1062 ;
1063 lead_repl_len = (int)(p - lead_repl);
1064
1065 /* We can probably always add an extra space when doing "O" on
1066 * the comment-end */
1067 extra_space = TRUE;
1068
1069 /* Check whether we allow automatic ending of comments */
1070 for (p2 = p; *p2 && *p2 != ':'; p2++)
1071 {
1072 if (*p2 == COM_AUTO_END)
1073 end_comment_pending = -1; /* means we want to set it */
1074 }
1075 if (end_comment_pending == -1)
1076 {
1077 /* Find last character in end-comment string */
1078 while (*p2 && *p2 != ',')
1079 p2++;
1080 end_comment_pending = p2[-1];
1081 }
1082 break;
1083 }
1084 if (*p == COM_FIRST)
1085 {
1086 /*
1087 * Comment leader for first line only: Don't repeat leader
1088 * when using "O", blank out leader when using "o".
1089 */
1090 if (dir == BACKWARD)
1091 lead_len = 0;
1092 else
1093 {
1094 lead_repl = (char_u *)"";
1095 lead_repl_len = 0;
1096 }
1097 break;
1098 }
1099 }
1100 if (lead_len)
1101 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001102 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001103 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001104 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 allocated = leader; /* remember to free it later */
1106
1107 if (leader == NULL)
1108 lead_len = 0;
1109 else
1110 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001111 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
1113 /*
1114 * Replace leader with lead_repl, right or left adjusted
1115 */
1116 if (lead_repl != NULL)
1117 {
1118 int c = 0;
1119 int off = 0;
1120
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001121 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001124 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 else if (VIM_ISDIGIT(*p) || *p == '-')
1126 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001127 else
1128 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 if (c == COM_RIGHT) /* right adjusted leader */
1131 {
1132 /* find last non-white in the leader to line up with */
1133 for (p = leader + lead_len - 1; p > leader
1134 && vim_iswhite(*p); --p)
1135 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001137
1138#ifdef FEAT_MBYTE
1139 /* Compute the length of the replaced characters in
1140 * screen characters, not bytes. */
1141 {
1142 int repl_size = vim_strnsize(lead_repl,
1143 lead_repl_len);
1144 int old_size = 0;
1145 char_u *endp = p;
1146 int l;
1147
1148 while (old_size < repl_size && p > leader)
1149 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001150 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001151 old_size += ptr2cells(p);
1152 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001153 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001154 if (l != 0)
1155 mch_memmove(endp + l, endp,
1156 (size_t)((leader + lead_len) - endp));
1157 lead_len += l;
1158 }
1159#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (p < leader + lead_repl_len)
1161 p = leader;
1162 else
1163 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1166 if (p + lead_repl_len > leader + lead_len)
1167 p[lead_repl_len] = NUL;
1168
1169 /* blank-out any other chars from the old leader. */
1170 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001171 {
1172#ifdef FEAT_MBYTE
1173 int l = mb_head_off(leader, p);
1174
1175 if (l > 1)
1176 {
1177 p -= l;
1178 if (ptr2cells(p) > 1)
1179 {
1180 p[1] = ' ';
1181 --l;
1182 }
1183 mch_memmove(p + 1, p + l + 1,
1184 (size_t)((leader + lead_len) - (p + l + 1)));
1185 lead_len -= l;
1186 *p = ' ';
1187 }
1188 else
1189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (!vim_iswhite(*p))
1191 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194 else /* left adjusted leader */
1195 {
1196 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001197#ifdef FEAT_MBYTE
1198 /* Compute the length of the replaced characters in
1199 * screen characters, not bytes. Move the part that is
1200 * not to be overwritten. */
1201 {
1202 int repl_size = vim_strnsize(lead_repl,
1203 lead_repl_len);
1204 int i;
1205 int l;
1206
1207 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1208 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001209 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001210 if (vim_strnsize(p, i + l) > repl_size)
1211 break;
1212 }
1213 if (i != lead_repl_len)
1214 {
1215 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001216 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001217 lead_len += lead_repl_len - i;
1218 }
1219 }
1220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1222
1223 /* Replace any remaining non-white chars in the old
1224 * leader by spaces. Keep Tabs, the indent must
1225 * remain the same. */
1226 for (p += lead_repl_len; p < leader + lead_len; ++p)
1227 if (!vim_iswhite(*p))
1228 {
1229 /* Don't put a space before a TAB. */
1230 if (p + 1 < leader + lead_len && p[1] == TAB)
1231 {
1232 --lead_len;
1233 mch_memmove(p, p + 1,
1234 (leader + lead_len) - p);
1235 }
1236 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001237 {
1238#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001239 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001240
1241 if (l > 1)
1242 {
1243 if (ptr2cells(p) > 1)
1244 {
1245 /* Replace a double-wide char with
1246 * two spaces */
1247 --l;
1248 *p++ = ' ';
1249 }
1250 mch_memmove(p + 1, p + l,
1251 (leader + lead_len) - p);
1252 lead_len -= l - 1;
1253 }
1254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258 *p = NUL;
1259 }
1260
1261 /* Recompute the indent, it may have changed. */
1262 if (curbuf->b_p_ai
1263#ifdef FEAT_SMARTINDENT
1264 || do_si
1265#endif
1266 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001267 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 /* Add the indent offset */
1270 if (newindent + off < 0)
1271 {
1272 off = -newindent;
1273 newindent = 0;
1274 }
1275 else
1276 newindent += off;
1277
1278 /* Correct trailing spaces for the shift, so that
1279 * alignment remains equal. */
1280 while (off > 0 && lead_len > 0
1281 && leader[lead_len - 1] == ' ')
1282 {
1283 /* Don't do it when there is a tab before the space */
1284 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1285 break;
1286 --lead_len;
1287 --off;
1288 }
1289
1290 /* If the leader ends in white space, don't add an
1291 * extra space */
1292 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1293 extra_space = FALSE;
1294 leader[lead_len] = NUL;
1295 }
1296
1297 if (extra_space)
1298 {
1299 leader[lead_len++] = ' ';
1300 leader[lead_len] = NUL;
1301 }
1302
1303 newcol = lead_len;
1304
1305 /*
1306 * if a new indent will be set below, remove the indent that
1307 * is in the comment leader
1308 */
1309 if (newindent
1310#ifdef FEAT_SMARTINDENT
1311 || did_si
1312#endif
1313 )
1314 {
1315 while (lead_len && vim_iswhite(*leader))
1316 {
1317 --lead_len;
1318 --newcol;
1319 ++leader;
1320 }
1321 }
1322
1323 }
1324#ifdef FEAT_SMARTINDENT
1325 did_si = can_si = FALSE;
1326#endif
1327 }
1328 else if (comment_end != NULL)
1329 {
1330 /*
1331 * We have finished a comment, so we don't use the leader.
1332 * If this was a C-comment and 'ai' or 'si' is set do a normal
1333 * indent to align with the line containing the start of the
1334 * comment.
1335 */
1336 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1337 (curbuf->b_p_ai
1338#ifdef FEAT_SMARTINDENT
1339 || do_si
1340#endif
1341 ))
1342 {
1343 old_cursor = curwin->w_cursor;
1344 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1345 if ((pos = findmatch(NULL, NUL)) != NULL)
1346 {
1347 curwin->w_cursor.lnum = pos->lnum;
1348 newindent = get_indent();
1349 }
1350 curwin->w_cursor = old_cursor;
1351 }
1352 }
1353 }
1354#endif
1355
1356 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1357 if (p_extra != NULL)
1358 {
1359 *p_extra = saved_char; /* restore char that NUL replaced */
1360
1361 /*
1362 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1363 * non-blank.
1364 *
1365 * When in REPLACE mode, put the deleted blanks on the replace stack,
1366 * preceded by a NUL, so they can be put back when a BS is entered.
1367 */
1368 if (REPLACE_NORMAL(State))
1369 replace_push(NUL); /* end of extra blanks */
1370 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1371 {
1372 while ((*p_extra == ' ' || *p_extra == '\t')
1373#ifdef FEAT_MBYTE
1374 && (!enc_utf8
1375 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1376#endif
1377 )
1378 {
1379 if (REPLACE_NORMAL(State))
1380 replace_push(*p_extra);
1381 ++p_extra;
1382 ++less_cols_off;
1383 }
1384 }
1385 if (*p_extra != NUL)
1386 did_ai = FALSE; /* append some text, don't truncate now */
1387
1388 /* columns for marks adjusted for removed columns */
1389 less_cols = (int)(p_extra - saved_line);
1390 }
1391
1392 if (p_extra == NULL)
1393 p_extra = (char_u *)""; /* append empty line */
1394
1395#ifdef FEAT_COMMENTS
1396 /* concatenate leader and p_extra, if there is a leader */
1397 if (lead_len)
1398 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001399 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1400 {
1401 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001402 int padding = second_line_indent
1403 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001404
1405 /* Here whitespace is inserted after the comment char.
1406 * Below, set_indent(newindent, SIN_INSERT) will insert the
1407 * whitespace needed before the comment char. */
1408 for (i = 0; i < padding; i++)
1409 {
1410 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001411 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001412 newcol++;
1413 }
1414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 STRCAT(leader, p_extra);
1416 p_extra = leader;
1417 did_ai = TRUE; /* So truncating blanks works with comments */
1418 less_cols -= lead_len;
1419 }
1420 else
1421 end_comment_pending = NUL; /* turns out there was no leader */
1422#endif
1423
1424 old_cursor = curwin->w_cursor;
1425 if (dir == BACKWARD)
1426 --curwin->w_cursor.lnum;
1427#ifdef FEAT_VREPLACE
1428 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1429#endif
1430 {
1431 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1432 == FAIL)
1433 goto theend;
1434 /* Postpone calling changed_lines(), because it would mess up folding
1435 * with markers. */
1436 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1437 did_append = TRUE;
1438 }
1439#ifdef FEAT_VREPLACE
1440 else
1441 {
1442 /*
1443 * In VREPLACE mode we are starting to replace the next line.
1444 */
1445 curwin->w_cursor.lnum++;
1446 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1447 {
1448 /* In case we NL to a new line, BS to the previous one, and NL
1449 * again, we don't want to save the new line for undo twice.
1450 */
1451 (void)u_save_cursor(); /* errors are ignored! */
1452 vr_lines_changed++;
1453 }
1454 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1455 changed_bytes(curwin->w_cursor.lnum, 0);
1456 curwin->w_cursor.lnum--;
1457 did_append = FALSE;
1458 }
1459#endif
1460
1461 if (newindent
1462#ifdef FEAT_SMARTINDENT
1463 || did_si
1464#endif
1465 )
1466 {
1467 ++curwin->w_cursor.lnum;
1468#ifdef FEAT_SMARTINDENT
1469 if (did_si)
1470 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001471 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001474 newindent -= newindent % sw;
1475 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001478 /* Copy the indent */
1479 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 (void)copy_indent(newindent, saved_line);
1482
1483 /*
1484 * Set the 'preserveindent' option so that any further screwing
1485 * with the line doesn't entirely destroy our efforts to preserve
1486 * it. It gets restored at the function end.
1487 */
1488 curbuf->b_p_pi = TRUE;
1489 }
1490 else
1491 (void)set_indent(newindent, SIN_INSERT);
1492 less_cols -= curwin->w_cursor.col;
1493
1494 ai_col = curwin->w_cursor.col;
1495
1496 /*
1497 * In REPLACE mode, for each character in the new indent, there must
1498 * be a NUL on the replace stack, for when it is deleted with BS
1499 */
1500 if (REPLACE_NORMAL(State))
1501 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1502 replace_push(NUL);
1503 newcol += curwin->w_cursor.col;
1504#ifdef FEAT_SMARTINDENT
1505 if (no_si)
1506 did_si = FALSE;
1507#endif
1508 }
1509
1510#ifdef FEAT_COMMENTS
1511 /*
1512 * In REPLACE mode, for each character in the extra leader, there must be
1513 * a NUL on the replace stack, for when it is deleted with BS.
1514 */
1515 if (REPLACE_NORMAL(State))
1516 while (lead_len-- > 0)
1517 replace_push(NUL);
1518#endif
1519
1520 curwin->w_cursor = old_cursor;
1521
1522 if (dir == FORWARD)
1523 {
1524 if (trunc_line || (State & INSERT))
1525 {
1526 /* truncate current line at cursor */
1527 saved_line[curwin->w_cursor.col] = NUL;
1528 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1529 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1530 truncate_spaces(saved_line);
1531 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1532 saved_line = NULL;
1533 if (did_append)
1534 {
1535 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1536 curwin->w_cursor.lnum + 1, 1L);
1537 did_append = FALSE;
1538
1539 /* Move marks after the line break to the new line. */
1540 if (flags & OPENLINE_MARKFIX)
1541 mark_col_adjust(curwin->w_cursor.lnum,
1542 curwin->w_cursor.col + less_cols_off,
1543 1L, (long)-less_cols);
1544 }
1545 else
1546 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1547 }
1548
1549 /*
1550 * Put the cursor on the new line. Careful: the scrollup() above may
1551 * have moved w_cursor, we must use old_cursor.
1552 */
1553 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1554 }
1555 if (did_append)
1556 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1557
1558 curwin->w_cursor.col = newcol;
1559#ifdef FEAT_VIRTUALEDIT
1560 curwin->w_cursor.coladd = 0;
1561#endif
1562
1563#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1564 /*
1565 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1566 * fixthisline() from doing it (via change_indent()) by telling it we're in
1567 * normal INSERT mode.
1568 */
1569 if (State & VREPLACE_FLAG)
1570 {
1571 vreplace_mode = State; /* So we know to put things right later */
1572 State = INSERT;
1573 }
1574 else
1575 vreplace_mode = 0;
1576#endif
1577#ifdef FEAT_LISP
1578 /*
1579 * May do lisp indenting.
1580 */
1581 if (!p_paste
1582# ifdef FEAT_COMMENTS
1583 && leader == NULL
1584# endif
1585 && curbuf->b_p_lisp
1586 && curbuf->b_p_ai)
1587 {
1588 fixthisline(get_lisp_indent);
1589 p = ml_get_curline();
1590 ai_col = (colnr_T)(skipwhite(p) - p);
1591 }
1592#endif
1593#ifdef FEAT_CINDENT
1594 /*
1595 * May do indenting after opening a new line.
1596 */
1597 if (!p_paste
1598 && (curbuf->b_p_cin
1599# ifdef FEAT_EVAL
1600 || *curbuf->b_p_inde != NUL
1601# endif
1602 )
1603 && in_cinkeys(dir == FORWARD
1604 ? KEY_OPEN_FORW
1605 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1606 {
1607 do_c_expr_indent();
1608 p = ml_get_curline();
1609 ai_col = (colnr_T)(skipwhite(p) - p);
1610 }
1611#endif
1612#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1613 if (vreplace_mode != 0)
1614 State = vreplace_mode;
1615#endif
1616
1617#ifdef FEAT_VREPLACE
1618 /*
1619 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1620 * original line, and inserts the new stuff char by char, pushing old stuff
1621 * onto the replace stack (via ins_char()).
1622 */
1623 if (State & VREPLACE_FLAG)
1624 {
1625 /* Put new line in p_extra */
1626 p_extra = vim_strsave(ml_get_curline());
1627 if (p_extra == NULL)
1628 goto theend;
1629
1630 /* Put back original line */
1631 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1632
1633 /* Insert new stuff into line again */
1634 curwin->w_cursor.col = 0;
1635#ifdef FEAT_VIRTUALEDIT
1636 curwin->w_cursor.coladd = 0;
1637#endif
1638 ins_bytes(p_extra); /* will call changed_bytes() */
1639 vim_free(p_extra);
1640 next_line = NULL;
1641 }
1642#endif
1643
1644 retval = TRUE; /* success! */
1645theend:
1646 curbuf->b_p_pi = saved_pi;
1647 vim_free(saved_line);
1648 vim_free(next_line);
1649 vim_free(allocated);
1650 return retval;
1651}
1652
1653#if defined(FEAT_COMMENTS) || defined(PROTO)
1654/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001655 * get_leader_len() returns the length in bytes of the prefix of the given
1656 * string which introduces a comment. If this string is not a comment then
1657 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 * When "flags" is not NULL, it is set to point to the flags of the recognized
1659 * comment leader.
1660 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001661 * If "include_space" is set, include trailing whitespace while calculating the
1662 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 */
1664 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001665get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 char_u *line;
1667 char_u **flags;
1668 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001669 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001672 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 int got_com = FALSE;
1674 int found_one;
1675 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1676 char_u *string; /* pointer to comment string */
1677 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001678 int middle_match_len = 0;
1679 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001680 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar81340392012-06-06 16:12:59 +02001682 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 while (vim_iswhite(line[i])) /* leading white space is ignored */
1684 ++i;
1685
1686 /*
1687 * Repeat to match several nested comment strings.
1688 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001689 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 /*
1692 * scan through the 'comments' option for a match
1693 */
1694 found_one = FALSE;
1695 for (list = curbuf->b_p_com; *list; )
1696 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001697 /* Get one option part into part_buf[]. Advance "list" to next
1698 * one. Put "string" at start of string. */
1699 if (!got_com && flags != NULL)
1700 *flags = list; /* remember where flags started */
1701 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1703 string = vim_strchr(part_buf, ':');
1704 if (string == NULL) /* missing ':', ignore this part */
1705 continue;
1706 *string++ = NUL; /* isolate flags from string */
1707
Bram Moolenaara4271d52011-05-10 13:38:27 +02001708 /* If we found a middle match previously, use that match when this
1709 * is not a middle or end. */
1710 if (middle_match_len != 0
1711 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1712 && vim_strchr(part_buf, COM_END) == NULL)
1713 break;
1714
1715 /* When we already found a nested comment, only accept further
1716 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1718 continue;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1722 continue;
1723
Bram Moolenaara4271d52011-05-10 13:38:27 +02001724 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 * When string starts with white space, must have some white space
1726 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001727 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (vim_iswhite(string[0]))
1729 {
1730 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001731 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 while (vim_iswhite(string[0]))
1733 ++string;
1734 }
1735 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1736 ;
1737 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001738 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaara4271d52011-05-10 13:38:27 +02001740 /* When 'b' flag used, there must be white space or an
1741 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (vim_strchr(part_buf, COM_BLANK) != NULL
1743 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1744 continue;
1745
Bram Moolenaara4271d52011-05-10 13:38:27 +02001746 /* We have found a match, stop searching unless this is a middle
1747 * comment. The middle comment can be a substring of the end
1748 * comment in which case it's better to return the length of the
1749 * end comment and its flags. Thus we keep searching with middle
1750 * and end matches and use an end match if it matches better. */
1751 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1752 {
1753 if (middle_match_len == 0)
1754 {
1755 middle_match_len = j;
1756 saved_flags = prev_list;
1757 }
1758 continue;
1759 }
1760 if (middle_match_len != 0 && j > middle_match_len)
1761 /* Use this match instead of the middle match, since it's a
1762 * longer thus better match. */
1763 middle_match_len = 0;
1764
1765 if (middle_match_len == 0)
1766 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 found_one = TRUE;
1768 break;
1769 }
1770
Bram Moolenaara4271d52011-05-10 13:38:27 +02001771 if (middle_match_len != 0)
1772 {
1773 /* Use the previously found middle match after failing to find a
1774 * match with an end. */
1775 if (!got_com && flags != NULL)
1776 *flags = saved_flags;
1777 i += middle_match_len;
1778 found_one = TRUE;
1779 }
1780
1781 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (!found_one)
1783 break;
1784
Bram Moolenaar81340392012-06-06 16:12:59 +02001785 result = i;
1786
Bram Moolenaara4271d52011-05-10 13:38:27 +02001787 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 while (vim_iswhite(line[i]))
1789 ++i;
1790
Bram Moolenaar81340392012-06-06 16:12:59 +02001791 if (include_space)
1792 result = i;
1793
Bram Moolenaara4271d52011-05-10 13:38:27 +02001794 /* If this comment doesn't nest, stop here. */
1795 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (vim_strchr(part_buf, COM_NEST) == NULL)
1797 break;
1798 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001799 return result;
1800}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001801
Bram Moolenaar81340392012-06-06 16:12:59 +02001802/*
1803 * Return the offset at which the last comment in line starts. If there is no
1804 * comment in the whole line, -1 is returned.
1805 *
1806 * When "flags" is not null, it is set to point to the flags describing the
1807 * recognized comment leader.
1808 */
1809 int
1810get_last_leader_offset(line, flags)
1811 char_u *line;
1812 char_u **flags;
1813{
1814 int result = -1;
1815 int i, j;
1816 int lower_check_bound = 0;
1817 char_u *string;
1818 char_u *com_leader;
1819 char_u *com_flags;
1820 char_u *list;
1821 int found_one;
1822 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1823
1824 /*
1825 * Repeat to match several nested comment strings.
1826 */
1827 i = (int)STRLEN(line);
1828 while (--i >= lower_check_bound)
1829 {
1830 /*
1831 * scan through the 'comments' option for a match
1832 */
1833 found_one = FALSE;
1834 for (list = curbuf->b_p_com; *list; )
1835 {
1836 char_u *flags_save = list;
1837
1838 /*
1839 * Get one option part into part_buf[]. Advance list to next one.
1840 * put string at start of string.
1841 */
1842 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1843 string = vim_strchr(part_buf, ':');
1844 if (string == NULL) /* If everything is fine, this cannot actually
1845 * happen. */
1846 {
1847 continue;
1848 }
1849 *string++ = NUL; /* Isolate flags from string. */
1850 com_leader = string;
1851
1852 /*
1853 * Line contents and string must match.
1854 * When string starts with white space, must have some white space
1855 * (but the amount does not need to match, there might be a mix of
1856 * TABs and spaces).
1857 */
1858 if (vim_iswhite(string[0]))
1859 {
1860 if (i == 0 || !vim_iswhite(line[i - 1]))
1861 continue;
1862 while (vim_iswhite(string[0]))
1863 ++string;
1864 }
1865 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1866 /* do nothing */;
1867 if (string[j] != NUL)
1868 continue;
1869
1870 /*
1871 * When 'b' flag used, there must be white space or an
1872 * end-of-line after the string in the line.
1873 */
1874 if (vim_strchr(part_buf, COM_BLANK) != NULL
1875 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1876 {
1877 continue;
1878 }
1879
1880 /*
1881 * We have found a match, stop searching.
1882 */
1883 found_one = TRUE;
1884
1885 if (flags)
1886 *flags = flags_save;
1887 com_flags = flags_save;
1888
1889 break;
1890 }
1891
1892 if (found_one)
1893 {
1894 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1895 int len1, len2, off;
1896
1897 result = i;
1898 /*
1899 * If this comment nests, continue searching.
1900 */
1901 if (vim_strchr(part_buf, COM_NEST) != NULL)
1902 continue;
1903
1904 lower_check_bound = i;
1905
1906 /* Let's verify whether the comment leader found is a substring
1907 * of other comment leaders. If it is, let's adjust the
1908 * lower_check_bound so that we make sure that we have determined
1909 * the comment leader correctly.
1910 */
1911
1912 while (vim_iswhite(*com_leader))
1913 ++com_leader;
1914 len1 = (int)STRLEN(com_leader);
1915
1916 for (list = curbuf->b_p_com; *list; )
1917 {
1918 char_u *flags_save = list;
1919
1920 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1921 if (flags_save == com_flags)
1922 continue;
1923 string = vim_strchr(part_buf2, ':');
1924 ++string;
1925 while (vim_iswhite(*string))
1926 ++string;
1927 len2 = (int)STRLEN(string);
1928 if (len2 == 0)
1929 continue;
1930
1931 /* Now we have to verify whether string ends with a substring
1932 * beginning the com_leader. */
1933 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1934 {
1935 --off;
1936 if (!STRNCMP(string + off, com_leader, len2 - off))
1937 {
1938 if (i - off < lower_check_bound)
1939 lower_check_bound = i - off;
1940 }
1941 }
1942 }
1943 }
1944 }
1945 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946}
1947#endif
1948
1949/*
1950 * Return the number of window lines occupied by buffer line "lnum".
1951 */
1952 int
1953plines(lnum)
1954 linenr_T lnum;
1955{
1956 return plines_win(curwin, lnum, TRUE);
1957}
1958
1959 int
1960plines_win(wp, lnum, winheight)
1961 win_T *wp;
1962 linenr_T lnum;
1963 int winheight; /* when TRUE limit to window height */
1964{
1965#if defined(FEAT_DIFF) || defined(PROTO)
1966 /* Check for filler lines above this buffer line. When folded the result
1967 * is one line anyway. */
1968 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1969}
1970
1971 int
1972plines_nofill(lnum)
1973 linenr_T lnum;
1974{
1975 return plines_win_nofill(curwin, lnum, TRUE);
1976}
1977
1978 int
1979plines_win_nofill(wp, lnum, winheight)
1980 win_T *wp;
1981 linenr_T lnum;
1982 int winheight; /* when TRUE limit to window height */
1983{
1984#endif
1985 int lines;
1986
1987 if (!wp->w_p_wrap)
1988 return 1;
1989
1990#ifdef FEAT_VERTSPLIT
1991 if (wp->w_width == 0)
1992 return 1;
1993#endif
1994
1995#ifdef FEAT_FOLDING
1996 /* A folded lines is handled just like an empty line. */
1997 /* NOTE: Caller must handle lines that are MAYBE folded. */
1998 if (lineFolded(wp, lnum) == TRUE)
1999 return 1;
2000#endif
2001
2002 lines = plines_win_nofold(wp, lnum);
2003 if (winheight > 0 && lines > wp->w_height)
2004 return (int)wp->w_height;
2005 return lines;
2006}
2007
2008/*
2009 * Return number of window lines physical line "lnum" will occupy in window
2010 * "wp". Does not care about folding, 'wrap' or 'diff'.
2011 */
2012 int
2013plines_win_nofold(wp, lnum)
2014 win_T *wp;
2015 linenr_T lnum;
2016{
2017 char_u *s;
2018 long col;
2019 int width;
2020
2021 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2022 if (*s == NUL) /* empty line */
2023 return 1;
2024 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2025
2026 /*
2027 * If list mode is on, then the '$' at the end of the line may take up one
2028 * extra column.
2029 */
2030 if (wp->w_p_list && lcs_eol != NUL)
2031 col += 1;
2032
2033 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002034 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 */
2036 width = W_WIDTH(wp) - win_col_off(wp);
2037 if (width <= 0)
2038 return 32000;
2039 if (col <= width)
2040 return 1;
2041 col -= width;
2042 width += win_col_off2(wp);
2043 return (col + (width - 1)) / width + 1;
2044}
2045
2046/*
2047 * Like plines_win(), but only reports the number of physical screen lines
2048 * used from the start of the line to the given column number.
2049 */
2050 int
2051plines_win_col(wp, lnum, column)
2052 win_T *wp;
2053 linenr_T lnum;
2054 long column;
2055{
2056 long col;
2057 char_u *s;
2058 int lines = 0;
2059 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002060 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062#ifdef FEAT_DIFF
2063 /* Check for filler lines above this buffer line. When folded the result
2064 * is one line anyway. */
2065 lines = diff_check_fill(wp, lnum);
2066#endif
2067
2068 if (!wp->w_p_wrap)
2069 return lines + 1;
2070
2071#ifdef FEAT_VERTSPLIT
2072 if (wp->w_width == 0)
2073 return lines + 1;
2074#endif
2075
Bram Moolenaar597a4222014-06-25 14:39:50 +02002076 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 col = 0;
2079 while (*s != NUL && --column >= 0)
2080 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002081 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002082 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084
2085 /*
2086 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2087 * INSERT mode, then col must be adjusted so that it represents the last
2088 * screen position of the TAB. This only fixes an error when the TAB wraps
2089 * from one screen line to the next (when 'columns' is not a multiple of
2090 * 'ts') -- webb.
2091 */
2092 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002093 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
2095 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002096 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 */
2098 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002099 if (width <= 0)
2100 return 9999;
2101
2102 lines += 1;
2103 if (col > width)
2104 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2105 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106}
2107
2108 int
2109plines_m_win(wp, first, last)
2110 win_T *wp;
2111 linenr_T first, last;
2112{
2113 int count = 0;
2114
2115 while (first <= last)
2116 {
2117#ifdef FEAT_FOLDING
2118 int x;
2119
2120 /* Check if there are any really folded lines, but also included lines
2121 * that are maybe folded. */
2122 x = foldedCount(wp, first, NULL);
2123 if (x > 0)
2124 {
2125 ++count; /* count 1 for "+-- folded" line */
2126 first += x;
2127 }
2128 else
2129#endif
2130 {
2131#ifdef FEAT_DIFF
2132 if (first == wp->w_topline)
2133 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2134 else
2135#endif
2136 count += plines_win(wp, first, TRUE);
2137 ++first;
2138 }
2139 }
2140 return (count);
2141}
2142
2143#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2144/*
2145 * Insert string "p" at the cursor position. Stops at a NUL byte.
2146 * Handles Replace mode and multi-byte characters.
2147 */
2148 void
2149ins_bytes(p)
2150 char_u *p;
2151{
2152 ins_bytes_len(p, (int)STRLEN(p));
2153}
2154#endif
2155
2156#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2157 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2158/*
2159 * Insert string "p" with length "len" at the cursor position.
2160 * Handles Replace mode and multi-byte characters.
2161 */
2162 void
2163ins_bytes_len(p, len)
2164 char_u *p;
2165 int len;
2166{
2167 int i;
2168# ifdef FEAT_MBYTE
2169 int n;
2170
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002171 if (has_mbyte)
2172 for (i = 0; i < len; i += n)
2173 {
2174 if (enc_utf8)
2175 /* avoid reading past p[len] */
2176 n = utfc_ptr2len_len(p + i, len - i);
2177 else
2178 n = (*mb_ptr2len)(p + i);
2179 ins_char_bytes(p + i, n);
2180 }
2181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002183 for (i = 0; i < len; ++i)
2184 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185}
2186#endif
2187
2188/*
2189 * Insert or replace a single character at the cursor position.
2190 * When in REPLACE or VREPLACE mode, replace any existing character.
2191 * Caller must have prepared for undo.
2192 * For multi-byte characters we get the whole character, the caller must
2193 * convert bytes to a character.
2194 */
2195 void
2196ins_char(c)
2197 int c;
2198{
2199#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002200 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 int n;
2202
2203 n = (*mb_char2bytes)(c, buf);
2204
2205 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2206 * Happens for CTRL-Vu9900. */
2207 if (buf[0] == 0)
2208 buf[0] = '\n';
2209
2210 ins_char_bytes(buf, n);
2211}
2212
2213 void
2214ins_char_bytes(buf, charlen)
2215 char_u *buf;
2216 int charlen;
2217{
2218 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#endif
2220 int newlen; /* nr of bytes inserted */
2221 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2222 char_u *p;
2223 char_u *newp;
2224 char_u *oldp;
2225 int linelen; /* length of old line including NUL */
2226 colnr_T col;
2227 linenr_T lnum = curwin->w_cursor.lnum;
2228 int i;
2229
2230#ifdef FEAT_VIRTUALEDIT
2231 /* Break tabs if needed. */
2232 if (virtual_active() && curwin->w_cursor.coladd > 0)
2233 coladvance_force(getviscol());
2234#endif
2235
2236 col = curwin->w_cursor.col;
2237 oldp = ml_get(lnum);
2238 linelen = (int)STRLEN(oldp) + 1;
2239
2240 /* The lengths default to the values for when not replacing. */
2241 oldlen = 0;
2242#ifdef FEAT_MBYTE
2243 newlen = charlen;
2244#else
2245 newlen = 1;
2246#endif
2247
2248 if (State & REPLACE_FLAG)
2249 {
2250#ifdef FEAT_VREPLACE
2251 if (State & VREPLACE_FLAG)
2252 {
2253 colnr_T new_vcol = 0; /* init for GCC */
2254 colnr_T vcol;
2255 int old_list;
2256#ifndef FEAT_MBYTE
2257 char_u buf[2];
2258#endif
2259
2260 /*
2261 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2262 * Returns the old value of list, so when finished,
2263 * curwin->w_p_list should be set back to this.
2264 */
2265 old_list = curwin->w_p_list;
2266 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2267 curwin->w_p_list = FALSE;
2268
2269 /*
2270 * In virtual replace mode each character may replace one or more
2271 * characters (zero if it's a TAB). Count the number of bytes to
2272 * be deleted to make room for the new character, counting screen
2273 * cells. May result in adding spaces to fill a gap.
2274 */
2275 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2276#ifndef FEAT_MBYTE
2277 buf[0] = c;
2278 buf[1] = NUL;
2279#endif
2280 new_vcol = vcol + chartabsize(buf, vcol);
2281 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2282 {
2283 vcol += chartabsize(oldp + col + oldlen, vcol);
2284 /* Don't need to remove a TAB that takes us to the right
2285 * position. */
2286 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2287 break;
2288#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002289 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#else
2291 ++oldlen;
2292#endif
2293 /* Deleted a bit too much, insert spaces. */
2294 if (vcol > new_vcol)
2295 newlen += vcol - new_vcol;
2296 }
2297 curwin->w_p_list = old_list;
2298 }
2299 else
2300#endif
2301 if (oldp[col] != NUL)
2302 {
2303 /* normal replace */
2304#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002305 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#else
2307 oldlen = 1;
2308#endif
2309 }
2310
2311
2312 /* Push the replaced bytes onto the replace stack, so that they can be
2313 * put back when BS is used. The bytes of a multi-byte character are
2314 * done the other way around, so that the first byte is popped off
2315 * first (it tells the byte length of the character). */
2316 replace_push(NUL);
2317 for (i = 0; i < oldlen; ++i)
2318 {
2319#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002320 if (has_mbyte)
2321 i += replace_push_mb(oldp + col + i) - 1;
2322 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002324 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326 }
2327
2328 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2329 if (newp == NULL)
2330 return;
2331
2332 /* Copy bytes before the cursor. */
2333 if (col > 0)
2334 mch_memmove(newp, oldp, (size_t)col);
2335
2336 /* Copy bytes after the changed character(s). */
2337 p = newp + col;
2338 mch_memmove(p + newlen, oldp + col + oldlen,
2339 (size_t)(linelen - col - oldlen));
2340
2341 /* Insert or overwrite the new character. */
2342#ifdef FEAT_MBYTE
2343 mch_memmove(p, buf, charlen);
2344 i = charlen;
2345#else
2346 *p = c;
2347 i = 1;
2348#endif
2349
2350 /* Fill with spaces when necessary. */
2351 while (i < newlen)
2352 p[i++] = ' ';
2353
2354 /* Replace the line in the buffer. */
2355 ml_replace(lnum, newp, FALSE);
2356
2357 /* mark the buffer as changed and prepare for displaying */
2358 changed_bytes(lnum, col);
2359
2360 /*
2361 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2362 * show the match for right parens and braces.
2363 */
2364 if (p_sm && (State & INSERT)
2365 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002366#ifdef FEAT_INS_EXPAND
2367 && !ins_compl_active()
2368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002370 {
2371#ifdef FEAT_MBYTE
2372 if (has_mbyte)
2373 showmatch(mb_ptr2char(buf));
2374 else
2375#endif
2376 showmatch(c);
2377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379#ifdef FEAT_RIGHTLEFT
2380 if (!p_ri || (State & REPLACE_FLAG))
2381#endif
2382 {
2383 /* Normal insert: move cursor right */
2384#ifdef FEAT_MBYTE
2385 curwin->w_cursor.col += charlen;
2386#else
2387 ++curwin->w_cursor.col;
2388#endif
2389 }
2390 /*
2391 * TODO: should try to update w_row here, to avoid recomputing it later.
2392 */
2393}
2394
2395/*
2396 * Insert a string at the cursor position.
2397 * Note: Does NOT handle Replace mode.
2398 * Caller must have prepared for undo.
2399 */
2400 void
2401ins_str(s)
2402 char_u *s;
2403{
2404 char_u *oldp, *newp;
2405 int newlen = (int)STRLEN(s);
2406 int oldlen;
2407 colnr_T col;
2408 linenr_T lnum = curwin->w_cursor.lnum;
2409
2410#ifdef FEAT_VIRTUALEDIT
2411 if (virtual_active() && curwin->w_cursor.coladd > 0)
2412 coladvance_force(getviscol());
2413#endif
2414
2415 col = curwin->w_cursor.col;
2416 oldp = ml_get(lnum);
2417 oldlen = (int)STRLEN(oldp);
2418
2419 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2420 if (newp == NULL)
2421 return;
2422 if (col > 0)
2423 mch_memmove(newp, oldp, (size_t)col);
2424 mch_memmove(newp + col, s, (size_t)newlen);
2425 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2426 ml_replace(lnum, newp, FALSE);
2427 changed_bytes(lnum, col);
2428 curwin->w_cursor.col += newlen;
2429}
2430
2431/*
2432 * Delete one character under the cursor.
2433 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2434 * Caller must have prepared for undo.
2435 *
2436 * return FAIL for failure, OK otherwise
2437 */
2438 int
2439del_char(fixpos)
2440 int fixpos;
2441{
2442#ifdef FEAT_MBYTE
2443 if (has_mbyte)
2444 {
2445 /* Make sure the cursor is at the start of a character. */
2446 mb_adjust_cursor();
2447 if (*ml_get_cursor() == NUL)
2448 return FAIL;
2449 return del_chars(1L, fixpos);
2450 }
2451#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002452 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453}
2454
2455#if defined(FEAT_MBYTE) || defined(PROTO)
2456/*
2457 * Like del_bytes(), but delete characters instead of bytes.
2458 */
2459 int
2460del_chars(count, fixpos)
2461 long count;
2462 int fixpos;
2463{
2464 long bytes = 0;
2465 long i;
2466 char_u *p;
2467 int l;
2468
2469 p = ml_get_cursor();
2470 for (i = 0; i < count && *p != NUL; ++i)
2471 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002472 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 bytes += l;
2474 p += l;
2475 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002476 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477}
2478#endif
2479
2480/*
2481 * Delete "count" bytes under the cursor.
2482 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2483 * Caller must have prepared for undo.
2484 *
2485 * return FAIL for failure, OK otherwise
2486 */
2487 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002488del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002490 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002491 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 char_u *oldp, *newp;
2494 colnr_T oldlen;
2495 linenr_T lnum = curwin->w_cursor.lnum;
2496 colnr_T col = curwin->w_cursor.col;
2497 int was_alloced;
2498 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002499 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501 oldp = ml_get(lnum);
2502 oldlen = (int)STRLEN(oldp);
2503
2504 /*
2505 * Can't do anything when the cursor is on the NUL after the line.
2506 */
2507 if (col >= oldlen)
2508 return FAIL;
2509
2510#ifdef FEAT_MBYTE
2511 /* If 'delcombine' is set and deleting (less than) one character, only
2512 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002513 if (p_deco && use_delcombine && enc_utf8
2514 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002516 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 int n;
2518
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002519 (void)utfc_ptr2char(oldp + col, cc);
2520 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 /* Find the last composing char, there can be several. */
2523 n = col;
2524 do
2525 {
2526 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002527 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 n += count;
2529 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2530 fixpos = 0;
2531 }
2532 }
2533#endif
2534
2535 /*
2536 * When count is too big, reduce it.
2537 */
2538 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2539 if (movelen <= 1)
2540 {
2541 /*
2542 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002543 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2544 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002546 if (col > 0 && fixpos && restart_edit == 0
2547#ifdef FEAT_VIRTUALEDIT
2548 && (ve_flags & VE_ONEMORE) == 0
2549#endif
2550 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 --curwin->w_cursor.col;
2553#ifdef FEAT_VIRTUALEDIT
2554 curwin->w_cursor.coladd = 0;
2555#endif
2556#ifdef FEAT_MBYTE
2557 if (has_mbyte)
2558 curwin->w_cursor.col -=
2559 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2560#endif
2561 }
2562 count = oldlen - col;
2563 movelen = 1;
2564 }
2565
2566 /*
2567 * If the old line has been allocated the deletion can be done in the
2568 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002569 * Can't do this when using Netbeans, because we would need to invoke
2570 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002571 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002574 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002575 was_alloced = FALSE;
2576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002578 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (was_alloced)
2580 newp = oldp; /* use same allocated memory */
2581 else
2582 { /* need to allocate a new line */
2583 newp = alloc((unsigned)(oldlen + 1 - count));
2584 if (newp == NULL)
2585 return FAIL;
2586 mch_memmove(newp, oldp, (size_t)col);
2587 }
2588 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2589 if (!was_alloced)
2590 ml_replace(lnum, newp, FALSE);
2591
2592 /* mark the buffer as changed and prepare for displaying */
2593 changed_bytes(lnum, curwin->w_cursor.col);
2594
2595 return OK;
2596}
2597
2598/*
2599 * Delete from cursor to end of line.
2600 * Caller must have prepared for undo.
2601 *
2602 * return FAIL for failure, OK otherwise
2603 */
2604 int
2605truncate_line(fixpos)
2606 int fixpos; /* if TRUE fix the cursor position when done */
2607{
2608 char_u *newp;
2609 linenr_T lnum = curwin->w_cursor.lnum;
2610 colnr_T col = curwin->w_cursor.col;
2611
2612 if (col == 0)
2613 newp = vim_strsave((char_u *)"");
2614 else
2615 newp = vim_strnsave(ml_get(lnum), col);
2616
2617 if (newp == NULL)
2618 return FAIL;
2619
2620 ml_replace(lnum, newp, FALSE);
2621
2622 /* mark the buffer as changed and prepare for displaying */
2623 changed_bytes(lnum, curwin->w_cursor.col);
2624
2625 /*
2626 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2627 */
2628 if (fixpos && curwin->w_cursor.col > 0)
2629 --curwin->w_cursor.col;
2630
2631 return OK;
2632}
2633
2634/*
2635 * Delete "nlines" lines at the cursor.
2636 * Saves the lines for undo first if "undo" is TRUE.
2637 */
2638 void
2639del_lines(nlines, undo)
2640 long nlines; /* number of lines to delete */
2641 int undo; /* if TRUE, prepare for undo */
2642{
2643 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002644 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 if (nlines <= 0)
2647 return;
2648
2649 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002650 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 return;
2652
2653 for (n = 0; n < nlines; )
2654 {
2655 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2656 break;
2657
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002658 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ++n;
2660
2661 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002662 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 break;
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002666 /* Correct the cursor position before calling deleted_lines_mark(), it may
2667 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 curwin->w_cursor.col = 0;
2669 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002670
2671 /* adjust marks, mark the buffer as changed and prepare for displaying */
2672 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673}
2674
2675 int
2676gchar_pos(pos)
2677 pos_T *pos;
2678{
2679 char_u *ptr = ml_get_pos(pos);
2680
2681#ifdef FEAT_MBYTE
2682 if (has_mbyte)
2683 return (*mb_ptr2char)(ptr);
2684#endif
2685 return (int)*ptr;
2686}
2687
2688 int
2689gchar_cursor()
2690{
2691#ifdef FEAT_MBYTE
2692 if (has_mbyte)
2693 return (*mb_ptr2char)(ml_get_cursor());
2694#endif
2695 return (int)*ml_get_cursor();
2696}
2697
2698/*
2699 * Write a character at the current cursor position.
2700 * It is directly written into the block.
2701 */
2702 void
2703pchar_cursor(c)
2704 int c;
2705{
2706 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2707 + curwin->w_cursor.col) = c;
2708}
2709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710/*
2711 * When extra == 0: Return TRUE if the cursor is before or on the first
2712 * non-blank in the line.
2713 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2714 * the line.
2715 */
2716 int
2717inindent(extra)
2718 int extra;
2719{
2720 char_u *ptr;
2721 colnr_T col;
2722
2723 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2724 ++ptr;
2725 if (col >= curwin->w_cursor.col + extra)
2726 return TRUE;
2727 else
2728 return FALSE;
2729}
2730
2731/*
2732 * Skip to next part of an option argument: Skip space and comma.
2733 */
2734 char_u *
2735skip_to_option_part(p)
2736 char_u *p;
2737{
2738 if (*p == ',')
2739 ++p;
2740 while (*p == ' ')
2741 ++p;
2742 return p;
2743}
2744
2745/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002746 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 *
2748 * Most often called through changed_bytes() and changed_lines(), which also
2749 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002750 *
2751 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
2753 void
2754changed()
2755{
2756#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2757 /* The text of the preediting area is inserted, but this doesn't
2758 * mean a change of the buffer yet. That is delayed until the
2759 * text is committed. (this means preedit becomes empty) */
2760 if (im_is_preediting() && !xim_changed_while_preediting)
2761 return;
2762 xim_changed_while_preediting = FALSE;
2763#endif
2764
2765 if (!curbuf->b_changed)
2766 {
2767 int save_msg_scroll = msg_scroll;
2768
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002769 /* Give a warning about changing a read-only file. This may also
2770 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 /* Create a swap file if that is wanted.
2774 * Don't do this for "nofile" and "nowrite" buffer types. */
2775 if (curbuf->b_may_swap
2776#ifdef FEAT_QUICKFIX
2777 && !bt_dontwrite(curbuf)
2778#endif
2779 )
2780 {
2781 ml_open_file(curbuf);
2782
2783 /* The ml_open_file() can cause an ATTENTION message.
2784 * Wait two seconds, to make sure the user reads this unexpected
2785 * message. Since we could be anywhere, call wait_return() now,
2786 * and don't let the emsg() set msg_scroll. */
2787 if (need_wait_return && emsg_silent == 0)
2788 {
2789 out_flush();
2790 ui_delay(2000L, TRUE);
2791 wait_return(TRUE);
2792 msg_scroll = save_msg_scroll;
2793 }
2794 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002795 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798}
2799
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002800/*
2801 * Internal part of changed(), no user interaction.
2802 */
2803 void
2804changed_int()
2805{
2806 curbuf->b_changed = TRUE;
2807 ml_setflags(curbuf);
2808#ifdef FEAT_WINDOWS
2809 check_status(curbuf);
2810 redraw_tabline = TRUE;
2811#endif
2812#ifdef FEAT_TITLE
2813 need_maketitle = TRUE; /* set window title later */
2814#endif
2815}
2816
Bram Moolenaardba8a912005-04-24 22:08:39 +00002817static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2818static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2820
2821/*
2822 * Changed bytes within a single line for the current buffer.
2823 * - marks the windows on this buffer to be redisplayed
2824 * - marks the buffer changed by calling changed()
2825 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002826 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 */
2828 void
2829changed_bytes(lnum, col)
2830 linenr_T lnum;
2831 colnr_T col;
2832{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002833 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002835
2836#ifdef FEAT_DIFF
2837 /* Diff highlighting in other diff windows may need to be updated too. */
2838 if (curwin->w_p_diff)
2839 {
2840 win_T *wp;
2841 linenr_T wlnum;
2842
2843 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2844 if (wp->w_p_diff && wp != curwin)
2845 {
2846 redraw_win_later(wp, VALID);
2847 wlnum = diff_lnum_win(lnum, wp);
2848 if (wlnum > 0)
2849 changedOneline(wp->w_buffer, wlnum);
2850 }
2851 }
2852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853}
2854
2855 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002856changedOneline(buf, lnum)
2857 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 linenr_T lnum;
2859{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002860 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
2862 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002863 if (lnum < buf->b_mod_top)
2864 buf->b_mod_top = lnum;
2865 else if (lnum >= buf->b_mod_bot)
2866 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 else
2869 {
2870 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002871 buf->b_mod_set = TRUE;
2872 buf->b_mod_top = lnum;
2873 buf->b_mod_bot = lnum + 1;
2874 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876}
2877
2878/*
2879 * Appended "count" lines below line "lnum" in the current buffer.
2880 * Must be called AFTER the change and after mark_adjust().
2881 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2882 */
2883 void
2884appended_lines(lnum, count)
2885 linenr_T lnum;
2886 long count;
2887{
2888 changed_lines(lnum + 1, 0, lnum + 1, count);
2889}
2890
2891/*
2892 * Like appended_lines(), but adjust marks first.
2893 */
2894 void
2895appended_lines_mark(lnum, count)
2896 linenr_T lnum;
2897 long count;
2898{
2899 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2900 changed_lines(lnum + 1, 0, lnum + 1, count);
2901}
2902
2903/*
2904 * Deleted "count" lines at line "lnum" in the current buffer.
2905 * Must be called AFTER the change and after mark_adjust().
2906 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2907 */
2908 void
2909deleted_lines(lnum, count)
2910 linenr_T lnum;
2911 long count;
2912{
2913 changed_lines(lnum, 0, lnum + count, -count);
2914}
2915
2916/*
2917 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002918 * Make sure the cursor is on a valid line before calling, a GUI callback may
2919 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
2921 void
2922deleted_lines_mark(lnum, count)
2923 linenr_T lnum;
2924 long count;
2925{
2926 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2927 changed_lines(lnum, 0, lnum + count, -count);
2928}
2929
2930/*
2931 * Changed lines for the current buffer.
2932 * Must be called AFTER the change and after mark_adjust().
2933 * - mark the buffer changed by calling changed()
2934 * - mark the windows on this buffer to be redisplayed
2935 * - invalidate cached values
2936 * "lnum" is the first line that needs displaying, "lnume" the first line
2937 * below the changed lines (BEFORE the change).
2938 * When only inserting lines, "lnum" and "lnume" are equal.
2939 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002940 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
2942 void
2943changed_lines(lnum, col, lnume, xtra)
2944 linenr_T lnum; /* first line with change */
2945 colnr_T col; /* column in first line with change */
2946 linenr_T lnume; /* line below last changed line */
2947 long xtra; /* number of extra lines (negative when deleting) */
2948{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002949 changed_lines_buf(curbuf, lnum, lnume, xtra);
2950
2951#ifdef FEAT_DIFF
2952 if (xtra == 0 && curwin->w_p_diff)
2953 {
2954 /* When the number of lines doesn't change then mark_adjust() isn't
2955 * called and other diff buffers still need to be marked for
2956 * displaying. */
2957 win_T *wp;
2958 linenr_T wlnum;
2959
2960 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2961 if (wp->w_p_diff && wp != curwin)
2962 {
2963 redraw_win_later(wp, VALID);
2964 wlnum = diff_lnum_win(lnum, wp);
2965 if (wlnum > 0)
2966 changed_lines_buf(wp->w_buffer, wlnum,
2967 lnume - lnum + wlnum, 0L);
2968 }
2969 }
2970#endif
2971
2972 changed_common(lnum, col, lnume, xtra);
2973}
2974
2975 static void
2976changed_lines_buf(buf, lnum, lnume, xtra)
2977 buf_T *buf;
2978 linenr_T lnum; /* first line with change */
2979 linenr_T lnume; /* line below last changed line */
2980 long xtra; /* number of extra lines (negative when deleting) */
2981{
2982 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
2984 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002985 if (lnum < buf->b_mod_top)
2986 buf->b_mod_top = lnum;
2987 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
2989 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002990 buf->b_mod_bot += xtra;
2991 if (buf->b_mod_bot < lnum)
2992 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002994 if (lnume + xtra > buf->b_mod_bot)
2995 buf->b_mod_bot = lnume + xtra;
2996 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998 else
2999 {
3000 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003001 buf->b_mod_set = TRUE;
3002 buf->b_mod_top = lnum;
3003 buf->b_mod_bot = lnume + xtra;
3004 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006}
3007
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003008/*
3009 * Common code for when a change is was made.
3010 * See changed_lines() for the arguments.
3011 * Careful: may trigger autocommands that reload the buffer.
3012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 static void
3014changed_common(lnum, col, lnume, xtra)
3015 linenr_T lnum;
3016 colnr_T col;
3017 linenr_T lnume;
3018 long xtra;
3019{
3020 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003021#ifdef FEAT_WINDOWS
3022 tabpage_T *tp;
3023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 int i;
3025#ifdef FEAT_JUMPLIST
3026 int cols;
3027 pos_T *p;
3028 int add;
3029#endif
3030
3031 /* mark the buffer as modified */
3032 changed();
3033
3034 /* set the '. mark */
3035 if (!cmdmod.keepjumps)
3036 {
3037 curbuf->b_last_change.lnum = lnum;
3038 curbuf->b_last_change.col = col;
3039
3040#ifdef FEAT_JUMPLIST
3041 /* Create a new entry if a new undo-able change was started or we
3042 * don't have an entry yet. */
3043 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3044 {
3045 if (curbuf->b_changelistlen == 0)
3046 add = TRUE;
3047 else
3048 {
3049 /* Don't create a new entry when the line number is the same
3050 * as the last one and the column is not too far away. Avoids
3051 * creating many entries for typing "xxxxx". */
3052 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3053 if (p->lnum != lnum)
3054 add = TRUE;
3055 else
3056 {
3057 cols = comp_textwidth(FALSE);
3058 if (cols == 0)
3059 cols = 79;
3060 add = (p->col + cols < col || col + cols < p->col);
3061 }
3062 }
3063 if (add)
3064 {
3065 /* This is the first of a new sequence of undo-able changes
3066 * and it's at some distance of the last change. Use a new
3067 * position in the changelist. */
3068 curbuf->b_new_change = FALSE;
3069
3070 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3071 {
3072 /* changelist is full: remove oldest entry */
3073 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3074 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3075 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003076 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
3078 /* Correct position in changelist for other windows on
3079 * this buffer. */
3080 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3081 --wp->w_changelistidx;
3082 }
3083 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003084 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
3086 /* For other windows, if the position in the changelist is
3087 * at the end it stays at the end. */
3088 if (wp->w_buffer == curbuf
3089 && wp->w_changelistidx == curbuf->b_changelistlen)
3090 ++wp->w_changelistidx;
3091 }
3092 ++curbuf->b_changelistlen;
3093 }
3094 }
3095 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3096 curbuf->b_last_change;
3097 /* The current window is always after the last change, so that "g,"
3098 * takes you back to it. */
3099 curwin->w_changelistidx = curbuf->b_changelistlen;
3100#endif
3101 }
3102
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003103 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
3105 if (wp->w_buffer == curbuf)
3106 {
3107 /* Mark this window to be redrawn later. */
3108 if (wp->w_redr_type < VALID)
3109 wp->w_redr_type = VALID;
3110
3111 /* Check if a change in the buffer has invalidated the cached
3112 * values for the cursor. */
3113#ifdef FEAT_FOLDING
3114 /*
3115 * Update the folds for this window. Can't postpone this, because
3116 * a following operator might work on the whole fold: ">>dd".
3117 */
3118 foldUpdate(wp, lnum, lnume + xtra - 1);
3119
3120 /* The change may cause lines above or below the change to become
3121 * included in a fold. Set lnum/lnume to the first/last line that
3122 * might be displayed differently.
3123 * Set w_cline_folded here as an efficient way to update it when
3124 * inserting lines just above a closed fold. */
3125 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3126 if (wp->w_cursor.lnum == lnum)
3127 wp->w_cline_folded = i;
3128 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3129 if (wp->w_cursor.lnum == lnume)
3130 wp->w_cline_folded = i;
3131
3132 /* If the changed line is in a range of previously folded lines,
3133 * compare with the first line in that range. */
3134 if (wp->w_cursor.lnum <= lnum)
3135 {
3136 i = find_wl_entry(wp, lnum);
3137 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3138 changed_line_abv_curs_win(wp);
3139 }
3140#endif
3141
3142 if (wp->w_cursor.lnum > lnum)
3143 changed_line_abv_curs_win(wp);
3144 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3145 changed_cline_bef_curs_win(wp);
3146 if (wp->w_botline >= lnum)
3147 {
3148 /* Assume that botline doesn't change (inserted lines make
3149 * other lines scroll down below botline). */
3150 approximate_botline_win(wp);
3151 }
3152
3153 /* Check if any w_lines[] entries have become invalid.
3154 * For entries below the change: Correct the lnums for
3155 * inserted/deleted lines. Makes it possible to stop displaying
3156 * after the change. */
3157 for (i = 0; i < wp->w_lines_valid; ++i)
3158 if (wp->w_lines[i].wl_valid)
3159 {
3160 if (wp->w_lines[i].wl_lnum >= lnum)
3161 {
3162 if (wp->w_lines[i].wl_lnum < lnume)
3163 {
3164 /* line included in change */
3165 wp->w_lines[i].wl_valid = FALSE;
3166 }
3167 else if (xtra != 0)
3168 {
3169 /* line below change */
3170 wp->w_lines[i].wl_lnum += xtra;
3171#ifdef FEAT_FOLDING
3172 wp->w_lines[i].wl_lastlnum += xtra;
3173#endif
3174 }
3175 }
3176#ifdef FEAT_FOLDING
3177 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3178 {
3179 /* change somewhere inside this range of folded lines,
3180 * may need to be redrawn */
3181 wp->w_lines[i].wl_valid = FALSE;
3182 }
3183#endif
3184 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003185
3186#ifdef FEAT_FOLDING
3187 /* Take care of side effects for setting w_topline when folds have
3188 * changed. Esp. when the buffer was changed in another window. */
3189 if (hasAnyFolding(wp))
3190 set_topline(wp, wp->w_topline);
3191#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003192 /* relative numbering may require updating more */
3193 if (wp->w_p_rnu)
3194 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 }
3197
3198 /* Call update_screen() later, which checks out what needs to be redrawn,
3199 * since it notices b_mod_set and then uses b_mod_*. */
3200 if (must_redraw < VALID)
3201 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003202
3203#ifdef FEAT_AUTOCMD
3204 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003205 if (lnum <= curwin->w_cursor.lnum
3206 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003207 last_cursormoved.lnum = 0;
3208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209}
3210
3211/*
3212 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3213 */
3214 void
3215unchanged(buf, ff)
3216 buf_T *buf;
3217 int ff; /* also reset 'fileformat' */
3218{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003219 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
3221 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003222 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (ff)
3224 save_file_ff(buf);
3225#ifdef FEAT_WINDOWS
3226 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003227 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#endif
3229#ifdef FEAT_TITLE
3230 need_maketitle = TRUE; /* set window title later */
3231#endif
3232 }
3233 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#ifdef FEAT_NETBEANS_INTG
3235 netbeans_unmodified(buf);
3236#endif
3237}
3238
3239#if defined(FEAT_WINDOWS) || defined(PROTO)
3240/*
3241 * check_status: called when the status bars for the buffer 'buf'
3242 * need to be updated
3243 */
3244 void
3245check_status(buf)
3246 buf_T *buf;
3247{
3248 win_T *wp;
3249
3250 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3251 if (wp->w_buffer == buf && wp->w_status_height)
3252 {
3253 wp->w_redr_status = TRUE;
3254 if (must_redraw < VALID)
3255 must_redraw = VALID;
3256 }
3257}
3258#endif
3259
3260/*
3261 * If the file is readonly, give a warning message with the first change.
3262 * Don't do this for autocommands.
3263 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003264 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003266 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 */
3268 void
3269change_warning(col)
3270 int col; /* column for message; non-zero when in insert
3271 mode and 'showmode' is on */
3272{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003273 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 if (curbuf->b_did_warn == FALSE
3276 && curbufIsChanged() == 0
3277#ifdef FEAT_AUTOCMD
3278 && !autocmd_busy
3279#endif
3280 && curbuf->b_p_ro)
3281 {
3282#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003283 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003285 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (!curbuf->b_p_ro)
3287 return;
3288#endif
3289 /*
3290 * Do what msg() does, but with a column offset if the warning should
3291 * be after the mode message.
3292 */
3293 msg_start();
3294 if (msg_row == Rows - 1)
3295 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003296 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003297 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3298#ifdef FEAT_EVAL
3299 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 msg_clr_eos();
3302 (void)msg_end();
3303 if (msg_silent == 0 && !silent_mode)
3304 {
3305 out_flush();
3306 ui_delay(1000L, TRUE); /* give the user time to think about it */
3307 }
3308 curbuf->b_did_warn = TRUE;
3309 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3310 if (msg_row < Rows - 1)
3311 showmode();
3312 }
3313}
3314
3315/*
3316 * Ask for a reply from the user, a 'y' or a 'n'.
3317 * No other characters are accepted, the message is repeated until a valid
3318 * reply is entered or CTRL-C is hit.
3319 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3320 * from any buffers but directly from the user.
3321 *
3322 * return the 'y' or 'n'
3323 */
3324 int
3325ask_yesno(str, direct)
3326 char_u *str;
3327 int direct;
3328{
3329 int r = ' ';
3330 int save_State = State;
3331
3332 if (exiting) /* put terminal in raw mode for this question */
3333 settmode(TMODE_RAW);
3334 ++no_wait_return;
3335#ifdef USE_ON_FLY_SCROLL
3336 dont_scroll = TRUE; /* disallow scrolling here */
3337#endif
3338 State = CONFIRM; /* mouse behaves like with :confirm */
3339#ifdef FEAT_MOUSE
3340 setmouse(); /* disables mouse for xterm */
3341#endif
3342 ++no_mapping;
3343 ++allow_keys; /* no mapping here, but recognize keys */
3344
3345 while (r != 'y' && r != 'n')
3346 {
3347 /* same highlighting as for wait_return */
3348 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3349 if (direct)
3350 r = get_keystroke();
3351 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003352 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (r == Ctrl_C || r == ESC)
3354 r = 'n';
3355 msg_putchar(r); /* show what you typed */
3356 out_flush();
3357 }
3358 --no_wait_return;
3359 State = save_State;
3360#ifdef FEAT_MOUSE
3361 setmouse();
3362#endif
3363 --no_mapping;
3364 --allow_keys;
3365
3366 return r;
3367}
3368
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003369#if defined(FEAT_MOUSE) || defined(PROTO)
3370/*
3371 * Return TRUE if "c" is a mouse key.
3372 */
3373 int
3374is_mouse_key(c)
3375 int c;
3376{
3377 return c == K_LEFTMOUSE
3378 || c == K_LEFTMOUSE_NM
3379 || c == K_LEFTDRAG
3380 || c == K_LEFTRELEASE
3381 || c == K_LEFTRELEASE_NM
3382 || c == K_MIDDLEMOUSE
3383 || c == K_MIDDLEDRAG
3384 || c == K_MIDDLERELEASE
3385 || c == K_RIGHTMOUSE
3386 || c == K_RIGHTDRAG
3387 || c == K_RIGHTRELEASE
3388 || c == K_MOUSEDOWN
3389 || c == K_MOUSEUP
3390 || c == K_MOUSELEFT
3391 || c == K_MOUSERIGHT
3392 || c == K_X1MOUSE
3393 || c == K_X1DRAG
3394 || c == K_X1RELEASE
3395 || c == K_X2MOUSE
3396 || c == K_X2DRAG
3397 || c == K_X2RELEASE;
3398}
3399#endif
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401/*
3402 * Get a key stroke directly from the user.
3403 * Ignores mouse clicks and scrollbar events, except a click for the left
3404 * button (used at the more prompt).
3405 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3406 * Disadvantage: typeahead is ignored.
3407 * Translates the interrupt character for unix to ESC.
3408 */
3409 int
3410get_keystroke()
3411{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003412 char_u *buf = NULL;
3413 int buflen = 150;
3414 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 int len = 0;
3416 int n;
3417 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003418 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 mapped_ctrl_c = FALSE; /* mappings are not used here */
3421 for (;;)
3422 {
3423 cursor_on();
3424 out_flush();
3425
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003426 /* Leave some room for check_termcode() to insert a key code into (max
3427 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3428 * bytes. */
3429 maxlen = (buflen - 6 - len) / 3;
3430 if (buf == NULL)
3431 buf = alloc(buflen);
3432 else if (maxlen < 10)
3433 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003434 char_u *t_buf = buf;
3435
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003436 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003437 * escape sequence. */
3438 buflen += 100;
3439 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003440 if (buf == NULL)
3441 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003442 maxlen = (buflen - 6 - len) / 3;
3443 }
3444 if (buf == NULL)
3445 {
3446 do_outofmem_msg((long_u)buflen);
3447 return ESC; /* panic! */
3448 }
3449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003451 * terminal code to complete. */
3452 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (n > 0)
3454 {
3455 /* Replace zero and CSI by a special key code. */
3456 n = fix_input_buffer(buf + len, n, FALSE);
3457 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003458 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003460 else if (len > 0)
3461 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar4395a712006-09-05 18:57:57 +00003463 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003464 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003465 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003467
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003468 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003469 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003470 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003471 {
3472 /* Redrawing was postponed, do it now. */
3473 update_screen(0);
3474 setcursor(); /* put cursor back where it belongs */
3475 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003476 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003477 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003478 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003480 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 continue;
3482
3483 /* Handle modifier and/or special key code. */
3484 n = buf[0];
3485 if (n == K_SPECIAL)
3486 {
3487 n = TO_SPECIAL(buf[1], buf[2]);
3488 if (buf[1] == KS_MODIFIER
3489 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003490#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003491 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003492#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003493#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 || n == K_VER_SCROLLBAR
3495 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496#endif
3497 )
3498 {
3499 if (buf[1] == KS_MODIFIER)
3500 mod_mask = buf[2];
3501 len -= 3;
3502 if (len > 0)
3503 mch_memmove(buf, buf + 3, (size_t)len);
3504 continue;
3505 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003506 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508#ifdef FEAT_MBYTE
3509 if (has_mbyte)
3510 {
3511 if (MB_BYTE2LEN(n) > len)
3512 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003513 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 n = (*mb_ptr2char)(buf);
3515 }
3516#endif
3517#ifdef UNIX
3518 if (n == intr_char)
3519 n = ESC;
3520#endif
3521 break;
3522 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003523 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 mapped_ctrl_c = save_mapped_ctrl_c;
3526 return n;
3527}
3528
3529/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003530 * Get a number from the user.
3531 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003534get_number(colon, mouse_used)
3535 int colon; /* allow colon to abort */
3536 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
3538 int n = 0;
3539 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003540 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003542 if (mouse_used != NULL)
3543 *mouse_used = FALSE;
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* When not printing messages, the user won't know what to type, return a
3546 * zero (as if CR was hit). */
3547 if (msg_silent != 0)
3548 return 0;
3549
3550#ifdef USE_ON_FLY_SCROLL
3551 dont_scroll = TRUE; /* disallow scrolling here */
3552#endif
3553 ++no_mapping;
3554 ++allow_keys; /* no mapping here, but recognize keys */
3555 for (;;)
3556 {
3557 windgoto(msg_row, msg_col);
3558 c = safe_vgetc();
3559 if (VIM_ISDIGIT(c))
3560 {
3561 n = n * 10 + c - '0';
3562 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003563 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3566 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003567 if (typed > 0)
3568 {
3569 MSG_PUTS("\b \b");
3570 --typed;
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003574#ifdef FEAT_MOUSE
3575 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3576 {
3577 *mouse_used = TRUE;
3578 n = mouse_row + 1;
3579 break;
3580 }
3581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 else if (n == 0 && c == ':' && colon)
3583 {
3584 stuffcharReadbuff(':');
3585 if (!exmode_active)
3586 cmdline_row = msg_row;
3587 skip_redraw = TRUE; /* skip redraw once */
3588 do_redraw = FALSE;
3589 break;
3590 }
3591 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3592 break;
3593 }
3594 --no_mapping;
3595 --allow_keys;
3596 return n;
3597}
3598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003599/*
3600 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003601 * When "mouse_used" is not NULL allow using the mouse and in that case return
3602 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 */
3604 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003605prompt_for_number(mouse_used)
3606 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003607{
3608 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003609 int save_cmdline_row;
3610 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003611
3612 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003613 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003614 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003615 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003616 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003617
Bram Moolenaar203335e2006-09-03 14:35:42 +00003618 /* Set the state such that text can be selected/copied/pasted and we still
3619 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003620 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003621 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003622 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003623 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003624
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003625 i = get_number(TRUE, mouse_used);
3626 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003627 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003628 /* don't call wait_return() now */
3629 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003630 cmdline_row = msg_row - 1;
3631 need_wait_return = FALSE;
3632 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003633 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003634 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003635 else
3636 cmdline_row = save_cmdline_row;
3637 State = save_State;
3638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003639 return i;
3640}
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 void
3643msgmore(n)
3644 long n;
3645{
3646 long pn;
3647
3648 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3650 return;
3651
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003652 /* We don't want to overwrite another important message, but do overwrite
3653 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3654 * then "put" reports the last action. */
3655 if (keep_msg != NULL && !keep_msg_more)
3656 return;
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (n > 0)
3659 pn = n;
3660 else
3661 pn = -n;
3662
3663 if (pn > p_report)
3664 {
3665 if (pn == 1)
3666 {
3667 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003668 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3669 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003671 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3672 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
3674 else
3675 {
3676 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003677 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3678 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003680 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3681 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003684 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (msg(msg_buf))
3686 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003687 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003688 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 }
3691}
3692
3693/*
3694 * flush map and typeahead buffers and give a warning for an error
3695 */
3696 void
3697beep_flush()
3698{
3699 if (emsg_silent == 0)
3700 {
3701 flush_buffers(FALSE);
3702 vim_beep();
3703 }
3704}
3705
3706/*
3707 * give a warning for an error
3708 */
3709 void
3710vim_beep()
3711{
3712 if (emsg_silent == 0)
3713 {
3714 if (p_vb
3715#ifdef FEAT_GUI
3716 /* While the GUI is starting up the termcap is set for the GUI
3717 * but the output still goes to a terminal. */
3718 && !(gui.in_use && gui.starting)
3719#endif
3720 )
3721 {
3722 out_str(T_VB);
3723 }
3724 else
3725 {
3726#ifdef MSDOS
3727 /*
3728 * The number of beeps outputted is reduced to avoid having to wait
3729 * for all the beeps to finish. This is only a problem on systems
3730 * where the beeps don't overlap.
3731 */
3732 if (beep_count == 0 || beep_count == 10)
3733 {
3734 out_char(BELL);
3735 beep_count = 1;
3736 }
3737 else
3738 ++beep_count;
3739#else
3740 out_char(BELL);
3741#endif
3742 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003743
3744 /* When 'verbose' is set and we are sourcing a script or executing a
3745 * function give the user a hint where the beep comes from. */
3746 if (vim_strchr(p_debug, 'e') != NULL)
3747 {
3748 msg_source(hl_attr(HLF_W));
3749 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752}
3753
3754/*
3755 * To get the "real" home directory:
3756 * - get value of $HOME
3757 * For Unix:
3758 * - go to that directory
3759 * - do mch_dirname() to get the real name of that directory.
3760 * This also works with mounts and links.
3761 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3762 */
3763static char_u *homedir = NULL;
3764
3765 void
3766init_homedir()
3767{
3768 char_u *var;
3769
Bram Moolenaar05159a02005-02-26 23:04:13 +00003770 /* In case we are called a second time (when 'encoding' changes). */
3771 vim_free(homedir);
3772 homedir = NULL;
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774#ifdef VMS
3775 var = mch_getenv((char_u *)"SYS$LOGIN");
3776#else
3777 var = mch_getenv((char_u *)"HOME");
3778#endif
3779
3780 if (var != NULL && *var == NUL) /* empty is same as not set */
3781 var = NULL;
3782
3783#ifdef WIN3264
3784 /*
3785 * Weird but true: $HOME may contain an indirect reference to another
3786 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3787 * when $HOME is being set.
3788 */
3789 if (var != NULL && *var == '%')
3790 {
3791 char_u *p;
3792 char_u *exp;
3793
3794 p = vim_strchr(var + 1, '%');
3795 if (p != NULL)
3796 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003797 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 exp = mch_getenv(NameBuff);
3799 if (exp != NULL && *exp != NUL
3800 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3801 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003802 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 var = NameBuff;
3804 /* Also set $HOME, it's needed for _viminfo. */
3805 vim_setenv((char_u *)"HOME", NameBuff);
3806 }
3807 }
3808 }
3809
3810 /*
3811 * Typically, $HOME is not defined on Windows, unless the user has
3812 * specifically defined it for Vim's sake. However, on Windows NT
3813 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3814 * each user. Try constructing $HOME from these.
3815 */
3816 if (var == NULL)
3817 {
3818 char_u *homedrive, *homepath;
3819
3820 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3821 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003822 if (homepath == NULL || *homepath == NUL)
3823 homepath = "\\";
3824 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3826 {
3827 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3828 if (NameBuff[0] != NUL)
3829 {
3830 var = NameBuff;
3831 /* Also set $HOME, it's needed for _viminfo. */
3832 vim_setenv((char_u *)"HOME", NameBuff);
3833 }
3834 }
3835 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003836
3837# if defined(FEAT_MBYTE)
3838 if (enc_utf8 && var != NULL)
3839 {
3840 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003841 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003842
3843 /* Convert from active codepage to UTF-8. Other conversions are
3844 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003845 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003846 if (pp != NULL)
3847 {
3848 homedir = pp;
3849 return;
3850 }
3851 }
3852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853#endif
3854
3855#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3856 /*
3857 * Default home dir is C:/
3858 * Best assumption we can make in such a situation.
3859 */
3860 if (var == NULL)
3861 var = "C:/";
3862#endif
3863 if (var != NULL)
3864 {
3865#ifdef UNIX
3866 /*
3867 * Change to the directory and get the actual path. This resolves
3868 * links. Don't do it when we can't return.
3869 */
3870 if (mch_dirname(NameBuff, MAXPATHL) == OK
3871 && mch_chdir((char *)NameBuff) == 0)
3872 {
3873 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3874 var = IObuff;
3875 if (mch_chdir((char *)NameBuff) != 0)
3876 EMSG(_(e_prev_dir));
3877 }
3878#endif
3879 homedir = vim_strsave(var);
3880 }
3881}
3882
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003883#if defined(EXITFREE) || defined(PROTO)
3884 void
3885free_homedir()
3886{
3887 vim_free(homedir);
3888}
Bram Moolenaar24305862012-08-15 14:05:05 +02003889
3890# ifdef FEAT_CMDL_COMPL
3891 void
3892free_users()
3893{
3894 ga_clear_strings(&ga_users);
3895}
3896# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003897#endif
3898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003900 * Call expand_env() and store the result in an allocated string.
3901 * This is not very memory efficient, this expects the result to be freed
3902 * again soon.
3903 */
3904 char_u *
3905expand_env_save(src)
3906 char_u *src;
3907{
3908 return expand_env_save_opt(src, FALSE);
3909}
3910
3911/*
3912 * Idem, but when "one" is TRUE handle the string as one file name, only
3913 * expand "~" at the start.
3914 */
3915 char_u *
3916expand_env_save_opt(src, one)
3917 char_u *src;
3918 int one;
3919{
3920 char_u *p;
3921
3922 p = alloc(MAXPATHL);
3923 if (p != NULL)
3924 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3925 return p;
3926}
3927
3928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 * Expand environment variable with path name.
3930 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003931 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 * If anything fails no expansion is done and dst equals src.
3933 */
3934 void
3935expand_env(src, dst, dstlen)
3936 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3937 char_u *dst; /* where to put the result */
3938 int dstlen; /* maximum length of the result */
3939{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003940 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941}
3942
3943 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003944expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003945 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 char_u *dst; /* where to put the result */
3947 int dstlen; /* maximum length of the result */
3948 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003949 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003950 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003952 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 char_u *tail;
3954 int c;
3955 char_u *var;
3956 int copy_char;
3957 int mustfree; /* var was allocated, need to free it later */
3958 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003959 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003961 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003962 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003963
3964 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 --dstlen; /* leave one char space for "\," */
3966 while (*src && dstlen > 0)
3967 {
3968 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003969 if ((*src == '$'
3970#ifdef VMS
3971 && at_start
3972#endif
3973 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3975 || *src == '%'
3976#endif
3977 || (*src == '~' && at_start))
3978 {
3979 mustfree = FALSE;
3980
3981 /*
3982 * The variable name is copied into dst temporarily, because it may
3983 * be a string in read-only memory and a NUL needs to be appended.
3984 */
3985 if (*src != '~') /* environment var */
3986 {
3987 tail = src + 1;
3988 var = dst;
3989 c = dstlen - 1;
3990
3991#ifdef UNIX
3992 /* Unix has ${var-name} type environment vars */
3993 if (*tail == '{' && !vim_isIDc('{'))
3994 {
3995 tail++; /* ignore '{' */
3996 while (c-- > 0 && *tail && *tail != '}')
3997 *var++ = *tail++;
3998 }
3999 else
4000#endif
4001 {
4002 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
4003#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4004 || (*src == '%' && *tail != '%')
4005#endif
4006 ))
4007 {
4008#ifdef OS2 /* env vars only in uppercase */
4009 *var++ = TOUPPER_LOC(*tail);
4010 tail++; /* toupper() may be a macro! */
4011#else
4012 *var++ = *tail++;
4013#endif
4014 }
4015 }
4016
4017#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
4018# ifdef UNIX
4019 if (src[1] == '{' && *tail != '}')
4020# else
4021 if (*src == '%' && *tail != '%')
4022# endif
4023 var = NULL;
4024 else
4025 {
4026# ifdef UNIX
4027 if (src[1] == '{')
4028# else
4029 if (*src == '%')
4030#endif
4031 ++tail;
4032#endif
4033 *var = NUL;
4034 var = vim_getenv(dst, &mustfree);
4035#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
4036 }
4037#endif
4038 }
4039 /* home directory */
4040 else if ( src[1] == NUL
4041 || vim_ispathsep(src[1])
4042 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4043 {
4044 var = homedir;
4045 tail = src + 1;
4046 }
4047 else /* user directory */
4048 {
4049#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4050 /*
4051 * Copy ~user to dst[], so we can put a NUL after it.
4052 */
4053 tail = src;
4054 var = dst;
4055 c = dstlen - 1;
4056 while ( c-- > 0
4057 && *tail
4058 && vim_isfilec(*tail)
4059 && !vim_ispathsep(*tail))
4060 *var++ = *tail++;
4061 *var = NUL;
4062# ifdef UNIX
4063 /*
4064 * If the system supports getpwnam(), use it.
4065 * Otherwise, or if getpwnam() fails, the shell is used to
4066 * expand ~user. This is slower and may fail if the shell
4067 * does not support ~user (old versions of /bin/sh).
4068 */
4069# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4070 {
4071 struct passwd *pw;
4072
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004073 /* Note: memory allocated by getpwnam() is never freed.
4074 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 pw = getpwnam((char *)dst + 1);
4076 if (pw != NULL)
4077 var = (char_u *)pw->pw_dir;
4078 else
4079 var = NULL;
4080 }
4081 if (var == NULL)
4082# endif
4083 {
4084 expand_T xpc;
4085
4086 ExpandInit(&xpc);
4087 xpc.xp_context = EXPAND_FILES;
4088 var = ExpandOne(&xpc, dst, NULL,
4089 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 mustfree = TRUE;
4091 }
4092
4093# else /* !UNIX, thus VMS */
4094 /*
4095 * USER_HOME is a comma-separated list of
4096 * directories to search for the user account in.
4097 */
4098 {
4099 char_u test[MAXPATHL], paths[MAXPATHL];
4100 char_u *path, *next_path, *ptr;
4101 struct stat st;
4102
4103 STRCPY(paths, USER_HOME);
4104 next_path = paths;
4105 while (*next_path)
4106 {
4107 for (path = next_path; *next_path && *next_path != ',';
4108 next_path++);
4109 if (*next_path)
4110 *next_path++ = NUL;
4111 STRCPY(test, path);
4112 STRCAT(test, "/");
4113 STRCAT(test, dst + 1);
4114 if (mch_stat(test, &st) == 0)
4115 {
4116 var = alloc(STRLEN(test) + 1);
4117 STRCPY(var, test);
4118 mustfree = TRUE;
4119 break;
4120 }
4121 }
4122 }
4123# endif /* UNIX */
4124#else
4125 /* cannot expand user's home directory, so don't try */
4126 var = NULL;
4127 tail = (char_u *)""; /* for gcc */
4128#endif /* UNIX || VMS */
4129 }
4130
4131#ifdef BACKSLASH_IN_FILENAME
4132 /* If 'shellslash' is set change backslashes to forward slashes.
4133 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4134 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4135 {
4136 char_u *p = vim_strsave(var);
4137
4138 if (p != NULL)
4139 {
4140 if (mustfree)
4141 vim_free(var);
4142 var = p;
4143 mustfree = TRUE;
4144 forward_slash(var);
4145 }
4146 }
4147#endif
4148
4149 /* If "var" contains white space, escape it with a backslash.
4150 * Required for ":e ~/tt" when $HOME includes a space. */
4151 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4152 {
4153 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4154
4155 if (p != NULL)
4156 {
4157 if (mustfree)
4158 vim_free(var);
4159 var = p;
4160 mustfree = TRUE;
4161 }
4162 }
4163
4164 if (var != NULL && *var != NUL
4165 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4166 {
4167 STRCPY(dst, var);
4168 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004169 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 /* if var[] ends in a path separator and tail[] starts
4171 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004172 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4174 && dst[-1] != ':'
4175#endif
4176 && vim_ispathsep(*tail))
4177 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004178 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 src = tail;
4180 copy_char = FALSE;
4181 }
4182 if (mustfree)
4183 vim_free(var);
4184 }
4185
4186 if (copy_char) /* copy at least one char */
4187 {
4188 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004189 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004190 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4191 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 */
4193 at_start = FALSE;
4194 if (src[0] == '\\' && src[1] != NUL)
4195 {
4196 *dst++ = *src++;
4197 --dstlen;
4198 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004199 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 at_start = TRUE;
4201 *dst++ = *src++;
4202 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004203
4204 if (startstr != NULL && src - startstr_len >= srcp
4205 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4206 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208 }
4209 *dst = NUL;
4210}
4211
4212/*
4213 * Vim's version of getenv().
4214 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004215 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004216 * "mustfree" is set to TRUE when returned is allocated, it must be
4217 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 */
4219 char_u *
4220vim_getenv(name, mustfree)
4221 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004222 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
4224 char_u *p;
4225 char_u *pend;
4226 int vimruntime;
4227
4228#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4229 /* use "C:/" when $HOME is not set */
4230 if (STRCMP(name, "HOME") == 0)
4231 return homedir;
4232#endif
4233
4234 p = mch_getenv(name);
4235 if (p != NULL && *p == NUL) /* empty is the same as not set */
4236 p = NULL;
4237
4238 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004239 {
4240#if defined(FEAT_MBYTE) && defined(WIN3264)
4241 if (enc_utf8)
4242 {
4243 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004244 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004245
4246 /* Convert from active codepage to UTF-8. Other conversions are
4247 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004248 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004249 if (pp != NULL)
4250 {
4251 p = pp;
4252 *mustfree = TRUE;
4253 }
4254 }
4255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4260 if (!vimruntime && STRCMP(name, "VIM") != 0)
4261 return NULL;
4262
4263 /*
4264 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4265 * Don't do this when default_vimruntime_dir is non-empty.
4266 */
4267 if (vimruntime
4268#ifdef HAVE_PATHDEF
4269 && *default_vimruntime_dir == NUL
4270#endif
4271 )
4272 {
4273 p = mch_getenv((char_u *)"VIM");
4274 if (p != NULL && *p == NUL) /* empty is the same as not set */
4275 p = NULL;
4276 if (p != NULL)
4277 {
4278 p = vim_version_dir(p);
4279 if (p != NULL)
4280 *mustfree = TRUE;
4281 else
4282 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004283
4284#if defined(FEAT_MBYTE) && defined(WIN3264)
4285 if (enc_utf8)
4286 {
4287 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004288 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004289
4290 /* Convert from active codepage to UTF-8. Other conversions
4291 * are not done, because they would fail for non-ASCII
4292 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004293 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004294 if (pp != NULL)
4295 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004296 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004297 vim_free(p);
4298 p = pp;
4299 *mustfree = TRUE;
4300 }
4301 }
4302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 }
4305
4306 /*
4307 * When expanding $VIM or $VIMRUNTIME fails, try using:
4308 * - the directory name from 'helpfile' (unless it contains '$')
4309 * - the executable name from argv[0]
4310 */
4311 if (p == NULL)
4312 {
4313 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4314 p = p_hf;
4315#ifdef USE_EXE_NAME
4316 /*
4317 * Use the name of the executable, obtained from argv[0].
4318 */
4319 else
4320 p = exe_name;
4321#endif
4322 if (p != NULL)
4323 {
4324 /* remove the file name */
4325 pend = gettail(p);
4326
4327 /* remove "doc/" from 'helpfile', if present */
4328 if (p == p_hf)
4329 pend = remove_tail(p, pend, (char_u *)"doc");
4330
4331#ifdef USE_EXE_NAME
4332# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004333 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 if (p == exe_name)
4335 {
4336 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004337 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004339 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4340 if (pend1 != pend)
4341 {
4342 pnew = alloc((unsigned)(pend1 - p) + 15);
4343 if (pnew != NULL)
4344 {
4345 STRNCPY(pnew, p, (pend1 - p));
4346 STRCPY(pnew + (pend1 - p), "Resources/vim");
4347 p = pnew;
4348 pend = p + STRLEN(p);
4349 }
4350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352# endif
4353 /* remove "src/" from exe_name, if present */
4354 if (p == exe_name)
4355 pend = remove_tail(p, pend, (char_u *)"src");
4356#endif
4357
4358 /* for $VIM, remove "runtime/" or "vim54/", if present */
4359 if (!vimruntime)
4360 {
4361 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4362 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4363 }
4364
4365 /* remove trailing path separator */
4366#ifndef MACOS_CLASSIC
4367 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004368 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004369 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 --pend;
4371#endif
4372
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004373#ifdef MACOS_X
4374 if (p == exe_name || p == p_hf)
4375#endif
4376 /* check that the result is a directory name */
4377 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 if (p != NULL && !mch_isdir(p))
4380 {
4381 vim_free(p);
4382 p = NULL;
4383 }
4384 else
4385 {
4386#ifdef USE_EXE_NAME
4387 /* may add "/vim54" or "/runtime" if it exists */
4388 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4389 {
4390 vim_free(p);
4391 p = pend;
4392 }
4393#endif
4394 *mustfree = TRUE;
4395 }
4396 }
4397 }
4398
4399#ifdef HAVE_PATHDEF
4400 /* When there is a pathdef.c file we can use default_vim_dir and
4401 * default_vimruntime_dir */
4402 if (p == NULL)
4403 {
4404 /* Only use default_vimruntime_dir when it is not empty */
4405 if (vimruntime && *default_vimruntime_dir != NUL)
4406 {
4407 p = default_vimruntime_dir;
4408 *mustfree = FALSE;
4409 }
4410 else if (*default_vim_dir != NUL)
4411 {
4412 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4413 *mustfree = TRUE;
4414 else
4415 {
4416 p = default_vim_dir;
4417 *mustfree = FALSE;
4418 }
4419 }
4420 }
4421#endif
4422
4423 /*
4424 * Set the environment variable, so that the new value can be found fast
4425 * next time, and others can also use it (e.g. Perl).
4426 */
4427 if (p != NULL)
4428 {
4429 if (vimruntime)
4430 {
4431 vim_setenv((char_u *)"VIMRUNTIME", p);
4432 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 else
4435 {
4436 vim_setenv((char_u *)"VIM", p);
4437 didset_vim = TRUE;
4438 }
4439 }
4440 return p;
4441}
4442
4443/*
4444 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4445 * Return NULL if not, return its name in allocated memory otherwise.
4446 */
4447 static char_u *
4448vim_version_dir(vimdir)
4449 char_u *vimdir;
4450{
4451 char_u *p;
4452
4453 if (vimdir == NULL || *vimdir == NUL)
4454 return NULL;
4455 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4456 if (p != NULL && mch_isdir(p))
4457 return p;
4458 vim_free(p);
4459 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4460 if (p != NULL && mch_isdir(p))
4461 return p;
4462 vim_free(p);
4463 return NULL;
4464}
4465
4466/*
4467 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4468 * the length of "name/". Otherwise return "pend".
4469 */
4470 static char_u *
4471remove_tail(p, pend, name)
4472 char_u *p;
4473 char_u *pend;
4474 char_u *name;
4475{
4476 int len = (int)STRLEN(name) + 1;
4477 char_u *newend = pend - len;
4478
4479 if (newend >= p
4480 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004481 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 return newend;
4483 return pend;
4484}
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 * Our portable version of setenv.
4488 */
4489 void
4490vim_setenv(name, val)
4491 char_u *name;
4492 char_u *val;
4493{
4494#ifdef HAVE_SETENV
4495 mch_setenv((char *)name, (char *)val, 1);
4496#else
4497 char_u *envbuf;
4498
4499 /*
4500 * Putenv does not copy the string, it has to remain
4501 * valid. The allocated memory will never be freed.
4502 */
4503 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4504 if (envbuf != NULL)
4505 {
4506 sprintf((char *)envbuf, "%s=%s", name, val);
4507 putenv((char *)envbuf);
4508 }
4509#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004510#ifdef FEAT_GETTEXT
4511 /*
4512 * When setting $VIMRUNTIME adjust the directory to find message
4513 * translations to $VIMRUNTIME/lang.
4514 */
4515 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4516 {
4517 char_u *buf = concat_str(val, (char_u *)"/lang");
4518
4519 if (buf != NULL)
4520 {
4521 bindtextdomain(VIMPACKAGE, (char *)buf);
4522 vim_free(buf);
4523 }
4524 }
4525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526}
4527
4528#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4529/*
4530 * Function given to ExpandGeneric() to obtain an environment variable name.
4531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 char_u *
4533get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004534 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 int idx;
4536{
4537# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4538 /*
4539 * No environ[] on the Amiga and on the Mac (using MPW).
4540 */
4541 return NULL;
4542# else
4543# ifndef __WIN32__
4544 /* Borland C++ 5.2 has this in a header file. */
4545 extern char **environ;
4546# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004547# define ENVNAMELEN 100
4548 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 char_u *str;
4550 int n;
4551
4552 str = (char_u *)environ[idx];
4553 if (str == NULL)
4554 return NULL;
4555
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004556 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
4558 if (str[n] == '=' || str[n] == NUL)
4559 break;
4560 name[n] = str[n];
4561 }
4562 name[n] = NUL;
4563 return name;
4564# endif
4565}
Bram Moolenaar24305862012-08-15 14:05:05 +02004566
4567/*
4568 * Find all user names for user completion.
4569 * Done only once and then cached.
4570 */
4571 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004572init_users()
4573{
Bram Moolenaar24305862012-08-15 14:05:05 +02004574 static int lazy_init_done = FALSE;
4575
4576 if (lazy_init_done)
4577 return;
4578
4579 lazy_init_done = TRUE;
4580 ga_init2(&ga_users, sizeof(char_u *), 20);
4581
4582# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4583 {
4584 char_u* user;
4585 struct passwd* pw;
4586
4587 setpwent();
4588 while ((pw = getpwent()) != NULL)
4589 /* pw->pw_name shouldn't be NULL but just in case... */
4590 if (pw->pw_name != NULL)
4591 {
4592 if (ga_grow(&ga_users, 1) == FAIL)
4593 break;
4594 user = vim_strsave((char_u*)pw->pw_name);
4595 if (user == NULL)
4596 break;
4597 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4598 }
4599 endpwent();
4600 }
4601# endif
4602}
4603
4604/*
4605 * Function given to ExpandGeneric() to obtain an user names.
4606 */
4607 char_u*
4608get_users(xp, idx)
4609 expand_T *xp UNUSED;
4610 int idx;
4611{
4612 init_users();
4613 if (idx < ga_users.ga_len)
4614 return ((char_u **)ga_users.ga_data)[idx];
4615 return NULL;
4616}
4617
4618/*
4619 * Check whether name matches a user name. Return:
4620 * 0 if name does not match any user name.
4621 * 1 if name partially matches the beginning of a user name.
4622 * 2 is name fully matches a user name.
4623 */
4624int match_user(name)
4625 char_u* name;
4626{
4627 int i;
4628 int n = (int)STRLEN(name);
4629 int result = 0;
4630
4631 init_users();
4632 for (i = 0; i < ga_users.ga_len; i++)
4633 {
4634 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4635 return 2; /* full match */
4636 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4637 result = 1; /* partial match */
4638 }
4639 return result;
4640}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641#endif
4642
4643/*
4644 * Replace home directory by "~" in each space or comma separated file name in
4645 * 'src'.
4646 * If anything fails (except when out of space) dst equals src.
4647 */
4648 void
4649home_replace(buf, src, dst, dstlen, one)
4650 buf_T *buf; /* when not NULL, check for help files */
4651 char_u *src; /* input file name */
4652 char_u *dst; /* where to put the result */
4653 int dstlen; /* maximum length of the result */
4654 int one; /* if TRUE, only replace one file name, include
4655 spaces and commas in the file name. */
4656{
4657 size_t dirlen = 0, envlen = 0;
4658 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004659 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 char_u *p;
4661
4662 if (src == NULL)
4663 {
4664 *dst = NUL;
4665 return;
4666 }
4667
4668 /*
4669 * If the file is a help file, remove the path completely.
4670 */
4671 if (buf != NULL && buf->b_help)
4672 {
4673 STRCPY(dst, gettail(src));
4674 return;
4675 }
4676
4677 /*
4678 * We check both the value of the $HOME environment variable and the
4679 * "real" home directory.
4680 */
4681 if (homedir != NULL)
4682 dirlen = STRLEN(homedir);
4683
4684#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004685 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004687 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4688#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004689 /* Empty is the same as not set. */
4690 if (homedir_env != NULL && *homedir_env == NUL)
4691 homedir_env = NULL;
4692
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004693#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004694 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004695 {
4696 int usedlen = 0;
4697 int flen;
4698 char_u *fbuf = NULL;
4699
4700 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004701 (void)modify_fname((char_u *)":p", &usedlen,
4702 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004703 flen = (int)STRLEN(homedir_env);
4704 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4705 /* Remove the trailing / that is added to a directory. */
4706 homedir_env[flen - 1] = NUL;
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708#endif
4709
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (homedir_env != NULL)
4711 envlen = STRLEN(homedir_env);
4712
4713 if (!one)
4714 src = skipwhite(src);
4715 while (*src && dstlen > 0)
4716 {
4717 /*
4718 * Here we are at the beginning of a file name.
4719 * First, check to see if the beginning of the file name matches
4720 * $HOME or the "real" home directory. Check that there is a '/'
4721 * after the match (so that if e.g. the file is "/home/pieter/bla",
4722 * and the home directory is "/home/piet", the file does not end up
4723 * as "~er/bla" (which would seem to indicate the file "bla" in user
4724 * er's home directory)).
4725 */
4726 p = homedir;
4727 len = dirlen;
4728 for (;;)
4729 {
4730 if ( len
4731 && fnamencmp(src, p, len) == 0
4732 && (vim_ispathsep(src[len])
4733 || (!one && (src[len] == ',' || src[len] == ' '))
4734 || src[len] == NUL))
4735 {
4736 src += len;
4737 if (--dstlen > 0)
4738 *dst++ = '~';
4739
4740 /*
4741 * If it's just the home directory, add "/".
4742 */
4743 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4744 *dst++ = '/';
4745 break;
4746 }
4747 if (p == homedir_env)
4748 break;
4749 p = homedir_env;
4750 len = envlen;
4751 }
4752
4753 /* if (!one) skip to separator: space or comma */
4754 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4755 *dst++ = *src++;
4756 /* skip separator */
4757 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4758 *dst++ = *src++;
4759 }
4760 /* if (dstlen == 0) out of space, what to do??? */
4761
4762 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004763
4764 if (homedir_env != homedir_env_orig)
4765 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766}
4767
4768/*
4769 * Like home_replace, store the replaced string in allocated memory.
4770 * When something fails, NULL is returned.
4771 */
4772 char_u *
4773home_replace_save(buf, src)
4774 buf_T *buf; /* when not NULL, check for help files */
4775 char_u *src; /* input file name */
4776{
4777 char_u *dst;
4778 unsigned len;
4779
4780 len = 3; /* space for "~/" and trailing NUL */
4781 if (src != NULL) /* just in case */
4782 len += (unsigned)STRLEN(src);
4783 dst = alloc(len);
4784 if (dst != NULL)
4785 home_replace(buf, src, dst, len, TRUE);
4786 return dst;
4787}
4788
4789/*
4790 * Compare two file names and return:
4791 * FPC_SAME if they both exist and are the same file.
4792 * FPC_SAMEX if they both don't exist and have the same file name.
4793 * FPC_DIFF if they both exist and are different files.
4794 * FPC_NOTX if they both don't exist.
4795 * FPC_DIFFX if one of them doesn't exist.
4796 * For the first name environment variables are expanded
4797 */
4798 int
4799fullpathcmp(s1, s2, checkname)
4800 char_u *s1, *s2;
4801 int checkname; /* when both don't exist, check file names */
4802{
4803#ifdef UNIX
4804 char_u exp1[MAXPATHL];
4805 char_u full1[MAXPATHL];
4806 char_u full2[MAXPATHL];
4807 struct stat st1, st2;
4808 int r1, r2;
4809
4810 expand_env(s1, exp1, MAXPATHL);
4811 r1 = mch_stat((char *)exp1, &st1);
4812 r2 = mch_stat((char *)s2, &st2);
4813 if (r1 != 0 && r2 != 0)
4814 {
4815 /* if mch_stat() doesn't work, may compare the names */
4816 if (checkname)
4817 {
4818 if (fnamecmp(exp1, s2) == 0)
4819 return FPC_SAMEX;
4820 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4821 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4822 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4823 return FPC_SAMEX;
4824 }
4825 return FPC_NOTX;
4826 }
4827 if (r1 != 0 || r2 != 0)
4828 return FPC_DIFFX;
4829 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4830 return FPC_SAME;
4831 return FPC_DIFF;
4832#else
4833 char_u *exp1; /* expanded s1 */
4834 char_u *full1; /* full path of s1 */
4835 char_u *full2; /* full path of s2 */
4836 int retval = FPC_DIFF;
4837 int r1, r2;
4838
4839 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4840 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4841 {
4842 full1 = exp1 + MAXPATHL;
4843 full2 = full1 + MAXPATHL;
4844
4845 expand_env(s1, exp1, MAXPATHL);
4846 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4847 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4848
4849 /* If vim_FullName() fails, the file probably doesn't exist. */
4850 if (r1 != OK && r2 != OK)
4851 {
4852 if (checkname && fnamecmp(exp1, s2) == 0)
4853 retval = FPC_SAMEX;
4854 else
4855 retval = FPC_NOTX;
4856 }
4857 else if (r1 != OK || r2 != OK)
4858 retval = FPC_DIFFX;
4859 else if (fnamecmp(full1, full2))
4860 retval = FPC_DIFF;
4861 else
4862 retval = FPC_SAME;
4863 vim_free(exp1);
4864 }
4865 return retval;
4866#endif
4867}
4868
4869/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004870 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004871 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004872 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 */
4874 char_u *
4875gettail(fname)
4876 char_u *fname;
4877{
4878 char_u *p1, *p2;
4879
4880 if (fname == NULL)
4881 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004882 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004884 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004886 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 return p1;
4889}
4890
Bram Moolenaar31710262010-08-13 13:36:15 +02004891#if defined(FEAT_SEARCHPATH)
4892static char_u *gettail_dir __ARGS((char_u *fname));
4893
4894/*
4895 * Return the end of the directory name, on the first path
4896 * separator:
4897 * "/path/file", "/path/dir/", "/path//dir", "/file"
4898 * ^ ^ ^ ^
4899 */
4900 static char_u *
4901gettail_dir(fname)
4902 char_u *fname;
4903{
4904 char_u *dir_end = fname;
4905 char_u *next_dir_end = fname;
4906 int look_for_sep = TRUE;
4907 char_u *p;
4908
4909 for (p = fname; *p != NUL; )
4910 {
4911 if (vim_ispathsep(*p))
4912 {
4913 if (look_for_sep)
4914 {
4915 next_dir_end = p;
4916 look_for_sep = FALSE;
4917 }
4918 }
4919 else
4920 {
4921 if (!look_for_sep)
4922 dir_end = next_dir_end;
4923 look_for_sep = TRUE;
4924 }
4925 mb_ptr_adv(p);
4926 }
4927 return dir_end;
4928}
4929#endif
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004932 * Get pointer to tail of "fname", including path separators. Putting a NUL
4933 * here leaves the directory name. Takes care of "c:/" and "//".
4934 * Always returns a valid pointer.
4935 */
4936 char_u *
4937gettail_sep(fname)
4938 char_u *fname;
4939{
4940 char_u *p;
4941 char_u *t;
4942
4943 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4944 t = gettail(fname);
4945 while (t > p && after_pathsep(fname, t))
4946 --t;
4947#ifdef VMS
4948 /* path separator is part of the path */
4949 ++t;
4950#endif
4951 return t;
4952}
4953
4954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 * get the next path component (just after the next path separator).
4956 */
4957 char_u *
4958getnextcomp(fname)
4959 char_u *fname;
4960{
4961 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004962 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (*fname)
4964 ++fname;
4965 return fname;
4966}
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968/*
4969 * Get a pointer to one character past the head of a path name.
4970 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4971 * If there is no head, path is returned.
4972 */
4973 char_u *
4974get_past_head(path)
4975 char_u *path;
4976{
4977 char_u *retval;
4978
4979#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4980 /* may skip "c:" */
4981 if (isalpha(path[0]) && path[1] == ':')
4982 retval = path + 2;
4983 else
4984 retval = path;
4985#else
4986# if defined(AMIGA)
4987 /* may skip "label:" */
4988 retval = vim_strchr(path, ':');
4989 if (retval == NULL)
4990 retval = path;
4991# else /* Unix */
4992 retval = path;
4993# endif
4994#endif
4995
4996 while (vim_ispathsep(*retval))
4997 ++retval;
4998
4999 return retval;
5000}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
5002/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005003 * Return TRUE if 'c' is a path separator.
5004 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 */
5006 int
5007vim_ispathsep(c)
5008 int c;
5009{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005010#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005012#else
5013# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005015# else
5016# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5018 return (c == ':' || c == '[' || c == ']' || c == '/'
5019 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005020# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005022# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025}
5026
Bram Moolenaar69c35002013-11-04 02:54:12 +01005027/*
5028 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5029 */
5030 int
5031vim_ispathsep_nocolon(c)
5032 int c;
5033{
5034 return vim_ispathsep(c)
5035#ifdef BACKSLASH_IN_FILENAME
5036 && c != ':'
5037#endif
5038 ;
5039}
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5042/*
5043 * return TRUE if 'c' is a path list separator.
5044 */
5045 int
5046vim_ispathlistsep(c)
5047 int c;
5048{
5049#ifdef UNIX
5050 return (c == ':');
5051#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005052 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053#endif
5054}
5055#endif
5056
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005057#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5058 || defined(FEAT_EVAL) || defined(PROTO)
5059/*
5060 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5061 * It's done in-place.
5062 */
5063 void
5064shorten_dir(str)
5065 char_u *str;
5066{
5067 char_u *tail, *s, *d;
5068 int skip = FALSE;
5069
5070 tail = gettail(str);
5071 d = str;
5072 for (s = str; ; ++s)
5073 {
5074 if (s >= tail) /* copy the whole tail */
5075 {
5076 *d++ = *s;
5077 if (*s == NUL)
5078 break;
5079 }
5080 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5081 {
5082 *d++ = *s;
5083 skip = FALSE;
5084 }
5085 else if (!skip)
5086 {
5087 *d++ = *s; /* copy next char */
5088 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5089 skip = TRUE;
5090# ifdef FEAT_MBYTE
5091 if (has_mbyte)
5092 {
5093 int l = mb_ptr2len(s);
5094
5095 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005096 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005097 }
5098# endif
5099 }
5100 }
5101}
5102#endif
5103
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005104/*
5105 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5106 * Also returns TRUE if there is no directory name.
5107 * "fname" must be writable!.
5108 */
5109 int
5110dir_of_file_exists(fname)
5111 char_u *fname;
5112{
5113 char_u *p;
5114 int c;
5115 int retval;
5116
5117 p = gettail_sep(fname);
5118 if (p == fname)
5119 return TRUE;
5120 c = *p;
5121 *p = NUL;
5122 retval = mch_isdir(fname);
5123 *p = c;
5124 return retval;
5125}
5126
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005128 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5129 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 */
5131 int
5132vim_fnamecmp(x, y)
5133 char_u *x, *y;
5134{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005135#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005137#else
5138 if (p_fic)
5139 return MB_STRICMP(x, y);
5140 return STRCMP(x, y);
5141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142}
5143
5144 int
5145vim_fnamencmp(x, y, len)
5146 char_u *x, *y;
5147 size_t len;
5148{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005149#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005150 char_u *px = x;
5151 char_u *py = y;
5152 int cx = NUL;
5153 int cy = NUL;
5154
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005155 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005157 cx = PTR2CHAR(px);
5158 cy = PTR2CHAR(py);
5159 if (cx == NUL || cy == NUL
5160 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5161 && !(cx == '/' && cy == '\\')
5162 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005164 len -= MB_PTR2LEN(px);
5165 px += MB_PTR2LEN(px);
5166 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 if (len == 0)
5169 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005170 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005171#else
5172 if (p_fic)
5173 return MB_STRNICMP(x, y, len);
5174 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
5178/*
5179 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005180 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
5182 char_u *
5183concat_fnames(fname1, fname2, sep)
5184 char_u *fname1;
5185 char_u *fname2;
5186 int sep;
5187{
5188 char_u *dest;
5189
5190 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5191 if (dest != NULL)
5192 {
5193 STRCPY(dest, fname1);
5194 if (sep)
5195 add_pathsep(dest);
5196 STRCAT(dest, fname2);
5197 }
5198 return dest;
5199}
5200
Bram Moolenaard6754642005-01-17 22:18:45 +00005201/*
5202 * Concatenate two strings and return the result in allocated memory.
5203 * Returns NULL when out of memory.
5204 */
5205 char_u *
5206concat_str(str1, str2)
5207 char_u *str1;
5208 char_u *str2;
5209{
5210 char_u *dest;
5211 size_t l = STRLEN(str1);
5212
5213 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5214 if (dest != NULL)
5215 {
5216 STRCPY(dest, str1);
5217 STRCPY(dest + l, str2);
5218 }
5219 return dest;
5220}
Bram Moolenaard6754642005-01-17 22:18:45 +00005221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222/*
5223 * Add a path separator to a file name, unless it already ends in a path
5224 * separator.
5225 */
5226 void
5227add_pathsep(p)
5228 char_u *p;
5229{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005230 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 STRCAT(p, PATHSEPSTR);
5232}
5233
5234/*
5235 * FullName_save - Make an allocated copy of a full file name.
5236 * Returns NULL when out of memory.
5237 */
5238 char_u *
5239FullName_save(fname, force)
5240 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005241 int force; /* force expansion, even when it already looks
5242 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 char_u *buf;
5245 char_u *new_fname = NULL;
5246
5247 if (fname == NULL)
5248 return NULL;
5249
5250 buf = alloc((unsigned)MAXPATHL);
5251 if (buf != NULL)
5252 {
5253 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5254 new_fname = vim_strsave(buf);
5255 else
5256 new_fname = vim_strsave(fname);
5257 vim_free(buf);
5258 }
5259 return new_fname;
5260}
5261
5262#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5263
5264static char_u *skip_string __ARGS((char_u *p));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005265static pos_T *ind_find_start_comment __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
5267/*
5268 * Find the start of a comment, not knowing if we are in a comment right now.
5269 * Search starts at w_cursor.lnum and goes backwards.
5270 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005271 static pos_T *
5272ind_find_start_comment() /* XXX */
5273{
5274 return find_start_comment(curbuf->b_ind_maxcomment);
5275}
5276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 pos_T *
5278find_start_comment(ind_maxcomment) /* XXX */
5279 int ind_maxcomment;
5280{
5281 pos_T *pos;
5282 char_u *line;
5283 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005284 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005286 for (;;)
5287 {
5288 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5289 if (pos == NULL)
5290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005292 /*
5293 * Check if the comment start we found is inside a string.
5294 * If it is then restrict the search to below this line and try again.
5295 */
5296 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005297 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005298 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005299 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005300 break;
5301 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5302 if (cur_maxcomment <= 0)
5303 {
5304 pos = NULL;
5305 break;
5306 }
5307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 return pos;
5309}
5310
5311/*
5312 * Skip to the end of a "string" and a 'c' character.
5313 * If there is no string or character, return argument unmodified.
5314 */
5315 static char_u *
5316skip_string(p)
5317 char_u *p;
5318{
5319 int i;
5320
5321 /*
5322 * We loop, because strings may be concatenated: "date""time".
5323 */
5324 for ( ; ; ++p)
5325 {
5326 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5327 {
5328 if (!p[1]) /* ' at end of line */
5329 break;
5330 i = 2;
5331 if (p[1] == '\\') /* '\n' or '\000' */
5332 {
5333 ++i;
5334 while (vim_isdigit(p[i - 1])) /* '\000' */
5335 ++i;
5336 }
5337 if (p[i] == '\'') /* check for trailing ' */
5338 {
5339 p += i;
5340 continue;
5341 }
5342 }
5343 else if (p[0] == '"') /* start of string */
5344 {
5345 for (++p; p[0]; ++p)
5346 {
5347 if (p[0] == '\\' && p[1] != NUL)
5348 ++p;
5349 else if (p[0] == '"') /* end of string */
5350 break;
5351 }
5352 if (p[0] == '"')
5353 continue;
5354 }
5355 break; /* no string found */
5356 }
5357 if (!*p)
5358 --p; /* backup from NUL */
5359 return p;
5360}
5361#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5362
5363#if defined(FEAT_CINDENT) || defined(PROTO)
5364
5365/*
5366 * Do C or expression indenting on the current line.
5367 */
5368 void
5369do_c_expr_indent()
5370{
5371# ifdef FEAT_EVAL
5372 if (*curbuf->b_p_inde != NUL)
5373 fixthisline(get_expr_indent);
5374 else
5375# endif
5376 fixthisline(get_c_indent);
5377}
5378
5379/*
5380 * Functions for C-indenting.
5381 * Most of this originally comes from Eric Fischer.
5382 */
5383/*
5384 * Below "XXX" means that this function may unlock the current line.
5385 */
5386
5387static char_u *cin_skipcomment __ARGS((char_u *));
5388static int cin_nocode __ARGS((char_u *));
5389static pos_T *find_line_comment __ARGS((void));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005390static int cin_has_js_key __ARGS((char_u *text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391static int cin_islabel_skip __ARGS((char_u **));
5392static int cin_isdefault __ARGS((char_u *));
5393static char_u *after_label __ARGS((char_u *l));
5394static int get_indent_nolabel __ARGS((linenr_T lnum));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005395static int skip_label __ARGS((linenr_T, char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396static int cin_first_id_amount __ARGS((void));
5397static int cin_get_equal_amount __ARGS((linenr_T lnum));
5398static int cin_ispreproc __ARGS((char_u *));
5399static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5400static int cin_iscomment __ARGS((char_u *));
5401static int cin_islinecomment __ARGS((char_u *));
5402static int cin_isterminated __ARGS((char_u *, int, int));
5403static int cin_isinit __ARGS((void));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005404static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405static int cin_isif __ARGS((char_u *));
5406static int cin_iselse __ARGS((char_u *));
5407static int cin_isdo __ARGS((char_u *));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005408static int cin_iswhileofdo __ARGS((char_u *, linenr_T));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005409static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005410static int cin_iswhileofdo_end __ARGS((int terminated));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005412static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005413static int get_baseclass_amount __ARGS((int col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005415static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416static int cin_skip2pos __ARGS((pos_T *trypos));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005417static pos_T *find_start_brace __ARGS((void));
5418static pos_T *find_match_paren __ARGS((int));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005419static pos_T *find_match_char __ARGS((int c, int ind_maxparen));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005420static int corr_ind_maxparen __ARGS((pos_T *startpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421static int find_last_paren __ARGS((char_u *l, int start, int end));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005422static int find_match __ARGS((int lookfor, linenr_T ourscope));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005423static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425/*
5426 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005427 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 */
5429 static char_u *
5430cin_skipcomment(s)
5431 char_u *s;
5432{
5433 while (*s)
5434 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005435 char_u *prev_s = s;
5436
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005438
5439 /* Perl/shell # comment comment continues until eol. Require a space
5440 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005441 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005442 {
5443 s += STRLEN(s);
5444 break;
5445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (*s != '/')
5447 break;
5448 ++s;
5449 if (*s == '/') /* slash-slash comment continues till eol */
5450 {
5451 s += STRLEN(s);
5452 break;
5453 }
5454 if (*s != '*')
5455 break;
5456 for (++s; *s; ++s) /* skip slash-star comment */
5457 if (s[0] == '*' && s[1] == '/')
5458 {
5459 s += 2;
5460 break;
5461 }
5462 }
5463 return s;
5464}
5465
5466/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005467 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 * not considered code.
5469 */
5470 static int
5471cin_nocode(s)
5472 char_u *s;
5473{
5474 return *cin_skipcomment(s) == NUL;
5475}
5476
5477/*
5478 * Check previous lines for a "//" line comment, skipping over blank lines.
5479 */
5480 static pos_T *
5481find_line_comment() /* XXX */
5482{
5483 static pos_T pos;
5484 char_u *line;
5485 char_u *p;
5486
5487 pos = curwin->w_cursor;
5488 while (--pos.lnum > 0)
5489 {
5490 line = ml_get(pos.lnum);
5491 p = skipwhite(line);
5492 if (cin_islinecomment(p))
5493 {
5494 pos.col = (int)(p - line);
5495 return &pos;
5496 }
5497 if (*p != NUL)
5498 break;
5499 }
5500 return NULL;
5501}
5502
5503/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005504 * Return TRUE if "text" starts with "key:".
5505 */
5506 static int
5507cin_has_js_key(text)
5508 char_u *text;
5509{
5510 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005511 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005512
5513 if (*s == '\'' || *s == '"')
5514 {
5515 /* can be 'key': or "key": */
5516 quote = *s;
5517 ++s;
5518 }
5519 if (!vim_isIDc(*s)) /* need at least one ID character */
5520 return FALSE;
5521
5522 while (vim_isIDc(*s))
5523 ++s;
5524 if (*s == quote)
5525 ++s;
5526
5527 s = cin_skipcomment(s);
5528
5529 /* "::" is not a label, it's C++ */
5530 return (*s == ':' && s[1] != ':');
5531}
5532
5533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005535 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 */
5537 static int
5538cin_islabel_skip(s)
5539 char_u **s;
5540{
5541 if (!vim_isIDc(**s)) /* need at least one ID character */
5542 return FALSE;
5543
5544 while (vim_isIDc(**s))
5545 (*s)++;
5546
5547 *s = cin_skipcomment(*s);
5548
5549 /* "::" is not a label, it's C++ */
5550 return (**s == ':' && *++*s != ':');
5551}
5552
5553/*
5554 * Recognize a label: "label:".
5555 * Note: curwin->w_cursor must be where we are looking for the label.
5556 */
5557 int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005558cin_islabel() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
5560 char_u *s;
5561
5562 s = cin_skipcomment(ml_get_curline());
5563
5564 /*
5565 * Exclude "default" from labels, since it should be indented
5566 * like a switch label. Same for C++ scope declarations.
5567 */
5568 if (cin_isdefault(s))
5569 return FALSE;
5570 if (cin_isscopedecl(s))
5571 return FALSE;
5572
5573 if (cin_islabel_skip(&s))
5574 {
5575 /*
5576 * Only accept a label if the previous line is terminated or is a case
5577 * label.
5578 */
5579 pos_T cursor_save;
5580 pos_T *trypos;
5581 char_u *line;
5582
5583 cursor_save = curwin->w_cursor;
5584 while (curwin->w_cursor.lnum > 1)
5585 {
5586 --curwin->w_cursor.lnum;
5587
5588 /*
5589 * If we're in a comment now, skip to the start of the comment.
5590 */
5591 curwin->w_cursor.col = 0;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005592 if ((trypos = ind_find_start_comment()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 curwin->w_cursor = *trypos;
5594
5595 line = ml_get_curline();
5596 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5597 continue;
5598 if (*(line = cin_skipcomment(line)) == NUL)
5599 continue;
5600
5601 curwin->w_cursor = cursor_save;
5602 if (cin_isterminated(line, TRUE, FALSE)
5603 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005604 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 || (cin_islabel_skip(&line) && cin_nocode(line)))
5606 return TRUE;
5607 return FALSE;
5608 }
5609 curwin->w_cursor = cursor_save;
5610 return TRUE; /* label at start of file??? */
5611 }
5612 return FALSE;
5613}
5614
5615/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005616 * Recognize structure initialization and enumerations:
5617 * "[typedef] [static|public|protected|private] enum"
5618 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 */
5620 static int
5621cin_isinit(void)
5622{
5623 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005624 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
5626 s = cin_skipcomment(ml_get_curline());
5627
Bram Moolenaar75342212013-03-07 13:13:52 +01005628 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 s = cin_skipcomment(s + 7);
5630
Bram Moolenaar75342212013-03-07 13:13:52 +01005631 for (;;)
5632 {
5633 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005634
Bram Moolenaar75342212013-03-07 13:13:52 +01005635 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5636 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005637 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005638 if (cin_starts_with(s, skip[i]))
5639 {
5640 s = cin_skipcomment(s + l);
5641 l = 0;
5642 break;
5643 }
5644 }
5645 if (l != 0)
5646 break;
5647 }
5648
5649 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 return TRUE;
5651
5652 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5653 return TRUE;
5654
5655 return FALSE;
5656}
5657
5658/*
5659 * Recognize a switch label: "case .*:" or "default:".
5660 */
5661 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005662cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005664 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005667 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 for (s += 4; *s; ++s)
5670 {
5671 s = cin_skipcomment(s);
5672 if (*s == ':')
5673 {
5674 if (s[1] == ':') /* skip over "::" for C++ */
5675 ++s;
5676 else
5677 return TRUE;
5678 }
5679 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005680 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5682 return FALSE; /* stop at comment */
5683 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005684 {
5685 /* JS etc. */
5686 if (strict)
5687 return FALSE; /* stop at string */
5688 else
5689 return TRUE;
5690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 return FALSE;
5693 }
5694
5695 if (cin_isdefault(s))
5696 return TRUE;
5697 return FALSE;
5698}
5699
5700/*
5701 * Recognize a "default" switch label.
5702 */
5703 static int
5704cin_isdefault(s)
5705 char_u *s;
5706{
5707 return (STRNCMP(s, "default", 7) == 0
5708 && *(s = cin_skipcomment(s + 7)) == ':'
5709 && s[1] != ':');
5710}
5711
5712/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005713 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 */
5715 int
5716cin_isscopedecl(s)
5717 char_u *s;
5718{
5719 int i;
5720
5721 s = cin_skipcomment(s);
5722 if (STRNCMP(s, "public", 6) == 0)
5723 i = 6;
5724 else if (STRNCMP(s, "protected", 9) == 0)
5725 i = 9;
5726 else if (STRNCMP(s, "private", 7) == 0)
5727 i = 7;
5728 else
5729 return FALSE;
5730 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5731}
5732
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005733/* Maximum number of lines to search back for a "namespace" line. */
5734#define FIND_NAMESPACE_LIM 20
5735
5736/*
5737 * Recognize a "namespace" scope declaration.
5738 */
5739 static int
5740cin_is_cpp_namespace(s)
5741 char_u *s;
5742{
5743 char_u *p;
5744 int has_name = FALSE;
5745
5746 s = cin_skipcomment(s);
5747 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5748 {
5749 p = cin_skipcomment(skipwhite(s + 9));
5750 while (*p != NUL)
5751 {
5752 if (vim_iswhite(*p))
5753 {
5754 has_name = TRUE; /* found end of a name */
5755 p = cin_skipcomment(skipwhite(p));
5756 }
5757 else if (*p == '{')
5758 {
5759 break;
5760 }
5761 else if (vim_iswordc(*p))
5762 {
5763 if (has_name)
5764 return FALSE; /* word character after skipping past name */
5765 ++p;
5766 }
5767 else
5768 {
5769 return FALSE;
5770 }
5771 }
5772 return TRUE;
5773 }
5774 return FALSE;
5775}
5776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777/*
5778 * Return a pointer to the first non-empty non-comment character after a ':'.
5779 * Return NULL if not found.
5780 * case 234: a = b;
5781 * ^
5782 */
5783 static char_u *
5784after_label(l)
5785 char_u *l;
5786{
5787 for ( ; *l; ++l)
5788 {
5789 if (*l == ':')
5790 {
5791 if (l[1] == ':') /* skip over "::" for C++ */
5792 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005793 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795 }
5796 else if (*l == '\'' && l[1] && l[2] == '\'')
5797 l += 2; /* skip over 'x' */
5798 }
5799 if (*l == NUL)
5800 return NULL;
5801 l = cin_skipcomment(l + 1);
5802 if (*l == NUL)
5803 return NULL;
5804 return l;
5805}
5806
5807/*
5808 * Get indent of line "lnum", skipping a label.
5809 * Return 0 if there is nothing after the label.
5810 */
5811 static int
5812get_indent_nolabel(lnum) /* XXX */
5813 linenr_T lnum;
5814{
5815 char_u *l;
5816 pos_T fp;
5817 colnr_T col;
5818 char_u *p;
5819
5820 l = ml_get(lnum);
5821 p = after_label(l);
5822 if (p == NULL)
5823 return 0;
5824
5825 fp.col = (colnr_T)(p - l);
5826 fp.lnum = lnum;
5827 getvcol(curwin, &fp, &col, NULL, NULL);
5828 return (int)col;
5829}
5830
5831/*
5832 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005833 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 * label: if (asdf && asdfasdf)
5835 * ^
5836 */
5837 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005838skip_label(lnum, pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 linenr_T lnum;
5840 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 char_u *l;
5843 int amount;
5844 pos_T cursor_save;
5845
5846 cursor_save = curwin->w_cursor;
5847 curwin->w_cursor.lnum = lnum;
5848 l = ml_get_curline();
5849 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005850 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
5852 amount = get_indent_nolabel(lnum);
5853 l = after_label(ml_get_curline());
5854 if (l == NULL) /* just in case */
5855 l = ml_get_curline();
5856 }
5857 else
5858 {
5859 amount = get_indent();
5860 l = ml_get_curline();
5861 }
5862 *pp = l;
5863
5864 curwin->w_cursor = cursor_save;
5865 return amount;
5866}
5867
5868/*
5869 * Return the indent of the first variable name after a type in a declaration.
5870 * int a, indent of "a"
5871 * static struct foo b, indent of "b"
5872 * enum bla c, indent of "c"
5873 * Returns zero when it doesn't look like a declaration.
5874 */
5875 static int
5876cin_first_id_amount()
5877{
5878 char_u *line, *p, *s;
5879 int len;
5880 pos_T fp;
5881 colnr_T col;
5882
5883 line = ml_get_curline();
5884 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005885 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5887 {
5888 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005889 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5892 p = skipwhite(p + 6);
5893 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5894 p = skipwhite(p + 4);
5895 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5896 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5897 {
5898 s = skipwhite(p + len);
5899 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5900 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5901 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5902 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5903 p = s;
5904 }
5905 for (len = 0; vim_isIDc(p[len]); ++len)
5906 ;
5907 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5908 return 0;
5909
5910 p = skipwhite(p + len);
5911 fp.lnum = curwin->w_cursor.lnum;
5912 fp.col = (colnr_T)(p - line);
5913 getvcol(curwin, &fp, &col, NULL, NULL);
5914 return (int)col;
5915}
5916
5917/*
5918 * Return the indent of the first non-blank after an equal sign.
5919 * char *foo = "here";
5920 * Return zero if no (useful) equal sign found.
5921 * Return -1 if the line above "lnum" ends in a backslash.
5922 * foo = "asdf\
5923 * asdf\
5924 * here";
5925 */
5926 static int
5927cin_get_equal_amount(lnum)
5928 linenr_T lnum;
5929{
5930 char_u *line;
5931 char_u *s;
5932 colnr_T col;
5933 pos_T fp;
5934
5935 if (lnum > 1)
5936 {
5937 line = ml_get(lnum - 1);
5938 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5939 return -1;
5940 }
5941
5942 line = s = ml_get(lnum);
5943 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5944 {
5945 if (cin_iscomment(s)) /* ignore comments */
5946 s = cin_skipcomment(s);
5947 else
5948 ++s;
5949 }
5950 if (*s != '=')
5951 return 0;
5952
5953 s = skipwhite(s + 1);
5954 if (cin_nocode(s))
5955 return 0;
5956
5957 if (*s == '"') /* nice alignment for continued strings */
5958 ++s;
5959
5960 fp.lnum = lnum;
5961 fp.col = (colnr_T)(s - line);
5962 getvcol(curwin, &fp, &col, NULL, NULL);
5963 return (int)col;
5964}
5965
5966/*
5967 * Recognize a preprocessor statement: Any line that starts with '#'.
5968 */
5969 static int
5970cin_ispreproc(s)
5971 char_u *s;
5972{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005973 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 return TRUE;
5975 return FALSE;
5976}
5977
5978/*
5979 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5980 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5981 * start and return the line in "*pp".
5982 */
5983 static int
5984cin_ispreproc_cont(pp, lnump)
5985 char_u **pp;
5986 linenr_T *lnump;
5987{
5988 char_u *line = *pp;
5989 linenr_T lnum = *lnump;
5990 int retval = FALSE;
5991
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005992 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
5994 if (cin_ispreproc(line))
5995 {
5996 retval = TRUE;
5997 *lnump = lnum;
5998 break;
5999 }
6000 if (lnum == 1)
6001 break;
6002 line = ml_get(--lnum);
6003 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6004 break;
6005 }
6006
6007 if (lnum != *lnump)
6008 *pp = ml_get(*lnump);
6009 return retval;
6010}
6011
6012/*
6013 * Recognize the start of a C or C++ comment.
6014 */
6015 static int
6016cin_iscomment(p)
6017 char_u *p;
6018{
6019 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6020}
6021
6022/*
6023 * Recognize the start of a "//" comment.
6024 */
6025 static int
6026cin_islinecomment(p)
6027 char_u *p;
6028{
6029 return (p[0] == '/' && p[1] == '/');
6030}
6031
6032/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006033 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6034 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006036 * If a line begins with an "else", only consider it terminated if no unmatched
6037 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 * Return the character terminating the line (ending char's have precedence if
6039 * both apply in order to determine initializations).
6040 */
6041 static int
6042cin_isterminated(s, incl_open, incl_comma)
6043 char_u *s;
6044 int incl_open; /* include '{' at the end as terminator */
6045 int incl_comma; /* recognize a trailing comma */
6046{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006047 char_u found_start = 0;
6048 unsigned n_open = 0;
6049 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
6051 s = cin_skipcomment(s);
6052
6053 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6054 found_start = *s;
6055
Bram Moolenaar496f9512011-05-19 16:35:09 +02006056 if (!found_start)
6057 is_else = cin_iselse(s);
6058
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 while (*s)
6060 {
6061 /* skip over comments, "" strings and 'c'haracters */
6062 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006063 if (*s == '}' && n_open > 0)
6064 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006065 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006066 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 && cin_nocode(s + 1))
6068 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006069 else if (*s == '{')
6070 {
6071 if (incl_open && cin_nocode(s + 1))
6072 return *s;
6073 else
6074 ++n_open;
6075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076
6077 if (*s)
6078 s++;
6079 }
6080 return found_start;
6081}
6082
6083/*
6084 * Recognize the basic picture of a function declaration -- it needs to
6085 * have an open paren somewhere and a close paren at the end of the line and
6086 * no semicolons anywhere.
6087 * When a line ends in a comma we continue looking in the next line.
6088 * "sp" points to a string with the line. When looking at other lines it must
6089 * be restored to the line. When it's NULL fetch lines here.
6090 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006091 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 */
6093 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006094cin_isfuncdecl(sp, first_lnum, min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 char_u **sp;
6096 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006097 linenr_T min_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
6099 char_u *s;
6100 linenr_T lnum = first_lnum;
6101 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006102 pos_T *trypos;
6103 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
6105 if (sp == NULL)
6106 s = ml_get(lnum);
6107 else
6108 s = *sp;
6109
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006110 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006111 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006112 {
6113 lnum = trypos->lnum;
6114 if (lnum < min_lnum)
6115 return FALSE;
6116
6117 s = ml_get(lnum);
6118 }
6119
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006120 /* Ignore line starting with #. */
6121 if (cin_ispreproc(s))
6122 return FALSE;
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6125 {
6126 if (cin_iscomment(s)) /* ignore comments */
6127 s = cin_skipcomment(s);
6128 else
6129 ++s;
6130 }
6131 if (*s != '(')
6132 return FALSE; /* ';', ' or " before any () or no '(' */
6133
6134 while (*s && *s != ';' && *s != '\'' && *s != '"')
6135 {
6136 if (*s == ')' && cin_nocode(s + 1))
6137 {
6138 /* ')' at the end: may have found a match
6139 * Check for he previous line not to end in a backslash:
6140 * #if defined(x) && \
6141 * defined(y)
6142 */
6143 lnum = first_lnum - 1;
6144 s = ml_get(lnum);
6145 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6146 retval = TRUE;
6147 goto done;
6148 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006149 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006151 int comma = (*s == ',');
6152
6153 /* ',' at the end: continue looking in the next line.
6154 * At the end: check for ',' in the next line, for this style:
6155 * func(arg1
6156 * , arg2) */
6157 for (;;)
6158 {
6159 if (lnum >= curbuf->b_ml.ml_line_count)
6160 break;
6161 s = ml_get(++lnum);
6162 if (!cin_ispreproc(s))
6163 break;
6164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (lnum >= curbuf->b_ml.ml_line_count)
6166 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006167 /* Require a comma at end of the line or a comma or ')' at the
6168 * start of next line. */
6169 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006170 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006171 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006172 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
6174 else if (cin_iscomment(s)) /* ignore comments */
6175 s = cin_skipcomment(s);
6176 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006179 just_started = FALSE;
6180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182
6183done:
6184 if (lnum != first_lnum && sp != NULL)
6185 *sp = ml_get(first_lnum);
6186
6187 return retval;
6188}
6189
6190 static int
6191cin_isif(p)
6192 char_u *p;
6193{
6194 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6195}
6196
6197 static int
6198cin_iselse(p)
6199 char_u *p;
6200{
6201 if (*p == '}') /* accept "} else" */
6202 p = cin_skipcomment(p + 1);
6203 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6204}
6205
6206 static int
6207cin_isdo(p)
6208 char_u *p;
6209{
6210 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6211}
6212
6213/*
6214 * Check if this is a "while" that should have a matching "do".
6215 * We only accept a "while (condition) ;", with only white space between the
6216 * ')' and ';'. The condition may be spread over several lines.
6217 */
6218 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006219cin_iswhileofdo(p, lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 char_u *p;
6221 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 pos_T cursor_save;
6224 pos_T *trypos;
6225 int retval = FALSE;
6226
6227 p = cin_skipcomment(p);
6228 if (*p == '}') /* accept "} while (cond);" */
6229 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006230 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 cursor_save = curwin->w_cursor;
6233 curwin->w_cursor.lnum = lnum;
6234 curwin->w_cursor.col = 0;
6235 p = ml_get_curline();
6236 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6237 {
6238 ++p;
6239 ++curwin->w_cursor.col;
6240 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006241 if ((trypos = findmatchlimit(NULL, 0, 0,
6242 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6244 retval = TRUE;
6245 curwin->w_cursor = cursor_save;
6246 }
6247 return retval;
6248}
6249
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006250/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006251 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006252 * Return 0 if there is none.
6253 * Otherwise return !0 and update "*poffset" to point to the place where the
6254 * string was found.
6255 */
6256 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006257cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006258 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006259 int *poffset;
6260{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006261 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006262
6263 if (offset-- < 2)
6264 return 0;
6265 while (offset > 2 && vim_iswhite(line[offset]))
6266 --offset;
6267
6268 offset -= 1;
6269 if (!STRNCMP(line + offset, "if", 2))
6270 goto probablyFound;
6271
6272 if (offset >= 1)
6273 {
6274 offset -= 1;
6275 if (!STRNCMP(line + offset, "for", 3))
6276 goto probablyFound;
6277
6278 if (offset >= 2)
6279 {
6280 offset -= 2;
6281 if (!STRNCMP(line + offset, "while", 5))
6282 goto probablyFound;
6283 }
6284 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006285 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006286
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006287probablyFound:
6288 if (!offset || !vim_isIDc(line[offset - 1]))
6289 {
6290 *poffset = offset;
6291 return 1;
6292 }
6293 return 0;
6294}
6295
6296/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006297 * Return TRUE if we are at the end of a do-while.
6298 * do
6299 * nothing;
6300 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006301 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006302 * Adjust the cursor to the line with "while".
6303 */
6304 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006305cin_iswhileofdo_end(terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006306 int terminated;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006307{
6308 char_u *line;
6309 char_u *p;
6310 char_u *s;
6311 pos_T *trypos;
6312 int i;
6313
6314 if (terminated != ';') /* there must be a ';' at the end */
6315 return FALSE;
6316
6317 p = line = ml_get_curline();
6318 while (*p != NUL)
6319 {
6320 p = cin_skipcomment(p);
6321 if (*p == ')')
6322 {
6323 s = skipwhite(p + 1);
6324 if (*s == ';' && cin_nocode(s + 1))
6325 {
6326 /* Found ");" at end of the line, now check there is "while"
6327 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006328 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006329 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006330 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006331 if (trypos != NULL)
6332 {
6333 s = cin_skipcomment(ml_get(trypos->lnum));
6334 if (*s == '}') /* accept "} while (cond);" */
6335 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006336 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006337 {
6338 curwin->w_cursor.lnum = trypos->lnum;
6339 return TRUE;
6340 }
6341 }
6342
6343 /* Searching may have made "line" invalid, get it again. */
6344 line = ml_get_curline();
6345 p = line + i;
6346 }
6347 }
6348 if (*p != NUL)
6349 ++p;
6350 }
6351 return FALSE;
6352}
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 static int
6355cin_isbreak(p)
6356 char_u *p;
6357{
6358 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6359}
6360
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006361/*
6362 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 * constructor-initialization. eg:
6364 *
6365 * class MyClass :
6366 * baseClass <-- here
6367 * class MyClass : public baseClass,
6368 * anotherBaseClass <-- here (should probably lineup ??)
6369 * MyClass::MyClass(...) :
6370 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006371 *
6372 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 */
6374 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006375cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006376 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
6378 char_u *s;
6379 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006380 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006381 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383 *col = 0;
6384
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006385 s = skipwhite(line);
6386 if (*s == '#') /* skip #define FOO x ? (x) : x */
6387 return FALSE;
6388 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (*s == NUL)
6390 return FALSE;
6391
6392 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6393
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006394 /* Search for a line starting with '#', empty, ending in ';' or containing
6395 * '{' or '}' and start below it. This handles the following situations:
6396 * a = cond ?
6397 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006398 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006399 * func::foo()
6400 * : something
6401 * {}
6402 * Foo::Foo (int one, int two)
6403 * : something(4),
6404 * somethingelse(3)
6405 * {}
6406 */
6407 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006409 line = ml_get(lnum - 1);
6410 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006411 if (*s == '#' || *s == NUL)
6412 break;
6413 while (*s != NUL)
6414 {
6415 s = cin_skipcomment(s);
6416 if (*s == '{' || *s == '}'
6417 || (*s == ';' && cin_nocode(s + 1)))
6418 break;
6419 if (*s != NUL)
6420 ++s;
6421 }
6422 if (*s != NUL)
6423 break;
6424 --lnum;
6425 }
6426
Bram Moolenaare7c56862007-08-04 10:14:52 +00006427 line = ml_get(lnum);
6428 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006429 for (;;)
6430 {
6431 if (*s == NUL)
6432 {
6433 if (lnum == curwin->w_cursor.lnum)
6434 break;
6435 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006436 line = ml_get(++lnum);
6437 s = cin_skipcomment(line);
6438 if (*s == NUL)
6439 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006440 }
6441
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006442 if (s[0] == '"')
6443 s = skip_string(s) + 1;
6444 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 {
6446 if (s[1] == ':')
6447 {
6448 /* skip double colon. It can't be a constructor
6449 * initialization any more */
6450 lookfor_ctor_init = FALSE;
6451 s = cin_skipcomment(s + 2);
6452 }
6453 else if (lookfor_ctor_init || class_or_struct)
6454 {
6455 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006456 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 cpp_base_class = TRUE;
6458 lookfor_ctor_init = class_or_struct = FALSE;
6459 *col = 0;
6460 s = cin_skipcomment(s + 1);
6461 }
6462 else
6463 s = cin_skipcomment(s + 1);
6464 }
6465 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6466 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6467 {
6468 class_or_struct = TRUE;
6469 lookfor_ctor_init = FALSE;
6470
6471 if (*s == 'c')
6472 s = cin_skipcomment(s + 5);
6473 else
6474 s = cin_skipcomment(s + 6);
6475 }
6476 else
6477 {
6478 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6479 {
6480 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6481 }
6482 else if (s[0] == ')')
6483 {
6484 /* Constructor-initialization is assumed if we come across
6485 * something like "):" */
6486 class_or_struct = FALSE;
6487 lookfor_ctor_init = TRUE;
6488 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006489 else if (s[0] == '?')
6490 {
6491 /* Avoid seeing '() :' after '?' as constructor init. */
6492 return FALSE;
6493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 else if (!vim_isIDc(s[0]))
6495 {
6496 /* if it is not an identifier, we are wrong */
6497 class_or_struct = FALSE;
6498 lookfor_ctor_init = FALSE;
6499 }
6500 else if (*col == 0)
6501 {
6502 /* it can't be a constructor-initialization any more */
6503 lookfor_ctor_init = FALSE;
6504
6505 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006506 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 *col = (colnr_T)(s - line);
6508 }
6509
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006510 /* When the line ends in a comma don't align with it. */
6511 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6512 *col = 0;
6513
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 s = cin_skipcomment(s + 1);
6515 }
6516 }
6517
6518 return cpp_base_class;
6519}
6520
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006521 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006522get_baseclass_amount(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006523 int col;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006524{
6525 int amount;
6526 colnr_T vcol;
6527 pos_T *trypos;
6528
6529 if (col == 0)
6530 {
6531 amount = get_indent();
6532 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006533 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006534 amount = get_indent_lnum(trypos->lnum); /* XXX */
6535 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006536 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006537 }
6538 else
6539 {
6540 curwin->w_cursor.col = col;
6541 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6542 amount = (int)vcol;
6543 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006544 if (amount < curbuf->b_ind_cpp_baseclass)
6545 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006546 return amount;
6547}
6548
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549/*
6550 * Return TRUE if string "s" ends with the string "find", possibly followed by
6551 * white space and comments. Skip strings and comments.
6552 * Ignore "ignore" after "find" if it's not NULL.
6553 */
6554 static int
6555cin_ends_in(s, find, ignore)
6556 char_u *s;
6557 char_u *find;
6558 char_u *ignore;
6559{
6560 char_u *p = s;
6561 char_u *r;
6562 int len = (int)STRLEN(find);
6563
6564 while (*p != NUL)
6565 {
6566 p = cin_skipcomment(p);
6567 if (STRNCMP(p, find, len) == 0)
6568 {
6569 r = skipwhite(p + len);
6570 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6571 r = skipwhite(r + STRLEN(ignore));
6572 if (cin_nocode(r))
6573 return TRUE;
6574 }
6575 if (*p != NUL)
6576 ++p;
6577 }
6578 return FALSE;
6579}
6580
6581/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006582 * Return TRUE when "s" starts with "word" and then a non-ID character.
6583 */
6584 static int
6585cin_starts_with(s, word)
6586 char_u *s;
6587 char *word;
6588{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006589 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006590
6591 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6592}
6593
6594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 * Skip strings, chars and comments until at or past "trypos".
6596 * Return the column found.
6597 */
6598 static int
6599cin_skip2pos(trypos)
6600 pos_T *trypos;
6601{
6602 char_u *line;
6603 char_u *p;
6604
6605 p = line = ml_get(trypos->lnum);
6606 while (*p && (colnr_T)(p - line) < trypos->col)
6607 {
6608 if (cin_iscomment(p))
6609 p = cin_skipcomment(p);
6610 else
6611 {
6612 p = skip_string(p);
6613 ++p;
6614 }
6615 }
6616 return (int)(p - line);
6617}
6618
6619/*
6620 * Find the '{' at the start of the block we are in.
6621 * Return NULL if no match found.
6622 * Ignore a '{' that is in a comment, makes indenting the next three lines
6623 * work. */
6624/* foo() */
6625/* { */
6626/* } */
6627
6628 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006629find_start_brace() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631 pos_T cursor_save;
6632 pos_T *trypos;
6633 pos_T *pos;
6634 static pos_T pos_copy;
6635
6636 cursor_save = curwin->w_cursor;
6637 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6638 {
6639 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6640 trypos = &pos_copy;
6641 curwin->w_cursor = *trypos;
6642 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006643 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006645 && (pos = ind_find_start_comment()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 break;
6647 if (pos != NULL)
6648 curwin->w_cursor.lnum = pos->lnum;
6649 }
6650 curwin->w_cursor = cursor_save;
6651 return trypos;
6652}
6653
6654/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006655 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006656 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 */
6658 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006659find_match_paren(ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 int ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006662 return find_match_char('(', ind_maxparen);
6663}
6664
6665 static pos_T *
6666find_match_char(c, ind_maxparen) /* XXX */
6667 int c;
6668 int ind_maxparen;
6669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 pos_T cursor_save;
6671 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006672 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006673 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006676 ind_maxp_wk = ind_maxparen;
6677retry:
6678 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
6680 /* check if the ( is in a // comment */
6681 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006682 {
6683 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6684 if (ind_maxp_wk > 0)
6685 {
6686 curwin->w_cursor = *trypos;
6687 curwin->w_cursor.col = 0; /* XXX */
6688 goto retry;
6689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 else
6693 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006694 pos_T *trypos_wk;
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6697 trypos = &pos_copy;
6698 curwin->w_cursor = *trypos;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006699 if ((trypos_wk = ind_find_start_comment()) != NULL) /* XXX */
6700 {
6701 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6702 - trypos_wk->lnum);
6703 if (ind_maxp_wk > 0)
6704 {
6705 curwin->w_cursor = *trypos_wk;
6706 goto retry;
6707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 }
6711 }
6712 curwin->w_cursor = cursor_save;
6713 return trypos;
6714}
6715
6716/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006717 * Find the matching '(', ignoring it if it is in a comment or before an
6718 * unmatched {.
6719 * Return NULL if no match found.
6720 */
6721 static pos_T *
6722find_match_paren_after_brace(ind_maxparen) /* XXX */
6723 int ind_maxparen;
6724{
6725 pos_T *trypos = find_match_paren(ind_maxparen);
6726
6727 if (trypos != NULL)
6728 {
6729 pos_T *tryposBrace = find_start_brace();
6730
6731 /* If both an unmatched '(' and '{' is found. Ignore the '('
6732 * position if the '{' is further down. */
6733 if (tryposBrace != NULL
6734 && (trypos->lnum != tryposBrace->lnum
6735 ? trypos->lnum < tryposBrace->lnum
6736 : trypos->col < tryposBrace->col))
6737 trypos = NULL;
6738 }
6739 return trypos;
6740}
6741
6742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 * Return ind_maxparen corrected for the difference in line number between the
6744 * cursor position and "startpos". This makes sure that searching for a
6745 * matching paren above the cursor line doesn't find a match because of
6746 * looking a few lines further.
6747 */
6748 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006749corr_ind_maxparen(startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 pos_T *startpos;
6751{
6752 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6753
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006754 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6755 return curbuf->b_ind_maxparen - (int)n;
6756 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757}
6758
6759/*
6760 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006761 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 */
6763 static int
6764find_last_paren(l, start, end)
6765 char_u *l;
6766 int start, end;
6767{
6768 int i;
6769 int retval = FALSE;
6770 int open_count = 0;
6771
6772 curwin->w_cursor.col = 0; /* default is start of line */
6773
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006774 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 {
6776 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6777 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6778 if (l[i] == start)
6779 ++open_count;
6780 else if (l[i] == end)
6781 {
6782 if (open_count > 0)
6783 --open_count;
6784 else
6785 {
6786 curwin->w_cursor.col = i;
6787 retval = TRUE;
6788 }
6789 }
6790 }
6791 return retval;
6792}
6793
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006794/*
6795 * Parse 'cinoptions' and set the values in "curbuf".
6796 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6797 */
6798 void
6799parse_cino(buf)
6800 buf_T *buf;
6801{
6802 char_u *p;
6803 char_u *l;
6804 char_u *digits;
6805 int n;
6806 int divider;
6807 int fraction = 0;
6808 int sw = (int)get_sw_value(buf);
6809
6810 /*
6811 * Set the default values.
6812 */
6813 /* Spaces from a block's opening brace the prevailing indent for that
6814 * block should be. */
6815 buf->b_ind_level = sw;
6816
6817 /* Spaces from the edge of the line an open brace that's at the end of a
6818 * line is imagined to be. */
6819 buf->b_ind_open_imag = 0;
6820
6821 /* Spaces from the prevailing indent for a line that is not preceded by
6822 * an opening brace. */
6823 buf->b_ind_no_brace = 0;
6824
6825 /* Column where the first { of a function should be located }. */
6826 buf->b_ind_first_open = 0;
6827
6828 /* Spaces from the prevailing indent a leftmost open brace should be
6829 * located. */
6830 buf->b_ind_open_extra = 0;
6831
6832 /* Spaces from the matching open brace (real location for one at the left
6833 * edge; imaginary location from one that ends a line) the matching close
6834 * brace should be located. */
6835 buf->b_ind_close_extra = 0;
6836
6837 /* Spaces from the edge of the line an open brace sitting in the leftmost
6838 * column is imagined to be. */
6839 buf->b_ind_open_left_imag = 0;
6840
6841 /* Spaces jump labels should be shifted to the left if N is non-negative,
6842 * otherwise the jump label will be put to column 1. */
6843 buf->b_ind_jump_label = -1;
6844
6845 /* Spaces from the switch() indent a "case xx" label should be located. */
6846 buf->b_ind_case = sw;
6847
6848 /* Spaces from the "case xx:" code after a switch() should be located. */
6849 buf->b_ind_case_code = sw;
6850
6851 /* Lineup break at end of case in switch() with case label. */
6852 buf->b_ind_case_break = 0;
6853
6854 /* Spaces from the class declaration indent a scope declaration label
6855 * should be located. */
6856 buf->b_ind_scopedecl = sw;
6857
6858 /* Spaces from the scope declaration label code should be located. */
6859 buf->b_ind_scopedecl_code = sw;
6860
6861 /* Amount K&R-style parameters should be indented. */
6862 buf->b_ind_param = sw;
6863
6864 /* Amount a function type spec should be indented. */
6865 buf->b_ind_func_type = sw;
6866
6867 /* Amount a cpp base class declaration or constructor initialization
6868 * should be indented. */
6869 buf->b_ind_cpp_baseclass = sw;
6870
6871 /* additional spaces beyond the prevailing indent a continuation line
6872 * should be located. */
6873 buf->b_ind_continuation = sw;
6874
6875 /* Spaces from the indent of the line with an unclosed parentheses. */
6876 buf->b_ind_unclosed = sw * 2;
6877
6878 /* Spaces from the indent of the line with an unclosed parentheses, which
6879 * itself is also unclosed. */
6880 buf->b_ind_unclosed2 = sw;
6881
6882 /* Suppress ignoring spaces from the indent of a line starting with an
6883 * unclosed parentheses. */
6884 buf->b_ind_unclosed_noignore = 0;
6885
6886 /* If the opening paren is the last nonwhite character on the line, and
6887 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6888 * context (for very long lines). */
6889 buf->b_ind_unclosed_wrapped = 0;
6890
6891 /* Suppress ignoring white space when lining up with the character after
6892 * an unclosed parentheses. */
6893 buf->b_ind_unclosed_whiteok = 0;
6894
6895 /* Indent a closing parentheses under the line start of the matching
6896 * opening parentheses. */
6897 buf->b_ind_matching_paren = 0;
6898
6899 /* Indent a closing parentheses under the previous line. */
6900 buf->b_ind_paren_prev = 0;
6901
6902 /* Extra indent for comments. */
6903 buf->b_ind_comment = 0;
6904
6905 /* Spaces from the comment opener when there is nothing after it. */
6906 buf->b_ind_in_comment = 3;
6907
6908 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6909 * after the comment opener. */
6910 buf->b_ind_in_comment2 = 0;
6911
6912 /* Max lines to search for an open paren. */
6913 buf->b_ind_maxparen = 20;
6914
6915 /* Max lines to search for an open comment. */
6916 buf->b_ind_maxcomment = 70;
6917
6918 /* Handle braces for java code. */
6919 buf->b_ind_java = 0;
6920
6921 /* Not to confuse JS object properties with labels. */
6922 buf->b_ind_js = 0;
6923
6924 /* Handle blocked cases correctly. */
6925 buf->b_ind_keep_case_label = 0;
6926
6927 /* Handle C++ namespace. */
6928 buf->b_ind_cpp_namespace = 0;
6929
6930 /* Handle continuation lines containing conditions of if(), for() and
6931 * while(). */
6932 buf->b_ind_if_for_while = 0;
6933
6934 for (p = buf->b_p_cino; *p; )
6935 {
6936 l = p++;
6937 if (*p == '-')
6938 ++p;
6939 digits = p; /* remember where the digits start */
6940 n = getdigits(&p);
6941 divider = 0;
6942 if (*p == '.') /* ".5s" means a fraction */
6943 {
6944 fraction = atol((char *)++p);
6945 while (VIM_ISDIGIT(*p))
6946 {
6947 ++p;
6948 if (divider)
6949 divider *= 10;
6950 else
6951 divider = 10;
6952 }
6953 }
6954 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6955 {
6956 if (p == digits)
6957 n = sw; /* just "s" is one 'shiftwidth' */
6958 else
6959 {
6960 n *= sw;
6961 if (divider)
6962 n += (sw * fraction + divider / 2) / divider;
6963 }
6964 ++p;
6965 }
6966 if (l[1] == '-')
6967 n = -n;
6968
6969 /* When adding an entry here, also update the default 'cinoptions' in
6970 * doc/indent.txt, and add explanation for it! */
6971 switch (*l)
6972 {
6973 case '>': buf->b_ind_level = n; break;
6974 case 'e': buf->b_ind_open_imag = n; break;
6975 case 'n': buf->b_ind_no_brace = n; break;
6976 case 'f': buf->b_ind_first_open = n; break;
6977 case '{': buf->b_ind_open_extra = n; break;
6978 case '}': buf->b_ind_close_extra = n; break;
6979 case '^': buf->b_ind_open_left_imag = n; break;
6980 case 'L': buf->b_ind_jump_label = n; break;
6981 case ':': buf->b_ind_case = n; break;
6982 case '=': buf->b_ind_case_code = n; break;
6983 case 'b': buf->b_ind_case_break = n; break;
6984 case 'p': buf->b_ind_param = n; break;
6985 case 't': buf->b_ind_func_type = n; break;
6986 case '/': buf->b_ind_comment = n; break;
6987 case 'c': buf->b_ind_in_comment = n; break;
6988 case 'C': buf->b_ind_in_comment2 = n; break;
6989 case 'i': buf->b_ind_cpp_baseclass = n; break;
6990 case '+': buf->b_ind_continuation = n; break;
6991 case '(': buf->b_ind_unclosed = n; break;
6992 case 'u': buf->b_ind_unclosed2 = n; break;
6993 case 'U': buf->b_ind_unclosed_noignore = n; break;
6994 case 'W': buf->b_ind_unclosed_wrapped = n; break;
6995 case 'w': buf->b_ind_unclosed_whiteok = n; break;
6996 case 'm': buf->b_ind_matching_paren = n; break;
6997 case 'M': buf->b_ind_paren_prev = n; break;
6998 case ')': buf->b_ind_maxparen = n; break;
6999 case '*': buf->b_ind_maxcomment = n; break;
7000 case 'g': buf->b_ind_scopedecl = n; break;
7001 case 'h': buf->b_ind_scopedecl_code = n; break;
7002 case 'j': buf->b_ind_java = n; break;
7003 case 'J': buf->b_ind_js = n; break;
7004 case 'l': buf->b_ind_keep_case_label = n; break;
7005 case '#': buf->b_ind_hash_comment = n; break;
7006 case 'N': buf->b_ind_cpp_namespace = n; break;
7007 case 'k': buf->b_ind_if_for_while = n; break;
7008 }
7009 if (*p == ',')
7010 ++p;
7011 }
7012}
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 int
7015get_c_indent()
7016{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 pos_T cur_curpos;
7018 int amount;
7019 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007020 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 colnr_T col;
7022 char_u *theline;
7023 char_u *linecopy;
7024 pos_T *trypos;
7025 pos_T *tryposBrace = NULL;
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007026 pos_T tryposBraceCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 pos_T our_paren_pos;
7028 char_u *start;
7029 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007030#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031#define BRACE_AT_START 2 /* '{' is at start of line */
7032#define BRACE_AT_END 3 /* '{' is at end of line */
7033 linenr_T ourscope;
7034 char_u *l;
7035 char_u *look;
7036 char_u terminated;
7037 int lookfor;
7038#define LOOKFOR_INITIAL 0
7039#define LOOKFOR_IF 1
7040#define LOOKFOR_DO 2
7041#define LOOKFOR_CASE 3
7042#define LOOKFOR_ANY 4
7043#define LOOKFOR_TERM 5
7044#define LOOKFOR_UNTERM 6
7045#define LOOKFOR_SCOPEDECL 7
7046#define LOOKFOR_NOBREAK 8
7047#define LOOKFOR_CPP_BASECLASS 9
7048#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007049#define LOOKFOR_JS_KEY 11
Bram Moolenaardcefba92015-03-20 19:06:06 +01007050#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
7052 int whilelevel;
7053 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 int n;
7055 int iscase;
7056 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007057 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007059 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007060 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007061 int js_cur_has_key = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007063 /* make a copy, value is changed below */
7064 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
7066 /* remember where the cursor was when we started */
7067 cur_curpos = curwin->w_cursor;
7068
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007069 /* if we are at line 1 0 is fine, right? */
7070 if (cur_curpos.lnum == 1)
7071 return 0;
7072
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 /* Get a copy of the current contents of the line.
7074 * This is required, because only the most recent line obtained with
7075 * ml_get is valid! */
7076 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7077 if (linecopy == NULL)
7078 return 0;
7079
7080 /*
7081 * In insert mode and the cursor is on a ')' truncate the line at the
7082 * cursor position. We don't want to line up with the matching '(' when
7083 * inserting new stuff.
7084 * For unknown reasons the cursor might be past the end of the line, thus
7085 * check for that.
7086 */
7087 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007088 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 && linecopy[curwin->w_cursor.col] == ')')
7090 linecopy[curwin->w_cursor.col] = NUL;
7091
7092 theline = skipwhite(linecopy);
7093
7094 /* move the cursor to the start of the line */
7095
7096 curwin->w_cursor.col = 0;
7097
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007098 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007099
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 /*
7101 * #defines and so on always go at the left when included in 'cinkeys'.
7102 */
7103 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007104 amount = curbuf->b_ind_hash_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
7106 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007107 * Is it a non-case label? Then that goes at the left margin too unless:
7108 * - JS flag is set.
7109 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007111 else if (original_line_islabel && !curbuf->b_ind_js
7112 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 {
7114 amount = 0;
7115 }
7116
7117 /*
7118 * If we're inside a "//" comment and there is a "//" comment in a
7119 * previous line, lineup with that one.
7120 */
7121 else if (cin_islinecomment(theline)
7122 && (trypos = find_line_comment()) != NULL) /* XXX */
7123 {
7124 /* find how indented the line beginning the comment is */
7125 getvcol(curwin, trypos, &col, NULL, NULL);
7126 amount = col;
7127 }
7128
7129 /*
7130 * If we're inside a comment and not looking at the start of the
7131 * comment, try using the 'comments' option.
7132 */
7133 else if (!cin_iscomment(theline)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007134 && (trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007135 /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 {
7137 int lead_start_len = 2;
7138 int lead_middle_len = 1;
7139 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7140 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7141 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7142 char_u *p;
7143 int start_align = 0;
7144 int start_off = 0;
7145 int done = FALSE;
7146
7147 /* find how indented the line beginning the comment is */
7148 getvcol(curwin, trypos, &col, NULL, NULL);
7149 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007150 *lead_start = NUL;
7151 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153 p = curbuf->b_p_com;
7154 while (*p != NUL)
7155 {
7156 int align = 0;
7157 int off = 0;
7158 int what = 0;
7159
7160 while (*p != NUL && *p != ':')
7161 {
7162 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7163 what = *p++;
7164 else if (*p == COM_LEFT || *p == COM_RIGHT)
7165 align = *p++;
7166 else if (VIM_ISDIGIT(*p) || *p == '-')
7167 off = getdigits(&p);
7168 else
7169 ++p;
7170 }
7171
7172 if (*p == ':')
7173 ++p;
7174 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7175 if (what == COM_START)
7176 {
7177 STRCPY(lead_start, lead_end);
7178 lead_start_len = (int)STRLEN(lead_start);
7179 start_off = off;
7180 start_align = align;
7181 }
7182 else if (what == COM_MIDDLE)
7183 {
7184 STRCPY(lead_middle, lead_end);
7185 lead_middle_len = (int)STRLEN(lead_middle);
7186 }
7187 else if (what == COM_END)
7188 {
7189 /* If our line starts with the middle comment string, line it
7190 * up with the comment opener per the 'comments' option. */
7191 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7192 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7193 {
7194 done = TRUE;
7195 if (curwin->w_cursor.lnum > 1)
7196 {
7197 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007198 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 * the middle comment string matches in the previous
7200 * line, use the indent of that line. XXX */
7201 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7202 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7203 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7204 else if (STRNCMP(look, lead_middle,
7205 lead_middle_len) == 0)
7206 {
7207 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7208 break;
7209 }
7210 /* If the start comment string doesn't match with the
7211 * start of the comment, skip this entry. XXX */
7212 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7213 lead_start, lead_start_len) != 0)
7214 continue;
7215 }
7216 if (start_off != 0)
7217 amount += start_off;
7218 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007219 amount += vim_strsize(lead_start)
7220 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 break;
7222 }
7223
7224 /* If our line starts with the end comment string, line it up
7225 * with the middle comment */
7226 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7227 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7228 {
7229 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7230 /* XXX */
7231 if (off != 0)
7232 amount += off;
7233 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007234 amount += vim_strsize(lead_start)
7235 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 done = TRUE;
7237 break;
7238 }
7239 }
7240 }
7241
7242 /* If our line starts with an asterisk, line up with the
7243 * asterisk in the comment opener; otherwise, line up
7244 * with the first character of the comment text.
7245 */
7246 if (done)
7247 ;
7248 else if (theline[0] == '*')
7249 amount += 1;
7250 else
7251 {
7252 /*
7253 * If we are more than one line away from the comment opener, take
7254 * the indent of the previous non-empty line. If 'cino' has "CO"
7255 * and we are just below the comment opener and there are any
7256 * white characters after it line up with the text after it;
7257 * otherwise, add the amount specified by "c" in 'cino'
7258 */
7259 amount = -1;
7260 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7261 {
7262 if (linewhite(lnum)) /* skip blank lines */
7263 continue;
7264 amount = get_indent_lnum(lnum); /* XXX */
7265 break;
7266 }
7267 if (amount == -1) /* use the comment opener */
7268 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007269 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {
7271 start = ml_get(trypos->lnum);
7272 look = start + trypos->col + 2; /* skip / and * */
7273 if (*look != NUL) /* if something after it */
7274 trypos->col = (colnr_T)(skipwhite(look) - start);
7275 }
7276 getvcol(curwin, trypos, &col, NULL, NULL);
7277 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007278 if (curbuf->b_ind_in_comment2 || *look == NUL)
7279 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281 }
7282 }
7283
7284 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007285 * Are we looking at a ']' that has a match?
7286 */
7287 else if (*skipwhite(theline) == ']'
7288 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7289 {
7290 /* align with the line containing the '['. */
7291 amount = get_indent_lnum(trypos->lnum);
7292 }
7293
7294 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 * Are we inside parentheses or braces?
7296 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007297 else if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007298 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007299 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 || trypos != NULL)
7301 {
7302 if (trypos != NULL && tryposBrace != NULL)
7303 {
7304 /* Both an unmatched '(' and '{' is found. Use the one which is
7305 * closer to the current cursor position, set the other to NULL. */
7306 if (trypos->lnum != tryposBrace->lnum
7307 ? trypos->lnum < tryposBrace->lnum
7308 : trypos->col < tryposBrace->col)
7309 trypos = NULL;
7310 else
7311 tryposBrace = NULL;
7312 }
7313
7314 if (trypos != NULL)
7315 {
7316 /*
7317 * If the matching paren is more than one line away, use the indent of
7318 * a previous non-empty line that matches the same paren.
7319 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007320 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007322 /* Line up with the start of the matching paren line. */
7323 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7324 }
7325 else
7326 {
7327 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007328 our_paren_pos = *trypos;
7329 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007331 l = skipwhite(ml_get(lnum));
7332 if (cin_nocode(l)) /* skip comment lines */
7333 continue;
7334 if (cin_ispreproc_cont(&l, &lnum))
7335 continue; /* ignore #define, #if, etc. */
7336 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007338 /* Skip a comment. XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007339 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007340 {
7341 lnum = trypos->lnum + 1;
7342 continue;
7343 }
7344
7345 /* XXX */
7346 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007347 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007348 && trypos->lnum == our_paren_pos.lnum
7349 && trypos->col == our_paren_pos.col)
7350 {
7351 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007353 if (theline[0] == ')')
7354 {
7355 if (our_paren_pos.lnum != lnum
7356 && cur_amount > amount)
7357 cur_amount = amount;
7358 amount = -1;
7359 }
7360 break;
7361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 }
7363 }
7364
7365 /*
7366 * Line up with line where the matching paren is. XXX
7367 * If the line starts with a '(' or the indent for unclosed
7368 * parentheses is zero, line up with the unclosed parentheses.
7369 */
7370 if (amount == -1)
7371 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007372 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007373 int is_if_for_while = 0;
7374
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007375 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007376 {
7377 /* Look for the outermost opening parenthesis on this line
7378 * and check whether it belongs to an "if", "for" or "while". */
7379
7380 pos_T cursor_save = curwin->w_cursor;
7381 pos_T outermost;
7382 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007383
7384 trypos = &our_paren_pos;
7385 do {
7386 outermost = *trypos;
7387 curwin->w_cursor.lnum = outermost.lnum;
7388 curwin->w_cursor.col = outermost.col;
7389
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007390 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007391 } while (trypos && trypos->lnum == outermost.lnum);
7392
7393 curwin->w_cursor = cursor_save;
7394
7395 line = ml_get(outermost.lnum);
7396
7397 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007398 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007399 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007400
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007401 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007402 look = skipwhite(look);
7403 if (*look == '(')
7404 {
7405 linenr_T save_lnum = curwin->w_cursor.lnum;
7406 char_u *line;
7407 int look_col;
7408
7409 /* Ignore a '(' in front of the line that has a match before
7410 * our matching '('. */
7411 curwin->w_cursor.lnum = our_paren_pos.lnum;
7412 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007413 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007414 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007415 if ((trypos = findmatchlimit(NULL, ')', 0,
7416 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007417 != NULL
7418 && trypos->lnum == our_paren_pos.lnum
7419 && trypos->col < our_paren_pos.col)
7420 ignore_paren_col = trypos->col + 1;
7421
7422 curwin->w_cursor.lnum = save_lnum;
7423 look = ml_get(our_paren_pos.lnum) + look_col;
7424 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007425 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7426 && is_if_for_while == 0)
7427 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007428 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
7430 /*
7431 * If we're looking at a close paren, line up right there;
7432 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007433 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 * the last nonwhite character of the line, use either the
7435 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007436 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 * lines).
7438 */
7439 if (theline[0] != ')')
7440 {
7441 cur_amount = MAXCOL;
7442 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007443 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 && cin_ends_in(l, (char_u *)"(", NULL))
7445 {
7446 /* look for opening unmatched paren, indent one level
7447 * for each additional level */
7448 n = 1;
7449 for (col = 0; col < our_paren_pos.col; ++col)
7450 {
7451 switch (l[col])
7452 {
7453 case '(':
7454 case '{': ++n;
7455 break;
7456
7457 case ')':
7458 case '}': if (n > 1)
7459 --n;
7460 break;
7461 }
7462 }
7463
7464 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007465 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007467 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 our_paren_pos.col++;
7469 else
7470 {
7471 col = our_paren_pos.col + 1;
7472 while (vim_iswhite(l[col]))
7473 col++;
7474 if (l[col] != NUL) /* In case of trailing space */
7475 our_paren_pos.col = col;
7476 else
7477 our_paren_pos.col++;
7478 }
7479 }
7480
7481 /*
7482 * Find how indented the paren is, or the character after it
7483 * if we did the above "if".
7484 */
7485 if (our_paren_pos.col > 0)
7486 {
7487 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7488 if (cur_amount > (int)col)
7489 cur_amount = col;
7490 }
7491 }
7492
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007493 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 {
7495 /* Line up with the start of the matching paren line. */
7496 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007497 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7498 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007499 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 {
7501 if (cur_amount != MAXCOL)
7502 amount = cur_amount;
7503 }
7504 else
7505 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007506 /* Add b_ind_unclosed2 for each '(' before our matching one,
7507 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007509 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 {
7511 --our_paren_pos.col;
7512 switch (*ml_get_pos(&our_paren_pos))
7513 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007514 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 col = our_paren_pos.col;
7516 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007517 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 col = MAXCOL;
7519 break;
7520 }
7521 }
7522
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007523 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 * braces */
7525 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007526 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 else
7528 {
7529 curwin->w_cursor.lnum = our_paren_pos.lnum;
7530 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007531 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7532 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007533 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007535 {
7536 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007537 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007538 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007539 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
7542 /*
7543 * For a line starting with ')' use the minimum of the two
7544 * positions, to avoid giving it more indent than the previous
7545 * lines:
7546 * func_long_name( if (x
7547 * arg && yy
7548 * ) ^ not here ) ^ not here
7549 */
7550 if (cur_amount < amount)
7551 amount = cur_amount;
7552 }
7553 }
7554
7555 /* add extra indent for a comment */
7556 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007557 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 else
7560 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007561 /*
7562 * We are inside braces, there is a { before this line at the position
7563 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007564 * Make a copy of tryposBrace, it may point to pos_copy inside
7565 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007566 */
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007567 tryposBraceCopy = *tryposBrace;
7568 tryposBrace = &tryposBraceCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 ourscope = trypos->lnum;
7571 start = ml_get(ourscope);
7572
7573 /*
7574 * Now figure out how indented the line is in general.
7575 * If the brace was at the start of the line, we use that;
7576 * otherwise, check out the indentation of the line as
7577 * a whole and then add the "imaginary indent" to that.
7578 */
7579 look = skipwhite(start);
7580 if (*look == '{')
7581 {
7582 getvcol(curwin, trypos, &col, NULL, NULL);
7583 amount = col;
7584 if (*start == '{')
7585 start_brace = BRACE_IN_COL0;
7586 else
7587 start_brace = BRACE_AT_START;
7588 }
7589 else
7590 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007591 /* That opening brace might have been on a continuation
7592 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 curwin->w_cursor.lnum = ourscope;
7594
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007595 /* Position the cursor over the rightmost paren, so that
7596 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 lnum = ourscope;
7598 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007599 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7600 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 lnum = trypos->lnum;
7602
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007603 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 * case 1: if (asdf &&
7605 * ldfd) {
7606 * }
7607 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007608 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7609 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007611 else if (curbuf->b_ind_js)
7612 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007614 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615
7616 start_brace = BRACE_AT_END;
7617 }
7618
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007619 /* For Javascript check if the line starts with "key:". */
7620 if (curbuf->b_ind_js)
7621 js_cur_has_key = cin_has_js_key(theline);
7622
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007624 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 * we want to be. otherwise, add the amount of room
7626 * that an indent is supposed to be.
7627 */
7628 if (theline[0] == '}')
7629 {
7630 /*
7631 * they may want closing braces to line up with something
7632 * other than the open brace. indulge them, if so.
7633 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007634 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636 else
7637 {
7638 /*
7639 * If we're looking at an "else", try to find an "if"
7640 * to match it with.
7641 * If we're looking at a "while", try to find a "do"
7642 * to match it with.
7643 */
7644 lookfor = LOOKFOR_INITIAL;
7645 if (cin_iselse(theline))
7646 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007647 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 lookfor = LOOKFOR_DO;
7649 if (lookfor != LOOKFOR_INITIAL)
7650 {
7651 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007652 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {
7654 amount = get_indent(); /* XXX */
7655 goto theend;
7656 }
7657 }
7658
7659 /*
7660 * We get here if we are not on an "while-of-do" or "else" (or
7661 * failed to find a matching "if").
7662 * Search backwards for something to line up with.
7663 * First set amount for when we don't find anything.
7664 */
7665
7666 /*
7667 * if the '{' is _really_ at the left margin, use the imaginary
7668 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007669 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 */
7671
7672 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7673 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007674 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007675 lookfor_cpp_namespace = TRUE;
7676 }
7677 else if (start_brace == BRACE_AT_START &&
7678 lookfor_cpp_namespace) /* '{' is at start */
7679 {
7680
7681 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
7683 else
7684 {
7685 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007686 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007687 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007688
7689 l = skipwhite(ml_get_curline());
7690 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007691 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 else
7694 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007695 /* Compensate for adding b_ind_open_extra later. */
7696 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 if (amount < 0)
7698 amount = 0;
7699 }
7700 }
7701
7702 lookfor_break = FALSE;
7703
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007704 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
7706 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007707 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
7709 else if (cin_isscopedecl(theline)) /* private:, ... */
7710 {
7711 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007712 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 }
7714 else
7715 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007716 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7717 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 lookfor_break = TRUE;
7719
7720 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007721 /* b_ind_level from start of block */
7722 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724 scope_amount = amount;
7725 whilelevel = 0;
7726
7727 /*
7728 * Search backwards. If we find something we recognize, line up
7729 * with that.
7730 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007731 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 * the usual amount relative to the conditional
7733 * that opens the block.
7734 */
7735 curwin->w_cursor = cur_curpos;
7736 for (;;)
7737 {
7738 curwin->w_cursor.lnum--;
7739 curwin->w_cursor.col = 0;
7740
7741 /*
7742 * If we went all the way back to the start of our scope, line
7743 * up with it.
7744 */
7745 if (curwin->w_cursor.lnum <= ourscope)
7746 {
7747 /* we reached end of scope:
7748 * if looking for a enum or structure initialization
7749 * go further back:
7750 * if it is an initializer (enum xxx or xxx =), then
7751 * don't add ind_continuation, otherwise it is a variable
7752 * declaration:
7753 * int x,
7754 * here; <-- add ind_continuation
7755 */
7756 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7757 {
7758 if (curwin->w_cursor.lnum == 0
7759 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007760 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007762 /* nothing found (abuse curbuf->b_ind_maxparen as
7763 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 * initialization) */
7765 if (cont_amount > 0)
7766 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007767 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 amount += ind_continuation;
7769 break;
7770 }
7771
7772 l = ml_get_curline();
7773
7774 /*
7775 * If we're in a comment now, skip to the start of the
7776 * comment.
7777 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007778 trypos = ind_find_start_comment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 if (trypos != NULL)
7780 {
7781 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007782 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 continue;
7784 }
7785
7786 /*
7787 * Skip preprocessor directives and blank lines.
7788 */
7789 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7790 continue;
7791
7792 if (cin_nocode(l))
7793 continue;
7794
7795 terminated = cin_isterminated(l, FALSE, TRUE);
7796
7797 /*
7798 * If we are at top level and the line looks like a
7799 * function declaration, we are done
7800 * (it's a variable declaration).
7801 */
7802 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007803 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {
7805 /* if the line is terminated with another ','
7806 * it is a continued variable initialization.
7807 * don't add extra indent.
7808 * TODO: does not work, if a function
7809 * declaration is split over multiple lines:
7810 * cin_isfuncdecl returns FALSE then.
7811 */
7812 if (terminated == ',')
7813 break;
7814
7815 /* if it es a enum declaration or an assignment,
7816 * we are done.
7817 */
7818 if (terminated != ';' && cin_isinit())
7819 break;
7820
7821 /* nothing useful found */
7822 if (terminated == 0 || terminated == '{')
7823 continue;
7824 }
7825
7826 if (terminated != ';')
7827 {
7828 /* Skip parens and braces. Position the cursor
7829 * over the rightmost paren, so that matching it
7830 * will take us back to the start of the line.
7831 */ /* XXX */
7832 trypos = NULL;
7833 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007834 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007835 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007838 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839
7840 if (trypos != NULL)
7841 {
7842 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007843 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 continue;
7845 }
7846 }
7847
7848 /* it's a variable declaration, add indentation
7849 * like in
7850 * int a,
7851 * b;
7852 */
7853 if (cont_amount > 0)
7854 amount = cont_amount;
7855 else
7856 amount += ind_continuation;
7857 }
7858 else if (lookfor == LOOKFOR_UNTERM)
7859 {
7860 if (cont_amount > 0)
7861 amount = cont_amount;
7862 else
7863 amount += ind_continuation;
7864 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007865 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007866 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007867 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007868 && lookfor != LOOKFOR_CPP_BASECLASS
7869 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007870 {
7871 amount = scope_amount;
7872 if (theline[0] == '{')
7873 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007874 amount += curbuf->b_ind_open_extra;
7875 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007876 }
7877 }
7878
7879 if (lookfor_cpp_namespace)
7880 {
7881 /*
7882 * Looking for C++ namespace, need to look further
7883 * back.
7884 */
7885 if (curwin->w_cursor.lnum == ourscope)
7886 continue;
7887
7888 if (curwin->w_cursor.lnum == 0
7889 || curwin->w_cursor.lnum
7890 < ourscope - FIND_NAMESPACE_LIM)
7891 break;
7892
7893 l = ml_get_curline();
7894
7895 /* If we're in a comment now, skip to the start of
7896 * the comment. */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007897 trypos = ind_find_start_comment();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007898 if (trypos != NULL)
7899 {
7900 curwin->w_cursor.lnum = trypos->lnum + 1;
7901 curwin->w_cursor.col = 0;
7902 continue;
7903 }
7904
7905 /* Skip preprocessor directives and blank lines. */
7906 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7907 continue;
7908
7909 /* Finally the actual check for "namespace". */
7910 if (cin_is_cpp_namespace(l))
7911 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007912 amount += curbuf->b_ind_cpp_namespace
7913 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007914 break;
7915 }
7916
7917 if (cin_nocode(l))
7918 continue;
7919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 }
7921 break;
7922 }
7923
7924 /*
7925 * If we're in a comment now, skip to the start of the comment.
7926 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007927 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {
7929 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007930 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 continue;
7932 }
7933
7934 l = ml_get_curline();
7935
7936 /*
7937 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007938 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007940 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 if (iscase || cin_isscopedecl(l))
7942 {
7943 /* we are only looking for cpp base class
7944 * declaration/initialization any longer */
7945 if (lookfor == LOOKFOR_CPP_BASECLASS)
7946 break;
7947
7948 /* When looking for a "do" we are not interested in
7949 * labels. */
7950 if (whilelevel > 0)
7951 continue;
7952
7953 /*
7954 * case xx:
7955 * c = 99 + <- this indent plus continuation
7956 *-> here;
7957 */
7958 if (lookfor == LOOKFOR_UNTERM
7959 || lookfor == LOOKFOR_ENUM_OR_INIT)
7960 {
7961 if (cont_amount > 0)
7962 amount = cont_amount;
7963 else
7964 amount += ind_continuation;
7965 break;
7966 }
7967
7968 /*
7969 * case xx: <- line up with this case
7970 * x = 333;
7971 * case yy:
7972 */
7973 if ( (iscase && lookfor == LOOKFOR_CASE)
7974 || (iscase && lookfor_break)
7975 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7976 {
7977 /*
7978 * Check that this case label is not for another
7979 * switch()
7980 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007981 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007982 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
7984 amount = get_indent(); /* XXX */
7985 break;
7986 }
7987 continue;
7988 }
7989
7990 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7991
7992 /*
7993 * case xx: if (cond) <- line up with this if
7994 * y = y + 1;
7995 * -> s = 99;
7996 *
7997 * case xx:
7998 * if (cond) <- line up with this line
7999 * y = y + 1;
8000 * -> s = 99;
8001 */
8002 if (lookfor == LOOKFOR_TERM)
8003 {
8004 if (n)
8005 amount = n;
8006
8007 if (!lookfor_break)
8008 break;
8009 }
8010
8011 /*
8012 * case xx: x = x + 1; <- line up with this x
8013 * -> y = y + 1;
8014 *
8015 * case xx: if (cond) <- line up with this if
8016 * -> y = y + 1;
8017 */
8018 if (n)
8019 {
8020 amount = n;
8021 l = after_label(ml_get_curline());
8022 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008023 {
8024 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008025 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008026 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008027 amount += curbuf->b_ind_level
8028 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 break;
8031 }
8032
8033 /*
8034 * Try to get the indent of a statement before the switch
8035 * label. If nothing is found, line up relative to the
8036 * switch label.
8037 * break; <- may line up with this line
8038 * case xx:
8039 * -> y = 1;
8040 */
8041 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008042 ? curbuf->b_ind_case_code
8043 : curbuf->b_ind_scopedecl_code);
8044 lookfor = curbuf->b_ind_case_break
8045 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 continue;
8047 }
8048
8049 /*
8050 * Looking for a switch() label or C++ scope declaration,
8051 * ignore other lines, skip {}-blocks.
8052 */
8053 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8054 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008055 if (find_last_paren(l, '{', '}')
8056 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008057 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008059 curwin->w_cursor.col = 0;
8060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 continue;
8062 }
8063
8064 /*
8065 * Ignore jump labels with nothing after them.
8066 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008067 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {
8069 l = after_label(ml_get_curline());
8070 if (l == NULL || cin_nocode(l))
8071 continue;
8072 }
8073
8074 /*
8075 * Ignore #defines, #if, etc.
8076 * Ignore comment and empty lines.
8077 * (need to get the line again, cin_islabel() may have
8078 * unlocked it)
8079 */
8080 l = ml_get_curline();
8081 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
8082 || cin_nocode(l))
8083 continue;
8084
8085 /*
8086 * Are we at the start of a cpp base class declaration or
8087 * constructor initialization?
8088 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008089 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008090 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008091 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008092 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008093 l = ml_get_curline();
8094 }
8095 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {
8097 if (lookfor == LOOKFOR_UNTERM)
8098 {
8099 if (cont_amount > 0)
8100 amount = cont_amount;
8101 else
8102 amount += ind_continuation;
8103 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008104 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008106 /* Need to find start of the declaration. */
8107 lookfor = LOOKFOR_UNTERM;
8108 ind_continuation = 0;
8109 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 }
8111 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008112 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008113 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 break;
8115 }
8116 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8117 {
8118 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008119 * declaration or initialization before the opening brace.
8120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (cin_isterminated(l, TRUE, FALSE))
8122 break;
8123 else
8124 continue;
8125 }
8126
8127 /*
8128 * What happens next depends on the line being terminated.
8129 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008130 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 * 123,
8132 * sizeof
8133 * here
8134 * Otherwise check whether it is a enumeration or structure
8135 * initialisation (not indented) or a variable declaration
8136 * (indented).
8137 */
8138 terminated = cin_isterminated(l, FALSE, TRUE);
8139
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008140 if (js_cur_has_key)
8141 {
8142 js_cur_has_key = 0; /* only check the first line */
8143 if (curbuf->b_ind_js && terminated == ',')
8144 {
8145 /* For Javascript we might be inside an object:
8146 * key: something, <- align with this
8147 * key: something
8148 * or:
8149 * key: something + <- align with this
8150 * something,
8151 * key: something
8152 */
8153 lookfor = LOOKFOR_JS_KEY;
8154 }
8155 }
8156 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8157 {
8158 amount = get_indent();
8159 break;
8160 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008161 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008162 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008163 if (tryposBrace != NULL && tryposBrace->lnum
8164 >= curwin->w_cursor.lnum)
8165 break;
8166 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008167 /* line below current line is the one that starts a
8168 * (possibly broken) line ending in a comma. */
8169 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008170 else
8171 {
8172 amount = get_indent();
8173 if (curwin->w_cursor.lnum - 1 == ourscope)
8174 /* line above is start of the scope, thus current
8175 * line is the one that stars a (possibly broken)
8176 * line ending in a comma. */
8177 break;
8178 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008179 }
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8182 && terminated == ','))
8183 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008184 if (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')
8185 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 /*
8187 * if we're in the middle of a paren thing,
8188 * go back to the line that starts it so
8189 * we can get the right prevailing indent
8190 * if ( foo &&
8191 * bar )
8192 */
8193 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008194 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008196 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 */
8198 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008199 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008200 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8201 || (trypos->lnum == tryposBrace->lnum
8202 && trypos->col < tryposBrace->col)))
8203 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
8205 /*
8206 * If we are looking for ',', we also look for matching
8207 * braces.
8208 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008209 if (trypos == NULL && terminated == ','
8210 && 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 /*
8216 * Check if we are on a case label now. This is
8217 * handled above.
8218 * case xx: if ( asdf &&
8219 * asdf)
8220 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008221 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008223 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {
8225 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008226 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 continue;
8228 }
8229 }
8230
8231 /*
8232 * Skip over continuation lines to find the one to get the
8233 * indent from
8234 * char *usethis = "bla\
8235 * bla",
8236 * here;
8237 */
8238 if (terminated == ',')
8239 {
8240 while (curwin->w_cursor.lnum > 1)
8241 {
8242 l = ml_get(curwin->w_cursor.lnum - 1);
8243 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8244 break;
8245 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008246 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248 }
8249
8250 /*
8251 * Get indent and pointer to text for current line,
8252 * ignoring any jump label. XXX
8253 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008254 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008255 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008256 else
8257 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 /*
8259 * If this is just above the line we are indenting, and it
8260 * starts with a '{', line it up with this line.
8261 * while (not)
8262 * -> {
8263 * }
8264 */
8265 if (terminated != ',' && lookfor != LOOKFOR_TERM
8266 && theline[0] == '{')
8267 {
8268 amount = cur_amount;
8269 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008270 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 * doesn't start with a '{', which must have a match
8272 * in the same line (scope is the same). Probably:
8273 * { 1, 2 },
8274 * -> { 3, 4 }
8275 */
8276 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008277 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008279 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
8281 /* have to look back, whether it is a cpp base
8282 * class declaration or initialization */
8283 lookfor = LOOKFOR_CPP_BASECLASS;
8284 continue;
8285 }
8286 break;
8287 }
8288
8289 /*
8290 * Check if we are after an "if", "while", etc.
8291 * Also allow " } else".
8292 */
8293 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8294 {
8295 /*
8296 * Found an unterminated line after an if (), line up
8297 * with the last one.
8298 * if (cond)
8299 * 100 +
8300 * -> here;
8301 */
8302 if (lookfor == LOOKFOR_UNTERM
8303 || lookfor == LOOKFOR_ENUM_OR_INIT)
8304 {
8305 if (cont_amount > 0)
8306 amount = cont_amount;
8307 else
8308 amount += ind_continuation;
8309 break;
8310 }
8311
8312 /*
8313 * If this is just above the line we are indenting, we
8314 * are finished.
8315 * while (not)
8316 * -> here;
8317 * Otherwise this indent can be used when the line
8318 * before this is terminated.
8319 * yyy;
8320 * if (stat)
8321 * while (not)
8322 * xxx;
8323 * -> here;
8324 */
8325 amount = cur_amount;
8326 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008327 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (lookfor != LOOKFOR_TERM)
8329 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008330 amount += curbuf->b_ind_level
8331 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 break;
8333 }
8334
8335 /*
8336 * Special trick: when expecting the while () after a
8337 * do, line up with the while()
8338 * do
8339 * x = 1;
8340 * -> here
8341 */
8342 l = skipwhite(ml_get_curline());
8343 if (cin_isdo(l))
8344 {
8345 if (whilelevel == 0)
8346 break;
8347 --whilelevel;
8348 }
8349
8350 /*
8351 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008352 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 * Need to use the scope of this "else". XXX
8354 * If whilelevel != 0 continue looking for a "do {".
8355 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008356 if (cin_iselse(l) && whilelevel == 0)
8357 {
8358 /* If we're looking at "} else", let's make sure we
8359 * find the opening brace of the enclosing scope,
8360 * not the one from "if () {". */
8361 if (*l == '}')
8362 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008363 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008364
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008365 if ((trypos = find_start_brace()) == NULL
8366 || find_match(LOOKFOR_IF, trypos->lnum)
8367 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008368 break;
8369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 }
8371
8372 /*
8373 * If we're below an unterminated line that is not an
8374 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008375 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 * the line before this one.
8377 */
8378 else
8379 {
8380 /*
8381 * Found two unterminated lines on a row, line up with
8382 * the last one.
8383 * c = 99 +
8384 * 100 +
8385 * -> here;
8386 */
8387 if (lookfor == LOOKFOR_UNTERM)
8388 {
8389 /* When line ends in a comma add extra indent */
8390 if (terminated == ',')
8391 amount += ind_continuation;
8392 break;
8393 }
8394
8395 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8396 {
8397 /* Found two lines ending in ',', lineup with the
8398 * lowest one, but check for cpp base class
8399 * declaration/initialization, if it is an
8400 * opening brace or we are looking just for
8401 * enumerations/initializations. */
8402 if (terminated == ',')
8403 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008404 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 break;
8406
8407 lookfor = LOOKFOR_CPP_BASECLASS;
8408 continue;
8409 }
8410
8411 /* Ignore unterminated lines in between, but
8412 * reduce indent. */
8413 if (amount > cur_amount)
8414 amount = cur_amount;
8415 }
8416 else
8417 {
8418 /*
8419 * Found first unterminated line on a row, may
8420 * line up with this line, remember its indent
8421 * 100 +
8422 * -> here;
8423 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008424 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 amount = cur_amount;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008426 if (*skipwhite(l) == ']' || l[STRLEN(l) - 1] == ']')
8427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
8429 /*
8430 * If previous line ends in ',', check whether we
8431 * are in an initialization or enum
8432 * struct xxx =
8433 * {
8434 * sizeof a,
8435 * 124 };
8436 * or a normal possible continuation line.
8437 * but only, of no other statement has been found
8438 * yet.
8439 */
8440 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8441 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008442 if (curbuf->b_ind_js)
8443 {
8444 /* Search for a line ending in a comma
8445 * and line up with the line below it
8446 * (could be the current line).
8447 * some = [
8448 * 1, <- line up here
8449 * 2,
8450 * some = [
8451 * 3 + <- line up here
8452 * 4 *
8453 * 5,
8454 * 6,
8455 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008456 if (cin_iscomment(skipwhite(l)))
8457 break;
8458 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008459 trypos = find_match_char('[',
8460 curbuf->b_ind_maxparen);
8461 if (trypos != NULL)
8462 {
8463 if (trypos->lnum
8464 == curwin->w_cursor.lnum - 1)
8465 {
8466 /* Current line is first inside
8467 * [], line up with it. */
8468 break;
8469 }
8470 ourscope = trypos->lnum;
8471 }
8472 }
8473 else
8474 {
8475 lookfor = LOOKFOR_ENUM_OR_INIT;
8476 cont_amount = cin_first_id_amount();
8477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 }
8479 else
8480 {
8481 if (lookfor == LOOKFOR_INITIAL
8482 && *l != NUL
8483 && l[STRLEN(l) - 1] == '\\')
8484 /* XXX */
8485 cont_amount = cin_get_equal_amount(
8486 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008487 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008488 && lookfor != LOOKFOR_JS_KEY
8489 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 lookfor = LOOKFOR_UNTERM;
8491 }
8492 }
8493 }
8494 }
8495
8496 /*
8497 * Check if we are after a while (cond);
8498 * If so: Ignore until the matching "do".
8499 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008500 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {
8502 /*
8503 * Found an unterminated line after a while ();, line up
8504 * with the last one.
8505 * while (cond);
8506 * 100 + <- line up with this one
8507 * -> here;
8508 */
8509 if (lookfor == LOOKFOR_UNTERM
8510 || lookfor == LOOKFOR_ENUM_OR_INIT)
8511 {
8512 if (cont_amount > 0)
8513 amount = cont_amount;
8514 else
8515 amount += ind_continuation;
8516 break;
8517 }
8518
8519 if (whilelevel == 0)
8520 {
8521 lookfor = LOOKFOR_TERM;
8522 amount = get_indent(); /* XXX */
8523 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008524 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 }
8526 ++whilelevel;
8527 }
8528
8529 /*
8530 * We are after a "normal" statement.
8531 * If we had another statement we can stop now and use the
8532 * indent of that other statement.
8533 * Otherwise the indent of the current statement may be used,
8534 * search backwards for the next "normal" statement.
8535 */
8536 else
8537 {
8538 /*
8539 * Skip single break line, if before a switch label. It
8540 * may be lined up with the case label.
8541 */
8542 if (lookfor == LOOKFOR_NOBREAK
8543 && cin_isbreak(skipwhite(ml_get_curline())))
8544 {
8545 lookfor = LOOKFOR_ANY;
8546 continue;
8547 }
8548
8549 /*
8550 * Handle "do {" line.
8551 */
8552 if (whilelevel > 0)
8553 {
8554 l = cin_skipcomment(ml_get_curline());
8555 if (cin_isdo(l))
8556 {
8557 amount = get_indent(); /* XXX */
8558 --whilelevel;
8559 continue;
8560 }
8561 }
8562
8563 /*
8564 * Found a terminated line above an unterminated line. Add
8565 * the amount for a continuation line.
8566 * x = 1;
8567 * y = foo +
8568 * -> here;
8569 * or
8570 * int x = 1;
8571 * int foo,
8572 * -> here;
8573 */
8574 if (lookfor == LOOKFOR_UNTERM
8575 || lookfor == LOOKFOR_ENUM_OR_INIT)
8576 {
8577 if (cont_amount > 0)
8578 amount = cont_amount;
8579 else
8580 amount += ind_continuation;
8581 break;
8582 }
8583
8584 /*
8585 * Found a terminated line above a terminated line or "if"
8586 * etc. line. Use the amount of the line below us.
8587 * x = 1; x = 1;
8588 * if (asdf) y = 2;
8589 * while (asdf) ->here;
8590 * here;
8591 * ->foo;
8592 */
8593 if (lookfor == LOOKFOR_TERM)
8594 {
8595 if (!lookfor_break && whilelevel == 0)
8596 break;
8597 }
8598
8599 /*
8600 * First line above the one we're indenting is terminated.
8601 * To know what needs to be done look further backward for
8602 * a terminated line.
8603 */
8604 else
8605 {
8606 /*
8607 * position the cursor over the rightmost paren, so
8608 * that matching it will take us back to the start of
8609 * the line. Helps for:
8610 * func(asdr,
8611 * asdfasdf);
8612 * here;
8613 */
8614term_again:
8615 l = ml_get_curline();
8616 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008617 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008618 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 {
8620 /*
8621 * Check if we are on a case label now. This is
8622 * handled above.
8623 * case xx: if ( asdf &&
8624 * asdf)
8625 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008626 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008628 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {
8630 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008631 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 continue;
8633 }
8634 }
8635
8636 /* When aligning with the case statement, don't align
8637 * with a statement after it.
8638 * case 1: { <-- don't use this { position
8639 * stat;
8640 * }
8641 * case 2:
8642 * stat;
8643 * }
8644 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008645 iscase = (curbuf->b_ind_keep_case_label
8646 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647
8648 /*
8649 * Get indent and pointer to text for current line,
8650 * ignoring any jump label.
8651 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008652 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008655 amount += curbuf->b_ind_open_extra;
8656 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008657 l = skipwhite(l);
8658 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008659 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8661
8662 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008663 * When a terminated line starts with "else" skip to
8664 * the matching "if":
8665 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008666 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008667 * Need to use the scope of this "else". XXX
8668 * If whilelevel != 0 continue looking for a "do {".
8669 */
8670 if (lookfor == LOOKFOR_TERM
8671 && *l != '}'
8672 && cin_iselse(l)
8673 && whilelevel == 0)
8674 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008675 if ((trypos = find_start_brace()) == NULL
8676 || find_match(LOOKFOR_IF, trypos->lnum)
8677 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008678 break;
8679 continue;
8680 }
8681
8682 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 * If we're at the end of a block, skip to the start of
8684 * that block.
8685 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008686 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008687 if (find_last_paren(l, '{', '}') /* XXX */
8688 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008690 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 /* if not "else {" check for terminated again */
8692 /* but skip block for "} else {" */
8693 l = cin_skipcomment(ml_get_curline());
8694 if (*l == '}' || !cin_iselse(l))
8695 goto term_again;
8696 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008697 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 }
8699 }
8700 }
8701 }
8702 }
8703 }
8704
8705 /* add extra indent for a comment */
8706 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008707 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008708
8709 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008710 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8711 amount -= curbuf->b_ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 else
8714 {
8715 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008716 * ok -- we're not inside any sort of structure at all!
8717 *
8718 * This means we're at the top level, and everything should
8719 * basically just match where the previous line is, except
8720 * for the lines immediately following a function declaration,
8721 * which are K&R-style parameters and need to be indented.
8722 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 * if our line starts with an open brace, forget about any
8724 * prevailing indent and make sure it looks like the start
8725 * of a function
8726 */
8727
8728 if (theline[0] == '{')
8729 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008730 amount = curbuf->b_ind_first_open;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 }
8732
8733 /*
8734 * If the NEXT line is a function declaration, the current
8735 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008736 * Don't do this if the current line looks like a comment or if the
8737 * current line is terminated, ie. ends in ';', or if the current line
8738 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 */
8740 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8741 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008742 && vim_strchr(theline, '{') == NULL
8743 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 && !cin_ends_in(theline, (char_u *)":", NULL)
8745 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008746 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008747 cur_curpos.lnum + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 && !cin_isterminated(theline, FALSE, TRUE))
8749 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008750 amount = curbuf->b_ind_func_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
8752 else
8753 {
8754 amount = 0;
8755 curwin->w_cursor = cur_curpos;
8756
8757 /* search backwards until we find something we recognize */
8758
8759 while (curwin->w_cursor.lnum > 1)
8760 {
8761 curwin->w_cursor.lnum--;
8762 curwin->w_cursor.col = 0;
8763
8764 l = ml_get_curline();
8765
8766 /*
8767 * If we're in a comment now, skip to the start of the comment.
8768 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008769 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 {
8771 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008772 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 continue;
8774 }
8775
8776 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008777 * Are we at the start of a cpp base class declaration or
8778 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008780 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008781 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar18144c82006-04-12 21:52:12 +00008782 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008783 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008784 l = ml_get_curline();
8785 }
8786 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008788 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008789 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 break;
8791 }
8792
8793 /*
8794 * Skip preprocessor directives and blank lines.
8795 */
8796 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8797 continue;
8798
8799 if (cin_nocode(l))
8800 continue;
8801
8802 /*
8803 * If the previous line ends in ',', use one level of
8804 * indentation:
8805 * int foo,
8806 * bar;
8807 * do this before checking for '}' in case of eg.
8808 * enum foobar
8809 * {
8810 * ...
8811 * } foo,
8812 * bar;
8813 */
8814 n = 0;
8815 if (cin_ends_in(l, (char_u *)",", NULL)
8816 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8817 {
8818 /* take us back to opening paren */
8819 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008820 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008821 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008822 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823
8824 /* For a line ending in ',' that is a continuation line go
8825 * back to the first line with a backslash:
8826 * char *foo = "bla\
8827 * bla",
8828 * here;
8829 */
8830 while (n == 0 && curwin->w_cursor.lnum > 1)
8831 {
8832 l = ml_get(curwin->w_cursor.lnum - 1);
8833 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8834 break;
8835 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008836 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 }
8838
8839 amount = get_indent(); /* XXX */
8840
8841 if (amount == 0)
8842 amount = cin_first_id_amount();
8843 if (amount == 0)
8844 amount = ind_continuation;
8845 break;
8846 }
8847
8848 /*
8849 * If the line looks like a function declaration, and we're
8850 * not in a comment, put it the left margin.
8851 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008852 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 break;
8854 l = ml_get_curline();
8855
8856 /*
8857 * Finding the closing '}' of a previous function. Put
8858 * current line at the left margin. For when 'cino' has "fs".
8859 */
8860 if (*skipwhite(l) == '}')
8861 break;
8862
8863 /* (matching {)
8864 * If the previous line ends on '};' (maybe followed by
8865 * comments) align at column 0. For example:
8866 * char *string_array[] = { "foo",
8867 * / * x * / "b};ar" }; / * foobar * /
8868 */
8869 if (cin_ends_in(l, (char_u *)"};", NULL))
8870 break;
8871
8872 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008873 * If the previous line ends on '[' we are probably in an
8874 * array constant:
8875 * something = [
8876 * 234, <- extra indent
8877 */
8878 if (cin_ends_in(l, (char_u *)"[", NULL))
8879 {
8880 amount = get_indent() + ind_continuation;
8881 break;
8882 }
8883
8884 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008885 * Find a line only has a semicolon that belongs to a previous
8886 * line ending in '}', e.g. before an #endif. Don't increase
8887 * indent then.
8888 */
8889 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8890 {
8891 pos_T curpos_save = curwin->w_cursor;
8892
8893 while (curwin->w_cursor.lnum > 1)
8894 {
8895 look = ml_get(--curwin->w_cursor.lnum);
8896 if (!(cin_nocode(look) || cin_ispreproc_cont(
8897 &look, &curwin->w_cursor.lnum)))
8898 break;
8899 }
8900 if (curwin->w_cursor.lnum > 0
8901 && cin_ends_in(look, (char_u *)"}", NULL))
8902 break;
8903
8904 curwin->w_cursor = curpos_save;
8905 }
8906
8907 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 * If the PREVIOUS line is a function declaration, the current
8909 * line (and the ones that follow) needs to be indented as
8910 * parameters.
8911 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008912 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008914 amount = curbuf->b_ind_param;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 break;
8916 }
8917
8918 /*
8919 * If the previous line ends in ';' and the line before the
8920 * previous line ends in ',' or '\', ident to column zero:
8921 * int foo,
8922 * bar;
8923 * indent_to_0 here;
8924 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008925 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 {
8927 l = ml_get(curwin->w_cursor.lnum - 1);
8928 if (cin_ends_in(l, (char_u *)",", NULL)
8929 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8930 break;
8931 l = ml_get_curline();
8932 }
8933
8934 /*
8935 * Doesn't look like anything interesting -- so just
8936 * use the indent of this line.
8937 *
8938 * Position the cursor over the rightmost paren, so that
8939 * matching it will take us back to the start of the line.
8940 */
8941 find_last_paren(l, '(', ')');
8942
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008943 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008944 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 amount = get_indent(); /* XXX */
8946 break;
8947 }
8948
8949 /* add extra indent for a comment */
8950 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008951 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
8953 /* add extra indent if the previous line ended in a backslash:
8954 * "asdfasdf\
8955 * here";
8956 * char *foo = "asdf\
8957 * here";
8958 */
8959 if (cur_curpos.lnum > 1)
8960 {
8961 l = ml_get(cur_curpos.lnum - 1);
8962 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8963 {
8964 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8965 if (cur_amount > 0)
8966 amount = cur_amount;
8967 else if (cur_amount == 0)
8968 amount += ind_continuation;
8969 }
8970 }
8971 }
8972 }
8973
8974theend:
8975 /* put the cursor back where it belongs */
8976 curwin->w_cursor = cur_curpos;
8977
8978 vim_free(linecopy);
8979
8980 if (amount < 0)
8981 return 0;
8982 return amount;
8983}
8984
8985 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008986find_match(lookfor, ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 int lookfor;
8988 linenr_T ourscope;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 char_u *look;
8991 pos_T *theirscope;
8992 char_u *mightbeif;
8993 int elselevel;
8994 int whilelevel;
8995
8996 if (lookfor == LOOKFOR_IF)
8997 {
8998 elselevel = 1;
8999 whilelevel = 0;
9000 }
9001 else
9002 {
9003 elselevel = 0;
9004 whilelevel = 1;
9005 }
9006
9007 curwin->w_cursor.col = 0;
9008
9009 while (curwin->w_cursor.lnum > ourscope + 1)
9010 {
9011 curwin->w_cursor.lnum--;
9012 curwin->w_cursor.col = 0;
9013
9014 look = cin_skipcomment(ml_get_curline());
9015 if (cin_iselse(look)
9016 || cin_isif(look)
9017 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009018 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 {
9020 /*
9021 * if we've gone outside the braces entirely,
9022 * we must be out of scope...
9023 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009024 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 if (theirscope == NULL)
9026 break;
9027
9028 /*
9029 * and if the brace enclosing this is further
9030 * back than the one enclosing the else, we're
9031 * out of luck too.
9032 */
9033 if (theirscope->lnum < ourscope)
9034 break;
9035
9036 /*
9037 * and if they're enclosed in a *deeper* brace,
9038 * then we can ignore it because it's in a
9039 * different scope...
9040 */
9041 if (theirscope->lnum > ourscope)
9042 continue;
9043
9044 /*
9045 * if it was an "else" (that's not an "else if")
9046 * then we need to go back to another if, so
9047 * increment elselevel
9048 */
9049 look = cin_skipcomment(ml_get_curline());
9050 if (cin_iselse(look))
9051 {
9052 mightbeif = cin_skipcomment(look + 4);
9053 if (!cin_isif(mightbeif))
9054 ++elselevel;
9055 continue;
9056 }
9057
9058 /*
9059 * if it was a "while" then we need to go back to
9060 * another "do", so increment whilelevel. XXX
9061 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009062 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 {
9064 ++whilelevel;
9065 continue;
9066 }
9067
9068 /* If it's an "if" decrement elselevel */
9069 look = cin_skipcomment(ml_get_curline());
9070 if (cin_isif(look))
9071 {
9072 elselevel--;
9073 /*
9074 * When looking for an "if" ignore "while"s that
9075 * get in the way.
9076 */
9077 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9078 whilelevel = 0;
9079 }
9080
9081 /* If it's a "do" decrement whilelevel */
9082 if (cin_isdo(look))
9083 whilelevel--;
9084
9085 /*
9086 * if we've used up all the elses, then
9087 * this must be the if that we want!
9088 * match the indent level of that if.
9089 */
9090 if (elselevel <= 0 && whilelevel <= 0)
9091 {
9092 return OK;
9093 }
9094 }
9095 }
9096 return FAIL;
9097}
9098
9099# if defined(FEAT_EVAL) || defined(PROTO)
9100/*
9101 * Get indent level from 'indentexpr'.
9102 */
9103 int
9104get_expr_indent()
9105{
9106 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009107 pos_T save_pos;
9108 colnr_T save_curswant;
9109 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009111 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9112 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009114 /* Save and restore cursor position and curswant, in case it was changed
9115 * via :normal commands */
9116 save_pos = curwin->w_cursor;
9117 save_curswant = curwin->w_curswant;
9118 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009120 if (use_sandbox)
9121 ++sandbox;
9122 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009124 if (use_sandbox)
9125 --sandbox;
9126 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127
9128 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9129 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9130 * command. */
9131 save_State = State;
9132 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009133 curwin->w_cursor = save_pos;
9134 curwin->w_curswant = save_curswant;
9135 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 check_cursor();
9137 State = save_State;
9138
9139 /* If there is an error, just keep the current indent. */
9140 if (indent < 0)
9141 indent = get_indent();
9142
9143 return indent;
9144}
9145# endif
9146
9147#endif /* FEAT_CINDENT */
9148
9149#if defined(FEAT_LISP) || defined(PROTO)
9150
9151static int lisp_match __ARGS((char_u *p));
9152
9153 static int
9154lisp_match(p)
9155 char_u *p;
9156{
9157 char_u buf[LSIZE];
9158 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009159 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160
9161 while (*word != NUL)
9162 {
9163 (void)copy_option_part(&word, buf, LSIZE, ",");
9164 len = (int)STRLEN(buf);
9165 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9166 return TRUE;
9167 }
9168 return FALSE;
9169}
9170
9171/*
9172 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9173 * The incompatible newer method is quite a bit better at indenting
9174 * code in lisp-like languages than the traditional one; it's still
9175 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9176 *
9177 * TODO:
9178 * Findmatch() should be adapted for lisp, also to make showmatch
9179 * work correctly: now (v5.3) it seems all C/C++ oriented:
9180 * - it does not recognize the #\( and #\) notations as character literals
9181 * - it doesn't know about comments starting with a semicolon
9182 * - it incorrectly interprets '(' as a character literal
9183 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009184 * Update from Sergey Khorev:
9185 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 */
9187 int
9188get_lisp_indent()
9189{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009190 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 int amount;
9192 char_u *that;
9193 colnr_T col;
9194 colnr_T firsttry;
9195 int parencount, quotecount;
9196 int vi_lisp;
9197
9198 /* Set vi_lisp to use the vi-compatible method */
9199 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9200
9201 realpos = curwin->w_cursor;
9202 curwin->w_cursor.col = 0;
9203
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009204 if ((pos = findmatch(NULL, '(')) == NULL)
9205 pos = findmatch(NULL, '[');
9206 else
9207 {
9208 paren = *pos;
9209 pos = findmatch(NULL, '[');
9210 if (pos == NULL || ltp(pos, &paren))
9211 pos = &paren;
9212 }
9213 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 {
9215 /* Extra trick: Take the indent of the first previous non-white
9216 * line that is at the same () level. */
9217 amount = -1;
9218 parencount = 0;
9219
9220 while (--curwin->w_cursor.lnum >= pos->lnum)
9221 {
9222 if (linewhite(curwin->w_cursor.lnum))
9223 continue;
9224 for (that = ml_get_curline(); *that != NUL; ++that)
9225 {
9226 if (*that == ';')
9227 {
9228 while (*(that + 1) != NUL)
9229 ++that;
9230 continue;
9231 }
9232 if (*that == '\\')
9233 {
9234 if (*(that + 1) != NUL)
9235 ++that;
9236 continue;
9237 }
9238 if (*that == '"' && *(that + 1) != NUL)
9239 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009240 while (*++that && *that != '"')
9241 {
9242 /* skipping escaped characters in the string */
9243 if (*that == '\\')
9244 {
9245 if (*++that == NUL)
9246 break;
9247 if (that[1] == NUL)
9248 {
9249 ++that;
9250 break;
9251 }
9252 }
9253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009255 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009257 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 --parencount;
9259 }
9260 if (parencount == 0)
9261 {
9262 amount = get_indent();
9263 break;
9264 }
9265 }
9266
9267 if (amount == -1)
9268 {
9269 curwin->w_cursor.lnum = pos->lnum;
9270 curwin->w_cursor.col = pos->col;
9271 col = pos->col;
9272
9273 that = ml_get_curline();
9274
9275 if (vi_lisp && get_indent() == 0)
9276 amount = 2;
9277 else
9278 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009279 char_u *line = that;
9280
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 amount = 0;
9282 while (*that && col)
9283 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009284 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 col--;
9286 }
9287
9288 /*
9289 * Some keywords require "body" indenting rules (the
9290 * non-standard-lisp ones are Scheme special forms):
9291 *
9292 * (let ((a 1)) instead (let ((a 1))
9293 * (...)) of (...))
9294 */
9295
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009296 if (!vi_lisp && (*that == '(' || *that == '[')
9297 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 amount += 2;
9299 else
9300 {
9301 that++;
9302 amount++;
9303 firsttry = amount;
9304
9305 while (vim_iswhite(*that))
9306 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009307 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 ++that;
9309 }
9310
9311 if (*that && *that != ';') /* not a comment line */
9312 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009313 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009315 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 firsttry++;
9317
9318 parencount = 0;
9319 quotecount = 0;
9320
9321 if (vi_lisp
9322 || (*that != '"'
9323 && *that != '\''
9324 && *that != '#'
9325 && (*that < '0' || *that > '9')))
9326 {
9327 while (*that
9328 && (!vim_iswhite(*that)
9329 || quotecount
9330 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009331 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 && !quotecount
9333 && !parencount
9334 && vi_lisp)))
9335 {
9336 if (*that == '"')
9337 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009338 if ((*that == '(' || *that == '[')
9339 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009341 if ((*that == ')' || *that == ']')
9342 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 --parencount;
9344 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009345 amount += lbr_chartabsize_adv(
9346 line, &that, (colnr_T)amount);
9347 amount += lbr_chartabsize_adv(
9348 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 }
9350 }
9351 while (vim_iswhite(*that))
9352 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009353 amount += lbr_chartabsize(
9354 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 that++;
9356 }
9357 if (!*that || *that == ';')
9358 amount = firsttry;
9359 }
9360 }
9361 }
9362 }
9363 }
9364 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009365 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366
9367 curwin->w_cursor = realpos;
9368
9369 return amount;
9370}
9371#endif /* FEAT_LISP */
9372
9373 void
9374prepare_to_exit()
9375{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009376#if defined(SIGHUP) && defined(SIG_IGN)
9377 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9378 * makes Vim exit and then handling SIGHUP causes various reentrance
9379 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009380 signal(SIGHUP, SIG_IGN);
9381#endif
9382
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383#ifdef FEAT_GUI
9384 if (gui.in_use)
9385 {
9386 gui.dying = TRUE;
9387 out_trash(); /* trash any pending output */
9388 }
9389 else
9390#endif
9391 {
9392 windgoto((int)Rows - 1, 0);
9393
9394 /*
9395 * Switch terminal mode back now, so messages end up on the "normal"
9396 * screen (if there are two screens).
9397 */
9398 settmode(TMODE_COOK);
9399#ifdef WIN3264
9400 if (can_end_termcap_mode(FALSE) == TRUE)
9401#endif
9402 stoptermcap();
9403 out_flush();
9404 }
9405}
9406
9407/*
9408 * Preserve files and exit.
9409 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009410 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9411 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 */
9413 void
9414preserve_exit()
9415{
9416 buf_T *buf;
9417
9418 prepare_to_exit();
9419
Bram Moolenaar4770d092006-01-12 23:22:24 +00009420 /* Setting this will prevent free() calls. That avoids calling free()
9421 * recursively when free() was invoked with a bad pointer. */
9422 really_exiting = TRUE;
9423
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 out_str(IObuff);
9425 screen_start(); /* don't know where cursor is now */
9426 out_flush();
9427
9428 ml_close_notmod(); /* close all not-modified buffers */
9429
9430 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9431 {
9432 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9433 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009434 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 screen_start(); /* don't know where cursor is now */
9436 out_flush();
9437 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9438 break;
9439 }
9440 }
9441
9442 ml_close_all(FALSE); /* close all memfiles, without deleting */
9443
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009444 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445
9446 getout(1);
9447}
9448
9449/*
9450 * return TRUE if "fname" exists.
9451 */
9452 int
9453vim_fexists(fname)
9454 char_u *fname;
9455{
9456 struct stat st;
9457
9458 if (mch_stat((char *)fname, &st))
9459 return FALSE;
9460 return TRUE;
9461}
9462
9463/*
9464 * Check for CTRL-C pressed, but only once in a while.
9465 * Should be used instead of ui_breakcheck() for functions that check for
9466 * each line in the file. Calling ui_breakcheck() each time takes too much
9467 * time, because it can be a system call.
9468 */
9469
9470#ifndef BREAKCHECK_SKIP
9471# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9472# define BREAKCHECK_SKIP 200
9473# else
9474# define BREAKCHECK_SKIP 32
9475# endif
9476#endif
9477
9478static int breakcheck_count = 0;
9479
9480 void
9481line_breakcheck()
9482{
9483 if (++breakcheck_count >= BREAKCHECK_SKIP)
9484 {
9485 breakcheck_count = 0;
9486 ui_breakcheck();
9487 }
9488}
9489
9490/*
9491 * Like line_breakcheck() but check 10 times less often.
9492 */
9493 void
9494fast_breakcheck()
9495{
9496 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9497 {
9498 breakcheck_count = 0;
9499 ui_breakcheck();
9500 }
9501}
9502
9503/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009504 * Invoke expand_wildcards() for one pattern.
9505 * Expand items like "%:h" before the expansion.
9506 * Returns OK or FAIL.
9507 */
9508 int
9509expand_wildcards_eval(pat, num_file, file, flags)
9510 char_u **pat; /* pointer to input pattern */
9511 int *num_file; /* resulting number of files */
9512 char_u ***file; /* array of resulting files */
9513 int flags; /* EW_DIR, etc. */
9514{
9515 int ret = FAIL;
9516 char_u *eval_pat = NULL;
9517 char_u *exp_pat = *pat;
9518 char_u *ignored_msg;
9519 int usedlen;
9520
9521 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9522 {
9523 ++emsg_off;
9524 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9525 NULL, &ignored_msg, NULL);
9526 --emsg_off;
9527 if (eval_pat != NULL)
9528 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9529 }
9530
9531 if (exp_pat != NULL)
9532 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9533
9534 if (eval_pat != NULL)
9535 {
9536 vim_free(exp_pat);
9537 vim_free(eval_pat);
9538 }
9539
9540 return ret;
9541}
9542
9543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9545 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009546 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 */
9548 int
9549expand_wildcards(num_pat, pat, num_file, file, flags)
9550 int num_pat; /* number of input patterns */
9551 char_u **pat; /* array of input patterns */
9552 int *num_file; /* resulting number of files */
9553 char_u ***file; /* array of resulting files */
9554 int flags; /* EW_DIR, etc. */
9555{
9556 int retval;
9557 int i, j;
9558 char_u *p;
9559 int non_suf_match; /* number without matching suffix */
9560
9561 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9562
9563 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009564 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 return retval;
9566
9567#ifdef FEAT_WILDIGN
9568 /*
9569 * Remove names that match 'wildignore'.
9570 */
9571 if (*p_wig)
9572 {
9573 char_u *ffname;
9574
9575 /* check all files in (*file)[] */
9576 for (i = 0; i < *num_file; ++i)
9577 {
9578 ffname = FullName_save((*file)[i], FALSE);
9579 if (ffname == NULL) /* out of memory */
9580 break;
9581# ifdef VMS
9582 vms_remove_version(ffname);
9583# endif
9584 if (match_file_list(p_wig, (*file)[i], ffname))
9585 {
9586 /* remove this matching file from the list */
9587 vim_free((*file)[i]);
9588 for (j = i; j + 1 < *num_file; ++j)
9589 (*file)[j] = (*file)[j + 1];
9590 --*num_file;
9591 --i;
9592 }
9593 vim_free(ffname);
9594 }
9595 }
9596#endif
9597
9598 /*
9599 * Move the names where 'suffixes' match to the end.
9600 */
9601 if (*num_file > 1)
9602 {
9603 non_suf_match = 0;
9604 for (i = 0; i < *num_file; ++i)
9605 {
9606 if (!match_suffix((*file)[i]))
9607 {
9608 /*
9609 * Move the name without matching suffix to the front
9610 * of the list.
9611 */
9612 p = (*file)[i];
9613 for (j = i; j > non_suf_match; --j)
9614 (*file)[j] = (*file)[j - 1];
9615 (*file)[non_suf_match++] = p;
9616 }
9617 }
9618 }
9619
9620 return retval;
9621}
9622
9623/*
9624 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9625 */
9626 int
9627match_suffix(fname)
9628 char_u *fname;
9629{
9630 int fnamelen, setsuflen;
9631 char_u *setsuf;
9632#define MAXSUFLEN 30 /* maximum length of a file suffix */
9633 char_u suf_buf[MAXSUFLEN];
9634
9635 fnamelen = (int)STRLEN(fname);
9636 setsuflen = 0;
9637 for (setsuf = p_su; *setsuf; )
9638 {
9639 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009640 if (setsuflen == 0)
9641 {
9642 char_u *tail = gettail(fname);
9643
9644 /* empty entry: match name without a '.' */
9645 if (vim_strchr(tail, '.') == NULL)
9646 {
9647 setsuflen = 1;
9648 break;
9649 }
9650 }
9651 else
9652 {
9653 if (fnamelen >= setsuflen
9654 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9655 (size_t)setsuflen) == 0)
9656 break;
9657 setsuflen = 0;
9658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 }
9660 return (setsuflen != 0);
9661}
9662
9663#if !defined(NO_EXPANDPATH) || defined(PROTO)
9664
9665# ifdef VIM_BACKTICK
9666static int vim_backtick __ARGS((char_u *p));
9667static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9668# endif
9669
9670# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9671/*
9672 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9673 * it's shared between these systems.
9674 */
9675# if defined(DJGPP) || defined(PROTO)
9676# define _cdecl /* DJGPP doesn't have this */
9677# else
9678# ifdef __BORLANDC__
9679# define _cdecl _RTLENTRYF
9680# endif
9681# endif
9682
9683/*
9684 * comparison function for qsort in dos_expandpath()
9685 */
9686 static int _cdecl
9687pstrcmp(const void *a, const void *b)
9688{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009689 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690}
9691
9692# ifndef WIN3264
9693 static void
9694namelowcpy(
9695 char_u *d,
9696 char_u *s)
9697{
9698# ifdef DJGPP
9699 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9700 while (*s)
9701 *d++ = *s++;
9702 else
9703# endif
9704 while (*s)
9705 *d++ = TOLOWER_LOC(*s++);
9706 *d = NUL;
9707}
9708# endif
9709
9710/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009711 * Recursively expand one path component into all matching files and/or
9712 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 * Return the number of matches found.
9714 * "path" has backslashes before chars that are not to be expanded, starting
9715 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009716 * Return the number of matches found.
9717 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 */
9719 static int
9720dos_expandpath(
9721 garray_T *gap,
9722 char_u *path,
9723 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009724 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009725 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009727 char_u *buf;
9728 char_u *path_end;
9729 char_u *p, *s, *e;
9730 int start_len = gap->ga_len;
9731 char_u *pat;
9732 regmatch_T regmatch;
9733 int starts_with_dot;
9734 int matches;
9735 int len;
9736 int starstar = FALSE;
9737 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#ifdef WIN3264
9739 WIN32_FIND_DATA fb;
9740 HANDLE hFind = (HANDLE)0;
9741# ifdef FEAT_MBYTE
9742 WIN32_FIND_DATAW wfb;
9743 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9744# endif
9745#else
9746 struct ffblk fb;
9747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009749 int ok;
9750
9751 /* Expanding "**" may take a long time, check for CTRL-C. */
9752 if (stardepth > 0)
9753 {
9754 ui_breakcheck();
9755 if (got_int)
9756 return 0;
9757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758
9759 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009760 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 if (buf == NULL)
9762 return 0;
9763
9764 /*
9765 * Find the first part in the path name that contains a wildcard or a ~1.
9766 * Copy it into buf, including the preceding characters.
9767 */
9768 p = buf;
9769 s = buf;
9770 e = NULL;
9771 path_end = path;
9772 while (*path_end != NUL)
9773 {
9774 /* May ignore a wildcard that has a backslash before it; it will
9775 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9776 if (path_end >= path + wildoff && rem_backslash(path_end))
9777 *p++ = *path_end++;
9778 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9779 {
9780 if (e != NULL)
9781 break;
9782 s = p + 1;
9783 }
9784 else if (path_end >= path + wildoff
9785 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9786 e = p;
9787#ifdef FEAT_MBYTE
9788 if (has_mbyte)
9789 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009790 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 STRNCPY(p, path_end, len);
9792 p += len;
9793 path_end += len;
9794 }
9795 else
9796#endif
9797 *p++ = *path_end++;
9798 }
9799 e = p;
9800 *e = NUL;
9801
9802 /* now we have one wildcard component between s and e */
9803 /* Remove backslashes between "wildoff" and the start of the wildcard
9804 * component. */
9805 for (p = buf + wildoff; p < s; ++p)
9806 if (rem_backslash(p))
9807 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009808 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 --e;
9810 --s;
9811 }
9812
Bram Moolenaar231334e2005-07-25 20:46:57 +00009813 /* Check for "**" between "s" and "e". */
9814 for (p = s; p < e; ++p)
9815 if (p[0] == '*' && p[1] == '*')
9816 starstar = TRUE;
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 starts_with_dot = (*s == '.');
9819 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9820 if (pat == NULL)
9821 {
9822 vim_free(buf);
9823 return 0;
9824 }
9825
9826 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009827 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009828 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 regmatch.rm_ic = TRUE; /* Always ignore case */
9830 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009831 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009832 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 vim_free(pat);
9834
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009835 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 {
9837 vim_free(buf);
9838 return 0;
9839 }
9840
9841 /* remember the pattern or file name being looked for */
9842 matchname = vim_strsave(s);
9843
Bram Moolenaar231334e2005-07-25 20:46:57 +00009844 /* If "**" is by itself, this is the first time we encounter it and more
9845 * is following then find matches without any directory. */
9846 if (!didstar && stardepth < 100 && starstar && e - s == 2
9847 && *path_end == '/')
9848 {
9849 STRCPY(s, path_end + 1);
9850 ++stardepth;
9851 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9852 --stardepth;
9853 }
9854
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 /* Scan all files in the directory with "dir/ *.*" */
9856 STRCPY(s, "*.*");
9857#ifdef WIN3264
9858# ifdef FEAT_MBYTE
9859 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9860 {
9861 /* The active codepage differs from 'encoding'. Attempt using the
9862 * wide function. If it fails because it is not implemented fall back
9863 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009864 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 if (wn != NULL)
9866 {
9867 hFind = FindFirstFileW(wn, &wfb);
9868 if (hFind == INVALID_HANDLE_VALUE
9869 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9870 {
9871 vim_free(wn);
9872 wn = NULL;
9873 }
9874 }
9875 }
9876
9877 if (wn == NULL)
9878# endif
9879 hFind = FindFirstFile(buf, &fb);
9880 ok = (hFind != INVALID_HANDLE_VALUE);
9881#else
9882 /* If we are expanding wildcards we try both files and directories */
9883 ok = (findfirst((char *)buf, &fb,
9884 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9885#endif
9886
9887 while (ok)
9888 {
9889#ifdef WIN3264
9890# ifdef FEAT_MBYTE
9891 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009892 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 else
9894# endif
9895 p = (char_u *)fb.cFileName;
9896#else
9897 p = (char_u *)fb.ff_name;
9898#endif
9899 /* Ignore entries starting with a dot, unless when asked for. Accept
9900 * all entries found with "matchname". */
9901 if ((p[0] != '.' || starts_with_dot)
9902 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009903 || (regmatch.regprog != NULL
9904 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009905 || ((flags & EW_NOTWILD)
9906 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 {
9908#ifdef WIN3264
9909 STRCPY(s, p);
9910#else
9911 namelowcpy(s, p);
9912#endif
9913 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009914
9915 if (starstar && stardepth < 100)
9916 {
9917 /* For "**" in the pattern first go deeper in the tree to
9918 * find matches. */
9919 STRCPY(buf + len, "/**");
9920 STRCPY(buf + len + 3, path_end);
9921 ++stardepth;
9922 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9923 --stardepth;
9924 }
9925
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 STRCPY(buf + len, path_end);
9927 if (mch_has_exp_wildcard(path_end))
9928 {
9929 /* need to expand another component of the path */
9930 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009931 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 }
9933 else
9934 {
9935 /* no more wildcards, check if there is a match */
9936 /* remove backslashes for the remaining components only */
9937 if (*path_end != 0)
9938 backslash_halve(buf + len + 1);
9939 if (mch_getperm(buf) >= 0) /* add existing file */
9940 addfile(gap, buf, flags);
9941 }
9942 }
9943
9944#ifdef WIN3264
9945# ifdef FEAT_MBYTE
9946 if (wn != NULL)
9947 {
9948 vim_free(p);
9949 ok = FindNextFileW(hFind, &wfb);
9950 }
9951 else
9952# endif
9953 ok = FindNextFile(hFind, &fb);
9954#else
9955 ok = (findnext(&fb) == 0);
9956#endif
9957
9958 /* If no more matches and no match was used, try expanding the name
9959 * itself. Finds the long name of a short filename. */
9960 if (!ok && matchname != NULL && gap->ga_len == start_len)
9961 {
9962 STRCPY(s, matchname);
9963#ifdef WIN3264
9964 FindClose(hFind);
9965# ifdef FEAT_MBYTE
9966 if (wn != NULL)
9967 {
9968 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009969 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 if (wn != NULL)
9971 hFind = FindFirstFileW(wn, &wfb);
9972 }
9973 if (wn == NULL)
9974# endif
9975 hFind = FindFirstFile(buf, &fb);
9976 ok = (hFind != INVALID_HANDLE_VALUE);
9977#else
9978 ok = (findfirst((char *)buf, &fb,
9979 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9980#endif
9981 vim_free(matchname);
9982 matchname = NULL;
9983 }
9984 }
9985
9986#ifdef WIN3264
9987 FindClose(hFind);
9988# ifdef FEAT_MBYTE
9989 vim_free(wn);
9990# endif
9991#endif
9992 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009993 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 vim_free(matchname);
9995
9996 matches = gap->ga_len - start_len;
9997 if (matches > 0)
9998 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9999 sizeof(char_u *), pstrcmp);
10000 return matches;
10001}
10002
10003 int
10004mch_expandpath(
10005 garray_T *gap,
10006 char_u *path,
10007 int flags) /* EW_* flags */
10008{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010009 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010}
10011# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
10012
Bram Moolenaar231334e2005-07-25 20:46:57 +000010013#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10014 || defined(PROTO)
10015/*
10016 * Unix style wildcard expansion code.
10017 * It's here because it's used both for Unix and Mac.
10018 */
10019static int pstrcmp __ARGS((const void *, const void *));
10020
10021 static int
10022pstrcmp(a, b)
10023 const void *a, *b;
10024{
10025 return (pathcmp(*(char **)a, *(char **)b, -1));
10026}
10027
10028/*
10029 * Recursively expand one path component into all matching files and/or
10030 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10031 * "path" has backslashes before chars that are not to be expanded, starting
10032 * at "path + wildoff".
10033 * Return the number of matches found.
10034 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10035 */
10036 int
10037unix_expandpath(gap, path, wildoff, flags, didstar)
10038 garray_T *gap;
10039 char_u *path;
10040 int wildoff;
10041 int flags; /* EW_* flags */
10042 int didstar; /* expanded "**" once already */
10043{
10044 char_u *buf;
10045 char_u *path_end;
10046 char_u *p, *s, *e;
10047 int start_len = gap->ga_len;
10048 char_u *pat;
10049 regmatch_T regmatch;
10050 int starts_with_dot;
10051 int matches;
10052 int len;
10053 int starstar = FALSE;
10054 static int stardepth = 0; /* depth for "**" expansion */
10055
10056 DIR *dirp;
10057 struct dirent *dp;
10058
10059 /* Expanding "**" may take a long time, check for CTRL-C. */
10060 if (stardepth > 0)
10061 {
10062 ui_breakcheck();
10063 if (got_int)
10064 return 0;
10065 }
10066
10067 /* make room for file name */
10068 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10069 if (buf == NULL)
10070 return 0;
10071
10072 /*
10073 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010074 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010075 * Copy it into "buf", including the preceding characters.
10076 */
10077 p = buf;
10078 s = buf;
10079 e = NULL;
10080 path_end = path;
10081 while (*path_end != NUL)
10082 {
10083 /* May ignore a wildcard that has a backslash before it; it will
10084 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10085 if (path_end >= path + wildoff && rem_backslash(path_end))
10086 *p++ = *path_end++;
10087 else if (*path_end == '/')
10088 {
10089 if (e != NULL)
10090 break;
10091 s = p + 1;
10092 }
10093 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010094 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010095 || (!p_fic && (flags & EW_ICASE)
10096 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010097 e = p;
10098#ifdef FEAT_MBYTE
10099 if (has_mbyte)
10100 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010101 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010102 STRNCPY(p, path_end, len);
10103 p += len;
10104 path_end += len;
10105 }
10106 else
10107#endif
10108 *p++ = *path_end++;
10109 }
10110 e = p;
10111 *e = NUL;
10112
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010113 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010114 /* Remove backslashes between "wildoff" and the start of the wildcard
10115 * component. */
10116 for (p = buf + wildoff; p < s; ++p)
10117 if (rem_backslash(p))
10118 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010119 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010120 --e;
10121 --s;
10122 }
10123
10124 /* Check for "**" between "s" and "e". */
10125 for (p = s; p < e; ++p)
10126 if (p[0] == '*' && p[1] == '*')
10127 starstar = TRUE;
10128
10129 /* convert the file pattern to a regexp pattern */
10130 starts_with_dot = (*s == '.');
10131 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10132 if (pat == NULL)
10133 {
10134 vim_free(buf);
10135 return 0;
10136 }
10137
10138 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010139 if (flags & EW_ICASE)
10140 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10141 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010142 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010143 if (flags & (EW_NOERROR | EW_NOTWILD))
10144 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010145 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010146 if (flags & (EW_NOERROR | EW_NOTWILD))
10147 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010148 vim_free(pat);
10149
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010150 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010151 {
10152 vim_free(buf);
10153 return 0;
10154 }
10155
10156 /* If "**" is by itself, this is the first time we encounter it and more
10157 * is following then find matches without any directory. */
10158 if (!didstar && stardepth < 100 && starstar && e - s == 2
10159 && *path_end == '/')
10160 {
10161 STRCPY(s, path_end + 1);
10162 ++stardepth;
10163 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10164 --stardepth;
10165 }
10166
10167 /* open the directory for scanning */
10168 *s = NUL;
10169 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10170
10171 /* Find all matching entries */
10172 if (dirp != NULL)
10173 {
10174 for (;;)
10175 {
10176 dp = readdir(dirp);
10177 if (dp == NULL)
10178 break;
10179 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010180 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10181 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010182 || ((flags & EW_NOTWILD)
10183 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010184 {
10185 STRCPY(s, dp->d_name);
10186 len = STRLEN(buf);
10187
10188 if (starstar && stardepth < 100)
10189 {
10190 /* For "**" in the pattern first go deeper in the tree to
10191 * find matches. */
10192 STRCPY(buf + len, "/**");
10193 STRCPY(buf + len + 3, path_end);
10194 ++stardepth;
10195 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10196 --stardepth;
10197 }
10198
10199 STRCPY(buf + len, path_end);
10200 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10201 {
10202 /* need to expand another component of the path */
10203 /* remove backslashes for the remaining components only */
10204 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10205 }
10206 else
10207 {
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010208 struct stat sb;
10209
Bram Moolenaar231334e2005-07-25 20:46:57 +000010210 /* no more wildcards, check if there is a match */
10211 /* remove backslashes for the remaining components only */
10212 if (*path_end != NUL)
10213 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010214 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010215 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010216 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010217 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010218#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010219 size_t precomp_len = STRLEN(buf)+1;
10220 char_u *precomp_buf =
10221 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010222
Bram Moolenaar231334e2005-07-25 20:46:57 +000010223 if (precomp_buf)
10224 {
10225 mch_memmove(buf, precomp_buf, precomp_len);
10226 vim_free(precomp_buf);
10227 }
10228#endif
10229 addfile(gap, buf, flags);
10230 }
10231 }
10232 }
10233 }
10234
10235 closedir(dirp);
10236 }
10237
10238 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010239 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010240
10241 matches = gap->ga_len - start_len;
10242 if (matches > 0)
10243 qsort(((char_u **)gap->ga_data) + start_len, matches,
10244 sizeof(char_u *), pstrcmp);
10245 return matches;
10246}
10247#endif
10248
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010249#if defined(FEAT_SEARCHPATH)
10250static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10251static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010252static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10253static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010254static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10255static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10256
10257/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010258 * Moves "*psep" back to the previous path separator in "path".
10259 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010260 */
10261 static int
10262find_previous_pathsep(path, psep)
10263 char_u *path;
10264 char_u **psep;
10265{
10266 /* skip the current separator */
10267 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010268 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010269
10270 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010271 while (*psep > path)
10272 {
10273 if (vim_ispathsep(**psep))
10274 return OK;
10275 mb_ptr_back(path, *psep);
10276 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010277
10278 return FAIL;
10279}
10280
10281/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010282 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10283 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010284 */
10285 static int
10286is_unique(maybe_unique, gap, i)
10287 char_u *maybe_unique;
10288 garray_T *gap;
10289 int i;
10290{
10291 int j;
10292 int candidate_len;
10293 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010294 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010295 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010296
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010297 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010298 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010299 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010300 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010301
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010302 candidate_len = (int)STRLEN(maybe_unique);
10303 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010304 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010305 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010306
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010307 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010308 if (fnamecmp(maybe_unique, rival) == 0
10309 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010310 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010311 }
10312
Bram Moolenaar162bd912010-07-28 22:29:10 +020010313 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010314}
10315
10316/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010317 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010318 * paths are expanded to their equivalent fullpath. This includes the "."
10319 * (relative to current buffer directory) and empty path (relative to current
10320 * directory) notations.
10321 *
10322 * TODO: handle upward search (;) and path limiter (**N) notations by
10323 * expanding each into their equivalent path(s).
10324 */
10325 static void
10326expand_path_option(curdir, gap)
10327 char_u *curdir;
10328 garray_T *gap;
10329{
10330 char_u *path_option = *curbuf->b_p_path == NUL
10331 ? p_path : curbuf->b_p_path;
10332 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010333 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010334 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010335
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010336 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010337 return;
10338
10339 while (*path_option != NUL)
10340 {
10341 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10342
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010343 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010344 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010345 /* Relative to current buffer:
10346 * "/path/file" + "." -> "/path/"
10347 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010348 if (curbuf->b_ffname == NULL)
10349 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010350 p = gettail(curbuf->b_ffname);
10351 len = (int)(p - curbuf->b_ffname);
10352 if (len + (int)STRLEN(buf) >= MAXPATHL)
10353 continue;
10354 if (buf[1] == NUL)
10355 buf[len] = NUL;
10356 else
10357 STRMOVE(buf + len, buf + 2);
10358 mch_memmove(buf, curbuf->b_ffname, len);
10359 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010360 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010361 else if (buf[0] == NUL)
10362 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010363 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010364 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010365 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010366 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010367 else if (!mch_isFullName(buf))
10368 {
10369 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010370 len = (int)STRLEN(curdir);
10371 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010372 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010373 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010374 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010375 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010376 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010377 }
10378
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010379 if (ga_grow(gap, 1) == FAIL)
10380 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010381
10382# if defined(MSWIN) || defined(MSDOS)
10383 /* Avoid the path ending in a backslash, it fails when a comma is
10384 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010385 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010386 if (buf[len - 1] == '\\')
10387 buf[len - 1] = '/';
10388# endif
10389
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010390 p = vim_strsave(buf);
10391 if (p == NULL)
10392 break;
10393 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010394 }
10395
10396 vim_free(buf);
10397}
10398
10399/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010400 * Returns a pointer to the file or directory name in "fname" that matches the
10401 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010402 *
10403 * path: /foo/bar/baz
10404 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010405 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010406 */
10407 static char_u *
10408get_path_cutoff(fname, gap)
10409 char_u *fname;
10410 garray_T *gap;
10411{
10412 int i;
10413 int maxlen = 0;
10414 char_u **path_part = (char_u **)gap->ga_data;
10415 char_u *cutoff = NULL;
10416
10417 for (i = 0; i < gap->ga_len; i++)
10418 {
10419 int j = 0;
10420
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010421 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010422# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010423 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10424#endif
10425 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010426 j++;
10427 if (j > maxlen)
10428 {
10429 maxlen = j;
10430 cutoff = &fname[j];
10431 }
10432 }
10433
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010434 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010435 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010436 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010437 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010438
10439 return cutoff;
10440}
10441
10442/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010443 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10444 * that they are unique with respect to each other while conserving the part
10445 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010446 */
10447 static void
10448uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010449 garray_T *gap;
10450 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010451{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010452 int i;
10453 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010454 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010455 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010456 char_u *pat;
10457 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010458 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010459 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010460 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010461 char_u **in_curdir = NULL;
10462 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010463
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010464 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010465 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010466
10467 /*
10468 * We need to prepend a '*' at the beginning of file_pattern so that the
10469 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010470 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010471 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010472 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010473 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010474 if (file_pattern == NULL)
10475 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010476 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010477 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010478 STRCAT(file_pattern, pattern);
10479 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10480 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010481 if (pat == NULL)
10482 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010483
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010484 regmatch.rm_ic = TRUE; /* always ignore case */
10485 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10486 vim_free(pat);
10487 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010488 return;
10489
Bram Moolenaar162bd912010-07-28 22:29:10 +020010490 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010491 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010492 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010493 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010494
10495 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010496 if (in_curdir == NULL)
10497 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010498
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010499 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010500 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010501 char_u *path = fnames[i];
10502 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010503 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010504 char_u *pathsep_p;
10505 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010506
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010507 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010508 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010509 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010510 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010511 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010512
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010513 /* Shorten the filename while maintaining its uniqueness */
10514 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010515
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010516 /* we start at the end of the path */
10517 pathsep_p = path + len - 1;
10518
10519 while (find_previous_pathsep(path, &pathsep_p))
10520 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10521 && is_unique(pathsep_p + 1, gap, i)
10522 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10523 {
10524 sort_again = TRUE;
10525 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10526 break;
10527 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010528
10529 if (mch_isFullName(path))
10530 {
10531 /*
10532 * Last resort: shorten relative to curdir if possible.
10533 * 'possible' means:
10534 * 1. It is under the current directory.
10535 * 2. The result is actually shorter than the original.
10536 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010537 * Before curdir After
10538 * /foo/bar/file.txt /foo/bar ./file.txt
10539 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10540 * /file.txt / /file.txt
10541 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010542 */
10543 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010544 if (short_name != NULL && short_name > path + 1
10545#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010546 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010547 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010548 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010549 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010550 && !vim_ispathsep(*short_name)
10551#endif
10552 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010553 {
10554 STRCPY(path, ".");
10555 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010556 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010557 }
10558 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010559 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010560 }
10561
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010562 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010563 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010564 {
10565 char_u *rel_path;
10566 char_u *path = in_curdir[i];
10567
10568 if (path == NULL)
10569 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010570
10571 /* If the {filename} is not unique, change it to ./{filename}.
10572 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010573 short_name = shorten_fname(path, curdir);
10574 if (short_name == NULL)
10575 short_name = path;
10576 if (is_unique(short_name, gap, i))
10577 {
10578 STRCPY(fnames[i], short_name);
10579 continue;
10580 }
10581
10582 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10583 if (rel_path == NULL)
10584 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010585 STRCPY(rel_path, ".");
10586 add_pathsep(rel_path);
10587 STRCAT(rel_path, short_name);
10588
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010589 vim_free(fnames[i]);
10590 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010591 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010592 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010593 }
10594
Bram Moolenaar162bd912010-07-28 22:29:10 +020010595theend:
10596 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010597 if (in_curdir != NULL)
10598 {
10599 for (i = 0; i < gap->ga_len; i++)
10600 vim_free(in_curdir[i]);
10601 vim_free(in_curdir);
10602 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010603 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010604 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010605
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010606 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010607 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010608}
10609
10610/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010611 * Calls globpath() with 'path' values for the given pattern and stores the
10612 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010613 * Returns the total number of matches.
10614 */
10615 static int
10616expand_in_path(gap, pattern, flags)
10617 garray_T *gap;
10618 char_u *pattern;
10619 int flags; /* EW_* flags */
10620{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010621 char_u *curdir;
10622 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010623 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010624
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010625 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010626 return 0;
10627 mch_dirname(curdir, MAXPATHL);
10628
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010629 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010630 expand_path_option(curdir, &path_ga);
10631 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010632 if (path_ga.ga_len == 0)
10633 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010634
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010635 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010636 ga_clear_strings(&path_ga);
10637 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010638 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010640 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010641 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010642
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010643 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644}
10645#endif
10646
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010647#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10648/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010649 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10650 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010651 */
10652 void
10653remove_duplicates(gap)
10654 garray_T *gap;
10655{
10656 int i;
10657 int j;
10658 char_u **fnames = (char_u **)gap->ga_data;
10659
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010660 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010661 for (i = gap->ga_len - 1; i > 0; --i)
10662 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10663 {
10664 vim_free(fnames[i]);
10665 for (j = i + 1; j < gap->ga_len; ++j)
10666 fnames[j - 1] = fnames[j];
10667 --gap->ga_len;
10668 }
10669}
10670#endif
10671
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010672static int has_env_var __ARGS((char_u *p));
10673
10674/*
10675 * Return TRUE if "p" contains what looks like an environment variable.
10676 * Allowing for escaping.
10677 */
10678 static int
10679has_env_var(p)
10680 char_u *p;
10681{
10682 for ( ; *p; mb_ptr_adv(p))
10683 {
10684 if (*p == '\\' && p[1] != NUL)
10685 ++p;
10686 else if (vim_strchr((char_u *)
10687#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
10688 "$%"
10689#else
10690 "$"
10691#endif
10692 , *p) != NULL)
10693 return TRUE;
10694 }
10695 return FALSE;
10696}
10697
10698#ifdef SPECIAL_WILDCHAR
10699static int has_special_wildchar __ARGS((char_u *p));
10700
10701/*
10702 * Return TRUE if "p" contains a special wildcard character.
10703 * Allowing for escaping.
10704 */
10705 static int
10706has_special_wildchar(p)
10707 char_u *p;
10708{
10709 for ( ; *p; mb_ptr_adv(p))
10710 {
10711 if (*p == '\\' && p[1] != NUL)
10712 ++p;
10713 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10714 return TRUE;
10715 }
10716 return FALSE;
10717}
10718#endif
10719
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720/*
10721 * Generic wildcard expansion code.
10722 *
10723 * Characters in "pat" that should not be expanded must be preceded with a
10724 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10725 *
10726 * Return FAIL when no single file was found. In this case "num_file" is not
10727 * set, and "file" may contain an error message.
10728 * Return OK when some files found. "num_file" is set to the number of
10729 * matches, "file" to the array of matches. Call FreeWild() later.
10730 */
10731 int
10732gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10733 int num_pat; /* number of input patterns */
10734 char_u **pat; /* array of input patterns */
10735 int *num_file; /* resulting number of files */
10736 char_u ***file; /* array of resulting files */
10737 int flags; /* EW_* flags */
10738{
10739 int i;
10740 garray_T ga;
10741 char_u *p;
10742 static int recursive = FALSE;
10743 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010744#if defined(FEAT_SEARCHPATH)
10745 int did_expand_in_path = FALSE;
10746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747
10748 /*
10749 * expand_env() is called to expand things like "~user". If this fails,
10750 * it calls ExpandOne(), which brings us back here. In this case, always
10751 * call the machine specific expansion function, if possible. Otherwise,
10752 * return FAIL.
10753 */
10754 if (recursive)
10755#ifdef SPECIAL_WILDCHAR
10756 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10757#else
10758 return FAIL;
10759#endif
10760
10761#ifdef SPECIAL_WILDCHAR
10762 /*
10763 * If there are any special wildcard characters which we cannot handle
10764 * here, call machine specific function for all the expansion. This
10765 * avoids starting the shell for each argument separately.
10766 * For `=expr` do use the internal function.
10767 */
10768 for (i = 0; i < num_pat; i++)
10769 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010770 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771# ifdef VIM_BACKTICK
10772 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10773# endif
10774 )
10775 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10776 }
10777#endif
10778
10779 recursive = TRUE;
10780
10781 /*
10782 * The matching file names are stored in a growarray. Init it empty.
10783 */
10784 ga_init2(&ga, (int)sizeof(char_u *), 30);
10785
10786 for (i = 0; i < num_pat; ++i)
10787 {
10788 add_pat = -1;
10789 p = pat[i];
10790
10791#ifdef VIM_BACKTICK
10792 if (vim_backtick(p))
10793 add_pat = expand_backtick(&ga, p, flags);
10794 else
10795#endif
10796 {
10797 /*
10798 * First expand environment variables, "~/" and "~user/".
10799 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010800 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010802 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 if (p == NULL)
10804 p = pat[i];
10805#ifdef UNIX
10806 /*
10807 * On Unix, if expand_env() can't expand an environment
10808 * variable, use the shell to do that. Discard previously
10809 * found file names and start all over again.
10810 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010811 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 {
10813 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010814 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010816 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 recursive = FALSE;
10818 return i;
10819 }
10820#endif
10821 }
10822
10823 /*
10824 * If there are wildcards: Expand file names and add each match to
10825 * the list. If there is no match, and EW_NOTFOUND is given, add
10826 * the pattern.
10827 * If there are no wildcards: Add the file name if it exists or
10828 * when EW_NOTFOUND is given.
10829 */
10830 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010831 {
10832#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010833 if ((flags & EW_PATH)
10834 && !mch_isFullName(p)
10835 && !(p[0] == '.'
10836 && (vim_ispathsep(p[1])
10837 || (p[1] == '.' && vim_ispathsep(p[2]))))
10838 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010839 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010840 /* :find completion where 'path' is used.
10841 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010842 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010843 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010844 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010845 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010846 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010847 else
10848#endif
10849 add_pat = mch_expandpath(&ga, p, flags);
10850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 }
10852
10853 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10854 {
10855 char_u *t = backslash_halve_save(p);
10856
10857#if defined(MACOS_CLASSIC)
10858 slash_to_colon(t);
10859#endif
10860 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10861 * "vim c:/" work. */
10862 if (flags & EW_NOTFOUND)
10863 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10864 else if (mch_getperm(t) >= 0)
10865 addfile(&ga, t, flags);
10866 vim_free(t);
10867 }
10868
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010869#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010870 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010871 uniquefy_paths(&ga, p);
10872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 if (p != pat[i])
10874 vim_free(p);
10875 }
10876
10877 *num_file = ga.ga_len;
10878 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10879
10880 recursive = FALSE;
10881
10882 return (ga.ga_data != NULL) ? OK : FAIL;
10883}
10884
10885# ifdef VIM_BACKTICK
10886
10887/*
10888 * Return TRUE if we can expand this backtick thing here.
10889 */
10890 static int
10891vim_backtick(p)
10892 char_u *p;
10893{
10894 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10895}
10896
10897/*
10898 * Expand an item in `backticks` by executing it as a command.
10899 * Currently only works when pat[] starts and ends with a `.
10900 * Returns number of file names found.
10901 */
10902 static int
10903expand_backtick(gap, pat, flags)
10904 garray_T *gap;
10905 char_u *pat;
10906 int flags; /* EW_* flags */
10907{
10908 char_u *p;
10909 char_u *cmd;
10910 char_u *buffer;
10911 int cnt = 0;
10912 int i;
10913
10914 /* Create the command: lop off the backticks. */
10915 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10916 if (cmd == NULL)
10917 return 0;
10918
10919#ifdef FEAT_EVAL
10920 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010921 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 else
10923#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010924 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010925 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 vim_free(cmd);
10927 if (buffer == NULL)
10928 return 0;
10929
10930 cmd = buffer;
10931 while (*cmd != NUL)
10932 {
10933 cmd = skipwhite(cmd); /* skip over white space */
10934 p = cmd;
10935 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10936 ++p;
10937 /* add an entry if it is not empty */
10938 if (p > cmd)
10939 {
10940 i = *p;
10941 *p = NUL;
10942 addfile(gap, cmd, flags);
10943 *p = i;
10944 ++cnt;
10945 }
10946 cmd = p;
10947 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10948 ++cmd;
10949 }
10950
10951 vim_free(buffer);
10952 return cnt;
10953}
10954# endif /* VIM_BACKTICK */
10955
10956/*
10957 * Add a file to a file list. Accepted flags:
10958 * EW_DIR add directories
10959 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010960 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 * EW_NOTFOUND add even when it doesn't exist
10962 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010963 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 */
10965 void
10966addfile(gap, f, flags)
10967 garray_T *gap;
10968 char_u *f; /* filename */
10969 int flags;
10970{
10971 char_u *p;
10972 int isdir;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010973 struct stat sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010975 /* if the file/dir/link doesn't exist, may not add it */
10976 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010010977 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 return;
10979
10980#ifdef FNAME_ILLEGAL
10981 /* if the file/dir contains illegal characters, don't add it */
10982 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10983 return;
10984#endif
10985
10986 isdir = mch_isdir(f);
10987 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10988 return;
10989
Bram Moolenaarb5971142015-03-21 17:32:19 +010010990 /* If the file isn't executable, may not add it. Do accept directories.
10991 * When invoked from expand_shellcmd() do not use $PATH. */
10992 if (!isdir && (flags & EW_EXEC)
10993 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010994 return;
10995
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 /* Make room for another item in the file list. */
10997 if (ga_grow(gap, 1) == FAIL)
10998 return;
10999
11000 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11001 if (p == NULL)
11002 return;
11003
11004 STRCPY(p, f);
11005#ifdef BACKSLASH_IN_FILENAME
11006 slash_adjust(p);
11007#endif
11008 /*
11009 * Append a slash or backslash after directory names if none is present.
11010 */
11011#ifndef DONT_ADD_PATHSEP_TO_DIR
11012 if (isdir && (flags & EW_ADDSLASH))
11013 add_pathsep(p);
11014#endif
11015 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016}
11017#endif /* !NO_EXPANDPATH */
11018
11019#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11020
11021#ifndef SEEK_SET
11022# define SEEK_SET 0
11023#endif
11024#ifndef SEEK_END
11025# define SEEK_END 2
11026#endif
11027
11028/*
11029 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011030 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11031 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 * Returns an allocated string, or NULL for error.
11033 */
11034 char_u *
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011035get_cmd_output(cmd, infile, flags, ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011037 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 int flags; /* can be SHELL_SILENT */
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011039 int *ret_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040{
11041 char_u *tempname;
11042 char_u *command;
11043 char_u *buffer = NULL;
11044 int len;
11045 int i = 0;
11046 FILE *fd;
11047
11048 if (check_restricted() || check_secure())
11049 return NULL;
11050
11051 /* get a name for the temp file */
11052 if ((tempname = vim_tempname('o')) == NULL)
11053 {
11054 EMSG(_(e_notmp));
11055 return NULL;
11056 }
11057
11058 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011059 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 if (command == NULL)
11061 goto done;
11062
11063 /*
11064 * Call the shell to execute the command (errors are ignored).
11065 * Don't check timestamps here.
11066 */
11067 ++no_check_timestamps;
11068 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11069 --no_check_timestamps;
11070
11071 vim_free(command);
11072
11073 /*
11074 * read the names from the file into memory
11075 */
11076# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011077 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 fd = mch_fopen((char *)tempname, "r");
11079# else
11080 fd = mch_fopen((char *)tempname, READBIN);
11081# endif
11082
11083 if (fd == NULL)
11084 {
11085 EMSG2(_(e_notopen), tempname);
11086 goto done;
11087 }
11088
11089 fseek(fd, 0L, SEEK_END);
11090 len = ftell(fd); /* get size of temp file */
11091 fseek(fd, 0L, SEEK_SET);
11092
11093 buffer = alloc(len + 1);
11094 if (buffer != NULL)
11095 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11096 fclose(fd);
11097 mch_remove(tempname);
11098 if (buffer == NULL)
11099 goto done;
11100#ifdef VMS
11101 len = i; /* VMS doesn't give us what we asked for... */
11102#endif
11103 if (i != len)
11104 {
11105 EMSG2(_(e_notread), tempname);
11106 vim_free(buffer);
11107 buffer = NULL;
11108 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011109 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011110 {
11111 /* Change NUL into SOH, otherwise the string is truncated. */
11112 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011113 if (buffer[i] == NUL)
11114 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011115
Bram Moolenaar162bd912010-07-28 22:29:10 +020011116 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011117 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011118 else
11119 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120
11121done:
11122 vim_free(tempname);
11123 return buffer;
11124}
11125#endif
11126
11127/*
11128 * Free the list of files returned by expand_wildcards() or other expansion
11129 * functions.
11130 */
11131 void
11132FreeWild(count, files)
11133 int count;
11134 char_u **files;
11135{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011136 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 return;
11138#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
11139 /*
11140 * Is this still OK for when other functions than expand_wildcards() have
11141 * been used???
11142 */
11143 _fnexplodefree((char **)files);
11144#else
11145 while (count--)
11146 vim_free(files[count]);
11147 vim_free(files);
11148#endif
11149}
11150
11151/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011152 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 * Don't do this when still processing a command or a mapping.
11154 * Don't do this when inside a ":normal" command.
11155 */
11156 int
11157goto_im()
11158{
11159 return (p_im && stuff_empty() && typebuf_typed());
11160}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011161
11162/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011163 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011164 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11165 * - Remove any argument. E.g., "csh -f" -> "csh".
11166 * But don't allow a space in the path, so that this works:
11167 * "/usr/bin/csh --rcfile ~/.cshrc"
11168 * But don't do that for Windows, it's common to have a space in the path.
11169 */
11170 char_u *
11171get_isolated_shell_name()
11172{
11173 char_u *p;
11174
11175#ifdef WIN3264
11176 p = gettail(p_sh);
11177 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11178#else
11179 p = skiptowhite(p_sh);
11180 if (*p == NUL)
11181 {
11182 /* No white space, use the tail. */
11183 p = vim_strsave(gettail(p_sh));
11184 }
11185 else
11186 {
11187 char_u *p1, *p2;
11188
11189 /* Find the last path separator before the space. */
11190 p1 = p_sh;
11191 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11192 if (vim_ispathsep(*p2))
11193 p1 = p2 + 1;
11194 p = vim_strnsave(p1, (int)(p - p1));
11195 }
11196#endif
11197 return p;
11198}