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