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