blob: 08cd20c91d34badb25c373aae6f50f08b0fa4b7e [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
17#ifdef HAVE_FCNTL_H
18# include <fcntl.h> /* for chdir() */
19#endif
20
21static char_u *vim_version_dir __ARGS((char_u *vimdir));
22static char_u *remove_tail __ARGS((char_u *p, char_u *pend, char_u *name));
23#if defined(USE_EXE_NAME) && defined(MACOS_X)
24static char_u *remove_tail_with_ext __ARGS((char_u *p, char_u *pend, char_u *ext));
25#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026static int copy_indent __ARGS((int size, char_u *src));
27
28/*
29 * Count the size (in window cells) of the indent in the current line.
30 */
31 int
32get_indent()
33{
34 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts);
35}
36
37/*
38 * Count the size (in window cells) of the indent in line "lnum".
39 */
40 int
41get_indent_lnum(lnum)
42 linenr_T lnum;
43{
44 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts);
45}
46
47#if defined(FEAT_FOLDING) || defined(PROTO)
48/*
49 * Count the size (in window cells) of the indent in line "lnum" of buffer
50 * "buf".
51 */
52 int
53get_indent_buf(buf, lnum)
54 buf_T *buf;
55 linenr_T lnum;
56{
57 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts);
58}
59#endif
60
61/*
62 * count the size (in window cells) of the indent in line "ptr", with
63 * 'tabstop' at "ts"
64 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000065 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000066get_indent_str(ptr, ts)
67 char_u *ptr;
68 int ts;
69{
70 int count = 0;
71
72 for ( ; *ptr; ++ptr)
73 {
74 if (*ptr == TAB) /* count a tab for what it is worth */
75 count += ts - (count % ts);
76 else if (*ptr == ' ')
77 ++count; /* count a space for one */
78 else
79 break;
80 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000081 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000082}
83
84/*
85 * Set the indent of the current line.
86 * Leaves the cursor on the first non-blank in the line.
87 * Caller must take care of undo.
88 * "flags":
89 * SIN_CHANGED: call changed_bytes() if the line was changed.
90 * SIN_INSERT: insert the indent in front of the line.
91 * SIN_UNDO: save line for undo before changing it.
92 * Returns TRUE if the line was changed.
93 */
94 int
95set_indent(size, flags)
96 int size;
97 int flags;
98{
99 char_u *p;
100 char_u *newline;
101 char_u *oldline;
102 char_u *s;
103 int todo;
104 int ind_len;
105 int line_len;
106 int doit = FALSE;
107 int ind_done;
108 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000109 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
111 /*
112 * First check if there is anything to do and compute the number of
113 * characters needed for the indent.
114 */
115 todo = size;
116 ind_len = 0;
117 p = oldline = ml_get_curline();
118
119 /* Calculate the buffer size for the new indent, and check to see if it
120 * isn't already set */
121
122 /* if 'expandtab' isn't set: use TABs */
123 if (!curbuf->b_p_et)
124 {
125 /* If 'preserveindent' is set then reuse as much as possible of
126 * the existing indent structure for the new indent */
127 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
128 {
129 ind_done = 0;
130
131 /* count as many characters as we can use */
132 while (todo > 0 && vim_iswhite(*p))
133 {
134 if (*p == TAB)
135 {
136 tab_pad = (int)curbuf->b_p_ts
137 - (ind_done % (int)curbuf->b_p_ts);
138 /* stop if this tab will overshoot the target */
139 if (todo < tab_pad)
140 break;
141 todo -= tab_pad;
142 ++ind_len;
143 ind_done += tab_pad;
144 }
145 else
146 {
147 --todo;
148 ++ind_len;
149 ++ind_done;
150 }
151 ++p;
152 }
153
154 /* Fill to next tabstop with a tab, if possible */
155 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
156 if (todo >= tab_pad)
157 {
158 doit = TRUE;
159 todo -= tab_pad;
160 ++ind_len;
161 /* ind_done += tab_pad; */
162 }
163 }
164
165 /* count tabs required for indent */
166 while (todo >= (int)curbuf->b_p_ts)
167 {
168 if (*p != TAB)
169 doit = TRUE;
170 else
171 ++p;
172 todo -= (int)curbuf->b_p_ts;
173 ++ind_len;
174 /* ind_done += (int)curbuf->b_p_ts; */
175 }
176 }
177 /* count spaces required for indent */
178 while (todo > 0)
179 {
180 if (*p != ' ')
181 doit = TRUE;
182 else
183 ++p;
184 --todo;
185 ++ind_len;
186 /* ++ind_done; */
187 }
188
189 /* Return if the indent is OK already. */
190 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
191 return FALSE;
192
193 /* Allocate memory for the new line. */
194 if (flags & SIN_INSERT)
195 p = oldline;
196 else
197 p = skipwhite(p);
198 line_len = (int)STRLEN(p) + 1;
199 newline = alloc(ind_len + line_len);
200 if (newline == NULL)
201 return FALSE;
202
203 /* Put the characters in the new line. */
204 s = newline;
205 todo = size;
206 /* if 'expandtab' isn't set: use TABs */
207 if (!curbuf->b_p_et)
208 {
209 /* If 'preserveindent' is set then reuse as much as possible of
210 * the existing indent structure for the new indent */
211 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
212 {
213 p = oldline;
214 ind_done = 0;
215
216 while (todo > 0 && vim_iswhite(*p))
217 {
218 if (*p == TAB)
219 {
220 tab_pad = (int)curbuf->b_p_ts
221 - (ind_done % (int)curbuf->b_p_ts);
222 /* stop if this tab will overshoot the target */
223 if (todo < tab_pad)
224 break;
225 todo -= tab_pad;
226 ind_done += tab_pad;
227 }
228 else
229 {
230 --todo;
231 ++ind_done;
232 }
233 *s++ = *p++;
234 }
235
236 /* Fill to next tabstop with a tab, if possible */
237 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
238 if (todo >= tab_pad)
239 {
240 *s++ = TAB;
241 todo -= tab_pad;
242 }
243
244 p = skipwhite(p);
245 }
246
247 while (todo >= (int)curbuf->b_p_ts)
248 {
249 *s++ = TAB;
250 todo -= (int)curbuf->b_p_ts;
251 }
252 }
253 while (todo > 0)
254 {
255 *s++ = ' ';
256 --todo;
257 }
258 mch_memmove(s, p, (size_t)line_len);
259
260 /* Replace the line (unless undo fails). */
261 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
262 {
263 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
264 if (flags & SIN_CHANGED)
265 changed_bytes(curwin->w_cursor.lnum, 0);
266 /* Correct saved cursor position if it's after the indent. */
267 if (saved_cursor.lnum == curwin->w_cursor.lnum
268 && saved_cursor.col >= (colnr_T)(p - oldline))
269 saved_cursor.col += ind_len - (p - oldline);
Bram Moolenaar5409c052005-03-18 20:27:04 +0000270 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 }
272 else
273 vim_free(newline);
274
275 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000276 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277}
278
279/*
280 * Copy the indent from ptr to the current line (and fill to size)
281 * Leaves the cursor on the first non-blank in the line.
282 * Returns TRUE if the line was changed.
283 */
284 static int
285copy_indent(size, src)
286 int size;
287 char_u *src;
288{
289 char_u *p = NULL;
290 char_u *line = NULL;
291 char_u *s;
292 int todo;
293 int ind_len;
294 int line_len = 0;
295 int tab_pad;
296 int ind_done;
297 int round;
298
299 /* Round 1: compute the number of characters needed for the indent
300 * Round 2: copy the characters. */
301 for (round = 1; round <= 2; ++round)
302 {
303 todo = size;
304 ind_len = 0;
305 ind_done = 0;
306 s = src;
307
308 /* Count/copy the usable portion of the source line */
309 while (todo > 0 && vim_iswhite(*s))
310 {
311 if (*s == TAB)
312 {
313 tab_pad = (int)curbuf->b_p_ts
314 - (ind_done % (int)curbuf->b_p_ts);
315 /* Stop if this tab will overshoot the target */
316 if (todo < tab_pad)
317 break;
318 todo -= tab_pad;
319 ind_done += tab_pad;
320 }
321 else
322 {
323 --todo;
324 ++ind_done;
325 }
326 ++ind_len;
327 if (round == 2)
328 *p++ = *s;
329 ++s;
330 }
331
332 /* Fill to next tabstop with a tab, if possible */
333 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
334 if (todo >= tab_pad)
335 {
336 todo -= tab_pad;
337 ++ind_len;
338 if (round == 2)
339 *p++ = TAB;
340 }
341
342 /* Add tabs required for indent */
343 while (todo >= (int)curbuf->b_p_ts)
344 {
345 todo -= (int)curbuf->b_p_ts;
346 ++ind_len;
347 if (round == 2)
348 *p++ = TAB;
349 }
350
351 /* Count/add spaces required for indent */
352 while (todo > 0)
353 {
354 --todo;
355 ++ind_len;
356 if (round == 2)
357 *p++ = ' ';
358 }
359
360 if (round == 1)
361 {
362 /* Allocate memory for the result: the copied indent, new indent
363 * and the rest of the line. */
364 line_len = (int)STRLEN(ml_get_curline()) + 1;
365 line = alloc(ind_len + line_len);
366 if (line == NULL)
367 return FALSE;
368 p = line;
369 }
370 }
371
372 /* Append the original line */
373 mch_memmove(p, ml_get_curline(), (size_t)line_len);
374
375 /* Replace the line */
376 ml_replace(curwin->w_cursor.lnum, line, FALSE);
377
378 /* Put the cursor after the indent. */
379 curwin->w_cursor.col = ind_len;
380 return TRUE;
381}
382
383/*
384 * Return the indent of the current line after a number. Return -1 if no
385 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000386 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 */
388 int
389get_number_indent(lnum)
390 linenr_T lnum;
391{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 colnr_T col;
393 pos_T pos;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000394 regmmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
396 if (lnum > curbuf->b_ml.ml_line_count)
397 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000398 pos.lnum = 0;
399 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
400 if (regmatch.regprog != NULL)
401 {
402 regmatch.rmm_ic = FALSE;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000403 regmatch.rmm_maxcol = 0;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000404 if (vim_regexec_multi(&regmatch, curwin, curbuf, lnum, (colnr_T)0))
405 {
406 pos.lnum = regmatch.endpos[0].lnum + lnum;
407 pos.col = regmatch.endpos[0].col;
408#ifdef FEAT_VIRTUALEDIT
409 pos.coladd = 0;
410#endif
411 }
412 vim_free(regmatch.regprog);
413 }
414
415 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 getvcol(curwin, &pos, &col, NULL, NULL);
418 return (int)col;
419}
420
421#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
422
423static int cin_is_cinword __ARGS((char_u *line));
424
425/*
426 * Return TRUE if the string "line" starts with a word from 'cinwords'.
427 */
428 static int
429cin_is_cinword(line)
430 char_u *line;
431{
432 char_u *cinw;
433 char_u *cinw_buf;
434 int cinw_len;
435 int retval = FALSE;
436 int len;
437
438 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
439 cinw_buf = alloc((unsigned)cinw_len);
440 if (cinw_buf != NULL)
441 {
442 line = skipwhite(line);
443 for (cinw = curbuf->b_p_cinw; *cinw; )
444 {
445 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
446 if (STRNCMP(line, cinw_buf, len) == 0
447 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
448 {
449 retval = TRUE;
450 break;
451 }
452 }
453 vim_free(cinw_buf);
454 }
455 return retval;
456}
457#endif
458
459/*
460 * open_line: Add a new line below or above the current line.
461 *
462 * For VREPLACE mode, we only add a new line when we get to the end of the
463 * file, otherwise we just start replacing the next line.
464 *
465 * Caller must take care of undo. Since VREPLACE may affect any number of
466 * lines however, it may call u_save_cursor() again when starting to change a
467 * new line.
468 * "flags": OPENLINE_DELSPACES delete spaces after cursor
469 * OPENLINE_DO_COM format comments
470 * OPENLINE_KEEPTRAIL keep trailing spaces
471 * OPENLINE_MARKFIX adjust mark positions after the line break
472 *
473 * Return TRUE for success, FALSE for failure
474 */
475 int
476open_line(dir, flags, old_indent)
477 int dir; /* FORWARD or BACKWARD */
478 int flags;
479 int old_indent; /* indent for after ^^D in Insert mode */
480{
481 char_u *saved_line; /* copy of the original line */
482 char_u *next_line = NULL; /* copy of the next line */
483 char_u *p_extra = NULL; /* what goes to next line */
484 int less_cols = 0; /* less columns for mark in new line */
485 int less_cols_off = 0; /* columns to skip for mark adjust */
486 pos_T old_cursor; /* old cursor position */
487 int newcol = 0; /* new cursor column */
488 int newindent = 0; /* auto-indent of the new line */
489 int n;
490 int trunc_line = FALSE; /* truncate current line afterwards */
491 int retval = FALSE; /* return value, default is FAIL */
492#ifdef FEAT_COMMENTS
493 int extra_len = 0; /* length of p_extra string */
494 int lead_len; /* length of comment leader */
495 char_u *lead_flags; /* position in 'comments' for comment leader */
496 char_u *leader = NULL; /* copy of comment leader */
497#endif
498 char_u *allocated = NULL; /* allocated memory */
499#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
500 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
501 char_u *p;
502#endif
503 int saved_char = NUL; /* init for GCC */
504#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
505 pos_T *pos;
506#endif
507#ifdef FEAT_SMARTINDENT
508 int do_si = (!p_paste && curbuf->b_p_si
509# ifdef FEAT_CINDENT
510 && !curbuf->b_p_cin
511# endif
512 );
513 int no_si = FALSE; /* reset did_si afterwards */
514 int first_char = NUL; /* init for GCC */
515#endif
516#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
517 int vreplace_mode;
518#endif
519 int did_append; /* appended a new line */
520 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
521
522 /*
523 * make a copy of the current line so we can mess with it
524 */
525 saved_line = vim_strsave(ml_get_curline());
526 if (saved_line == NULL) /* out of memory! */
527 return FALSE;
528
529#ifdef FEAT_VREPLACE
530 if (State & VREPLACE_FLAG)
531 {
532 /*
533 * With VREPLACE we make a copy of the next line, which we will be
534 * starting to replace. First make the new line empty and let vim play
535 * with the indenting and comment leader to its heart's content. Then
536 * we grab what it ended up putting on the new line, put back the
537 * original line, and call ins_char() to put each new character onto
538 * the line, replacing what was there before and pushing the right
539 * stuff onto the replace stack. -- webb.
540 */
541 if (curwin->w_cursor.lnum < orig_line_count)
542 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
543 else
544 next_line = vim_strsave((char_u *)"");
545 if (next_line == NULL) /* out of memory! */
546 goto theend;
547
548 /*
549 * In VREPLACE mode, a NL replaces the rest of the line, and starts
550 * replacing the next line, so push all of the characters left on the
551 * line onto the replace stack. We'll push any other characters that
552 * might be replaced at the start of the next line (due to autoindent
553 * etc) a bit later.
554 */
555 replace_push(NUL); /* Call twice because BS over NL expects it */
556 replace_push(NUL);
557 p = saved_line + curwin->w_cursor.col;
558 while (*p != NUL)
559 replace_push(*p++);
560 saved_line[curwin->w_cursor.col] = NUL;
561 }
562#endif
563
564 if ((State & INSERT)
565#ifdef FEAT_VREPLACE
566 && !(State & VREPLACE_FLAG)
567#endif
568 )
569 {
570 p_extra = saved_line + curwin->w_cursor.col;
571#ifdef FEAT_SMARTINDENT
572 if (do_si) /* need first char after new line break */
573 {
574 p = skipwhite(p_extra);
575 first_char = *p;
576 }
577#endif
578#ifdef FEAT_COMMENTS
579 extra_len = (int)STRLEN(p_extra);
580#endif
581 saved_char = *p_extra;
582 *p_extra = NUL;
583 }
584
585 u_clearline(); /* cannot do "U" command when adding lines */
586#ifdef FEAT_SMARTINDENT
587 did_si = FALSE;
588#endif
589 ai_col = 0;
590
591 /*
592 * If we just did an auto-indent, then we didn't type anything on
593 * the prior line, and it should be truncated. Do this even if 'ai' is not
594 * set because automatically inserting a comment leader also sets did_ai.
595 */
596 if (dir == FORWARD && did_ai)
597 trunc_line = TRUE;
598
599 /*
600 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
601 * indent to use for the new line.
602 */
603 if (curbuf->b_p_ai
604#ifdef FEAT_SMARTINDENT
605 || do_si
606#endif
607 )
608 {
609 /*
610 * count white space on current line
611 */
612 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts);
613 if (newindent == 0)
614 newindent = old_indent; /* for ^^D command in insert mode */
615
616#ifdef FEAT_SMARTINDENT
617 /*
618 * Do smart indenting.
619 * In insert/replace mode (only when dir == FORWARD)
620 * we may move some text to the next line. If it starts with '{'
621 * don't add an indent. Fixes inserting a NL before '{' in line
622 * "if (condition) {"
623 */
624 if (!trunc_line && do_si && *saved_line != NUL
625 && (p_extra == NULL || first_char != '{'))
626 {
627 char_u *ptr;
628 char_u last_char;
629
630 old_cursor = curwin->w_cursor;
631 ptr = saved_line;
632# ifdef FEAT_COMMENTS
633 if (flags & OPENLINE_DO_COM)
634 lead_len = get_leader_len(ptr, NULL, FALSE);
635 else
636 lead_len = 0;
637# endif
638 if (dir == FORWARD)
639 {
640 /*
641 * Skip preprocessor directives, unless they are
642 * recognised as comments.
643 */
644 if (
645# ifdef FEAT_COMMENTS
646 lead_len == 0 &&
647# endif
648 ptr[0] == '#')
649 {
650 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
651 ptr = ml_get(--curwin->w_cursor.lnum);
652 newindent = get_indent();
653 }
654# ifdef FEAT_COMMENTS
655 if (flags & OPENLINE_DO_COM)
656 lead_len = get_leader_len(ptr, NULL, FALSE);
657 else
658 lead_len = 0;
659 if (lead_len > 0)
660 {
661 /*
662 * This case gets the following right:
663 * \*
664 * * A comment (read '\' as '/').
665 * *\
666 * #define IN_THE_WAY
667 * This should line up here;
668 */
669 p = skipwhite(ptr);
670 if (p[0] == '/' && p[1] == '*')
671 p++;
672 if (p[0] == '*')
673 {
674 for (p++; *p; p++)
675 {
676 if (p[0] == '/' && p[-1] == '*')
677 {
678 /*
679 * End of C comment, indent should line up
680 * with the line containing the start of
681 * the comment
682 */
683 curwin->w_cursor.col = (colnr_T)(p - ptr);
684 if ((pos = findmatch(NULL, NUL)) != NULL)
685 {
686 curwin->w_cursor.lnum = pos->lnum;
687 newindent = get_indent();
688 }
689 }
690 }
691 }
692 }
693 else /* Not a comment line */
694# endif
695 {
696 /* Find last non-blank in line */
697 p = ptr + STRLEN(ptr) - 1;
698 while (p > ptr && vim_iswhite(*p))
699 --p;
700 last_char = *p;
701
702 /*
703 * find the character just before the '{' or ';'
704 */
705 if (last_char == '{' || last_char == ';')
706 {
707 if (p > ptr)
708 --p;
709 while (p > ptr && vim_iswhite(*p))
710 --p;
711 }
712 /*
713 * Try to catch lines that are split over multiple
714 * lines. eg:
715 * if (condition &&
716 * condition) {
717 * Should line up here!
718 * }
719 */
720 if (*p == ')')
721 {
722 curwin->w_cursor.col = (colnr_T)(p - ptr);
723 if ((pos = findmatch(NULL, '(')) != NULL)
724 {
725 curwin->w_cursor.lnum = pos->lnum;
726 newindent = get_indent();
727 ptr = ml_get_curline();
728 }
729 }
730 /*
731 * If last character is '{' do indent, without
732 * checking for "if" and the like.
733 */
734 if (last_char == '{')
735 {
736 did_si = TRUE; /* do indent */
737 no_si = TRUE; /* don't delete it when '{' typed */
738 }
739 /*
740 * Look for "if" and the like, use 'cinwords'.
741 * Don't do this if the previous line ended in ';' or
742 * '}'.
743 */
744 else if (last_char != ';' && last_char != '}'
745 && cin_is_cinword(ptr))
746 did_si = TRUE;
747 }
748 }
749 else /* dir == BACKWARD */
750 {
751 /*
752 * Skip preprocessor directives, unless they are
753 * recognised as comments.
754 */
755 if (
756# ifdef FEAT_COMMENTS
757 lead_len == 0 &&
758# endif
759 ptr[0] == '#')
760 {
761 int was_backslashed = FALSE;
762
763 while ((ptr[0] == '#' || was_backslashed) &&
764 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
765 {
766 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
767 was_backslashed = TRUE;
768 else
769 was_backslashed = FALSE;
770 ptr = ml_get(++curwin->w_cursor.lnum);
771 }
772 if (was_backslashed)
773 newindent = 0; /* Got to end of file */
774 else
775 newindent = get_indent();
776 }
777 p = skipwhite(ptr);
778 if (*p == '}') /* if line starts with '}': do indent */
779 did_si = TRUE;
780 else /* can delete indent when '{' typed */
781 can_si_back = TRUE;
782 }
783 curwin->w_cursor = old_cursor;
784 }
785 if (do_si)
786 can_si = TRUE;
787#endif /* FEAT_SMARTINDENT */
788
789 did_ai = TRUE;
790 }
791
792#ifdef FEAT_COMMENTS
793 /*
794 * Find out if the current line starts with a comment leader.
795 * This may then be inserted in front of the new line.
796 */
797 end_comment_pending = NUL;
798 if (flags & OPENLINE_DO_COM)
799 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD);
800 else
801 lead_len = 0;
802 if (lead_len > 0)
803 {
804 char_u *lead_repl = NULL; /* replaces comment leader */
805 int lead_repl_len = 0; /* length of *lead_repl */
806 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
807 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
808 char_u *comment_end = NULL; /* where lead_end has been found */
809 int extra_space = FALSE; /* append extra space */
810 int current_flag;
811 int require_blank = FALSE; /* requires blank after middle */
812 char_u *p2;
813
814 /*
815 * If the comment leader has the start, middle or end flag, it may not
816 * be used or may be replaced with the middle leader.
817 */
818 for (p = lead_flags; *p && *p != ':'; ++p)
819 {
820 if (*p == COM_BLANK)
821 {
822 require_blank = TRUE;
823 continue;
824 }
825 if (*p == COM_START || *p == COM_MIDDLE)
826 {
827 current_flag = *p;
828 if (*p == COM_START)
829 {
830 /*
831 * Doing "O" on a start of comment does not insert leader.
832 */
833 if (dir == BACKWARD)
834 {
835 lead_len = 0;
836 break;
837 }
838
839 /* find start of middle part */
840 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
841 require_blank = FALSE;
842 }
843
844 /*
845 * Isolate the strings of the middle and end leader.
846 */
847 while (*p && p[-1] != ':') /* find end of middle flags */
848 {
849 if (*p == COM_BLANK)
850 require_blank = TRUE;
851 ++p;
852 }
853 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
854
855 while (*p && p[-1] != ':') /* find end of end flags */
856 {
857 /* Check whether we allow automatic ending of comments */
858 if (*p == COM_AUTO_END)
859 end_comment_pending = -1; /* means we want to set it */
860 ++p;
861 }
862 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
863
864 if (end_comment_pending == -1) /* we can set it now */
865 end_comment_pending = lead_end[n - 1];
866
867 /*
868 * If the end of the comment is in the same line, don't use
869 * the comment leader.
870 */
871 if (dir == FORWARD)
872 {
873 for (p = saved_line + lead_len; *p; ++p)
874 if (STRNCMP(p, lead_end, n) == 0)
875 {
876 comment_end = p;
877 lead_len = 0;
878 break;
879 }
880 }
881
882 /*
883 * Doing "o" on a start of comment inserts the middle leader.
884 */
885 if (lead_len > 0)
886 {
887 if (current_flag == COM_START)
888 {
889 lead_repl = lead_middle;
890 lead_repl_len = (int)STRLEN(lead_middle);
891 }
892
893 /*
894 * If we have hit RETURN immediately after the start
895 * comment leader, then put a space after the middle
896 * comment leader on the next line.
897 */
898 if (!vim_iswhite(saved_line[lead_len - 1])
899 && ((p_extra != NULL
900 && (int)curwin->w_cursor.col == lead_len)
901 || (p_extra == NULL
902 && saved_line[lead_len] == NUL)
903 || require_blank))
904 extra_space = TRUE;
905 }
906 break;
907 }
908 if (*p == COM_END)
909 {
910 /*
911 * Doing "o" on the end of a comment does not insert leader.
912 * Remember where the end is, might want to use it to find the
913 * start (for C-comments).
914 */
915 if (dir == FORWARD)
916 {
917 comment_end = skipwhite(saved_line);
918 lead_len = 0;
919 break;
920 }
921
922 /*
923 * Doing "O" on the end of a comment inserts the middle leader.
924 * Find the string for the middle leader, searching backwards.
925 */
926 while (p > curbuf->b_p_com && *p != ',')
927 --p;
928 for (lead_repl = p; lead_repl > curbuf->b_p_com
929 && lead_repl[-1] != ':'; --lead_repl)
930 ;
931 lead_repl_len = (int)(p - lead_repl);
932
933 /* We can probably always add an extra space when doing "O" on
934 * the comment-end */
935 extra_space = TRUE;
936
937 /* Check whether we allow automatic ending of comments */
938 for (p2 = p; *p2 && *p2 != ':'; p2++)
939 {
940 if (*p2 == COM_AUTO_END)
941 end_comment_pending = -1; /* means we want to set it */
942 }
943 if (end_comment_pending == -1)
944 {
945 /* Find last character in end-comment string */
946 while (*p2 && *p2 != ',')
947 p2++;
948 end_comment_pending = p2[-1];
949 }
950 break;
951 }
952 if (*p == COM_FIRST)
953 {
954 /*
955 * Comment leader for first line only: Don't repeat leader
956 * when using "O", blank out leader when using "o".
957 */
958 if (dir == BACKWARD)
959 lead_len = 0;
960 else
961 {
962 lead_repl = (char_u *)"";
963 lead_repl_len = 0;
964 }
965 break;
966 }
967 }
968 if (lead_len)
969 {
970 /* allocate buffer (may concatenate p_exta later) */
971 leader = alloc(lead_len + lead_repl_len + extra_space +
972 extra_len + 1);
973 allocated = leader; /* remember to free it later */
974
975 if (leader == NULL)
976 lead_len = 0;
977 else
978 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000979 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 /*
982 * Replace leader with lead_repl, right or left adjusted
983 */
984 if (lead_repl != NULL)
985 {
986 int c = 0;
987 int off = 0;
988
989 for (p = lead_flags; *p && *p != ':'; ++p)
990 {
991 if (*p == COM_RIGHT || *p == COM_LEFT)
992 c = *p;
993 else if (VIM_ISDIGIT(*p) || *p == '-')
994 off = getdigits(&p);
995 }
996 if (c == COM_RIGHT) /* right adjusted leader */
997 {
998 /* find last non-white in the leader to line up with */
999 for (p = leader + lead_len - 1; p > leader
1000 && vim_iswhite(*p); --p)
1001 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001003
1004#ifdef FEAT_MBYTE
1005 /* Compute the length of the replaced characters in
1006 * screen characters, not bytes. */
1007 {
1008 int repl_size = vim_strnsize(lead_repl,
1009 lead_repl_len);
1010 int old_size = 0;
1011 char_u *endp = p;
1012 int l;
1013
1014 while (old_size < repl_size && p > leader)
1015 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001016 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001017 old_size += ptr2cells(p);
1018 }
1019 l = lead_repl_len - (endp - p);
1020 if (l != 0)
1021 mch_memmove(endp + l, endp,
1022 (size_t)((leader + lead_len) - endp));
1023 lead_len += l;
1024 }
1025#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (p < leader + lead_repl_len)
1027 p = leader;
1028 else
1029 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1032 if (p + lead_repl_len > leader + lead_len)
1033 p[lead_repl_len] = NUL;
1034
1035 /* blank-out any other chars from the old leader. */
1036 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001037 {
1038#ifdef FEAT_MBYTE
1039 int l = mb_head_off(leader, p);
1040
1041 if (l > 1)
1042 {
1043 p -= l;
1044 if (ptr2cells(p) > 1)
1045 {
1046 p[1] = ' ';
1047 --l;
1048 }
1049 mch_memmove(p + 1, p + l + 1,
1050 (size_t)((leader + lead_len) - (p + l + 1)));
1051 lead_len -= l;
1052 *p = ' ';
1053 }
1054 else
1055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (!vim_iswhite(*p))
1057 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060 else /* left adjusted leader */
1061 {
1062 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001063#ifdef FEAT_MBYTE
1064 /* Compute the length of the replaced characters in
1065 * screen characters, not bytes. Move the part that is
1066 * not to be overwritten. */
1067 {
1068 int repl_size = vim_strnsize(lead_repl,
1069 lead_repl_len);
1070 int i;
1071 int l;
1072
1073 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1074 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001075 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001076 if (vim_strnsize(p, i + l) > repl_size)
1077 break;
1078 }
1079 if (i != lead_repl_len)
1080 {
1081 mch_memmove(p + lead_repl_len, p + i,
1082 (size_t)(lead_len - i - (leader - p)));
1083 lead_len += lead_repl_len - i;
1084 }
1085 }
1086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1088
1089 /* Replace any remaining non-white chars in the old
1090 * leader by spaces. Keep Tabs, the indent must
1091 * remain the same. */
1092 for (p += lead_repl_len; p < leader + lead_len; ++p)
1093 if (!vim_iswhite(*p))
1094 {
1095 /* Don't put a space before a TAB. */
1096 if (p + 1 < leader + lead_len && p[1] == TAB)
1097 {
1098 --lead_len;
1099 mch_memmove(p, p + 1,
1100 (leader + lead_len) - p);
1101 }
1102 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001103 {
1104#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001105 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001106
1107 if (l > 1)
1108 {
1109 if (ptr2cells(p) > 1)
1110 {
1111 /* Replace a double-wide char with
1112 * two spaces */
1113 --l;
1114 *p++ = ' ';
1115 }
1116 mch_memmove(p + 1, p + l,
1117 (leader + lead_len) - p);
1118 lead_len -= l - 1;
1119 }
1120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124 *p = NUL;
1125 }
1126
1127 /* Recompute the indent, it may have changed. */
1128 if (curbuf->b_p_ai
1129#ifdef FEAT_SMARTINDENT
1130 || do_si
1131#endif
1132 )
1133 newindent = get_indent_str(leader, (int)curbuf->b_p_ts);
1134
1135 /* Add the indent offset */
1136 if (newindent + off < 0)
1137 {
1138 off = -newindent;
1139 newindent = 0;
1140 }
1141 else
1142 newindent += off;
1143
1144 /* Correct trailing spaces for the shift, so that
1145 * alignment remains equal. */
1146 while (off > 0 && lead_len > 0
1147 && leader[lead_len - 1] == ' ')
1148 {
1149 /* Don't do it when there is a tab before the space */
1150 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1151 break;
1152 --lead_len;
1153 --off;
1154 }
1155
1156 /* If the leader ends in white space, don't add an
1157 * extra space */
1158 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1159 extra_space = FALSE;
1160 leader[lead_len] = NUL;
1161 }
1162
1163 if (extra_space)
1164 {
1165 leader[lead_len++] = ' ';
1166 leader[lead_len] = NUL;
1167 }
1168
1169 newcol = lead_len;
1170
1171 /*
1172 * if a new indent will be set below, remove the indent that
1173 * is in the comment leader
1174 */
1175 if (newindent
1176#ifdef FEAT_SMARTINDENT
1177 || did_si
1178#endif
1179 )
1180 {
1181 while (lead_len && vim_iswhite(*leader))
1182 {
1183 --lead_len;
1184 --newcol;
1185 ++leader;
1186 }
1187 }
1188
1189 }
1190#ifdef FEAT_SMARTINDENT
1191 did_si = can_si = FALSE;
1192#endif
1193 }
1194 else if (comment_end != NULL)
1195 {
1196 /*
1197 * We have finished a comment, so we don't use the leader.
1198 * If this was a C-comment and 'ai' or 'si' is set do a normal
1199 * indent to align with the line containing the start of the
1200 * comment.
1201 */
1202 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1203 (curbuf->b_p_ai
1204#ifdef FEAT_SMARTINDENT
1205 || do_si
1206#endif
1207 ))
1208 {
1209 old_cursor = curwin->w_cursor;
1210 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1211 if ((pos = findmatch(NULL, NUL)) != NULL)
1212 {
1213 curwin->w_cursor.lnum = pos->lnum;
1214 newindent = get_indent();
1215 }
1216 curwin->w_cursor = old_cursor;
1217 }
1218 }
1219 }
1220#endif
1221
1222 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1223 if (p_extra != NULL)
1224 {
1225 *p_extra = saved_char; /* restore char that NUL replaced */
1226
1227 /*
1228 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1229 * non-blank.
1230 *
1231 * When in REPLACE mode, put the deleted blanks on the replace stack,
1232 * preceded by a NUL, so they can be put back when a BS is entered.
1233 */
1234 if (REPLACE_NORMAL(State))
1235 replace_push(NUL); /* end of extra blanks */
1236 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1237 {
1238 while ((*p_extra == ' ' || *p_extra == '\t')
1239#ifdef FEAT_MBYTE
1240 && (!enc_utf8
1241 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1242#endif
1243 )
1244 {
1245 if (REPLACE_NORMAL(State))
1246 replace_push(*p_extra);
1247 ++p_extra;
1248 ++less_cols_off;
1249 }
1250 }
1251 if (*p_extra != NUL)
1252 did_ai = FALSE; /* append some text, don't truncate now */
1253
1254 /* columns for marks adjusted for removed columns */
1255 less_cols = (int)(p_extra - saved_line);
1256 }
1257
1258 if (p_extra == NULL)
1259 p_extra = (char_u *)""; /* append empty line */
1260
1261#ifdef FEAT_COMMENTS
1262 /* concatenate leader and p_extra, if there is a leader */
1263 if (lead_len)
1264 {
1265 STRCAT(leader, p_extra);
1266 p_extra = leader;
1267 did_ai = TRUE; /* So truncating blanks works with comments */
1268 less_cols -= lead_len;
1269 }
1270 else
1271 end_comment_pending = NUL; /* turns out there was no leader */
1272#endif
1273
1274 old_cursor = curwin->w_cursor;
1275 if (dir == BACKWARD)
1276 --curwin->w_cursor.lnum;
1277#ifdef FEAT_VREPLACE
1278 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1279#endif
1280 {
1281 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1282 == FAIL)
1283 goto theend;
1284 /* Postpone calling changed_lines(), because it would mess up folding
1285 * with markers. */
1286 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1287 did_append = TRUE;
1288 }
1289#ifdef FEAT_VREPLACE
1290 else
1291 {
1292 /*
1293 * In VREPLACE mode we are starting to replace the next line.
1294 */
1295 curwin->w_cursor.lnum++;
1296 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1297 {
1298 /* In case we NL to a new line, BS to the previous one, and NL
1299 * again, we don't want to save the new line for undo twice.
1300 */
1301 (void)u_save_cursor(); /* errors are ignored! */
1302 vr_lines_changed++;
1303 }
1304 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1305 changed_bytes(curwin->w_cursor.lnum, 0);
1306 curwin->w_cursor.lnum--;
1307 did_append = FALSE;
1308 }
1309#endif
1310
1311 if (newindent
1312#ifdef FEAT_SMARTINDENT
1313 || did_si
1314#endif
1315 )
1316 {
1317 ++curwin->w_cursor.lnum;
1318#ifdef FEAT_SMARTINDENT
1319 if (did_si)
1320 {
1321 if (p_sr)
1322 newindent -= newindent % (int)curbuf->b_p_sw;
1323 newindent += (int)curbuf->b_p_sw;
1324 }
1325#endif
1326 /* Copy the indent only if expand tab is disabled */
1327 if (curbuf->b_p_ci && !curbuf->b_p_et)
1328 {
1329 (void)copy_indent(newindent, saved_line);
1330
1331 /*
1332 * Set the 'preserveindent' option so that any further screwing
1333 * with the line doesn't entirely destroy our efforts to preserve
1334 * it. It gets restored at the function end.
1335 */
1336 curbuf->b_p_pi = TRUE;
1337 }
1338 else
1339 (void)set_indent(newindent, SIN_INSERT);
1340 less_cols -= curwin->w_cursor.col;
1341
1342 ai_col = curwin->w_cursor.col;
1343
1344 /*
1345 * In REPLACE mode, for each character in the new indent, there must
1346 * be a NUL on the replace stack, for when it is deleted with BS
1347 */
1348 if (REPLACE_NORMAL(State))
1349 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1350 replace_push(NUL);
1351 newcol += curwin->w_cursor.col;
1352#ifdef FEAT_SMARTINDENT
1353 if (no_si)
1354 did_si = FALSE;
1355#endif
1356 }
1357
1358#ifdef FEAT_COMMENTS
1359 /*
1360 * In REPLACE mode, for each character in the extra leader, there must be
1361 * a NUL on the replace stack, for when it is deleted with BS.
1362 */
1363 if (REPLACE_NORMAL(State))
1364 while (lead_len-- > 0)
1365 replace_push(NUL);
1366#endif
1367
1368 curwin->w_cursor = old_cursor;
1369
1370 if (dir == FORWARD)
1371 {
1372 if (trunc_line || (State & INSERT))
1373 {
1374 /* truncate current line at cursor */
1375 saved_line[curwin->w_cursor.col] = NUL;
1376 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1377 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1378 truncate_spaces(saved_line);
1379 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1380 saved_line = NULL;
1381 if (did_append)
1382 {
1383 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1384 curwin->w_cursor.lnum + 1, 1L);
1385 did_append = FALSE;
1386
1387 /* Move marks after the line break to the new line. */
1388 if (flags & OPENLINE_MARKFIX)
1389 mark_col_adjust(curwin->w_cursor.lnum,
1390 curwin->w_cursor.col + less_cols_off,
1391 1L, (long)-less_cols);
1392 }
1393 else
1394 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1395 }
1396
1397 /*
1398 * Put the cursor on the new line. Careful: the scrollup() above may
1399 * have moved w_cursor, we must use old_cursor.
1400 */
1401 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1402 }
1403 if (did_append)
1404 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1405
1406 curwin->w_cursor.col = newcol;
1407#ifdef FEAT_VIRTUALEDIT
1408 curwin->w_cursor.coladd = 0;
1409#endif
1410
1411#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1412 /*
1413 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1414 * fixthisline() from doing it (via change_indent()) by telling it we're in
1415 * normal INSERT mode.
1416 */
1417 if (State & VREPLACE_FLAG)
1418 {
1419 vreplace_mode = State; /* So we know to put things right later */
1420 State = INSERT;
1421 }
1422 else
1423 vreplace_mode = 0;
1424#endif
1425#ifdef FEAT_LISP
1426 /*
1427 * May do lisp indenting.
1428 */
1429 if (!p_paste
1430# ifdef FEAT_COMMENTS
1431 && leader == NULL
1432# endif
1433 && curbuf->b_p_lisp
1434 && curbuf->b_p_ai)
1435 {
1436 fixthisline(get_lisp_indent);
1437 p = ml_get_curline();
1438 ai_col = (colnr_T)(skipwhite(p) - p);
1439 }
1440#endif
1441#ifdef FEAT_CINDENT
1442 /*
1443 * May do indenting after opening a new line.
1444 */
1445 if (!p_paste
1446 && (curbuf->b_p_cin
1447# ifdef FEAT_EVAL
1448 || *curbuf->b_p_inde != NUL
1449# endif
1450 )
1451 && in_cinkeys(dir == FORWARD
1452 ? KEY_OPEN_FORW
1453 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1454 {
1455 do_c_expr_indent();
1456 p = ml_get_curline();
1457 ai_col = (colnr_T)(skipwhite(p) - p);
1458 }
1459#endif
1460#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1461 if (vreplace_mode != 0)
1462 State = vreplace_mode;
1463#endif
1464
1465#ifdef FEAT_VREPLACE
1466 /*
1467 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1468 * original line, and inserts the new stuff char by char, pushing old stuff
1469 * onto the replace stack (via ins_char()).
1470 */
1471 if (State & VREPLACE_FLAG)
1472 {
1473 /* Put new line in p_extra */
1474 p_extra = vim_strsave(ml_get_curline());
1475 if (p_extra == NULL)
1476 goto theend;
1477
1478 /* Put back original line */
1479 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1480
1481 /* Insert new stuff into line again */
1482 curwin->w_cursor.col = 0;
1483#ifdef FEAT_VIRTUALEDIT
1484 curwin->w_cursor.coladd = 0;
1485#endif
1486 ins_bytes(p_extra); /* will call changed_bytes() */
1487 vim_free(p_extra);
1488 next_line = NULL;
1489 }
1490#endif
1491
1492 retval = TRUE; /* success! */
1493theend:
1494 curbuf->b_p_pi = saved_pi;
1495 vim_free(saved_line);
1496 vim_free(next_line);
1497 vim_free(allocated);
1498 return retval;
1499}
1500
1501#if defined(FEAT_COMMENTS) || defined(PROTO)
1502/*
1503 * get_leader_len() returns the length of the prefix of the given string
1504 * which introduces a comment. If this string is not a comment then 0 is
1505 * returned.
1506 * When "flags" is not NULL, it is set to point to the flags of the recognized
1507 * comment leader.
1508 * "backward" must be true for the "O" command.
1509 */
1510 int
1511get_leader_len(line, flags, backward)
1512 char_u *line;
1513 char_u **flags;
1514 int backward;
1515{
1516 int i, j;
1517 int got_com = FALSE;
1518 int found_one;
1519 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1520 char_u *string; /* pointer to comment string */
1521 char_u *list;
1522
1523 i = 0;
1524 while (vim_iswhite(line[i])) /* leading white space is ignored */
1525 ++i;
1526
1527 /*
1528 * Repeat to match several nested comment strings.
1529 */
1530 while (line[i])
1531 {
1532 /*
1533 * scan through the 'comments' option for a match
1534 */
1535 found_one = FALSE;
1536 for (list = curbuf->b_p_com; *list; )
1537 {
1538 /*
1539 * Get one option part into part_buf[]. Advance list to next one.
1540 * put string at start of string.
1541 */
1542 if (!got_com && flags != NULL) /* remember where flags started */
1543 *flags = list;
1544 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1545 string = vim_strchr(part_buf, ':');
1546 if (string == NULL) /* missing ':', ignore this part */
1547 continue;
1548 *string++ = NUL; /* isolate flags from string */
1549
1550 /*
1551 * When already found a nested comment, only accept further
1552 * nested comments.
1553 */
1554 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1555 continue;
1556
1557 /* When 'O' flag used don't use for "O" command */
1558 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1559 continue;
1560
1561 /*
1562 * Line contents and string must match.
1563 * When string starts with white space, must have some white space
1564 * (but the amount does not need to match, there might be a mix of
1565 * TABs and spaces).
1566 */
1567 if (vim_iswhite(string[0]))
1568 {
1569 if (i == 0 || !vim_iswhite(line[i - 1]))
1570 continue;
1571 while (vim_iswhite(string[0]))
1572 ++string;
1573 }
1574 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1575 ;
1576 if (string[j] != NUL)
1577 continue;
1578
1579 /*
1580 * When 'b' flag used, there must be white space or an
1581 * end-of-line after the string in the line.
1582 */
1583 if (vim_strchr(part_buf, COM_BLANK) != NULL
1584 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1585 continue;
1586
1587 /*
1588 * We have found a match, stop searching.
1589 */
1590 i += j;
1591 got_com = TRUE;
1592 found_one = TRUE;
1593 break;
1594 }
1595
1596 /*
1597 * No match found, stop scanning.
1598 */
1599 if (!found_one)
1600 break;
1601
1602 /*
1603 * Include any trailing white space.
1604 */
1605 while (vim_iswhite(line[i]))
1606 ++i;
1607
1608 /*
1609 * If this comment doesn't nest, stop here.
1610 */
1611 if (vim_strchr(part_buf, COM_NEST) == NULL)
1612 break;
1613 }
1614 return (got_com ? i : 0);
1615}
1616#endif
1617
1618/*
1619 * Return the number of window lines occupied by buffer line "lnum".
1620 */
1621 int
1622plines(lnum)
1623 linenr_T lnum;
1624{
1625 return plines_win(curwin, lnum, TRUE);
1626}
1627
1628 int
1629plines_win(wp, lnum, winheight)
1630 win_T *wp;
1631 linenr_T lnum;
1632 int winheight; /* when TRUE limit to window height */
1633{
1634#if defined(FEAT_DIFF) || defined(PROTO)
1635 /* Check for filler lines above this buffer line. When folded the result
1636 * is one line anyway. */
1637 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1638}
1639
1640 int
1641plines_nofill(lnum)
1642 linenr_T lnum;
1643{
1644 return plines_win_nofill(curwin, lnum, TRUE);
1645}
1646
1647 int
1648plines_win_nofill(wp, lnum, winheight)
1649 win_T *wp;
1650 linenr_T lnum;
1651 int winheight; /* when TRUE limit to window height */
1652{
1653#endif
1654 int lines;
1655
1656 if (!wp->w_p_wrap)
1657 return 1;
1658
1659#ifdef FEAT_VERTSPLIT
1660 if (wp->w_width == 0)
1661 return 1;
1662#endif
1663
1664#ifdef FEAT_FOLDING
1665 /* A folded lines is handled just like an empty line. */
1666 /* NOTE: Caller must handle lines that are MAYBE folded. */
1667 if (lineFolded(wp, lnum) == TRUE)
1668 return 1;
1669#endif
1670
1671 lines = plines_win_nofold(wp, lnum);
1672 if (winheight > 0 && lines > wp->w_height)
1673 return (int)wp->w_height;
1674 return lines;
1675}
1676
1677/*
1678 * Return number of window lines physical line "lnum" will occupy in window
1679 * "wp". Does not care about folding, 'wrap' or 'diff'.
1680 */
1681 int
1682plines_win_nofold(wp, lnum)
1683 win_T *wp;
1684 linenr_T lnum;
1685{
1686 char_u *s;
1687 long col;
1688 int width;
1689
1690 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
1691 if (*s == NUL) /* empty line */
1692 return 1;
1693 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
1694
1695 /*
1696 * If list mode is on, then the '$' at the end of the line may take up one
1697 * extra column.
1698 */
1699 if (wp->w_p_list && lcs_eol != NUL)
1700 col += 1;
1701
1702 /*
1703 * Add column offset for 'number' and 'foldcolumn'.
1704 */
1705 width = W_WIDTH(wp) - win_col_off(wp);
1706 if (width <= 0)
1707 return 32000;
1708 if (col <= width)
1709 return 1;
1710 col -= width;
1711 width += win_col_off2(wp);
1712 return (col + (width - 1)) / width + 1;
1713}
1714
1715/*
1716 * Like plines_win(), but only reports the number of physical screen lines
1717 * used from the start of the line to the given column number.
1718 */
1719 int
1720plines_win_col(wp, lnum, column)
1721 win_T *wp;
1722 linenr_T lnum;
1723 long column;
1724{
1725 long col;
1726 char_u *s;
1727 int lines = 0;
1728 int width;
1729
1730#ifdef FEAT_DIFF
1731 /* Check for filler lines above this buffer line. When folded the result
1732 * is one line anyway. */
1733 lines = diff_check_fill(wp, lnum);
1734#endif
1735
1736 if (!wp->w_p_wrap)
1737 return lines + 1;
1738
1739#ifdef FEAT_VERTSPLIT
1740 if (wp->w_width == 0)
1741 return lines + 1;
1742#endif
1743
1744 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
1745
1746 col = 0;
1747 while (*s != NUL && --column >= 0)
1748 {
1749 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001750 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752
1753 /*
1754 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
1755 * INSERT mode, then col must be adjusted so that it represents the last
1756 * screen position of the TAB. This only fixes an error when the TAB wraps
1757 * from one screen line to the next (when 'columns' is not a multiple of
1758 * 'ts') -- webb.
1759 */
1760 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
1761 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1;
1762
1763 /*
1764 * Add column offset for 'number', 'foldcolumn', etc.
1765 */
1766 width = W_WIDTH(wp) - win_col_off(wp);
1767 if (width > 0)
1768 {
1769 lines += 1;
1770 if (col >= width)
1771 lines += (col - width) / (width + win_col_off2(wp));
1772 if (lines <= wp->w_height)
1773 return lines;
1774 }
1775 return (int)(wp->w_height); /* maximum length */
1776}
1777
1778 int
1779plines_m_win(wp, first, last)
1780 win_T *wp;
1781 linenr_T first, last;
1782{
1783 int count = 0;
1784
1785 while (first <= last)
1786 {
1787#ifdef FEAT_FOLDING
1788 int x;
1789
1790 /* Check if there are any really folded lines, but also included lines
1791 * that are maybe folded. */
1792 x = foldedCount(wp, first, NULL);
1793 if (x > 0)
1794 {
1795 ++count; /* count 1 for "+-- folded" line */
1796 first += x;
1797 }
1798 else
1799#endif
1800 {
1801#ifdef FEAT_DIFF
1802 if (first == wp->w_topline)
1803 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
1804 else
1805#endif
1806 count += plines_win(wp, first, TRUE);
1807 ++first;
1808 }
1809 }
1810 return (count);
1811}
1812
1813#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
1814/*
1815 * Insert string "p" at the cursor position. Stops at a NUL byte.
1816 * Handles Replace mode and multi-byte characters.
1817 */
1818 void
1819ins_bytes(p)
1820 char_u *p;
1821{
1822 ins_bytes_len(p, (int)STRLEN(p));
1823}
1824#endif
1825
1826#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1827 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
1828/*
1829 * Insert string "p" with length "len" at the cursor position.
1830 * Handles Replace mode and multi-byte characters.
1831 */
1832 void
1833ins_bytes_len(p, len)
1834 char_u *p;
1835 int len;
1836{
1837 int i;
1838# ifdef FEAT_MBYTE
1839 int n;
1840
1841 for (i = 0; i < len; i += n)
1842 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001843 n = (*mb_ptr2len)(p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 ins_char_bytes(p + i, n);
1845 }
1846# else
1847 for (i = 0; i < len; ++i)
1848 ins_char(p[i]);
1849# endif
1850}
1851#endif
1852
1853/*
1854 * Insert or replace a single character at the cursor position.
1855 * When in REPLACE or VREPLACE mode, replace any existing character.
1856 * Caller must have prepared for undo.
1857 * For multi-byte characters we get the whole character, the caller must
1858 * convert bytes to a character.
1859 */
1860 void
1861ins_char(c)
1862 int c;
1863{
1864#if defined(FEAT_MBYTE) || defined(PROTO)
1865 char_u buf[MB_MAXBYTES];
1866 int n;
1867
1868 n = (*mb_char2bytes)(c, buf);
1869
1870 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
1871 * Happens for CTRL-Vu9900. */
1872 if (buf[0] == 0)
1873 buf[0] = '\n';
1874
1875 ins_char_bytes(buf, n);
1876}
1877
1878 void
1879ins_char_bytes(buf, charlen)
1880 char_u *buf;
1881 int charlen;
1882{
1883 int c = buf[0];
1884 int l, j;
1885#endif
1886 int newlen; /* nr of bytes inserted */
1887 int oldlen; /* nr of bytes deleted (0 when not replacing) */
1888 char_u *p;
1889 char_u *newp;
1890 char_u *oldp;
1891 int linelen; /* length of old line including NUL */
1892 colnr_T col;
1893 linenr_T lnum = curwin->w_cursor.lnum;
1894 int i;
1895
1896#ifdef FEAT_VIRTUALEDIT
1897 /* Break tabs if needed. */
1898 if (virtual_active() && curwin->w_cursor.coladd > 0)
1899 coladvance_force(getviscol());
1900#endif
1901
1902 col = curwin->w_cursor.col;
1903 oldp = ml_get(lnum);
1904 linelen = (int)STRLEN(oldp) + 1;
1905
1906 /* The lengths default to the values for when not replacing. */
1907 oldlen = 0;
1908#ifdef FEAT_MBYTE
1909 newlen = charlen;
1910#else
1911 newlen = 1;
1912#endif
1913
1914 if (State & REPLACE_FLAG)
1915 {
1916#ifdef FEAT_VREPLACE
1917 if (State & VREPLACE_FLAG)
1918 {
1919 colnr_T new_vcol = 0; /* init for GCC */
1920 colnr_T vcol;
1921 int old_list;
1922#ifndef FEAT_MBYTE
1923 char_u buf[2];
1924#endif
1925
1926 /*
1927 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
1928 * Returns the old value of list, so when finished,
1929 * curwin->w_p_list should be set back to this.
1930 */
1931 old_list = curwin->w_p_list;
1932 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
1933 curwin->w_p_list = FALSE;
1934
1935 /*
1936 * In virtual replace mode each character may replace one or more
1937 * characters (zero if it's a TAB). Count the number of bytes to
1938 * be deleted to make room for the new character, counting screen
1939 * cells. May result in adding spaces to fill a gap.
1940 */
1941 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
1942#ifndef FEAT_MBYTE
1943 buf[0] = c;
1944 buf[1] = NUL;
1945#endif
1946 new_vcol = vcol + chartabsize(buf, vcol);
1947 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
1948 {
1949 vcol += chartabsize(oldp + col + oldlen, vcol);
1950 /* Don't need to remove a TAB that takes us to the right
1951 * position. */
1952 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
1953 break;
1954#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001955 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956#else
1957 ++oldlen;
1958#endif
1959 /* Deleted a bit too much, insert spaces. */
1960 if (vcol > new_vcol)
1961 newlen += vcol - new_vcol;
1962 }
1963 curwin->w_p_list = old_list;
1964 }
1965 else
1966#endif
1967 if (oldp[col] != NUL)
1968 {
1969 /* normal replace */
1970#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001971 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972#else
1973 oldlen = 1;
1974#endif
1975 }
1976
1977
1978 /* Push the replaced bytes onto the replace stack, so that they can be
1979 * put back when BS is used. The bytes of a multi-byte character are
1980 * done the other way around, so that the first byte is popped off
1981 * first (it tells the byte length of the character). */
1982 replace_push(NUL);
1983 for (i = 0; i < oldlen; ++i)
1984 {
1985#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001986 l = (*mb_ptr2len)(oldp + col + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 for (j = l; j >= 0; --j)
1988 replace_push(oldp[col + i + j]);
1989 i += l;
1990#else
1991 replace_push(oldp[col + i]);
1992#endif
1993 }
1994 }
1995
1996 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
1997 if (newp == NULL)
1998 return;
1999
2000 /* Copy bytes before the cursor. */
2001 if (col > 0)
2002 mch_memmove(newp, oldp, (size_t)col);
2003
2004 /* Copy bytes after the changed character(s). */
2005 p = newp + col;
2006 mch_memmove(p + newlen, oldp + col + oldlen,
2007 (size_t)(linelen - col - oldlen));
2008
2009 /* Insert or overwrite the new character. */
2010#ifdef FEAT_MBYTE
2011 mch_memmove(p, buf, charlen);
2012 i = charlen;
2013#else
2014 *p = c;
2015 i = 1;
2016#endif
2017
2018 /* Fill with spaces when necessary. */
2019 while (i < newlen)
2020 p[i++] = ' ';
2021
2022 /* Replace the line in the buffer. */
2023 ml_replace(lnum, newp, FALSE);
2024
2025 /* mark the buffer as changed and prepare for displaying */
2026 changed_bytes(lnum, col);
2027
2028 /*
2029 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2030 * show the match for right parens and braces.
2031 */
2032 if (p_sm && (State & INSERT)
2033 && msg_silent == 0
2034#ifdef FEAT_MBYTE
2035 && charlen == 1
2036#endif
2037 )
2038 showmatch(c);
2039
2040#ifdef FEAT_RIGHTLEFT
2041 if (!p_ri || (State & REPLACE_FLAG))
2042#endif
2043 {
2044 /* Normal insert: move cursor right */
2045#ifdef FEAT_MBYTE
2046 curwin->w_cursor.col += charlen;
2047#else
2048 ++curwin->w_cursor.col;
2049#endif
2050 }
2051 /*
2052 * TODO: should try to update w_row here, to avoid recomputing it later.
2053 */
2054}
2055
2056/*
2057 * Insert a string at the cursor position.
2058 * Note: Does NOT handle Replace mode.
2059 * Caller must have prepared for undo.
2060 */
2061 void
2062ins_str(s)
2063 char_u *s;
2064{
2065 char_u *oldp, *newp;
2066 int newlen = (int)STRLEN(s);
2067 int oldlen;
2068 colnr_T col;
2069 linenr_T lnum = curwin->w_cursor.lnum;
2070
2071#ifdef FEAT_VIRTUALEDIT
2072 if (virtual_active() && curwin->w_cursor.coladd > 0)
2073 coladvance_force(getviscol());
2074#endif
2075
2076 col = curwin->w_cursor.col;
2077 oldp = ml_get(lnum);
2078 oldlen = (int)STRLEN(oldp);
2079
2080 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2081 if (newp == NULL)
2082 return;
2083 if (col > 0)
2084 mch_memmove(newp, oldp, (size_t)col);
2085 mch_memmove(newp + col, s, (size_t)newlen);
2086 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2087 ml_replace(lnum, newp, FALSE);
2088 changed_bytes(lnum, col);
2089 curwin->w_cursor.col += newlen;
2090}
2091
2092/*
2093 * Delete one character under the cursor.
2094 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2095 * Caller must have prepared for undo.
2096 *
2097 * return FAIL for failure, OK otherwise
2098 */
2099 int
2100del_char(fixpos)
2101 int fixpos;
2102{
2103#ifdef FEAT_MBYTE
2104 if (has_mbyte)
2105 {
2106 /* Make sure the cursor is at the start of a character. */
2107 mb_adjust_cursor();
2108 if (*ml_get_cursor() == NUL)
2109 return FAIL;
2110 return del_chars(1L, fixpos);
2111 }
2112#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002113 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114}
2115
2116#if defined(FEAT_MBYTE) || defined(PROTO)
2117/*
2118 * Like del_bytes(), but delete characters instead of bytes.
2119 */
2120 int
2121del_chars(count, fixpos)
2122 long count;
2123 int fixpos;
2124{
2125 long bytes = 0;
2126 long i;
2127 char_u *p;
2128 int l;
2129
2130 p = ml_get_cursor();
2131 for (i = 0; i < count && *p != NUL; ++i)
2132 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002133 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 bytes += l;
2135 p += l;
2136 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002137 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138}
2139#endif
2140
2141/*
2142 * Delete "count" bytes under the cursor.
2143 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2144 * Caller must have prepared for undo.
2145 *
2146 * return FAIL for failure, OK otherwise
2147 */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00002148/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 int
Bram Moolenaare3226be2005-12-18 22:10:00 +00002150del_bytes(count, fixpos, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 long count;
2152 int fixpos;
Bram Moolenaare3226be2005-12-18 22:10:00 +00002153 int use_delcombine; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 char_u *oldp, *newp;
2156 colnr_T oldlen;
2157 linenr_T lnum = curwin->w_cursor.lnum;
2158 colnr_T col = curwin->w_cursor.col;
2159 int was_alloced;
2160 long movelen;
2161
2162 oldp = ml_get(lnum);
2163 oldlen = (int)STRLEN(oldp);
2164
2165 /*
2166 * Can't do anything when the cursor is on the NUL after the line.
2167 */
2168 if (col >= oldlen)
2169 return FAIL;
2170
2171#ifdef FEAT_MBYTE
2172 /* If 'delcombine' is set and deleting (less than) one character, only
2173 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002174 if (p_deco && use_delcombine && enc_utf8
2175 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 int c1, c2;
2178 int n;
2179
2180 (void)utfc_ptr2char(oldp + col, &c1, &c2);
2181 if (c1 != NUL)
2182 {
2183 /* Find the last composing char, there can be several. */
2184 n = col;
2185 do
2186 {
2187 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002188 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 n += count;
2190 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2191 fixpos = 0;
2192 }
2193 }
2194#endif
2195
2196 /*
2197 * When count is too big, reduce it.
2198 */
2199 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2200 if (movelen <= 1)
2201 {
2202 /*
2203 * If we just took off the last character of a non-blank line, and
2204 * fixpos is TRUE, we don't want to end up positioned at the NUL.
2205 */
2206 if (col > 0 && fixpos)
2207 {
2208 --curwin->w_cursor.col;
2209#ifdef FEAT_VIRTUALEDIT
2210 curwin->w_cursor.coladd = 0;
2211#endif
2212#ifdef FEAT_MBYTE
2213 if (has_mbyte)
2214 curwin->w_cursor.col -=
2215 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2216#endif
2217 }
2218 count = oldlen - col;
2219 movelen = 1;
2220 }
2221
2222 /*
2223 * If the old line has been allocated the deletion can be done in the
2224 * existing line. Otherwise a new line has to be allocated
2225 */
2226 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
2227#ifdef FEAT_NETBEANS_INTG
2228 if (was_alloced && usingNetbeans)
2229 netbeans_removed(curbuf, lnum, col, count);
2230 /* else is handled by ml_replace() */
2231#endif
2232 if (was_alloced)
2233 newp = oldp; /* use same allocated memory */
2234 else
2235 { /* need to allocate a new line */
2236 newp = alloc((unsigned)(oldlen + 1 - count));
2237 if (newp == NULL)
2238 return FAIL;
2239 mch_memmove(newp, oldp, (size_t)col);
2240 }
2241 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2242 if (!was_alloced)
2243 ml_replace(lnum, newp, FALSE);
2244
2245 /* mark the buffer as changed and prepare for displaying */
2246 changed_bytes(lnum, curwin->w_cursor.col);
2247
2248 return OK;
2249}
2250
2251/*
2252 * Delete from cursor to end of line.
2253 * Caller must have prepared for undo.
2254 *
2255 * return FAIL for failure, OK otherwise
2256 */
2257 int
2258truncate_line(fixpos)
2259 int fixpos; /* if TRUE fix the cursor position when done */
2260{
2261 char_u *newp;
2262 linenr_T lnum = curwin->w_cursor.lnum;
2263 colnr_T col = curwin->w_cursor.col;
2264
2265 if (col == 0)
2266 newp = vim_strsave((char_u *)"");
2267 else
2268 newp = vim_strnsave(ml_get(lnum), col);
2269
2270 if (newp == NULL)
2271 return FAIL;
2272
2273 ml_replace(lnum, newp, FALSE);
2274
2275 /* mark the buffer as changed and prepare for displaying */
2276 changed_bytes(lnum, curwin->w_cursor.col);
2277
2278 /*
2279 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2280 */
2281 if (fixpos && curwin->w_cursor.col > 0)
2282 --curwin->w_cursor.col;
2283
2284 return OK;
2285}
2286
2287/*
2288 * Delete "nlines" lines at the cursor.
2289 * Saves the lines for undo first if "undo" is TRUE.
2290 */
2291 void
2292del_lines(nlines, undo)
2293 long nlines; /* number of lines to delete */
2294 int undo; /* if TRUE, prepare for undo */
2295{
2296 long n;
2297
2298 if (nlines <= 0)
2299 return;
2300
2301 /* save the deleted lines for undo */
2302 if (undo && u_savedel(curwin->w_cursor.lnum, nlines) == FAIL)
2303 return;
2304
2305 for (n = 0; n < nlines; )
2306 {
2307 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2308 break;
2309
2310 ml_delete(curwin->w_cursor.lnum, TRUE);
2311 ++n;
2312
2313 /* If we delete the last line in the file, stop */
2314 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2315 break;
2316 }
2317 /* adjust marks, mark the buffer as changed and prepare for displaying */
2318 deleted_lines_mark(curwin->w_cursor.lnum, n);
2319
2320 curwin->w_cursor.col = 0;
2321 check_cursor_lnum();
2322}
2323
2324 int
2325gchar_pos(pos)
2326 pos_T *pos;
2327{
2328 char_u *ptr = ml_get_pos(pos);
2329
2330#ifdef FEAT_MBYTE
2331 if (has_mbyte)
2332 return (*mb_ptr2char)(ptr);
2333#endif
2334 return (int)*ptr;
2335}
2336
2337 int
2338gchar_cursor()
2339{
2340#ifdef FEAT_MBYTE
2341 if (has_mbyte)
2342 return (*mb_ptr2char)(ml_get_cursor());
2343#endif
2344 return (int)*ml_get_cursor();
2345}
2346
2347/*
2348 * Write a character at the current cursor position.
2349 * It is directly written into the block.
2350 */
2351 void
2352pchar_cursor(c)
2353 int c;
2354{
2355 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2356 + curwin->w_cursor.col) = c;
2357}
2358
2359#if 0 /* not used */
2360/*
2361 * Put *pos at end of current buffer
2362 */
2363 void
2364goto_endofbuf(pos)
2365 pos_T *pos;
2366{
2367 char_u *p;
2368
2369 pos->lnum = curbuf->b_ml.ml_line_count;
2370 pos->col = 0;
2371 p = ml_get(pos->lnum);
2372 while (*p++)
2373 ++pos->col;
2374}
2375#endif
2376
2377/*
2378 * When extra == 0: Return TRUE if the cursor is before or on the first
2379 * non-blank in the line.
2380 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2381 * the line.
2382 */
2383 int
2384inindent(extra)
2385 int extra;
2386{
2387 char_u *ptr;
2388 colnr_T col;
2389
2390 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2391 ++ptr;
2392 if (col >= curwin->w_cursor.col + extra)
2393 return TRUE;
2394 else
2395 return FALSE;
2396}
2397
2398/*
2399 * Skip to next part of an option argument: Skip space and comma.
2400 */
2401 char_u *
2402skip_to_option_part(p)
2403 char_u *p;
2404{
2405 if (*p == ',')
2406 ++p;
2407 while (*p == ' ')
2408 ++p;
2409 return p;
2410}
2411
2412/*
2413 * changed() is called when something in the current buffer is changed.
2414 *
2415 * Most often called through changed_bytes() and changed_lines(), which also
2416 * mark the area of the display to be redrawn.
2417 */
2418 void
2419changed()
2420{
2421#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2422 /* The text of the preediting area is inserted, but this doesn't
2423 * mean a change of the buffer yet. That is delayed until the
2424 * text is committed. (this means preedit becomes empty) */
2425 if (im_is_preediting() && !xim_changed_while_preediting)
2426 return;
2427 xim_changed_while_preediting = FALSE;
2428#endif
2429
2430 if (!curbuf->b_changed)
2431 {
2432 int save_msg_scroll = msg_scroll;
2433
2434 change_warning(0);
2435 /* Create a swap file if that is wanted.
2436 * Don't do this for "nofile" and "nowrite" buffer types. */
2437 if (curbuf->b_may_swap
2438#ifdef FEAT_QUICKFIX
2439 && !bt_dontwrite(curbuf)
2440#endif
2441 )
2442 {
2443 ml_open_file(curbuf);
2444
2445 /* The ml_open_file() can cause an ATTENTION message.
2446 * Wait two seconds, to make sure the user reads this unexpected
2447 * message. Since we could be anywhere, call wait_return() now,
2448 * and don't let the emsg() set msg_scroll. */
2449 if (need_wait_return && emsg_silent == 0)
2450 {
2451 out_flush();
2452 ui_delay(2000L, TRUE);
2453 wait_return(TRUE);
2454 msg_scroll = save_msg_scroll;
2455 }
2456 }
2457 curbuf->b_changed = TRUE;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002458 ml_setflags(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459#ifdef FEAT_WINDOWS
2460 check_status(curbuf);
2461#endif
2462#ifdef FEAT_TITLE
2463 need_maketitle = TRUE; /* set window title later */
2464#endif
2465 }
2466 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467}
2468
Bram Moolenaardba8a912005-04-24 22:08:39 +00002469static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2470static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2472
2473/*
2474 * Changed bytes within a single line for the current buffer.
2475 * - marks the windows on this buffer to be redisplayed
2476 * - marks the buffer changed by calling changed()
2477 * - invalidates cached values
2478 */
2479 void
2480changed_bytes(lnum, col)
2481 linenr_T lnum;
2482 colnr_T col;
2483{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002484 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002486
2487#ifdef FEAT_DIFF
2488 /* Diff highlighting in other diff windows may need to be updated too. */
2489 if (curwin->w_p_diff)
2490 {
2491 win_T *wp;
2492 linenr_T wlnum;
2493
2494 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2495 if (wp->w_p_diff && wp != curwin)
2496 {
2497 redraw_win_later(wp, VALID);
2498 wlnum = diff_lnum_win(lnum, wp);
2499 if (wlnum > 0)
2500 changedOneline(wp->w_buffer, wlnum);
2501 }
2502 }
2503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504}
2505
2506 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002507changedOneline(buf, lnum)
2508 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 linenr_T lnum;
2510{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002511 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
2513 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002514 if (lnum < buf->b_mod_top)
2515 buf->b_mod_top = lnum;
2516 else if (lnum >= buf->b_mod_bot)
2517 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519 else
2520 {
2521 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002522 buf->b_mod_set = TRUE;
2523 buf->b_mod_top = lnum;
2524 buf->b_mod_bot = lnum + 1;
2525 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
2527}
2528
2529/*
2530 * Appended "count" lines below line "lnum" in the current buffer.
2531 * Must be called AFTER the change and after mark_adjust().
2532 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2533 */
2534 void
2535appended_lines(lnum, count)
2536 linenr_T lnum;
2537 long count;
2538{
2539 changed_lines(lnum + 1, 0, lnum + 1, count);
2540}
2541
2542/*
2543 * Like appended_lines(), but adjust marks first.
2544 */
2545 void
2546appended_lines_mark(lnum, count)
2547 linenr_T lnum;
2548 long count;
2549{
2550 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2551 changed_lines(lnum + 1, 0, lnum + 1, count);
2552}
2553
2554/*
2555 * Deleted "count" lines at line "lnum" in the current buffer.
2556 * Must be called AFTER the change and after mark_adjust().
2557 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2558 */
2559 void
2560deleted_lines(lnum, count)
2561 linenr_T lnum;
2562 long count;
2563{
2564 changed_lines(lnum, 0, lnum + count, -count);
2565}
2566
2567/*
2568 * Like deleted_lines(), but adjust marks first.
2569 */
2570 void
2571deleted_lines_mark(lnum, count)
2572 linenr_T lnum;
2573 long count;
2574{
2575 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2576 changed_lines(lnum, 0, lnum + count, -count);
2577}
2578
2579/*
2580 * Changed lines for the current buffer.
2581 * Must be called AFTER the change and after mark_adjust().
2582 * - mark the buffer changed by calling changed()
2583 * - mark the windows on this buffer to be redisplayed
2584 * - invalidate cached values
2585 * "lnum" is the first line that needs displaying, "lnume" the first line
2586 * below the changed lines (BEFORE the change).
2587 * When only inserting lines, "lnum" and "lnume" are equal.
2588 * Takes care of calling changed() and updating b_mod_*.
2589 */
2590 void
2591changed_lines(lnum, col, lnume, xtra)
2592 linenr_T lnum; /* first line with change */
2593 colnr_T col; /* column in first line with change */
2594 linenr_T lnume; /* line below last changed line */
2595 long xtra; /* number of extra lines (negative when deleting) */
2596{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002597 changed_lines_buf(curbuf, lnum, lnume, xtra);
2598
2599#ifdef FEAT_DIFF
2600 if (xtra == 0 && curwin->w_p_diff)
2601 {
2602 /* When the number of lines doesn't change then mark_adjust() isn't
2603 * called and other diff buffers still need to be marked for
2604 * displaying. */
2605 win_T *wp;
2606 linenr_T wlnum;
2607
2608 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2609 if (wp->w_p_diff && wp != curwin)
2610 {
2611 redraw_win_later(wp, VALID);
2612 wlnum = diff_lnum_win(lnum, wp);
2613 if (wlnum > 0)
2614 changed_lines_buf(wp->w_buffer, wlnum,
2615 lnume - lnum + wlnum, 0L);
2616 }
2617 }
2618#endif
2619
2620 changed_common(lnum, col, lnume, xtra);
2621}
2622
2623 static void
2624changed_lines_buf(buf, lnum, lnume, xtra)
2625 buf_T *buf;
2626 linenr_T lnum; /* first line with change */
2627 linenr_T lnume; /* line below last changed line */
2628 long xtra; /* number of extra lines (negative when deleting) */
2629{
2630 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002633 if (lnum < buf->b_mod_top)
2634 buf->b_mod_top = lnum;
2635 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
2637 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002638 buf->b_mod_bot += xtra;
2639 if (buf->b_mod_bot < lnum)
2640 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002642 if (lnume + xtra > buf->b_mod_bot)
2643 buf->b_mod_bot = lnume + xtra;
2644 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646 else
2647 {
2648 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002649 buf->b_mod_set = TRUE;
2650 buf->b_mod_top = lnum;
2651 buf->b_mod_bot = lnume + xtra;
2652 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654}
2655
2656 static void
2657changed_common(lnum, col, lnume, xtra)
2658 linenr_T lnum;
2659 colnr_T col;
2660 linenr_T lnume;
2661 long xtra;
2662{
2663 win_T *wp;
2664 int i;
2665#ifdef FEAT_JUMPLIST
2666 int cols;
2667 pos_T *p;
2668 int add;
2669#endif
2670
2671 /* mark the buffer as modified */
2672 changed();
2673
2674 /* set the '. mark */
2675 if (!cmdmod.keepjumps)
2676 {
2677 curbuf->b_last_change.lnum = lnum;
2678 curbuf->b_last_change.col = col;
2679
2680#ifdef FEAT_JUMPLIST
2681 /* Create a new entry if a new undo-able change was started or we
2682 * don't have an entry yet. */
2683 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
2684 {
2685 if (curbuf->b_changelistlen == 0)
2686 add = TRUE;
2687 else
2688 {
2689 /* Don't create a new entry when the line number is the same
2690 * as the last one and the column is not too far away. Avoids
2691 * creating many entries for typing "xxxxx". */
2692 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
2693 if (p->lnum != lnum)
2694 add = TRUE;
2695 else
2696 {
2697 cols = comp_textwidth(FALSE);
2698 if (cols == 0)
2699 cols = 79;
2700 add = (p->col + cols < col || col + cols < p->col);
2701 }
2702 }
2703 if (add)
2704 {
2705 /* This is the first of a new sequence of undo-able changes
2706 * and it's at some distance of the last change. Use a new
2707 * position in the changelist. */
2708 curbuf->b_new_change = FALSE;
2709
2710 if (curbuf->b_changelistlen == JUMPLISTSIZE)
2711 {
2712 /* changelist is full: remove oldest entry */
2713 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
2714 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
2715 sizeof(pos_T) * (JUMPLISTSIZE - 1));
2716 FOR_ALL_WINDOWS(wp)
2717 {
2718 /* Correct position in changelist for other windows on
2719 * this buffer. */
2720 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
2721 --wp->w_changelistidx;
2722 }
2723 }
2724 FOR_ALL_WINDOWS(wp)
2725 {
2726 /* For other windows, if the position in the changelist is
2727 * at the end it stays at the end. */
2728 if (wp->w_buffer == curbuf
2729 && wp->w_changelistidx == curbuf->b_changelistlen)
2730 ++wp->w_changelistidx;
2731 }
2732 ++curbuf->b_changelistlen;
2733 }
2734 }
2735 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
2736 curbuf->b_last_change;
2737 /* The current window is always after the last change, so that "g,"
2738 * takes you back to it. */
2739 curwin->w_changelistidx = curbuf->b_changelistlen;
2740#endif
2741 }
2742
2743 FOR_ALL_WINDOWS(wp)
2744 {
2745 if (wp->w_buffer == curbuf)
2746 {
2747 /* Mark this window to be redrawn later. */
2748 if (wp->w_redr_type < VALID)
2749 wp->w_redr_type = VALID;
2750
2751 /* Check if a change in the buffer has invalidated the cached
2752 * values for the cursor. */
2753#ifdef FEAT_FOLDING
2754 /*
2755 * Update the folds for this window. Can't postpone this, because
2756 * a following operator might work on the whole fold: ">>dd".
2757 */
2758 foldUpdate(wp, lnum, lnume + xtra - 1);
2759
2760 /* The change may cause lines above or below the change to become
2761 * included in a fold. Set lnum/lnume to the first/last line that
2762 * might be displayed differently.
2763 * Set w_cline_folded here as an efficient way to update it when
2764 * inserting lines just above a closed fold. */
2765 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
2766 if (wp->w_cursor.lnum == lnum)
2767 wp->w_cline_folded = i;
2768 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
2769 if (wp->w_cursor.lnum == lnume)
2770 wp->w_cline_folded = i;
2771
2772 /* If the changed line is in a range of previously folded lines,
2773 * compare with the first line in that range. */
2774 if (wp->w_cursor.lnum <= lnum)
2775 {
2776 i = find_wl_entry(wp, lnum);
2777 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
2778 changed_line_abv_curs_win(wp);
2779 }
2780#endif
2781
2782 if (wp->w_cursor.lnum > lnum)
2783 changed_line_abv_curs_win(wp);
2784 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
2785 changed_cline_bef_curs_win(wp);
2786 if (wp->w_botline >= lnum)
2787 {
2788 /* Assume that botline doesn't change (inserted lines make
2789 * other lines scroll down below botline). */
2790 approximate_botline_win(wp);
2791 }
2792
2793 /* Check if any w_lines[] entries have become invalid.
2794 * For entries below the change: Correct the lnums for
2795 * inserted/deleted lines. Makes it possible to stop displaying
2796 * after the change. */
2797 for (i = 0; i < wp->w_lines_valid; ++i)
2798 if (wp->w_lines[i].wl_valid)
2799 {
2800 if (wp->w_lines[i].wl_lnum >= lnum)
2801 {
2802 if (wp->w_lines[i].wl_lnum < lnume)
2803 {
2804 /* line included in change */
2805 wp->w_lines[i].wl_valid = FALSE;
2806 }
2807 else if (xtra != 0)
2808 {
2809 /* line below change */
2810 wp->w_lines[i].wl_lnum += xtra;
2811#ifdef FEAT_FOLDING
2812 wp->w_lines[i].wl_lastlnum += xtra;
2813#endif
2814 }
2815 }
2816#ifdef FEAT_FOLDING
2817 else if (wp->w_lines[i].wl_lastlnum >= lnum)
2818 {
2819 /* change somewhere inside this range of folded lines,
2820 * may need to be redrawn */
2821 wp->w_lines[i].wl_valid = FALSE;
2822 }
2823#endif
2824 }
2825 }
2826 }
2827
2828 /* Call update_screen() later, which checks out what needs to be redrawn,
2829 * since it notices b_mod_set and then uses b_mod_*. */
2830 if (must_redraw < VALID)
2831 must_redraw = VALID;
2832}
2833
2834/*
2835 * unchanged() is called when the changed flag must be reset for buffer 'buf'
2836 */
2837 void
2838unchanged(buf, ff)
2839 buf_T *buf;
2840 int ff; /* also reset 'fileformat' */
2841{
2842 if (buf->b_changed || (ff && file_ff_differs(buf)))
2843 {
2844 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002845 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 if (ff)
2847 save_file_ff(buf);
2848#ifdef FEAT_WINDOWS
2849 check_status(buf);
2850#endif
2851#ifdef FEAT_TITLE
2852 need_maketitle = TRUE; /* set window title later */
2853#endif
2854 }
2855 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856#ifdef FEAT_NETBEANS_INTG
2857 netbeans_unmodified(buf);
2858#endif
2859}
2860
2861#if defined(FEAT_WINDOWS) || defined(PROTO)
2862/*
2863 * check_status: called when the status bars for the buffer 'buf'
2864 * need to be updated
2865 */
2866 void
2867check_status(buf)
2868 buf_T *buf;
2869{
2870 win_T *wp;
2871
2872 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2873 if (wp->w_buffer == buf && wp->w_status_height)
2874 {
2875 wp->w_redr_status = TRUE;
2876 if (must_redraw < VALID)
2877 must_redraw = VALID;
2878 }
2879}
2880#endif
2881
2882/*
2883 * If the file is readonly, give a warning message with the first change.
2884 * Don't do this for autocommands.
2885 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002886 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 * will be TRUE.
2888 */
2889 void
2890change_warning(col)
2891 int col; /* column for message; non-zero when in insert
2892 mode and 'showmode' is on */
2893{
2894 if (curbuf->b_did_warn == FALSE
2895 && curbufIsChanged() == 0
2896#ifdef FEAT_AUTOCMD
2897 && !autocmd_busy
2898#endif
2899 && curbuf->b_p_ro)
2900 {
2901#ifdef FEAT_AUTOCMD
2902 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
2903 if (!curbuf->b_p_ro)
2904 return;
2905#endif
2906 /*
2907 * Do what msg() does, but with a column offset if the warning should
2908 * be after the mode message.
2909 */
2910 msg_start();
2911 if (msg_row == Rows - 1)
2912 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00002913 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 MSG_PUTS_ATTR(_("W10: Warning: Changing a readonly file"),
2915 hl_attr(HLF_W) | MSG_HIST);
2916 msg_clr_eos();
2917 (void)msg_end();
2918 if (msg_silent == 0 && !silent_mode)
2919 {
2920 out_flush();
2921 ui_delay(1000L, TRUE); /* give the user time to think about it */
2922 }
2923 curbuf->b_did_warn = TRUE;
2924 redraw_cmdline = FALSE; /* don't redraw and erase the message */
2925 if (msg_row < Rows - 1)
2926 showmode();
2927 }
2928}
2929
2930/*
2931 * Ask for a reply from the user, a 'y' or a 'n'.
2932 * No other characters are accepted, the message is repeated until a valid
2933 * reply is entered or CTRL-C is hit.
2934 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
2935 * from any buffers but directly from the user.
2936 *
2937 * return the 'y' or 'n'
2938 */
2939 int
2940ask_yesno(str, direct)
2941 char_u *str;
2942 int direct;
2943{
2944 int r = ' ';
2945 int save_State = State;
2946
2947 if (exiting) /* put terminal in raw mode for this question */
2948 settmode(TMODE_RAW);
2949 ++no_wait_return;
2950#ifdef USE_ON_FLY_SCROLL
2951 dont_scroll = TRUE; /* disallow scrolling here */
2952#endif
2953 State = CONFIRM; /* mouse behaves like with :confirm */
2954#ifdef FEAT_MOUSE
2955 setmouse(); /* disables mouse for xterm */
2956#endif
2957 ++no_mapping;
2958 ++allow_keys; /* no mapping here, but recognize keys */
2959
2960 while (r != 'y' && r != 'n')
2961 {
2962 /* same highlighting as for wait_return */
2963 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
2964 if (direct)
2965 r = get_keystroke();
2966 else
2967 r = safe_vgetc();
2968 if (r == Ctrl_C || r == ESC)
2969 r = 'n';
2970 msg_putchar(r); /* show what you typed */
2971 out_flush();
2972 }
2973 --no_wait_return;
2974 State = save_State;
2975#ifdef FEAT_MOUSE
2976 setmouse();
2977#endif
2978 --no_mapping;
2979 --allow_keys;
2980
2981 return r;
2982}
2983
2984/*
2985 * Get a key stroke directly from the user.
2986 * Ignores mouse clicks and scrollbar events, except a click for the left
2987 * button (used at the more prompt).
2988 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
2989 * Disadvantage: typeahead is ignored.
2990 * Translates the interrupt character for unix to ESC.
2991 */
2992 int
2993get_keystroke()
2994{
2995#define CBUFLEN 151
2996 char_u buf[CBUFLEN];
2997 int len = 0;
2998 int n;
2999 int save_mapped_ctrl_c = mapped_ctrl_c;
3000
3001 mapped_ctrl_c = FALSE; /* mappings are not used here */
3002 for (;;)
3003 {
3004 cursor_on();
3005 out_flush();
3006
3007 /* First time: blocking wait. Second time: wait up to 100ms for a
3008 * terminal code to complete. Leave some room for check_termcode() to
3009 * insert a key code into (max 5 chars plus NUL). And
3010 * fix_input_buffer() can triple the number of bytes. */
3011 n = ui_inchar(buf + len, (CBUFLEN - 6 - len) / 3,
3012 len == 0 ? -1L : 100L, 0);
3013 if (n > 0)
3014 {
3015 /* Replace zero and CSI by a special key code. */
3016 n = fix_input_buffer(buf + len, n, FALSE);
3017 len += n;
3018 }
3019
3020 /* incomplete termcode: get more characters */
3021 if ((n = check_termcode(1, buf, len)) < 0)
3022 continue;
3023 /* found a termcode: adjust length */
3024 if (n > 0)
3025 len = n;
3026 if (len == 0) /* nothing typed yet */
3027 continue;
3028
3029 /* Handle modifier and/or special key code. */
3030 n = buf[0];
3031 if (n == K_SPECIAL)
3032 {
3033 n = TO_SPECIAL(buf[1], buf[2]);
3034 if (buf[1] == KS_MODIFIER
3035 || n == K_IGNORE
3036#ifdef FEAT_MOUSE
3037 || n == K_LEFTMOUSE_NM
3038 || n == K_LEFTDRAG
3039 || n == K_LEFTRELEASE
3040 || n == K_LEFTRELEASE_NM
3041 || n == K_MIDDLEMOUSE
3042 || n == K_MIDDLEDRAG
3043 || n == K_MIDDLERELEASE
3044 || n == K_RIGHTMOUSE
3045 || n == K_RIGHTDRAG
3046 || n == K_RIGHTRELEASE
3047 || n == K_MOUSEDOWN
3048 || n == K_MOUSEUP
3049 || n == K_X1MOUSE
3050 || n == K_X1DRAG
3051 || n == K_X1RELEASE
3052 || n == K_X2MOUSE
3053 || n == K_X2DRAG
3054 || n == K_X2RELEASE
3055# ifdef FEAT_GUI
3056 || n == K_VER_SCROLLBAR
3057 || n == K_HOR_SCROLLBAR
3058# endif
3059#endif
3060 )
3061 {
3062 if (buf[1] == KS_MODIFIER)
3063 mod_mask = buf[2];
3064 len -= 3;
3065 if (len > 0)
3066 mch_memmove(buf, buf + 3, (size_t)len);
3067 continue;
3068 }
3069 }
3070#ifdef FEAT_MBYTE
3071 if (has_mbyte)
3072 {
3073 if (MB_BYTE2LEN(n) > len)
3074 continue; /* more bytes to get */
3075 buf[len >= CBUFLEN ? CBUFLEN - 1 : len] = NUL;
3076 n = (*mb_ptr2char)(buf);
3077 }
3078#endif
3079#ifdef UNIX
3080 if (n == intr_char)
3081 n = ESC;
3082#endif
3083 break;
3084 }
3085
3086 mapped_ctrl_c = save_mapped_ctrl_c;
3087 return n;
3088}
3089
3090/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003091 * Get a number from the user.
3092 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 */
3094 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003095get_number(colon, mouse_used)
3096 int colon; /* allow colon to abort */
3097 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099 int n = 0;
3100 int c;
3101
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003102 if (mouse_used != NULL)
3103 *mouse_used = FALSE;
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 /* When not printing messages, the user won't know what to type, return a
3106 * zero (as if CR was hit). */
3107 if (msg_silent != 0)
3108 return 0;
3109
3110#ifdef USE_ON_FLY_SCROLL
3111 dont_scroll = TRUE; /* disallow scrolling here */
3112#endif
3113 ++no_mapping;
3114 ++allow_keys; /* no mapping here, but recognize keys */
3115 for (;;)
3116 {
3117 windgoto(msg_row, msg_col);
3118 c = safe_vgetc();
3119 if (VIM_ISDIGIT(c))
3120 {
3121 n = n * 10 + c - '0';
3122 msg_putchar(c);
3123 }
3124 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3125 {
3126 n /= 10;
3127 MSG_PUTS("\b \b");
3128 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003129#ifdef FEAT_MOUSE
3130 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3131 {
3132 *mouse_used = TRUE;
3133 n = mouse_row + 1;
3134 break;
3135 }
3136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 else if (n == 0 && c == ':' && colon)
3138 {
3139 stuffcharReadbuff(':');
3140 if (!exmode_active)
3141 cmdline_row = msg_row;
3142 skip_redraw = TRUE; /* skip redraw once */
3143 do_redraw = FALSE;
3144 break;
3145 }
3146 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3147 break;
3148 }
3149 --no_mapping;
3150 --allow_keys;
3151 return n;
3152}
3153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003154/*
3155 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003156 * When "mouse_used" is not NULL allow using the mouse and in that case return
3157 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003158 */
3159 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003160prompt_for_number(mouse_used)
3161 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003162{
3163 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003164 int save_cmdline_row;
3165 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003166
3167 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003168 if (mouse_used != NULL)
3169 MSG_PUTS(_("Type number or click with mouse (<Enter> cancels): "));
3170 else
3171 MSG_PUTS(_("Choice number (<Enter> cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003172
3173 /* Set the state such that text can be selected/copied/pasted. */
3174 save_cmdline_row = cmdline_row;
3175 cmdline_row = Rows - 1;
3176 save_State = State;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003177 if (mouse_used == NULL)
3178 State = CMDLINE;
3179 else
3180 State = NORMAL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003181
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003182 i = get_number(TRUE, mouse_used);
3183 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003184 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003185 /* don't call wait_return() now */
3186 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003187 cmdline_row = msg_row - 1;
3188 need_wait_return = FALSE;
3189 msg_didany = FALSE;
3190 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003191 else
3192 cmdline_row = save_cmdline_row;
3193 State = save_State;
3194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003195 return i;
3196}
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 void
3199msgmore(n)
3200 long n;
3201{
3202 long pn;
3203
3204 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3206 return;
3207
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003208 /* We don't want to overwrite another important message, but do overwrite
3209 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3210 * then "put" reports the last action. */
3211 if (keep_msg != NULL && !keep_msg_more)
3212 return;
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 if (n > 0)
3215 pn = n;
3216 else
3217 pn = -n;
3218
3219 if (pn > p_report)
3220 {
3221 if (pn == 1)
3222 {
3223 if (n > 0)
3224 STRCPY(msg_buf, _("1 more line"));
3225 else
3226 STRCPY(msg_buf, _("1 line less"));
3227 }
3228 else
3229 {
3230 if (n > 0)
3231 sprintf((char *)msg_buf, _("%ld more lines"), pn);
3232 else
3233 sprintf((char *)msg_buf, _("%ld fewer lines"), pn);
3234 }
3235 if (got_int)
3236 STRCAT(msg_buf, _(" (Interrupted)"));
3237 if (msg(msg_buf))
3238 {
3239 set_keep_msg(msg_buf);
3240 keep_msg_attr = 0;
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003241 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
3243 }
3244}
3245
3246/*
3247 * flush map and typeahead buffers and give a warning for an error
3248 */
3249 void
3250beep_flush()
3251{
3252 if (emsg_silent == 0)
3253 {
3254 flush_buffers(FALSE);
3255 vim_beep();
3256 }
3257}
3258
3259/*
3260 * give a warning for an error
3261 */
3262 void
3263vim_beep()
3264{
3265 if (emsg_silent == 0)
3266 {
3267 if (p_vb
3268#ifdef FEAT_GUI
3269 /* While the GUI is starting up the termcap is set for the GUI
3270 * but the output still goes to a terminal. */
3271 && !(gui.in_use && gui.starting)
3272#endif
3273 )
3274 {
3275 out_str(T_VB);
3276 }
3277 else
3278 {
3279#ifdef MSDOS
3280 /*
3281 * The number of beeps outputted is reduced to avoid having to wait
3282 * for all the beeps to finish. This is only a problem on systems
3283 * where the beeps don't overlap.
3284 */
3285 if (beep_count == 0 || beep_count == 10)
3286 {
3287 out_char(BELL);
3288 beep_count = 1;
3289 }
3290 else
3291 ++beep_count;
3292#else
3293 out_char(BELL);
3294#endif
3295 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003296
3297 /* When 'verbose' is set and we are sourcing a script or executing a
3298 * function give the user a hint where the beep comes from. */
3299 if (vim_strchr(p_debug, 'e') != NULL)
3300 {
3301 msg_source(hl_attr(HLF_W));
3302 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
3305}
3306
3307/*
3308 * To get the "real" home directory:
3309 * - get value of $HOME
3310 * For Unix:
3311 * - go to that directory
3312 * - do mch_dirname() to get the real name of that directory.
3313 * This also works with mounts and links.
3314 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3315 */
3316static char_u *homedir = NULL;
3317
3318 void
3319init_homedir()
3320{
3321 char_u *var;
3322
Bram Moolenaar05159a02005-02-26 23:04:13 +00003323 /* In case we are called a second time (when 'encoding' changes). */
3324 vim_free(homedir);
3325 homedir = NULL;
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#ifdef VMS
3328 var = mch_getenv((char_u *)"SYS$LOGIN");
3329#else
3330 var = mch_getenv((char_u *)"HOME");
3331#endif
3332
3333 if (var != NULL && *var == NUL) /* empty is same as not set */
3334 var = NULL;
3335
3336#ifdef WIN3264
3337 /*
3338 * Weird but true: $HOME may contain an indirect reference to another
3339 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3340 * when $HOME is being set.
3341 */
3342 if (var != NULL && *var == '%')
3343 {
3344 char_u *p;
3345 char_u *exp;
3346
3347 p = vim_strchr(var + 1, '%');
3348 if (p != NULL)
3349 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003350 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 exp = mch_getenv(NameBuff);
3352 if (exp != NULL && *exp != NUL
3353 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3354 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003355 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 var = NameBuff;
3357 /* Also set $HOME, it's needed for _viminfo. */
3358 vim_setenv((char_u *)"HOME", NameBuff);
3359 }
3360 }
3361 }
3362
3363 /*
3364 * Typically, $HOME is not defined on Windows, unless the user has
3365 * specifically defined it for Vim's sake. However, on Windows NT
3366 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3367 * each user. Try constructing $HOME from these.
3368 */
3369 if (var == NULL)
3370 {
3371 char_u *homedrive, *homepath;
3372
3373 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3374 homepath = mch_getenv((char_u *)"HOMEPATH");
3375 if (homedrive != NULL && homepath != NULL
3376 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3377 {
3378 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3379 if (NameBuff[0] != NUL)
3380 {
3381 var = NameBuff;
3382 /* Also set $HOME, it's needed for _viminfo. */
3383 vim_setenv((char_u *)"HOME", NameBuff);
3384 }
3385 }
3386 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003387
3388# if defined(FEAT_MBYTE)
3389 if (enc_utf8 && var != NULL)
3390 {
3391 int len;
3392 char_u *pp;
3393
3394 /* Convert from active codepage to UTF-8. Other conversions are
3395 * not done, because they would fail for non-ASCII characters. */
3396 acp_to_enc(var, STRLEN(var), &pp, &len);
3397 if (pp != NULL)
3398 {
3399 homedir = pp;
3400 return;
3401 }
3402 }
3403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#endif
3405
3406#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3407 /*
3408 * Default home dir is C:/
3409 * Best assumption we can make in such a situation.
3410 */
3411 if (var == NULL)
3412 var = "C:/";
3413#endif
3414 if (var != NULL)
3415 {
3416#ifdef UNIX
3417 /*
3418 * Change to the directory and get the actual path. This resolves
3419 * links. Don't do it when we can't return.
3420 */
3421 if (mch_dirname(NameBuff, MAXPATHL) == OK
3422 && mch_chdir((char *)NameBuff) == 0)
3423 {
3424 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3425 var = IObuff;
3426 if (mch_chdir((char *)NameBuff) != 0)
3427 EMSG(_(e_prev_dir));
3428 }
3429#endif
3430 homedir = vim_strsave(var);
3431 }
3432}
3433
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003434#if defined(EXITFREE) || defined(PROTO)
3435 void
3436free_homedir()
3437{
3438 vim_free(homedir);
3439}
3440#endif
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442/*
3443 * Expand environment variable with path name.
3444 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
3445 * Skips over "\ ", "\~" and "\$".
3446 * If anything fails no expansion is done and dst equals src.
3447 */
3448 void
3449expand_env(src, dst, dstlen)
3450 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3451 char_u *dst; /* where to put the result */
3452 int dstlen; /* maximum length of the result */
3453{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003454 expand_env_esc(src, dst, dstlen, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455}
3456
3457 void
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003458expand_env_esc(srcp, dst, dstlen, esc, startstr)
3459 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 char_u *dst; /* where to put the result */
3461 int dstlen; /* maximum length of the result */
3462 int esc; /* escape spaces in expanded variables */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003463 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003465 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 char_u *tail;
3467 int c;
3468 char_u *var;
3469 int copy_char;
3470 int mustfree; /* var was allocated, need to free it later */
3471 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003472 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003474 if (startstr != NULL)
3475 startstr_len = STRLEN(startstr);
3476
3477 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 --dstlen; /* leave one char space for "\," */
3479 while (*src && dstlen > 0)
3480 {
3481 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003482 if ((*src == '$'
3483#ifdef VMS
3484 && at_start
3485#endif
3486 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3488 || *src == '%'
3489#endif
3490 || (*src == '~' && at_start))
3491 {
3492 mustfree = FALSE;
3493
3494 /*
3495 * The variable name is copied into dst temporarily, because it may
3496 * be a string in read-only memory and a NUL needs to be appended.
3497 */
3498 if (*src != '~') /* environment var */
3499 {
3500 tail = src + 1;
3501 var = dst;
3502 c = dstlen - 1;
3503
3504#ifdef UNIX
3505 /* Unix has ${var-name} type environment vars */
3506 if (*tail == '{' && !vim_isIDc('{'))
3507 {
3508 tail++; /* ignore '{' */
3509 while (c-- > 0 && *tail && *tail != '}')
3510 *var++ = *tail++;
3511 }
3512 else
3513#endif
3514 {
3515 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3516#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3517 || (*src == '%' && *tail != '%')
3518#endif
3519 ))
3520 {
3521#ifdef OS2 /* env vars only in uppercase */
3522 *var++ = TOUPPER_LOC(*tail);
3523 tail++; /* toupper() may be a macro! */
3524#else
3525 *var++ = *tail++;
3526#endif
3527 }
3528 }
3529
3530#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3531# ifdef UNIX
3532 if (src[1] == '{' && *tail != '}')
3533# else
3534 if (*src == '%' && *tail != '%')
3535# endif
3536 var = NULL;
3537 else
3538 {
3539# ifdef UNIX
3540 if (src[1] == '{')
3541# else
3542 if (*src == '%')
3543#endif
3544 ++tail;
3545#endif
3546 *var = NUL;
3547 var = vim_getenv(dst, &mustfree);
3548#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3549 }
3550#endif
3551 }
3552 /* home directory */
3553 else if ( src[1] == NUL
3554 || vim_ispathsep(src[1])
3555 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3556 {
3557 var = homedir;
3558 tail = src + 1;
3559 }
3560 else /* user directory */
3561 {
3562#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
3563 /*
3564 * Copy ~user to dst[], so we can put a NUL after it.
3565 */
3566 tail = src;
3567 var = dst;
3568 c = dstlen - 1;
3569 while ( c-- > 0
3570 && *tail
3571 && vim_isfilec(*tail)
3572 && !vim_ispathsep(*tail))
3573 *var++ = *tail++;
3574 *var = NUL;
3575# ifdef UNIX
3576 /*
3577 * If the system supports getpwnam(), use it.
3578 * Otherwise, or if getpwnam() fails, the shell is used to
3579 * expand ~user. This is slower and may fail if the shell
3580 * does not support ~user (old versions of /bin/sh).
3581 */
3582# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
3583 {
3584 struct passwd *pw;
3585
3586 pw = getpwnam((char *)dst + 1);
3587 if (pw != NULL)
3588 var = (char_u *)pw->pw_dir;
3589 else
3590 var = NULL;
3591 }
3592 if (var == NULL)
3593# endif
3594 {
3595 expand_T xpc;
3596
3597 ExpandInit(&xpc);
3598 xpc.xp_context = EXPAND_FILES;
3599 var = ExpandOne(&xpc, dst, NULL,
3600 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
3601 ExpandCleanup(&xpc);
3602 mustfree = TRUE;
3603 }
3604
3605# else /* !UNIX, thus VMS */
3606 /*
3607 * USER_HOME is a comma-separated list of
3608 * directories to search for the user account in.
3609 */
3610 {
3611 char_u test[MAXPATHL], paths[MAXPATHL];
3612 char_u *path, *next_path, *ptr;
3613 struct stat st;
3614
3615 STRCPY(paths, USER_HOME);
3616 next_path = paths;
3617 while (*next_path)
3618 {
3619 for (path = next_path; *next_path && *next_path != ',';
3620 next_path++);
3621 if (*next_path)
3622 *next_path++ = NUL;
3623 STRCPY(test, path);
3624 STRCAT(test, "/");
3625 STRCAT(test, dst + 1);
3626 if (mch_stat(test, &st) == 0)
3627 {
3628 var = alloc(STRLEN(test) + 1);
3629 STRCPY(var, test);
3630 mustfree = TRUE;
3631 break;
3632 }
3633 }
3634 }
3635# endif /* UNIX */
3636#else
3637 /* cannot expand user's home directory, so don't try */
3638 var = NULL;
3639 tail = (char_u *)""; /* for gcc */
3640#endif /* UNIX || VMS */
3641 }
3642
3643#ifdef BACKSLASH_IN_FILENAME
3644 /* If 'shellslash' is set change backslashes to forward slashes.
3645 * Can't use slash_adjust(), p_ssl may be set temporarily. */
3646 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
3647 {
3648 char_u *p = vim_strsave(var);
3649
3650 if (p != NULL)
3651 {
3652 if (mustfree)
3653 vim_free(var);
3654 var = p;
3655 mustfree = TRUE;
3656 forward_slash(var);
3657 }
3658 }
3659#endif
3660
3661 /* If "var" contains white space, escape it with a backslash.
3662 * Required for ":e ~/tt" when $HOME includes a space. */
3663 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
3664 {
3665 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
3666
3667 if (p != NULL)
3668 {
3669 if (mustfree)
3670 vim_free(var);
3671 var = p;
3672 mustfree = TRUE;
3673 }
3674 }
3675
3676 if (var != NULL && *var != NUL
3677 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
3678 {
3679 STRCPY(dst, var);
3680 dstlen -= (int)STRLEN(var);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003681 c = STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 /* if var[] ends in a path separator and tail[] starts
3683 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003684 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
3686 && dst[-1] != ':'
3687#endif
3688 && vim_ispathsep(*tail))
3689 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003690 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 src = tail;
3692 copy_char = FALSE;
3693 }
3694 if (mustfree)
3695 vim_free(var);
3696 }
3697
3698 if (copy_char) /* copy at least one char */
3699 {
3700 /*
3701 * Recogize the start of a new name, for '~'.
3702 */
3703 at_start = FALSE;
3704 if (src[0] == '\\' && src[1] != NUL)
3705 {
3706 *dst++ = *src++;
3707 --dstlen;
3708 }
3709 else if (src[0] == ' ' || src[0] == ',')
3710 at_start = TRUE;
3711 *dst++ = *src++;
3712 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003713
3714 if (startstr != NULL && src - startstr_len >= srcp
3715 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
3716 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 }
3719 *dst = NUL;
3720}
3721
3722/*
3723 * Vim's version of getenv().
3724 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00003725 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
3727 char_u *
3728vim_getenv(name, mustfree)
3729 char_u *name;
3730 int *mustfree; /* set to TRUE when returned is allocated */
3731{
3732 char_u *p;
3733 char_u *pend;
3734 int vimruntime;
3735
3736#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3737 /* use "C:/" when $HOME is not set */
3738 if (STRCMP(name, "HOME") == 0)
3739 return homedir;
3740#endif
3741
3742 p = mch_getenv(name);
3743 if (p != NULL && *p == NUL) /* empty is the same as not set */
3744 p = NULL;
3745
3746 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003747 {
3748#if defined(FEAT_MBYTE) && defined(WIN3264)
3749 if (enc_utf8)
3750 {
3751 int len;
3752 char_u *pp;
3753
3754 /* Convert from active codepage to UTF-8. Other conversions are
3755 * not done, because they would fail for non-ASCII characters. */
3756 acp_to_enc(p, STRLEN(p), &pp, &len);
3757 if (pp != NULL)
3758 {
3759 p = pp;
3760 *mustfree = TRUE;
3761 }
3762 }
3763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
3768 if (!vimruntime && STRCMP(name, "VIM") != 0)
3769 return NULL;
3770
3771 /*
3772 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
3773 * Don't do this when default_vimruntime_dir is non-empty.
3774 */
3775 if (vimruntime
3776#ifdef HAVE_PATHDEF
3777 && *default_vimruntime_dir == NUL
3778#endif
3779 )
3780 {
3781 p = mch_getenv((char_u *)"VIM");
3782 if (p != NULL && *p == NUL) /* empty is the same as not set */
3783 p = NULL;
3784 if (p != NULL)
3785 {
3786 p = vim_version_dir(p);
3787 if (p != NULL)
3788 *mustfree = TRUE;
3789 else
3790 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00003791
3792#if defined(FEAT_MBYTE) && defined(WIN3264)
3793 if (enc_utf8)
3794 {
3795 int len;
3796 char_u *pp;
3797
3798 /* Convert from active codepage to UTF-8. Other conversions
3799 * are not done, because they would fail for non-ASCII
3800 * characters. */
3801 acp_to_enc(p, STRLEN(p), &pp, &len);
3802 if (pp != NULL)
3803 {
3804 if (mustfree)
3805 vim_free(p);
3806 p = pp;
3807 *mustfree = TRUE;
3808 }
3809 }
3810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 }
3813
3814 /*
3815 * When expanding $VIM or $VIMRUNTIME fails, try using:
3816 * - the directory name from 'helpfile' (unless it contains '$')
3817 * - the executable name from argv[0]
3818 */
3819 if (p == NULL)
3820 {
3821 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
3822 p = p_hf;
3823#ifdef USE_EXE_NAME
3824 /*
3825 * Use the name of the executable, obtained from argv[0].
3826 */
3827 else
3828 p = exe_name;
3829#endif
3830 if (p != NULL)
3831 {
3832 /* remove the file name */
3833 pend = gettail(p);
3834
3835 /* remove "doc/" from 'helpfile', if present */
3836 if (p == p_hf)
3837 pend = remove_tail(p, pend, (char_u *)"doc");
3838
3839#ifdef USE_EXE_NAME
3840# ifdef MACOS_X
3841 /* remove "build/..." from exe_name, if present */
3842 if (p == exe_name)
3843 {
3844 char_u *pend1;
3845 char_u *pend2;
3846
3847 pend1 = remove_tail(p, pend, (char_u *)"Contents/MacOS");
3848 pend2 = remove_tail_with_ext(p, pend1, (char_u *)".app");
3849 pend = remove_tail(p, pend2, (char_u *)"build");
3850 /* When runnig from project builder get rid of the
3851 * build/???.app, otherwise keep the ???.app */
3852 if (pend2 == pend)
3853 pend = pend1;
3854 }
3855# endif
3856 /* remove "src/" from exe_name, if present */
3857 if (p == exe_name)
3858 pend = remove_tail(p, pend, (char_u *)"src");
3859#endif
3860
3861 /* for $VIM, remove "runtime/" or "vim54/", if present */
3862 if (!vimruntime)
3863 {
3864 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
3865 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
3866 }
3867
3868 /* remove trailing path separator */
3869#ifndef MACOS_CLASSIC
3870 /* With MacOS path (with colons) the final colon is required */
3871 /* to avoid confusion between absoulute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003872 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 --pend;
3874#endif
3875
3876 /* check that the result is a directory name */
3877 p = vim_strnsave(p, (int)(pend - p));
3878
3879 if (p != NULL && !mch_isdir(p))
3880 {
3881 vim_free(p);
3882 p = NULL;
3883 }
3884 else
3885 {
3886#ifdef USE_EXE_NAME
3887 /* may add "/vim54" or "/runtime" if it exists */
3888 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
3889 {
3890 vim_free(p);
3891 p = pend;
3892 }
3893#endif
3894 *mustfree = TRUE;
3895 }
3896 }
3897 }
3898
3899#ifdef HAVE_PATHDEF
3900 /* When there is a pathdef.c file we can use default_vim_dir and
3901 * default_vimruntime_dir */
3902 if (p == NULL)
3903 {
3904 /* Only use default_vimruntime_dir when it is not empty */
3905 if (vimruntime && *default_vimruntime_dir != NUL)
3906 {
3907 p = default_vimruntime_dir;
3908 *mustfree = FALSE;
3909 }
3910 else if (*default_vim_dir != NUL)
3911 {
3912 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
3913 *mustfree = TRUE;
3914 else
3915 {
3916 p = default_vim_dir;
3917 *mustfree = FALSE;
3918 }
3919 }
3920 }
3921#endif
3922
3923 /*
3924 * Set the environment variable, so that the new value can be found fast
3925 * next time, and others can also use it (e.g. Perl).
3926 */
3927 if (p != NULL)
3928 {
3929 if (vimruntime)
3930 {
3931 vim_setenv((char_u *)"VIMRUNTIME", p);
3932 didset_vimruntime = TRUE;
3933#ifdef FEAT_GETTEXT
3934 {
Bram Moolenaard6754642005-01-17 22:18:45 +00003935 char_u *buf = concat_str(p, (char_u *)"/lang");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937 if (buf != NULL)
3938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 bindtextdomain(VIMPACKAGE, (char *)buf);
3940 vim_free(buf);
3941 }
3942 }
3943#endif
3944 }
3945 else
3946 {
3947 vim_setenv((char_u *)"VIM", p);
3948 didset_vim = TRUE;
3949 }
3950 }
3951 return p;
3952}
3953
3954/*
3955 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
3956 * Return NULL if not, return its name in allocated memory otherwise.
3957 */
3958 static char_u *
3959vim_version_dir(vimdir)
3960 char_u *vimdir;
3961{
3962 char_u *p;
3963
3964 if (vimdir == NULL || *vimdir == NUL)
3965 return NULL;
3966 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
3967 if (p != NULL && mch_isdir(p))
3968 return p;
3969 vim_free(p);
3970 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
3971 if (p != NULL && mch_isdir(p))
3972 return p;
3973 vim_free(p);
3974 return NULL;
3975}
3976
3977/*
3978 * If the string between "p" and "pend" ends in "name/", return "pend" minus
3979 * the length of "name/". Otherwise return "pend".
3980 */
3981 static char_u *
3982remove_tail(p, pend, name)
3983 char_u *p;
3984 char_u *pend;
3985 char_u *name;
3986{
3987 int len = (int)STRLEN(name) + 1;
3988 char_u *newend = pend - len;
3989
3990 if (newend >= p
3991 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003992 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 return newend;
3994 return pend;
3995}
3996
3997#if defined(USE_EXE_NAME) && defined(MACOS_X)
3998/*
3999 * If the string between "p" and "pend" ends in "???.ext/", return "pend"
4000 * minus the length of "???.ext/". Otherwise return "pend".
4001 */
4002 static char_u *
4003remove_tail_with_ext(p, pend, ext)
4004 char_u *p;
4005 char_u *pend;
4006 char_u *ext;
4007{
4008 int len = (int)STRLEN(ext) + 1;
4009 char_u *newend = pend - len;
4010
4011 if (newend >= p && fnamencmp(newend, ext, len - 1) == 0)
Bram Moolenaar86b68352004-12-27 21:59:20 +00004012 while (newend > p && !after_pathsep(p, newend))
4013 mb_ptr_back(p, newend);
4014 if (newend == p || after_pathsep(p, newend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return newend;
4016 return pend;
4017}
4018#endif
4019
4020/*
4021 * Call expand_env() and store the result in an allocated string.
4022 * This is not very memory efficient, this expects the result to be freed
4023 * again soon.
4024 */
4025 char_u *
4026expand_env_save(src)
4027 char_u *src;
4028{
4029 char_u *p;
4030
4031 p = alloc(MAXPATHL);
4032 if (p != NULL)
4033 expand_env(src, p, MAXPATHL);
4034 return p;
4035}
4036
4037/*
4038 * Our portable version of setenv.
4039 */
4040 void
4041vim_setenv(name, val)
4042 char_u *name;
4043 char_u *val;
4044{
4045#ifdef HAVE_SETENV
4046 mch_setenv((char *)name, (char *)val, 1);
4047#else
4048 char_u *envbuf;
4049
4050 /*
4051 * Putenv does not copy the string, it has to remain
4052 * valid. The allocated memory will never be freed.
4053 */
4054 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4055 if (envbuf != NULL)
4056 {
4057 sprintf((char *)envbuf, "%s=%s", name, val);
4058 putenv((char *)envbuf);
4059 }
4060#endif
4061}
4062
4063#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4064/*
4065 * Function given to ExpandGeneric() to obtain an environment variable name.
4066 */
4067/*ARGSUSED*/
4068 char_u *
4069get_env_name(xp, idx)
4070 expand_T *xp;
4071 int idx;
4072{
4073# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4074 /*
4075 * No environ[] on the Amiga and on the Mac (using MPW).
4076 */
4077 return NULL;
4078# else
4079# ifndef __WIN32__
4080 /* Borland C++ 5.2 has this in a header file. */
4081 extern char **environ;
4082# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004083# define ENVNAMELEN 100
4084 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 char_u *str;
4086 int n;
4087
4088 str = (char_u *)environ[idx];
4089 if (str == NULL)
4090 return NULL;
4091
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004092 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
4094 if (str[n] == '=' || str[n] == NUL)
4095 break;
4096 name[n] = str[n];
4097 }
4098 name[n] = NUL;
4099 return name;
4100# endif
4101}
4102#endif
4103
4104/*
4105 * Replace home directory by "~" in each space or comma separated file name in
4106 * 'src'.
4107 * If anything fails (except when out of space) dst equals src.
4108 */
4109 void
4110home_replace(buf, src, dst, dstlen, one)
4111 buf_T *buf; /* when not NULL, check for help files */
4112 char_u *src; /* input file name */
4113 char_u *dst; /* where to put the result */
4114 int dstlen; /* maximum length of the result */
4115 int one; /* if TRUE, only replace one file name, include
4116 spaces and commas in the file name. */
4117{
4118 size_t dirlen = 0, envlen = 0;
4119 size_t len;
4120 char_u *homedir_env;
4121 char_u *p;
4122
4123 if (src == NULL)
4124 {
4125 *dst = NUL;
4126 return;
4127 }
4128
4129 /*
4130 * If the file is a help file, remove the path completely.
4131 */
4132 if (buf != NULL && buf->b_help)
4133 {
4134 STRCPY(dst, gettail(src));
4135 return;
4136 }
4137
4138 /*
4139 * We check both the value of the $HOME environment variable and the
4140 * "real" home directory.
4141 */
4142 if (homedir != NULL)
4143 dirlen = STRLEN(homedir);
4144
4145#ifdef VMS
4146 homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
4147#else
4148 homedir_env = mch_getenv((char_u *)"HOME");
4149#endif
4150
4151 if (homedir_env != NULL && *homedir_env == NUL)
4152 homedir_env = NULL;
4153 if (homedir_env != NULL)
4154 envlen = STRLEN(homedir_env);
4155
4156 if (!one)
4157 src = skipwhite(src);
4158 while (*src && dstlen > 0)
4159 {
4160 /*
4161 * Here we are at the beginning of a file name.
4162 * First, check to see if the beginning of the file name matches
4163 * $HOME or the "real" home directory. Check that there is a '/'
4164 * after the match (so that if e.g. the file is "/home/pieter/bla",
4165 * and the home directory is "/home/piet", the file does not end up
4166 * as "~er/bla" (which would seem to indicate the file "bla" in user
4167 * er's home directory)).
4168 */
4169 p = homedir;
4170 len = dirlen;
4171 for (;;)
4172 {
4173 if ( len
4174 && fnamencmp(src, p, len) == 0
4175 && (vim_ispathsep(src[len])
4176 || (!one && (src[len] == ',' || src[len] == ' '))
4177 || src[len] == NUL))
4178 {
4179 src += len;
4180 if (--dstlen > 0)
4181 *dst++ = '~';
4182
4183 /*
4184 * If it's just the home directory, add "/".
4185 */
4186 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4187 *dst++ = '/';
4188 break;
4189 }
4190 if (p == homedir_env)
4191 break;
4192 p = homedir_env;
4193 len = envlen;
4194 }
4195
4196 /* if (!one) skip to separator: space or comma */
4197 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4198 *dst++ = *src++;
4199 /* skip separator */
4200 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4201 *dst++ = *src++;
4202 }
4203 /* if (dstlen == 0) out of space, what to do??? */
4204
4205 *dst = NUL;
4206}
4207
4208/*
4209 * Like home_replace, store the replaced string in allocated memory.
4210 * When something fails, NULL is returned.
4211 */
4212 char_u *
4213home_replace_save(buf, src)
4214 buf_T *buf; /* when not NULL, check for help files */
4215 char_u *src; /* input file name */
4216{
4217 char_u *dst;
4218 unsigned len;
4219
4220 len = 3; /* space for "~/" and trailing NUL */
4221 if (src != NULL) /* just in case */
4222 len += (unsigned)STRLEN(src);
4223 dst = alloc(len);
4224 if (dst != NULL)
4225 home_replace(buf, src, dst, len, TRUE);
4226 return dst;
4227}
4228
4229/*
4230 * Compare two file names and return:
4231 * FPC_SAME if they both exist and are the same file.
4232 * FPC_SAMEX if they both don't exist and have the same file name.
4233 * FPC_DIFF if they both exist and are different files.
4234 * FPC_NOTX if they both don't exist.
4235 * FPC_DIFFX if one of them doesn't exist.
4236 * For the first name environment variables are expanded
4237 */
4238 int
4239fullpathcmp(s1, s2, checkname)
4240 char_u *s1, *s2;
4241 int checkname; /* when both don't exist, check file names */
4242{
4243#ifdef UNIX
4244 char_u exp1[MAXPATHL];
4245 char_u full1[MAXPATHL];
4246 char_u full2[MAXPATHL];
4247 struct stat st1, st2;
4248 int r1, r2;
4249
4250 expand_env(s1, exp1, MAXPATHL);
4251 r1 = mch_stat((char *)exp1, &st1);
4252 r2 = mch_stat((char *)s2, &st2);
4253 if (r1 != 0 && r2 != 0)
4254 {
4255 /* if mch_stat() doesn't work, may compare the names */
4256 if (checkname)
4257 {
4258 if (fnamecmp(exp1, s2) == 0)
4259 return FPC_SAMEX;
4260 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4261 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4262 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4263 return FPC_SAMEX;
4264 }
4265 return FPC_NOTX;
4266 }
4267 if (r1 != 0 || r2 != 0)
4268 return FPC_DIFFX;
4269 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4270 return FPC_SAME;
4271 return FPC_DIFF;
4272#else
4273 char_u *exp1; /* expanded s1 */
4274 char_u *full1; /* full path of s1 */
4275 char_u *full2; /* full path of s2 */
4276 int retval = FPC_DIFF;
4277 int r1, r2;
4278
4279 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4280 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4281 {
4282 full1 = exp1 + MAXPATHL;
4283 full2 = full1 + MAXPATHL;
4284
4285 expand_env(s1, exp1, MAXPATHL);
4286 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4287 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4288
4289 /* If vim_FullName() fails, the file probably doesn't exist. */
4290 if (r1 != OK && r2 != OK)
4291 {
4292 if (checkname && fnamecmp(exp1, s2) == 0)
4293 retval = FPC_SAMEX;
4294 else
4295 retval = FPC_NOTX;
4296 }
4297 else if (r1 != OK || r2 != OK)
4298 retval = FPC_DIFFX;
4299 else if (fnamecmp(full1, full2))
4300 retval = FPC_DIFF;
4301 else
4302 retval = FPC_SAME;
4303 vim_free(exp1);
4304 }
4305 return retval;
4306#endif
4307}
4308
4309/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004310 * Get the tail of a path: the file name.
4311 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 */
4313 char_u *
4314gettail(fname)
4315 char_u *fname;
4316{
4317 char_u *p1, *p2;
4318
4319 if (fname == NULL)
4320 return (char_u *)"";
4321 for (p1 = p2 = fname; *p2; ) /* find last part of path */
4322 {
4323 if (vim_ispathsep(*p2))
4324 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004325 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327 return p1;
4328}
4329
4330/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004331 * Get pointer to tail of "fname", including path separators. Putting a NUL
4332 * here leaves the directory name. Takes care of "c:/" and "//".
4333 * Always returns a valid pointer.
4334 */
4335 char_u *
4336gettail_sep(fname)
4337 char_u *fname;
4338{
4339 char_u *p;
4340 char_u *t;
4341
4342 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4343 t = gettail(fname);
4344 while (t > p && after_pathsep(fname, t))
4345 --t;
4346#ifdef VMS
4347 /* path separator is part of the path */
4348 ++t;
4349#endif
4350 return t;
4351}
4352
4353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 * get the next path component (just after the next path separator).
4355 */
4356 char_u *
4357getnextcomp(fname)
4358 char_u *fname;
4359{
4360 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004361 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (*fname)
4363 ++fname;
4364 return fname;
4365}
4366
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367/*
4368 * Get a pointer to one character past the head of a path name.
4369 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4370 * If there is no head, path is returned.
4371 */
4372 char_u *
4373get_past_head(path)
4374 char_u *path;
4375{
4376 char_u *retval;
4377
4378#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4379 /* may skip "c:" */
4380 if (isalpha(path[0]) && path[1] == ':')
4381 retval = path + 2;
4382 else
4383 retval = path;
4384#else
4385# if defined(AMIGA)
4386 /* may skip "label:" */
4387 retval = vim_strchr(path, ':');
4388 if (retval == NULL)
4389 retval = path;
4390# else /* Unix */
4391 retval = path;
4392# endif
4393#endif
4394
4395 while (vim_ispathsep(*retval))
4396 ++retval;
4397
4398 return retval;
4399}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401/*
4402 * return TRUE if 'c' is a path separator.
4403 */
4404 int
4405vim_ispathsep(c)
4406 int c;
4407{
4408#ifdef RISCOS
4409 return (c == '.' || c == ':');
4410#else
4411# ifdef UNIX
4412 return (c == '/'); /* UNIX has ':' inside file names */
4413# else
4414# ifdef BACKSLASH_IN_FILENAME
4415 return (c == ':' || c == '/' || c == '\\');
4416# else
4417# ifdef VMS
4418 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4419 return (c == ':' || c == '[' || c == ']' || c == '/'
4420 || c == '<' || c == '>' || c == '"' );
4421# else
4422# ifdef COLON_AS_PATHSEP
4423 return (c == ':');
4424# else /* Amiga */
4425 return (c == ':' || c == '/');
4426# endif
4427# endif /* VMS */
4428# endif
4429# endif
4430#endif /* RISC OS */
4431}
4432
4433#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4434/*
4435 * return TRUE if 'c' is a path list separator.
4436 */
4437 int
4438vim_ispathlistsep(c)
4439 int c;
4440{
4441#ifdef UNIX
4442 return (c == ':');
4443#else
4444 return (c == ';'); /* might not be rigth for every system... */
4445#endif
4446}
4447#endif
4448
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004449/*
4450 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
4451 * Also returns TRUE if there is no directory name.
4452 * "fname" must be writable!.
4453 */
4454 int
4455dir_of_file_exists(fname)
4456 char_u *fname;
4457{
4458 char_u *p;
4459 int c;
4460 int retval;
4461
4462 p = gettail_sep(fname);
4463 if (p == fname)
4464 return TRUE;
4465 c = *p;
4466 *p = NUL;
4467 retval = mch_isdir(fname);
4468 *p = c;
4469 return retval;
4470}
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#if (defined(CASE_INSENSITIVE_FILENAME) && defined(BACKSLASH_IN_FILENAME)) \
4473 || defined(PROTO)
4474/*
4475 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally.
4476 */
4477 int
4478vim_fnamecmp(x, y)
4479 char_u *x, *y;
4480{
4481 return vim_fnamencmp(x, y, MAXPATHL);
4482}
4483
4484 int
4485vim_fnamencmp(x, y, len)
4486 char_u *x, *y;
4487 size_t len;
4488{
4489 while (len > 0 && *x && *y)
4490 {
4491 if (TOLOWER_LOC(*x) != TOLOWER_LOC(*y)
4492 && !(*x == '/' && *y == '\\')
4493 && !(*x == '\\' && *y == '/'))
4494 break;
4495 ++x;
4496 ++y;
4497 --len;
4498 }
4499 if (len == 0)
4500 return 0;
4501 return (*x - *y);
4502}
4503#endif
4504
4505/*
4506 * Concatenate file names fname1 and fname2 into allocated memory.
4507 * Only add a '/' or '\\' when 'sep' is TRUE and it is neccesary.
4508 */
4509 char_u *
4510concat_fnames(fname1, fname2, sep)
4511 char_u *fname1;
4512 char_u *fname2;
4513 int sep;
4514{
4515 char_u *dest;
4516
4517 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
4518 if (dest != NULL)
4519 {
4520 STRCPY(dest, fname1);
4521 if (sep)
4522 add_pathsep(dest);
4523 STRCAT(dest, fname2);
4524 }
4525 return dest;
4526}
4527
Bram Moolenaard6754642005-01-17 22:18:45 +00004528#if defined(FEAT_EVAL) || defined(FEAT_GETTEXT) || defined(PROTO)
4529/*
4530 * Concatenate two strings and return the result in allocated memory.
4531 * Returns NULL when out of memory.
4532 */
4533 char_u *
4534concat_str(str1, str2)
4535 char_u *str1;
4536 char_u *str2;
4537{
4538 char_u *dest;
4539 size_t l = STRLEN(str1);
4540
4541 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
4542 if (dest != NULL)
4543 {
4544 STRCPY(dest, str1);
4545 STRCPY(dest + l, str2);
4546 }
4547 return dest;
4548}
4549#endif
4550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551/*
4552 * Add a path separator to a file name, unless it already ends in a path
4553 * separator.
4554 */
4555 void
4556add_pathsep(p)
4557 char_u *p;
4558{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004559 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 STRCAT(p, PATHSEPSTR);
4561}
4562
4563/*
4564 * FullName_save - Make an allocated copy of a full file name.
4565 * Returns NULL when out of memory.
4566 */
4567 char_u *
4568FullName_save(fname, force)
4569 char_u *fname;
4570 int force; /* force expansion, even when it already looks
4571 like a full path name */
4572{
4573 char_u *buf;
4574 char_u *new_fname = NULL;
4575
4576 if (fname == NULL)
4577 return NULL;
4578
4579 buf = alloc((unsigned)MAXPATHL);
4580 if (buf != NULL)
4581 {
4582 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
4583 new_fname = vim_strsave(buf);
4584 else
4585 new_fname = vim_strsave(fname);
4586 vim_free(buf);
4587 }
4588 return new_fname;
4589}
4590
4591#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
4592
4593static char_u *skip_string __ARGS((char_u *p));
4594
4595/*
4596 * Find the start of a comment, not knowing if we are in a comment right now.
4597 * Search starts at w_cursor.lnum and goes backwards.
4598 */
4599 pos_T *
4600find_start_comment(ind_maxcomment) /* XXX */
4601 int ind_maxcomment;
4602{
4603 pos_T *pos;
4604 char_u *line;
4605 char_u *p;
4606
4607 if ((pos = findmatchlimit(NULL, '*', FM_BACKWARD, ind_maxcomment)) == NULL)
4608 return NULL;
4609
4610 /*
4611 * Check if the comment start we found is inside a string.
4612 */
4613 line = ml_get(pos->lnum);
4614 for (p = line; *p && (unsigned)(p - line) < pos->col; ++p)
4615 p = skip_string(p);
4616 if ((unsigned)(p - line) > pos->col)
4617 return NULL;
4618 return pos;
4619}
4620
4621/*
4622 * Skip to the end of a "string" and a 'c' character.
4623 * If there is no string or character, return argument unmodified.
4624 */
4625 static char_u *
4626skip_string(p)
4627 char_u *p;
4628{
4629 int i;
4630
4631 /*
4632 * We loop, because strings may be concatenated: "date""time".
4633 */
4634 for ( ; ; ++p)
4635 {
4636 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
4637 {
4638 if (!p[1]) /* ' at end of line */
4639 break;
4640 i = 2;
4641 if (p[1] == '\\') /* '\n' or '\000' */
4642 {
4643 ++i;
4644 while (vim_isdigit(p[i - 1])) /* '\000' */
4645 ++i;
4646 }
4647 if (p[i] == '\'') /* check for trailing ' */
4648 {
4649 p += i;
4650 continue;
4651 }
4652 }
4653 else if (p[0] == '"') /* start of string */
4654 {
4655 for (++p; p[0]; ++p)
4656 {
4657 if (p[0] == '\\' && p[1] != NUL)
4658 ++p;
4659 else if (p[0] == '"') /* end of string */
4660 break;
4661 }
4662 if (p[0] == '"')
4663 continue;
4664 }
4665 break; /* no string found */
4666 }
4667 if (!*p)
4668 --p; /* backup from NUL */
4669 return p;
4670}
4671#endif /* FEAT_CINDENT || FEAT_SYN_HL */
4672
4673#if defined(FEAT_CINDENT) || defined(PROTO)
4674
4675/*
4676 * Do C or expression indenting on the current line.
4677 */
4678 void
4679do_c_expr_indent()
4680{
4681# ifdef FEAT_EVAL
4682 if (*curbuf->b_p_inde != NUL)
4683 fixthisline(get_expr_indent);
4684 else
4685# endif
4686 fixthisline(get_c_indent);
4687}
4688
4689/*
4690 * Functions for C-indenting.
4691 * Most of this originally comes from Eric Fischer.
4692 */
4693/*
4694 * Below "XXX" means that this function may unlock the current line.
4695 */
4696
4697static char_u *cin_skipcomment __ARGS((char_u *));
4698static int cin_nocode __ARGS((char_u *));
4699static pos_T *find_line_comment __ARGS((void));
4700static int cin_islabel_skip __ARGS((char_u **));
4701static int cin_isdefault __ARGS((char_u *));
4702static char_u *after_label __ARGS((char_u *l));
4703static int get_indent_nolabel __ARGS((linenr_T lnum));
4704static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment));
4705static int cin_first_id_amount __ARGS((void));
4706static int cin_get_equal_amount __ARGS((linenr_T lnum));
4707static int cin_ispreproc __ARGS((char_u *));
4708static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
4709static int cin_iscomment __ARGS((char_u *));
4710static int cin_islinecomment __ARGS((char_u *));
4711static int cin_isterminated __ARGS((char_u *, int, int));
4712static int cin_isinit __ARGS((void));
4713static int cin_isfuncdecl __ARGS((char_u **, linenr_T));
4714static int cin_isif __ARGS((char_u *));
4715static int cin_iselse __ARGS((char_u *));
4716static int cin_isdo __ARGS((char_u *));
4717static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int));
4718static int cin_isbreak __ARGS((char_u *));
4719static int cin_is_cpp_baseclass __ARGS((char_u *line, colnr_T *col));
4720static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
4721static int cin_skip2pos __ARGS((pos_T *trypos));
4722static pos_T *find_start_brace __ARGS((int));
4723static pos_T *find_match_paren __ARGS((int, int));
4724static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos));
4725static int find_last_paren __ARGS((char_u *l, int start, int end));
4726static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment));
4727
4728/*
4729 * Skip over white space and C comments within the line.
4730 */
4731 static char_u *
4732cin_skipcomment(s)
4733 char_u *s;
4734{
4735 while (*s)
4736 {
4737 s = skipwhite(s);
4738 if (*s != '/')
4739 break;
4740 ++s;
4741 if (*s == '/') /* slash-slash comment continues till eol */
4742 {
4743 s += STRLEN(s);
4744 break;
4745 }
4746 if (*s != '*')
4747 break;
4748 for (++s; *s; ++s) /* skip slash-star comment */
4749 if (s[0] == '*' && s[1] == '/')
4750 {
4751 s += 2;
4752 break;
4753 }
4754 }
4755 return s;
4756}
4757
4758/*
4759 * Return TRUE if there there is no code at *s. White space and comments are
4760 * not considered code.
4761 */
4762 static int
4763cin_nocode(s)
4764 char_u *s;
4765{
4766 return *cin_skipcomment(s) == NUL;
4767}
4768
4769/*
4770 * Check previous lines for a "//" line comment, skipping over blank lines.
4771 */
4772 static pos_T *
4773find_line_comment() /* XXX */
4774{
4775 static pos_T pos;
4776 char_u *line;
4777 char_u *p;
4778
4779 pos = curwin->w_cursor;
4780 while (--pos.lnum > 0)
4781 {
4782 line = ml_get(pos.lnum);
4783 p = skipwhite(line);
4784 if (cin_islinecomment(p))
4785 {
4786 pos.col = (int)(p - line);
4787 return &pos;
4788 }
4789 if (*p != NUL)
4790 break;
4791 }
4792 return NULL;
4793}
4794
4795/*
4796 * Check if string matches "label:"; move to character after ':' if true.
4797 */
4798 static int
4799cin_islabel_skip(s)
4800 char_u **s;
4801{
4802 if (!vim_isIDc(**s)) /* need at least one ID character */
4803 return FALSE;
4804
4805 while (vim_isIDc(**s))
4806 (*s)++;
4807
4808 *s = cin_skipcomment(*s);
4809
4810 /* "::" is not a label, it's C++ */
4811 return (**s == ':' && *++*s != ':');
4812}
4813
4814/*
4815 * Recognize a label: "label:".
4816 * Note: curwin->w_cursor must be where we are looking for the label.
4817 */
4818 int
4819cin_islabel(ind_maxcomment) /* XXX */
4820 int ind_maxcomment;
4821{
4822 char_u *s;
4823
4824 s = cin_skipcomment(ml_get_curline());
4825
4826 /*
4827 * Exclude "default" from labels, since it should be indented
4828 * like a switch label. Same for C++ scope declarations.
4829 */
4830 if (cin_isdefault(s))
4831 return FALSE;
4832 if (cin_isscopedecl(s))
4833 return FALSE;
4834
4835 if (cin_islabel_skip(&s))
4836 {
4837 /*
4838 * Only accept a label if the previous line is terminated or is a case
4839 * label.
4840 */
4841 pos_T cursor_save;
4842 pos_T *trypos;
4843 char_u *line;
4844
4845 cursor_save = curwin->w_cursor;
4846 while (curwin->w_cursor.lnum > 1)
4847 {
4848 --curwin->w_cursor.lnum;
4849
4850 /*
4851 * If we're in a comment now, skip to the start of the comment.
4852 */
4853 curwin->w_cursor.col = 0;
4854 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
4855 curwin->w_cursor = *trypos;
4856
4857 line = ml_get_curline();
4858 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
4859 continue;
4860 if (*(line = cin_skipcomment(line)) == NUL)
4861 continue;
4862
4863 curwin->w_cursor = cursor_save;
4864 if (cin_isterminated(line, TRUE, FALSE)
4865 || cin_isscopedecl(line)
4866 || cin_iscase(line)
4867 || (cin_islabel_skip(&line) && cin_nocode(line)))
4868 return TRUE;
4869 return FALSE;
4870 }
4871 curwin->w_cursor = cursor_save;
4872 return TRUE; /* label at start of file??? */
4873 }
4874 return FALSE;
4875}
4876
4877/*
4878 * Recognize structure initialization and enumerations.
4879 * Q&D-Implementation:
4880 * check for "=" at end or "[typedef] enum" at beginning of line.
4881 */
4882 static int
4883cin_isinit(void)
4884{
4885 char_u *s;
4886
4887 s = cin_skipcomment(ml_get_curline());
4888
4889 if (STRNCMP(s, "typedef", 7) == 0 && !vim_isIDc(s[7]))
4890 s = cin_skipcomment(s + 7);
4891
4892 if (STRNCMP(s, "enum", 4) == 0 && !vim_isIDc(s[4]))
4893 return TRUE;
4894
4895 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
4896 return TRUE;
4897
4898 return FALSE;
4899}
4900
4901/*
4902 * Recognize a switch label: "case .*:" or "default:".
4903 */
4904 int
4905cin_iscase(s)
4906 char_u *s;
4907{
4908 s = cin_skipcomment(s);
4909 if (STRNCMP(s, "case", 4) == 0 && !vim_isIDc(s[4]))
4910 {
4911 for (s += 4; *s; ++s)
4912 {
4913 s = cin_skipcomment(s);
4914 if (*s == ':')
4915 {
4916 if (s[1] == ':') /* skip over "::" for C++ */
4917 ++s;
4918 else
4919 return TRUE;
4920 }
4921 if (*s == '\'' && s[1] && s[2] == '\'')
4922 s += 2; /* skip over '.' */
4923 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
4924 return FALSE; /* stop at comment */
4925 else if (*s == '"')
4926 return FALSE; /* stop at string */
4927 }
4928 return FALSE;
4929 }
4930
4931 if (cin_isdefault(s))
4932 return TRUE;
4933 return FALSE;
4934}
4935
4936/*
4937 * Recognize a "default" switch label.
4938 */
4939 static int
4940cin_isdefault(s)
4941 char_u *s;
4942{
4943 return (STRNCMP(s, "default", 7) == 0
4944 && *(s = cin_skipcomment(s + 7)) == ':'
4945 && s[1] != ':');
4946}
4947
4948/*
4949 * Recognize a "public/private/proctected" scope declaration label.
4950 */
4951 int
4952cin_isscopedecl(s)
4953 char_u *s;
4954{
4955 int i;
4956
4957 s = cin_skipcomment(s);
4958 if (STRNCMP(s, "public", 6) == 0)
4959 i = 6;
4960 else if (STRNCMP(s, "protected", 9) == 0)
4961 i = 9;
4962 else if (STRNCMP(s, "private", 7) == 0)
4963 i = 7;
4964 else
4965 return FALSE;
4966 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
4967}
4968
4969/*
4970 * Return a pointer to the first non-empty non-comment character after a ':'.
4971 * Return NULL if not found.
4972 * case 234: a = b;
4973 * ^
4974 */
4975 static char_u *
4976after_label(l)
4977 char_u *l;
4978{
4979 for ( ; *l; ++l)
4980 {
4981 if (*l == ':')
4982 {
4983 if (l[1] == ':') /* skip over "::" for C++ */
4984 ++l;
4985 else if (!cin_iscase(l + 1))
4986 break;
4987 }
4988 else if (*l == '\'' && l[1] && l[2] == '\'')
4989 l += 2; /* skip over 'x' */
4990 }
4991 if (*l == NUL)
4992 return NULL;
4993 l = cin_skipcomment(l + 1);
4994 if (*l == NUL)
4995 return NULL;
4996 return l;
4997}
4998
4999/*
5000 * Get indent of line "lnum", skipping a label.
5001 * Return 0 if there is nothing after the label.
5002 */
5003 static int
5004get_indent_nolabel(lnum) /* XXX */
5005 linenr_T lnum;
5006{
5007 char_u *l;
5008 pos_T fp;
5009 colnr_T col;
5010 char_u *p;
5011
5012 l = ml_get(lnum);
5013 p = after_label(l);
5014 if (p == NULL)
5015 return 0;
5016
5017 fp.col = (colnr_T)(p - l);
5018 fp.lnum = lnum;
5019 getvcol(curwin, &fp, &col, NULL, NULL);
5020 return (int)col;
5021}
5022
5023/*
5024 * Find indent for line "lnum", ignoring any case or jump label.
5025 * Also return a pointer to the text (after the label).
5026 * label: if (asdf && asdfasdf)
5027 * ^
5028 */
5029 static int
5030skip_label(lnum, pp, ind_maxcomment)
5031 linenr_T lnum;
5032 char_u **pp;
5033 int ind_maxcomment;
5034{
5035 char_u *l;
5036 int amount;
5037 pos_T cursor_save;
5038
5039 cursor_save = curwin->w_cursor;
5040 curwin->w_cursor.lnum = lnum;
5041 l = ml_get_curline();
5042 /* XXX */
5043 if (cin_iscase(l) || cin_isscopedecl(l) || cin_islabel(ind_maxcomment))
5044 {
5045 amount = get_indent_nolabel(lnum);
5046 l = after_label(ml_get_curline());
5047 if (l == NULL) /* just in case */
5048 l = ml_get_curline();
5049 }
5050 else
5051 {
5052 amount = get_indent();
5053 l = ml_get_curline();
5054 }
5055 *pp = l;
5056
5057 curwin->w_cursor = cursor_save;
5058 return amount;
5059}
5060
5061/*
5062 * Return the indent of the first variable name after a type in a declaration.
5063 * int a, indent of "a"
5064 * static struct foo b, indent of "b"
5065 * enum bla c, indent of "c"
5066 * Returns zero when it doesn't look like a declaration.
5067 */
5068 static int
5069cin_first_id_amount()
5070{
5071 char_u *line, *p, *s;
5072 int len;
5073 pos_T fp;
5074 colnr_T col;
5075
5076 line = ml_get_curline();
5077 p = skipwhite(line);
5078 len = skiptowhite(p) - p;
5079 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5080 {
5081 p = skipwhite(p + 6);
5082 len = skiptowhite(p) - p;
5083 }
5084 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5085 p = skipwhite(p + 6);
5086 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5087 p = skipwhite(p + 4);
5088 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5089 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5090 {
5091 s = skipwhite(p + len);
5092 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5093 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5094 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5095 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5096 p = s;
5097 }
5098 for (len = 0; vim_isIDc(p[len]); ++len)
5099 ;
5100 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5101 return 0;
5102
5103 p = skipwhite(p + len);
5104 fp.lnum = curwin->w_cursor.lnum;
5105 fp.col = (colnr_T)(p - line);
5106 getvcol(curwin, &fp, &col, NULL, NULL);
5107 return (int)col;
5108}
5109
5110/*
5111 * Return the indent of the first non-blank after an equal sign.
5112 * char *foo = "here";
5113 * Return zero if no (useful) equal sign found.
5114 * Return -1 if the line above "lnum" ends in a backslash.
5115 * foo = "asdf\
5116 * asdf\
5117 * here";
5118 */
5119 static int
5120cin_get_equal_amount(lnum)
5121 linenr_T lnum;
5122{
5123 char_u *line;
5124 char_u *s;
5125 colnr_T col;
5126 pos_T fp;
5127
5128 if (lnum > 1)
5129 {
5130 line = ml_get(lnum - 1);
5131 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5132 return -1;
5133 }
5134
5135 line = s = ml_get(lnum);
5136 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5137 {
5138 if (cin_iscomment(s)) /* ignore comments */
5139 s = cin_skipcomment(s);
5140 else
5141 ++s;
5142 }
5143 if (*s != '=')
5144 return 0;
5145
5146 s = skipwhite(s + 1);
5147 if (cin_nocode(s))
5148 return 0;
5149
5150 if (*s == '"') /* nice alignment for continued strings */
5151 ++s;
5152
5153 fp.lnum = lnum;
5154 fp.col = (colnr_T)(s - line);
5155 getvcol(curwin, &fp, &col, NULL, NULL);
5156 return (int)col;
5157}
5158
5159/*
5160 * Recognize a preprocessor statement: Any line that starts with '#'.
5161 */
5162 static int
5163cin_ispreproc(s)
5164 char_u *s;
5165{
5166 s = skipwhite(s);
5167 if (*s == '#')
5168 return TRUE;
5169 return FALSE;
5170}
5171
5172/*
5173 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5174 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5175 * start and return the line in "*pp".
5176 */
5177 static int
5178cin_ispreproc_cont(pp, lnump)
5179 char_u **pp;
5180 linenr_T *lnump;
5181{
5182 char_u *line = *pp;
5183 linenr_T lnum = *lnump;
5184 int retval = FALSE;
5185
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005186 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
5188 if (cin_ispreproc(line))
5189 {
5190 retval = TRUE;
5191 *lnump = lnum;
5192 break;
5193 }
5194 if (lnum == 1)
5195 break;
5196 line = ml_get(--lnum);
5197 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5198 break;
5199 }
5200
5201 if (lnum != *lnump)
5202 *pp = ml_get(*lnump);
5203 return retval;
5204}
5205
5206/*
5207 * Recognize the start of a C or C++ comment.
5208 */
5209 static int
5210cin_iscomment(p)
5211 char_u *p;
5212{
5213 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5214}
5215
5216/*
5217 * Recognize the start of a "//" comment.
5218 */
5219 static int
5220cin_islinecomment(p)
5221 char_u *p;
5222{
5223 return (p[0] == '/' && p[1] == '/');
5224}
5225
5226/*
5227 * Recognize a line that starts with '{' or '}', or ends with ';', '{' or '}'.
5228 * Don't consider "} else" a terminated line.
5229 * Return the character terminating the line (ending char's have precedence if
5230 * both apply in order to determine initializations).
5231 */
5232 static int
5233cin_isterminated(s, incl_open, incl_comma)
5234 char_u *s;
5235 int incl_open; /* include '{' at the end as terminator */
5236 int incl_comma; /* recognize a trailing comma */
5237{
5238 char_u found_start = 0;
5239
5240 s = cin_skipcomment(s);
5241
5242 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
5243 found_start = *s;
5244
5245 while (*s)
5246 {
5247 /* skip over comments, "" strings and 'c'haracters */
5248 s = skip_string(cin_skipcomment(s));
5249 if ((*s == ';' || (incl_open && *s == '{') || *s == '}'
5250 || (incl_comma && *s == ','))
5251 && cin_nocode(s + 1))
5252 return *s;
5253
5254 if (*s)
5255 s++;
5256 }
5257 return found_start;
5258}
5259
5260/*
5261 * Recognize the basic picture of a function declaration -- it needs to
5262 * have an open paren somewhere and a close paren at the end of the line and
5263 * no semicolons anywhere.
5264 * When a line ends in a comma we continue looking in the next line.
5265 * "sp" points to a string with the line. When looking at other lines it must
5266 * be restored to the line. When it's NULL fetch lines here.
5267 * "lnum" is where we start looking.
5268 */
5269 static int
5270cin_isfuncdecl(sp, first_lnum)
5271 char_u **sp;
5272 linenr_T first_lnum;
5273{
5274 char_u *s;
5275 linenr_T lnum = first_lnum;
5276 int retval = FALSE;
5277
5278 if (sp == NULL)
5279 s = ml_get(lnum);
5280 else
5281 s = *sp;
5282
5283 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
5284 {
5285 if (cin_iscomment(s)) /* ignore comments */
5286 s = cin_skipcomment(s);
5287 else
5288 ++s;
5289 }
5290 if (*s != '(')
5291 return FALSE; /* ';', ' or " before any () or no '(' */
5292
5293 while (*s && *s != ';' && *s != '\'' && *s != '"')
5294 {
5295 if (*s == ')' && cin_nocode(s + 1))
5296 {
5297 /* ')' at the end: may have found a match
5298 * Check for he previous line not to end in a backslash:
5299 * #if defined(x) && \
5300 * defined(y)
5301 */
5302 lnum = first_lnum - 1;
5303 s = ml_get(lnum);
5304 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
5305 retval = TRUE;
5306 goto done;
5307 }
5308 if (*s == ',' && cin_nocode(s + 1))
5309 {
5310 /* ',' at the end: continue looking in the next line */
5311 if (lnum >= curbuf->b_ml.ml_line_count)
5312 break;
5313
5314 s = ml_get(++lnum);
5315 }
5316 else if (cin_iscomment(s)) /* ignore comments */
5317 s = cin_skipcomment(s);
5318 else
5319 ++s;
5320 }
5321
5322done:
5323 if (lnum != first_lnum && sp != NULL)
5324 *sp = ml_get(first_lnum);
5325
5326 return retval;
5327}
5328
5329 static int
5330cin_isif(p)
5331 char_u *p;
5332{
5333 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
5334}
5335
5336 static int
5337cin_iselse(p)
5338 char_u *p;
5339{
5340 if (*p == '}') /* accept "} else" */
5341 p = cin_skipcomment(p + 1);
5342 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
5343}
5344
5345 static int
5346cin_isdo(p)
5347 char_u *p;
5348{
5349 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
5350}
5351
5352/*
5353 * Check if this is a "while" that should have a matching "do".
5354 * We only accept a "while (condition) ;", with only white space between the
5355 * ')' and ';'. The condition may be spread over several lines.
5356 */
5357 static int
5358cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */
5359 char_u *p;
5360 linenr_T lnum;
5361 int ind_maxparen;
5362{
5363 pos_T cursor_save;
5364 pos_T *trypos;
5365 int retval = FALSE;
5366
5367 p = cin_skipcomment(p);
5368 if (*p == '}') /* accept "} while (cond);" */
5369 p = cin_skipcomment(p + 1);
5370 if (STRNCMP(p, "while", 5) == 0 && !vim_isIDc(p[5]))
5371 {
5372 cursor_save = curwin->w_cursor;
5373 curwin->w_cursor.lnum = lnum;
5374 curwin->w_cursor.col = 0;
5375 p = ml_get_curline();
5376 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
5377 {
5378 ++p;
5379 ++curwin->w_cursor.col;
5380 }
5381 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL
5382 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
5383 retval = TRUE;
5384 curwin->w_cursor = cursor_save;
5385 }
5386 return retval;
5387}
5388
5389 static int
5390cin_isbreak(p)
5391 char_u *p;
5392{
5393 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
5394}
5395
5396/* Find the position of a C++ base-class declaration or
5397 * constructor-initialization. eg:
5398 *
5399 * class MyClass :
5400 * baseClass <-- here
5401 * class MyClass : public baseClass,
5402 * anotherBaseClass <-- here (should probably lineup ??)
5403 * MyClass::MyClass(...) :
5404 * baseClass(...) <-- here (constructor-initialization)
5405 */
5406 static int
5407cin_is_cpp_baseclass(line, col)
5408 char_u *line;
5409 colnr_T *col;
5410{
5411 char_u *s;
5412 int class_or_struct, lookfor_ctor_init, cpp_base_class;
5413
5414 *col = 0;
5415
Bram Moolenaar21cf8232004-07-16 20:18:37 +00005416 s = skipwhite(line);
5417 if (*s == '#') /* skip #define FOO x ? (x) : x */
5418 return FALSE;
5419 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 if (*s == NUL)
5421 return FALSE;
5422
5423 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
5424
5425 while(*s != NUL)
5426 {
5427 if (s[0] == ':')
5428 {
5429 if (s[1] == ':')
5430 {
5431 /* skip double colon. It can't be a constructor
5432 * initialization any more */
5433 lookfor_ctor_init = FALSE;
5434 s = cin_skipcomment(s + 2);
5435 }
5436 else if (lookfor_ctor_init || class_or_struct)
5437 {
5438 /* we have something found, that looks like the start of
5439 * cpp-base-class-declaration or contructor-initialization */
5440 cpp_base_class = TRUE;
5441 lookfor_ctor_init = class_or_struct = FALSE;
5442 *col = 0;
5443 s = cin_skipcomment(s + 1);
5444 }
5445 else
5446 s = cin_skipcomment(s + 1);
5447 }
5448 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
5449 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
5450 {
5451 class_or_struct = TRUE;
5452 lookfor_ctor_init = FALSE;
5453
5454 if (*s == 'c')
5455 s = cin_skipcomment(s + 5);
5456 else
5457 s = cin_skipcomment(s + 6);
5458 }
5459 else
5460 {
5461 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
5462 {
5463 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
5464 }
5465 else if (s[0] == ')')
5466 {
5467 /* Constructor-initialization is assumed if we come across
5468 * something like "):" */
5469 class_or_struct = FALSE;
5470 lookfor_ctor_init = TRUE;
5471 }
5472 else if (!vim_isIDc(s[0]))
5473 {
5474 /* if it is not an identifier, we are wrong */
5475 class_or_struct = FALSE;
5476 lookfor_ctor_init = FALSE;
5477 }
5478 else if (*col == 0)
5479 {
5480 /* it can't be a constructor-initialization any more */
5481 lookfor_ctor_init = FALSE;
5482
5483 /* the first statement starts here: lineup with this one... */
5484 if (cpp_base_class && *col == 0)
5485 *col = (colnr_T)(s - line);
5486 }
5487
5488 s = cin_skipcomment(s + 1);
5489 }
5490 }
5491
5492 return cpp_base_class;
5493}
5494
5495/*
5496 * Return TRUE if string "s" ends with the string "find", possibly followed by
5497 * white space and comments. Skip strings and comments.
5498 * Ignore "ignore" after "find" if it's not NULL.
5499 */
5500 static int
5501cin_ends_in(s, find, ignore)
5502 char_u *s;
5503 char_u *find;
5504 char_u *ignore;
5505{
5506 char_u *p = s;
5507 char_u *r;
5508 int len = (int)STRLEN(find);
5509
5510 while (*p != NUL)
5511 {
5512 p = cin_skipcomment(p);
5513 if (STRNCMP(p, find, len) == 0)
5514 {
5515 r = skipwhite(p + len);
5516 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
5517 r = skipwhite(r + STRLEN(ignore));
5518 if (cin_nocode(r))
5519 return TRUE;
5520 }
5521 if (*p != NUL)
5522 ++p;
5523 }
5524 return FALSE;
5525}
5526
5527/*
5528 * Skip strings, chars and comments until at or past "trypos".
5529 * Return the column found.
5530 */
5531 static int
5532cin_skip2pos(trypos)
5533 pos_T *trypos;
5534{
5535 char_u *line;
5536 char_u *p;
5537
5538 p = line = ml_get(trypos->lnum);
5539 while (*p && (colnr_T)(p - line) < trypos->col)
5540 {
5541 if (cin_iscomment(p))
5542 p = cin_skipcomment(p);
5543 else
5544 {
5545 p = skip_string(p);
5546 ++p;
5547 }
5548 }
5549 return (int)(p - line);
5550}
5551
5552/*
5553 * Find the '{' at the start of the block we are in.
5554 * Return NULL if no match found.
5555 * Ignore a '{' that is in a comment, makes indenting the next three lines
5556 * work. */
5557/* foo() */
5558/* { */
5559/* } */
5560
5561 static pos_T *
5562find_start_brace(ind_maxcomment) /* XXX */
5563 int ind_maxcomment;
5564{
5565 pos_T cursor_save;
5566 pos_T *trypos;
5567 pos_T *pos;
5568 static pos_T pos_copy;
5569
5570 cursor_save = curwin->w_cursor;
5571 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
5572 {
5573 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
5574 trypos = &pos_copy;
5575 curwin->w_cursor = *trypos;
5576 pos = NULL;
5577 /* ignore the { if it's in a // comment */
5578 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
5579 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */
5580 break;
5581 if (pos != NULL)
5582 curwin->w_cursor.lnum = pos->lnum;
5583 }
5584 curwin->w_cursor = cursor_save;
5585 return trypos;
5586}
5587
5588/*
5589 * Find the matching '(', failing if it is in a comment.
5590 * Return NULL of no match found.
5591 */
5592 static pos_T *
5593find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */
5594 int ind_maxparen;
5595 int ind_maxcomment;
5596{
5597 pos_T cursor_save;
5598 pos_T *trypos;
5599 static pos_T pos_copy;
5600
5601 cursor_save = curwin->w_cursor;
5602 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
5603 {
5604 /* check if the ( is in a // comment */
5605 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
5606 trypos = NULL;
5607 else
5608 {
5609 pos_copy = *trypos; /* copy trypos, findmatch will change it */
5610 trypos = &pos_copy;
5611 curwin->w_cursor = *trypos;
5612 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */
5613 trypos = NULL;
5614 }
5615 }
5616 curwin->w_cursor = cursor_save;
5617 return trypos;
5618}
5619
5620/*
5621 * Return ind_maxparen corrected for the difference in line number between the
5622 * cursor position and "startpos". This makes sure that searching for a
5623 * matching paren above the cursor line doesn't find a match because of
5624 * looking a few lines further.
5625 */
5626 static int
5627corr_ind_maxparen(ind_maxparen, startpos)
5628 int ind_maxparen;
5629 pos_T *startpos;
5630{
5631 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
5632
5633 if (n > 0 && n < ind_maxparen / 2)
5634 return ind_maxparen - (int)n;
5635 return ind_maxparen;
5636}
5637
5638/*
5639 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
5640 * line "l".
5641 */
5642 static int
5643find_last_paren(l, start, end)
5644 char_u *l;
5645 int start, end;
5646{
5647 int i;
5648 int retval = FALSE;
5649 int open_count = 0;
5650
5651 curwin->w_cursor.col = 0; /* default is start of line */
5652
5653 for (i = 0; l[i]; i++)
5654 {
5655 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
5656 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
5657 if (l[i] == start)
5658 ++open_count;
5659 else if (l[i] == end)
5660 {
5661 if (open_count > 0)
5662 --open_count;
5663 else
5664 {
5665 curwin->w_cursor.col = i;
5666 retval = TRUE;
5667 }
5668 }
5669 }
5670 return retval;
5671}
5672
5673 int
5674get_c_indent()
5675{
5676 /*
5677 * spaces from a block's opening brace the prevailing indent for that
5678 * block should be
5679 */
5680 int ind_level = curbuf->b_p_sw;
5681
5682 /*
5683 * spaces from the edge of the line an open brace that's at the end of a
5684 * line is imagined to be.
5685 */
5686 int ind_open_imag = 0;
5687
5688 /*
5689 * spaces from the prevailing indent for a line that is not precededof by
5690 * an opening brace.
5691 */
5692 int ind_no_brace = 0;
5693
5694 /*
5695 * column where the first { of a function should be located }
5696 */
5697 int ind_first_open = 0;
5698
5699 /*
5700 * spaces from the prevailing indent a leftmost open brace should be
5701 * located
5702 */
5703 int ind_open_extra = 0;
5704
5705 /*
5706 * spaces from the matching open brace (real location for one at the left
5707 * edge; imaginary location from one that ends a line) the matching close
5708 * brace should be located
5709 */
5710 int ind_close_extra = 0;
5711
5712 /*
5713 * spaces from the edge of the line an open brace sitting in the leftmost
5714 * column is imagined to be
5715 */
5716 int ind_open_left_imag = 0;
5717
5718 /*
5719 * spaces from the switch() indent a "case xx" label should be located
5720 */
5721 int ind_case = curbuf->b_p_sw;
5722
5723 /*
5724 * spaces from the "case xx:" code after a switch() should be located
5725 */
5726 int ind_case_code = curbuf->b_p_sw;
5727
5728 /*
5729 * lineup break at end of case in switch() with case label
5730 */
5731 int ind_case_break = 0;
5732
5733 /*
5734 * spaces from the class declaration indent a scope declaration label
5735 * should be located
5736 */
5737 int ind_scopedecl = curbuf->b_p_sw;
5738
5739 /*
5740 * spaces from the scope declaration label code should be located
5741 */
5742 int ind_scopedecl_code = curbuf->b_p_sw;
5743
5744 /*
5745 * amount K&R-style parameters should be indented
5746 */
5747 int ind_param = curbuf->b_p_sw;
5748
5749 /*
5750 * amount a function type spec should be indented
5751 */
5752 int ind_func_type = curbuf->b_p_sw;
5753
5754 /*
5755 * amount a cpp base class declaration or constructor initialization
5756 * should be indented
5757 */
5758 int ind_cpp_baseclass = curbuf->b_p_sw;
5759
5760 /*
5761 * additional spaces beyond the prevailing indent a continuation line
5762 * should be located
5763 */
5764 int ind_continuation = curbuf->b_p_sw;
5765
5766 /*
5767 * spaces from the indent of the line with an unclosed parentheses
5768 */
5769 int ind_unclosed = curbuf->b_p_sw * 2;
5770
5771 /*
5772 * spaces from the indent of the line with an unclosed parentheses, which
5773 * itself is also unclosed
5774 */
5775 int ind_unclosed2 = curbuf->b_p_sw;
5776
5777 /*
5778 * suppress ignoring spaces from the indent of a line starting with an
5779 * unclosed parentheses.
5780 */
5781 int ind_unclosed_noignore = 0;
5782
5783 /*
5784 * If the opening paren is the last nonwhite character on the line, and
5785 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer
5786 * context (for very long lines).
5787 */
5788 int ind_unclosed_wrapped = 0;
5789
5790 /*
5791 * suppress ignoring white space when lining up with the character after
5792 * an unclosed parentheses.
5793 */
5794 int ind_unclosed_whiteok = 0;
5795
5796 /*
5797 * indent a closing parentheses under the line start of the matching
5798 * opening parentheses.
5799 */
5800 int ind_matching_paren = 0;
5801
5802 /*
5803 * Extra indent for comments.
5804 */
5805 int ind_comment = 0;
5806
5807 /*
5808 * spaces from the comment opener when there is nothing after it.
5809 */
5810 int ind_in_comment = 3;
5811
5812 /*
5813 * boolean: if non-zero, use ind_in_comment even if there is something
5814 * after the comment opener.
5815 */
5816 int ind_in_comment2 = 0;
5817
5818 /*
5819 * max lines to search for an open paren
5820 */
5821 int ind_maxparen = 20;
5822
5823 /*
5824 * max lines to search for an open comment
5825 */
5826 int ind_maxcomment = 70;
5827
5828 /*
5829 * handle braces for java code
5830 */
5831 int ind_java = 0;
5832
5833 /*
5834 * handle blocked cases correctly
5835 */
5836 int ind_keep_case_label = 0;
5837
5838 pos_T cur_curpos;
5839 int amount;
5840 int scope_amount;
5841 int cur_amount;
5842 colnr_T col;
5843 char_u *theline;
5844 char_u *linecopy;
5845 pos_T *trypos;
5846 pos_T *tryposBrace = NULL;
5847 pos_T our_paren_pos;
5848 char_u *start;
5849 int start_brace;
5850#define BRACE_IN_COL0 1 /* '{' is in comumn 0 */
5851#define BRACE_AT_START 2 /* '{' is at start of line */
5852#define BRACE_AT_END 3 /* '{' is at end of line */
5853 linenr_T ourscope;
5854 char_u *l;
5855 char_u *look;
5856 char_u terminated;
5857 int lookfor;
5858#define LOOKFOR_INITIAL 0
5859#define LOOKFOR_IF 1
5860#define LOOKFOR_DO 2
5861#define LOOKFOR_CASE 3
5862#define LOOKFOR_ANY 4
5863#define LOOKFOR_TERM 5
5864#define LOOKFOR_UNTERM 6
5865#define LOOKFOR_SCOPEDECL 7
5866#define LOOKFOR_NOBREAK 8
5867#define LOOKFOR_CPP_BASECLASS 9
5868#define LOOKFOR_ENUM_OR_INIT 10
5869
5870 int whilelevel;
5871 linenr_T lnum;
5872 char_u *options;
5873 int fraction = 0; /* init for GCC */
5874 int divider;
5875 int n;
5876 int iscase;
5877 int lookfor_break;
5878 int cont_amount = 0; /* amount for continuation line */
5879
5880 for (options = curbuf->b_p_cino; *options; )
5881 {
5882 l = options++;
5883 if (*options == '-')
5884 ++options;
5885 n = getdigits(&options);
5886 divider = 0;
5887 if (*options == '.') /* ".5s" means a fraction */
5888 {
5889 fraction = atol((char *)++options);
5890 while (VIM_ISDIGIT(*options))
5891 {
5892 ++options;
5893 if (divider)
5894 divider *= 10;
5895 else
5896 divider = 10;
5897 }
5898 }
5899 if (*options == 's') /* "2s" means two times 'shiftwidth' */
5900 {
5901 if (n == 0 && fraction == 0)
5902 n = curbuf->b_p_sw; /* just "s" is one 'shiftwidth' */
5903 else
5904 {
5905 n *= curbuf->b_p_sw;
5906 if (divider)
5907 n += (curbuf->b_p_sw * fraction + divider / 2) / divider;
5908 }
5909 ++options;
5910 }
5911 if (l[1] == '-')
5912 n = -n;
5913 /* When adding an entry here, also update the default 'cinoptions' in
5914 * change.txt, and add explanation for it! */
5915 switch (*l)
5916 {
5917 case '>': ind_level = n; break;
5918 case 'e': ind_open_imag = n; break;
5919 case 'n': ind_no_brace = n; break;
5920 case 'f': ind_first_open = n; break;
5921 case '{': ind_open_extra = n; break;
5922 case '}': ind_close_extra = n; break;
5923 case '^': ind_open_left_imag = n; break;
5924 case ':': ind_case = n; break;
5925 case '=': ind_case_code = n; break;
5926 case 'b': ind_case_break = n; break;
5927 case 'p': ind_param = n; break;
5928 case 't': ind_func_type = n; break;
5929 case '/': ind_comment = n; break;
5930 case 'c': ind_in_comment = n; break;
5931 case 'C': ind_in_comment2 = n; break;
5932 case 'i': ind_cpp_baseclass = n; break;
5933 case '+': ind_continuation = n; break;
5934 case '(': ind_unclosed = n; break;
5935 case 'u': ind_unclosed2 = n; break;
5936 case 'U': ind_unclosed_noignore = n; break;
5937 case 'W': ind_unclosed_wrapped = n; break;
5938 case 'w': ind_unclosed_whiteok = n; break;
5939 case 'm': ind_matching_paren = n; break;
5940 case ')': ind_maxparen = n; break;
5941 case '*': ind_maxcomment = n; break;
5942 case 'g': ind_scopedecl = n; break;
5943 case 'h': ind_scopedecl_code = n; break;
5944 case 'j': ind_java = n; break;
5945 case 'l': ind_keep_case_label = n; break;
5946 }
5947 }
5948
5949 /* remember where the cursor was when we started */
5950 cur_curpos = curwin->w_cursor;
5951
5952 /* Get a copy of the current contents of the line.
5953 * This is required, because only the most recent line obtained with
5954 * ml_get is valid! */
5955 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
5956 if (linecopy == NULL)
5957 return 0;
5958
5959 /*
5960 * In insert mode and the cursor is on a ')' truncate the line at the
5961 * cursor position. We don't want to line up with the matching '(' when
5962 * inserting new stuff.
5963 * For unknown reasons the cursor might be past the end of the line, thus
5964 * check for that.
5965 */
5966 if ((State & INSERT)
5967 && curwin->w_cursor.col < STRLEN(linecopy)
5968 && linecopy[curwin->w_cursor.col] == ')')
5969 linecopy[curwin->w_cursor.col] = NUL;
5970
5971 theline = skipwhite(linecopy);
5972
5973 /* move the cursor to the start of the line */
5974
5975 curwin->w_cursor.col = 0;
5976
5977 /*
5978 * #defines and so on always go at the left when included in 'cinkeys'.
5979 */
5980 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
5981 {
5982 amount = 0;
5983 }
5984
5985 /*
5986 * Is it a non-case label? Then that goes at the left margin too.
5987 */
5988 else if (cin_islabel(ind_maxcomment)) /* XXX */
5989 {
5990 amount = 0;
5991 }
5992
5993 /*
5994 * If we're inside a "//" comment and there is a "//" comment in a
5995 * previous line, lineup with that one.
5996 */
5997 else if (cin_islinecomment(theline)
5998 && (trypos = find_line_comment()) != NULL) /* XXX */
5999 {
6000 /* find how indented the line beginning the comment is */
6001 getvcol(curwin, trypos, &col, NULL, NULL);
6002 amount = col;
6003 }
6004
6005 /*
6006 * If we're inside a comment and not looking at the start of the
6007 * comment, try using the 'comments' option.
6008 */
6009 else if (!cin_iscomment(theline)
6010 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
6011 {
6012 int lead_start_len = 2;
6013 int lead_middle_len = 1;
6014 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
6015 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
6016 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6017 char_u *p;
6018 int start_align = 0;
6019 int start_off = 0;
6020 int done = FALSE;
6021
6022 /* find how indented the line beginning the comment is */
6023 getvcol(curwin, trypos, &col, NULL, NULL);
6024 amount = col;
6025
6026 p = curbuf->b_p_com;
6027 while (*p != NUL)
6028 {
6029 int align = 0;
6030 int off = 0;
6031 int what = 0;
6032
6033 while (*p != NUL && *p != ':')
6034 {
6035 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
6036 what = *p++;
6037 else if (*p == COM_LEFT || *p == COM_RIGHT)
6038 align = *p++;
6039 else if (VIM_ISDIGIT(*p) || *p == '-')
6040 off = getdigits(&p);
6041 else
6042 ++p;
6043 }
6044
6045 if (*p == ':')
6046 ++p;
6047 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6048 if (what == COM_START)
6049 {
6050 STRCPY(lead_start, lead_end);
6051 lead_start_len = (int)STRLEN(lead_start);
6052 start_off = off;
6053 start_align = align;
6054 }
6055 else if (what == COM_MIDDLE)
6056 {
6057 STRCPY(lead_middle, lead_end);
6058 lead_middle_len = (int)STRLEN(lead_middle);
6059 }
6060 else if (what == COM_END)
6061 {
6062 /* If our line starts with the middle comment string, line it
6063 * up with the comment opener per the 'comments' option. */
6064 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
6065 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
6066 {
6067 done = TRUE;
6068 if (curwin->w_cursor.lnum > 1)
6069 {
6070 /* If the start comment string matches in the previous
6071 * line, use the indent of that line pluss offset. If
6072 * the middle comment string matches in the previous
6073 * line, use the indent of that line. XXX */
6074 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
6075 if (STRNCMP(look, lead_start, lead_start_len) == 0)
6076 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
6077 else if (STRNCMP(look, lead_middle,
6078 lead_middle_len) == 0)
6079 {
6080 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
6081 break;
6082 }
6083 /* If the start comment string doesn't match with the
6084 * start of the comment, skip this entry. XXX */
6085 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
6086 lead_start, lead_start_len) != 0)
6087 continue;
6088 }
6089 if (start_off != 0)
6090 amount += start_off;
6091 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006092 amount += vim_strsize(lead_start)
6093 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 break;
6095 }
6096
6097 /* If our line starts with the end comment string, line it up
6098 * with the middle comment */
6099 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
6100 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
6101 {
6102 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
6103 /* XXX */
6104 if (off != 0)
6105 amount += off;
6106 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006107 amount += vim_strsize(lead_start)
6108 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 done = TRUE;
6110 break;
6111 }
6112 }
6113 }
6114
6115 /* If our line starts with an asterisk, line up with the
6116 * asterisk in the comment opener; otherwise, line up
6117 * with the first character of the comment text.
6118 */
6119 if (done)
6120 ;
6121 else if (theline[0] == '*')
6122 amount += 1;
6123 else
6124 {
6125 /*
6126 * If we are more than one line away from the comment opener, take
6127 * the indent of the previous non-empty line. If 'cino' has "CO"
6128 * and we are just below the comment opener and there are any
6129 * white characters after it line up with the text after it;
6130 * otherwise, add the amount specified by "c" in 'cino'
6131 */
6132 amount = -1;
6133 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
6134 {
6135 if (linewhite(lnum)) /* skip blank lines */
6136 continue;
6137 amount = get_indent_lnum(lnum); /* XXX */
6138 break;
6139 }
6140 if (amount == -1) /* use the comment opener */
6141 {
6142 if (!ind_in_comment2)
6143 {
6144 start = ml_get(trypos->lnum);
6145 look = start + trypos->col + 2; /* skip / and * */
6146 if (*look != NUL) /* if something after it */
6147 trypos->col = (colnr_T)(skipwhite(look) - start);
6148 }
6149 getvcol(curwin, trypos, &col, NULL, NULL);
6150 amount = col;
6151 if (ind_in_comment2 || *look == NUL)
6152 amount += ind_in_comment;
6153 }
6154 }
6155 }
6156
6157 /*
6158 * Are we inside parentheses or braces?
6159 */ /* XXX */
6160 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL
6161 && ind_java == 0)
6162 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL
6163 || trypos != NULL)
6164 {
6165 if (trypos != NULL && tryposBrace != NULL)
6166 {
6167 /* Both an unmatched '(' and '{' is found. Use the one which is
6168 * closer to the current cursor position, set the other to NULL. */
6169 if (trypos->lnum != tryposBrace->lnum
6170 ? trypos->lnum < tryposBrace->lnum
6171 : trypos->col < tryposBrace->col)
6172 trypos = NULL;
6173 else
6174 tryposBrace = NULL;
6175 }
6176
6177 if (trypos != NULL)
6178 {
6179 /*
6180 * If the matching paren is more than one line away, use the indent of
6181 * a previous non-empty line that matches the same paren.
6182 */
6183 amount = -1;
6184 cur_amount = MAXCOL;
6185 our_paren_pos = *trypos;
6186 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
6187 {
6188 l = skipwhite(ml_get(lnum));
6189 if (cin_nocode(l)) /* skip comment lines */
6190 continue;
6191 if (cin_ispreproc_cont(&l, &lnum)) /* ignore #defines, #if, etc. */
6192 continue;
6193 curwin->w_cursor.lnum = lnum;
6194
6195 /* Skip a comment. XXX */
6196 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
6197 {
6198 lnum = trypos->lnum + 1;
6199 continue;
6200 }
6201
6202 /* XXX */
6203 if ((trypos = find_match_paren(
6204 corr_ind_maxparen(ind_maxparen, &cur_curpos),
6205 ind_maxcomment)) != NULL
6206 && trypos->lnum == our_paren_pos.lnum
6207 && trypos->col == our_paren_pos.col)
6208 {
6209 amount = get_indent_lnum(lnum); /* XXX */
6210
6211 if (theline[0] == ')')
6212 {
6213 if (our_paren_pos.lnum != lnum && cur_amount > amount)
6214 cur_amount = amount;
6215 amount = -1;
6216 }
6217 break;
6218 }
6219 }
6220
6221 /*
6222 * Line up with line where the matching paren is. XXX
6223 * If the line starts with a '(' or the indent for unclosed
6224 * parentheses is zero, line up with the unclosed parentheses.
6225 */
6226 if (amount == -1)
6227 {
6228 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment);
6229 if (theline[0] == ')' || ind_unclosed == 0
6230 || (!ind_unclosed_noignore && *skipwhite(look) == '('))
6231 {
6232 /*
6233 * If we're looking at a close paren, line up right there;
6234 * otherwise, line up with the next (non-white) character.
6235 * When ind_unclosed_wrapped is set and the matching paren is
6236 * the last nonwhite character of the line, use either the
6237 * indent of the current line or the indentation of the next
6238 * outer paren and add ind_unclosed_wrapped (for very long
6239 * lines).
6240 */
6241 if (theline[0] != ')')
6242 {
6243 cur_amount = MAXCOL;
6244 l = ml_get(our_paren_pos.lnum);
6245 if (ind_unclosed_wrapped
6246 && cin_ends_in(l, (char_u *)"(", NULL))
6247 {
6248 /* look for opening unmatched paren, indent one level
6249 * for each additional level */
6250 n = 1;
6251 for (col = 0; col < our_paren_pos.col; ++col)
6252 {
6253 switch (l[col])
6254 {
6255 case '(':
6256 case '{': ++n;
6257 break;
6258
6259 case ')':
6260 case '}': if (n > 1)
6261 --n;
6262 break;
6263 }
6264 }
6265
6266 our_paren_pos.col = 0;
6267 amount += n * ind_unclosed_wrapped;
6268 }
6269 else if (ind_unclosed_whiteok)
6270 our_paren_pos.col++;
6271 else
6272 {
6273 col = our_paren_pos.col + 1;
6274 while (vim_iswhite(l[col]))
6275 col++;
6276 if (l[col] != NUL) /* In case of trailing space */
6277 our_paren_pos.col = col;
6278 else
6279 our_paren_pos.col++;
6280 }
6281 }
6282
6283 /*
6284 * Find how indented the paren is, or the character after it
6285 * if we did the above "if".
6286 */
6287 if (our_paren_pos.col > 0)
6288 {
6289 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
6290 if (cur_amount > (int)col)
6291 cur_amount = col;
6292 }
6293 }
6294
6295 if (theline[0] == ')' && ind_matching_paren)
6296 {
6297 /* Line up with the start of the matching paren line. */
6298 }
6299 else if (ind_unclosed == 0 || (!ind_unclosed_noignore
6300 && *skipwhite(look) == '('))
6301 {
6302 if (cur_amount != MAXCOL)
6303 amount = cur_amount;
6304 }
6305 else
6306 {
6307 /* add ind_unclosed2 for each '(' before our matching one */
6308 col = our_paren_pos.col;
6309 while (our_paren_pos.col > 0)
6310 {
6311 --our_paren_pos.col;
6312 switch (*ml_get_pos(&our_paren_pos))
6313 {
6314 case '(': amount += ind_unclosed2;
6315 col = our_paren_pos.col;
6316 break;
6317 case ')': amount -= ind_unclosed2;
6318 col = MAXCOL;
6319 break;
6320 }
6321 }
6322
6323 /* Use ind_unclosed once, when the first '(' is not inside
6324 * braces */
6325 if (col == MAXCOL)
6326 amount += ind_unclosed;
6327 else
6328 {
6329 curwin->w_cursor.lnum = our_paren_pos.lnum;
6330 curwin->w_cursor.col = col;
6331 if ((trypos = find_match_paren(ind_maxparen,
6332 ind_maxcomment)) != NULL)
6333 amount += ind_unclosed2;
6334 else
6335 amount += ind_unclosed;
6336 }
6337 /*
6338 * For a line starting with ')' use the minimum of the two
6339 * positions, to avoid giving it more indent than the previous
6340 * lines:
6341 * func_long_name( if (x
6342 * arg && yy
6343 * ) ^ not here ) ^ not here
6344 */
6345 if (cur_amount < amount)
6346 amount = cur_amount;
6347 }
6348 }
6349
6350 /* add extra indent for a comment */
6351 if (cin_iscomment(theline))
6352 amount += ind_comment;
6353 }
6354
6355 /*
6356 * Are we at least inside braces, then?
6357 */
6358 else
6359 {
6360 trypos = tryposBrace;
6361
6362 ourscope = trypos->lnum;
6363 start = ml_get(ourscope);
6364
6365 /*
6366 * Now figure out how indented the line is in general.
6367 * If the brace was at the start of the line, we use that;
6368 * otherwise, check out the indentation of the line as
6369 * a whole and then add the "imaginary indent" to that.
6370 */
6371 look = skipwhite(start);
6372 if (*look == '{')
6373 {
6374 getvcol(curwin, trypos, &col, NULL, NULL);
6375 amount = col;
6376 if (*start == '{')
6377 start_brace = BRACE_IN_COL0;
6378 else
6379 start_brace = BRACE_AT_START;
6380 }
6381 else
6382 {
6383 /*
6384 * that opening brace might have been on a continuation
6385 * line. if so, find the start of the line.
6386 */
6387 curwin->w_cursor.lnum = ourscope;
6388
6389 /*
6390 * position the cursor over the rightmost paren, so that
6391 * matching it will take us back to the start of the line.
6392 */
6393 lnum = ourscope;
6394 if (find_last_paren(start, '(', ')')
6395 && (trypos = find_match_paren(ind_maxparen,
6396 ind_maxcomment)) != NULL)
6397 lnum = trypos->lnum;
6398
6399 /*
6400 * It could have been something like
6401 * case 1: if (asdf &&
6402 * ldfd) {
6403 * }
6404 */
6405 if (ind_keep_case_label && cin_iscase(skipwhite(ml_get_curline())))
6406 amount = get_indent();
6407 else
6408 amount = skip_label(lnum, &l, ind_maxcomment);
6409
6410 start_brace = BRACE_AT_END;
6411 }
6412
6413 /*
6414 * if we're looking at a closing brace, that's where
6415 * we want to be. otherwise, add the amount of room
6416 * that an indent is supposed to be.
6417 */
6418 if (theline[0] == '}')
6419 {
6420 /*
6421 * they may want closing braces to line up with something
6422 * other than the open brace. indulge them, if so.
6423 */
6424 amount += ind_close_extra;
6425 }
6426 else
6427 {
6428 /*
6429 * If we're looking at an "else", try to find an "if"
6430 * to match it with.
6431 * If we're looking at a "while", try to find a "do"
6432 * to match it with.
6433 */
6434 lookfor = LOOKFOR_INITIAL;
6435 if (cin_iselse(theline))
6436 lookfor = LOOKFOR_IF;
6437 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen))
6438 /* XXX */
6439 lookfor = LOOKFOR_DO;
6440 if (lookfor != LOOKFOR_INITIAL)
6441 {
6442 curwin->w_cursor.lnum = cur_curpos.lnum;
6443 if (find_match(lookfor, ourscope, ind_maxparen,
6444 ind_maxcomment) == OK)
6445 {
6446 amount = get_indent(); /* XXX */
6447 goto theend;
6448 }
6449 }
6450
6451 /*
6452 * We get here if we are not on an "while-of-do" or "else" (or
6453 * failed to find a matching "if").
6454 * Search backwards for something to line up with.
6455 * First set amount for when we don't find anything.
6456 */
6457
6458 /*
6459 * if the '{' is _really_ at the left margin, use the imaginary
6460 * location of a left-margin brace. Otherwise, correct the
6461 * location for ind_open_extra.
6462 */
6463
6464 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
6465 {
6466 amount = ind_open_left_imag;
6467 }
6468 else
6469 {
6470 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
6471 amount += ind_open_imag;
6472 else
6473 {
6474 /* Compensate for adding ind_open_extra later. */
6475 amount -= ind_open_extra;
6476 if (amount < 0)
6477 amount = 0;
6478 }
6479 }
6480
6481 lookfor_break = FALSE;
6482
6483 if (cin_iscase(theline)) /* it's a switch() label */
6484 {
6485 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
6486 amount += ind_case;
6487 }
6488 else if (cin_isscopedecl(theline)) /* private:, ... */
6489 {
6490 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
6491 amount += ind_scopedecl;
6492 }
6493 else
6494 {
6495 if (ind_case_break && cin_isbreak(theline)) /* break; ... */
6496 lookfor_break = TRUE;
6497
6498 lookfor = LOOKFOR_INITIAL;
6499 amount += ind_level; /* ind_level from start of block */
6500 }
6501 scope_amount = amount;
6502 whilelevel = 0;
6503
6504 /*
6505 * Search backwards. If we find something we recognize, line up
6506 * with that.
6507 *
6508 * if we're looking at an open brace, indent
6509 * the usual amount relative to the conditional
6510 * that opens the block.
6511 */
6512 curwin->w_cursor = cur_curpos;
6513 for (;;)
6514 {
6515 curwin->w_cursor.lnum--;
6516 curwin->w_cursor.col = 0;
6517
6518 /*
6519 * If we went all the way back to the start of our scope, line
6520 * up with it.
6521 */
6522 if (curwin->w_cursor.lnum <= ourscope)
6523 {
6524 /* we reached end of scope:
6525 * if looking for a enum or structure initialization
6526 * go further back:
6527 * if it is an initializer (enum xxx or xxx =), then
6528 * don't add ind_continuation, otherwise it is a variable
6529 * declaration:
6530 * int x,
6531 * here; <-- add ind_continuation
6532 */
6533 if (lookfor == LOOKFOR_ENUM_OR_INIT)
6534 {
6535 if (curwin->w_cursor.lnum == 0
6536 || curwin->w_cursor.lnum
6537 < ourscope - ind_maxparen)
6538 {
6539 /* nothing found (abuse ind_maxparen as limit)
6540 * assume terminated line (i.e. a variable
6541 * initialization) */
6542 if (cont_amount > 0)
6543 amount = cont_amount;
6544 else
6545 amount += ind_continuation;
6546 break;
6547 }
6548
6549 l = ml_get_curline();
6550
6551 /*
6552 * If we're in a comment now, skip to the start of the
6553 * comment.
6554 */
6555 trypos = find_start_comment(ind_maxcomment);
6556 if (trypos != NULL)
6557 {
6558 curwin->w_cursor.lnum = trypos->lnum + 1;
6559 continue;
6560 }
6561
6562 /*
6563 * Skip preprocessor directives and blank lines.
6564 */
6565 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
6566 continue;
6567
6568 if (cin_nocode(l))
6569 continue;
6570
6571 terminated = cin_isterminated(l, FALSE, TRUE);
6572
6573 /*
6574 * If we are at top level and the line looks like a
6575 * function declaration, we are done
6576 * (it's a variable declaration).
6577 */
6578 if (start_brace != BRACE_IN_COL0
6579 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum))
6580 {
6581 /* if the line is terminated with another ','
6582 * it is a continued variable initialization.
6583 * don't add extra indent.
6584 * TODO: does not work, if a function
6585 * declaration is split over multiple lines:
6586 * cin_isfuncdecl returns FALSE then.
6587 */
6588 if (terminated == ',')
6589 break;
6590
6591 /* if it es a enum declaration or an assignment,
6592 * we are done.
6593 */
6594 if (terminated != ';' && cin_isinit())
6595 break;
6596
6597 /* nothing useful found */
6598 if (terminated == 0 || terminated == '{')
6599 continue;
6600 }
6601
6602 if (terminated != ';')
6603 {
6604 /* Skip parens and braces. Position the cursor
6605 * over the rightmost paren, so that matching it
6606 * will take us back to the start of the line.
6607 */ /* XXX */
6608 trypos = NULL;
6609 if (find_last_paren(l, '(', ')'))
6610 trypos = find_match_paren(ind_maxparen,
6611 ind_maxcomment);
6612
6613 if (trypos == NULL && find_last_paren(l, '{', '}'))
6614 trypos = find_start_brace(ind_maxcomment);
6615
6616 if (trypos != NULL)
6617 {
6618 curwin->w_cursor.lnum = trypos->lnum + 1;
6619 continue;
6620 }
6621 }
6622
6623 /* it's a variable declaration, add indentation
6624 * like in
6625 * int a,
6626 * b;
6627 */
6628 if (cont_amount > 0)
6629 amount = cont_amount;
6630 else
6631 amount += ind_continuation;
6632 }
6633 else if (lookfor == LOOKFOR_UNTERM)
6634 {
6635 if (cont_amount > 0)
6636 amount = cont_amount;
6637 else
6638 amount += ind_continuation;
6639 }
6640 else if (lookfor != LOOKFOR_TERM
6641 && lookfor != LOOKFOR_CPP_BASECLASS)
6642 {
6643 amount = scope_amount;
6644 if (theline[0] == '{')
6645 amount += ind_open_extra;
6646 }
6647 break;
6648 }
6649
6650 /*
6651 * If we're in a comment now, skip to the start of the comment.
6652 */ /* XXX */
6653 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
6654 {
6655 curwin->w_cursor.lnum = trypos->lnum + 1;
6656 continue;
6657 }
6658
6659 l = ml_get_curline();
6660
6661 /*
6662 * If this is a switch() label, may line up relative to that.
6663 * if this is a C++ scope declaration, do the same.
6664 */
6665 iscase = cin_iscase(l);
6666 if (iscase || cin_isscopedecl(l))
6667 {
6668 /* we are only looking for cpp base class
6669 * declaration/initialization any longer */
6670 if (lookfor == LOOKFOR_CPP_BASECLASS)
6671 break;
6672
6673 /* When looking for a "do" we are not interested in
6674 * labels. */
6675 if (whilelevel > 0)
6676 continue;
6677
6678 /*
6679 * case xx:
6680 * c = 99 + <- this indent plus continuation
6681 *-> here;
6682 */
6683 if (lookfor == LOOKFOR_UNTERM
6684 || lookfor == LOOKFOR_ENUM_OR_INIT)
6685 {
6686 if (cont_amount > 0)
6687 amount = cont_amount;
6688 else
6689 amount += ind_continuation;
6690 break;
6691 }
6692
6693 /*
6694 * case xx: <- line up with this case
6695 * x = 333;
6696 * case yy:
6697 */
6698 if ( (iscase && lookfor == LOOKFOR_CASE)
6699 || (iscase && lookfor_break)
6700 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
6701 {
6702 /*
6703 * Check that this case label is not for another
6704 * switch()
6705 */ /* XXX */
6706 if ((trypos = find_start_brace(ind_maxcomment)) ==
6707 NULL || trypos->lnum == ourscope)
6708 {
6709 amount = get_indent(); /* XXX */
6710 break;
6711 }
6712 continue;
6713 }
6714
6715 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
6716
6717 /*
6718 * case xx: if (cond) <- line up with this if
6719 * y = y + 1;
6720 * -> s = 99;
6721 *
6722 * case xx:
6723 * if (cond) <- line up with this line
6724 * y = y + 1;
6725 * -> s = 99;
6726 */
6727 if (lookfor == LOOKFOR_TERM)
6728 {
6729 if (n)
6730 amount = n;
6731
6732 if (!lookfor_break)
6733 break;
6734 }
6735
6736 /*
6737 * case xx: x = x + 1; <- line up with this x
6738 * -> y = y + 1;
6739 *
6740 * case xx: if (cond) <- line up with this if
6741 * -> y = y + 1;
6742 */
6743 if (n)
6744 {
6745 amount = n;
6746 l = after_label(ml_get_curline());
6747 if (l != NULL && cin_is_cinword(l))
6748 amount += ind_level + ind_no_brace;
6749 break;
6750 }
6751
6752 /*
6753 * Try to get the indent of a statement before the switch
6754 * label. If nothing is found, line up relative to the
6755 * switch label.
6756 * break; <- may line up with this line
6757 * case xx:
6758 * -> y = 1;
6759 */
6760 scope_amount = get_indent() + (iscase /* XXX */
6761 ? ind_case_code : ind_scopedecl_code);
6762 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
6763 continue;
6764 }
6765
6766 /*
6767 * Looking for a switch() label or C++ scope declaration,
6768 * ignore other lines, skip {}-blocks.
6769 */
6770 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
6771 {
6772 if (find_last_paren(l, '{', '}') && (trypos =
6773 find_start_brace(ind_maxcomment)) != NULL)
6774 curwin->w_cursor.lnum = trypos->lnum + 1;
6775 continue;
6776 }
6777
6778 /*
6779 * Ignore jump labels with nothing after them.
6780 */
6781 if (cin_islabel(ind_maxcomment))
6782 {
6783 l = after_label(ml_get_curline());
6784 if (l == NULL || cin_nocode(l))
6785 continue;
6786 }
6787
6788 /*
6789 * Ignore #defines, #if, etc.
6790 * Ignore comment and empty lines.
6791 * (need to get the line again, cin_islabel() may have
6792 * unlocked it)
6793 */
6794 l = ml_get_curline();
6795 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
6796 || cin_nocode(l))
6797 continue;
6798
6799 /*
6800 * Are we at the start of a cpp base class declaration or
6801 * constructor initialization?
6802 */ /* XXX */
6803 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass
6804 && cin_is_cpp_baseclass(l, &col))
6805 {
6806 if (lookfor == LOOKFOR_UNTERM)
6807 {
6808 if (cont_amount > 0)
6809 amount = cont_amount;
6810 else
6811 amount += ind_continuation;
6812 }
6813 else if (col == 0 || theline[0] == '{')
6814 {
6815 amount = get_indent();
6816 if (find_last_paren(l, '(', ')')
6817 && (trypos = find_match_paren(ind_maxparen,
6818 ind_maxcomment)) != NULL)
6819 amount = get_indent_lnum(trypos->lnum); /* XXX */
6820 if (theline[0] != '{')
6821 amount += ind_cpp_baseclass;
6822 }
6823 else
6824 {
6825 curwin->w_cursor.col = col;
6826 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
6827 amount = (int)col;
6828 }
6829 break;
6830 }
6831 else if (lookfor == LOOKFOR_CPP_BASECLASS)
6832 {
6833 /* only look, whether there is a cpp base class
6834 * declaration or initialization before the opening brace. */
6835 if (cin_isterminated(l, TRUE, FALSE))
6836 break;
6837 else
6838 continue;
6839 }
6840
6841 /*
6842 * What happens next depends on the line being terminated.
6843 * If terminated with a ',' only consider it terminating if
6844 * there is anoter unterminated statement behind, eg:
6845 * 123,
6846 * sizeof
6847 * here
6848 * Otherwise check whether it is a enumeration or structure
6849 * initialisation (not indented) or a variable declaration
6850 * (indented).
6851 */
6852 terminated = cin_isterminated(l, FALSE, TRUE);
6853
6854 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
6855 && terminated == ','))
6856 {
6857 /*
6858 * if we're in the middle of a paren thing,
6859 * go back to the line that starts it so
6860 * we can get the right prevailing indent
6861 * if ( foo &&
6862 * bar )
6863 */
6864 /*
6865 * position the cursor over the rightmost paren, so that
6866 * matching it will take us back to the start of the line.
6867 */
6868 (void)find_last_paren(l, '(', ')');
6869 trypos = find_match_paren(
6870 corr_ind_maxparen(ind_maxparen, &cur_curpos),
6871 ind_maxcomment);
6872
6873 /*
6874 * If we are looking for ',', we also look for matching
6875 * braces.
6876 */
6877 if (trypos == NULL && find_last_paren(l, '{', '}'))
6878 trypos = find_start_brace(ind_maxcomment);
6879
6880 if (trypos != NULL)
6881 {
6882 /*
6883 * Check if we are on a case label now. This is
6884 * handled above.
6885 * case xx: if ( asdf &&
6886 * asdf)
6887 */
6888 curwin->w_cursor.lnum = trypos->lnum;
6889 l = ml_get_curline();
6890 if (cin_iscase(l) || cin_isscopedecl(l))
6891 {
6892 ++curwin->w_cursor.lnum;
6893 continue;
6894 }
6895 }
6896
6897 /*
6898 * Skip over continuation lines to find the one to get the
6899 * indent from
6900 * char *usethis = "bla\
6901 * bla",
6902 * here;
6903 */
6904 if (terminated == ',')
6905 {
6906 while (curwin->w_cursor.lnum > 1)
6907 {
6908 l = ml_get(curwin->w_cursor.lnum - 1);
6909 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
6910 break;
6911 --curwin->w_cursor.lnum;
6912 }
6913 }
6914
6915 /*
6916 * Get indent and pointer to text for current line,
6917 * ignoring any jump label. XXX
6918 */
6919 cur_amount = skip_label(curwin->w_cursor.lnum,
6920 &l, ind_maxcomment);
6921
6922 /*
6923 * If this is just above the line we are indenting, and it
6924 * starts with a '{', line it up with this line.
6925 * while (not)
6926 * -> {
6927 * }
6928 */
6929 if (terminated != ',' && lookfor != LOOKFOR_TERM
6930 && theline[0] == '{')
6931 {
6932 amount = cur_amount;
6933 /*
6934 * Only add ind_open_extra when the current line
6935 * doesn't start with a '{', which must have a match
6936 * in the same line (scope is the same). Probably:
6937 * { 1, 2 },
6938 * -> { 3, 4 }
6939 */
6940 if (*skipwhite(l) != '{')
6941 amount += ind_open_extra;
6942
6943 if (ind_cpp_baseclass)
6944 {
6945 /* have to look back, whether it is a cpp base
6946 * class declaration or initialization */
6947 lookfor = LOOKFOR_CPP_BASECLASS;
6948 continue;
6949 }
6950 break;
6951 }
6952
6953 /*
6954 * Check if we are after an "if", "while", etc.
6955 * Also allow " } else".
6956 */
6957 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
6958 {
6959 /*
6960 * Found an unterminated line after an if (), line up
6961 * with the last one.
6962 * if (cond)
6963 * 100 +
6964 * -> here;
6965 */
6966 if (lookfor == LOOKFOR_UNTERM
6967 || lookfor == LOOKFOR_ENUM_OR_INIT)
6968 {
6969 if (cont_amount > 0)
6970 amount = cont_amount;
6971 else
6972 amount += ind_continuation;
6973 break;
6974 }
6975
6976 /*
6977 * If this is just above the line we are indenting, we
6978 * are finished.
6979 * while (not)
6980 * -> here;
6981 * Otherwise this indent can be used when the line
6982 * before this is terminated.
6983 * yyy;
6984 * if (stat)
6985 * while (not)
6986 * xxx;
6987 * -> here;
6988 */
6989 amount = cur_amount;
6990 if (theline[0] == '{')
6991 amount += ind_open_extra;
6992 if (lookfor != LOOKFOR_TERM)
6993 {
6994 amount += ind_level + ind_no_brace;
6995 break;
6996 }
6997
6998 /*
6999 * Special trick: when expecting the while () after a
7000 * do, line up with the while()
7001 * do
7002 * x = 1;
7003 * -> here
7004 */
7005 l = skipwhite(ml_get_curline());
7006 if (cin_isdo(l))
7007 {
7008 if (whilelevel == 0)
7009 break;
7010 --whilelevel;
7011 }
7012
7013 /*
7014 * When searching for a terminated line, don't use the
7015 * one between the "if" and the "else".
7016 * Need to use the scope of this "else". XXX
7017 * If whilelevel != 0 continue looking for a "do {".
7018 */
7019 if (cin_iselse(l)
7020 && whilelevel == 0
7021 && ((trypos = find_start_brace(ind_maxcomment))
7022 == NULL
7023 || find_match(LOOKFOR_IF, trypos->lnum,
7024 ind_maxparen, ind_maxcomment) == FAIL))
7025 break;
7026 }
7027
7028 /*
7029 * If we're below an unterminated line that is not an
7030 * "if" or something, we may line up with this line or
7031 * add someting for a continuation line, depending on
7032 * the line before this one.
7033 */
7034 else
7035 {
7036 /*
7037 * Found two unterminated lines on a row, line up with
7038 * the last one.
7039 * c = 99 +
7040 * 100 +
7041 * -> here;
7042 */
7043 if (lookfor == LOOKFOR_UNTERM)
7044 {
7045 /* When line ends in a comma add extra indent */
7046 if (terminated == ',')
7047 amount += ind_continuation;
7048 break;
7049 }
7050
7051 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7052 {
7053 /* Found two lines ending in ',', lineup with the
7054 * lowest one, but check for cpp base class
7055 * declaration/initialization, if it is an
7056 * opening brace or we are looking just for
7057 * enumerations/initializations. */
7058 if (terminated == ',')
7059 {
7060 if (ind_cpp_baseclass == 0)
7061 break;
7062
7063 lookfor = LOOKFOR_CPP_BASECLASS;
7064 continue;
7065 }
7066
7067 /* Ignore unterminated lines in between, but
7068 * reduce indent. */
7069 if (amount > cur_amount)
7070 amount = cur_amount;
7071 }
7072 else
7073 {
7074 /*
7075 * Found first unterminated line on a row, may
7076 * line up with this line, remember its indent
7077 * 100 +
7078 * -> here;
7079 */
7080 amount = cur_amount;
7081
7082 /*
7083 * If previous line ends in ',', check whether we
7084 * are in an initialization or enum
7085 * struct xxx =
7086 * {
7087 * sizeof a,
7088 * 124 };
7089 * or a normal possible continuation line.
7090 * but only, of no other statement has been found
7091 * yet.
7092 */
7093 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
7094 {
7095 lookfor = LOOKFOR_ENUM_OR_INIT;
7096 cont_amount = cin_first_id_amount();
7097 }
7098 else
7099 {
7100 if (lookfor == LOOKFOR_INITIAL
7101 && *l != NUL
7102 && l[STRLEN(l) - 1] == '\\')
7103 /* XXX */
7104 cont_amount = cin_get_equal_amount(
7105 curwin->w_cursor.lnum);
7106 if (lookfor != LOOKFOR_TERM)
7107 lookfor = LOOKFOR_UNTERM;
7108 }
7109 }
7110 }
7111 }
7112
7113 /*
7114 * Check if we are after a while (cond);
7115 * If so: Ignore until the matching "do".
7116 */
7117 /* XXX */
7118 else if (cin_iswhileofdo(l,
7119 curwin->w_cursor.lnum, ind_maxparen))
7120 {
7121 /*
7122 * Found an unterminated line after a while ();, line up
7123 * with the last one.
7124 * while (cond);
7125 * 100 + <- line up with this one
7126 * -> here;
7127 */
7128 if (lookfor == LOOKFOR_UNTERM
7129 || lookfor == LOOKFOR_ENUM_OR_INIT)
7130 {
7131 if (cont_amount > 0)
7132 amount = cont_amount;
7133 else
7134 amount += ind_continuation;
7135 break;
7136 }
7137
7138 if (whilelevel == 0)
7139 {
7140 lookfor = LOOKFOR_TERM;
7141 amount = get_indent(); /* XXX */
7142 if (theline[0] == '{')
7143 amount += ind_open_extra;
7144 }
7145 ++whilelevel;
7146 }
7147
7148 /*
7149 * We are after a "normal" statement.
7150 * If we had another statement we can stop now and use the
7151 * indent of that other statement.
7152 * Otherwise the indent of the current statement may be used,
7153 * search backwards for the next "normal" statement.
7154 */
7155 else
7156 {
7157 /*
7158 * Skip single break line, if before a switch label. It
7159 * may be lined up with the case label.
7160 */
7161 if (lookfor == LOOKFOR_NOBREAK
7162 && cin_isbreak(skipwhite(ml_get_curline())))
7163 {
7164 lookfor = LOOKFOR_ANY;
7165 continue;
7166 }
7167
7168 /*
7169 * Handle "do {" line.
7170 */
7171 if (whilelevel > 0)
7172 {
7173 l = cin_skipcomment(ml_get_curline());
7174 if (cin_isdo(l))
7175 {
7176 amount = get_indent(); /* XXX */
7177 --whilelevel;
7178 continue;
7179 }
7180 }
7181
7182 /*
7183 * Found a terminated line above an unterminated line. Add
7184 * the amount for a continuation line.
7185 * x = 1;
7186 * y = foo +
7187 * -> here;
7188 * or
7189 * int x = 1;
7190 * int foo,
7191 * -> here;
7192 */
7193 if (lookfor == LOOKFOR_UNTERM
7194 || lookfor == LOOKFOR_ENUM_OR_INIT)
7195 {
7196 if (cont_amount > 0)
7197 amount = cont_amount;
7198 else
7199 amount += ind_continuation;
7200 break;
7201 }
7202
7203 /*
7204 * Found a terminated line above a terminated line or "if"
7205 * etc. line. Use the amount of the line below us.
7206 * x = 1; x = 1;
7207 * if (asdf) y = 2;
7208 * while (asdf) ->here;
7209 * here;
7210 * ->foo;
7211 */
7212 if (lookfor == LOOKFOR_TERM)
7213 {
7214 if (!lookfor_break && whilelevel == 0)
7215 break;
7216 }
7217
7218 /*
7219 * First line above the one we're indenting is terminated.
7220 * To know what needs to be done look further backward for
7221 * a terminated line.
7222 */
7223 else
7224 {
7225 /*
7226 * position the cursor over the rightmost paren, so
7227 * that matching it will take us back to the start of
7228 * the line. Helps for:
7229 * func(asdr,
7230 * asdfasdf);
7231 * here;
7232 */
7233term_again:
7234 l = ml_get_curline();
7235 if (find_last_paren(l, '(', ')')
7236 && (trypos = find_match_paren(ind_maxparen,
7237 ind_maxcomment)) != NULL)
7238 {
7239 /*
7240 * Check if we are on a case label now. This is
7241 * handled above.
7242 * case xx: if ( asdf &&
7243 * asdf)
7244 */
7245 curwin->w_cursor.lnum = trypos->lnum;
7246 l = ml_get_curline();
7247 if (cin_iscase(l) || cin_isscopedecl(l))
7248 {
7249 ++curwin->w_cursor.lnum;
7250 continue;
7251 }
7252 }
7253
7254 /* When aligning with the case statement, don't align
7255 * with a statement after it.
7256 * case 1: { <-- don't use this { position
7257 * stat;
7258 * }
7259 * case 2:
7260 * stat;
7261 * }
7262 */
7263 iscase = (ind_keep_case_label && cin_iscase(l));
7264
7265 /*
7266 * Get indent and pointer to text for current line,
7267 * ignoring any jump label.
7268 */
7269 amount = skip_label(curwin->w_cursor.lnum,
7270 &l, ind_maxcomment);
7271
7272 if (theline[0] == '{')
7273 amount += ind_open_extra;
7274 /* See remark above: "Only add ind_open_extra.." */
7275 if (*skipwhite(l) == '{')
7276 amount -= ind_open_extra;
7277 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
7278
7279 /*
7280 * If we're at the end of a block, skip to the start of
7281 * that block.
7282 */
7283 curwin->w_cursor.col = 0;
7284 if (*cin_skipcomment(l) == '}'
7285 && (trypos = find_start_brace(ind_maxcomment))
7286 != NULL) /* XXX */
7287 {
7288 curwin->w_cursor.lnum = trypos->lnum;
7289 /* if not "else {" check for terminated again */
7290 /* but skip block for "} else {" */
7291 l = cin_skipcomment(ml_get_curline());
7292 if (*l == '}' || !cin_iselse(l))
7293 goto term_again;
7294 ++curwin->w_cursor.lnum;
7295 }
7296 }
7297 }
7298 }
7299 }
7300 }
7301
7302 /* add extra indent for a comment */
7303 if (cin_iscomment(theline))
7304 amount += ind_comment;
7305 }
7306
7307 /*
7308 * ok -- we're not inside any sort of structure at all!
7309 *
7310 * this means we're at the top level, and everything should
7311 * basically just match where the previous line is, except
7312 * for the lines immediately following a function declaration,
7313 * which are K&R-style parameters and need to be indented.
7314 */
7315 else
7316 {
7317 /*
7318 * if our line starts with an open brace, forget about any
7319 * prevailing indent and make sure it looks like the start
7320 * of a function
7321 */
7322
7323 if (theline[0] == '{')
7324 {
7325 amount = ind_first_open;
7326 }
7327
7328 /*
7329 * If the NEXT line is a function declaration, the current
7330 * line needs to be indented as a function type spec.
7331 * Don't do this if the current line looks like a comment
7332 * or if the current line is terminated, ie. ends in ';'.
7333 */
7334 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
7335 && !cin_nocode(theline)
7336 && !cin_ends_in(theline, (char_u *)":", NULL)
7337 && !cin_ends_in(theline, (char_u *)",", NULL)
7338 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1)
7339 && !cin_isterminated(theline, FALSE, TRUE))
7340 {
7341 amount = ind_func_type;
7342 }
7343 else
7344 {
7345 amount = 0;
7346 curwin->w_cursor = cur_curpos;
7347
7348 /* search backwards until we find something we recognize */
7349
7350 while (curwin->w_cursor.lnum > 1)
7351 {
7352 curwin->w_cursor.lnum--;
7353 curwin->w_cursor.col = 0;
7354
7355 l = ml_get_curline();
7356
7357 /*
7358 * If we're in a comment now, skip to the start of the comment.
7359 */ /* XXX */
7360 if ((trypos = find_start_comment(ind_maxcomment)) != NULL)
7361 {
7362 curwin->w_cursor.lnum = trypos->lnum + 1;
7363 continue;
7364 }
7365
7366 /*
7367 * Are we at the start of a cpp base class declaration or constructor
7368 * initialization?
7369 */ /* XXX */
7370 if (ind_cpp_baseclass != 0 && theline[0] != '{'
7371 && cin_is_cpp_baseclass(l, &col))
7372 {
7373 if (col == 0)
7374 {
7375 amount = get_indent() + ind_cpp_baseclass; /* XXX */
7376 if (find_last_paren(l, '(', ')')
7377 && (trypos = find_match_paren(ind_maxparen,
7378 ind_maxcomment)) != NULL)
7379 amount = get_indent_lnum(trypos->lnum)
7380 + ind_cpp_baseclass; /* XXX */
7381 }
7382 else
7383 {
7384 curwin->w_cursor.col = col;
7385 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
7386 amount = (int)col;
7387 }
7388 break;
7389 }
7390
7391 /*
7392 * Skip preprocessor directives and blank lines.
7393 */
7394 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7395 continue;
7396
7397 if (cin_nocode(l))
7398 continue;
7399
7400 /*
7401 * If the previous line ends in ',', use one level of
7402 * indentation:
7403 * int foo,
7404 * bar;
7405 * do this before checking for '}' in case of eg.
7406 * enum foobar
7407 * {
7408 * ...
7409 * } foo,
7410 * bar;
7411 */
7412 n = 0;
7413 if (cin_ends_in(l, (char_u *)",", NULL)
7414 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
7415 {
7416 /* take us back to opening paren */
7417 if (find_last_paren(l, '(', ')')
7418 && (trypos = find_match_paren(ind_maxparen,
7419 ind_maxcomment)) != NULL)
7420 curwin->w_cursor.lnum = trypos->lnum;
7421
7422 /* For a line ending in ',' that is a continuation line go
7423 * back to the first line with a backslash:
7424 * char *foo = "bla\
7425 * bla",
7426 * here;
7427 */
7428 while (n == 0 && curwin->w_cursor.lnum > 1)
7429 {
7430 l = ml_get(curwin->w_cursor.lnum - 1);
7431 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
7432 break;
7433 --curwin->w_cursor.lnum;
7434 }
7435
7436 amount = get_indent(); /* XXX */
7437
7438 if (amount == 0)
7439 amount = cin_first_id_amount();
7440 if (amount == 0)
7441 amount = ind_continuation;
7442 break;
7443 }
7444
7445 /*
7446 * If the line looks like a function declaration, and we're
7447 * not in a comment, put it the left margin.
7448 */
7449 if (cin_isfuncdecl(NULL, cur_curpos.lnum)) /* XXX */
7450 break;
7451 l = ml_get_curline();
7452
7453 /*
7454 * Finding the closing '}' of a previous function. Put
7455 * current line at the left margin. For when 'cino' has "fs".
7456 */
7457 if (*skipwhite(l) == '}')
7458 break;
7459
7460 /* (matching {)
7461 * If the previous line ends on '};' (maybe followed by
7462 * comments) align at column 0. For example:
7463 * char *string_array[] = { "foo",
7464 * / * x * / "b};ar" }; / * foobar * /
7465 */
7466 if (cin_ends_in(l, (char_u *)"};", NULL))
7467 break;
7468
7469 /*
7470 * If the PREVIOUS line is a function declaration, the current
7471 * line (and the ones that follow) needs to be indented as
7472 * parameters.
7473 */
7474 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum))
7475 {
7476 amount = ind_param;
7477 break;
7478 }
7479
7480 /*
7481 * If the previous line ends in ';' and the line before the
7482 * previous line ends in ',' or '\', ident to column zero:
7483 * int foo,
7484 * bar;
7485 * indent_to_0 here;
7486 */
7487 if (cin_ends_in(l, (char_u*)";", NULL))
7488 {
7489 l = ml_get(curwin->w_cursor.lnum - 1);
7490 if (cin_ends_in(l, (char_u *)",", NULL)
7491 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
7492 break;
7493 l = ml_get_curline();
7494 }
7495
7496 /*
7497 * Doesn't look like anything interesting -- so just
7498 * use the indent of this line.
7499 *
7500 * Position the cursor over the rightmost paren, so that
7501 * matching it will take us back to the start of the line.
7502 */
7503 find_last_paren(l, '(', ')');
7504
7505 if ((trypos = find_match_paren(ind_maxparen,
7506 ind_maxcomment)) != NULL)
7507 curwin->w_cursor.lnum = trypos->lnum;
7508 amount = get_indent(); /* XXX */
7509 break;
7510 }
7511
7512 /* add extra indent for a comment */
7513 if (cin_iscomment(theline))
7514 amount += ind_comment;
7515
7516 /* add extra indent if the previous line ended in a backslash:
7517 * "asdfasdf\
7518 * here";
7519 * char *foo = "asdf\
7520 * here";
7521 */
7522 if (cur_curpos.lnum > 1)
7523 {
7524 l = ml_get(cur_curpos.lnum - 1);
7525 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
7526 {
7527 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
7528 if (cur_amount > 0)
7529 amount = cur_amount;
7530 else if (cur_amount == 0)
7531 amount += ind_continuation;
7532 }
7533 }
7534 }
7535 }
7536
7537theend:
7538 /* put the cursor back where it belongs */
7539 curwin->w_cursor = cur_curpos;
7540
7541 vim_free(linecopy);
7542
7543 if (amount < 0)
7544 return 0;
7545 return amount;
7546}
7547
7548 static int
7549find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment)
7550 int lookfor;
7551 linenr_T ourscope;
7552 int ind_maxparen;
7553 int ind_maxcomment;
7554{
7555 char_u *look;
7556 pos_T *theirscope;
7557 char_u *mightbeif;
7558 int elselevel;
7559 int whilelevel;
7560
7561 if (lookfor == LOOKFOR_IF)
7562 {
7563 elselevel = 1;
7564 whilelevel = 0;
7565 }
7566 else
7567 {
7568 elselevel = 0;
7569 whilelevel = 1;
7570 }
7571
7572 curwin->w_cursor.col = 0;
7573
7574 while (curwin->w_cursor.lnum > ourscope + 1)
7575 {
7576 curwin->w_cursor.lnum--;
7577 curwin->w_cursor.col = 0;
7578
7579 look = cin_skipcomment(ml_get_curline());
7580 if (cin_iselse(look)
7581 || cin_isif(look)
7582 || cin_isdo(look) /* XXX */
7583 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
7584 {
7585 /*
7586 * if we've gone outside the braces entirely,
7587 * we must be out of scope...
7588 */
7589 theirscope = find_start_brace(ind_maxcomment); /* XXX */
7590 if (theirscope == NULL)
7591 break;
7592
7593 /*
7594 * and if the brace enclosing this is further
7595 * back than the one enclosing the else, we're
7596 * out of luck too.
7597 */
7598 if (theirscope->lnum < ourscope)
7599 break;
7600
7601 /*
7602 * and if they're enclosed in a *deeper* brace,
7603 * then we can ignore it because it's in a
7604 * different scope...
7605 */
7606 if (theirscope->lnum > ourscope)
7607 continue;
7608
7609 /*
7610 * if it was an "else" (that's not an "else if")
7611 * then we need to go back to another if, so
7612 * increment elselevel
7613 */
7614 look = cin_skipcomment(ml_get_curline());
7615 if (cin_iselse(look))
7616 {
7617 mightbeif = cin_skipcomment(look + 4);
7618 if (!cin_isif(mightbeif))
7619 ++elselevel;
7620 continue;
7621 }
7622
7623 /*
7624 * if it was a "while" then we need to go back to
7625 * another "do", so increment whilelevel. XXX
7626 */
7627 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
7628 {
7629 ++whilelevel;
7630 continue;
7631 }
7632
7633 /* If it's an "if" decrement elselevel */
7634 look = cin_skipcomment(ml_get_curline());
7635 if (cin_isif(look))
7636 {
7637 elselevel--;
7638 /*
7639 * When looking for an "if" ignore "while"s that
7640 * get in the way.
7641 */
7642 if (elselevel == 0 && lookfor == LOOKFOR_IF)
7643 whilelevel = 0;
7644 }
7645
7646 /* If it's a "do" decrement whilelevel */
7647 if (cin_isdo(look))
7648 whilelevel--;
7649
7650 /*
7651 * if we've used up all the elses, then
7652 * this must be the if that we want!
7653 * match the indent level of that if.
7654 */
7655 if (elselevel <= 0 && whilelevel <= 0)
7656 {
7657 return OK;
7658 }
7659 }
7660 }
7661 return FAIL;
7662}
7663
7664# if defined(FEAT_EVAL) || defined(PROTO)
7665/*
7666 * Get indent level from 'indentexpr'.
7667 */
7668 int
7669get_expr_indent()
7670{
7671 int indent;
7672 pos_T pos;
7673 int save_State;
7674
7675 pos = curwin->w_cursor;
7676 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
7677 ++sandbox;
7678 indent = eval_to_number(curbuf->b_p_inde);
7679 --sandbox;
7680
7681 /* Restore the cursor position so that 'indentexpr' doesn't need to.
7682 * Pretend to be in Insert mode, allow cursor past end of line for "o"
7683 * command. */
7684 save_State = State;
7685 State = INSERT;
7686 curwin->w_cursor = pos;
7687 check_cursor();
7688 State = save_State;
7689
7690 /* If there is an error, just keep the current indent. */
7691 if (indent < 0)
7692 indent = get_indent();
7693
7694 return indent;
7695}
7696# endif
7697
7698#endif /* FEAT_CINDENT */
7699
7700#if defined(FEAT_LISP) || defined(PROTO)
7701
7702static int lisp_match __ARGS((char_u *p));
7703
7704 static int
7705lisp_match(p)
7706 char_u *p;
7707{
7708 char_u buf[LSIZE];
7709 int len;
7710 char_u *word = p_lispwords;
7711
7712 while (*word != NUL)
7713 {
7714 (void)copy_option_part(&word, buf, LSIZE, ",");
7715 len = (int)STRLEN(buf);
7716 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
7717 return TRUE;
7718 }
7719 return FALSE;
7720}
7721
7722/*
7723 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
7724 * The incompatible newer method is quite a bit better at indenting
7725 * code in lisp-like languages than the traditional one; it's still
7726 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
7727 *
7728 * TODO:
7729 * Findmatch() should be adapted for lisp, also to make showmatch
7730 * work correctly: now (v5.3) it seems all C/C++ oriented:
7731 * - it does not recognize the #\( and #\) notations as character literals
7732 * - it doesn't know about comments starting with a semicolon
7733 * - it incorrectly interprets '(' as a character literal
7734 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007735 * Update from Sergey Khorev:
7736 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
7738 int
7739get_lisp_indent()
7740{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007741 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 int amount;
7743 char_u *that;
7744 colnr_T col;
7745 colnr_T firsttry;
7746 int parencount, quotecount;
7747 int vi_lisp;
7748
7749 /* Set vi_lisp to use the vi-compatible method */
7750 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
7751
7752 realpos = curwin->w_cursor;
7753 curwin->w_cursor.col = 0;
7754
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007755 if ((pos = findmatch(NULL, '(')) == NULL)
7756 pos = findmatch(NULL, '[');
7757 else
7758 {
7759 paren = *pos;
7760 pos = findmatch(NULL, '[');
7761 if (pos == NULL || ltp(pos, &paren))
7762 pos = &paren;
7763 }
7764 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {
7766 /* Extra trick: Take the indent of the first previous non-white
7767 * line that is at the same () level. */
7768 amount = -1;
7769 parencount = 0;
7770
7771 while (--curwin->w_cursor.lnum >= pos->lnum)
7772 {
7773 if (linewhite(curwin->w_cursor.lnum))
7774 continue;
7775 for (that = ml_get_curline(); *that != NUL; ++that)
7776 {
7777 if (*that == ';')
7778 {
7779 while (*(that + 1) != NUL)
7780 ++that;
7781 continue;
7782 }
7783 if (*that == '\\')
7784 {
7785 if (*(that + 1) != NUL)
7786 ++that;
7787 continue;
7788 }
7789 if (*that == '"' && *(that + 1) != NUL)
7790 {
7791 that++;
7792 while (*that && (*that != '"' || *(that - 1) == '\\'))
7793 ++that;
7794 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007795 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007797 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 --parencount;
7799 }
7800 if (parencount == 0)
7801 {
7802 amount = get_indent();
7803 break;
7804 }
7805 }
7806
7807 if (amount == -1)
7808 {
7809 curwin->w_cursor.lnum = pos->lnum;
7810 curwin->w_cursor.col = pos->col;
7811 col = pos->col;
7812
7813 that = ml_get_curline();
7814
7815 if (vi_lisp && get_indent() == 0)
7816 amount = 2;
7817 else
7818 {
7819 amount = 0;
7820 while (*that && col)
7821 {
7822 amount += lbr_chartabsize_adv(&that, (colnr_T)amount);
7823 col--;
7824 }
7825
7826 /*
7827 * Some keywords require "body" indenting rules (the
7828 * non-standard-lisp ones are Scheme special forms):
7829 *
7830 * (let ((a 1)) instead (let ((a 1))
7831 * (...)) of (...))
7832 */
7833
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007834 if (!vi_lisp && (*that == '(' || *that == '[')
7835 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 amount += 2;
7837 else
7838 {
7839 that++;
7840 amount++;
7841 firsttry = amount;
7842
7843 while (vim_iswhite(*that))
7844 {
7845 amount += lbr_chartabsize(that, (colnr_T)amount);
7846 ++that;
7847 }
7848
7849 if (*that && *that != ';') /* not a comment line */
7850 {
7851 /* test *that != '(' to accomodate first let/do
7852 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007853 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 firsttry++;
7855
7856 parencount = 0;
7857 quotecount = 0;
7858
7859 if (vi_lisp
7860 || (*that != '"'
7861 && *that != '\''
7862 && *that != '#'
7863 && (*that < '0' || *that > '9')))
7864 {
7865 while (*that
7866 && (!vim_iswhite(*that)
7867 || quotecount
7868 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007869 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 && !quotecount
7871 && !parencount
7872 && vi_lisp)))
7873 {
7874 if (*that == '"')
7875 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007876 if ((*that == '(' || *that == '[')
7877 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007879 if ((*that == ')' || *that == ']')
7880 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 --parencount;
7882 if (*that == '\\' && *(that+1) != NUL)
7883 amount += lbr_chartabsize_adv(&that,
7884 (colnr_T)amount);
7885 amount += lbr_chartabsize_adv(&that,
7886 (colnr_T)amount);
7887 }
7888 }
7889 while (vim_iswhite(*that))
7890 {
7891 amount += lbr_chartabsize(that, (colnr_T)amount);
7892 that++;
7893 }
7894 if (!*that || *that == ';')
7895 amount = firsttry;
7896 }
7897 }
7898 }
7899 }
7900 }
7901 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007902 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
7904 curwin->w_cursor = realpos;
7905
7906 return amount;
7907}
7908#endif /* FEAT_LISP */
7909
7910 void
7911prepare_to_exit()
7912{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007913#if defined(SIGHUP) && defined(SIG_IGN)
7914 /* Ignore SIGHUP, because a dropped connection causes a read error, which
7915 * makes Vim exit and then handling SIGHUP causes various reentrance
7916 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007917 signal(SIGHUP, SIG_IGN);
7918#endif
7919
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920#ifdef FEAT_GUI
7921 if (gui.in_use)
7922 {
7923 gui.dying = TRUE;
7924 out_trash(); /* trash any pending output */
7925 }
7926 else
7927#endif
7928 {
7929 windgoto((int)Rows - 1, 0);
7930
7931 /*
7932 * Switch terminal mode back now, so messages end up on the "normal"
7933 * screen (if there are two screens).
7934 */
7935 settmode(TMODE_COOK);
7936#ifdef WIN3264
7937 if (can_end_termcap_mode(FALSE) == TRUE)
7938#endif
7939 stoptermcap();
7940 out_flush();
7941 }
7942}
7943
7944/*
7945 * Preserve files and exit.
7946 * When called IObuff must contain a message.
7947 */
7948 void
7949preserve_exit()
7950{
7951 buf_T *buf;
7952
7953 prepare_to_exit();
7954
Bram Moolenaar4770d092006-01-12 23:22:24 +00007955 /* Setting this will prevent free() calls. That avoids calling free()
7956 * recursively when free() was invoked with a bad pointer. */
7957 really_exiting = TRUE;
7958
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 out_str(IObuff);
7960 screen_start(); /* don't know where cursor is now */
7961 out_flush();
7962
7963 ml_close_notmod(); /* close all not-modified buffers */
7964
7965 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7966 {
7967 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
7968 {
7969 OUT_STR(_("Vim: preserving files...\n"));
7970 screen_start(); /* don't know where cursor is now */
7971 out_flush();
7972 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
7973 break;
7974 }
7975 }
7976
7977 ml_close_all(FALSE); /* close all memfiles, without deleting */
7978
7979 OUT_STR(_("Vim: Finished.\n"));
7980
7981 getout(1);
7982}
7983
7984/*
7985 * return TRUE if "fname" exists.
7986 */
7987 int
7988vim_fexists(fname)
7989 char_u *fname;
7990{
7991 struct stat st;
7992
7993 if (mch_stat((char *)fname, &st))
7994 return FALSE;
7995 return TRUE;
7996}
7997
7998/*
7999 * Check for CTRL-C pressed, but only once in a while.
8000 * Should be used instead of ui_breakcheck() for functions that check for
8001 * each line in the file. Calling ui_breakcheck() each time takes too much
8002 * time, because it can be a system call.
8003 */
8004
8005#ifndef BREAKCHECK_SKIP
8006# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
8007# define BREAKCHECK_SKIP 200
8008# else
8009# define BREAKCHECK_SKIP 32
8010# endif
8011#endif
8012
8013static int breakcheck_count = 0;
8014
8015 void
8016line_breakcheck()
8017{
8018 if (++breakcheck_count >= BREAKCHECK_SKIP)
8019 {
8020 breakcheck_count = 0;
8021 ui_breakcheck();
8022 }
8023}
8024
8025/*
8026 * Like line_breakcheck() but check 10 times less often.
8027 */
8028 void
8029fast_breakcheck()
8030{
8031 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
8032 {
8033 breakcheck_count = 0;
8034 ui_breakcheck();
8035 }
8036}
8037
8038/*
8039 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
8040 * 'wildignore'.
8041 */
8042 int
8043expand_wildcards(num_pat, pat, num_file, file, flags)
8044 int num_pat; /* number of input patterns */
8045 char_u **pat; /* array of input patterns */
8046 int *num_file; /* resulting number of files */
8047 char_u ***file; /* array of resulting files */
8048 int flags; /* EW_DIR, etc. */
8049{
8050 int retval;
8051 int i, j;
8052 char_u *p;
8053 int non_suf_match; /* number without matching suffix */
8054
8055 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
8056
8057 /* When keeping all matches, return here */
8058 if (flags & EW_KEEPALL)
8059 return retval;
8060
8061#ifdef FEAT_WILDIGN
8062 /*
8063 * Remove names that match 'wildignore'.
8064 */
8065 if (*p_wig)
8066 {
8067 char_u *ffname;
8068
8069 /* check all files in (*file)[] */
8070 for (i = 0; i < *num_file; ++i)
8071 {
8072 ffname = FullName_save((*file)[i], FALSE);
8073 if (ffname == NULL) /* out of memory */
8074 break;
8075# ifdef VMS
8076 vms_remove_version(ffname);
8077# endif
8078 if (match_file_list(p_wig, (*file)[i], ffname))
8079 {
8080 /* remove this matching file from the list */
8081 vim_free((*file)[i]);
8082 for (j = i; j + 1 < *num_file; ++j)
8083 (*file)[j] = (*file)[j + 1];
8084 --*num_file;
8085 --i;
8086 }
8087 vim_free(ffname);
8088 }
8089 }
8090#endif
8091
8092 /*
8093 * Move the names where 'suffixes' match to the end.
8094 */
8095 if (*num_file > 1)
8096 {
8097 non_suf_match = 0;
8098 for (i = 0; i < *num_file; ++i)
8099 {
8100 if (!match_suffix((*file)[i]))
8101 {
8102 /*
8103 * Move the name without matching suffix to the front
8104 * of the list.
8105 */
8106 p = (*file)[i];
8107 for (j = i; j > non_suf_match; --j)
8108 (*file)[j] = (*file)[j - 1];
8109 (*file)[non_suf_match++] = p;
8110 }
8111 }
8112 }
8113
8114 return retval;
8115}
8116
8117/*
8118 * Return TRUE if "fname" matches with an entry in 'suffixes'.
8119 */
8120 int
8121match_suffix(fname)
8122 char_u *fname;
8123{
8124 int fnamelen, setsuflen;
8125 char_u *setsuf;
8126#define MAXSUFLEN 30 /* maximum length of a file suffix */
8127 char_u suf_buf[MAXSUFLEN];
8128
8129 fnamelen = (int)STRLEN(fname);
8130 setsuflen = 0;
8131 for (setsuf = p_su; *setsuf; )
8132 {
8133 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
8134 if (fnamelen >= setsuflen
8135 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
8136 (size_t)setsuflen) == 0)
8137 break;
8138 setsuflen = 0;
8139 }
8140 return (setsuflen != 0);
8141}
8142
8143#if !defined(NO_EXPANDPATH) || defined(PROTO)
8144
8145# ifdef VIM_BACKTICK
8146static int vim_backtick __ARGS((char_u *p));
8147static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
8148# endif
8149
8150# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
8151/*
8152 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
8153 * it's shared between these systems.
8154 */
8155# if defined(DJGPP) || defined(PROTO)
8156# define _cdecl /* DJGPP doesn't have this */
8157# else
8158# ifdef __BORLANDC__
8159# define _cdecl _RTLENTRYF
8160# endif
8161# endif
8162
8163/*
8164 * comparison function for qsort in dos_expandpath()
8165 */
8166 static int _cdecl
8167pstrcmp(const void *a, const void *b)
8168{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008169 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170}
8171
8172# ifndef WIN3264
8173 static void
8174namelowcpy(
8175 char_u *d,
8176 char_u *s)
8177{
8178# ifdef DJGPP
8179 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
8180 while (*s)
8181 *d++ = *s++;
8182 else
8183# endif
8184 while (*s)
8185 *d++ = TOLOWER_LOC(*s++);
8186 *d = NUL;
8187}
8188# endif
8189
8190/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00008191 * Recursively expand one path component into all matching files and/or
8192 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 * Return the number of matches found.
8194 * "path" has backslashes before chars that are not to be expanded, starting
8195 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00008196 * Return the number of matches found.
8197 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 */
8199 static int
8200dos_expandpath(
8201 garray_T *gap,
8202 char_u *path,
8203 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00008204 int flags, /* EW_* flags */
8205 int didstar) /* expaneded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
Bram Moolenaar231334e2005-07-25 20:46:57 +00008207 char_u *buf;
8208 char_u *path_end;
8209 char_u *p, *s, *e;
8210 int start_len = gap->ga_len;
8211 char_u *pat;
8212 regmatch_T regmatch;
8213 int starts_with_dot;
8214 int matches;
8215 int len;
8216 int starstar = FALSE;
8217 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218#ifdef WIN3264
8219 WIN32_FIND_DATA fb;
8220 HANDLE hFind = (HANDLE)0;
8221# ifdef FEAT_MBYTE
8222 WIN32_FIND_DATAW wfb;
8223 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
8224# endif
8225#else
8226 struct ffblk fb;
8227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00008229 int ok;
8230
8231 /* Expanding "**" may take a long time, check for CTRL-C. */
8232 if (stardepth > 0)
8233 {
8234 ui_breakcheck();
8235 if (got_int)
8236 return 0;
8237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
8239 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00008240 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 if (buf == NULL)
8242 return 0;
8243
8244 /*
8245 * Find the first part in the path name that contains a wildcard or a ~1.
8246 * Copy it into buf, including the preceding characters.
8247 */
8248 p = buf;
8249 s = buf;
8250 e = NULL;
8251 path_end = path;
8252 while (*path_end != NUL)
8253 {
8254 /* May ignore a wildcard that has a backslash before it; it will
8255 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
8256 if (path_end >= path + wildoff && rem_backslash(path_end))
8257 *p++ = *path_end++;
8258 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
8259 {
8260 if (e != NULL)
8261 break;
8262 s = p + 1;
8263 }
8264 else if (path_end >= path + wildoff
8265 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
8266 e = p;
8267#ifdef FEAT_MBYTE
8268 if (has_mbyte)
8269 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008270 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 STRNCPY(p, path_end, len);
8272 p += len;
8273 path_end += len;
8274 }
8275 else
8276#endif
8277 *p++ = *path_end++;
8278 }
8279 e = p;
8280 *e = NUL;
8281
8282 /* now we have one wildcard component between s and e */
8283 /* Remove backslashes between "wildoff" and the start of the wildcard
8284 * component. */
8285 for (p = buf + wildoff; p < s; ++p)
8286 if (rem_backslash(p))
8287 {
8288 STRCPY(p, p + 1);
8289 --e;
8290 --s;
8291 }
8292
Bram Moolenaar231334e2005-07-25 20:46:57 +00008293 /* Check for "**" between "s" and "e". */
8294 for (p = s; p < e; ++p)
8295 if (p[0] == '*' && p[1] == '*')
8296 starstar = TRUE;
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 starts_with_dot = (*s == '.');
8299 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
8300 if (pat == NULL)
8301 {
8302 vim_free(buf);
8303 return 0;
8304 }
8305
8306 /* compile the regexp into a program */
8307 regmatch.rm_ic = TRUE; /* Always ignore case */
8308 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
8309 vim_free(pat);
8310
8311 if (regmatch.regprog == NULL)
8312 {
8313 vim_free(buf);
8314 return 0;
8315 }
8316
8317 /* remember the pattern or file name being looked for */
8318 matchname = vim_strsave(s);
8319
Bram Moolenaar231334e2005-07-25 20:46:57 +00008320 /* If "**" is by itself, this is the first time we encounter it and more
8321 * is following then find matches without any directory. */
8322 if (!didstar && stardepth < 100 && starstar && e - s == 2
8323 && *path_end == '/')
8324 {
8325 STRCPY(s, path_end + 1);
8326 ++stardepth;
8327 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
8328 --stardepth;
8329 }
8330
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 /* Scan all files in the directory with "dir/ *.*" */
8332 STRCPY(s, "*.*");
8333#ifdef WIN3264
8334# ifdef FEAT_MBYTE
8335 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
8336 {
8337 /* The active codepage differs from 'encoding'. Attempt using the
8338 * wide function. If it fails because it is not implemented fall back
8339 * to the non-wide version (for Windows 98) */
8340 wn = enc_to_ucs2(buf, NULL);
8341 if (wn != NULL)
8342 {
8343 hFind = FindFirstFileW(wn, &wfb);
8344 if (hFind == INVALID_HANDLE_VALUE
8345 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
8346 {
8347 vim_free(wn);
8348 wn = NULL;
8349 }
8350 }
8351 }
8352
8353 if (wn == NULL)
8354# endif
8355 hFind = FindFirstFile(buf, &fb);
8356 ok = (hFind != INVALID_HANDLE_VALUE);
8357#else
8358 /* If we are expanding wildcards we try both files and directories */
8359 ok = (findfirst((char *)buf, &fb,
8360 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
8361#endif
8362
8363 while (ok)
8364 {
8365#ifdef WIN3264
8366# ifdef FEAT_MBYTE
8367 if (wn != NULL)
8368 p = ucs2_to_enc(wfb.cFileName, NULL); /* p is allocated here */
8369 else
8370# endif
8371 p = (char_u *)fb.cFileName;
8372#else
8373 p = (char_u *)fb.ff_name;
8374#endif
8375 /* Ignore entries starting with a dot, unless when asked for. Accept
8376 * all entries found with "matchname". */
8377 if ((p[0] != '.' || starts_with_dot)
8378 && (matchname == NULL
8379 || vim_regexec(&regmatch, p, (colnr_T)0)))
8380 {
8381#ifdef WIN3264
8382 STRCPY(s, p);
8383#else
8384 namelowcpy(s, p);
8385#endif
8386 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00008387
8388 if (starstar && stardepth < 100)
8389 {
8390 /* For "**" in the pattern first go deeper in the tree to
8391 * find matches. */
8392 STRCPY(buf + len, "/**");
8393 STRCPY(buf + len + 3, path_end);
8394 ++stardepth;
8395 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
8396 --stardepth;
8397 }
8398
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 STRCPY(buf + len, path_end);
8400 if (mch_has_exp_wildcard(path_end))
8401 {
8402 /* need to expand another component of the path */
8403 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00008404 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 }
8406 else
8407 {
8408 /* no more wildcards, check if there is a match */
8409 /* remove backslashes for the remaining components only */
8410 if (*path_end != 0)
8411 backslash_halve(buf + len + 1);
8412 if (mch_getperm(buf) >= 0) /* add existing file */
8413 addfile(gap, buf, flags);
8414 }
8415 }
8416
8417#ifdef WIN3264
8418# ifdef FEAT_MBYTE
8419 if (wn != NULL)
8420 {
8421 vim_free(p);
8422 ok = FindNextFileW(hFind, &wfb);
8423 }
8424 else
8425# endif
8426 ok = FindNextFile(hFind, &fb);
8427#else
8428 ok = (findnext(&fb) == 0);
8429#endif
8430
8431 /* If no more matches and no match was used, try expanding the name
8432 * itself. Finds the long name of a short filename. */
8433 if (!ok && matchname != NULL && gap->ga_len == start_len)
8434 {
8435 STRCPY(s, matchname);
8436#ifdef WIN3264
8437 FindClose(hFind);
8438# ifdef FEAT_MBYTE
8439 if (wn != NULL)
8440 {
8441 vim_free(wn);
8442 wn = enc_to_ucs2(buf, NULL);
8443 if (wn != NULL)
8444 hFind = FindFirstFileW(wn, &wfb);
8445 }
8446 if (wn == NULL)
8447# endif
8448 hFind = FindFirstFile(buf, &fb);
8449 ok = (hFind != INVALID_HANDLE_VALUE);
8450#else
8451 ok = (findfirst((char *)buf, &fb,
8452 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
8453#endif
8454 vim_free(matchname);
8455 matchname = NULL;
8456 }
8457 }
8458
8459#ifdef WIN3264
8460 FindClose(hFind);
8461# ifdef FEAT_MBYTE
8462 vim_free(wn);
8463# endif
8464#endif
8465 vim_free(buf);
8466 vim_free(regmatch.regprog);
8467 vim_free(matchname);
8468
8469 matches = gap->ga_len - start_len;
8470 if (matches > 0)
8471 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
8472 sizeof(char_u *), pstrcmp);
8473 return matches;
8474}
8475
8476 int
8477mch_expandpath(
8478 garray_T *gap,
8479 char_u *path,
8480 int flags) /* EW_* flags */
8481{
Bram Moolenaar231334e2005-07-25 20:46:57 +00008482 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483}
8484# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
8485
Bram Moolenaar231334e2005-07-25 20:46:57 +00008486#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
8487 || defined(PROTO)
8488/*
8489 * Unix style wildcard expansion code.
8490 * It's here because it's used both for Unix and Mac.
8491 */
8492static int pstrcmp __ARGS((const void *, const void *));
8493
8494 static int
8495pstrcmp(a, b)
8496 const void *a, *b;
8497{
8498 return (pathcmp(*(char **)a, *(char **)b, -1));
8499}
8500
8501/*
8502 * Recursively expand one path component into all matching files and/or
8503 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
8504 * "path" has backslashes before chars that are not to be expanded, starting
8505 * at "path + wildoff".
8506 * Return the number of matches found.
8507 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
8508 */
8509 int
8510unix_expandpath(gap, path, wildoff, flags, didstar)
8511 garray_T *gap;
8512 char_u *path;
8513 int wildoff;
8514 int flags; /* EW_* flags */
8515 int didstar; /* expanded "**" once already */
8516{
8517 char_u *buf;
8518 char_u *path_end;
8519 char_u *p, *s, *e;
8520 int start_len = gap->ga_len;
8521 char_u *pat;
8522 regmatch_T regmatch;
8523 int starts_with_dot;
8524 int matches;
8525 int len;
8526 int starstar = FALSE;
8527 static int stardepth = 0; /* depth for "**" expansion */
8528
8529 DIR *dirp;
8530 struct dirent *dp;
8531
8532 /* Expanding "**" may take a long time, check for CTRL-C. */
8533 if (stardepth > 0)
8534 {
8535 ui_breakcheck();
8536 if (got_int)
8537 return 0;
8538 }
8539
8540 /* make room for file name */
8541 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
8542 if (buf == NULL)
8543 return 0;
8544
8545 /*
8546 * Find the first part in the path name that contains a wildcard.
8547 * Copy it into "buf", including the preceding characters.
8548 */
8549 p = buf;
8550 s = buf;
8551 e = NULL;
8552 path_end = path;
8553 while (*path_end != NUL)
8554 {
8555 /* May ignore a wildcard that has a backslash before it; it will
8556 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
8557 if (path_end >= path + wildoff && rem_backslash(path_end))
8558 *p++ = *path_end++;
8559 else if (*path_end == '/')
8560 {
8561 if (e != NULL)
8562 break;
8563 s = p + 1;
8564 }
8565 else if (path_end >= path + wildoff
8566 && vim_strchr((char_u *)"*?[{~$", *path_end) != NULL)
8567 e = p;
8568#ifdef FEAT_MBYTE
8569 if (has_mbyte)
8570 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008571 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00008572 STRNCPY(p, path_end, len);
8573 p += len;
8574 path_end += len;
8575 }
8576 else
8577#endif
8578 *p++ = *path_end++;
8579 }
8580 e = p;
8581 *e = NUL;
8582
8583 /* now we have one wildcard component between "s" and "e" */
8584 /* Remove backslashes between "wildoff" and the start of the wildcard
8585 * component. */
8586 for (p = buf + wildoff; p < s; ++p)
8587 if (rem_backslash(p))
8588 {
8589 STRCPY(p, p + 1);
8590 --e;
8591 --s;
8592 }
8593
8594 /* Check for "**" between "s" and "e". */
8595 for (p = s; p < e; ++p)
8596 if (p[0] == '*' && p[1] == '*')
8597 starstar = TRUE;
8598
8599 /* convert the file pattern to a regexp pattern */
8600 starts_with_dot = (*s == '.');
8601 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
8602 if (pat == NULL)
8603 {
8604 vim_free(buf);
8605 return 0;
8606 }
8607
8608 /* compile the regexp into a program */
Bram Moolenaarcc016f52005-12-10 20:23:46 +00008609#ifdef CASE_INSENSITIVE_FILENAME
Bram Moolenaar231334e2005-07-25 20:46:57 +00008610 regmatch.rm_ic = TRUE; /* Behave like Terminal.app */
8611#else
8612 regmatch.rm_ic = FALSE; /* Don't ever ignore case */
8613#endif
8614 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
8615 vim_free(pat);
8616
8617 if (regmatch.regprog == NULL)
8618 {
8619 vim_free(buf);
8620 return 0;
8621 }
8622
8623 /* If "**" is by itself, this is the first time we encounter it and more
8624 * is following then find matches without any directory. */
8625 if (!didstar && stardepth < 100 && starstar && e - s == 2
8626 && *path_end == '/')
8627 {
8628 STRCPY(s, path_end + 1);
8629 ++stardepth;
8630 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
8631 --stardepth;
8632 }
8633
8634 /* open the directory for scanning */
8635 *s = NUL;
8636 dirp = opendir(*buf == NUL ? "." : (char *)buf);
8637
8638 /* Find all matching entries */
8639 if (dirp != NULL)
8640 {
8641 for (;;)
8642 {
8643 dp = readdir(dirp);
8644 if (dp == NULL)
8645 break;
8646 if ((dp->d_name[0] != '.' || starts_with_dot)
8647 && vim_regexec(&regmatch, (char_u *)dp->d_name, (colnr_T)0))
8648 {
8649 STRCPY(s, dp->d_name);
8650 len = STRLEN(buf);
8651
8652 if (starstar && stardepth < 100)
8653 {
8654 /* For "**" in the pattern first go deeper in the tree to
8655 * find matches. */
8656 STRCPY(buf + len, "/**");
8657 STRCPY(buf + len + 3, path_end);
8658 ++stardepth;
8659 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
8660 --stardepth;
8661 }
8662
8663 STRCPY(buf + len, path_end);
8664 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
8665 {
8666 /* need to expand another component of the path */
8667 /* remove backslashes for the remaining components only */
8668 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
8669 }
8670 else
8671 {
8672 /* no more wildcards, check if there is a match */
8673 /* remove backslashes for the remaining components only */
8674 if (*path_end != NUL)
8675 backslash_halve(buf + len + 1);
8676 if (mch_getperm(buf) >= 0) /* add existing file */
8677 {
8678#if defined(MACOS_X) && defined(FEAT_MBYTE)
8679 size_t precomp_len = STRLEN(buf)+1;
8680 char_u *precomp_buf =
8681 mac_precompose_path(buf, precomp_len, &precomp_len);
8682 if (precomp_buf)
8683 {
8684 mch_memmove(buf, precomp_buf, precomp_len);
8685 vim_free(precomp_buf);
8686 }
8687#endif
8688 addfile(gap, buf, flags);
8689 }
8690 }
8691 }
8692 }
8693
8694 closedir(dirp);
8695 }
8696
8697 vim_free(buf);
8698 vim_free(regmatch.regprog);
8699
8700 matches = gap->ga_len - start_len;
8701 if (matches > 0)
8702 qsort(((char_u **)gap->ga_data) + start_len, matches,
8703 sizeof(char_u *), pstrcmp);
8704 return matches;
8705}
8706#endif
8707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708/*
8709 * Generic wildcard expansion code.
8710 *
8711 * Characters in "pat" that should not be expanded must be preceded with a
8712 * backslash. E.g., "/path\ with\ spaces/my\*star*"
8713 *
8714 * Return FAIL when no single file was found. In this case "num_file" is not
8715 * set, and "file" may contain an error message.
8716 * Return OK when some files found. "num_file" is set to the number of
8717 * matches, "file" to the array of matches. Call FreeWild() later.
8718 */
8719 int
8720gen_expand_wildcards(num_pat, pat, num_file, file, flags)
8721 int num_pat; /* number of input patterns */
8722 char_u **pat; /* array of input patterns */
8723 int *num_file; /* resulting number of files */
8724 char_u ***file; /* array of resulting files */
8725 int flags; /* EW_* flags */
8726{
8727 int i;
8728 garray_T ga;
8729 char_u *p;
8730 static int recursive = FALSE;
8731 int add_pat;
8732
8733 /*
8734 * expand_env() is called to expand things like "~user". If this fails,
8735 * it calls ExpandOne(), which brings us back here. In this case, always
8736 * call the machine specific expansion function, if possible. Otherwise,
8737 * return FAIL.
8738 */
8739 if (recursive)
8740#ifdef SPECIAL_WILDCHAR
8741 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
8742#else
8743 return FAIL;
8744#endif
8745
8746#ifdef SPECIAL_WILDCHAR
8747 /*
8748 * If there are any special wildcard characters which we cannot handle
8749 * here, call machine specific function for all the expansion. This
8750 * avoids starting the shell for each argument separately.
8751 * For `=expr` do use the internal function.
8752 */
8753 for (i = 0; i < num_pat; i++)
8754 {
8755 if (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL
8756# ifdef VIM_BACKTICK
8757 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
8758# endif
8759 )
8760 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
8761 }
8762#endif
8763
8764 recursive = TRUE;
8765
8766 /*
8767 * The matching file names are stored in a growarray. Init it empty.
8768 */
8769 ga_init2(&ga, (int)sizeof(char_u *), 30);
8770
8771 for (i = 0; i < num_pat; ++i)
8772 {
8773 add_pat = -1;
8774 p = pat[i];
8775
8776#ifdef VIM_BACKTICK
8777 if (vim_backtick(p))
8778 add_pat = expand_backtick(&ga, p, flags);
8779 else
8780#endif
8781 {
8782 /*
8783 * First expand environment variables, "~/" and "~user/".
8784 */
8785 if (vim_strpbrk(p, (char_u *)"$~") != NULL)
8786 {
8787 p = expand_env_save(p);
8788 if (p == NULL)
8789 p = pat[i];
8790#ifdef UNIX
8791 /*
8792 * On Unix, if expand_env() can't expand an environment
8793 * variable, use the shell to do that. Discard previously
8794 * found file names and start all over again.
8795 */
8796 else if (vim_strpbrk(p, (char_u *)"$~") != NULL)
8797 {
8798 vim_free(p);
8799 ga_clear(&ga);
8800 i = mch_expand_wildcards(num_pat, pat, num_file, file,
8801 flags);
8802 recursive = FALSE;
8803 return i;
8804 }
8805#endif
8806 }
8807
8808 /*
8809 * If there are wildcards: Expand file names and add each match to
8810 * the list. If there is no match, and EW_NOTFOUND is given, add
8811 * the pattern.
8812 * If there are no wildcards: Add the file name if it exists or
8813 * when EW_NOTFOUND is given.
8814 */
8815 if (mch_has_exp_wildcard(p))
8816 add_pat = mch_expandpath(&ga, p, flags);
8817 }
8818
8819 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
8820 {
8821 char_u *t = backslash_halve_save(p);
8822
8823#if defined(MACOS_CLASSIC)
8824 slash_to_colon(t);
8825#endif
8826 /* When EW_NOTFOUND is used, always add files and dirs. Makes
8827 * "vim c:/" work. */
8828 if (flags & EW_NOTFOUND)
8829 addfile(&ga, t, flags | EW_DIR | EW_FILE);
8830 else if (mch_getperm(t) >= 0)
8831 addfile(&ga, t, flags);
8832 vim_free(t);
8833 }
8834
8835 if (p != pat[i])
8836 vim_free(p);
8837 }
8838
8839 *num_file = ga.ga_len;
8840 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
8841
8842 recursive = FALSE;
8843
8844 return (ga.ga_data != NULL) ? OK : FAIL;
8845}
8846
8847# ifdef VIM_BACKTICK
8848
8849/*
8850 * Return TRUE if we can expand this backtick thing here.
8851 */
8852 static int
8853vim_backtick(p)
8854 char_u *p;
8855{
8856 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
8857}
8858
8859/*
8860 * Expand an item in `backticks` by executing it as a command.
8861 * Currently only works when pat[] starts and ends with a `.
8862 * Returns number of file names found.
8863 */
8864 static int
8865expand_backtick(gap, pat, flags)
8866 garray_T *gap;
8867 char_u *pat;
8868 int flags; /* EW_* flags */
8869{
8870 char_u *p;
8871 char_u *cmd;
8872 char_u *buffer;
8873 int cnt = 0;
8874 int i;
8875
8876 /* Create the command: lop off the backticks. */
8877 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
8878 if (cmd == NULL)
8879 return 0;
8880
8881#ifdef FEAT_EVAL
8882 if (*cmd == '=') /* `={expr}`: Expand expression */
8883 buffer = eval_to_string(cmd + 1, &p);
8884 else
8885#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008886 buffer = get_cmd_output(cmd, NULL,
8887 (flags & EW_SILENT) ? SHELL_SILENT : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 vim_free(cmd);
8889 if (buffer == NULL)
8890 return 0;
8891
8892 cmd = buffer;
8893 while (*cmd != NUL)
8894 {
8895 cmd = skipwhite(cmd); /* skip over white space */
8896 p = cmd;
8897 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
8898 ++p;
8899 /* add an entry if it is not empty */
8900 if (p > cmd)
8901 {
8902 i = *p;
8903 *p = NUL;
8904 addfile(gap, cmd, flags);
8905 *p = i;
8906 ++cnt;
8907 }
8908 cmd = p;
8909 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
8910 ++cmd;
8911 }
8912
8913 vim_free(buffer);
8914 return cnt;
8915}
8916# endif /* VIM_BACKTICK */
8917
8918/*
8919 * Add a file to a file list. Accepted flags:
8920 * EW_DIR add directories
8921 * EW_FILE add files
8922 * EW_NOTFOUND add even when it doesn't exist
8923 * EW_ADDSLASH add slash after directory name
8924 */
8925 void
8926addfile(gap, f, flags)
8927 garray_T *gap;
8928 char_u *f; /* filename */
8929 int flags;
8930{
8931 char_u *p;
8932 int isdir;
8933
8934 /* if the file/dir doesn't exist, may not add it */
8935 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
8936 return;
8937
8938#ifdef FNAME_ILLEGAL
8939 /* if the file/dir contains illegal characters, don't add it */
8940 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
8941 return;
8942#endif
8943
8944 isdir = mch_isdir(f);
8945 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
8946 return;
8947
8948 /* Make room for another item in the file list. */
8949 if (ga_grow(gap, 1) == FAIL)
8950 return;
8951
8952 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
8953 if (p == NULL)
8954 return;
8955
8956 STRCPY(p, f);
8957#ifdef BACKSLASH_IN_FILENAME
8958 slash_adjust(p);
8959#endif
8960 /*
8961 * Append a slash or backslash after directory names if none is present.
8962 */
8963#ifndef DONT_ADD_PATHSEP_TO_DIR
8964 if (isdir && (flags & EW_ADDSLASH))
8965 add_pathsep(p);
8966#endif
8967 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968}
8969#endif /* !NO_EXPANDPATH */
8970
8971#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
8972
8973#ifndef SEEK_SET
8974# define SEEK_SET 0
8975#endif
8976#ifndef SEEK_END
8977# define SEEK_END 2
8978#endif
8979
8980/*
8981 * Get the stdout of an external command.
8982 * Returns an allocated string, or NULL for error.
8983 */
8984 char_u *
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008985get_cmd_output(cmd, infile, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008987 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 int flags; /* can be SHELL_SILENT */
8989{
8990 char_u *tempname;
8991 char_u *command;
8992 char_u *buffer = NULL;
8993 int len;
8994 int i = 0;
8995 FILE *fd;
8996
8997 if (check_restricted() || check_secure())
8998 return NULL;
8999
9000 /* get a name for the temp file */
9001 if ((tempname = vim_tempname('o')) == NULL)
9002 {
9003 EMSG(_(e_notmp));
9004 return NULL;
9005 }
9006
9007 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009008 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 if (command == NULL)
9010 goto done;
9011
9012 /*
9013 * Call the shell to execute the command (errors are ignored).
9014 * Don't check timestamps here.
9015 */
9016 ++no_check_timestamps;
9017 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
9018 --no_check_timestamps;
9019
9020 vim_free(command);
9021
9022 /*
9023 * read the names from the file into memory
9024 */
9025# ifdef VMS
9026 /* created temporary file is not allways readable as binary */
9027 fd = mch_fopen((char *)tempname, "r");
9028# else
9029 fd = mch_fopen((char *)tempname, READBIN);
9030# endif
9031
9032 if (fd == NULL)
9033 {
9034 EMSG2(_(e_notopen), tempname);
9035 goto done;
9036 }
9037
9038 fseek(fd, 0L, SEEK_END);
9039 len = ftell(fd); /* get size of temp file */
9040 fseek(fd, 0L, SEEK_SET);
9041
9042 buffer = alloc(len + 1);
9043 if (buffer != NULL)
9044 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
9045 fclose(fd);
9046 mch_remove(tempname);
9047 if (buffer == NULL)
9048 goto done;
9049#ifdef VMS
9050 len = i; /* VMS doesn't give us what we asked for... */
9051#endif
9052 if (i != len)
9053 {
9054 EMSG2(_(e_notread), tempname);
9055 vim_free(buffer);
9056 buffer = NULL;
9057 }
9058 else
9059 buffer[len] = '\0'; /* make sure the buffer is terminated */
9060
9061done:
9062 vim_free(tempname);
9063 return buffer;
9064}
9065#endif
9066
9067/*
9068 * Free the list of files returned by expand_wildcards() or other expansion
9069 * functions.
9070 */
9071 void
9072FreeWild(count, files)
9073 int count;
9074 char_u **files;
9075{
9076 if (files == NULL || count <= 0)
9077 return;
9078#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
9079 /*
9080 * Is this still OK for when other functions than expand_wildcards() have
9081 * been used???
9082 */
9083 _fnexplodefree((char **)files);
9084#else
9085 while (count--)
9086 vim_free(files[count]);
9087 vim_free(files);
9088#endif
9089}
9090
9091/*
9092 * return TRUE when need to go to Insert mode because of 'insertmode'.
9093 * Don't do this when still processing a command or a mapping.
9094 * Don't do this when inside a ":normal" command.
9095 */
9096 int
9097goto_im()
9098{
9099 return (p_im && stuff_empty() && typebuf_typed());
9100}