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