Mercurial > vim
annotate src/misc1.c @ 4221:dd4f3ceb5d86 v7.3.862
updated for version 7.3.862
Problem: Dragging the status line can be slow.
Solution: Look ahead and drop the drag event if there is a next one.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sat, 16 Mar 2013 14:20:51 +0100 |
parents | 6604853dd822 |
children | a82ef37d38c0 |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * misc1.c: functions that didn't seem to fit elsewhere | |
12 */ | |
13 | |
14 #include "vim.h" | |
15 #include "version.h" | |
16 | |
17 static char_u *vim_version_dir __ARGS((char_u *vimdir)); | |
18 static char_u *remove_tail __ARGS((char_u *p, char_u *pend, char_u *name)); | |
19 static int copy_indent __ARGS((int size, char_u *src)); | |
20 | |
3744 | 21 /* All user names (for ~user completion as done by shell). */ |
22 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
23 static garray_T ga_users; | |
24 #endif | |
25 | |
7 | 26 /* |
27 * Count the size (in window cells) of the indent in the current line. | |
28 */ | |
29 int | |
30 get_indent() | |
31 { | |
32 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts); | |
33 } | |
34 | |
35 /* | |
36 * Count the size (in window cells) of the indent in line "lnum". | |
37 */ | |
38 int | |
39 get_indent_lnum(lnum) | |
40 linenr_T lnum; | |
41 { | |
42 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts); | |
43 } | |
44 | |
45 #if defined(FEAT_FOLDING) || defined(PROTO) | |
46 /* | |
47 * Count the size (in window cells) of the indent in line "lnum" of buffer | |
48 * "buf". | |
49 */ | |
50 int | |
51 get_indent_buf(buf, lnum) | |
52 buf_T *buf; | |
53 linenr_T lnum; | |
54 { | |
55 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts); | |
56 } | |
57 #endif | |
58 | |
59 /* | |
60 * count the size (in window cells) of the indent in line "ptr", with | |
61 * 'tabstop' at "ts" | |
62 */ | |
164 | 63 int |
7 | 64 get_indent_str(ptr, ts) |
65 char_u *ptr; | |
66 int ts; | |
67 { | |
68 int count = 0; | |
69 | |
70 for ( ; *ptr; ++ptr) | |
71 { | |
72 if (*ptr == TAB) /* count a tab for what it is worth */ | |
73 count += ts - (count % ts); | |
74 else if (*ptr == ' ') | |
75 ++count; /* count a space for one */ | |
76 else | |
77 break; | |
78 } | |
164 | 79 return count; |
7 | 80 } |
81 | |
82 /* | |
83 * Set the indent of the current line. | |
84 * Leaves the cursor on the first non-blank in the line. | |
85 * Caller must take care of undo. | |
86 * "flags": | |
87 * SIN_CHANGED: call changed_bytes() if the line was changed. | |
88 * SIN_INSERT: insert the indent in front of the line. | |
89 * SIN_UNDO: save line for undo before changing it. | |
90 * Returns TRUE if the line was changed. | |
91 */ | |
92 int | |
93 set_indent(size, flags) | |
1324 | 94 int size; /* measured in spaces */ |
7 | 95 int flags; |
96 { | |
97 char_u *p; | |
98 char_u *newline; | |
99 char_u *oldline; | |
100 char_u *s; | |
101 int todo; | |
1324 | 102 int ind_len; /* measured in characters */ |
7 | 103 int line_len; |
104 int doit = FALSE; | |
1324 | 105 int ind_done = 0; /* measured in spaces */ |
7 | 106 int tab_pad; |
217 | 107 int retval = FALSE; |
1359 | 108 int orig_char_len = -1; /* number of initial whitespace chars when |
1324 | 109 'et' and 'pi' are both set */ |
7 | 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 | |
1324 | 122 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
123 * 'preserveindent' are set count the number of characters at the | |
124 * beginning of the line to be copied */ | |
125 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) | |
7 | 126 { |
127 /* If 'preserveindent' is set then reuse as much as possible of | |
128 * the existing indent structure for the new indent */ | |
129 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
130 { | |
131 ind_done = 0; | |
132 | |
133 /* count as many characters as we can use */ | |
134 while (todo > 0 && vim_iswhite(*p)) | |
135 { | |
136 if (*p == TAB) | |
137 { | |
138 tab_pad = (int)curbuf->b_p_ts | |
139 - (ind_done % (int)curbuf->b_p_ts); | |
140 /* stop if this tab will overshoot the target */ | |
141 if (todo < tab_pad) | |
142 break; | |
143 todo -= tab_pad; | |
144 ++ind_len; | |
145 ind_done += tab_pad; | |
146 } | |
147 else | |
148 { | |
149 --todo; | |
150 ++ind_len; | |
151 ++ind_done; | |
152 } | |
153 ++p; | |
154 } | |
155 | |
1324 | 156 /* Set initial number of whitespace chars to copy if we are |
157 * preserving indent but expandtab is set */ | |
158 if (curbuf->b_p_et) | |
159 orig_char_len = ind_len; | |
160 | |
7 | 161 /* Fill to next tabstop with a tab, if possible */ |
162 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
1359 | 163 if (todo >= tab_pad && orig_char_len == -1) |
7 | 164 { |
165 doit = TRUE; | |
166 todo -= tab_pad; | |
167 ++ind_len; | |
168 /* ind_done += tab_pad; */ | |
169 } | |
170 } | |
171 | |
172 /* count tabs required for indent */ | |
173 while (todo >= (int)curbuf->b_p_ts) | |
174 { | |
175 if (*p != TAB) | |
176 doit = TRUE; | |
177 else | |
178 ++p; | |
179 todo -= (int)curbuf->b_p_ts; | |
180 ++ind_len; | |
181 /* ind_done += (int)curbuf->b_p_ts; */ | |
182 } | |
183 } | |
184 /* count spaces required for indent */ | |
185 while (todo > 0) | |
186 { | |
187 if (*p != ' ') | |
188 doit = TRUE; | |
189 else | |
190 ++p; | |
191 --todo; | |
192 ++ind_len; | |
193 /* ++ind_done; */ | |
194 } | |
195 | |
196 /* Return if the indent is OK already. */ | |
197 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT)) | |
198 return FALSE; | |
199 | |
200 /* Allocate memory for the new line. */ | |
201 if (flags & SIN_INSERT) | |
202 p = oldline; | |
203 else | |
204 p = skipwhite(p); | |
205 line_len = (int)STRLEN(p) + 1; | |
1324 | 206 |
207 /* If 'preserveindent' and 'expandtab' are both set keep the original | |
208 * characters and allocate accordingly. We will fill the rest with spaces | |
209 * after the if (!curbuf->b_p_et) below. */ | |
1359 | 210 if (orig_char_len != -1) |
1324 | 211 { |
212 newline = alloc(orig_char_len + size - ind_done + line_len); | |
213 if (newline == NULL) | |
214 return FALSE; | |
1359 | 215 todo = size - ind_done; |
216 ind_len = orig_char_len + todo; /* Set total length of indent in | |
217 * characters, which may have been | |
218 * undercounted until now */ | |
1324 | 219 p = oldline; |
220 s = newline; | |
221 while (orig_char_len > 0) | |
222 { | |
223 *s++ = *p++; | |
224 orig_char_len--; | |
225 } | |
1474 | 226 |
1324 | 227 /* Skip over any additional white space (useful when newindent is less |
228 * than old) */ | |
229 while (vim_iswhite(*p)) | |
1474 | 230 ++p; |
1348 | 231 |
1324 | 232 } |
233 else | |
234 { | |
235 todo = size; | |
236 newline = alloc(ind_len + line_len); | |
237 if (newline == NULL) | |
238 return FALSE; | |
239 s = newline; | |
240 } | |
7 | 241 |
242 /* Put the characters in the new line. */ | |
243 /* if 'expandtab' isn't set: use TABs */ | |
244 if (!curbuf->b_p_et) | |
245 { | |
246 /* If 'preserveindent' is set then reuse as much as possible of | |
247 * the existing indent structure for the new indent */ | |
248 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
249 { | |
250 p = oldline; | |
251 ind_done = 0; | |
252 | |
253 while (todo > 0 && vim_iswhite(*p)) | |
254 { | |
255 if (*p == TAB) | |
256 { | |
257 tab_pad = (int)curbuf->b_p_ts | |
258 - (ind_done % (int)curbuf->b_p_ts); | |
259 /* stop if this tab will overshoot the target */ | |
260 if (todo < tab_pad) | |
261 break; | |
262 todo -= tab_pad; | |
263 ind_done += tab_pad; | |
264 } | |
265 else | |
266 { | |
267 --todo; | |
268 ++ind_done; | |
269 } | |
270 *s++ = *p++; | |
271 } | |
272 | |
273 /* Fill to next tabstop with a tab, if possible */ | |
274 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
275 if (todo >= tab_pad) | |
276 { | |
277 *s++ = TAB; | |
278 todo -= tab_pad; | |
279 } | |
280 | |
281 p = skipwhite(p); | |
282 } | |
283 | |
284 while (todo >= (int)curbuf->b_p_ts) | |
285 { | |
286 *s++ = TAB; | |
287 todo -= (int)curbuf->b_p_ts; | |
288 } | |
289 } | |
290 while (todo > 0) | |
291 { | |
292 *s++ = ' '; | |
293 --todo; | |
294 } | |
295 mch_memmove(s, p, (size_t)line_len); | |
296 | |
297 /* Replace the line (unless undo fails). */ | |
298 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) | |
299 { | |
300 ml_replace(curwin->w_cursor.lnum, newline, FALSE); | |
301 if (flags & SIN_CHANGED) | |
302 changed_bytes(curwin->w_cursor.lnum, 0); | |
303 /* Correct saved cursor position if it's after the indent. */ | |
304 if (saved_cursor.lnum == curwin->w_cursor.lnum | |
305 && saved_cursor.col >= (colnr_T)(p - oldline)) | |
835 | 306 saved_cursor.col += ind_len - (colnr_T)(p - oldline); |
217 | 307 retval = TRUE; |
7 | 308 } |
309 else | |
310 vim_free(newline); | |
311 | |
312 curwin->w_cursor.col = ind_len; | |
217 | 313 return retval; |
7 | 314 } |
315 | |
316 /* | |
317 * Copy the indent from ptr to the current line (and fill to size) | |
318 * Leaves the cursor on the first non-blank in the line. | |
319 * Returns TRUE if the line was changed. | |
320 */ | |
321 static int | |
322 copy_indent(size, src) | |
323 int size; | |
324 char_u *src; | |
325 { | |
326 char_u *p = NULL; | |
327 char_u *line = NULL; | |
328 char_u *s; | |
329 int todo; | |
330 int ind_len; | |
331 int line_len = 0; | |
332 int tab_pad; | |
333 int ind_done; | |
334 int round; | |
335 | |
336 /* Round 1: compute the number of characters needed for the indent | |
337 * Round 2: copy the characters. */ | |
338 for (round = 1; round <= 2; ++round) | |
339 { | |
340 todo = size; | |
341 ind_len = 0; | |
342 ind_done = 0; | |
343 s = src; | |
344 | |
345 /* Count/copy the usable portion of the source line */ | |
346 while (todo > 0 && vim_iswhite(*s)) | |
347 { | |
348 if (*s == TAB) | |
349 { | |
350 tab_pad = (int)curbuf->b_p_ts | |
351 - (ind_done % (int)curbuf->b_p_ts); | |
352 /* Stop if this tab will overshoot the target */ | |
353 if (todo < tab_pad) | |
354 break; | |
355 todo -= tab_pad; | |
356 ind_done += tab_pad; | |
357 } | |
358 else | |
359 { | |
360 --todo; | |
361 ++ind_done; | |
362 } | |
363 ++ind_len; | |
840 | 364 if (p != NULL) |
7 | 365 *p++ = *s; |
366 ++s; | |
367 } | |
368 | |
369 /* Fill to next tabstop with a tab, if possible */ | |
370 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
3058 | 371 if (todo >= tab_pad && !curbuf->b_p_et) |
7 | 372 { |
373 todo -= tab_pad; | |
374 ++ind_len; | |
840 | 375 if (p != NULL) |
7 | 376 *p++ = TAB; |
377 } | |
378 | |
379 /* Add tabs required for indent */ | |
3058 | 380 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et) |
7 | 381 { |
382 todo -= (int)curbuf->b_p_ts; | |
383 ++ind_len; | |
840 | 384 if (p != NULL) |
7 | 385 *p++ = TAB; |
386 } | |
387 | |
388 /* Count/add spaces required for indent */ | |
389 while (todo > 0) | |
390 { | |
391 --todo; | |
392 ++ind_len; | |
840 | 393 if (p != NULL) |
7 | 394 *p++ = ' '; |
395 } | |
396 | |
840 | 397 if (p == NULL) |
7 | 398 { |
399 /* Allocate memory for the result: the copied indent, new indent | |
400 * and the rest of the line. */ | |
401 line_len = (int)STRLEN(ml_get_curline()) + 1; | |
402 line = alloc(ind_len + line_len); | |
403 if (line == NULL) | |
404 return FALSE; | |
405 p = line; | |
406 } | |
407 } | |
408 | |
409 /* Append the original line */ | |
410 mch_memmove(p, ml_get_curline(), (size_t)line_len); | |
411 | |
412 /* Replace the line */ | |
413 ml_replace(curwin->w_cursor.lnum, line, FALSE); | |
414 | |
415 /* Put the cursor after the indent. */ | |
416 curwin->w_cursor.col = ind_len; | |
417 return TRUE; | |
418 } | |
419 | |
420 /* | |
421 * Return the indent of the current line after a number. Return -1 if no | |
422 * number was found. Used for 'n' in 'formatoptions': numbered list. | |
41 | 423 * Since a pattern is used it can actually handle more than numbers. |
7 | 424 */ |
425 int | |
426 get_number_indent(lnum) | |
427 linenr_T lnum; | |
428 { | |
429 colnr_T col; | |
430 pos_T pos; | |
431 | |
3632 | 432 regmatch_T regmatch; |
433 int lead_len = 0; /* length of comment leader */ | |
434 | |
7 | 435 if (lnum > curbuf->b_ml.ml_line_count) |
436 return -1; | |
41 | 437 pos.lnum = 0; |
3584 | 438 |
439 #ifdef FEAT_COMMENTS | |
3632 | 440 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */ |
441 if ((State & INSERT) || has_format_option(FO_Q_COMS)) | |
3584 | 442 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); |
3632 | 443 #endif |
444 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); | |
445 if (regmatch.regprog != NULL) | |
446 { | |
447 regmatch.rm_ic = FALSE; | |
448 | |
449 /* vim_regexec() expects a pointer to a line. This lets us | |
450 * start matching for the flp beyond any comment leader... */ | |
451 if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) | |
452 { | |
453 pos.lnum = lnum; | |
454 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); | |
41 | 455 #ifdef FEAT_VIRTUALEDIT |
3632 | 456 pos.coladd = 0; |
457 #endif | |
458 } | |
459 } | |
460 vim_free(regmatch.regprog); | |
41 | 461 |
462 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) | |
7 | 463 return -1; |
464 getvcol(curwin, &pos, &col, NULL, NULL); | |
465 return (int)col; | |
466 } | |
467 | |
468 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) | |
469 | |
470 static int cin_is_cinword __ARGS((char_u *line)); | |
471 | |
472 /* | |
473 * Return TRUE if the string "line" starts with a word from 'cinwords'. | |
474 */ | |
475 static int | |
476 cin_is_cinword(line) | |
477 char_u *line; | |
478 { | |
479 char_u *cinw; | |
480 char_u *cinw_buf; | |
481 int cinw_len; | |
482 int retval = FALSE; | |
483 int len; | |
484 | |
485 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; | |
486 cinw_buf = alloc((unsigned)cinw_len); | |
487 if (cinw_buf != NULL) | |
488 { | |
489 line = skipwhite(line); | |
490 for (cinw = curbuf->b_p_cinw; *cinw; ) | |
491 { | |
492 len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); | |
493 if (STRNCMP(line, cinw_buf, len) == 0 | |
494 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) | |
495 { | |
496 retval = TRUE; | |
497 break; | |
498 } | |
499 } | |
500 vim_free(cinw_buf); | |
501 } | |
502 return retval; | |
503 } | |
504 #endif | |
505 | |
506 /* | |
507 * open_line: Add a new line below or above the current line. | |
508 * | |
509 * For VREPLACE mode, we only add a new line when we get to the end of the | |
510 * file, otherwise we just start replacing the next line. | |
511 * | |
512 * Caller must take care of undo. Since VREPLACE may affect any number of | |
513 * lines however, it may call u_save_cursor() again when starting to change a | |
514 * new line. | |
515 * "flags": OPENLINE_DELSPACES delete spaces after cursor | |
516 * OPENLINE_DO_COM format comments | |
517 * OPENLINE_KEEPTRAIL keep trailing spaces | |
518 * OPENLINE_MARKFIX adjust mark positions after the line break | |
3584 | 519 * OPENLINE_COM_LIST format comments with list or 2nd line indent |
520 * | |
521 * "second_line_indent": indent for after ^^D in Insert mode or if flag | |
522 * OPENLINE_COM_LIST | |
7 | 523 * |
524 * Return TRUE for success, FALSE for failure | |
525 */ | |
526 int | |
3584 | 527 open_line(dir, flags, second_line_indent) |
7 | 528 int dir; /* FORWARD or BACKWARD */ |
529 int flags; | |
3584 | 530 int second_line_indent; |
7 | 531 { |
532 char_u *saved_line; /* copy of the original line */ | |
533 char_u *next_line = NULL; /* copy of the next line */ | |
534 char_u *p_extra = NULL; /* what goes to next line */ | |
535 int less_cols = 0; /* less columns for mark in new line */ | |
536 int less_cols_off = 0; /* columns to skip for mark adjust */ | |
537 pos_T old_cursor; /* old cursor position */ | |
538 int newcol = 0; /* new cursor column */ | |
539 int newindent = 0; /* auto-indent of the new line */ | |
540 int n; | |
541 int trunc_line = FALSE; /* truncate current line afterwards */ | |
542 int retval = FALSE; /* return value, default is FAIL */ | |
543 #ifdef FEAT_COMMENTS | |
544 int extra_len = 0; /* length of p_extra string */ | |
545 int lead_len; /* length of comment leader */ | |
546 char_u *lead_flags; /* position in 'comments' for comment leader */ | |
547 char_u *leader = NULL; /* copy of comment leader */ | |
548 #endif | |
549 char_u *allocated = NULL; /* allocated memory */ | |
550 #if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \ | |
551 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS) | |
552 char_u *p; | |
553 #endif | |
554 int saved_char = NUL; /* init for GCC */ | |
555 #if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS) | |
556 pos_T *pos; | |
557 #endif | |
558 #ifdef FEAT_SMARTINDENT | |
559 int do_si = (!p_paste && curbuf->b_p_si | |
560 # ifdef FEAT_CINDENT | |
561 && !curbuf->b_p_cin | |
562 # endif | |
563 ); | |
564 int no_si = FALSE; /* reset did_si afterwards */ | |
565 int first_char = NUL; /* init for GCC */ | |
566 #endif | |
567 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
568 int vreplace_mode; | |
569 #endif | |
570 int did_append; /* appended a new line */ | |
571 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ | |
572 | |
573 /* | |
574 * make a copy of the current line so we can mess with it | |
575 */ | |
576 saved_line = vim_strsave(ml_get_curline()); | |
577 if (saved_line == NULL) /* out of memory! */ | |
578 return FALSE; | |
579 | |
580 #ifdef FEAT_VREPLACE | |
581 if (State & VREPLACE_FLAG) | |
582 { | |
583 /* | |
584 * With VREPLACE we make a copy of the next line, which we will be | |
585 * starting to replace. First make the new line empty and let vim play | |
586 * with the indenting and comment leader to its heart's content. Then | |
587 * we grab what it ended up putting on the new line, put back the | |
588 * original line, and call ins_char() to put each new character onto | |
589 * the line, replacing what was there before and pushing the right | |
590 * stuff onto the replace stack. -- webb. | |
591 */ | |
592 if (curwin->w_cursor.lnum < orig_line_count) | |
593 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); | |
594 else | |
595 next_line = vim_strsave((char_u *)""); | |
596 if (next_line == NULL) /* out of memory! */ | |
597 goto theend; | |
598 | |
599 /* | |
600 * In VREPLACE mode, a NL replaces the rest of the line, and starts | |
601 * replacing the next line, so push all of the characters left on the | |
602 * line onto the replace stack. We'll push any other characters that | |
603 * might be replaced at the start of the next line (due to autoindent | |
604 * etc) a bit later. | |
605 */ | |
606 replace_push(NUL); /* Call twice because BS over NL expects it */ | |
607 replace_push(NUL); | |
608 p = saved_line + curwin->w_cursor.col; | |
609 while (*p != NUL) | |
1470 | 610 { |
611 #ifdef FEAT_MBYTE | |
612 if (has_mbyte) | |
613 p += replace_push_mb(p); | |
614 else | |
615 #endif | |
616 replace_push(*p++); | |
617 } | |
7 | 618 saved_line[curwin->w_cursor.col] = NUL; |
619 } | |
620 #endif | |
621 | |
622 if ((State & INSERT) | |
623 #ifdef FEAT_VREPLACE | |
624 && !(State & VREPLACE_FLAG) | |
625 #endif | |
626 ) | |
627 { | |
628 p_extra = saved_line + curwin->w_cursor.col; | |
629 #ifdef FEAT_SMARTINDENT | |
630 if (do_si) /* need first char after new line break */ | |
631 { | |
632 p = skipwhite(p_extra); | |
633 first_char = *p; | |
634 } | |
635 #endif | |
636 #ifdef FEAT_COMMENTS | |
637 extra_len = (int)STRLEN(p_extra); | |
638 #endif | |
639 saved_char = *p_extra; | |
640 *p_extra = NUL; | |
641 } | |
642 | |
643 u_clearline(); /* cannot do "U" command when adding lines */ | |
644 #ifdef FEAT_SMARTINDENT | |
645 did_si = FALSE; | |
646 #endif | |
647 ai_col = 0; | |
648 | |
649 /* | |
650 * If we just did an auto-indent, then we didn't type anything on | |
651 * the prior line, and it should be truncated. Do this even if 'ai' is not | |
652 * set because automatically inserting a comment leader also sets did_ai. | |
653 */ | |
654 if (dir == FORWARD && did_ai) | |
655 trunc_line = TRUE; | |
656 | |
657 /* | |
658 * If 'autoindent' and/or 'smartindent' is set, try to figure out what | |
659 * indent to use for the new line. | |
660 */ | |
661 if (curbuf->b_p_ai | |
662 #ifdef FEAT_SMARTINDENT | |
663 || do_si | |
664 #endif | |
665 ) | |
666 { | |
667 /* | |
668 * count white space on current line | |
669 */ | |
670 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts); | |
3584 | 671 if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
672 newindent = second_line_indent; /* for ^^D command in insert mode */ | |
7 | 673 |
674 #ifdef FEAT_SMARTINDENT | |
675 /* | |
676 * Do smart indenting. | |
677 * In insert/replace mode (only when dir == FORWARD) | |
678 * we may move some text to the next line. If it starts with '{' | |
679 * don't add an indent. Fixes inserting a NL before '{' in line | |
680 * "if (condition) {" | |
681 */ | |
682 if (!trunc_line && do_si && *saved_line != NUL | |
683 && (p_extra == NULL || first_char != '{')) | |
684 { | |
685 char_u *ptr; | |
686 char_u last_char; | |
687 | |
688 old_cursor = curwin->w_cursor; | |
689 ptr = saved_line; | |
690 # ifdef FEAT_COMMENTS | |
691 if (flags & OPENLINE_DO_COM) | |
3562 | 692 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
7 | 693 else |
694 lead_len = 0; | |
695 # endif | |
696 if (dir == FORWARD) | |
697 { | |
698 /* | |
699 * Skip preprocessor directives, unless they are | |
700 * recognised as comments. | |
701 */ | |
702 if ( | |
703 # ifdef FEAT_COMMENTS | |
704 lead_len == 0 && | |
705 # endif | |
706 ptr[0] == '#') | |
707 { | |
708 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) | |
709 ptr = ml_get(--curwin->w_cursor.lnum); | |
710 newindent = get_indent(); | |
711 } | |
712 # ifdef FEAT_COMMENTS | |
713 if (flags & OPENLINE_DO_COM) | |
3562 | 714 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
7 | 715 else |
716 lead_len = 0; | |
717 if (lead_len > 0) | |
718 { | |
719 /* | |
720 * This case gets the following right: | |
721 * \* | |
722 * * A comment (read '\' as '/'). | |
723 * *\ | |
724 * #define IN_THE_WAY | |
725 * This should line up here; | |
726 */ | |
727 p = skipwhite(ptr); | |
728 if (p[0] == '/' && p[1] == '*') | |
729 p++; | |
730 if (p[0] == '*') | |
731 { | |
732 for (p++; *p; p++) | |
733 { | |
734 if (p[0] == '/' && p[-1] == '*') | |
735 { | |
736 /* | |
737 * End of C comment, indent should line up | |
738 * with the line containing the start of | |
739 * the comment | |
740 */ | |
741 curwin->w_cursor.col = (colnr_T)(p - ptr); | |
742 if ((pos = findmatch(NULL, NUL)) != NULL) | |
743 { | |
744 curwin->w_cursor.lnum = pos->lnum; | |
745 newindent = get_indent(); | |
746 } | |
747 } | |
748 } | |
749 } | |
750 } | |
751 else /* Not a comment line */ | |
752 # endif | |
753 { | |
754 /* Find last non-blank in line */ | |
755 p = ptr + STRLEN(ptr) - 1; | |
756 while (p > ptr && vim_iswhite(*p)) | |
757 --p; | |
758 last_char = *p; | |
759 | |
760 /* | |
761 * find the character just before the '{' or ';' | |
762 */ | |
763 if (last_char == '{' || last_char == ';') | |
764 { | |
765 if (p > ptr) | |
766 --p; | |
767 while (p > ptr && vim_iswhite(*p)) | |
768 --p; | |
769 } | |
770 /* | |
771 * Try to catch lines that are split over multiple | |
772 * lines. eg: | |
773 * if (condition && | |
774 * condition) { | |
775 * Should line up here! | |
776 * } | |
777 */ | |
778 if (*p == ')') | |
779 { | |
780 curwin->w_cursor.col = (colnr_T)(p - ptr); | |
781 if ((pos = findmatch(NULL, '(')) != NULL) | |
782 { | |
783 curwin->w_cursor.lnum = pos->lnum; | |
784 newindent = get_indent(); | |
785 ptr = ml_get_curline(); | |
786 } | |
787 } | |
788 /* | |
789 * If last character is '{' do indent, without | |
790 * checking for "if" and the like. | |
791 */ | |
792 if (last_char == '{') | |
793 { | |
794 did_si = TRUE; /* do indent */ | |
795 no_si = TRUE; /* don't delete it when '{' typed */ | |
796 } | |
797 /* | |
798 * Look for "if" and the like, use 'cinwords'. | |
799 * Don't do this if the previous line ended in ';' or | |
800 * '}'. | |
801 */ | |
802 else if (last_char != ';' && last_char != '}' | |
803 && cin_is_cinword(ptr)) | |
804 did_si = TRUE; | |
805 } | |
806 } | |
807 else /* dir == BACKWARD */ | |
808 { | |
809 /* | |
810 * Skip preprocessor directives, unless they are | |
811 * recognised as comments. | |
812 */ | |
813 if ( | |
814 # ifdef FEAT_COMMENTS | |
815 lead_len == 0 && | |
816 # endif | |
817 ptr[0] == '#') | |
818 { | |
819 int was_backslashed = FALSE; | |
820 | |
821 while ((ptr[0] == '#' || was_backslashed) && | |
822 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) | |
823 { | |
824 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') | |
825 was_backslashed = TRUE; | |
826 else | |
827 was_backslashed = FALSE; | |
828 ptr = ml_get(++curwin->w_cursor.lnum); | |
829 } | |
830 if (was_backslashed) | |
831 newindent = 0; /* Got to end of file */ | |
832 else | |
833 newindent = get_indent(); | |
834 } | |
835 p = skipwhite(ptr); | |
836 if (*p == '}') /* if line starts with '}': do indent */ | |
837 did_si = TRUE; | |
838 else /* can delete indent when '{' typed */ | |
839 can_si_back = TRUE; | |
840 } | |
841 curwin->w_cursor = old_cursor; | |
842 } | |
843 if (do_si) | |
844 can_si = TRUE; | |
845 #endif /* FEAT_SMARTINDENT */ | |
846 | |
847 did_ai = TRUE; | |
848 } | |
849 | |
850 #ifdef FEAT_COMMENTS | |
851 /* | |
852 * Find out if the current line starts with a comment leader. | |
853 * This may then be inserted in front of the new line. | |
854 */ | |
855 end_comment_pending = NUL; | |
856 if (flags & OPENLINE_DO_COM) | |
3562 | 857 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE); |
7 | 858 else |
859 lead_len = 0; | |
860 if (lead_len > 0) | |
861 { | |
862 char_u *lead_repl = NULL; /* replaces comment leader */ | |
863 int lead_repl_len = 0; /* length of *lead_repl */ | |
864 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ | |
865 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ | |
866 char_u *comment_end = NULL; /* where lead_end has been found */ | |
867 int extra_space = FALSE; /* append extra space */ | |
868 int current_flag; | |
869 int require_blank = FALSE; /* requires blank after middle */ | |
870 char_u *p2; | |
871 | |
872 /* | |
873 * If the comment leader has the start, middle or end flag, it may not | |
874 * be used or may be replaced with the middle leader. | |
875 */ | |
876 for (p = lead_flags; *p && *p != ':'; ++p) | |
877 { | |
878 if (*p == COM_BLANK) | |
879 { | |
880 require_blank = TRUE; | |
881 continue; | |
882 } | |
883 if (*p == COM_START || *p == COM_MIDDLE) | |
884 { | |
885 current_flag = *p; | |
886 if (*p == COM_START) | |
887 { | |
888 /* | |
889 * Doing "O" on a start of comment does not insert leader. | |
890 */ | |
891 if (dir == BACKWARD) | |
892 { | |
893 lead_len = 0; | |
894 break; | |
895 } | |
896 | |
897 /* find start of middle part */ | |
898 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); | |
899 require_blank = FALSE; | |
900 } | |
901 | |
902 /* | |
903 * Isolate the strings of the middle and end leader. | |
904 */ | |
905 while (*p && p[-1] != ':') /* find end of middle flags */ | |
906 { | |
907 if (*p == COM_BLANK) | |
908 require_blank = TRUE; | |
909 ++p; | |
910 } | |
911 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); | |
912 | |
913 while (*p && p[-1] != ':') /* find end of end flags */ | |
914 { | |
915 /* Check whether we allow automatic ending of comments */ | |
916 if (*p == COM_AUTO_END) | |
917 end_comment_pending = -1; /* means we want to set it */ | |
918 ++p; | |
919 } | |
920 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
921 | |
922 if (end_comment_pending == -1) /* we can set it now */ | |
923 end_comment_pending = lead_end[n - 1]; | |
924 | |
925 /* | |
926 * If the end of the comment is in the same line, don't use | |
927 * the comment leader. | |
928 */ | |
929 if (dir == FORWARD) | |
930 { | |
931 for (p = saved_line + lead_len; *p; ++p) | |
932 if (STRNCMP(p, lead_end, n) == 0) | |
933 { | |
934 comment_end = p; | |
935 lead_len = 0; | |
936 break; | |
937 } | |
938 } | |
939 | |
940 /* | |
941 * Doing "o" on a start of comment inserts the middle leader. | |
942 */ | |
943 if (lead_len > 0) | |
944 { | |
945 if (current_flag == COM_START) | |
946 { | |
947 lead_repl = lead_middle; | |
948 lead_repl_len = (int)STRLEN(lead_middle); | |
949 } | |
950 | |
951 /* | |
952 * If we have hit RETURN immediately after the start | |
953 * comment leader, then put a space after the middle | |
954 * comment leader on the next line. | |
955 */ | |
956 if (!vim_iswhite(saved_line[lead_len - 1]) | |
957 && ((p_extra != NULL | |
958 && (int)curwin->w_cursor.col == lead_len) | |
959 || (p_extra == NULL | |
960 && saved_line[lead_len] == NUL) | |
961 || require_blank)) | |
962 extra_space = TRUE; | |
963 } | |
964 break; | |
965 } | |
966 if (*p == COM_END) | |
967 { | |
968 /* | |
969 * Doing "o" on the end of a comment does not insert leader. | |
970 * Remember where the end is, might want to use it to find the | |
971 * start (for C-comments). | |
972 */ | |
973 if (dir == FORWARD) | |
974 { | |
975 comment_end = skipwhite(saved_line); | |
976 lead_len = 0; | |
977 break; | |
978 } | |
979 | |
980 /* | |
981 * Doing "O" on the end of a comment inserts the middle leader. | |
982 * Find the string for the middle leader, searching backwards. | |
983 */ | |
984 while (p > curbuf->b_p_com && *p != ',') | |
985 --p; | |
986 for (lead_repl = p; lead_repl > curbuf->b_p_com | |
987 && lead_repl[-1] != ':'; --lead_repl) | |
988 ; | |
989 lead_repl_len = (int)(p - lead_repl); | |
990 | |
991 /* We can probably always add an extra space when doing "O" on | |
992 * the comment-end */ | |
993 extra_space = TRUE; | |
994 | |
995 /* Check whether we allow automatic ending of comments */ | |
996 for (p2 = p; *p2 && *p2 != ':'; p2++) | |
997 { | |
998 if (*p2 == COM_AUTO_END) | |
999 end_comment_pending = -1; /* means we want to set it */ | |
1000 } | |
1001 if (end_comment_pending == -1) | |
1002 { | |
1003 /* Find last character in end-comment string */ | |
1004 while (*p2 && *p2 != ',') | |
1005 p2++; | |
1006 end_comment_pending = p2[-1]; | |
1007 } | |
1008 break; | |
1009 } | |
1010 if (*p == COM_FIRST) | |
1011 { | |
1012 /* | |
1013 * Comment leader for first line only: Don't repeat leader | |
1014 * when using "O", blank out leader when using "o". | |
1015 */ | |
1016 if (dir == BACKWARD) | |
1017 lead_len = 0; | |
1018 else | |
1019 { | |
1020 lead_repl = (char_u *)""; | |
1021 lead_repl_len = 0; | |
1022 } | |
1023 break; | |
1024 } | |
1025 } | |
1026 if (lead_len) | |
1027 { | |
3596 | 1028 /* allocate buffer (may concatenate p_extra later) */ |
3584 | 1029 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len |
3596 | 1030 + (second_line_indent > 0 ? second_line_indent : 0) + 1); |
7 | 1031 allocated = leader; /* remember to free it later */ |
1032 | |
1033 if (leader == NULL) | |
1034 lead_len = 0; | |
1035 else | |
1036 { | |
419 | 1037 vim_strncpy(leader, saved_line, lead_len); |
7 | 1038 |
1039 /* | |
1040 * Replace leader with lead_repl, right or left adjusted | |
1041 */ | |
1042 if (lead_repl != NULL) | |
1043 { | |
1044 int c = 0; | |
1045 int off = 0; | |
1046 | |
1996 | 1047 for (p = lead_flags; *p != NUL && *p != ':'; ) |
7 | 1048 { |
1049 if (*p == COM_RIGHT || *p == COM_LEFT) | |
1996 | 1050 c = *p++; |
7 | 1051 else if (VIM_ISDIGIT(*p) || *p == '-') |
1052 off = getdigits(&p); | |
1996 | 1053 else |
1054 ++p; | |
7 | 1055 } |
1056 if (c == COM_RIGHT) /* right adjusted leader */ | |
1057 { | |
1058 /* find last non-white in the leader to line up with */ | |
1059 for (p = leader + lead_len - 1; p > leader | |
1060 && vim_iswhite(*p); --p) | |
1061 ; | |
1062 ++p; | |
17 | 1063 |
1064 #ifdef FEAT_MBYTE | |
1065 /* Compute the length of the replaced characters in | |
1066 * screen characters, not bytes. */ | |
1067 { | |
1068 int repl_size = vim_strnsize(lead_repl, | |
1069 lead_repl_len); | |
1070 int old_size = 0; | |
1071 char_u *endp = p; | |
1072 int l; | |
1073 | |
1074 while (old_size < repl_size && p > leader) | |
1075 { | |
39 | 1076 mb_ptr_back(leader, p); |
17 | 1077 old_size += ptr2cells(p); |
1078 } | |
835 | 1079 l = lead_repl_len - (int)(endp - p); |
17 | 1080 if (l != 0) |
1081 mch_memmove(endp + l, endp, | |
1082 (size_t)((leader + lead_len) - endp)); | |
1083 lead_len += l; | |
1084 } | |
1085 #else | |
7 | 1086 if (p < leader + lead_repl_len) |
1087 p = leader; | |
1088 else | |
1089 p -= lead_repl_len; | |
17 | 1090 #endif |
7 | 1091 mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
1092 if (p + lead_repl_len > leader + lead_len) | |
1093 p[lead_repl_len] = NUL; | |
1094 | |
1095 /* blank-out any other chars from the old leader. */ | |
1096 while (--p >= leader) | |
17 | 1097 { |
1098 #ifdef FEAT_MBYTE | |
1099 int l = mb_head_off(leader, p); | |
1100 | |
1101 if (l > 1) | |
1102 { | |
1103 p -= l; | |
1104 if (ptr2cells(p) > 1) | |
1105 { | |
1106 p[1] = ' '; | |
1107 --l; | |
1108 } | |
1109 mch_memmove(p + 1, p + l + 1, | |
1110 (size_t)((leader + lead_len) - (p + l + 1))); | |
1111 lead_len -= l; | |
1112 *p = ' '; | |
1113 } | |
1114 else | |
1115 #endif | |
7 | 1116 if (!vim_iswhite(*p)) |
1117 *p = ' '; | |
17 | 1118 } |
7 | 1119 } |
1120 else /* left adjusted leader */ | |
1121 { | |
1122 p = skipwhite(leader); | |
17 | 1123 #ifdef FEAT_MBYTE |
1124 /* Compute the length of the replaced characters in | |
1125 * screen characters, not bytes. Move the part that is | |
1126 * not to be overwritten. */ | |
1127 { | |
1128 int repl_size = vim_strnsize(lead_repl, | |
1129 lead_repl_len); | |
1130 int i; | |
1131 int l; | |
1132 | |
1133 for (i = 0; p[i] != NUL && i < lead_len; i += l) | |
1134 { | |
474 | 1135 l = (*mb_ptr2len)(p + i); |
17 | 1136 if (vim_strnsize(p, i + l) > repl_size) |
1137 break; | |
1138 } | |
1139 if (i != lead_repl_len) | |
1140 { | |
1141 mch_memmove(p + lead_repl_len, p + i, | |
2002 | 1142 (size_t)(lead_len - i - (p - leader))); |
17 | 1143 lead_len += lead_repl_len - i; |
1144 } | |
1145 } | |
1146 #endif | |
7 | 1147 mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
1148 | |
1149 /* Replace any remaining non-white chars in the old | |
1150 * leader by spaces. Keep Tabs, the indent must | |
1151 * remain the same. */ | |
1152 for (p += lead_repl_len; p < leader + lead_len; ++p) | |
1153 if (!vim_iswhite(*p)) | |
1154 { | |
1155 /* Don't put a space before a TAB. */ | |
1156 if (p + 1 < leader + lead_len && p[1] == TAB) | |
1157 { | |
1158 --lead_len; | |
1159 mch_memmove(p, p + 1, | |
1160 (leader + lead_len) - p); | |
1161 } | |
1162 else | |
17 | 1163 { |
1164 #ifdef FEAT_MBYTE | |
474 | 1165 int l = (*mb_ptr2len)(p); |
17 | 1166 |
1167 if (l > 1) | |
1168 { | |
1169 if (ptr2cells(p) > 1) | |
1170 { | |
1171 /* Replace a double-wide char with | |
1172 * two spaces */ | |
1173 --l; | |
1174 *p++ = ' '; | |
1175 } | |
1176 mch_memmove(p + 1, p + l, | |
1177 (leader + lead_len) - p); | |
1178 lead_len -= l - 1; | |
1179 } | |
1180 #endif | |
7 | 1181 *p = ' '; |
17 | 1182 } |
7 | 1183 } |
1184 *p = NUL; | |
1185 } | |
1186 | |
1187 /* Recompute the indent, it may have changed. */ | |
1188 if (curbuf->b_p_ai | |
1189 #ifdef FEAT_SMARTINDENT | |
1190 || do_si | |
1191 #endif | |
1192 ) | |
1193 newindent = get_indent_str(leader, (int)curbuf->b_p_ts); | |
1194 | |
1195 /* Add the indent offset */ | |
1196 if (newindent + off < 0) | |
1197 { | |
1198 off = -newindent; | |
1199 newindent = 0; | |
1200 } | |
1201 else | |
1202 newindent += off; | |
1203 | |
1204 /* Correct trailing spaces for the shift, so that | |
1205 * alignment remains equal. */ | |
1206 while (off > 0 && lead_len > 0 | |
1207 && leader[lead_len - 1] == ' ') | |
1208 { | |
1209 /* Don't do it when there is a tab before the space */ | |
1210 if (vim_strchr(skipwhite(leader), '\t') != NULL) | |
1211 break; | |
1212 --lead_len; | |
1213 --off; | |
1214 } | |
1215 | |
1216 /* If the leader ends in white space, don't add an | |
1217 * extra space */ | |
1218 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1])) | |
1219 extra_space = FALSE; | |
1220 leader[lead_len] = NUL; | |
1221 } | |
1222 | |
1223 if (extra_space) | |
1224 { | |
1225 leader[lead_len++] = ' '; | |
1226 leader[lead_len] = NUL; | |
1227 } | |
1228 | |
1229 newcol = lead_len; | |
1230 | |
1231 /* | |
1232 * if a new indent will be set below, remove the indent that | |
1233 * is in the comment leader | |
1234 */ | |
1235 if (newindent | |
1236 #ifdef FEAT_SMARTINDENT | |
1237 || did_si | |
1238 #endif | |
1239 ) | |
1240 { | |
1241 while (lead_len && vim_iswhite(*leader)) | |
1242 { | |
1243 --lead_len; | |
1244 --newcol; | |
1245 ++leader; | |
1246 } | |
1247 } | |
1248 | |
1249 } | |
1250 #ifdef FEAT_SMARTINDENT | |
1251 did_si = can_si = FALSE; | |
1252 #endif | |
1253 } | |
1254 else if (comment_end != NULL) | |
1255 { | |
1256 /* | |
1257 * We have finished a comment, so we don't use the leader. | |
1258 * If this was a C-comment and 'ai' or 'si' is set do a normal | |
1259 * indent to align with the line containing the start of the | |
1260 * comment. | |
1261 */ | |
1262 if (comment_end[0] == '*' && comment_end[1] == '/' && | |
1263 (curbuf->b_p_ai | |
1264 #ifdef FEAT_SMARTINDENT | |
1265 || do_si | |
1266 #endif | |
1267 )) | |
1268 { | |
1269 old_cursor = curwin->w_cursor; | |
1270 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); | |
1271 if ((pos = findmatch(NULL, NUL)) != NULL) | |
1272 { | |
1273 curwin->w_cursor.lnum = pos->lnum; | |
1274 newindent = get_indent(); | |
1275 } | |
1276 curwin->w_cursor = old_cursor; | |
1277 } | |
1278 } | |
1279 } | |
1280 #endif | |
1281 | |
1282 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ | |
1283 if (p_extra != NULL) | |
1284 { | |
1285 *p_extra = saved_char; /* restore char that NUL replaced */ | |
1286 | |
1287 /* | |
1288 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first | |
1289 * non-blank. | |
1290 * | |
1291 * When in REPLACE mode, put the deleted blanks on the replace stack, | |
1292 * preceded by a NUL, so they can be put back when a BS is entered. | |
1293 */ | |
1294 if (REPLACE_NORMAL(State)) | |
1295 replace_push(NUL); /* end of extra blanks */ | |
1296 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) | |
1297 { | |
1298 while ((*p_extra == ' ' || *p_extra == '\t') | |
1299 #ifdef FEAT_MBYTE | |
1300 && (!enc_utf8 | |
1301 || !utf_iscomposing(utf_ptr2char(p_extra + 1))) | |
1302 #endif | |
1303 ) | |
1304 { | |
1305 if (REPLACE_NORMAL(State)) | |
1306 replace_push(*p_extra); | |
1307 ++p_extra; | |
1308 ++less_cols_off; | |
1309 } | |
1310 } | |
1311 if (*p_extra != NUL) | |
1312 did_ai = FALSE; /* append some text, don't truncate now */ | |
1313 | |
1314 /* columns for marks adjusted for removed columns */ | |
1315 less_cols = (int)(p_extra - saved_line); | |
1316 } | |
1317 | |
1318 if (p_extra == NULL) | |
1319 p_extra = (char_u *)""; /* append empty line */ | |
1320 | |
1321 #ifdef FEAT_COMMENTS | |
1322 /* concatenate leader and p_extra, if there is a leader */ | |
1323 if (lead_len) | |
1324 { | |
3584 | 1325 if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
1326 { | |
1327 int i; | |
3592 | 1328 int padding = second_line_indent |
1329 - (newindent + (int)STRLEN(leader)); | |
3584 | 1330 |
1331 /* Here whitespace is inserted after the comment char. | |
1332 * Below, set_indent(newindent, SIN_INSERT) will insert the | |
1333 * whitespace needed before the comment char. */ | |
1334 for (i = 0; i < padding; i++) | |
1335 { | |
1336 STRCAT(leader, " "); | |
3705 | 1337 less_cols--; |
3584 | 1338 newcol++; |
1339 } | |
1340 } | |
7 | 1341 STRCAT(leader, p_extra); |
1342 p_extra = leader; | |
1343 did_ai = TRUE; /* So truncating blanks works with comments */ | |
1344 less_cols -= lead_len; | |
1345 } | |
1346 else | |
1347 end_comment_pending = NUL; /* turns out there was no leader */ | |
1348 #endif | |
1349 | |
1350 old_cursor = curwin->w_cursor; | |
1351 if (dir == BACKWARD) | |
1352 --curwin->w_cursor.lnum; | |
1353 #ifdef FEAT_VREPLACE | |
1354 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) | |
1355 #endif | |
1356 { | |
1357 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) | |
1358 == FAIL) | |
1359 goto theend; | |
1360 /* Postpone calling changed_lines(), because it would mess up folding | |
1361 * with markers. */ | |
1362 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); | |
1363 did_append = TRUE; | |
1364 } | |
1365 #ifdef FEAT_VREPLACE | |
1366 else | |
1367 { | |
1368 /* | |
1369 * In VREPLACE mode we are starting to replace the next line. | |
1370 */ | |
1371 curwin->w_cursor.lnum++; | |
1372 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) | |
1373 { | |
1374 /* In case we NL to a new line, BS to the previous one, and NL | |
1375 * again, we don't want to save the new line for undo twice. | |
1376 */ | |
1377 (void)u_save_cursor(); /* errors are ignored! */ | |
1378 vr_lines_changed++; | |
1379 } | |
1380 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); | |
1381 changed_bytes(curwin->w_cursor.lnum, 0); | |
1382 curwin->w_cursor.lnum--; | |
1383 did_append = FALSE; | |
1384 } | |
1385 #endif | |
1386 | |
1387 if (newindent | |
1388 #ifdef FEAT_SMARTINDENT | |
1389 || did_si | |
1390 #endif | |
1391 ) | |
1392 { | |
1393 ++curwin->w_cursor.lnum; | |
1394 #ifdef FEAT_SMARTINDENT | |
1395 if (did_si) | |
1396 { | |
3740 | 1397 int sw = (int)get_sw_value(); |
1398 | |
7 | 1399 if (p_sr) |
3740 | 1400 newindent -= newindent % sw; |
1401 newindent += sw; | |
7 | 1402 } |
1403 #endif | |
1324 | 1404 /* Copy the indent */ |
1405 if (curbuf->b_p_ci) | |
7 | 1406 { |
1407 (void)copy_indent(newindent, saved_line); | |
1408 | |
1409 /* | |
1410 * Set the 'preserveindent' option so that any further screwing | |
1411 * with the line doesn't entirely destroy our efforts to preserve | |
1412 * it. It gets restored at the function end. | |
1413 */ | |
1414 curbuf->b_p_pi = TRUE; | |
1415 } | |
1416 else | |
1417 (void)set_indent(newindent, SIN_INSERT); | |
1418 less_cols -= curwin->w_cursor.col; | |
1419 | |
1420 ai_col = curwin->w_cursor.col; | |
1421 | |
1422 /* | |
1423 * In REPLACE mode, for each character in the new indent, there must | |
1424 * be a NUL on the replace stack, for when it is deleted with BS | |
1425 */ | |
1426 if (REPLACE_NORMAL(State)) | |
1427 for (n = 0; n < (int)curwin->w_cursor.col; ++n) | |
1428 replace_push(NUL); | |
1429 newcol += curwin->w_cursor.col; | |
1430 #ifdef FEAT_SMARTINDENT | |
1431 if (no_si) | |
1432 did_si = FALSE; | |
1433 #endif | |
1434 } | |
1435 | |
1436 #ifdef FEAT_COMMENTS | |
1437 /* | |
1438 * In REPLACE mode, for each character in the extra leader, there must be | |
1439 * a NUL on the replace stack, for when it is deleted with BS. | |
1440 */ | |
1441 if (REPLACE_NORMAL(State)) | |
1442 while (lead_len-- > 0) | |
1443 replace_push(NUL); | |
1444 #endif | |
1445 | |
1446 curwin->w_cursor = old_cursor; | |
1447 | |
1448 if (dir == FORWARD) | |
1449 { | |
1450 if (trunc_line || (State & INSERT)) | |
1451 { | |
1452 /* truncate current line at cursor */ | |
1453 saved_line[curwin->w_cursor.col] = NUL; | |
1454 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ | |
1455 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) | |
1456 truncate_spaces(saved_line); | |
1457 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); | |
1458 saved_line = NULL; | |
1459 if (did_append) | |
1460 { | |
1461 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, | |
1462 curwin->w_cursor.lnum + 1, 1L); | |
1463 did_append = FALSE; | |
1464 | |
1465 /* Move marks after the line break to the new line. */ | |
1466 if (flags & OPENLINE_MARKFIX) | |
1467 mark_col_adjust(curwin->w_cursor.lnum, | |
1468 curwin->w_cursor.col + less_cols_off, | |
1469 1L, (long)-less_cols); | |
1470 } | |
1471 else | |
1472 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); | |
1473 } | |
1474 | |
1475 /* | |
1476 * Put the cursor on the new line. Careful: the scrollup() above may | |
1477 * have moved w_cursor, we must use old_cursor. | |
1478 */ | |
1479 curwin->w_cursor.lnum = old_cursor.lnum + 1; | |
1480 } | |
1481 if (did_append) | |
1482 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); | |
1483 | |
1484 curwin->w_cursor.col = newcol; | |
1485 #ifdef FEAT_VIRTUALEDIT | |
1486 curwin->w_cursor.coladd = 0; | |
1487 #endif | |
1488 | |
1489 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
1490 /* | |
1491 * In VREPLACE mode, we are handling the replace stack ourselves, so stop | |
1492 * fixthisline() from doing it (via change_indent()) by telling it we're in | |
1493 * normal INSERT mode. | |
1494 */ | |
1495 if (State & VREPLACE_FLAG) | |
1496 { | |
1497 vreplace_mode = State; /* So we know to put things right later */ | |
1498 State = INSERT; | |
1499 } | |
1500 else | |
1501 vreplace_mode = 0; | |
1502 #endif | |
1503 #ifdef FEAT_LISP | |
1504 /* | |
1505 * May do lisp indenting. | |
1506 */ | |
1507 if (!p_paste | |
1508 # ifdef FEAT_COMMENTS | |
1509 && leader == NULL | |
1510 # endif | |
1511 && curbuf->b_p_lisp | |
1512 && curbuf->b_p_ai) | |
1513 { | |
1514 fixthisline(get_lisp_indent); | |
1515 p = ml_get_curline(); | |
1516 ai_col = (colnr_T)(skipwhite(p) - p); | |
1517 } | |
1518 #endif | |
1519 #ifdef FEAT_CINDENT | |
1520 /* | |
1521 * May do indenting after opening a new line. | |
1522 */ | |
1523 if (!p_paste | |
1524 && (curbuf->b_p_cin | |
1525 # ifdef FEAT_EVAL | |
1526 || *curbuf->b_p_inde != NUL | |
1527 # endif | |
1528 ) | |
1529 && in_cinkeys(dir == FORWARD | |
1530 ? KEY_OPEN_FORW | |
1531 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) | |
1532 { | |
1533 do_c_expr_indent(); | |
1534 p = ml_get_curline(); | |
1535 ai_col = (colnr_T)(skipwhite(p) - p); | |
1536 } | |
1537 #endif | |
1538 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
1539 if (vreplace_mode != 0) | |
1540 State = vreplace_mode; | |
1541 #endif | |
1542 | |
1543 #ifdef FEAT_VREPLACE | |
1544 /* | |
1545 * Finally, VREPLACE gets the stuff on the new line, then puts back the | |
1546 * original line, and inserts the new stuff char by char, pushing old stuff | |
1547 * onto the replace stack (via ins_char()). | |
1548 */ | |
1549 if (State & VREPLACE_FLAG) | |
1550 { | |
1551 /* Put new line in p_extra */ | |
1552 p_extra = vim_strsave(ml_get_curline()); | |
1553 if (p_extra == NULL) | |
1554 goto theend; | |
1555 | |
1556 /* Put back original line */ | |
1557 ml_replace(curwin->w_cursor.lnum, next_line, FALSE); | |
1558 | |
1559 /* Insert new stuff into line again */ | |
1560 curwin->w_cursor.col = 0; | |
1561 #ifdef FEAT_VIRTUALEDIT | |
1562 curwin->w_cursor.coladd = 0; | |
1563 #endif | |
1564 ins_bytes(p_extra); /* will call changed_bytes() */ | |
1565 vim_free(p_extra); | |
1566 next_line = NULL; | |
1567 } | |
1568 #endif | |
1569 | |
1570 retval = TRUE; /* success! */ | |
1571 theend: | |
1572 curbuf->b_p_pi = saved_pi; | |
1573 vim_free(saved_line); | |
1574 vim_free(next_line); | |
1575 vim_free(allocated); | |
1576 return retval; | |
1577 } | |
1578 | |
1579 #if defined(FEAT_COMMENTS) || defined(PROTO) | |
1580 /* | |
1581 * get_leader_len() returns the length of the prefix of the given string | |
1582 * which introduces a comment. If this string is not a comment then 0 is | |
1583 * returned. | |
1584 * When "flags" is not NULL, it is set to point to the flags of the recognized | |
1585 * comment leader. | |
1586 * "backward" must be true for the "O" command. | |
3562 | 1587 * If "include_space" is set, include trailing whitespace while calculating the |
1588 * length. | |
7 | 1589 */ |
1590 int | |
3562 | 1591 get_leader_len(line, flags, backward, include_space) |
7 | 1592 char_u *line; |
1593 char_u **flags; | |
1594 int backward; | |
3562 | 1595 int include_space; |
7 | 1596 { |
1597 int i, j; | |
3562 | 1598 int result; |
7 | 1599 int got_com = FALSE; |
1600 int found_one; | |
1601 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ | |
1602 char_u *string; /* pointer to comment string */ | |
1603 char_u *list; | |
2809 | 1604 int middle_match_len = 0; |
1605 char_u *prev_list; | |
2813 | 1606 char_u *saved_flags = NULL; |
7 | 1607 |
3562 | 1608 result = i = 0; |
7 | 1609 while (vim_iswhite(line[i])) /* leading white space is ignored */ |
1610 ++i; | |
1611 | |
1612 /* | |
1613 * Repeat to match several nested comment strings. | |
1614 */ | |
2809 | 1615 while (line[i] != NUL) |
7 | 1616 { |
1617 /* | |
1618 * scan through the 'comments' option for a match | |
1619 */ | |
1620 found_one = FALSE; | |
1621 for (list = curbuf->b_p_com; *list; ) | |
1622 { | |
2809 | 1623 /* Get one option part into part_buf[]. Advance "list" to next |
1624 * one. Put "string" at start of string. */ | |
1625 if (!got_com && flags != NULL) | |
1626 *flags = list; /* remember where flags started */ | |
1627 prev_list = list; | |
7 | 1628 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
1629 string = vim_strchr(part_buf, ':'); | |
1630 if (string == NULL) /* missing ':', ignore this part */ | |
1631 continue; | |
1632 *string++ = NUL; /* isolate flags from string */ | |
1633 | |
2809 | 1634 /* If we found a middle match previously, use that match when this |
1635 * is not a middle or end. */ | |
1636 if (middle_match_len != 0 | |
1637 && vim_strchr(part_buf, COM_MIDDLE) == NULL | |
1638 && vim_strchr(part_buf, COM_END) == NULL) | |
1639 break; | |
1640 | |
1641 /* When we already found a nested comment, only accept further | |
1642 * nested comments. */ | |
7 | 1643 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
1644 continue; | |
1645 | |
2809 | 1646 /* When 'O' flag present and using "O" command skip this one. */ |
7 | 1647 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
1648 continue; | |
1649 | |
2809 | 1650 /* Line contents and string must match. |
7 | 1651 * When string starts with white space, must have some white space |
1652 * (but the amount does not need to match, there might be a mix of | |
2809 | 1653 * TABs and spaces). */ |
7 | 1654 if (vim_iswhite(string[0])) |
1655 { | |
1656 if (i == 0 || !vim_iswhite(line[i - 1])) | |
2809 | 1657 continue; /* missing shite space */ |
7 | 1658 while (vim_iswhite(string[0])) |
1659 ++string; | |
1660 } | |
1661 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) | |
1662 ; | |
1663 if (string[j] != NUL) | |
2809 | 1664 continue; /* string doesn't match */ |
1665 | |
1666 /* When 'b' flag used, there must be white space or an | |
1667 * end-of-line after the string in the line. */ | |
7 | 1668 if (vim_strchr(part_buf, COM_BLANK) != NULL |
1669 && !vim_iswhite(line[i + j]) && line[i + j] != NUL) | |
1670 continue; | |
1671 | |
2809 | 1672 /* We have found a match, stop searching unless this is a middle |
1673 * comment. The middle comment can be a substring of the end | |
1674 * comment in which case it's better to return the length of the | |
1675 * end comment and its flags. Thus we keep searching with middle | |
1676 * and end matches and use an end match if it matches better. */ | |
1677 if (vim_strchr(part_buf, COM_MIDDLE) != NULL) | |
1678 { | |
1679 if (middle_match_len == 0) | |
1680 { | |
1681 middle_match_len = j; | |
1682 saved_flags = prev_list; | |
1683 } | |
1684 continue; | |
1685 } | |
1686 if (middle_match_len != 0 && j > middle_match_len) | |
1687 /* Use this match instead of the middle match, since it's a | |
1688 * longer thus better match. */ | |
1689 middle_match_len = 0; | |
1690 | |
1691 if (middle_match_len == 0) | |
1692 i += j; | |
7 | 1693 found_one = TRUE; |
1694 break; | |
1695 } | |
1696 | |
2809 | 1697 if (middle_match_len != 0) |
1698 { | |
1699 /* Use the previously found middle match after failing to find a | |
1700 * match with an end. */ | |
1701 if (!got_com && flags != NULL) | |
1702 *flags = saved_flags; | |
1703 i += middle_match_len; | |
1704 found_one = TRUE; | |
1705 } | |
1706 | |
1707 /* No match found, stop scanning. */ | |
7 | 1708 if (!found_one) |
1709 break; | |
1710 | |
3562 | 1711 result = i; |
1712 | |
2809 | 1713 /* Include any trailing white space. */ |
7 | 1714 while (vim_iswhite(line[i])) |
1715 ++i; | |
1716 | |
3562 | 1717 if (include_space) |
1718 result = i; | |
1719 | |
2809 | 1720 /* If this comment doesn't nest, stop here. */ |
1721 got_com = TRUE; | |
7 | 1722 if (vim_strchr(part_buf, COM_NEST) == NULL) |
1723 break; | |
1724 } | |
3562 | 1725 return result; |
1726 } | |
1727 | |
1728 /* | |
1729 * Return the offset at which the last comment in line starts. If there is no | |
1730 * comment in the whole line, -1 is returned. | |
1731 * | |
1732 * When "flags" is not null, it is set to point to the flags describing the | |
1733 * recognized comment leader. | |
1734 */ | |
1735 int | |
1736 get_last_leader_offset(line, flags) | |
1737 char_u *line; | |
1738 char_u **flags; | |
1739 { | |
1740 int result = -1; | |
1741 int i, j; | |
1742 int lower_check_bound = 0; | |
1743 char_u *string; | |
1744 char_u *com_leader; | |
1745 char_u *com_flags; | |
1746 char_u *list; | |
1747 int found_one; | |
1748 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ | |
1749 | |
1750 /* | |
1751 * Repeat to match several nested comment strings. | |
1752 */ | |
1753 i = (int)STRLEN(line); | |
1754 while (--i >= lower_check_bound) | |
1755 { | |
1756 /* | |
1757 * scan through the 'comments' option for a match | |
1758 */ | |
1759 found_one = FALSE; | |
1760 for (list = curbuf->b_p_com; *list; ) | |
1761 { | |
1762 char_u *flags_save = list; | |
1763 | |
1764 /* | |
1765 * Get one option part into part_buf[]. Advance list to next one. | |
1766 * put string at start of string. | |
1767 */ | |
1768 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); | |
1769 string = vim_strchr(part_buf, ':'); | |
1770 if (string == NULL) /* If everything is fine, this cannot actually | |
1771 * happen. */ | |
1772 { | |
1773 continue; | |
1774 } | |
1775 *string++ = NUL; /* Isolate flags from string. */ | |
1776 com_leader = string; | |
1777 | |
1778 /* | |
1779 * Line contents and string must match. | |
1780 * When string starts with white space, must have some white space | |
1781 * (but the amount does not need to match, there might be a mix of | |
1782 * TABs and spaces). | |
1783 */ | |
1784 if (vim_iswhite(string[0])) | |
1785 { | |
1786 if (i == 0 || !vim_iswhite(line[i - 1])) | |
1787 continue; | |
1788 while (vim_iswhite(string[0])) | |
1789 ++string; | |
1790 } | |
1791 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) | |
1792 /* do nothing */; | |
1793 if (string[j] != NUL) | |
1794 continue; | |
1795 | |
1796 /* | |
1797 * When 'b' flag used, there must be white space or an | |
1798 * end-of-line after the string in the line. | |
1799 */ | |
1800 if (vim_strchr(part_buf, COM_BLANK) != NULL | |
1801 && !vim_iswhite(line[i + j]) && line[i + j] != NUL) | |
1802 { | |
1803 continue; | |
1804 } | |
1805 | |
1806 /* | |
1807 * We have found a match, stop searching. | |
1808 */ | |
1809 found_one = TRUE; | |
1810 | |
1811 if (flags) | |
1812 *flags = flags_save; | |
1813 com_flags = flags_save; | |
1814 | |
1815 break; | |
1816 } | |
1817 | |
1818 if (found_one) | |
1819 { | |
1820 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */ | |
1821 int len1, len2, off; | |
1822 | |
1823 result = i; | |
1824 /* | |
1825 * If this comment nests, continue searching. | |
1826 */ | |
1827 if (vim_strchr(part_buf, COM_NEST) != NULL) | |
1828 continue; | |
1829 | |
1830 lower_check_bound = i; | |
1831 | |
1832 /* Let's verify whether the comment leader found is a substring | |
1833 * of other comment leaders. If it is, let's adjust the | |
1834 * lower_check_bound so that we make sure that we have determined | |
1835 * the comment leader correctly. | |
1836 */ | |
1837 | |
1838 while (vim_iswhite(*com_leader)) | |
1839 ++com_leader; | |
1840 len1 = (int)STRLEN(com_leader); | |
1841 | |
1842 for (list = curbuf->b_p_com; *list; ) | |
1843 { | |
1844 char_u *flags_save = list; | |
1845 | |
1846 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ","); | |
1847 if (flags_save == com_flags) | |
1848 continue; | |
1849 string = vim_strchr(part_buf2, ':'); | |
1850 ++string; | |
1851 while (vim_iswhite(*string)) | |
1852 ++string; | |
1853 len2 = (int)STRLEN(string); | |
1854 if (len2 == 0) | |
1855 continue; | |
1856 | |
1857 /* Now we have to verify whether string ends with a substring | |
1858 * beginning the com_leader. */ | |
1859 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) | |
1860 { | |
1861 --off; | |
1862 if (!STRNCMP(string + off, com_leader, len2 - off)) | |
1863 { | |
1864 if (i - off < lower_check_bound) | |
1865 lower_check_bound = i - off; | |
1866 } | |
1867 } | |
1868 } | |
1869 } | |
1870 } | |
1871 return result; | |
7 | 1872 } |
1873 #endif | |
1874 | |
1875 /* | |
1876 * Return the number of window lines occupied by buffer line "lnum". | |
1877 */ | |
1878 int | |
1879 plines(lnum) | |
1880 linenr_T lnum; | |
1881 { | |
1882 return plines_win(curwin, lnum, TRUE); | |
1883 } | |
1884 | |
1885 int | |
1886 plines_win(wp, lnum, winheight) | |
1887 win_T *wp; | |
1888 linenr_T lnum; | |
1889 int winheight; /* when TRUE limit to window height */ | |
1890 { | |
1891 #if defined(FEAT_DIFF) || defined(PROTO) | |
1892 /* Check for filler lines above this buffer line. When folded the result | |
1893 * is one line anyway. */ | |
1894 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); | |
1895 } | |
1896 | |
1897 int | |
1898 plines_nofill(lnum) | |
1899 linenr_T lnum; | |
1900 { | |
1901 return plines_win_nofill(curwin, lnum, TRUE); | |
1902 } | |
1903 | |
1904 int | |
1905 plines_win_nofill(wp, lnum, winheight) | |
1906 win_T *wp; | |
1907 linenr_T lnum; | |
1908 int winheight; /* when TRUE limit to window height */ | |
1909 { | |
1910 #endif | |
1911 int lines; | |
1912 | |
1913 if (!wp->w_p_wrap) | |
1914 return 1; | |
1915 | |
1916 #ifdef FEAT_VERTSPLIT | |
1917 if (wp->w_width == 0) | |
1918 return 1; | |
1919 #endif | |
1920 | |
1921 #ifdef FEAT_FOLDING | |
1922 /* A folded lines is handled just like an empty line. */ | |
1923 /* NOTE: Caller must handle lines that are MAYBE folded. */ | |
1924 if (lineFolded(wp, lnum) == TRUE) | |
1925 return 1; | |
1926 #endif | |
1927 | |
1928 lines = plines_win_nofold(wp, lnum); | |
1929 if (winheight > 0 && lines > wp->w_height) | |
1930 return (int)wp->w_height; | |
1931 return lines; | |
1932 } | |
1933 | |
1934 /* | |
1935 * Return number of window lines physical line "lnum" will occupy in window | |
1936 * "wp". Does not care about folding, 'wrap' or 'diff'. | |
1937 */ | |
1938 int | |
1939 plines_win_nofold(wp, lnum) | |
1940 win_T *wp; | |
1941 linenr_T lnum; | |
1942 { | |
1943 char_u *s; | |
1944 long col; | |
1945 int width; | |
1946 | |
1947 s = ml_get_buf(wp->w_buffer, lnum, FALSE); | |
1948 if (*s == NUL) /* empty line */ | |
1949 return 1; | |
1950 col = win_linetabsize(wp, s, (colnr_T)MAXCOL); | |
1951 | |
1952 /* | |
1953 * If list mode is on, then the '$' at the end of the line may take up one | |
1954 * extra column. | |
1955 */ | |
1956 if (wp->w_p_list && lcs_eol != NUL) | |
1957 col += 1; | |
1958 | |
1959 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
1960 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
7 | 1961 */ |
1962 width = W_WIDTH(wp) - win_col_off(wp); | |
1963 if (width <= 0) | |
1964 return 32000; | |
1965 if (col <= width) | |
1966 return 1; | |
1967 col -= width; | |
1968 width += win_col_off2(wp); | |
1969 return (col + (width - 1)) / width + 1; | |
1970 } | |
1971 | |
1972 /* | |
1973 * Like plines_win(), but only reports the number of physical screen lines | |
1974 * used from the start of the line to the given column number. | |
1975 */ | |
1976 int | |
1977 plines_win_col(wp, lnum, column) | |
1978 win_T *wp; | |
1979 linenr_T lnum; | |
1980 long column; | |
1981 { | |
1982 long col; | |
1983 char_u *s; | |
1984 int lines = 0; | |
1985 int width; | |
1986 | |
1987 #ifdef FEAT_DIFF | |
1988 /* Check for filler lines above this buffer line. When folded the result | |
1989 * is one line anyway. */ | |
1990 lines = diff_check_fill(wp, lnum); | |
1991 #endif | |
1992 | |
1993 if (!wp->w_p_wrap) | |
1994 return lines + 1; | |
1995 | |
1996 #ifdef FEAT_VERTSPLIT | |
1997 if (wp->w_width == 0) | |
1998 return lines + 1; | |
1999 #endif | |
2000 | |
2001 s = ml_get_buf(wp->w_buffer, lnum, FALSE); | |
2002 | |
2003 col = 0; | |
2004 while (*s != NUL && --column >= 0) | |
2005 { | |
2006 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL); | |
39 | 2007 mb_ptr_adv(s); |
7 | 2008 } |
2009 | |
2010 /* | |
2011 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in | |
2012 * INSERT mode, then col must be adjusted so that it represents the last | |
2013 * screen position of the TAB. This only fixes an error when the TAB wraps | |
2014 * from one screen line to the next (when 'columns' is not a multiple of | |
2015 * 'ts') -- webb. | |
2016 */ | |
2017 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) | |
2018 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1; | |
2019 | |
2020 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
2021 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
7 | 2022 */ |
2023 width = W_WIDTH(wp) - win_col_off(wp); | |
1023 | 2024 if (width <= 0) |
2025 return 9999; | |
2026 | |
2027 lines += 1; | |
2028 if (col > width) | |
2029 lines += (col - width) / (width + win_col_off2(wp)) + 1; | |
2030 return lines; | |
7 | 2031 } |
2032 | |
2033 int | |
2034 plines_m_win(wp, first, last) | |
2035 win_T *wp; | |
2036 linenr_T first, last; | |
2037 { | |
2038 int count = 0; | |
2039 | |
2040 while (first <= last) | |
2041 { | |
2042 #ifdef FEAT_FOLDING | |
2043 int x; | |
2044 | |
2045 /* Check if there are any really folded lines, but also included lines | |
2046 * that are maybe folded. */ | |
2047 x = foldedCount(wp, first, NULL); | |
2048 if (x > 0) | |
2049 { | |
2050 ++count; /* count 1 for "+-- folded" line */ | |
2051 first += x; | |
2052 } | |
2053 else | |
2054 #endif | |
2055 { | |
2056 #ifdef FEAT_DIFF | |
2057 if (first == wp->w_topline) | |
2058 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; | |
2059 else | |
2060 #endif | |
2061 count += plines_win(wp, first, TRUE); | |
2062 ++first; | |
2063 } | |
2064 } | |
2065 return (count); | |
2066 } | |
2067 | |
2068 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO) | |
2069 /* | |
2070 * Insert string "p" at the cursor position. Stops at a NUL byte. | |
2071 * Handles Replace mode and multi-byte characters. | |
2072 */ | |
2073 void | |
2074 ins_bytes(p) | |
2075 char_u *p; | |
2076 { | |
2077 ins_bytes_len(p, (int)STRLEN(p)); | |
2078 } | |
2079 #endif | |
2080 | |
2081 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ | |
2082 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO) | |
2083 /* | |
2084 * Insert string "p" with length "len" at the cursor position. | |
2085 * Handles Replace mode and multi-byte characters. | |
2086 */ | |
2087 void | |
2088 ins_bytes_len(p, len) | |
2089 char_u *p; | |
2090 int len; | |
2091 { | |
2092 int i; | |
2093 # ifdef FEAT_MBYTE | |
2094 int n; | |
2095 | |
1617 | 2096 if (has_mbyte) |
2097 for (i = 0; i < len; i += n) | |
2098 { | |
2099 if (enc_utf8) | |
2100 /* avoid reading past p[len] */ | |
2101 n = utfc_ptr2len_len(p + i, len - i); | |
2102 else | |
2103 n = (*mb_ptr2len)(p + i); | |
2104 ins_char_bytes(p + i, n); | |
2105 } | |
2106 else | |
7 | 2107 # endif |
1617 | 2108 for (i = 0; i < len; ++i) |
2109 ins_char(p[i]); | |
7 | 2110 } |
2111 #endif | |
2112 | |
2113 /* | |
2114 * Insert or replace a single character at the cursor position. | |
2115 * When in REPLACE or VREPLACE mode, replace any existing character. | |
2116 * Caller must have prepared for undo. | |
2117 * For multi-byte characters we get the whole character, the caller must | |
2118 * convert bytes to a character. | |
2119 */ | |
2120 void | |
2121 ins_char(c) | |
2122 int c; | |
2123 { | |
2124 #if defined(FEAT_MBYTE) || defined(PROTO) | |
3549 | 2125 char_u buf[MB_MAXBYTES + 1]; |
7 | 2126 int n; |
2127 | |
2128 n = (*mb_char2bytes)(c, buf); | |
2129 | |
2130 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. | |
2131 * Happens for CTRL-Vu9900. */ | |
2132 if (buf[0] == 0) | |
2133 buf[0] = '\n'; | |
2134 | |
2135 ins_char_bytes(buf, n); | |
2136 } | |
2137 | |
2138 void | |
2139 ins_char_bytes(buf, charlen) | |
2140 char_u *buf; | |
2141 int charlen; | |
2142 { | |
2143 int c = buf[0]; | |
2144 #endif | |
2145 int newlen; /* nr of bytes inserted */ | |
2146 int oldlen; /* nr of bytes deleted (0 when not replacing) */ | |
2147 char_u *p; | |
2148 char_u *newp; | |
2149 char_u *oldp; | |
2150 int linelen; /* length of old line including NUL */ | |
2151 colnr_T col; | |
2152 linenr_T lnum = curwin->w_cursor.lnum; | |
2153 int i; | |
2154 | |
2155 #ifdef FEAT_VIRTUALEDIT | |
2156 /* Break tabs if needed. */ | |
2157 if (virtual_active() && curwin->w_cursor.coladd > 0) | |
2158 coladvance_force(getviscol()); | |
2159 #endif | |
2160 | |
2161 col = curwin->w_cursor.col; | |
2162 oldp = ml_get(lnum); | |
2163 linelen = (int)STRLEN(oldp) + 1; | |
2164 | |
2165 /* The lengths default to the values for when not replacing. */ | |
2166 oldlen = 0; | |
2167 #ifdef FEAT_MBYTE | |
2168 newlen = charlen; | |
2169 #else | |
2170 newlen = 1; | |
2171 #endif | |
2172 | |
2173 if (State & REPLACE_FLAG) | |
2174 { | |
2175 #ifdef FEAT_VREPLACE | |
2176 if (State & VREPLACE_FLAG) | |
2177 { | |
2178 colnr_T new_vcol = 0; /* init for GCC */ | |
2179 colnr_T vcol; | |
2180 int old_list; | |
2181 #ifndef FEAT_MBYTE | |
2182 char_u buf[2]; | |
2183 #endif | |
2184 | |
2185 /* | |
2186 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. | |
2187 * Returns the old value of list, so when finished, | |
2188 * curwin->w_p_list should be set back to this. | |
2189 */ | |
2190 old_list = curwin->w_p_list; | |
2191 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) | |
2192 curwin->w_p_list = FALSE; | |
2193 | |
2194 /* | |
2195 * In virtual replace mode each character may replace one or more | |
2196 * characters (zero if it's a TAB). Count the number of bytes to | |
2197 * be deleted to make room for the new character, counting screen | |
2198 * cells. May result in adding spaces to fill a gap. | |
2199 */ | |
2200 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); | |
2201 #ifndef FEAT_MBYTE | |
2202 buf[0] = c; | |
2203 buf[1] = NUL; | |
2204 #endif | |
2205 new_vcol = vcol + chartabsize(buf, vcol); | |
2206 while (oldp[col + oldlen] != NUL && vcol < new_vcol) | |
2207 { | |
2208 vcol += chartabsize(oldp + col + oldlen, vcol); | |
2209 /* Don't need to remove a TAB that takes us to the right | |
2210 * position. */ | |
2211 if (vcol > new_vcol && oldp[col + oldlen] == TAB) | |
2212 break; | |
2213 #ifdef FEAT_MBYTE | |
474 | 2214 oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
7 | 2215 #else |
2216 ++oldlen; | |
2217 #endif | |
2218 /* Deleted a bit too much, insert spaces. */ | |
2219 if (vcol > new_vcol) | |
2220 newlen += vcol - new_vcol; | |
2221 } | |
2222 curwin->w_p_list = old_list; | |
2223 } | |
2224 else | |
2225 #endif | |
2226 if (oldp[col] != NUL) | |
2227 { | |
2228 /* normal replace */ | |
2229 #ifdef FEAT_MBYTE | |
474 | 2230 oldlen = (*mb_ptr2len)(oldp + col); |
7 | 2231 #else |
2232 oldlen = 1; | |
2233 #endif | |
2234 } | |
2235 | |
2236 | |
2237 /* Push the replaced bytes onto the replace stack, so that they can be | |
2238 * put back when BS is used. The bytes of a multi-byte character are | |
2239 * done the other way around, so that the first byte is popped off | |
2240 * first (it tells the byte length of the character). */ | |
2241 replace_push(NUL); | |
2242 for (i = 0; i < oldlen; ++i) | |
2243 { | |
2244 #ifdef FEAT_MBYTE | |
1470 | 2245 if (has_mbyte) |
2246 i += replace_push_mb(oldp + col + i) - 1; | |
2247 else | |
2248 #endif | |
2249 replace_push(oldp[col + i]); | |
7 | 2250 } |
2251 } | |
2252 | |
2253 newp = alloc_check((unsigned)(linelen + newlen - oldlen)); | |
2254 if (newp == NULL) | |
2255 return; | |
2256 | |
2257 /* Copy bytes before the cursor. */ | |
2258 if (col > 0) | |
2259 mch_memmove(newp, oldp, (size_t)col); | |
2260 | |
2261 /* Copy bytes after the changed character(s). */ | |
2262 p = newp + col; | |
2263 mch_memmove(p + newlen, oldp + col + oldlen, | |
2264 (size_t)(linelen - col - oldlen)); | |
2265 | |
2266 /* Insert or overwrite the new character. */ | |
2267 #ifdef FEAT_MBYTE | |
2268 mch_memmove(p, buf, charlen); | |
2269 i = charlen; | |
2270 #else | |
2271 *p = c; | |
2272 i = 1; | |
2273 #endif | |
2274 | |
2275 /* Fill with spaces when necessary. */ | |
2276 while (i < newlen) | |
2277 p[i++] = ' '; | |
2278 | |
2279 /* Replace the line in the buffer. */ | |
2280 ml_replace(lnum, newp, FALSE); | |
2281 | |
2282 /* mark the buffer as changed and prepare for displaying */ | |
2283 changed_bytes(lnum, col); | |
2284 | |
2285 /* | |
2286 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly | |
2287 * show the match for right parens and braces. | |
2288 */ | |
2289 if (p_sm && (State & INSERT) | |
2290 && msg_silent == 0 | |
674 | 2291 #ifdef FEAT_INS_EXPAND |
2292 && !ins_compl_active() | |
2293 #endif | |
7 | 2294 ) |
4029 | 2295 { |
2296 #ifdef FEAT_MBYTE | |
2297 if (has_mbyte) | |
2298 showmatch(mb_ptr2char(buf)); | |
2299 else | |
2300 #endif | |
2301 showmatch(c); | |
2302 } | |
7 | 2303 |
2304 #ifdef FEAT_RIGHTLEFT | |
2305 if (!p_ri || (State & REPLACE_FLAG)) | |
2306 #endif | |
2307 { | |
2308 /* Normal insert: move cursor right */ | |
2309 #ifdef FEAT_MBYTE | |
2310 curwin->w_cursor.col += charlen; | |
2311 #else | |
2312 ++curwin->w_cursor.col; | |
2313 #endif | |
2314 } | |
2315 /* | |
2316 * TODO: should try to update w_row here, to avoid recomputing it later. | |
2317 */ | |
2318 } | |
2319 | |
2320 /* | |
2321 * Insert a string at the cursor position. | |
2322 * Note: Does NOT handle Replace mode. | |
2323 * Caller must have prepared for undo. | |
2324 */ | |
2325 void | |
2326 ins_str(s) | |
2327 char_u *s; | |
2328 { | |
2329 char_u *oldp, *newp; | |
2330 int newlen = (int)STRLEN(s); | |
2331 int oldlen; | |
2332 colnr_T col; | |
2333 linenr_T lnum = curwin->w_cursor.lnum; | |
2334 | |
2335 #ifdef FEAT_VIRTUALEDIT | |
2336 if (virtual_active() && curwin->w_cursor.coladd > 0) | |
2337 coladvance_force(getviscol()); | |
2338 #endif | |
2339 | |
2340 col = curwin->w_cursor.col; | |
2341 oldp = ml_get(lnum); | |
2342 oldlen = (int)STRLEN(oldp); | |
2343 | |
2344 newp = alloc_check((unsigned)(oldlen + newlen + 1)); | |
2345 if (newp == NULL) | |
2346 return; | |
2347 if (col > 0) | |
2348 mch_memmove(newp, oldp, (size_t)col); | |
2349 mch_memmove(newp + col, s, (size_t)newlen); | |
2350 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); | |
2351 ml_replace(lnum, newp, FALSE); | |
2352 changed_bytes(lnum, col); | |
2353 curwin->w_cursor.col += newlen; | |
2354 } | |
2355 | |
2356 /* | |
2357 * Delete one character under the cursor. | |
2358 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. | |
2359 * Caller must have prepared for undo. | |
2360 * | |
2361 * return FAIL for failure, OK otherwise | |
2362 */ | |
2363 int | |
2364 del_char(fixpos) | |
2365 int fixpos; | |
2366 { | |
2367 #ifdef FEAT_MBYTE | |
2368 if (has_mbyte) | |
2369 { | |
2370 /* Make sure the cursor is at the start of a character. */ | |
2371 mb_adjust_cursor(); | |
2372 if (*ml_get_cursor() == NUL) | |
2373 return FAIL; | |
2374 return del_chars(1L, fixpos); | |
2375 } | |
2376 #endif | |
610 | 2377 return del_bytes(1L, fixpos, TRUE); |
7 | 2378 } |
2379 | |
2380 #if defined(FEAT_MBYTE) || defined(PROTO) | |
2381 /* | |
2382 * Like del_bytes(), but delete characters instead of bytes. | |
2383 */ | |
2384 int | |
2385 del_chars(count, fixpos) | |
2386 long count; | |
2387 int fixpos; | |
2388 { | |
2389 long bytes = 0; | |
2390 long i; | |
2391 char_u *p; | |
2392 int l; | |
2393 | |
2394 p = ml_get_cursor(); | |
2395 for (i = 0; i < count && *p != NUL; ++i) | |
2396 { | |
474 | 2397 l = (*mb_ptr2len)(p); |
7 | 2398 bytes += l; |
2399 p += l; | |
2400 } | |
610 | 2401 return del_bytes(bytes, fixpos, TRUE); |
7 | 2402 } |
2403 #endif | |
2404 | |
2405 /* | |
2406 * Delete "count" bytes under the cursor. | |
2407 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. | |
2408 * Caller must have prepared for undo. | |
2409 * | |
2410 * return FAIL for failure, OK otherwise | |
2411 */ | |
2412 int | |
777 | 2413 del_bytes(count, fixpos_arg, use_delcombine) |
7 | 2414 long count; |
777 | 2415 int fixpos_arg; |
1877 | 2416 int use_delcombine UNUSED; /* 'delcombine' option applies */ |
7 | 2417 { |
2418 char_u *oldp, *newp; | |
2419 colnr_T oldlen; | |
2420 linenr_T lnum = curwin->w_cursor.lnum; | |
2421 colnr_T col = curwin->w_cursor.col; | |
2422 int was_alloced; | |
2423 long movelen; | |
777 | 2424 int fixpos = fixpos_arg; |
7 | 2425 |
2426 oldp = ml_get(lnum); | |
2427 oldlen = (int)STRLEN(oldp); | |
2428 | |
2429 /* | |
2430 * Can't do anything when the cursor is on the NUL after the line. | |
2431 */ | |
2432 if (col >= oldlen) | |
2433 return FAIL; | |
2434 | |
2435 #ifdef FEAT_MBYTE | |
2436 /* If 'delcombine' is set and deleting (less than) one character, only | |
2437 * delete the last combining character. */ | |
610 | 2438 if (p_deco && use_delcombine && enc_utf8 |
2439 && utfc_ptr2len(oldp + col) >= count) | |
7 | 2440 { |
714 | 2441 int cc[MAX_MCO]; |
7 | 2442 int n; |
2443 | |
714 | 2444 (void)utfc_ptr2char(oldp + col, cc); |
2445 if (cc[0] != NUL) | |
7 | 2446 { |
2447 /* Find the last composing char, there can be several. */ | |
2448 n = col; | |
2449 do | |
2450 { | |
2451 col = n; | |
474 | 2452 count = utf_ptr2len(oldp + n); |
7 | 2453 n += count; |
2454 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); | |
2455 fixpos = 0; | |
2456 } | |
2457 } | |
2458 #endif | |
2459 | |
2460 /* | |
2461 * When count is too big, reduce it. | |
2462 */ | |
2463 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ | |
2464 if (movelen <= 1) | |
2465 { | |
2466 /* | |
2467 * If we just took off the last character of a non-blank line, and | |
777 | 2468 * fixpos is TRUE, we don't want to end up positioned at the NUL, |
2469 * unless "restart_edit" is set or 'virtualedit' contains "onemore". | |
7 | 2470 */ |
777 | 2471 if (col > 0 && fixpos && restart_edit == 0 |
2472 #ifdef FEAT_VIRTUALEDIT | |
2473 && (ve_flags & VE_ONEMORE) == 0 | |
2474 #endif | |
2475 ) | |
7 | 2476 { |
2477 --curwin->w_cursor.col; | |
2478 #ifdef FEAT_VIRTUALEDIT | |
2479 curwin->w_cursor.coladd = 0; | |
2480 #endif | |
2481 #ifdef FEAT_MBYTE | |
2482 if (has_mbyte) | |
2483 curwin->w_cursor.col -= | |
2484 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); | |
2485 #endif | |
2486 } | |
2487 count = oldlen - col; | |
2488 movelen = 1; | |
2489 } | |
2490 | |
2491 /* | |
2492 * If the old line has been allocated the deletion can be done in the | |
2493 * existing line. Otherwise a new line has to be allocated | |
1532 | 2494 * Can't do this when using Netbeans, because we would need to invoke |
2495 * netbeans_removed(), which deallocates the line. Let ml_replace() take | |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
2496 * care of notifying Netbeans. |
7 | 2497 */ |
2498 #ifdef FEAT_NETBEANS_INTG | |
2210 | 2499 if (netbeans_active()) |
1532 | 2500 was_alloced = FALSE; |
2501 else | |
2502 #endif | |
2503 was_alloced = ml_line_alloced(); /* check if oldp was allocated */ | |
7 | 2504 if (was_alloced) |
2505 newp = oldp; /* use same allocated memory */ | |
2506 else | |
2507 { /* need to allocate a new line */ | |
2508 newp = alloc((unsigned)(oldlen + 1 - count)); | |
2509 if (newp == NULL) | |
2510 return FAIL; | |
2511 mch_memmove(newp, oldp, (size_t)col); | |
2512 } | |
2513 mch_memmove(newp + col, oldp + col + count, (size_t)movelen); | |
2514 if (!was_alloced) | |
2515 ml_replace(lnum, newp, FALSE); | |
2516 | |
2517 /* mark the buffer as changed and prepare for displaying */ | |
2518 changed_bytes(lnum, curwin->w_cursor.col); | |
2519 | |
2520 return OK; | |
2521 } | |
2522 | |
2523 /* | |
2524 * Delete from cursor to end of line. | |
2525 * Caller must have prepared for undo. | |
2526 * | |
2527 * return FAIL for failure, OK otherwise | |
2528 */ | |
2529 int | |
2530 truncate_line(fixpos) | |
2531 int fixpos; /* if TRUE fix the cursor position when done */ | |
2532 { | |
2533 char_u *newp; | |
2534 linenr_T lnum = curwin->w_cursor.lnum; | |
2535 colnr_T col = curwin->w_cursor.col; | |
2536 | |
2537 if (col == 0) | |
2538 newp = vim_strsave((char_u *)""); | |
2539 else | |
2540 newp = vim_strnsave(ml_get(lnum), col); | |
2541 | |
2542 if (newp == NULL) | |
2543 return FAIL; | |
2544 | |
2545 ml_replace(lnum, newp, FALSE); | |
2546 | |
2547 /* mark the buffer as changed and prepare for displaying */ | |
2548 changed_bytes(lnum, curwin->w_cursor.col); | |
2549 | |
2550 /* | |
2551 * If "fixpos" is TRUE we don't want to end up positioned at the NUL. | |
2552 */ | |
2553 if (fixpos && curwin->w_cursor.col > 0) | |
2554 --curwin->w_cursor.col; | |
2555 | |
2556 return OK; | |
2557 } | |
2558 | |
2559 /* | |
2560 * Delete "nlines" lines at the cursor. | |
2561 * Saves the lines for undo first if "undo" is TRUE. | |
2562 */ | |
2563 void | |
2564 del_lines(nlines, undo) | |
2565 long nlines; /* number of lines to delete */ | |
2566 int undo; /* if TRUE, prepare for undo */ | |
2567 { | |
2568 long n; | |
1929 | 2569 linenr_T first = curwin->w_cursor.lnum; |
7 | 2570 |
2571 if (nlines <= 0) | |
2572 return; | |
2573 | |
2574 /* save the deleted lines for undo */ | |
1929 | 2575 if (undo && u_savedel(first, nlines) == FAIL) |
7 | 2576 return; |
2577 | |
2578 for (n = 0; n < nlines; ) | |
2579 { | |
2580 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ | |
2581 break; | |
2582 | |
1929 | 2583 ml_delete(first, TRUE); |
7 | 2584 ++n; |
2585 | |
2586 /* If we delete the last line in the file, stop */ | |
1929 | 2587 if (first > curbuf->b_ml.ml_line_count) |
7 | 2588 break; |
2589 } | |
1929 | 2590 |
2591 /* Correct the cursor position before calling deleted_lines_mark(), it may | |
2592 * trigger a callback to display the cursor. */ | |
7 | 2593 curwin->w_cursor.col = 0; |
2594 check_cursor_lnum(); | |
1929 | 2595 |
2596 /* adjust marks, mark the buffer as changed and prepare for displaying */ | |
2597 deleted_lines_mark(first, n); | |
7 | 2598 } |
2599 | |
2600 int | |
2601 gchar_pos(pos) | |
2602 pos_T *pos; | |
2603 { | |
2604 char_u *ptr = ml_get_pos(pos); | |
2605 | |
2606 #ifdef FEAT_MBYTE | |
2607 if (has_mbyte) | |
2608 return (*mb_ptr2char)(ptr); | |
2609 #endif | |
2610 return (int)*ptr; | |
2611 } | |
2612 | |
2613 int | |
2614 gchar_cursor() | |
2615 { | |
2616 #ifdef FEAT_MBYTE | |
2617 if (has_mbyte) | |
2618 return (*mb_ptr2char)(ml_get_cursor()); | |
2619 #endif | |
2620 return (int)*ml_get_cursor(); | |
2621 } | |
2622 | |
2623 /* | |
2624 * Write a character at the current cursor position. | |
2625 * It is directly written into the block. | |
2626 */ | |
2627 void | |
2628 pchar_cursor(c) | |
2629 int c; | |
2630 { | |
2631 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) | |
2632 + curwin->w_cursor.col) = c; | |
2633 } | |
2634 | |
2635 /* | |
2636 * When extra == 0: Return TRUE if the cursor is before or on the first | |
2637 * non-blank in the line. | |
2638 * When extra == 1: Return TRUE if the cursor is before the first non-blank in | |
2639 * the line. | |
2640 */ | |
2641 int | |
2642 inindent(extra) | |
2643 int extra; | |
2644 { | |
2645 char_u *ptr; | |
2646 colnr_T col; | |
2647 | |
2648 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col) | |
2649 ++ptr; | |
2650 if (col >= curwin->w_cursor.col + extra) | |
2651 return TRUE; | |
2652 else | |
2653 return FALSE; | |
2654 } | |
2655 | |
2656 /* | |
2657 * Skip to next part of an option argument: Skip space and comma. | |
2658 */ | |
2659 char_u * | |
2660 skip_to_option_part(p) | |
2661 char_u *p; | |
2662 { | |
2663 if (*p == ',') | |
2664 ++p; | |
2665 while (*p == ' ') | |
2666 ++p; | |
2667 return p; | |
2668 } | |
2669 | |
2670 /* | |
2189 | 2671 * Call this function when something in the current buffer is changed. |
7 | 2672 * |
2673 * Most often called through changed_bytes() and changed_lines(), which also | |
2674 * mark the area of the display to be redrawn. | |
2189 | 2675 * |
2676 * Careful: may trigger autocommands that reload the buffer. | |
7 | 2677 */ |
2678 void | |
2679 changed() | |
2680 { | |
2681 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) | |
2682 /* The text of the preediting area is inserted, but this doesn't | |
2683 * mean a change of the buffer yet. That is delayed until the | |
2684 * text is committed. (this means preedit becomes empty) */ | |
2685 if (im_is_preediting() && !xim_changed_while_preediting) | |
2686 return; | |
2687 xim_changed_while_preediting = FALSE; | |
2688 #endif | |
2689 | |
2690 if (!curbuf->b_changed) | |
2691 { | |
2692 int save_msg_scroll = msg_scroll; | |
2693 | |
819 | 2694 /* Give a warning about changing a read-only file. This may also |
2695 * check-out the file, thus change "curbuf"! */ | |
7 | 2696 change_warning(0); |
819 | 2697 |
7 | 2698 /* Create a swap file if that is wanted. |
2699 * Don't do this for "nofile" and "nowrite" buffer types. */ | |
2700 if (curbuf->b_may_swap | |
2701 #ifdef FEAT_QUICKFIX | |
2702 && !bt_dontwrite(curbuf) | |
2703 #endif | |
2704 ) | |
2705 { | |
2706 ml_open_file(curbuf); | |
2707 | |
2708 /* The ml_open_file() can cause an ATTENTION message. | |
2709 * Wait two seconds, to make sure the user reads this unexpected | |
2710 * message. Since we could be anywhere, call wait_return() now, | |
2711 * and don't let the emsg() set msg_scroll. */ | |
2712 if (need_wait_return && emsg_silent == 0) | |
2713 { | |
2714 out_flush(); | |
2715 ui_delay(2000L, TRUE); | |
2716 wait_return(TRUE); | |
2717 msg_scroll = save_msg_scroll; | |
2718 } | |
2719 } | |
2162
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2720 changed_int(); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2721 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2722 ++curbuf->b_changedtick; |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2723 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2724 |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2725 /* |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2726 * Internal part of changed(), no user interaction. |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2727 */ |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2728 void |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2729 changed_int() |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2730 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2731 curbuf->b_changed = TRUE; |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2732 ml_setflags(curbuf); |
2165
733f0dc510c3
Undo changes that are meant for the Vim 7.3 branch.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
2733 #ifdef FEAT_WINDOWS |
2162
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2734 check_status(curbuf); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2735 redraw_tabline = TRUE; |
2165
733f0dc510c3
Undo changes that are meant for the Vim 7.3 branch.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
2736 #endif |
733f0dc510c3
Undo changes that are meant for the Vim 7.3 branch.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
2737 #ifdef FEAT_TITLE |
2162
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2738 need_maketitle = TRUE; /* set window title later */ |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2739 #endif |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2740 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2741 |
265 | 2742 static void changedOneline __ARGS((buf_T *buf, linenr_T lnum)); |
2743 static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra)); | |
7 | 2744 static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra)); |
2745 | |
2746 /* | |
2747 * Changed bytes within a single line for the current buffer. | |
2748 * - marks the windows on this buffer to be redisplayed | |
2749 * - marks the buffer changed by calling changed() | |
2750 * - invalidates cached values | |
2189 | 2751 * Careful: may trigger autocommands that reload the buffer. |
7 | 2752 */ |
2753 void | |
2754 changed_bytes(lnum, col) | |
2755 linenr_T lnum; | |
2756 colnr_T col; | |
2757 { | |
265 | 2758 changedOneline(curbuf, lnum); |
7 | 2759 changed_common(lnum, col, lnum + 1, 0L); |
265 | 2760 |
2761 #ifdef FEAT_DIFF | |
2762 /* Diff highlighting in other diff windows may need to be updated too. */ | |
2763 if (curwin->w_p_diff) | |
2764 { | |
2765 win_T *wp; | |
2766 linenr_T wlnum; | |
2767 | |
2768 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2769 if (wp->w_p_diff && wp != curwin) | |
2770 { | |
2771 redraw_win_later(wp, VALID); | |
2772 wlnum = diff_lnum_win(lnum, wp); | |
2773 if (wlnum > 0) | |
2774 changedOneline(wp->w_buffer, wlnum); | |
2775 } | |
2776 } | |
2777 #endif | |
7 | 2778 } |
2779 | |
2780 static void | |
265 | 2781 changedOneline(buf, lnum) |
2782 buf_T *buf; | |
7 | 2783 linenr_T lnum; |
2784 { | |
265 | 2785 if (buf->b_mod_set) |
7 | 2786 { |
2787 /* find the maximum area that must be redisplayed */ | |
265 | 2788 if (lnum < buf->b_mod_top) |
2789 buf->b_mod_top = lnum; | |
2790 else if (lnum >= buf->b_mod_bot) | |
2791 buf->b_mod_bot = lnum + 1; | |
7 | 2792 } |
2793 else | |
2794 { | |
2795 /* set the area that must be redisplayed to one line */ | |
265 | 2796 buf->b_mod_set = TRUE; |
2797 buf->b_mod_top = lnum; | |
2798 buf->b_mod_bot = lnum + 1; | |
2799 buf->b_mod_xlines = 0; | |
7 | 2800 } |
2801 } | |
2802 | |
2803 /* | |
2804 * Appended "count" lines below line "lnum" in the current buffer. | |
2805 * Must be called AFTER the change and after mark_adjust(). | |
2806 * Takes care of marking the buffer to be redrawn and sets the changed flag. | |
2807 */ | |
2808 void | |
2809 appended_lines(lnum, count) | |
2810 linenr_T lnum; | |
2811 long count; | |
2812 { | |
2813 changed_lines(lnum + 1, 0, lnum + 1, count); | |
2814 } | |
2815 | |
2816 /* | |
2817 * Like appended_lines(), but adjust marks first. | |
2818 */ | |
2819 void | |
2820 appended_lines_mark(lnum, count) | |
2821 linenr_T lnum; | |
2822 long count; | |
2823 { | |
2824 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); | |
2825 changed_lines(lnum + 1, 0, lnum + 1, count); | |
2826 } | |
2827 | |
2828 /* | |
2829 * Deleted "count" lines at line "lnum" in the current buffer. | |
2830 * Must be called AFTER the change and after mark_adjust(). | |
2831 * Takes care of marking the buffer to be redrawn and sets the changed flag. | |
2832 */ | |
2833 void | |
2834 deleted_lines(lnum, count) | |
2835 linenr_T lnum; | |
2836 long count; | |
2837 { | |
2838 changed_lines(lnum, 0, lnum + count, -count); | |
2839 } | |
2840 | |
2841 /* | |
2842 * Like deleted_lines(), but adjust marks first. | |
1929 | 2843 * Make sure the cursor is on a valid line before calling, a GUI callback may |
2844 * be triggered to display the cursor. | |
7 | 2845 */ |
2846 void | |
2847 deleted_lines_mark(lnum, count) | |
2848 linenr_T lnum; | |
2849 long count; | |
2850 { | |
2851 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); | |
2852 changed_lines(lnum, 0, lnum + count, -count); | |
2853 } | |
2854 | |
2855 /* | |
2856 * Changed lines for the current buffer. | |
2857 * Must be called AFTER the change and after mark_adjust(). | |
2858 * - mark the buffer changed by calling changed() | |
2859 * - mark the windows on this buffer to be redisplayed | |
2860 * - invalidate cached values | |
2861 * "lnum" is the first line that needs displaying, "lnume" the first line | |
2862 * below the changed lines (BEFORE the change). | |
2863 * When only inserting lines, "lnum" and "lnume" are equal. | |
2864 * Takes care of calling changed() and updating b_mod_*. | |
2189 | 2865 * Careful: may trigger autocommands that reload the buffer. |
7 | 2866 */ |
2867 void | |
2868 changed_lines(lnum, col, lnume, xtra) | |
2869 linenr_T lnum; /* first line with change */ | |
2870 colnr_T col; /* column in first line with change */ | |
2871 linenr_T lnume; /* line below last changed line */ | |
2872 long xtra; /* number of extra lines (negative when deleting) */ | |
2873 { | |
265 | 2874 changed_lines_buf(curbuf, lnum, lnume, xtra); |
2875 | |
2876 #ifdef FEAT_DIFF | |
2877 if (xtra == 0 && curwin->w_p_diff) | |
2878 { | |
2879 /* When the number of lines doesn't change then mark_adjust() isn't | |
2880 * called and other diff buffers still need to be marked for | |
2881 * displaying. */ | |
2882 win_T *wp; | |
2883 linenr_T wlnum; | |
2884 | |
2885 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2886 if (wp->w_p_diff && wp != curwin) | |
2887 { | |
2888 redraw_win_later(wp, VALID); | |
2889 wlnum = diff_lnum_win(lnum, wp); | |
2890 if (wlnum > 0) | |
2891 changed_lines_buf(wp->w_buffer, wlnum, | |
2892 lnume - lnum + wlnum, 0L); | |
2893 } | |
2894 } | |
2895 #endif | |
2896 | |
2897 changed_common(lnum, col, lnume, xtra); | |
2898 } | |
2899 | |
2900 static void | |
2901 changed_lines_buf(buf, lnum, lnume, xtra) | |
2902 buf_T *buf; | |
2903 linenr_T lnum; /* first line with change */ | |
2904 linenr_T lnume; /* line below last changed line */ | |
2905 long xtra; /* number of extra lines (negative when deleting) */ | |
2906 { | |
2907 if (buf->b_mod_set) | |
7 | 2908 { |
2909 /* find the maximum area that must be redisplayed */ | |
265 | 2910 if (lnum < buf->b_mod_top) |
2911 buf->b_mod_top = lnum; | |
2912 if (lnum < buf->b_mod_bot) | |
7 | 2913 { |
2914 /* adjust old bot position for xtra lines */ | |
265 | 2915 buf->b_mod_bot += xtra; |
2916 if (buf->b_mod_bot < lnum) | |
2917 buf->b_mod_bot = lnum; | |
7 | 2918 } |
265 | 2919 if (lnume + xtra > buf->b_mod_bot) |
2920 buf->b_mod_bot = lnume + xtra; | |
2921 buf->b_mod_xlines += xtra; | |
7 | 2922 } |
2923 else | |
2924 { | |
2925 /* set the area that must be redisplayed */ | |
265 | 2926 buf->b_mod_set = TRUE; |
2927 buf->b_mod_top = lnum; | |
2928 buf->b_mod_bot = lnume + xtra; | |
2929 buf->b_mod_xlines = xtra; | |
2930 } | |
7 | 2931 } |
2932 | |
2189 | 2933 /* |
2934 * Common code for when a change is was made. | |
2935 * See changed_lines() for the arguments. | |
2936 * Careful: may trigger autocommands that reload the buffer. | |
2937 */ | |
7 | 2938 static void |
2939 changed_common(lnum, col, lnume, xtra) | |
2940 linenr_T lnum; | |
2941 colnr_T col; | |
2942 linenr_T lnume; | |
2943 long xtra; | |
2944 { | |
2945 win_T *wp; | |
1863 | 2946 #ifdef FEAT_WINDOWS |
2947 tabpage_T *tp; | |
2948 #endif | |
7 | 2949 int i; |
2950 #ifdef FEAT_JUMPLIST | |
2951 int cols; | |
2952 pos_T *p; | |
2953 int add; | |
2954 #endif | |
2955 | |
2956 /* mark the buffer as modified */ | |
2957 changed(); | |
2958 | |
2959 /* set the '. mark */ | |
2960 if (!cmdmod.keepjumps) | |
2961 { | |
2962 curbuf->b_last_change.lnum = lnum; | |
2963 curbuf->b_last_change.col = col; | |
2964 | |
2965 #ifdef FEAT_JUMPLIST | |
2966 /* Create a new entry if a new undo-able change was started or we | |
2967 * don't have an entry yet. */ | |
2968 if (curbuf->b_new_change || curbuf->b_changelistlen == 0) | |
2969 { | |
2970 if (curbuf->b_changelistlen == 0) | |
2971 add = TRUE; | |
2972 else | |
2973 { | |
2974 /* Don't create a new entry when the line number is the same | |
2975 * as the last one and the column is not too far away. Avoids | |
2976 * creating many entries for typing "xxxxx". */ | |
2977 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; | |
2978 if (p->lnum != lnum) | |
2979 add = TRUE; | |
2980 else | |
2981 { | |
2982 cols = comp_textwidth(FALSE); | |
2983 if (cols == 0) | |
2984 cols = 79; | |
2985 add = (p->col + cols < col || col + cols < p->col); | |
2986 } | |
2987 } | |
2988 if (add) | |
2989 { | |
2990 /* This is the first of a new sequence of undo-able changes | |
2991 * and it's at some distance of the last change. Use a new | |
2992 * position in the changelist. */ | |
2993 curbuf->b_new_change = FALSE; | |
2994 | |
2995 if (curbuf->b_changelistlen == JUMPLISTSIZE) | |
2996 { | |
2997 /* changelist is full: remove oldest entry */ | |
2998 curbuf->b_changelistlen = JUMPLISTSIZE - 1; | |
2999 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, | |
3000 sizeof(pos_T) * (JUMPLISTSIZE - 1)); | |
1863 | 3001 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 3002 { |
3003 /* Correct position in changelist for other windows on | |
3004 * this buffer. */ | |
3005 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) | |
3006 --wp->w_changelistidx; | |
3007 } | |
3008 } | |
1863 | 3009 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 3010 { |
3011 /* For other windows, if the position in the changelist is | |
3012 * at the end it stays at the end. */ | |
3013 if (wp->w_buffer == curbuf | |
3014 && wp->w_changelistidx == curbuf->b_changelistlen) | |
3015 ++wp->w_changelistidx; | |
3016 } | |
3017 ++curbuf->b_changelistlen; | |
3018 } | |
3019 } | |
3020 curbuf->b_changelist[curbuf->b_changelistlen - 1] = | |
3021 curbuf->b_last_change; | |
3022 /* The current window is always after the last change, so that "g," | |
3023 * takes you back to it. */ | |
3024 curwin->w_changelistidx = curbuf->b_changelistlen; | |
3025 #endif | |
3026 } | |
3027 | |
1863 | 3028 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 3029 { |
3030 if (wp->w_buffer == curbuf) | |
3031 { | |
3032 /* Mark this window to be redrawn later. */ | |
3033 if (wp->w_redr_type < VALID) | |
3034 wp->w_redr_type = VALID; | |
3035 | |
3036 /* Check if a change in the buffer has invalidated the cached | |
3037 * values for the cursor. */ | |
3038 #ifdef FEAT_FOLDING | |
3039 /* | |
3040 * Update the folds for this window. Can't postpone this, because | |
3041 * a following operator might work on the whole fold: ">>dd". | |
3042 */ | |
3043 foldUpdate(wp, lnum, lnume + xtra - 1); | |
3044 | |
3045 /* The change may cause lines above or below the change to become | |
3046 * included in a fold. Set lnum/lnume to the first/last line that | |
3047 * might be displayed differently. | |
3048 * Set w_cline_folded here as an efficient way to update it when | |
3049 * inserting lines just above a closed fold. */ | |
3050 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); | |
3051 if (wp->w_cursor.lnum == lnum) | |
3052 wp->w_cline_folded = i; | |
3053 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); | |
3054 if (wp->w_cursor.lnum == lnume) | |
3055 wp->w_cline_folded = i; | |
3056 | |
3057 /* If the changed line is in a range of previously folded lines, | |
3058 * compare with the first line in that range. */ | |
3059 if (wp->w_cursor.lnum <= lnum) | |
3060 { | |
3061 i = find_wl_entry(wp, lnum); | |
3062 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) | |
3063 changed_line_abv_curs_win(wp); | |
3064 } | |
3065 #endif | |
3066 | |
3067 if (wp->w_cursor.lnum > lnum) | |
3068 changed_line_abv_curs_win(wp); | |
3069 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) | |
3070 changed_cline_bef_curs_win(wp); | |
3071 if (wp->w_botline >= lnum) | |
3072 { | |
3073 /* Assume that botline doesn't change (inserted lines make | |
3074 * other lines scroll down below botline). */ | |
3075 approximate_botline_win(wp); | |
3076 } | |
3077 | |
3078 /* Check if any w_lines[] entries have become invalid. | |
3079 * For entries below the change: Correct the lnums for | |
3080 * inserted/deleted lines. Makes it possible to stop displaying | |
3081 * after the change. */ | |
3082 for (i = 0; i < wp->w_lines_valid; ++i) | |
3083 if (wp->w_lines[i].wl_valid) | |
3084 { | |
3085 if (wp->w_lines[i].wl_lnum >= lnum) | |
3086 { | |
3087 if (wp->w_lines[i].wl_lnum < lnume) | |
3088 { | |
3089 /* line included in change */ | |
3090 wp->w_lines[i].wl_valid = FALSE; | |
3091 } | |
3092 else if (xtra != 0) | |
3093 { | |
3094 /* line below change */ | |
3095 wp->w_lines[i].wl_lnum += xtra; | |
3096 #ifdef FEAT_FOLDING | |
3097 wp->w_lines[i].wl_lastlnum += xtra; | |
3098 #endif | |
3099 } | |
3100 } | |
3101 #ifdef FEAT_FOLDING | |
3102 else if (wp->w_lines[i].wl_lastlnum >= lnum) | |
3103 { | |
3104 /* change somewhere inside this range of folded lines, | |
3105 * may need to be redrawn */ | |
3106 wp->w_lines[i].wl_valid = FALSE; | |
3107 } | |
3108 #endif | |
3109 } | |
1987 | 3110 |
3111 #ifdef FEAT_FOLDING | |
3112 /* Take care of side effects for setting w_topline when folds have | |
3113 * changed. Esp. when the buffer was changed in another window. */ | |
3114 if (hasAnyFolding(wp)) | |
3115 set_topline(wp, wp->w_topline); | |
3116 #endif | |
7 | 3117 } |
3118 } | |
3119 | |
3120 /* Call update_screen() later, which checks out what needs to be redrawn, | |
3121 * since it notices b_mod_set and then uses b_mod_*. */ | |
3122 if (must_redraw < VALID) | |
3123 must_redraw = VALID; | |
694 | 3124 |
3125 #ifdef FEAT_AUTOCMD | |
3126 /* when the cursor line is changed always trigger CursorMoved */ | |
1010 | 3127 if (lnum <= curwin->w_cursor.lnum |
3128 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) | |
694 | 3129 last_cursormoved.lnum = 0; |
3130 #endif | |
7 | 3131 } |
3132 | |
3133 /* | |
3134 * unchanged() is called when the changed flag must be reset for buffer 'buf' | |
3135 */ | |
3136 void | |
3137 unchanged(buf, ff) | |
3138 buf_T *buf; | |
3139 int ff; /* also reset 'fileformat' */ | |
3140 { | |
2685 | 3141 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
7 | 3142 { |
3143 buf->b_changed = 0; | |
39 | 3144 ml_setflags(buf); |
7 | 3145 if (ff) |
3146 save_file_ff(buf); | |
3147 #ifdef FEAT_WINDOWS | |
3148 check_status(buf); | |
673 | 3149 redraw_tabline = TRUE; |
7 | 3150 #endif |
3151 #ifdef FEAT_TITLE | |
3152 need_maketitle = TRUE; /* set window title later */ | |
3153 #endif | |
3154 } | |
3155 ++buf->b_changedtick; | |
3156 #ifdef FEAT_NETBEANS_INTG | |
3157 netbeans_unmodified(buf); | |
3158 #endif | |
3159 } | |
3160 | |
3161 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
3162 /* | |
3163 * check_status: called when the status bars for the buffer 'buf' | |
3164 * need to be updated | |
3165 */ | |
3166 void | |
3167 check_status(buf) | |
3168 buf_T *buf; | |
3169 { | |
3170 win_T *wp; | |
3171 | |
3172 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
3173 if (wp->w_buffer == buf && wp->w_status_height) | |
3174 { | |
3175 wp->w_redr_status = TRUE; | |
3176 if (must_redraw < VALID) | |
3177 must_redraw = VALID; | |
3178 } | |
3179 } | |
3180 #endif | |
3181 | |
3182 /* | |
3183 * If the file is readonly, give a warning message with the first change. | |
3184 * Don't do this for autocommands. | |
3185 * Don't use emsg(), because it flushes the macro buffer. | |
548 | 3186 * If we have undone all changes b_changed will be FALSE, but "b_did_warn" |
7 | 3187 * will be TRUE. |
2189 | 3188 * Careful: may trigger autocommands that reload the buffer. |
7 | 3189 */ |
3190 void | |
3191 change_warning(col) | |
3192 int col; /* column for message; non-zero when in insert | |
3193 mode and 'showmode' is on */ | |
3194 { | |
1848 | 3195 static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
3196 | |
7 | 3197 if (curbuf->b_did_warn == FALSE |
3198 && curbufIsChanged() == 0 | |
3199 #ifdef FEAT_AUTOCMD | |
3200 && !autocmd_busy | |
3201 #endif | |
3202 && curbuf->b_p_ro) | |
3203 { | |
3204 #ifdef FEAT_AUTOCMD | |
819 | 3205 ++curbuf_lock; |
7 | 3206 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
819 | 3207 --curbuf_lock; |
7 | 3208 if (!curbuf->b_p_ro) |
3209 return; | |
3210 #endif | |
3211 /* | |
3212 * Do what msg() does, but with a column offset if the warning should | |
3213 * be after the mode message. | |
3214 */ | |
3215 msg_start(); | |
3216 if (msg_row == Rows - 1) | |
3217 msg_col = col; | |
16 | 3218 msg_source(hl_attr(HLF_W)); |
1848 | 3219 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST); |
3220 #ifdef FEAT_EVAL | |
3221 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); | |
3222 #endif | |
7 | 3223 msg_clr_eos(); |
3224 (void)msg_end(); | |
3225 if (msg_silent == 0 && !silent_mode) | |
3226 { | |
3227 out_flush(); | |
3228 ui_delay(1000L, TRUE); /* give the user time to think about it */ | |
3229 } | |
3230 curbuf->b_did_warn = TRUE; | |
3231 redraw_cmdline = FALSE; /* don't redraw and erase the message */ | |
3232 if (msg_row < Rows - 1) | |
3233 showmode(); | |
3234 } | |
3235 } | |
3236 | |
3237 /* | |
3238 * Ask for a reply from the user, a 'y' or a 'n'. | |
3239 * No other characters are accepted, the message is repeated until a valid | |
3240 * reply is entered or CTRL-C is hit. | |
3241 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters | |
3242 * from any buffers but directly from the user. | |
3243 * | |
3244 * return the 'y' or 'n' | |
3245 */ | |
3246 int | |
3247 ask_yesno(str, direct) | |
3248 char_u *str; | |
3249 int direct; | |
3250 { | |
3251 int r = ' '; | |
3252 int save_State = State; | |
3253 | |
3254 if (exiting) /* put terminal in raw mode for this question */ | |
3255 settmode(TMODE_RAW); | |
3256 ++no_wait_return; | |
3257 #ifdef USE_ON_FLY_SCROLL | |
3258 dont_scroll = TRUE; /* disallow scrolling here */ | |
3259 #endif | |
3260 State = CONFIRM; /* mouse behaves like with :confirm */ | |
3261 #ifdef FEAT_MOUSE | |
3262 setmouse(); /* disables mouse for xterm */ | |
3263 #endif | |
3264 ++no_mapping; | |
3265 ++allow_keys; /* no mapping here, but recognize keys */ | |
3266 | |
3267 while (r != 'y' && r != 'n') | |
3268 { | |
3269 /* same highlighting as for wait_return */ | |
3270 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str); | |
3271 if (direct) | |
3272 r = get_keystroke(); | |
3273 else | |
1474 | 3274 r = plain_vgetc(); |
7 | 3275 if (r == Ctrl_C || r == ESC) |
3276 r = 'n'; | |
3277 msg_putchar(r); /* show what you typed */ | |
3278 out_flush(); | |
3279 } | |
3280 --no_wait_return; | |
3281 State = save_State; | |
3282 #ifdef FEAT_MOUSE | |
3283 setmouse(); | |
3284 #endif | |
3285 --no_mapping; | |
3286 --allow_keys; | |
3287 | |
3288 return r; | |
3289 } | |
3290 | |
4221 | 3291 #if defined(FEAT_MOUSE) || defined(PROTO) |
3292 /* | |
3293 * Return TRUE if "c" is a mouse key. | |
3294 */ | |
3295 int | |
3296 is_mouse_key(c) | |
3297 int c; | |
3298 { | |
3299 return c == K_LEFTMOUSE | |
3300 || c == K_LEFTMOUSE_NM | |
3301 || c == K_LEFTDRAG | |
3302 || c == K_LEFTRELEASE | |
3303 || c == K_LEFTRELEASE_NM | |
3304 || c == K_MIDDLEMOUSE | |
3305 || c == K_MIDDLEDRAG | |
3306 || c == K_MIDDLERELEASE | |
3307 || c == K_RIGHTMOUSE | |
3308 || c == K_RIGHTDRAG | |
3309 || c == K_RIGHTRELEASE | |
3310 || c == K_MOUSEDOWN | |
3311 || c == K_MOUSEUP | |
3312 || c == K_MOUSELEFT | |
3313 || c == K_MOUSERIGHT | |
3314 || c == K_X1MOUSE | |
3315 || c == K_X1DRAG | |
3316 || c == K_X1RELEASE | |
3317 || c == K_X2MOUSE | |
3318 || c == K_X2DRAG | |
3319 || c == K_X2RELEASE; | |
3320 } | |
3321 #endif | |
3322 | |
7 | 3323 /* |
3324 * Get a key stroke directly from the user. | |
3325 * Ignores mouse clicks and scrollbar events, except a click for the left | |
3326 * button (used at the more prompt). | |
3327 * Doesn't use vgetc(), because it syncs undo and eats mapped characters. | |
3328 * Disadvantage: typeahead is ignored. | |
3329 * Translates the interrupt character for unix to ESC. | |
3330 */ | |
3331 int | |
3332 get_keystroke() | |
3333 { | |
3328 | 3334 char_u *buf = NULL; |
3335 int buflen = 150; | |
3336 int maxlen; | |
7 | 3337 int len = 0; |
3338 int n; | |
3339 int save_mapped_ctrl_c = mapped_ctrl_c; | |
964 | 3340 int waited = 0; |
7 | 3341 |
3342 mapped_ctrl_c = FALSE; /* mappings are not used here */ | |
3343 for (;;) | |
3344 { | |
3345 cursor_on(); | |
3346 out_flush(); | |
3347 | |
3328 | 3348 /* Leave some room for check_termcode() to insert a key code into (max |
3349 * 5 chars plus NUL). And fix_input_buffer() can triple the number of | |
3350 * bytes. */ | |
3351 maxlen = (buflen - 6 - len) / 3; | |
3352 if (buf == NULL) | |
3353 buf = alloc(buflen); | |
3354 else if (maxlen < 10) | |
3355 { | |
3596 | 3356 /* Need some more space. This might happen when receiving a long |
3328 | 3357 * escape sequence. */ |
3358 buflen += 100; | |
3359 buf = vim_realloc(buf, buflen); | |
3360 maxlen = (buflen - 6 - len) / 3; | |
3361 } | |
3362 if (buf == NULL) | |
3363 { | |
3364 do_outofmem_msg((long_u)buflen); | |
3365 return ESC; /* panic! */ | |
3366 } | |
3367 | |
7 | 3368 /* First time: blocking wait. Second time: wait up to 100ms for a |
3328 | 3369 * terminal code to complete. */ |
3370 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0); | |
7 | 3371 if (n > 0) |
3372 { | |
3373 /* Replace zero and CSI by a special key code. */ | |
3374 n = fix_input_buffer(buf + len, n, FALSE); | |
3375 len += n; | |
964 | 3376 waited = 0; |
3377 } | |
3378 else if (len > 0) | |
3379 ++waited; /* keep track of the waiting time */ | |
3380 | |
3381 /* Incomplete termcode and not timed out yet: get more characters */ | |
3328 | 3382 if ((n = check_termcode(1, buf, buflen, &len)) < 0 |
964 | 3383 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) |
7 | 3384 continue; |
964 | 3385 |
2672 | 3386 if (n == KEYLEN_REMOVED) /* key code removed */ |
2721 | 3387 { |
2723 | 3388 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
2721 | 3389 { |
3390 /* Redrawing was postponed, do it now. */ | |
3391 update_screen(0); | |
3392 setcursor(); /* put cursor back where it belongs */ | |
3393 } | |
2672 | 3394 continue; |
2721 | 3395 } |
2672 | 3396 if (n > 0) /* found a termcode: adjust length */ |
7 | 3397 len = n; |
2672 | 3398 if (len == 0) /* nothing typed yet */ |
7 | 3399 continue; |
3400 | |
3401 /* Handle modifier and/or special key code. */ | |
3402 n = buf[0]; | |
3403 if (n == K_SPECIAL) | |
3404 { | |
3405 n = TO_SPECIAL(buf[1], buf[2]); | |
3406 if (buf[1] == KS_MODIFIER | |
3407 || n == K_IGNORE | |
4221 | 3408 || (is_mouse_key(n) && n != K_LEFTMOUSE) |
3409 #ifdef FEAT_GUI | |
7 | 3410 || n == K_VER_SCROLLBAR |
3411 || n == K_HOR_SCROLLBAR | |
3412 #endif | |
3413 ) | |
3414 { | |
3415 if (buf[1] == KS_MODIFIER) | |
3416 mod_mask = buf[2]; | |
3417 len -= 3; | |
3418 if (len > 0) | |
3419 mch_memmove(buf, buf + 3, (size_t)len); | |
3420 continue; | |
3421 } | |
828 | 3422 break; |
7 | 3423 } |
3424 #ifdef FEAT_MBYTE | |
3425 if (has_mbyte) | |
3426 { | |
3427 if (MB_BYTE2LEN(n) > len) | |
3428 continue; /* more bytes to get */ | |
3328 | 3429 buf[len >= buflen ? buflen - 1 : len] = NUL; |
7 | 3430 n = (*mb_ptr2char)(buf); |
3431 } | |
3432 #endif | |
3433 #ifdef UNIX | |
3434 if (n == intr_char) | |
3435 n = ESC; | |
3436 #endif | |
3437 break; | |
3438 } | |
3328 | 3439 vim_free(buf); |
7 | 3440 |
3441 mapped_ctrl_c = save_mapped_ctrl_c; | |
3442 return n; | |
3443 } | |
3444 | |
3445 /* | |
374 | 3446 * Get a number from the user. |
3447 * When "mouse_used" is not NULL allow using the mouse. | |
7 | 3448 */ |
3449 int | |
374 | 3450 get_number(colon, mouse_used) |
3451 int colon; /* allow colon to abort */ | |
3452 int *mouse_used; | |
7 | 3453 { |
3454 int n = 0; | |
3455 int c; | |
810 | 3456 int typed = 0; |
7 | 3457 |
374 | 3458 if (mouse_used != NULL) |
3459 *mouse_used = FALSE; | |
3460 | |
7 | 3461 /* When not printing messages, the user won't know what to type, return a |
3462 * zero (as if CR was hit). */ | |
3463 if (msg_silent != 0) | |
3464 return 0; | |
3465 | |
3466 #ifdef USE_ON_FLY_SCROLL | |
3467 dont_scroll = TRUE; /* disallow scrolling here */ | |
3468 #endif | |
3469 ++no_mapping; | |
3470 ++allow_keys; /* no mapping here, but recognize keys */ | |
3471 for (;;) | |
3472 { | |
3473 windgoto(msg_row, msg_col); | |
3474 c = safe_vgetc(); | |
3475 if (VIM_ISDIGIT(c)) | |
3476 { | |
3477 n = n * 10 + c - '0'; | |
3478 msg_putchar(c); | |
810 | 3479 ++typed; |
7 | 3480 } |
3481 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) | |
3482 { | |
810 | 3483 if (typed > 0) |
3484 { | |
3485 MSG_PUTS("\b \b"); | |
3486 --typed; | |
3487 } | |
7 | 3488 n /= 10; |
3489 } | |
374 | 3490 #ifdef FEAT_MOUSE |
3491 else if (mouse_used != NULL && c == K_LEFTMOUSE) | |
3492 { | |
3493 *mouse_used = TRUE; | |
3494 n = mouse_row + 1; | |
3495 break; | |
3496 } | |
3497 #endif | |
7 | 3498 else if (n == 0 && c == ':' && colon) |
3499 { | |
3500 stuffcharReadbuff(':'); | |
3501 if (!exmode_active) | |
3502 cmdline_row = msg_row; | |
3503 skip_redraw = TRUE; /* skip redraw once */ | |
3504 do_redraw = FALSE; | |
3505 break; | |
3506 } | |
3507 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) | |
3508 break; | |
3509 } | |
3510 --no_mapping; | |
3511 --allow_keys; | |
3512 return n; | |
3513 } | |
3514 | |
323 | 3515 /* |
3516 * Ask the user to enter a number. | |
374 | 3517 * When "mouse_used" is not NULL allow using the mouse and in that case return |
3518 * the line number. | |
323 | 3519 */ |
3520 int | |
374 | 3521 prompt_for_number(mouse_used) |
3522 int *mouse_used; | |
323 | 3523 { |
3524 int i; | |
344 | 3525 int save_cmdline_row; |
3526 int save_State; | |
323 | 3527 |
3528 /* When using ":silent" assume that <CR> was entered. */ | |
375 | 3529 if (mouse_used != NULL) |
1733 | 3530 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
375 | 3531 else |
1733 | 3532 MSG_PUTS(_("Type number and <Enter> (empty cancels): ")); |
344 | 3533 |
957 | 3534 /* Set the state such that text can be selected/copied/pasted and we still |
3535 * get mouse events. */ | |
344 | 3536 save_cmdline_row = cmdline_row; |
957 | 3537 cmdline_row = 0; |
344 | 3538 save_State = State; |
957 | 3539 State = CMDLINE; |
374 | 3540 |
3541 i = get_number(TRUE, mouse_used); | |
3542 if (KeyTyped) | |
3543 { | |
3544 /* don't call wait_return() now */ | |
3545 /* msg_putchar('\n'); */ | |
323 | 3546 cmdline_row = msg_row - 1; |
3547 need_wait_return = FALSE; | |
3548 msg_didany = FALSE; | |
1938 | 3549 msg_didout = FALSE; |
323 | 3550 } |
344 | 3551 else |
3552 cmdline_row = save_cmdline_row; | |
3553 State = save_State; | |
3554 | |
323 | 3555 return i; |
3556 } | |
3557 | |
7 | 3558 void |
3559 msgmore(n) | |
3560 long n; | |
3561 { | |
3562 long pn; | |
3563 | |
3564 if (global_busy /* no messages now, wait until global is finished */ | |
3565 || !messaging()) /* 'lazyredraw' set, don't do messages now */ | |
3566 return; | |
3567 | |
135 | 3568 /* We don't want to overwrite another important message, but do overwrite |
3569 * a previous "more lines" or "fewer lines" message, so that "5dd" and | |
3570 * then "put" reports the last action. */ | |
3571 if (keep_msg != NULL && !keep_msg_more) | |
3572 return; | |
3573 | |
7 | 3574 if (n > 0) |
3575 pn = n; | |
3576 else | |
3577 pn = -n; | |
3578 | |
3579 if (pn > p_report) | |
3580 { | |
3581 if (pn == 1) | |
3582 { | |
3583 if (n > 0) | |
2768 | 3584 vim_strncpy(msg_buf, (char_u *)_("1 more line"), |
3585 MSG_BUF_LEN - 1); | |
7 | 3586 else |
2768 | 3587 vim_strncpy(msg_buf, (char_u *)_("1 line less"), |
3588 MSG_BUF_LEN - 1); | |
7 | 3589 } |
3590 else | |
3591 { | |
3592 if (n > 0) | |
2768 | 3593 vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
3594 _("%ld more lines"), pn); | |
7 | 3595 else |
2768 | 3596 vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
3597 _("%ld fewer lines"), pn); | |
7 | 3598 } |
3599 if (got_int) | |
2768 | 3600 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN); |
7 | 3601 if (msg(msg_buf)) |
3602 { | |
680 | 3603 set_keep_msg(msg_buf, 0); |
135 | 3604 keep_msg_more = TRUE; |
7 | 3605 } |
3606 } | |
3607 } | |
3608 | |
3609 /* | |
3610 * flush map and typeahead buffers and give a warning for an error | |
3611 */ | |
3612 void | |
3613 beep_flush() | |
3614 { | |
3615 if (emsg_silent == 0) | |
3616 { | |
3617 flush_buffers(FALSE); | |
3618 vim_beep(); | |
3619 } | |
3620 } | |
3621 | |
3622 /* | |
3623 * give a warning for an error | |
3624 */ | |
3625 void | |
3626 vim_beep() | |
3627 { | |
3628 if (emsg_silent == 0) | |
3629 { | |
3630 if (p_vb | |
3631 #ifdef FEAT_GUI | |
3632 /* While the GUI is starting up the termcap is set for the GUI | |
3633 * but the output still goes to a terminal. */ | |
3634 && !(gui.in_use && gui.starting) | |
3635 #endif | |
3636 ) | |
3637 { | |
3638 out_str(T_VB); | |
3639 } | |
3640 else | |
3641 { | |
3642 #ifdef MSDOS | |
3643 /* | |
3644 * The number of beeps outputted is reduced to avoid having to wait | |
3645 * for all the beeps to finish. This is only a problem on systems | |
3646 * where the beeps don't overlap. | |
3647 */ | |
3648 if (beep_count == 0 || beep_count == 10) | |
3649 { | |
3650 out_char(BELL); | |
3651 beep_count = 1; | |
3652 } | |
3653 else | |
3654 ++beep_count; | |
3655 #else | |
3656 out_char(BELL); | |
3657 #endif | |
3658 } | |
169 | 3659 |
3660 /* When 'verbose' is set and we are sourcing a script or executing a | |
3661 * function give the user a hint where the beep comes from. */ | |
3662 if (vim_strchr(p_debug, 'e') != NULL) | |
3663 { | |
3664 msg_source(hl_attr(HLF_W)); | |
3665 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W)); | |
3666 } | |
7 | 3667 } |
3668 } | |
3669 | |
3670 /* | |
3671 * To get the "real" home directory: | |
3672 * - get value of $HOME | |
3673 * For Unix: | |
3674 * - go to that directory | |
3675 * - do mch_dirname() to get the real name of that directory. | |
3676 * This also works with mounts and links. | |
3677 * Don't do this for MS-DOS, it will change the "current dir" for a drive. | |
3678 */ | |
3679 static char_u *homedir = NULL; | |
3680 | |
3681 void | |
3682 init_homedir() | |
3683 { | |
3684 char_u *var; | |
3685 | |
170 | 3686 /* In case we are called a second time (when 'encoding' changes). */ |
3687 vim_free(homedir); | |
3688 homedir = NULL; | |
3689 | |
7 | 3690 #ifdef VMS |
3691 var = mch_getenv((char_u *)"SYS$LOGIN"); | |
3692 #else | |
3693 var = mch_getenv((char_u *)"HOME"); | |
3694 #endif | |
3695 | |
3696 if (var != NULL && *var == NUL) /* empty is same as not set */ | |
3697 var = NULL; | |
3698 | |
3699 #ifdef WIN3264 | |
3700 /* | |
3701 * Weird but true: $HOME may contain an indirect reference to another | |
3702 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set | |
3703 * when $HOME is being set. | |
3704 */ | |
3705 if (var != NULL && *var == '%') | |
3706 { | |
3707 char_u *p; | |
3708 char_u *exp; | |
3709 | |
3710 p = vim_strchr(var + 1, '%'); | |
3711 if (p != NULL) | |
3712 { | |
419 | 3713 vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
7 | 3714 exp = mch_getenv(NameBuff); |
3715 if (exp != NULL && *exp != NUL | |
3716 && STRLEN(exp) + STRLEN(p) < MAXPATHL) | |
3717 { | |
274 | 3718 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
7 | 3719 var = NameBuff; |
3720 /* Also set $HOME, it's needed for _viminfo. */ | |
3721 vim_setenv((char_u *)"HOME", NameBuff); | |
3722 } | |
3723 } | |
3724 } | |
3725 | |
3726 /* | |
3727 * Typically, $HOME is not defined on Windows, unless the user has | |
3728 * specifically defined it for Vim's sake. However, on Windows NT | |
3729 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for | |
3730 * each user. Try constructing $HOME from these. | |
3731 */ | |
3732 if (var == NULL) | |
3733 { | |
3734 char_u *homedrive, *homepath; | |
3735 | |
3736 homedrive = mch_getenv((char_u *)"HOMEDRIVE"); | |
3737 homepath = mch_getenv((char_u *)"HOMEPATH"); | |
2030
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3738 if (homepath == NULL || *homepath == NUL) |
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3739 homepath = "\\"; |
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3740 if (homedrive != NULL |
7 | 3741 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
3742 { | |
3743 sprintf((char *)NameBuff, "%s%s", homedrive, homepath); | |
3744 if (NameBuff[0] != NUL) | |
3745 { | |
3746 var = NameBuff; | |
3747 /* Also set $HOME, it's needed for _viminfo. */ | |
3748 vim_setenv((char_u *)"HOME", NameBuff); | |
3749 } | |
3750 } | |
3751 } | |
170 | 3752 |
3753 # if defined(FEAT_MBYTE) | |
3754 if (enc_utf8 && var != NULL) | |
3755 { | |
3756 int len; | |
2786 | 3757 char_u *pp = NULL; |
170 | 3758 |
3759 /* Convert from active codepage to UTF-8. Other conversions are | |
3760 * not done, because they would fail for non-ASCII characters. */ | |
835 | 3761 acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
170 | 3762 if (pp != NULL) |
3763 { | |
3764 homedir = pp; | |
3765 return; | |
3766 } | |
3767 } | |
3768 # endif | |
7 | 3769 #endif |
3770 | |
3771 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) | |
3772 /* | |
3773 * Default home dir is C:/ | |
3774 * Best assumption we can make in such a situation. | |
3775 */ | |
3776 if (var == NULL) | |
3777 var = "C:/"; | |
3778 #endif | |
3779 if (var != NULL) | |
3780 { | |
3781 #ifdef UNIX | |
3782 /* | |
3783 * Change to the directory and get the actual path. This resolves | |
3784 * links. Don't do it when we can't return. | |
3785 */ | |
3786 if (mch_dirname(NameBuff, MAXPATHL) == OK | |
3787 && mch_chdir((char *)NameBuff) == 0) | |
3788 { | |
3789 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) | |
3790 var = IObuff; | |
3791 if (mch_chdir((char *)NameBuff) != 0) | |
3792 EMSG(_(e_prev_dir)); | |
3793 } | |
3794 #endif | |
3795 homedir = vim_strsave(var); | |
3796 } | |
3797 } | |
3798 | |
359 | 3799 #if defined(EXITFREE) || defined(PROTO) |
3800 void | |
3801 free_homedir() | |
3802 { | |
3803 vim_free(homedir); | |
3804 } | |
3744 | 3805 |
3806 # ifdef FEAT_CMDL_COMPL | |
3807 void | |
3808 free_users() | |
3809 { | |
3810 ga_clear_strings(&ga_users); | |
3811 } | |
3812 # endif | |
359 | 3813 #endif |
3814 | |
7 | 3815 /* |
1408 | 3816 * Call expand_env() and store the result in an allocated string. |
3817 * This is not very memory efficient, this expects the result to be freed | |
3818 * again soon. | |
3819 */ | |
3820 char_u * | |
3821 expand_env_save(src) | |
3822 char_u *src; | |
3823 { | |
3824 return expand_env_save_opt(src, FALSE); | |
3825 } | |
3826 | |
3827 /* | |
3828 * Idem, but when "one" is TRUE handle the string as one file name, only | |
3829 * expand "~" at the start. | |
3830 */ | |
3831 char_u * | |
3832 expand_env_save_opt(src, one) | |
3833 char_u *src; | |
3834 int one; | |
3835 { | |
3836 char_u *p; | |
3837 | |
3838 p = alloc(MAXPATHL); | |
3839 if (p != NULL) | |
3840 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); | |
3841 return p; | |
3842 } | |
3843 | |
3844 /* | |
7 | 3845 * Expand environment variable with path name. |
3846 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. | |
1408 | 3847 * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
7 | 3848 * If anything fails no expansion is done and dst equals src. |
3849 */ | |
3850 void | |
3851 expand_env(src, dst, dstlen) | |
3852 char_u *src; /* input string e.g. "$HOME/vim.hlp" */ | |
3853 char_u *dst; /* where to put the result */ | |
3854 int dstlen; /* maximum length of the result */ | |
3855 { | |
1408 | 3856 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
7 | 3857 } |
3858 | |
3859 void | |
1408 | 3860 expand_env_esc(srcp, dst, dstlen, esc, one, startstr) |
374 | 3861 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */ |
7 | 3862 char_u *dst; /* where to put the result */ |
3863 int dstlen; /* maximum length of the result */ | |
3864 int esc; /* escape spaces in expanded variables */ | |
1408 | 3865 int one; /* "srcp" is one file name */ |
374 | 3866 char_u *startstr; /* start again after this (can be NULL) */ |
3867 { | |
3868 char_u *src; | |
7 | 3869 char_u *tail; |
3870 int c; | |
3871 char_u *var; | |
3872 int copy_char; | |
3873 int mustfree; /* var was allocated, need to free it later */ | |
3874 int at_start = TRUE; /* at start of a name */ | |
374 | 3875 int startstr_len = 0; |
3876 | |
3877 if (startstr != NULL) | |
835 | 3878 startstr_len = (int)STRLEN(startstr); |
374 | 3879 |
3880 src = skipwhite(srcp); | |
7 | 3881 --dstlen; /* leave one char space for "\," */ |
3882 while (*src && dstlen > 0) | |
3883 { | |
3884 copy_char = TRUE; | |
22 | 3885 if ((*src == '$' |
3886 #ifdef VMS | |
3887 && at_start | |
3888 #endif | |
3889 ) | |
7 | 3890 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
3891 || *src == '%' | |
3892 #endif | |
3893 || (*src == '~' && at_start)) | |
3894 { | |
3895 mustfree = FALSE; | |
3896 | |
3897 /* | |
3898 * The variable name is copied into dst temporarily, because it may | |
3899 * be a string in read-only memory and a NUL needs to be appended. | |
3900 */ | |
3901 if (*src != '~') /* environment var */ | |
3902 { | |
3903 tail = src + 1; | |
3904 var = dst; | |
3905 c = dstlen - 1; | |
3906 | |
3907 #ifdef UNIX | |
3908 /* Unix has ${var-name} type environment vars */ | |
3909 if (*tail == '{' && !vim_isIDc('{')) | |
3910 { | |
3911 tail++; /* ignore '{' */ | |
3912 while (c-- > 0 && *tail && *tail != '}') | |
3913 *var++ = *tail++; | |
3914 } | |
3915 else | |
3916 #endif | |
3917 { | |
3918 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) | |
3919 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
3920 || (*src == '%' && *tail != '%') | |
3921 #endif | |
3922 )) | |
3923 { | |
3924 #ifdef OS2 /* env vars only in uppercase */ | |
3925 *var++ = TOUPPER_LOC(*tail); | |
3926 tail++; /* toupper() may be a macro! */ | |
3927 #else | |
3928 *var++ = *tail++; | |
3929 #endif | |
3930 } | |
3931 } | |
3932 | |
3933 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) | |
3934 # ifdef UNIX | |
3935 if (src[1] == '{' && *tail != '}') | |
3936 # else | |
3937 if (*src == '%' && *tail != '%') | |
3938 # endif | |
3939 var = NULL; | |
3940 else | |
3941 { | |
3942 # ifdef UNIX | |
3943 if (src[1] == '{') | |
3944 # else | |
3945 if (*src == '%') | |
3946 #endif | |
3947 ++tail; | |
3948 #endif | |
3949 *var = NUL; | |
3950 var = vim_getenv(dst, &mustfree); | |
3951 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) | |
3952 } | |
3953 #endif | |
3954 } | |
3955 /* home directory */ | |
3956 else if ( src[1] == NUL | |
3957 || vim_ispathsep(src[1]) | |
3958 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) | |
3959 { | |
3960 var = homedir; | |
3961 tail = src + 1; | |
3962 } | |
3963 else /* user directory */ | |
3964 { | |
3965 #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) | |
3966 /* | |
3967 * Copy ~user to dst[], so we can put a NUL after it. | |
3968 */ | |
3969 tail = src; | |
3970 var = dst; | |
3971 c = dstlen - 1; | |
3972 while ( c-- > 0 | |
3973 && *tail | |
3974 && vim_isfilec(*tail) | |
3975 && !vim_ispathsep(*tail)) | |
3976 *var++ = *tail++; | |
3977 *var = NUL; | |
3978 # ifdef UNIX | |
3979 /* | |
3980 * If the system supports getpwnam(), use it. | |
3981 * Otherwise, or if getpwnam() fails, the shell is used to | |
3982 * expand ~user. This is slower and may fail if the shell | |
3983 * does not support ~user (old versions of /bin/sh). | |
3984 */ | |
3985 # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) | |
3986 { | |
3987 struct passwd *pw; | |
3988 | |
626 | 3989 /* Note: memory allocated by getpwnam() is never freed. |
3990 * Calling endpwent() apparently doesn't help. */ | |
7 | 3991 pw = getpwnam((char *)dst + 1); |
3992 if (pw != NULL) | |
3993 var = (char_u *)pw->pw_dir; | |
3994 else | |
3995 var = NULL; | |
3996 } | |
3997 if (var == NULL) | |
3998 # endif | |
3999 { | |
4000 expand_T xpc; | |
4001 | |
4002 ExpandInit(&xpc); | |
4003 xpc.xp_context = EXPAND_FILES; | |
4004 var = ExpandOne(&xpc, dst, NULL, | |
4005 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); | |
4006 mustfree = TRUE; | |
4007 } | |
4008 | |
4009 # else /* !UNIX, thus VMS */ | |
4010 /* | |
4011 * USER_HOME is a comma-separated list of | |
4012 * directories to search for the user account in. | |
4013 */ | |
4014 { | |
4015 char_u test[MAXPATHL], paths[MAXPATHL]; | |
4016 char_u *path, *next_path, *ptr; | |
4017 struct stat st; | |
4018 | |
4019 STRCPY(paths, USER_HOME); | |
4020 next_path = paths; | |
4021 while (*next_path) | |
4022 { | |
4023 for (path = next_path; *next_path && *next_path != ','; | |
4024 next_path++); | |
4025 if (*next_path) | |
4026 *next_path++ = NUL; | |
4027 STRCPY(test, path); | |
4028 STRCAT(test, "/"); | |
4029 STRCAT(test, dst + 1); | |
4030 if (mch_stat(test, &st) == 0) | |
4031 { | |
4032 var = alloc(STRLEN(test) + 1); | |
4033 STRCPY(var, test); | |
4034 mustfree = TRUE; | |
4035 break; | |
4036 } | |
4037 } | |
4038 } | |
4039 # endif /* UNIX */ | |
4040 #else | |
4041 /* cannot expand user's home directory, so don't try */ | |
4042 var = NULL; | |
4043 tail = (char_u *)""; /* for gcc */ | |
4044 #endif /* UNIX || VMS */ | |
4045 } | |
4046 | |
4047 #ifdef BACKSLASH_IN_FILENAME | |
4048 /* If 'shellslash' is set change backslashes to forward slashes. | |
4049 * Can't use slash_adjust(), p_ssl may be set temporarily. */ | |
4050 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) | |
4051 { | |
4052 char_u *p = vim_strsave(var); | |
4053 | |
4054 if (p != NULL) | |
4055 { | |
4056 if (mustfree) | |
4057 vim_free(var); | |
4058 var = p; | |
4059 mustfree = TRUE; | |
4060 forward_slash(var); | |
4061 } | |
4062 } | |
4063 #endif | |
4064 | |
4065 /* If "var" contains white space, escape it with a backslash. | |
4066 * Required for ":e ~/tt" when $HOME includes a space. */ | |
4067 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) | |
4068 { | |
4069 char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); | |
4070 | |
4071 if (p != NULL) | |
4072 { | |
4073 if (mustfree) | |
4074 vim_free(var); | |
4075 var = p; | |
4076 mustfree = TRUE; | |
4077 } | |
4078 } | |
4079 | |
4080 if (var != NULL && *var != NUL | |
4081 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) | |
4082 { | |
4083 STRCPY(dst, var); | |
4084 dstlen -= (int)STRLEN(var); | |
835 | 4085 c = (int)STRLEN(var); |
7 | 4086 /* if var[] ends in a path separator and tail[] starts |
4087 * with it, skip a character */ | |
39 | 4088 if (*var != NUL && after_pathsep(dst, dst + c) |
7 | 4089 #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
4090 && dst[-1] != ':' | |
4091 #endif | |
4092 && vim_ispathsep(*tail)) | |
4093 ++tail; | |
39 | 4094 dst += c; |
7 | 4095 src = tail; |
4096 copy_char = FALSE; | |
4097 } | |
4098 if (mustfree) | |
4099 vim_free(var); | |
4100 } | |
4101 | |
4102 if (copy_char) /* copy at least one char */ | |
4103 { | |
4104 /* | |
1224 | 4105 * Recognize the start of a new name, for '~'. |
1408 | 4106 * Don't do this when "one" is TRUE, to avoid expanding "~" in |
4107 * ":edit foo ~ foo". | |
7 | 4108 */ |
4109 at_start = FALSE; | |
4110 if (src[0] == '\\' && src[1] != NUL) | |
4111 { | |
4112 *dst++ = *src++; | |
4113 --dstlen; | |
4114 } | |
1408 | 4115 else if ((src[0] == ' ' || src[0] == ',') && !one) |
7 | 4116 at_start = TRUE; |
4117 *dst++ = *src++; | |
4118 --dstlen; | |
374 | 4119 |
4120 if (startstr != NULL && src - startstr_len >= srcp | |
4121 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0) | |
4122 at_start = TRUE; | |
7 | 4123 } |
4124 } | |
4125 *dst = NUL; | |
4126 } | |
4127 | |
4128 /* | |
4129 * Vim's version of getenv(). | |
4130 * Special handling of $HOME, $VIM and $VIMRUNTIME. | |
196 | 4131 * Also does ACP to 'enc' conversion for Win32. |
2786 | 4132 * "mustfree" is set to TRUE when returned is allocated, it must be |
4133 * initialized to FALSE by the caller. | |
7 | 4134 */ |
4135 char_u * | |
4136 vim_getenv(name, mustfree) | |
4137 char_u *name; | |
2786 | 4138 int *mustfree; |
7 | 4139 { |
4140 char_u *p; | |
4141 char_u *pend; | |
4142 int vimruntime; | |
4143 | |
4144 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) | |
4145 /* use "C:/" when $HOME is not set */ | |
4146 if (STRCMP(name, "HOME") == 0) | |
4147 return homedir; | |
4148 #endif | |
4149 | |
4150 p = mch_getenv(name); | |
4151 if (p != NULL && *p == NUL) /* empty is the same as not set */ | |
4152 p = NULL; | |
4153 | |
4154 if (p != NULL) | |
170 | 4155 { |
4156 #if defined(FEAT_MBYTE) && defined(WIN3264) | |
4157 if (enc_utf8) | |
4158 { | |
4159 int len; | |
2786 | 4160 char_u *pp = NULL; |
170 | 4161 |
4162 /* Convert from active codepage to UTF-8. Other conversions are | |
4163 * not done, because they would fail for non-ASCII characters. */ | |
835 | 4164 acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
170 | 4165 if (pp != NULL) |
4166 { | |
4167 p = pp; | |
4168 *mustfree = TRUE; | |
4169 } | |
4170 } | |
4171 #endif | |
7 | 4172 return p; |
170 | 4173 } |
7 | 4174 |
4175 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); | |
4176 if (!vimruntime && STRCMP(name, "VIM") != 0) | |
4177 return NULL; | |
4178 | |
4179 /* | |
4180 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. | |
4181 * Don't do this when default_vimruntime_dir is non-empty. | |
4182 */ | |
4183 if (vimruntime | |
4184 #ifdef HAVE_PATHDEF | |
4185 && *default_vimruntime_dir == NUL | |
4186 #endif | |
4187 ) | |
4188 { | |
4189 p = mch_getenv((char_u *)"VIM"); | |
4190 if (p != NULL && *p == NUL) /* empty is the same as not set */ | |
4191 p = NULL; | |
4192 if (p != NULL) | |
4193 { | |
4194 p = vim_version_dir(p); | |
4195 if (p != NULL) | |
4196 *mustfree = TRUE; | |
4197 else | |
4198 p = mch_getenv((char_u *)"VIM"); | |
170 | 4199 |
4200 #if defined(FEAT_MBYTE) && defined(WIN3264) | |
4201 if (enc_utf8) | |
4202 { | |
4203 int len; | |
2786 | 4204 char_u *pp = NULL; |
170 | 4205 |
4206 /* Convert from active codepage to UTF-8. Other conversions | |
4207 * are not done, because they would fail for non-ASCII | |
4208 * characters. */ | |
835 | 4209 acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
170 | 4210 if (pp != NULL) |
4211 { | |
2786 | 4212 if (*mustfree) |
170 | 4213 vim_free(p); |
4214 p = pp; | |
4215 *mustfree = TRUE; | |
4216 } | |
4217 } | |
4218 #endif | |
7 | 4219 } |
4220 } | |
4221 | |
4222 /* | |
4223 * When expanding $VIM or $VIMRUNTIME fails, try using: | |
4224 * - the directory name from 'helpfile' (unless it contains '$') | |
4225 * - the executable name from argv[0] | |
4226 */ | |
4227 if (p == NULL) | |
4228 { | |
4229 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) | |
4230 p = p_hf; | |
4231 #ifdef USE_EXE_NAME | |
4232 /* | |
4233 * Use the name of the executable, obtained from argv[0]. | |
4234 */ | |
4235 else | |
4236 p = exe_name; | |
4237 #endif | |
4238 if (p != NULL) | |
4239 { | |
4240 /* remove the file name */ | |
4241 pend = gettail(p); | |
4242 | |
4243 /* remove "doc/" from 'helpfile', if present */ | |
4244 if (p == p_hf) | |
4245 pend = remove_tail(p, pend, (char_u *)"doc"); | |
4246 | |
4247 #ifdef USE_EXE_NAME | |
4248 # ifdef MACOS_X | |
768 | 4249 /* remove "MacOS" from exe_name and add "Resources/vim" */ |
7 | 4250 if (p == exe_name) |
4251 { | |
4252 char_u *pend1; | |
768 | 4253 char_u *pnew; |
4254 | |
4255 pend1 = remove_tail(p, pend, (char_u *)"MacOS"); | |
4256 if (pend1 != pend) | |
4257 { | |
4258 pnew = alloc((unsigned)(pend1 - p) + 15); | |
4259 if (pnew != NULL) | |
4260 { | |
4261 STRNCPY(pnew, p, (pend1 - p)); | |
4262 STRCPY(pnew + (pend1 - p), "Resources/vim"); | |
4263 p = pnew; | |
4264 pend = p + STRLEN(p); | |
4265 } | |
4266 } | |
7 | 4267 } |
4268 # endif | |
4269 /* remove "src/" from exe_name, if present */ | |
4270 if (p == exe_name) | |
4271 pend = remove_tail(p, pend, (char_u *)"src"); | |
4272 #endif | |
4273 | |
4274 /* for $VIM, remove "runtime/" or "vim54/", if present */ | |
4275 if (!vimruntime) | |
4276 { | |
4277 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); | |
4278 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); | |
4279 } | |
4280 | |
4281 /* remove trailing path separator */ | |
4282 #ifndef MACOS_CLASSIC | |
4283 /* With MacOS path (with colons) the final colon is required */ | |
1532 | 4284 /* to avoid confusion between absolute and relative path */ |
39 | 4285 if (pend > p && after_pathsep(p, pend)) |
7 | 4286 --pend; |
4287 #endif | |
4288 | |
768 | 4289 #ifdef MACOS_X |
4290 if (p == exe_name || p == p_hf) | |
4291 #endif | |
4292 /* check that the result is a directory name */ | |
4293 p = vim_strnsave(p, (int)(pend - p)); | |
7 | 4294 |
4295 if (p != NULL && !mch_isdir(p)) | |
4296 { | |
4297 vim_free(p); | |
4298 p = NULL; | |
4299 } | |
4300 else | |
4301 { | |
4302 #ifdef USE_EXE_NAME | |
4303 /* may add "/vim54" or "/runtime" if it exists */ | |
4304 if (vimruntime && (pend = vim_version_dir(p)) != NULL) | |
4305 { | |
4306 vim_free(p); | |
4307 p = pend; | |
4308 } | |
4309 #endif | |
4310 *mustfree = TRUE; | |
4311 } | |
4312 } | |
4313 } | |
4314 | |
4315 #ifdef HAVE_PATHDEF | |
4316 /* When there is a pathdef.c file we can use default_vim_dir and | |
4317 * default_vimruntime_dir */ | |
4318 if (p == NULL) | |
4319 { | |
4320 /* Only use default_vimruntime_dir when it is not empty */ | |
4321 if (vimruntime && *default_vimruntime_dir != NUL) | |
4322 { | |
4323 p = default_vimruntime_dir; | |
4324 *mustfree = FALSE; | |
4325 } | |
4326 else if (*default_vim_dir != NUL) | |
4327 { | |
4328 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) | |
4329 *mustfree = TRUE; | |
4330 else | |
4331 { | |
4332 p = default_vim_dir; | |
4333 *mustfree = FALSE; | |
4334 } | |
4335 } | |
4336 } | |
4337 #endif | |
4338 | |
4339 /* | |
4340 * Set the environment variable, so that the new value can be found fast | |
4341 * next time, and others can also use it (e.g. Perl). | |
4342 */ | |
4343 if (p != NULL) | |
4344 { | |
4345 if (vimruntime) | |
4346 { | |
4347 vim_setenv((char_u *)"VIMRUNTIME", p); | |
4348 didset_vimruntime = TRUE; | |
4349 } | |
4350 else | |
4351 { | |
4352 vim_setenv((char_u *)"VIM", p); | |
4353 didset_vim = TRUE; | |
4354 } | |
4355 } | |
4356 return p; | |
4357 } | |
4358 | |
4359 /* | |
4360 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. | |
4361 * Return NULL if not, return its name in allocated memory otherwise. | |
4362 */ | |
4363 static char_u * | |
4364 vim_version_dir(vimdir) | |
4365 char_u *vimdir; | |
4366 { | |
4367 char_u *p; | |
4368 | |
4369 if (vimdir == NULL || *vimdir == NUL) | |
4370 return NULL; | |
4371 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); | |
4372 if (p != NULL && mch_isdir(p)) | |
4373 return p; | |
4374 vim_free(p); | |
4375 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); | |
4376 if (p != NULL && mch_isdir(p)) | |
4377 return p; | |
4378 vim_free(p); | |
4379 return NULL; | |
4380 } | |
4381 | |
4382 /* | |
4383 * If the string between "p" and "pend" ends in "name/", return "pend" minus | |
4384 * the length of "name/". Otherwise return "pend". | |
4385 */ | |
4386 static char_u * | |
4387 remove_tail(p, pend, name) | |
4388 char_u *p; | |
4389 char_u *pend; | |
4390 char_u *name; | |
4391 { | |
4392 int len = (int)STRLEN(name) + 1; | |
4393 char_u *newend = pend - len; | |
4394 | |
4395 if (newend >= p | |
4396 && fnamencmp(newend, name, len - 1) == 0 | |
39 | 4397 && (newend == p || after_pathsep(p, newend))) |
7 | 4398 return newend; |
4399 return pend; | |
4400 } | |
4401 | |
4402 /* | |
4403 * Our portable version of setenv. | |
4404 */ | |
4405 void | |
4406 vim_setenv(name, val) | |
4407 char_u *name; | |
4408 char_u *val; | |
4409 { | |
4410 #ifdef HAVE_SETENV | |
4411 mch_setenv((char *)name, (char *)val, 1); | |
4412 #else | |
4413 char_u *envbuf; | |
4414 | |
4415 /* | |
4416 * Putenv does not copy the string, it has to remain | |
4417 * valid. The allocated memory will never be freed. | |
4418 */ | |
4419 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); | |
4420 if (envbuf != NULL) | |
4421 { | |
4422 sprintf((char *)envbuf, "%s=%s", name, val); | |
4423 putenv((char *)envbuf); | |
4424 } | |
4425 #endif | |
3382 | 4426 #ifdef FEAT_GETTEXT |
4427 /* | |
4428 * When setting $VIMRUNTIME adjust the directory to find message | |
4429 * translations to $VIMRUNTIME/lang. | |
4430 */ | |
4431 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) | |
4432 { | |
4433 char_u *buf = concat_str(val, (char_u *)"/lang"); | |
4434 | |
4435 if (buf != NULL) | |
4436 { | |
4437 bindtextdomain(VIMPACKAGE, (char *)buf); | |
4438 vim_free(buf); | |
4439 } | |
4440 } | |
4441 #endif | |
7 | 4442 } |
4443 | |
4444 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
4445 /* | |
4446 * Function given to ExpandGeneric() to obtain an environment variable name. | |
4447 */ | |
4448 char_u * | |
4449 get_env_name(xp, idx) | |
1883 | 4450 expand_T *xp UNUSED; |
7 | 4451 int idx; |
4452 { | |
4453 # if defined(AMIGA) || defined(__MRC__) || defined(__SC__) | |
4454 /* | |
4455 * No environ[] on the Amiga and on the Mac (using MPW). | |
4456 */ | |
4457 return NULL; | |
4458 # else | |
4459 # ifndef __WIN32__ | |
4460 /* Borland C++ 5.2 has this in a header file. */ | |
4461 extern char **environ; | |
4462 # endif | |
17 | 4463 # define ENVNAMELEN 100 |
4464 static char_u name[ENVNAMELEN]; | |
7 | 4465 char_u *str; |
4466 int n; | |
4467 | |
4468 str = (char_u *)environ[idx]; | |
4469 if (str == NULL) | |
4470 return NULL; | |
4471 | |
17 | 4472 for (n = 0; n < ENVNAMELEN - 1; ++n) |
7 | 4473 { |
4474 if (str[n] == '=' || str[n] == NUL) | |
4475 break; | |
4476 name[n] = str[n]; | |
4477 } | |
4478 name[n] = NUL; | |
4479 return name; | |
4480 # endif | |
4481 } | |
3744 | 4482 |
4483 /* | |
4484 * Find all user names for user completion. | |
4485 * Done only once and then cached. | |
4486 */ | |
4487 static void | |
4488 init_users() { | |
4489 static int lazy_init_done = FALSE; | |
4490 | |
4491 if (lazy_init_done) | |
4492 return; | |
4493 | |
4494 lazy_init_done = TRUE; | |
4495 ga_init2(&ga_users, sizeof(char_u *), 20); | |
4496 | |
4497 # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) | |
4498 { | |
4499 char_u* user; | |
4500 struct passwd* pw; | |
4501 | |
4502 setpwent(); | |
4503 while ((pw = getpwent()) != NULL) | |
4504 /* pw->pw_name shouldn't be NULL but just in case... */ | |
4505 if (pw->pw_name != NULL) | |
4506 { | |
4507 if (ga_grow(&ga_users, 1) == FAIL) | |
4508 break; | |
4509 user = vim_strsave((char_u*)pw->pw_name); | |
4510 if (user == NULL) | |
4511 break; | |
4512 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user; | |
4513 } | |
4514 endpwent(); | |
4515 } | |
4516 # endif | |
4517 } | |
4518 | |
4519 /* | |
4520 * Function given to ExpandGeneric() to obtain an user names. | |
4521 */ | |
4522 char_u* | |
4523 get_users(xp, idx) | |
4524 expand_T *xp UNUSED; | |
4525 int idx; | |
4526 { | |
4527 init_users(); | |
4528 if (idx < ga_users.ga_len) | |
4529 return ((char_u **)ga_users.ga_data)[idx]; | |
4530 return NULL; | |
4531 } | |
4532 | |
4533 /* | |
4534 * Check whether name matches a user name. Return: | |
4535 * 0 if name does not match any user name. | |
4536 * 1 if name partially matches the beginning of a user name. | |
4537 * 2 is name fully matches a user name. | |
4538 */ | |
4539 int match_user(name) | |
4540 char_u* name; | |
4541 { | |
4542 int i; | |
4543 int n = (int)STRLEN(name); | |
4544 int result = 0; | |
4545 | |
4546 init_users(); | |
4547 for (i = 0; i < ga_users.ga_len; i++) | |
4548 { | |
4549 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0) | |
4550 return 2; /* full match */ | |
4551 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0) | |
4552 result = 1; /* partial match */ | |
4553 } | |
4554 return result; | |
4555 } | |
7 | 4556 #endif |
4557 | |
4558 /* | |
4559 * Replace home directory by "~" in each space or comma separated file name in | |
4560 * 'src'. | |
4561 * If anything fails (except when out of space) dst equals src. | |
4562 */ | |
4563 void | |
4564 home_replace(buf, src, dst, dstlen, one) | |
4565 buf_T *buf; /* when not NULL, check for help files */ | |
4566 char_u *src; /* input file name */ | |
4567 char_u *dst; /* where to put the result */ | |
4568 int dstlen; /* maximum length of the result */ | |
4569 int one; /* if TRUE, only replace one file name, include | |
4570 spaces and commas in the file name. */ | |
4571 { | |
4572 size_t dirlen = 0, envlen = 0; | |
4573 size_t len; | |
3598 | 4574 char_u *homedir_env, *homedir_env_orig; |
7 | 4575 char_u *p; |
4576 | |
4577 if (src == NULL) | |
4578 { | |
4579 *dst = NUL; | |
4580 return; | |
4581 } | |
4582 | |
4583 /* | |
4584 * If the file is a help file, remove the path completely. | |
4585 */ | |
4586 if (buf != NULL && buf->b_help) | |
4587 { | |
4588 STRCPY(dst, gettail(src)); | |
4589 return; | |
4590 } | |
4591 | |
4592 /* | |
4593 * We check both the value of the $HOME environment variable and the | |
4594 * "real" home directory. | |
4595 */ | |
4596 if (homedir != NULL) | |
4597 dirlen = STRLEN(homedir); | |
4598 | |
4599 #ifdef VMS | |
3598 | 4600 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); |
7 | 4601 #else |
3598 | 4602 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); |
4603 #endif | |
3658 | 4604 /* Empty is the same as not set. */ |
4605 if (homedir_env != NULL && *homedir_env == NUL) | |
4606 homedir_env = NULL; | |
4607 | |
3598 | 4608 #if defined(FEAT_MODIFY_FNAME) || defined(WIN3264) |
3658 | 4609 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL) |
3598 | 4610 { |
4611 int usedlen = 0; | |
4612 int flen; | |
4613 char_u *fbuf = NULL; | |
4614 | |
4615 flen = (int)STRLEN(homedir_env); | |
3608 | 4616 (void)modify_fname((char_u *)":p", &usedlen, |
4617 &homedir_env, &fbuf, &flen); | |
3598 | 4618 flen = (int)STRLEN(homedir_env); |
4619 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) | |
4620 /* Remove the trailing / that is added to a directory. */ | |
4621 homedir_env[flen - 1] = NUL; | |
4622 } | |
7 | 4623 #endif |
4624 | |
4625 if (homedir_env != NULL) | |
4626 envlen = STRLEN(homedir_env); | |
4627 | |
4628 if (!one) | |
4629 src = skipwhite(src); | |
4630 while (*src && dstlen > 0) | |
4631 { | |
4632 /* | |
4633 * Here we are at the beginning of a file name. | |
4634 * First, check to see if the beginning of the file name matches | |
4635 * $HOME or the "real" home directory. Check that there is a '/' | |
4636 * after the match (so that if e.g. the file is "/home/pieter/bla", | |
4637 * and the home directory is "/home/piet", the file does not end up | |
4638 * as "~er/bla" (which would seem to indicate the file "bla" in user | |
4639 * er's home directory)). | |
4640 */ | |
4641 p = homedir; | |
4642 len = dirlen; | |
4643 for (;;) | |
4644 { | |
4645 if ( len | |
4646 && fnamencmp(src, p, len) == 0 | |
4647 && (vim_ispathsep(src[len]) | |
4648 || (!one && (src[len] == ',' || src[len] == ' ')) | |
4649 || src[len] == NUL)) | |
4650 { | |
4651 src += len; | |
4652 if (--dstlen > 0) | |
4653 *dst++ = '~'; | |
4654 | |
4655 /* | |
4656 * If it's just the home directory, add "/". | |
4657 */ | |
4658 if (!vim_ispathsep(src[0]) && --dstlen > 0) | |
4659 *dst++ = '/'; | |
4660 break; | |
4661 } | |
4662 if (p == homedir_env) | |
4663 break; | |
4664 p = homedir_env; | |
4665 len = envlen; | |
4666 } | |
4667 | |
4668 /* if (!one) skip to separator: space or comma */ | |
4669 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) | |
4670 *dst++ = *src++; | |
4671 /* skip separator */ | |
4672 while ((*src == ' ' || *src == ',') && --dstlen > 0) | |
4673 *dst++ = *src++; | |
4674 } | |
4675 /* if (dstlen == 0) out of space, what to do??? */ | |
4676 | |
4677 *dst = NUL; | |
3598 | 4678 |
4679 if (homedir_env != homedir_env_orig) | |
4680 vim_free(homedir_env); | |
7 | 4681 } |
4682 | |
4683 /* | |
4684 * Like home_replace, store the replaced string in allocated memory. | |
4685 * When something fails, NULL is returned. | |
4686 */ | |
4687 char_u * | |
4688 home_replace_save(buf, src) | |
4689 buf_T *buf; /* when not NULL, check for help files */ | |
4690 char_u *src; /* input file name */ | |
4691 { | |
4692 char_u *dst; | |
4693 unsigned len; | |
4694 | |
4695 len = 3; /* space for "~/" and trailing NUL */ | |
4696 if (src != NULL) /* just in case */ | |
4697 len += (unsigned)STRLEN(src); | |
4698 dst = alloc(len); | |
4699 if (dst != NULL) | |
4700 home_replace(buf, src, dst, len, TRUE); | |
4701 return dst; | |
4702 } | |
4703 | |
4704 /* | |
4705 * Compare two file names and return: | |
4706 * FPC_SAME if they both exist and are the same file. | |
4707 * FPC_SAMEX if they both don't exist and have the same file name. | |
4708 * FPC_DIFF if they both exist and are different files. | |
4709 * FPC_NOTX if they both don't exist. | |
4710 * FPC_DIFFX if one of them doesn't exist. | |
4711 * For the first name environment variables are expanded | |
4712 */ | |
4713 int | |
4714 fullpathcmp(s1, s2, checkname) | |
4715 char_u *s1, *s2; | |
4716 int checkname; /* when both don't exist, check file names */ | |
4717 { | |
4718 #ifdef UNIX | |
4719 char_u exp1[MAXPATHL]; | |
4720 char_u full1[MAXPATHL]; | |
4721 char_u full2[MAXPATHL]; | |
4722 struct stat st1, st2; | |
4723 int r1, r2; | |
4724 | |
4725 expand_env(s1, exp1, MAXPATHL); | |
4726 r1 = mch_stat((char *)exp1, &st1); | |
4727 r2 = mch_stat((char *)s2, &st2); | |
4728 if (r1 != 0 && r2 != 0) | |
4729 { | |
4730 /* if mch_stat() doesn't work, may compare the names */ | |
4731 if (checkname) | |
4732 { | |
4733 if (fnamecmp(exp1, s2) == 0) | |
4734 return FPC_SAMEX; | |
4735 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
4736 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
4737 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) | |
4738 return FPC_SAMEX; | |
4739 } | |
4740 return FPC_NOTX; | |
4741 } | |
4742 if (r1 != 0 || r2 != 0) | |
4743 return FPC_DIFFX; | |
4744 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) | |
4745 return FPC_SAME; | |
4746 return FPC_DIFF; | |
4747 #else | |
4748 char_u *exp1; /* expanded s1 */ | |
4749 char_u *full1; /* full path of s1 */ | |
4750 char_u *full2; /* full path of s2 */ | |
4751 int retval = FPC_DIFF; | |
4752 int r1, r2; | |
4753 | |
4754 /* allocate one buffer to store three paths (alloc()/free() is slow!) */ | |
4755 if ((exp1 = alloc(MAXPATHL * 3)) != NULL) | |
4756 { | |
4757 full1 = exp1 + MAXPATHL; | |
4758 full2 = full1 + MAXPATHL; | |
4759 | |
4760 expand_env(s1, exp1, MAXPATHL); | |
4761 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
4762 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
4763 | |
4764 /* If vim_FullName() fails, the file probably doesn't exist. */ | |
4765 if (r1 != OK && r2 != OK) | |
4766 { | |
4767 if (checkname && fnamecmp(exp1, s2) == 0) | |
4768 retval = FPC_SAMEX; | |
4769 else | |
4770 retval = FPC_NOTX; | |
4771 } | |
4772 else if (r1 != OK || r2 != OK) | |
4773 retval = FPC_DIFFX; | |
4774 else if (fnamecmp(full1, full2)) | |
4775 retval = FPC_DIFF; | |
4776 else | |
4777 retval = FPC_SAME; | |
4778 vim_free(exp1); | |
4779 } | |
4780 return retval; | |
4781 #endif | |
4782 } | |
4783 | |
4784 /* | |
10 | 4785 * Get the tail of a path: the file name. |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4786 * When the path ends in a path separator the tail is the NUL after it. |
10 | 4787 * Fail safe: never returns NULL. |
7 | 4788 */ |
4789 char_u * | |
4790 gettail(fname) | |
4791 char_u *fname; | |
4792 { | |
4793 char_u *p1, *p2; | |
4794 | |
4795 if (fname == NULL) | |
4796 return (char_u *)""; | |
4797 for (p1 = p2 = fname; *p2; ) /* find last part of path */ | |
4798 { | |
4799 if (vim_ispathsep(*p2)) | |
4800 p1 = p2 + 1; | |
39 | 4801 mb_ptr_adv(p2); |
7 | 4802 } |
4803 return p1; | |
4804 } | |
4805 | |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4806 #if defined(FEAT_SEARCHPATH) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4807 static char_u *gettail_dir __ARGS((char_u *fname)); |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4808 |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4809 /* |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4810 * Return the end of the directory name, on the first path |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4811 * separator: |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4812 * "/path/file", "/path/dir/", "/path//dir", "/file" |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4813 * ^ ^ ^ ^ |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4814 */ |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4815 static char_u * |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4816 gettail_dir(fname) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4817 char_u *fname; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4818 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4819 char_u *dir_end = fname; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4820 char_u *next_dir_end = fname; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4821 int look_for_sep = TRUE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4822 char_u *p; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4823 |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4824 for (p = fname; *p != NUL; ) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4825 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4826 if (vim_ispathsep(*p)) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4827 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4828 if (look_for_sep) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4829 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4830 next_dir_end = p; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4831 look_for_sep = FALSE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4832 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4833 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4834 else |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4835 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4836 if (!look_for_sep) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4837 dir_end = next_dir_end; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4838 look_for_sep = TRUE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4839 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4840 mb_ptr_adv(p); |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4841 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4842 return dir_end; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4843 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4844 #endif |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4845 |
7 | 4846 /* |
39 | 4847 * Get pointer to tail of "fname", including path separators. Putting a NUL |
4848 * here leaves the directory name. Takes care of "c:/" and "//". | |
4849 * Always returns a valid pointer. | |
4850 */ | |
4851 char_u * | |
4852 gettail_sep(fname) | |
4853 char_u *fname; | |
4854 { | |
4855 char_u *p; | |
4856 char_u *t; | |
4857 | |
4858 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ | |
4859 t = gettail(fname); | |
4860 while (t > p && after_pathsep(fname, t)) | |
4861 --t; | |
4862 #ifdef VMS | |
4863 /* path separator is part of the path */ | |
4864 ++t; | |
4865 #endif | |
4866 return t; | |
4867 } | |
4868 | |
4869 /* | |
7 | 4870 * get the next path component (just after the next path separator). |
4871 */ | |
4872 char_u * | |
4873 getnextcomp(fname) | |
4874 char_u *fname; | |
4875 { | |
4876 while (*fname && !vim_ispathsep(*fname)) | |
39 | 4877 mb_ptr_adv(fname); |
7 | 4878 if (*fname) |
4879 ++fname; | |
4880 return fname; | |
4881 } | |
4882 | |
4883 /* | |
4884 * Get a pointer to one character past the head of a path name. | |
4885 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. | |
4886 * If there is no head, path is returned. | |
4887 */ | |
4888 char_u * | |
4889 get_past_head(path) | |
4890 char_u *path; | |
4891 { | |
4892 char_u *retval; | |
4893 | |
4894 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
4895 /* may skip "c:" */ | |
4896 if (isalpha(path[0]) && path[1] == ':') | |
4897 retval = path + 2; | |
4898 else | |
4899 retval = path; | |
4900 #else | |
4901 # if defined(AMIGA) | |
4902 /* may skip "label:" */ | |
4903 retval = vim_strchr(path, ':'); | |
4904 if (retval == NULL) | |
4905 retval = path; | |
4906 # else /* Unix */ | |
4907 retval = path; | |
4908 # endif | |
4909 #endif | |
4910 | |
4911 while (vim_ispathsep(*retval)) | |
4912 ++retval; | |
4913 | |
4914 return retval; | |
4915 } | |
4916 | |
4917 /* | |
4918 * return TRUE if 'c' is a path separator. | |
4919 */ | |
4920 int | |
4921 vim_ispathsep(c) | |
4922 int c; | |
4923 { | |
2823 | 4924 #ifdef UNIX |
7 | 4925 return (c == '/'); /* UNIX has ':' inside file names */ |
2823 | 4926 #else |
4927 # ifdef BACKSLASH_IN_FILENAME | |
7 | 4928 return (c == ':' || c == '/' || c == '\\'); |
2823 | 4929 # else |
4930 # ifdef VMS | |
7 | 4931 /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
4932 return (c == ':' || c == '[' || c == ']' || c == '/' | |
4933 || c == '<' || c == '>' || c == '"' ); | |
2823 | 4934 # else |
7 | 4935 return (c == ':' || c == '/'); |
2823 | 4936 # endif /* VMS */ |
7 | 4937 # endif |
2823 | 4938 #endif |
7 | 4939 } |
4940 | |
4941 #if defined(FEAT_SEARCHPATH) || defined(PROTO) | |
4942 /* | |
4943 * return TRUE if 'c' is a path list separator. | |
4944 */ | |
4945 int | |
4946 vim_ispathlistsep(c) | |
4947 int c; | |
4948 { | |
4949 #ifdef UNIX | |
4950 return (c == ':'); | |
4951 #else | |
1224 | 4952 return (c == ';'); /* might not be right for every system... */ |
7 | 4953 #endif |
4954 } | |
4955 #endif | |
4956 | |
819 | 4957 #if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \ |
4958 || defined(FEAT_EVAL) || defined(PROTO) | |
4959 /* | |
4960 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" | |
4961 * It's done in-place. | |
4962 */ | |
4963 void | |
4964 shorten_dir(str) | |
4965 char_u *str; | |
4966 { | |
4967 char_u *tail, *s, *d; | |
4968 int skip = FALSE; | |
4969 | |
4970 tail = gettail(str); | |
4971 d = str; | |
4972 for (s = str; ; ++s) | |
4973 { | |
4974 if (s >= tail) /* copy the whole tail */ | |
4975 { | |
4976 *d++ = *s; | |
4977 if (*s == NUL) | |
4978 break; | |
4979 } | |
4980 else if (vim_ispathsep(*s)) /* copy '/' and next char */ | |
4981 { | |
4982 *d++ = *s; | |
4983 skip = FALSE; | |
4984 } | |
4985 else if (!skip) | |
4986 { | |
4987 *d++ = *s; /* copy next char */ | |
4988 if (*s != '~' && *s != '.') /* and leading "~" and "." */ | |
4989 skip = TRUE; | |
4990 # ifdef FEAT_MBYTE | |
4991 if (has_mbyte) | |
4992 { | |
4993 int l = mb_ptr2len(s); | |
4994 | |
4995 while (--l > 0) | |
927 | 4996 *d++ = *++s; |
819 | 4997 } |
4998 # endif | |
4999 } | |
5000 } | |
5001 } | |
5002 #endif | |
5003 | |
594 | 5004 /* |
5005 * Return TRUE if the directory of "fname" exists, FALSE otherwise. | |
5006 * Also returns TRUE if there is no directory name. | |
5007 * "fname" must be writable!. | |
5008 */ | |
5009 int | |
5010 dir_of_file_exists(fname) | |
5011 char_u *fname; | |
5012 { | |
5013 char_u *p; | |
5014 int c; | |
5015 int retval; | |
5016 | |
5017 p = gettail_sep(fname); | |
5018 if (p == fname) | |
5019 return TRUE; | |
5020 c = *p; | |
5021 *p = NUL; | |
5022 retval = mch_isdir(fname); | |
5023 *p = c; | |
5024 return retval; | |
5025 } | |
5026 | |
7 | 5027 #if (defined(CASE_INSENSITIVE_FILENAME) && defined(BACKSLASH_IN_FILENAME)) \ |
5028 || defined(PROTO) | |
5029 /* | |
5030 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally. | |
5031 */ | |
5032 int | |
5033 vim_fnamecmp(x, y) | |
5034 char_u *x, *y; | |
5035 { | |
5036 return vim_fnamencmp(x, y, MAXPATHL); | |
5037 } | |
5038 | |
5039 int | |
5040 vim_fnamencmp(x, y, len) | |
5041 char_u *x, *y; | |
5042 size_t len; | |
5043 { | |
5044 while (len > 0 && *x && *y) | |
5045 { | |
5046 if (TOLOWER_LOC(*x) != TOLOWER_LOC(*y) | |
5047 && !(*x == '/' && *y == '\\') | |
5048 && !(*x == '\\' && *y == '/')) | |
5049 break; | |
5050 ++x; | |
5051 ++y; | |
5052 --len; | |
5053 } | |
5054 if (len == 0) | |
5055 return 0; | |
5056 return (*x - *y); | |
5057 } | |
5058 #endif | |
5059 | |
5060 /* | |
5061 * Concatenate file names fname1 and fname2 into allocated memory. | |
1224 | 5062 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
7 | 5063 */ |
5064 char_u * | |
5065 concat_fnames(fname1, fname2, sep) | |
5066 char_u *fname1; | |
5067 char_u *fname2; | |
5068 int sep; | |
5069 { | |
5070 char_u *dest; | |
5071 | |
5072 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); | |
5073 if (dest != NULL) | |
5074 { | |
5075 STRCPY(dest, fname1); | |
5076 if (sep) | |
5077 add_pathsep(dest); | |
5078 STRCAT(dest, fname2); | |
5079 } | |
5080 return dest; | |
5081 } | |
5082 | |
115 | 5083 /* |
5084 * Concatenate two strings and return the result in allocated memory. | |
5085 * Returns NULL when out of memory. | |
5086 */ | |
5087 char_u * | |
5088 concat_str(str1, str2) | |
5089 char_u *str1; | |
5090 char_u *str2; | |
5091 { | |
5092 char_u *dest; | |
5093 size_t l = STRLEN(str1); | |
5094 | |
5095 dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); | |
5096 if (dest != NULL) | |
5097 { | |
5098 STRCPY(dest, str1); | |
5099 STRCPY(dest + l, str2); | |
5100 } | |
5101 return dest; | |
5102 } | |
5103 | |
7 | 5104 /* |
5105 * Add a path separator to a file name, unless it already ends in a path | |
5106 * separator. | |
5107 */ | |
5108 void | |
5109 add_pathsep(p) | |
5110 char_u *p; | |
5111 { | |
39 | 5112 if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
7 | 5113 STRCAT(p, PATHSEPSTR); |
5114 } | |
5115 | |
5116 /* | |
5117 * FullName_save - Make an allocated copy of a full file name. | |
5118 * Returns NULL when out of memory. | |
5119 */ | |
5120 char_u * | |
5121 FullName_save(fname, force) | |
5122 char_u *fname; | |
3584 | 5123 int force; /* force expansion, even when it already looks |
5124 * like a full path name */ | |
7 | 5125 { |
5126 char_u *buf; | |
5127 char_u *new_fname = NULL; | |
5128 | |
5129 if (fname == NULL) | |
5130 return NULL; | |
5131 | |
5132 buf = alloc((unsigned)MAXPATHL); | |
5133 if (buf != NULL) | |
5134 { | |
5135 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) | |
5136 new_fname = vim_strsave(buf); | |
5137 else | |
5138 new_fname = vim_strsave(fname); | |
5139 vim_free(buf); | |
5140 } | |
5141 return new_fname; | |
5142 } | |
5143 | |
5144 #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) | |
5145 | |
5146 static char_u *skip_string __ARGS((char_u *p)); | |
5147 | |
5148 /* | |
5149 * Find the start of a comment, not knowing if we are in a comment right now. | |
5150 * Search starts at w_cursor.lnum and goes backwards. | |
5151 */ | |
5152 pos_T * | |
5153 find_start_comment(ind_maxcomment) /* XXX */ | |
5154 int ind_maxcomment; | |
5155 { | |
5156 pos_T *pos; | |
5157 char_u *line; | |
5158 char_u *p; | |
829 | 5159 int cur_maxcomment = ind_maxcomment; |
5160 | |
5161 for (;;) | |
5162 { | |
5163 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); | |
5164 if (pos == NULL) | |
5165 break; | |
5166 | |
5167 /* | |
5168 * Check if the comment start we found is inside a string. | |
5169 * If it is then restrict the search to below this line and try again. | |
5170 */ | |
5171 line = ml_get(pos->lnum); | |
1883 | 5172 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
829 | 5173 p = skip_string(p); |
1883 | 5174 if ((colnr_T)(p - line) <= pos->col) |
829 | 5175 break; |
5176 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; | |
5177 if (cur_maxcomment <= 0) | |
5178 { | |
5179 pos = NULL; | |
5180 break; | |
5181 } | |
5182 } | |
7 | 5183 return pos; |
5184 } | |
5185 | |
5186 /* | |
5187 * Skip to the end of a "string" and a 'c' character. | |
5188 * If there is no string or character, return argument unmodified. | |
5189 */ | |
5190 static char_u * | |
5191 skip_string(p) | |
5192 char_u *p; | |
5193 { | |
5194 int i; | |
5195 | |
5196 /* | |
5197 * We loop, because strings may be concatenated: "date""time". | |
5198 */ | |
5199 for ( ; ; ++p) | |
5200 { | |
5201 if (p[0] == '\'') /* 'c' or '\n' or '\000' */ | |
5202 { | |
5203 if (!p[1]) /* ' at end of line */ | |
5204 break; | |
5205 i = 2; | |
5206 if (p[1] == '\\') /* '\n' or '\000' */ | |
5207 { | |
5208 ++i; | |
5209 while (vim_isdigit(p[i - 1])) /* '\000' */ | |
5210 ++i; | |
5211 } | |
5212 if (p[i] == '\'') /* check for trailing ' */ | |
5213 { | |
5214 p += i; | |
5215 continue; | |
5216 } | |
5217 } | |
5218 else if (p[0] == '"') /* start of string */ | |
5219 { | |
5220 for (++p; p[0]; ++p) | |
5221 { | |
5222 if (p[0] == '\\' && p[1] != NUL) | |
5223 ++p; | |
5224 else if (p[0] == '"') /* end of string */ | |
5225 break; | |
5226 } | |
5227 if (p[0] == '"') | |
5228 continue; | |
5229 } | |
5230 break; /* no string found */ | |
5231 } | |
5232 if (!*p) | |
5233 --p; /* backup from NUL */ | |
5234 return p; | |
5235 } | |
5236 #endif /* FEAT_CINDENT || FEAT_SYN_HL */ | |
5237 | |
5238 #if defined(FEAT_CINDENT) || defined(PROTO) | |
5239 | |
5240 /* | |
5241 * Do C or expression indenting on the current line. | |
5242 */ | |
5243 void | |
5244 do_c_expr_indent() | |
5245 { | |
5246 # ifdef FEAT_EVAL | |
5247 if (*curbuf->b_p_inde != NUL) | |
5248 fixthisline(get_expr_indent); | |
5249 else | |
5250 # endif | |
5251 fixthisline(get_c_indent); | |
5252 } | |
5253 | |
5254 /* | |
5255 * Functions for C-indenting. | |
5256 * Most of this originally comes from Eric Fischer. | |
5257 */ | |
5258 /* | |
5259 * Below "XXX" means that this function may unlock the current line. | |
5260 */ | |
5261 | |
5262 static char_u *cin_skipcomment __ARGS((char_u *)); | |
5263 static int cin_nocode __ARGS((char_u *)); | |
5264 static pos_T *find_line_comment __ARGS((void)); | |
5265 static int cin_islabel_skip __ARGS((char_u **)); | |
5266 static int cin_isdefault __ARGS((char_u *)); | |
5267 static char_u *after_label __ARGS((char_u *l)); | |
5268 static int get_indent_nolabel __ARGS((linenr_T lnum)); | |
5269 static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment)); | |
5270 static int cin_first_id_amount __ARGS((void)); | |
5271 static int cin_get_equal_amount __ARGS((linenr_T lnum)); | |
5272 static int cin_ispreproc __ARGS((char_u *)); | |
5273 static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump)); | |
5274 static int cin_iscomment __ARGS((char_u *)); | |
5275 static int cin_islinecomment __ARGS((char_u *)); | |
5276 static int cin_isterminated __ARGS((char_u *, int, int)); | |
5277 static int cin_isinit __ARGS((void)); | |
3220 | 5278 static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T, int, int)); |
7 | 5279 static int cin_isif __ARGS((char_u *)); |
5280 static int cin_iselse __ARGS((char_u *)); | |
5281 static int cin_isdo __ARGS((char_u *)); | |
5282 static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int)); | |
3461 | 5283 static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset)); |
829 | 5284 static int cin_iswhileofdo_end __ARGS((int terminated, int ind_maxparen, int ind_maxcomment)); |
7 | 5285 static int cin_isbreak __ARGS((char_u *)); |
1336 | 5286 static int cin_is_cpp_baseclass __ARGS((colnr_T *col)); |
828 | 5287 static int get_baseclass_amount __ARGS((int col, int ind_maxparen, int ind_maxcomment, int ind_cpp_baseclass)); |
7 | 5288 static int cin_ends_in __ARGS((char_u *, char_u *, char_u *)); |
4184 | 5289 static int cin_starts_with __ARGS((char_u *s, char *word)); |
7 | 5290 static int cin_skip2pos __ARGS((pos_T *trypos)); |
5291 static pos_T *find_start_brace __ARGS((int)); | |
5292 static pos_T *find_match_paren __ARGS((int, int)); | |
5293 static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos)); | |
5294 static int find_last_paren __ARGS((char_u *l, int start, int end)); | |
5295 static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment)); | |
2857 | 5296 static int cin_is_cpp_namespace __ARGS((char_u *)); |
7 | 5297 |
1096 | 5298 static int ind_hash_comment = 0; /* # starts a comment */ |
5299 | |
7 | 5300 /* |
5301 * Skip over white space and C comments within the line. | |
1096 | 5302 * Also skip over Perl/shell comments if desired. |
7 | 5303 */ |
5304 static char_u * | |
5305 cin_skipcomment(s) | |
5306 char_u *s; | |
5307 { | |
5308 while (*s) | |
5309 { | |
1096 | 5310 char_u *prev_s = s; |
5311 | |
7 | 5312 s = skipwhite(s); |
1096 | 5313 |
5314 /* Perl/shell # comment comment continues until eol. Require a space | |
5315 * before # to avoid recognizing $#array. */ | |
5316 if (ind_hash_comment != 0 && s != prev_s && *s == '#') | |
5317 { | |
5318 s += STRLEN(s); | |
5319 break; | |
5320 } | |
7 | 5321 if (*s != '/') |
5322 break; | |
5323 ++s; | |
5324 if (*s == '/') /* slash-slash comment continues till eol */ | |
5325 { | |
5326 s += STRLEN(s); | |
5327 break; | |
5328 } | |
5329 if (*s != '*') | |
5330 break; | |
5331 for (++s; *s; ++s) /* skip slash-star comment */ | |
5332 if (s[0] == '*' && s[1] == '/') | |
5333 { | |
5334 s += 2; | |
5335 break; | |
5336 } | |
5337 } | |
5338 return s; | |
5339 } | |
5340 | |
5341 /* | |
2805 | 5342 * Return TRUE if there is no code at *s. White space and comments are |
7 | 5343 * not considered code. |
5344 */ | |
5345 static int | |
5346 cin_nocode(s) | |
5347 char_u *s; | |
5348 { | |
5349 return *cin_skipcomment(s) == NUL; | |
5350 } | |
5351 | |
5352 /* | |
5353 * Check previous lines for a "//" line comment, skipping over blank lines. | |
5354 */ | |
5355 static pos_T * | |
5356 find_line_comment() /* XXX */ | |
5357 { | |
5358 static pos_T pos; | |
5359 char_u *line; | |
5360 char_u *p; | |
5361 | |
5362 pos = curwin->w_cursor; | |
5363 while (--pos.lnum > 0) | |
5364 { | |
5365 line = ml_get(pos.lnum); | |
5366 p = skipwhite(line); | |
5367 if (cin_islinecomment(p)) | |
5368 { | |
5369 pos.col = (int)(p - line); | |
5370 return &pos; | |
5371 } | |
5372 if (*p != NUL) | |
5373 break; | |
5374 } | |
5375 return NULL; | |
5376 } | |
5377 | |
5378 /* | |
5379 * Check if string matches "label:"; move to character after ':' if true. | |
5380 */ | |
5381 static int | |
5382 cin_islabel_skip(s) | |
5383 char_u **s; | |
5384 { | |
5385 if (!vim_isIDc(**s)) /* need at least one ID character */ | |
5386 return FALSE; | |
5387 | |
5388 while (vim_isIDc(**s)) | |
5389 (*s)++; | |
5390 | |
5391 *s = cin_skipcomment(*s); | |
5392 | |
5393 /* "::" is not a label, it's C++ */ | |
5394 return (**s == ':' && *++*s != ':'); | |
5395 } | |
5396 | |
5397 /* | |
5398 * Recognize a label: "label:". | |
5399 * Note: curwin->w_cursor must be where we are looking for the label. | |
5400 */ | |
5401 int | |
5402 cin_islabel(ind_maxcomment) /* XXX */ | |
5403 int ind_maxcomment; | |
5404 { | |
5405 char_u *s; | |
5406 | |
5407 s = cin_skipcomment(ml_get_curline()); | |
5408 | |
5409 /* | |
5410 * Exclude "default" from labels, since it should be indented | |
5411 * like a switch label. Same for C++ scope declarations. | |
5412 */ | |
5413 if (cin_isdefault(s)) | |
5414 return FALSE; | |
5415 if (cin_isscopedecl(s)) | |
5416 return FALSE; | |
5417 | |
5418 if (cin_islabel_skip(&s)) | |
5419 { | |
5420 /* | |
5421 * Only accept a label if the previous line is terminated or is a case | |
5422 * label. | |
5423 */ | |
5424 pos_T cursor_save; | |
5425 pos_T *trypos; | |
5426 char_u *line; | |
5427 | |
5428 cursor_save = curwin->w_cursor; | |
5429 while (curwin->w_cursor.lnum > 1) | |
5430 { | |
5431 --curwin->w_cursor.lnum; | |
5432 | |
5433 /* | |
5434 * If we're in a comment now, skip to the start of the comment. | |
5435 */ | |
5436 curwin->w_cursor.col = 0; | |
5437 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */ | |
5438 curwin->w_cursor = *trypos; | |
5439 | |
5440 line = ml_get_curline(); | |
5441 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ | |
5442 continue; | |
5443 if (*(line = cin_skipcomment(line)) == NUL) | |
5444 continue; | |
5445 | |
5446 curwin->w_cursor = cursor_save; | |
5447 if (cin_isterminated(line, TRUE, FALSE) | |
5448 || cin_isscopedecl(line) | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5449 || cin_iscase(line, TRUE) |
7 | 5450 || (cin_islabel_skip(&line) && cin_nocode(line))) |
5451 return TRUE; | |
5452 return FALSE; | |
5453 } | |
5454 curwin->w_cursor = cursor_save; | |
5455 return TRUE; /* label at start of file??? */ | |
5456 } | |
5457 return FALSE; | |
5458 } | |
5459 | |
5460 /* | |
4184 | 5461 * Recognize structure initialization and enumerations: |
5462 * "[typedef] [static|public|protected|private] enum" | |
5463 * "[typedef] [static|public|protected|private] = {" | |
7 | 5464 */ |
5465 static int | |
5466 cin_isinit(void) | |
5467 { | |
5468 char_u *s; | |
4184 | 5469 static char *skip[] = {"static", "public", "protected", "private"}; |
7 | 5470 |
5471 s = cin_skipcomment(ml_get_curline()); | |
5472 | |
4184 | 5473 if (cin_starts_with(s, "typedef")) |
7 | 5474 s = cin_skipcomment(s + 7); |
5475 | |
4184 | 5476 for (;;) |
5477 { | |
5478 int i, l; | |
5479 | |
5480 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) | |
5481 { | |
4207 | 5482 l = (int)strlen(skip[i]); |
4184 | 5483 if (cin_starts_with(s, skip[i])) |
5484 { | |
5485 s = cin_skipcomment(s + l); | |
5486 l = 0; | |
5487 break; | |
5488 } | |
5489 } | |
5490 if (l != 0) | |
5491 break; | |
5492 } | |
5493 | |
5494 if (cin_starts_with(s, "enum")) | |
7 | 5495 return TRUE; |
5496 | |
5497 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) | |
5498 return TRUE; | |
5499 | |
5500 return FALSE; | |
5501 } | |
5502 | |
5503 /* | |
5504 * Recognize a switch label: "case .*:" or "default:". | |
5505 */ | |
5506 int | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5507 cin_iscase(s, strict) |
7 | 5508 char_u *s; |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5509 int strict; /* Allow relaxed check of case statement for JS */ |
7 | 5510 { |
5511 s = cin_skipcomment(s); | |
4184 | 5512 if (cin_starts_with(s, "case")) |
7 | 5513 { |
5514 for (s += 4; *s; ++s) | |
5515 { | |
5516 s = cin_skipcomment(s); | |
5517 if (*s == ':') | |
5518 { | |
5519 if (s[1] == ':') /* skip over "::" for C++ */ | |
5520 ++s; | |
5521 else | |
5522 return TRUE; | |
5523 } | |
5524 if (*s == '\'' && s[1] && s[2] == '\'') | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5525 s += 2; /* skip over ':' */ |
7 | 5526 else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
5527 return FALSE; /* stop at comment */ | |
5528 else if (*s == '"') | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5529 { |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5530 /* JS etc. */ |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5531 if (strict) |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5532 return FALSE; /* stop at string */ |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5533 else |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5534 return TRUE; |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5535 } |
7 | 5536 } |
5537 return FALSE; | |
5538 } | |
5539 | |
5540 if (cin_isdefault(s)) | |
5541 return TRUE; | |
5542 return FALSE; | |
5543 } | |
5544 | |
5545 /* | |
5546 * Recognize a "default" switch label. | |
5547 */ | |
5548 static int | |
5549 cin_isdefault(s) | |
5550 char_u *s; | |
5551 { | |
5552 return (STRNCMP(s, "default", 7) == 0 | |
5553 && *(s = cin_skipcomment(s + 7)) == ':' | |
5554 && s[1] != ':'); | |
5555 } | |
5556 | |
5557 /* | |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
5558 * Recognize a "public/private/protected" scope declaration label. |
7 | 5559 */ |
5560 int | |
5561 cin_isscopedecl(s) | |
5562 char_u *s; | |
5563 { | |
5564 int i; | |
5565 | |
5566 s = cin_skipcomment(s); | |
5567 if (STRNCMP(s, "public", 6) == 0) | |
5568 i = 6; | |
5569 else if (STRNCMP(s, "protected", 9) == 0) | |
5570 i = 9; | |
5571 else if (STRNCMP(s, "private", 7) == 0) | |
5572 i = 7; | |
5573 else | |
5574 return FALSE; | |
5575 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); | |
5576 } | |
5577 | |
2857 | 5578 /* Maximum number of lines to search back for a "namespace" line. */ |
5579 #define FIND_NAMESPACE_LIM 20 | |
5580 | |
5581 /* | |
5582 * Recognize a "namespace" scope declaration. | |
5583 */ | |
5584 static int | |
5585 cin_is_cpp_namespace(s) | |
5586 char_u *s; | |
5587 { | |
5588 char_u *p; | |
5589 int has_name = FALSE; | |
5590 | |
5591 s = cin_skipcomment(s); | |
5592 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) | |
5593 { | |
5594 p = cin_skipcomment(skipwhite(s + 9)); | |
5595 while (*p != NUL) | |
5596 { | |
5597 if (vim_iswhite(*p)) | |
5598 { | |
5599 has_name = TRUE; /* found end of a name */ | |
5600 p = cin_skipcomment(skipwhite(p)); | |
5601 } | |
5602 else if (*p == '{') | |
5603 { | |
5604 break; | |
5605 } | |
5606 else if (vim_iswordc(*p)) | |
5607 { | |
5608 if (has_name) | |
5609 return FALSE; /* word character after skipping past name */ | |
5610 ++p; | |
5611 } | |
5612 else | |
5613 { | |
5614 return FALSE; | |
5615 } | |
5616 } | |
5617 return TRUE; | |
5618 } | |
5619 return FALSE; | |
5620 } | |
5621 | |
7 | 5622 /* |
5623 * Return a pointer to the first non-empty non-comment character after a ':'. | |
5624 * Return NULL if not found. | |
5625 * case 234: a = b; | |
5626 * ^ | |
5627 */ | |
5628 static char_u * | |
5629 after_label(l) | |
5630 char_u *l; | |
5631 { | |
5632 for ( ; *l; ++l) | |
5633 { | |
5634 if (*l == ':') | |
5635 { | |
5636 if (l[1] == ':') /* skip over "::" for C++ */ | |
5637 ++l; | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5638 else if (!cin_iscase(l + 1, FALSE)) |
7 | 5639 break; |
5640 } | |
5641 else if (*l == '\'' && l[1] && l[2] == '\'') | |
5642 l += 2; /* skip over 'x' */ | |
5643 } | |
5644 if (*l == NUL) | |
5645 return NULL; | |
5646 l = cin_skipcomment(l + 1); | |
5647 if (*l == NUL) | |
5648 return NULL; | |
5649 return l; | |
5650 } | |
5651 | |
5652 /* | |
5653 * Get indent of line "lnum", skipping a label. | |
5654 * Return 0 if there is nothing after the label. | |
5655 */ | |
5656 static int | |
5657 get_indent_nolabel(lnum) /* XXX */ | |
5658 linenr_T lnum; | |
5659 { | |
5660 char_u *l; | |
5661 pos_T fp; | |
5662 colnr_T col; | |
5663 char_u *p; | |
5664 | |
5665 l = ml_get(lnum); | |
5666 p = after_label(l); | |
5667 if (p == NULL) | |
5668 return 0; | |
5669 | |
5670 fp.col = (colnr_T)(p - l); | |
5671 fp.lnum = lnum; | |
5672 getvcol(curwin, &fp, &col, NULL, NULL); | |
5673 return (int)col; | |
5674 } | |
5675 | |
5676 /* | |
5677 * Find indent for line "lnum", ignoring any case or jump label. | |
829 | 5678 * Also return a pointer to the text (after the label) in "pp". |
7 | 5679 * label: if (asdf && asdfasdf) |
5680 * ^ | |
5681 */ | |
5682 static int | |
5683 skip_label(lnum, pp, ind_maxcomment) | |
5684 linenr_T lnum; | |
5685 char_u **pp; | |
5686 int ind_maxcomment; | |
5687 { | |
5688 char_u *l; | |
5689 int amount; | |
5690 pos_T cursor_save; | |
5691 | |
5692 cursor_save = curwin->w_cursor; | |
5693 curwin->w_cursor.lnum = lnum; | |
5694 l = ml_get_curline(); | |
5695 /* XXX */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5696 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5697 || cin_islabel(ind_maxcomment)) |
7 | 5698 { |
5699 amount = get_indent_nolabel(lnum); | |
5700 l = after_label(ml_get_curline()); | |
5701 if (l == NULL) /* just in case */ | |
5702 l = ml_get_curline(); | |
5703 } | |
5704 else | |
5705 { | |
5706 amount = get_indent(); | |
5707 l = ml_get_curline(); | |
5708 } | |
5709 *pp = l; | |
5710 | |
5711 curwin->w_cursor = cursor_save; | |
5712 return amount; | |
5713 } | |
5714 | |
5715 /* | |
5716 * Return the indent of the first variable name after a type in a declaration. | |
5717 * int a, indent of "a" | |
5718 * static struct foo b, indent of "b" | |
5719 * enum bla c, indent of "c" | |
5720 * Returns zero when it doesn't look like a declaration. | |
5721 */ | |
5722 static int | |
5723 cin_first_id_amount() | |
5724 { | |
5725 char_u *line, *p, *s; | |
5726 int len; | |
5727 pos_T fp; | |
5728 colnr_T col; | |
5729 | |
5730 line = ml_get_curline(); | |
5731 p = skipwhite(line); | |
835 | 5732 len = (int)(skiptowhite(p) - p); |
7 | 5733 if (len == 6 && STRNCMP(p, "static", 6) == 0) |
5734 { | |
5735 p = skipwhite(p + 6); | |
856 | 5736 len = (int)(skiptowhite(p) - p); |
7 | 5737 } |
5738 if (len == 6 && STRNCMP(p, "struct", 6) == 0) | |
5739 p = skipwhite(p + 6); | |
5740 else if (len == 4 && STRNCMP(p, "enum", 4) == 0) | |
5741 p = skipwhite(p + 4); | |
5742 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) | |
5743 || (len == 6 && STRNCMP(p, "signed", 6) == 0)) | |
5744 { | |
5745 s = skipwhite(p + len); | |
5746 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3])) | |
5747 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4])) | |
5748 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5])) | |
5749 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4]))) | |
5750 p = s; | |
5751 } | |
5752 for (len = 0; vim_isIDc(p[len]); ++len) | |
5753 ; | |
5754 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p)) | |
5755 return 0; | |
5756 | |
5757 p = skipwhite(p + len); | |
5758 fp.lnum = curwin->w_cursor.lnum; | |
5759 fp.col = (colnr_T)(p - line); | |
5760 getvcol(curwin, &fp, &col, NULL, NULL); | |
5761 return (int)col; | |
5762 } | |
5763 | |
5764 /* | |
5765 * Return the indent of the first non-blank after an equal sign. | |
5766 * char *foo = "here"; | |
5767 * Return zero if no (useful) equal sign found. | |
5768 * Return -1 if the line above "lnum" ends in a backslash. | |
5769 * foo = "asdf\ | |
5770 * asdf\ | |
5771 * here"; | |
5772 */ | |
5773 static int | |
5774 cin_get_equal_amount(lnum) | |
5775 linenr_T lnum; | |
5776 { | |
5777 char_u *line; | |
5778 char_u *s; | |
5779 colnr_T col; | |
5780 pos_T fp; | |
5781 | |
5782 if (lnum > 1) | |
5783 { | |
5784 line = ml_get(lnum - 1); | |
5785 if (*line != NUL && line[STRLEN(line) - 1] == '\\') | |
5786 return -1; | |
5787 } | |
5788 | |
5789 line = s = ml_get(lnum); | |
5790 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) | |
5791 { | |
5792 if (cin_iscomment(s)) /* ignore comments */ | |
5793 s = cin_skipcomment(s); | |
5794 else | |
5795 ++s; | |
5796 } | |
5797 if (*s != '=') | |
5798 return 0; | |
5799 | |
5800 s = skipwhite(s + 1); | |
5801 if (cin_nocode(s)) | |
5802 return 0; | |
5803 | |
5804 if (*s == '"') /* nice alignment for continued strings */ | |
5805 ++s; | |
5806 | |
5807 fp.lnum = lnum; | |
5808 fp.col = (colnr_T)(s - line); | |
5809 getvcol(curwin, &fp, &col, NULL, NULL); | |
5810 return (int)col; | |
5811 } | |
5812 | |
5813 /* | |
5814 * Recognize a preprocessor statement: Any line that starts with '#'. | |
5815 */ | |
5816 static int | |
5817 cin_ispreproc(s) | |
5818 char_u *s; | |
5819 { | |
2776 | 5820 if (*skipwhite(s) == '#') |
7 | 5821 return TRUE; |
5822 return FALSE; | |
5823 } | |
5824 | |
5825 /* | |
5826 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a | |
5827 * continuation line of a preprocessor statement. Decrease "*lnump" to the | |
5828 * start and return the line in "*pp". | |
5829 */ | |
5830 static int | |
5831 cin_ispreproc_cont(pp, lnump) | |
5832 char_u **pp; | |
5833 linenr_T *lnump; | |
5834 { | |
5835 char_u *line = *pp; | |
5836 linenr_T lnum = *lnump; | |
5837 int retval = FALSE; | |
5838 | |
408 | 5839 for (;;) |
7 | 5840 { |
5841 if (cin_ispreproc(line)) | |
5842 { | |
5843 retval = TRUE; | |
5844 *lnump = lnum; | |
5845 break; | |
5846 } | |
5847 if (lnum == 1) | |
5848 break; | |
5849 line = ml_get(--lnum); | |
5850 if (*line == NUL || line[STRLEN(line) - 1] != '\\') | |
5851 break; | |
5852 } | |
5853 | |
5854 if (lnum != *lnump) | |
5855 *pp = ml_get(*lnump); | |
5856 return retval; | |
5857 } | |
5858 | |
5859 /* | |
5860 * Recognize the start of a C or C++ comment. | |
5861 */ | |
5862 static int | |
5863 cin_iscomment(p) | |
5864 char_u *p; | |
5865 { | |
5866 return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); | |
5867 } | |
5868 | |
5869 /* | |
5870 * Recognize the start of a "//" comment. | |
5871 */ | |
5872 static int | |
5873 cin_islinecomment(p) | |
5874 char_u *p; | |
5875 { | |
5876 return (p[0] == '/' && p[1] == '/'); | |
5877 } | |
5878 | |
5879 /* | |
2805 | 5880 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or |
5881 * '}'. | |
7 | 5882 * Don't consider "} else" a terminated line. |
2843 | 5883 * If a line begins with an "else", only consider it terminated if no unmatched |
5884 * opening braces follow (handle "else { foo();" correctly). | |
7 | 5885 * Return the character terminating the line (ending char's have precedence if |
5886 * both apply in order to determine initializations). | |
5887 */ | |
5888 static int | |
5889 cin_isterminated(s, incl_open, incl_comma) | |
5890 char_u *s; | |
5891 int incl_open; /* include '{' at the end as terminator */ | |
5892 int incl_comma; /* recognize a trailing comma */ | |
5893 { | |
2843 | 5894 char_u found_start = 0; |
5895 unsigned n_open = 0; | |
5896 int is_else = FALSE; | |
7 | 5897 |
5898 s = cin_skipcomment(s); | |
5899 | |
5900 if (*s == '{' || (*s == '}' && !cin_iselse(s))) | |
5901 found_start = *s; | |
5902 | |
2843 | 5903 if (!found_start) |
5904 is_else = cin_iselse(s); | |
5905 | |
7 | 5906 while (*s) |
5907 { | |
5908 /* skip over comments, "" strings and 'c'haracters */ | |
5909 s = skip_string(cin_skipcomment(s)); | |
2805 | 5910 if (*s == '}' && n_open > 0) |
5911 --n_open; | |
2843 | 5912 if ((!is_else || n_open == 0) |
2805 | 5913 && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) |
7 | 5914 && cin_nocode(s + 1)) |
5915 return *s; | |
2805 | 5916 else if (*s == '{') |
5917 { | |
5918 if (incl_open && cin_nocode(s + 1)) | |
5919 return *s; | |
5920 else | |
5921 ++n_open; | |
5922 } | |
7 | 5923 |
5924 if (*s) | |
5925 s++; | |
5926 } | |
5927 return found_start; | |
5928 } | |
5929 | |
5930 /* | |
5931 * Recognize the basic picture of a function declaration -- it needs to | |
5932 * have an open paren somewhere and a close paren at the end of the line and | |
5933 * no semicolons anywhere. | |
5934 * When a line ends in a comma we continue looking in the next line. | |
5935 * "sp" points to a string with the line. When looking at other lines it must | |
5936 * be restored to the line. When it's NULL fetch lines here. | |
5937 * "lnum" is where we start looking. | |
3220 | 5938 * "min_lnum" is the line before which we will not be looking. |
7 | 5939 */ |
5940 static int | |
3220 | 5941 cin_isfuncdecl(sp, first_lnum, min_lnum, ind_maxparen, ind_maxcomment) |
7 | 5942 char_u **sp; |
5943 linenr_T first_lnum; | |
3220 | 5944 linenr_T min_lnum; |
5945 int ind_maxparen; | |
5946 int ind_maxcomment; | |
7 | 5947 { |
5948 char_u *s; | |
5949 linenr_T lnum = first_lnum; | |
5950 int retval = FALSE; | |
3220 | 5951 pos_T *trypos; |
5952 int just_started = TRUE; | |
7 | 5953 |
5954 if (sp == NULL) | |
5955 s = ml_get(lnum); | |
5956 else | |
5957 s = *sp; | |
5958 | |
3220 | 5959 if (find_last_paren(s, '(', ')') |
5960 && (trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL) | |
5961 { | |
5962 lnum = trypos->lnum; | |
5963 if (lnum < min_lnum) | |
5964 return FALSE; | |
5965 | |
5966 s = ml_get(lnum); | |
5967 } | |
5968 | |
2776 | 5969 /* Ignore line starting with #. */ |
5970 if (cin_ispreproc(s)) | |
5971 return FALSE; | |
5972 | |
7 | 5973 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') |
5974 { | |
5975 if (cin_iscomment(s)) /* ignore comments */ | |
5976 s = cin_skipcomment(s); | |
5977 else | |
5978 ++s; | |
5979 } | |
5980 if (*s != '(') | |
5981 return FALSE; /* ';', ' or " before any () or no '(' */ | |
5982 | |
5983 while (*s && *s != ';' && *s != '\'' && *s != '"') | |
5984 { | |
5985 if (*s == ')' && cin_nocode(s + 1)) | |
5986 { | |
5987 /* ')' at the end: may have found a match | |
5988 * Check for he previous line not to end in a backslash: | |
5989 * #if defined(x) && \ | |
5990 * defined(y) | |
5991 */ | |
5992 lnum = first_lnum - 1; | |
5993 s = ml_get(lnum); | |
5994 if (*s == NUL || s[STRLEN(s) - 1] != '\\') | |
5995 retval = TRUE; | |
5996 goto done; | |
5997 } | |
2776 | 5998 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) |
5999 { | |
6000 int comma = (*s == ','); | |
6001 | |
6002 /* ',' at the end: continue looking in the next line. | |
6003 * At the end: check for ',' in the next line, for this style: | |
6004 * func(arg1 | |
6005 * , arg2) */ | |
6006 for (;;) | |
6007 { | |
6008 if (lnum >= curbuf->b_ml.ml_line_count) | |
6009 break; | |
6010 s = ml_get(++lnum); | |
6011 if (!cin_ispreproc(s)) | |
6012 break; | |
6013 } | |
7 | 6014 if (lnum >= curbuf->b_ml.ml_line_count) |
6015 break; | |
2776 | 6016 /* Require a comma at end of the line or a comma or ')' at the |
6017 * start of next line. */ | |
6018 s = skipwhite(s); | |
3220 | 6019 if (!just_started && (!comma && *s != ',' && *s != ')')) |
2776 | 6020 break; |
3220 | 6021 just_started = FALSE; |
7 | 6022 } |
6023 else if (cin_iscomment(s)) /* ignore comments */ | |
6024 s = cin_skipcomment(s); | |
6025 else | |
3220 | 6026 { |
7 | 6027 ++s; |
3220 | 6028 just_started = FALSE; |
6029 } | |
7 | 6030 } |
6031 | |
6032 done: | |
6033 if (lnum != first_lnum && sp != NULL) | |
6034 *sp = ml_get(first_lnum); | |
6035 | |
6036 return retval; | |
6037 } | |
6038 | |
6039 static int | |
6040 cin_isif(p) | |
6041 char_u *p; | |
6042 { | |
6043 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); | |
6044 } | |
6045 | |
6046 static int | |
6047 cin_iselse(p) | |
6048 char_u *p; | |
6049 { | |
6050 if (*p == '}') /* accept "} else" */ | |
6051 p = cin_skipcomment(p + 1); | |
6052 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); | |
6053 } | |
6054 | |
6055 static int | |
6056 cin_isdo(p) | |
6057 char_u *p; | |
6058 { | |
6059 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); | |
6060 } | |
6061 | |
6062 /* | |
6063 * Check if this is a "while" that should have a matching "do". | |
6064 * We only accept a "while (condition) ;", with only white space between the | |
6065 * ')' and ';'. The condition may be spread over several lines. | |
6066 */ | |
6067 static int | |
6068 cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */ | |
6069 char_u *p; | |
6070 linenr_T lnum; | |
6071 int ind_maxparen; | |
6072 { | |
6073 pos_T cursor_save; | |
6074 pos_T *trypos; | |
6075 int retval = FALSE; | |
6076 | |
6077 p = cin_skipcomment(p); | |
6078 if (*p == '}') /* accept "} while (cond);" */ | |
6079 p = cin_skipcomment(p + 1); | |
4184 | 6080 if (cin_starts_with(p, "while")) |
7 | 6081 { |
6082 cursor_save = curwin->w_cursor; | |
6083 curwin->w_cursor.lnum = lnum; | |
6084 curwin->w_cursor.col = 0; | |
6085 p = ml_get_curline(); | |
6086 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ | |
6087 { | |
6088 ++p; | |
6089 ++curwin->w_cursor.col; | |
6090 } | |
6091 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL | |
6092 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') | |
6093 retval = TRUE; | |
6094 curwin->w_cursor = cursor_save; | |
6095 } | |
6096 return retval; | |
6097 } | |
6098 | |
829 | 6099 /* |
3461 | 6100 * Check whether in "p" there is an "if", "for" or "while" before "*poffset". |
3454 | 6101 * Return 0 if there is none. |
6102 * Otherwise return !0 and update "*poffset" to point to the place where the | |
6103 * string was found. | |
6104 */ | |
6105 static int | |
3461 | 6106 cin_is_if_for_while_before_offset(line, poffset) |
3454 | 6107 char_u *line; |
6108 int *poffset; | |
6109 { | |
3461 | 6110 int offset = *poffset; |
3454 | 6111 |
6112 if (offset-- < 2) | |
6113 return 0; | |
6114 while (offset > 2 && vim_iswhite(line[offset])) | |
6115 --offset; | |
6116 | |
6117 offset -= 1; | |
6118 if (!STRNCMP(line + offset, "if", 2)) | |
6119 goto probablyFound; | |
6120 | |
6121 if (offset >= 1) | |
6122 { | |
6123 offset -= 1; | |
6124 if (!STRNCMP(line + offset, "for", 3)) | |
6125 goto probablyFound; | |
6126 | |
6127 if (offset >= 2) | |
6128 { | |
6129 offset -= 2; | |
6130 if (!STRNCMP(line + offset, "while", 5)) | |
6131 goto probablyFound; | |
6132 } | |
6133 } | |
6134 return 0; | |
3461 | 6135 |
3454 | 6136 probablyFound: |
6137 if (!offset || !vim_isIDc(line[offset - 1])) | |
6138 { | |
6139 *poffset = offset; | |
6140 return 1; | |
6141 } | |
6142 return 0; | |
6143 } | |
6144 | |
6145 /* | |
829 | 6146 * Return TRUE if we are at the end of a do-while. |
6147 * do | |
6148 * nothing; | |
6149 * while (foo | |
856 | 6150 * && bar); <-- here |
829 | 6151 * Adjust the cursor to the line with "while". |
6152 */ | |
6153 static int | |
6154 cin_iswhileofdo_end(terminated, ind_maxparen, ind_maxcomment) | |
6155 int terminated; | |
6156 int ind_maxparen; | |
6157 int ind_maxcomment; | |
6158 { | |
6159 char_u *line; | |
6160 char_u *p; | |
6161 char_u *s; | |
6162 pos_T *trypos; | |
6163 int i; | |
6164 | |
6165 if (terminated != ';') /* there must be a ';' at the end */ | |
6166 return FALSE; | |
6167 | |
6168 p = line = ml_get_curline(); | |
6169 while (*p != NUL) | |
6170 { | |
6171 p = cin_skipcomment(p); | |
6172 if (*p == ')') | |
6173 { | |
6174 s = skipwhite(p + 1); | |
6175 if (*s == ';' && cin_nocode(s + 1)) | |
6176 { | |
6177 /* Found ");" at end of the line, now check there is "while" | |
6178 * before the matching '('. XXX */ | |
835 | 6179 i = (int)(p - line); |
829 | 6180 curwin->w_cursor.col = i; |
6181 trypos = find_match_paren(ind_maxparen, ind_maxcomment); | |
6182 if (trypos != NULL) | |
6183 { | |
6184 s = cin_skipcomment(ml_get(trypos->lnum)); | |
6185 if (*s == '}') /* accept "} while (cond);" */ | |
6186 s = cin_skipcomment(s + 1); | |
4184 | 6187 if (cin_starts_with(s, "while")) |
829 | 6188 { |
6189 curwin->w_cursor.lnum = trypos->lnum; | |
6190 return TRUE; | |
6191 } | |
6192 } | |
6193 | |
6194 /* Searching may have made "line" invalid, get it again. */ | |
6195 line = ml_get_curline(); | |
6196 p = line + i; | |
6197 } | |
6198 } | |
6199 if (*p != NUL) | |
6200 ++p; | |
6201 } | |
6202 return FALSE; | |
6203 } | |
6204 | |
7 | 6205 static int |
6206 cin_isbreak(p) | |
6207 char_u *p; | |
6208 { | |
6209 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); | |
6210 } | |
6211 | |
828 | 6212 /* |
6213 * Find the position of a C++ base-class declaration or | |
7 | 6214 * constructor-initialization. eg: |
6215 * | |
6216 * class MyClass : | |
6217 * baseClass <-- here | |
6218 * class MyClass : public baseClass, | |
6219 * anotherBaseClass <-- here (should probably lineup ??) | |
6220 * MyClass::MyClass(...) : | |
6221 * baseClass(...) <-- here (constructor-initialization) | |
827 | 6222 * |
6223 * This is a lot of guessing. Watch out for "cond ? func() : foo". | |
7 | 6224 */ |
6225 static int | |
1336 | 6226 cin_is_cpp_baseclass(col) |
828 | 6227 colnr_T *col; /* return: column to align with */ |
7 | 6228 { |
6229 char_u *s; | |
6230 int class_or_struct, lookfor_ctor_init, cpp_base_class; | |
828 | 6231 linenr_T lnum = curwin->w_cursor.lnum; |
1336 | 6232 char_u *line = ml_get_curline(); |
7 | 6233 |
6234 *col = 0; | |
6235 | |
17 | 6236 s = skipwhite(line); |
6237 if (*s == '#') /* skip #define FOO x ? (x) : x */ | |
6238 return FALSE; | |
6239 s = cin_skipcomment(s); | |
7 | 6240 if (*s == NUL) |
6241 return FALSE; | |
6242 | |
6243 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; | |
6244 | |
828 | 6245 /* Search for a line starting with '#', empty, ending in ';' or containing |
6246 * '{' or '}' and start below it. This handles the following situations: | |
6247 * a = cond ? | |
6248 * func() : | |
856 | 6249 * asdf; |
828 | 6250 * func::foo() |
6251 * : something | |
6252 * {} | |
6253 * Foo::Foo (int one, int two) | |
6254 * : something(4), | |
6255 * somethingelse(3) | |
6256 * {} | |
6257 */ | |
6258 while (lnum > 1) | |
6259 { | |
1336 | 6260 line = ml_get(lnum - 1); |
6261 s = skipwhite(line); | |
828 | 6262 if (*s == '#' || *s == NUL) |
6263 break; | |
6264 while (*s != NUL) | |
6265 { | |
6266 s = cin_skipcomment(s); | |
6267 if (*s == '{' || *s == '}' | |
6268 || (*s == ';' && cin_nocode(s + 1))) | |
6269 break; | |
6270 if (*s != NUL) | |
6271 ++s; | |
6272 } | |
6273 if (*s != NUL) | |
6274 break; | |
6275 --lnum; | |
6276 } | |
6277 | |
1336 | 6278 line = ml_get(lnum); |
6279 s = cin_skipcomment(line); | |
828 | 6280 for (;;) |
6281 { | |
6282 if (*s == NUL) | |
6283 { | |
6284 if (lnum == curwin->w_cursor.lnum) | |
6285 break; | |
6286 /* Continue in the cursor line. */ | |
1336 | 6287 line = ml_get(++lnum); |
6288 s = cin_skipcomment(line); | |
6289 if (*s == NUL) | |
6290 continue; | |
828 | 6291 } |
6292 | |
2807 | 6293 if (s[0] == '"') |
6294 s = skip_string(s) + 1; | |
6295 else if (s[0] == ':') | |
7 | 6296 { |
6297 if (s[1] == ':') | |
6298 { | |
6299 /* skip double colon. It can't be a constructor | |
6300 * initialization any more */ | |
6301 lookfor_ctor_init = FALSE; | |
6302 s = cin_skipcomment(s + 2); | |
6303 } | |
6304 else if (lookfor_ctor_init || class_or_struct) | |
6305 { | |
6306 /* we have something found, that looks like the start of | |
1532 | 6307 * cpp-base-class-declaration or constructor-initialization */ |
7 | 6308 cpp_base_class = TRUE; |
6309 lookfor_ctor_init = class_or_struct = FALSE; | |
6310 *col = 0; | |
6311 s = cin_skipcomment(s + 1); | |
6312 } | |
6313 else | |
6314 s = cin_skipcomment(s + 1); | |
6315 } | |
6316 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) | |
6317 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) | |
6318 { | |
6319 class_or_struct = TRUE; | |
6320 lookfor_ctor_init = FALSE; | |
6321 | |
6322 if (*s == 'c') | |
6323 s = cin_skipcomment(s + 5); | |
6324 else | |
6325 s = cin_skipcomment(s + 6); | |
6326 } | |
6327 else | |
6328 { | |
6329 if (s[0] == '{' || s[0] == '}' || s[0] == ';') | |
6330 { | |
6331 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; | |
6332 } | |
6333 else if (s[0] == ')') | |
6334 { | |
6335 /* Constructor-initialization is assumed if we come across | |
6336 * something like "):" */ | |
6337 class_or_struct = FALSE; | |
6338 lookfor_ctor_init = TRUE; | |
6339 } | |
827 | 6340 else if (s[0] == '?') |
6341 { | |
6342 /* Avoid seeing '() :' after '?' as constructor init. */ | |
6343 return FALSE; | |
6344 } | |
7 | 6345 else if (!vim_isIDc(s[0])) |
6346 { | |
6347 /* if it is not an identifier, we are wrong */ | |
6348 class_or_struct = FALSE; | |
6349 lookfor_ctor_init = FALSE; | |
6350 } | |
6351 else if (*col == 0) | |
6352 { | |
6353 /* it can't be a constructor-initialization any more */ | |
6354 lookfor_ctor_init = FALSE; | |
6355 | |
6356 /* the first statement starts here: lineup with this one... */ | |
828 | 6357 if (cpp_base_class) |
7 | 6358 *col = (colnr_T)(s - line); |
6359 } | |
6360 | |
828 | 6361 /* When the line ends in a comma don't align with it. */ |
6362 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) | |
6363 *col = 0; | |
6364 | |
7 | 6365 s = cin_skipcomment(s + 1); |
6366 } | |
6367 } | |
6368 | |
6369 return cpp_base_class; | |
6370 } | |
6371 | |
828 | 6372 static int |
6373 get_baseclass_amount(col, ind_maxparen, ind_maxcomment, ind_cpp_baseclass) | |
6374 int col; | |
6375 int ind_maxparen; | |
6376 int ind_maxcomment; | |
6377 int ind_cpp_baseclass; | |
6378 { | |
6379 int amount; | |
6380 colnr_T vcol; | |
6381 pos_T *trypos; | |
6382 | |
6383 if (col == 0) | |
6384 { | |
6385 amount = get_indent(); | |
6386 if (find_last_paren(ml_get_curline(), '(', ')') | |
6387 && (trypos = find_match_paren(ind_maxparen, | |
6388 ind_maxcomment)) != NULL) | |
6389 amount = get_indent_lnum(trypos->lnum); /* XXX */ | |
6390 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) | |
6391 amount += ind_cpp_baseclass; | |
6392 } | |
6393 else | |
6394 { | |
6395 curwin->w_cursor.col = col; | |
6396 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); | |
6397 amount = (int)vcol; | |
6398 } | |
6399 if (amount < ind_cpp_baseclass) | |
6400 amount = ind_cpp_baseclass; | |
6401 return amount; | |
6402 } | |
6403 | |
7 | 6404 /* |
6405 * Return TRUE if string "s" ends with the string "find", possibly followed by | |
6406 * white space and comments. Skip strings and comments. | |
6407 * Ignore "ignore" after "find" if it's not NULL. | |
6408 */ | |
6409 static int | |
6410 cin_ends_in(s, find, ignore) | |
6411 char_u *s; | |
6412 char_u *find; | |
6413 char_u *ignore; | |
6414 { | |
6415 char_u *p = s; | |
6416 char_u *r; | |
6417 int len = (int)STRLEN(find); | |
6418 | |
6419 while (*p != NUL) | |
6420 { | |
6421 p = cin_skipcomment(p); | |
6422 if (STRNCMP(p, find, len) == 0) | |
6423 { | |
6424 r = skipwhite(p + len); | |
6425 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) | |
6426 r = skipwhite(r + STRLEN(ignore)); | |
6427 if (cin_nocode(r)) | |
6428 return TRUE; | |
6429 } | |
6430 if (*p != NUL) | |
6431 ++p; | |
6432 } | |
6433 return FALSE; | |
6434 } | |
6435 | |
6436 /* | |
4184 | 6437 * Return TRUE when "s" starts with "word" and then a non-ID character. |
6438 */ | |
6439 static int | |
6440 cin_starts_with(s, word) | |
6441 char_u *s; | |
6442 char *word; | |
6443 { | |
4207 | 6444 int l = (int)STRLEN(word); |
4184 | 6445 |
6446 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); | |
6447 } | |
6448 | |
6449 /* | |
7 | 6450 * Skip strings, chars and comments until at or past "trypos". |
6451 * Return the column found. | |
6452 */ | |
6453 static int | |
6454 cin_skip2pos(trypos) | |
6455 pos_T *trypos; | |
6456 { | |
6457 char_u *line; | |
6458 char_u *p; | |
6459 | |
6460 p = line = ml_get(trypos->lnum); | |
6461 while (*p && (colnr_T)(p - line) < trypos->col) | |
6462 { | |
6463 if (cin_iscomment(p)) | |
6464 p = cin_skipcomment(p); | |
6465 else | |
6466 { | |
6467 p = skip_string(p); | |
6468 ++p; | |
6469 } | |
6470 } | |
6471 return (int)(p - line); | |
6472 } | |
6473 | |
6474 /* | |
6475 * Find the '{' at the start of the block we are in. | |
6476 * Return NULL if no match found. | |
6477 * Ignore a '{' that is in a comment, makes indenting the next three lines | |
6478 * work. */ | |
6479 /* foo() */ | |
6480 /* { */ | |
6481 /* } */ | |
6482 | |
6483 static pos_T * | |
6484 find_start_brace(ind_maxcomment) /* XXX */ | |
6485 int ind_maxcomment; | |
6486 { | |
6487 pos_T cursor_save; | |
6488 pos_T *trypos; | |
6489 pos_T *pos; | |
6490 static pos_T pos_copy; | |
6491 | |
6492 cursor_save = curwin->w_cursor; | |
6493 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) | |
6494 { | |
6495 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ | |
6496 trypos = &pos_copy; | |
6497 curwin->w_cursor = *trypos; | |
6498 pos = NULL; | |
829 | 6499 /* ignore the { if it's in a // or / * * / comment */ |
7 | 6500 if ((colnr_T)cin_skip2pos(trypos) == trypos->col |
6501 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */ | |
6502 break; | |
6503 if (pos != NULL) | |
6504 curwin->w_cursor.lnum = pos->lnum; | |
6505 } | |
6506 curwin->w_cursor = cursor_save; | |
6507 return trypos; | |
6508 } | |
6509 | |
6510 /* | |
6511 * Find the matching '(', failing if it is in a comment. | |
3454 | 6512 * Return NULL if no match found. |
7 | 6513 */ |
6514 static pos_T * | |
6515 find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */ | |
6516 int ind_maxparen; | |
6517 int ind_maxcomment; | |
6518 { | |
6519 pos_T cursor_save; | |
6520 pos_T *trypos; | |
829 | 6521 static pos_T pos_copy; |
7 | 6522 |
6523 cursor_save = curwin->w_cursor; | |
6524 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL) | |
6525 { | |
6526 /* check if the ( is in a // comment */ | |
6527 if ((colnr_T)cin_skip2pos(trypos) > trypos->col) | |
6528 trypos = NULL; | |
6529 else | |
6530 { | |
6531 pos_copy = *trypos; /* copy trypos, findmatch will change it */ | |
6532 trypos = &pos_copy; | |
6533 curwin->w_cursor = *trypos; | |
6534 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */ | |
6535 trypos = NULL; | |
6536 } | |
6537 } | |
6538 curwin->w_cursor = cursor_save; | |
6539 return trypos; | |
6540 } | |
6541 | |
6542 /* | |
6543 * Return ind_maxparen corrected for the difference in line number between the | |
6544 * cursor position and "startpos". This makes sure that searching for a | |
6545 * matching paren above the cursor line doesn't find a match because of | |
6546 * looking a few lines further. | |
6547 */ | |
6548 static int | |
6549 corr_ind_maxparen(ind_maxparen, startpos) | |
6550 int ind_maxparen; | |
6551 pos_T *startpos; | |
6552 { | |
6553 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; | |
6554 | |
6555 if (n > 0 && n < ind_maxparen / 2) | |
6556 return ind_maxparen - (int)n; | |
6557 return ind_maxparen; | |
6558 } | |
6559 | |
6560 /* | |
6561 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in | |
3172 | 6562 * line "l". "l" must point to the start of the line. |
7 | 6563 */ |
6564 static int | |
6565 find_last_paren(l, start, end) | |
6566 char_u *l; | |
6567 int start, end; | |
6568 { | |
6569 int i; | |
6570 int retval = FALSE; | |
6571 int open_count = 0; | |
6572 | |
6573 curwin->w_cursor.col = 0; /* default is start of line */ | |
6574 | |
3172 | 6575 for (i = 0; l[i] != NUL; i++) |
7 | 6576 { |
6577 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ | |
6578 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ | |
6579 if (l[i] == start) | |
6580 ++open_count; | |
6581 else if (l[i] == end) | |
6582 { | |
6583 if (open_count > 0) | |
6584 --open_count; | |
6585 else | |
6586 { | |
6587 curwin->w_cursor.col = i; | |
6588 retval = TRUE; | |
6589 } | |
6590 } | |
6591 } | |
6592 return retval; | |
6593 } | |
6594 | |
6595 int | |
6596 get_c_indent() | |
6597 { | |
3740 | 6598 int sw = (int)get_sw_value(); |
6599 | |
7 | 6600 /* |
6601 * spaces from a block's opening brace the prevailing indent for that | |
6602 * block should be | |
6603 */ | |
3740 | 6604 |
6605 int ind_level = sw; | |
7 | 6606 |
6607 /* | |
6608 * spaces from the edge of the line an open brace that's at the end of a | |
6609 * line is imagined to be. | |
6610 */ | |
6611 int ind_open_imag = 0; | |
6612 | |
6613 /* | |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
6614 * spaces from the prevailing indent for a line that is not preceded by |
7 | 6615 * an opening brace. |
6616 */ | |
6617 int ind_no_brace = 0; | |
6618 | |
6619 /* | |
6620 * column where the first { of a function should be located } | |
6621 */ | |
6622 int ind_first_open = 0; | |
6623 | |
6624 /* | |
6625 * spaces from the prevailing indent a leftmost open brace should be | |
6626 * located | |
6627 */ | |
6628 int ind_open_extra = 0; | |
6629 | |
6630 /* | |
6631 * spaces from the matching open brace (real location for one at the left | |
6632 * edge; imaginary location from one that ends a line) the matching close | |
6633 * brace should be located | |
6634 */ | |
6635 int ind_close_extra = 0; | |
6636 | |
6637 /* | |
6638 * spaces from the edge of the line an open brace sitting in the leftmost | |
6639 * column is imagined to be | |
6640 */ | |
6641 int ind_open_left_imag = 0; | |
6642 | |
6643 /* | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6644 * Spaces jump labels should be shifted to the left if N is non-negative, |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6645 * otherwise the jump label will be put to column 1. |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6646 */ |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6647 int ind_jump_label = -1; |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6648 |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6649 /* |
7 | 6650 * spaces from the switch() indent a "case xx" label should be located |
6651 */ | |
3740 | 6652 int ind_case = sw; |
7 | 6653 |
6654 /* | |
6655 * spaces from the "case xx:" code after a switch() should be located | |
6656 */ | |
3740 | 6657 int ind_case_code = sw; |
7 | 6658 |
6659 /* | |
6660 * lineup break at end of case in switch() with case label | |
6661 */ | |
6662 int ind_case_break = 0; | |
6663 | |
6664 /* | |
6665 * spaces from the class declaration indent a scope declaration label | |
6666 * should be located | |
6667 */ | |
3740 | 6668 int ind_scopedecl = sw; |
7 | 6669 |
6670 /* | |
6671 * spaces from the scope declaration label code should be located | |
6672 */ | |
3740 | 6673 int ind_scopedecl_code = sw; |
7 | 6674 |
6675 /* | |
6676 * amount K&R-style parameters should be indented | |
6677 */ | |
3740 | 6678 int ind_param = sw; |
7 | 6679 |
6680 /* | |
6681 * amount a function type spec should be indented | |
6682 */ | |
3740 | 6683 int ind_func_type = sw; |
7 | 6684 |
6685 /* | |
6686 * amount a cpp base class declaration or constructor initialization | |
6687 * should be indented | |
6688 */ | |
3740 | 6689 int ind_cpp_baseclass = sw; |
7 | 6690 |
6691 /* | |
6692 * additional spaces beyond the prevailing indent a continuation line | |
6693 * should be located | |
6694 */ | |
3740 | 6695 int ind_continuation = sw; |
7 | 6696 |
6697 /* | |
6698 * spaces from the indent of the line with an unclosed parentheses | |
6699 */ | |
3740 | 6700 int ind_unclosed = sw * 2; |
7 | 6701 |
6702 /* | |
6703 * spaces from the indent of the line with an unclosed parentheses, which | |
6704 * itself is also unclosed | |
6705 */ | |
3740 | 6706 int ind_unclosed2 = sw; |
7 | 6707 |
6708 /* | |
6709 * suppress ignoring spaces from the indent of a line starting with an | |
6710 * unclosed parentheses. | |
6711 */ | |
6712 int ind_unclosed_noignore = 0; | |
6713 | |
6714 /* | |
6715 * If the opening paren is the last nonwhite character on the line, and | |
6716 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer | |
6717 * context (for very long lines). | |
6718 */ | |
6719 int ind_unclosed_wrapped = 0; | |
6720 | |
6721 /* | |
6722 * suppress ignoring white space when lining up with the character after | |
6723 * an unclosed parentheses. | |
6724 */ | |
6725 int ind_unclosed_whiteok = 0; | |
6726 | |
6727 /* | |
6728 * indent a closing parentheses under the line start of the matching | |
6729 * opening parentheses. | |
6730 */ | |
6731 int ind_matching_paren = 0; | |
6732 | |
6733 /* | |
829 | 6734 * indent a closing parentheses under the previous line. |
6735 */ | |
6736 int ind_paren_prev = 0; | |
6737 | |
6738 /* | |
7 | 6739 * Extra indent for comments. |
6740 */ | |
6741 int ind_comment = 0; | |
6742 | |
6743 /* | |
6744 * spaces from the comment opener when there is nothing after it. | |
6745 */ | |
6746 int ind_in_comment = 3; | |
6747 | |
6748 /* | |
6749 * boolean: if non-zero, use ind_in_comment even if there is something | |
6750 * after the comment opener. | |
6751 */ | |
6752 int ind_in_comment2 = 0; | |
6753 | |
6754 /* | |
6755 * max lines to search for an open paren | |
6756 */ | |
6757 int ind_maxparen = 20; | |
6758 | |
6759 /* | |
6760 * max lines to search for an open comment | |
6761 */ | |
6762 int ind_maxcomment = 70; | |
6763 | |
6764 /* | |
6765 * handle braces for java code | |
6766 */ | |
6767 int ind_java = 0; | |
6768 | |
6769 /* | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6770 * not to confuse JS object properties with labels |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6771 */ |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6772 int ind_js = 0; |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6773 |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6774 /* |
7 | 6775 * handle blocked cases correctly |
6776 */ | |
6777 int ind_keep_case_label = 0; | |
6778 | |
2857 | 6779 /* |
6780 * handle C++ namespace | |
6781 */ | |
6782 int ind_cpp_namespace = 0; | |
6783 | |
3454 | 6784 /* |
6785 * handle continuation lines containing conditions of if(), for() and | |
6786 * while() | |
6787 */ | |
6788 int ind_if_for_while = 0; | |
6789 | |
7 | 6790 pos_T cur_curpos; |
6791 int amount; | |
6792 int scope_amount; | |
834 | 6793 int cur_amount = MAXCOL; |
7 | 6794 colnr_T col; |
6795 char_u *theline; | |
6796 char_u *linecopy; | |
6797 pos_T *trypos; | |
6798 pos_T *tryposBrace = NULL; | |
6799 pos_T our_paren_pos; | |
6800 char_u *start; | |
6801 int start_brace; | |
1532 | 6802 #define BRACE_IN_COL0 1 /* '{' is in column 0 */ |
7 | 6803 #define BRACE_AT_START 2 /* '{' is at start of line */ |
6804 #define BRACE_AT_END 3 /* '{' is at end of line */ | |
6805 linenr_T ourscope; | |
6806 char_u *l; | |
6807 char_u *look; | |
6808 char_u terminated; | |
6809 int lookfor; | |
6810 #define LOOKFOR_INITIAL 0 | |
6811 #define LOOKFOR_IF 1 | |
6812 #define LOOKFOR_DO 2 | |
6813 #define LOOKFOR_CASE 3 | |
6814 #define LOOKFOR_ANY 4 | |
6815 #define LOOKFOR_TERM 5 | |
6816 #define LOOKFOR_UNTERM 6 | |
6817 #define LOOKFOR_SCOPEDECL 7 | |
6818 #define LOOKFOR_NOBREAK 8 | |
6819 #define LOOKFOR_CPP_BASECLASS 9 | |
6820 #define LOOKFOR_ENUM_OR_INIT 10 | |
6821 | |
6822 int whilelevel; | |
6823 linenr_T lnum; | |
6824 char_u *options; | |
3578 | 6825 char_u *digits; |
7 | 6826 int fraction = 0; /* init for GCC */ |
6827 int divider; | |
6828 int n; | |
6829 int iscase; | |
6830 int lookfor_break; | |
2857 | 6831 int lookfor_cpp_namespace = FALSE; |
7 | 6832 int cont_amount = 0; /* amount for continuation line */ |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6833 int original_line_islabel; |
3123 | 6834 int added_to_amount = 0; |
7 | 6835 |
6836 for (options = curbuf->b_p_cino; *options; ) | |
6837 { | |
6838 l = options++; | |
6839 if (*options == '-') | |
6840 ++options; | |
3578 | 6841 digits = options; /* remember where the digits start */ |
7 | 6842 n = getdigits(&options); |
6843 divider = 0; | |
6844 if (*options == '.') /* ".5s" means a fraction */ | |
6845 { | |
6846 fraction = atol((char *)++options); | |
6847 while (VIM_ISDIGIT(*options)) | |
6848 { | |
6849 ++options; | |
6850 if (divider) | |
6851 divider *= 10; | |
6852 else | |
6853 divider = 10; | |
6854 } | |
6855 } | |
6856 if (*options == 's') /* "2s" means two times 'shiftwidth' */ | |
6857 { | |
3578 | 6858 if (options == digits) |
3740 | 6859 n = sw; /* just "s" is one 'shiftwidth' */ |
7 | 6860 else |
6861 { | |
3740 | 6862 n *= sw; |
7 | 6863 if (divider) |
3740 | 6864 n += (sw * fraction + divider / 2) / divider; |
7 | 6865 } |
6866 ++options; | |
6867 } | |
6868 if (l[1] == '-') | |
6869 n = -n; | |
6870 /* When adding an entry here, also update the default 'cinoptions' in | |
1096 | 6871 * doc/indent.txt, and add explanation for it! */ |
7 | 6872 switch (*l) |
6873 { | |
6874 case '>': ind_level = n; break; | |
6875 case 'e': ind_open_imag = n; break; | |
6876 case 'n': ind_no_brace = n; break; | |
6877 case 'f': ind_first_open = n; break; | |
6878 case '{': ind_open_extra = n; break; | |
6879 case '}': ind_close_extra = n; break; | |
6880 case '^': ind_open_left_imag = n; break; | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6881 case 'L': ind_jump_label = n; break; |
7 | 6882 case ':': ind_case = n; break; |
6883 case '=': ind_case_code = n; break; | |
6884 case 'b': ind_case_break = n; break; | |
6885 case 'p': ind_param = n; break; | |
6886 case 't': ind_func_type = n; break; | |
6887 case '/': ind_comment = n; break; | |
6888 case 'c': ind_in_comment = n; break; | |
6889 case 'C': ind_in_comment2 = n; break; | |
6890 case 'i': ind_cpp_baseclass = n; break; | |
6891 case '+': ind_continuation = n; break; | |
6892 case '(': ind_unclosed = n; break; | |
6893 case 'u': ind_unclosed2 = n; break; | |
6894 case 'U': ind_unclosed_noignore = n; break; | |
6895 case 'W': ind_unclosed_wrapped = n; break; | |
6896 case 'w': ind_unclosed_whiteok = n; break; | |
6897 case 'm': ind_matching_paren = n; break; | |
829 | 6898 case 'M': ind_paren_prev = n; break; |
7 | 6899 case ')': ind_maxparen = n; break; |
6900 case '*': ind_maxcomment = n; break; | |
6901 case 'g': ind_scopedecl = n; break; | |
6902 case 'h': ind_scopedecl_code = n; break; | |
6903 case 'j': ind_java = n; break; | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6904 case 'J': ind_js = n; break; |
7 | 6905 case 'l': ind_keep_case_label = n; break; |
1096 | 6906 case '#': ind_hash_comment = n; break; |
2857 | 6907 case 'N': ind_cpp_namespace = n; break; |
3454 | 6908 case 'k': ind_if_for_while = n; break; |
7 | 6909 } |
2129
24100651daa9
updated for version 7.2.411
Bram Moolenaar <bram@zimbu.org>
parents:
2094
diff
changeset
|
6910 if (*options == ',') |
24100651daa9
updated for version 7.2.411
Bram Moolenaar <bram@zimbu.org>
parents:
2094
diff
changeset
|
6911 ++options; |
7 | 6912 } |
6913 | |
6914 /* remember where the cursor was when we started */ | |
6915 cur_curpos = curwin->w_cursor; | |
6916 | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6917 /* if we are at line 1 0 is fine, right? */ |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6918 if (cur_curpos.lnum == 1) |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6919 return 0; |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6920 |
7 | 6921 /* Get a copy of the current contents of the line. |
6922 * This is required, because only the most recent line obtained with | |
6923 * ml_get is valid! */ | |
6924 linecopy = vim_strsave(ml_get(cur_curpos.lnum)); | |
6925 if (linecopy == NULL) | |
6926 return 0; | |
6927 | |
6928 /* | |
6929 * In insert mode and the cursor is on a ')' truncate the line at the | |
6930 * cursor position. We don't want to line up with the matching '(' when | |
6931 * inserting new stuff. | |
6932 * For unknown reasons the cursor might be past the end of the line, thus | |
6933 * check for that. | |
6934 */ | |
6935 if ((State & INSERT) | |
1883 | 6936 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
7 | 6937 && linecopy[curwin->w_cursor.col] == ')') |
6938 linecopy[curwin->w_cursor.col] = NUL; | |
6939 | |
6940 theline = skipwhite(linecopy); | |
6941 | |
6942 /* move the cursor to the start of the line */ | |
6943 | |
6944 curwin->w_cursor.col = 0; | |
6945 | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6946 original_line_islabel = cin_islabel(ind_maxcomment); /* XXX */ |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6947 |
7 | 6948 /* |
6949 * #defines and so on always go at the left when included in 'cinkeys'. | |
6950 */ | |
6951 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) | |
6952 { | |
6953 amount = 0; | |
6954 } | |
6955 | |
6956 /* | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6957 * Is it a non-case label? Then that goes at the left margin too unless: |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6958 * - JS flag is set. |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6959 * - 'L' item has a positive value. |
7 | 6960 */ |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6961 else if (original_line_islabel && !ind_js && ind_jump_label < 0) |
7 | 6962 { |
6963 amount = 0; | |
6964 } | |
6965 | |
6966 /* | |
6967 * If we're inside a "//" comment and there is a "//" comment in a | |
6968 * previous line, lineup with that one. | |
6969 */ | |
6970 else if (cin_islinecomment(theline) | |
6971 && (trypos = find_line_comment()) != NULL) /* XXX */ | |
6972 { | |
6973 /* find how indented the line beginning the comment is */ | |
6974 getvcol(curwin, trypos, &col, NULL, NULL); | |
6975 amount = col; | |
6976 } | |
6977 | |
6978 /* | |
6979 * If we're inside a comment and not looking at the start of the | |
6980 * comment, try using the 'comments' option. | |
6981 */ | |
6982 else if (!cin_iscomment(theline) | |
6983 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */ | |
6984 { | |
6985 int lead_start_len = 2; | |
6986 int lead_middle_len = 1; | |
6987 char_u lead_start[COM_MAX_LEN]; /* start-comment string */ | |
6988 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ | |
6989 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ | |
6990 char_u *p; | |
6991 int start_align = 0; | |
6992 int start_off = 0; | |
6993 int done = FALSE; | |
6994 | |
6995 /* find how indented the line beginning the comment is */ | |
6996 getvcol(curwin, trypos, &col, NULL, NULL); | |
6997 amount = col; | |
2764 | 6998 *lead_start = NUL; |
6999 *lead_middle = NUL; | |
7 | 7000 |
7001 p = curbuf->b_p_com; | |
7002 while (*p != NUL) | |
7003 { | |
7004 int align = 0; | |
7005 int off = 0; | |
7006 int what = 0; | |
7007 | |
7008 while (*p != NUL && *p != ':') | |
7009 { | |
7010 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) | |
7011 what = *p++; | |
7012 else if (*p == COM_LEFT || *p == COM_RIGHT) | |
7013 align = *p++; | |
7014 else if (VIM_ISDIGIT(*p) || *p == '-') | |
7015 off = getdigits(&p); | |
7016 else | |
7017 ++p; | |
7018 } | |
7019 | |
7020 if (*p == ':') | |
7021 ++p; | |
7022 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
7023 if (what == COM_START) | |
7024 { | |
7025 STRCPY(lead_start, lead_end); | |
7026 lead_start_len = (int)STRLEN(lead_start); | |
7027 start_off = off; | |
7028 start_align = align; | |
7029 } | |
7030 else if (what == COM_MIDDLE) | |
7031 { | |
7032 STRCPY(lead_middle, lead_end); | |
7033 lead_middle_len = (int)STRLEN(lead_middle); | |
7034 } | |
7035 else if (what == COM_END) | |
7036 { | |
7037 /* If our line starts with the middle comment string, line it | |
7038 * up with the comment opener per the 'comments' option. */ | |
7039 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 | |
7040 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) | |
7041 { | |
7042 done = TRUE; | |
7043 if (curwin->w_cursor.lnum > 1) | |
7044 { | |
7045 /* If the start comment string matches in the previous | |
1532 | 7046 * line, use the indent of that line plus offset. If |
7 | 7047 * the middle comment string matches in the previous |
7048 * line, use the indent of that line. XXX */ | |
7049 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); | |
7050 if (STRNCMP(look, lead_start, lead_start_len) == 0) | |
7051 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
7052 else if (STRNCMP(look, lead_middle, | |
7053 lead_middle_len) == 0) | |
7054 { | |
7055 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
7056 break; | |
7057 } | |
7058 /* If the start comment string doesn't match with the | |
7059 * start of the comment, skip this entry. XXX */ | |
7060 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col, | |
7061 lead_start, lead_start_len) != 0) | |
7062 continue; | |
7063 } | |
7064 if (start_off != 0) | |
7065 amount += start_off; | |
7066 else if (start_align == COM_RIGHT) | |
17 | 7067 amount += vim_strsize(lead_start) |
7068 - vim_strsize(lead_middle); | |
7 | 7069 break; |
7070 } | |
7071 | |
7072 /* If our line starts with the end comment string, line it up | |
7073 * with the middle comment */ | |
7074 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 | |
7075 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) | |
7076 { | |
7077 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
7078 /* XXX */ | |
7079 if (off != 0) | |
7080 amount += off; | |
7081 else if (align == COM_RIGHT) | |
17 | 7082 amount += vim_strsize(lead_start) |
7083 - vim_strsize(lead_middle); | |
7 | 7084 done = TRUE; |
7085 break; | |
7086 } | |
7087 } | |
7088 } | |
7089 | |
7090 /* If our line starts with an asterisk, line up with the | |
7091 * asterisk in the comment opener; otherwise, line up | |
7092 * with the first character of the comment text. | |
7093 */ | |
7094 if (done) | |
7095 ; | |
7096 else if (theline[0] == '*') | |
7097 amount += 1; | |
7098 else | |
7099 { | |
7100 /* | |
7101 * If we are more than one line away from the comment opener, take | |
7102 * the indent of the previous non-empty line. If 'cino' has "CO" | |
7103 * and we are just below the comment opener and there are any | |
7104 * white characters after it line up with the text after it; | |
7105 * otherwise, add the amount specified by "c" in 'cino' | |
7106 */ | |
7107 amount = -1; | |
7108 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum) | |
7109 { | |
7110 if (linewhite(lnum)) /* skip blank lines */ | |
7111 continue; | |
7112 amount = get_indent_lnum(lnum); /* XXX */ | |
7113 break; | |
7114 } | |
7115 if (amount == -1) /* use the comment opener */ | |
7116 { | |
7117 if (!ind_in_comment2) | |
7118 { | |
7119 start = ml_get(trypos->lnum); | |
7120 look = start + trypos->col + 2; /* skip / and * */ | |
7121 if (*look != NUL) /* if something after it */ | |
7122 trypos->col = (colnr_T)(skipwhite(look) - start); | |
7123 } | |
7124 getvcol(curwin, trypos, &col, NULL, NULL); | |
7125 amount = col; | |
7126 if (ind_in_comment2 || *look == NUL) | |
7127 amount += ind_in_comment; | |
7128 } | |
7129 } | |
7130 } | |
7131 | |
7132 /* | |
7133 * Are we inside parentheses or braces? | |
7134 */ /* XXX */ | |
7135 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL | |
7136 && ind_java == 0) | |
7137 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL | |
7138 || trypos != NULL) | |
7139 { | |
7140 if (trypos != NULL && tryposBrace != NULL) | |
7141 { | |
7142 /* Both an unmatched '(' and '{' is found. Use the one which is | |
7143 * closer to the current cursor position, set the other to NULL. */ | |
7144 if (trypos->lnum != tryposBrace->lnum | |
7145 ? trypos->lnum < tryposBrace->lnum | |
7146 : trypos->col < tryposBrace->col) | |
7147 trypos = NULL; | |
7148 else | |
7149 tryposBrace = NULL; | |
7150 } | |
7151 | |
7152 if (trypos != NULL) | |
7153 { | |
7154 /* | |
7155 * If the matching paren is more than one line away, use the indent of | |
7156 * a previous non-empty line that matches the same paren. | |
7157 */ | |
829 | 7158 if (theline[0] == ')' && ind_paren_prev) |
7159 { | |
7160 /* Line up with the start of the matching paren line. */ | |
7161 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */ | |
7162 } | |
7163 else | |
7164 { | |
7165 amount = -1; | |
7166 our_paren_pos = *trypos; | |
7167 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) | |
7 | 7168 { |
829 | 7169 l = skipwhite(ml_get(lnum)); |
7170 if (cin_nocode(l)) /* skip comment lines */ | |
7171 continue; | |
7172 if (cin_ispreproc_cont(&l, &lnum)) | |
7173 continue; /* ignore #define, #if, etc. */ | |
7174 curwin->w_cursor.lnum = lnum; | |
7175 | |
7176 /* Skip a comment. XXX */ | |
7177 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) | |
7178 { | |
7179 lnum = trypos->lnum + 1; | |
7180 continue; | |
7181 } | |
7182 | |
7183 /* XXX */ | |
7184 if ((trypos = find_match_paren( | |
7185 corr_ind_maxparen(ind_maxparen, &cur_curpos), | |
7 | 7186 ind_maxcomment)) != NULL |
829 | 7187 && trypos->lnum == our_paren_pos.lnum |
7188 && trypos->col == our_paren_pos.col) | |
7189 { | |
7190 amount = get_indent_lnum(lnum); /* XXX */ | |
7191 | |
7192 if (theline[0] == ')') | |
7193 { | |
7194 if (our_paren_pos.lnum != lnum | |
7195 && cur_amount > amount) | |
7196 cur_amount = amount; | |
7197 amount = -1; | |
7198 } | |
7199 break; | |
7200 } | |
7 | 7201 } |
7202 } | |
7203 | |
7204 /* | |
7205 * Line up with line where the matching paren is. XXX | |
7206 * If the line starts with a '(' or the indent for unclosed | |
7207 * parentheses is zero, line up with the unclosed parentheses. | |
7208 */ | |
7209 if (amount == -1) | |
7210 { | |
829 | 7211 int ignore_paren_col = 0; |
3454 | 7212 int is_if_for_while = 0; |
7213 | |
7214 if (ind_if_for_while) | |
7215 { | |
7216 /* Look for the outermost opening parenthesis on this line | |
7217 * and check whether it belongs to an "if", "for" or "while". */ | |
7218 | |
7219 pos_T cursor_save = curwin->w_cursor; | |
7220 pos_T outermost; | |
7221 char_u *line; | |
7222 | |
7223 trypos = &our_paren_pos; | |
7224 do { | |
7225 outermost = *trypos; | |
7226 curwin->w_cursor.lnum = outermost.lnum; | |
7227 curwin->w_cursor.col = outermost.col; | |
7228 | |
7229 trypos = find_match_paren(ind_maxparen, ind_maxcomment); | |
7230 } while (trypos && trypos->lnum == outermost.lnum); | |
7231 | |
7232 curwin->w_cursor = cursor_save; | |
7233 | |
7234 line = ml_get(outermost.lnum); | |
7235 | |
7236 is_if_for_while = | |
3461 | 7237 cin_is_if_for_while_before_offset(line, &outermost.col); |
3454 | 7238 } |
829 | 7239 |
7 | 7240 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment); |
829 | 7241 look = skipwhite(look); |
7242 if (*look == '(') | |
7243 { | |
7244 linenr_T save_lnum = curwin->w_cursor.lnum; | |
7245 char_u *line; | |
7246 int look_col; | |
7247 | |
7248 /* Ignore a '(' in front of the line that has a match before | |
7249 * our matching '('. */ | |
7250 curwin->w_cursor.lnum = our_paren_pos.lnum; | |
7251 line = ml_get_curline(); | |
835 | 7252 look_col = (int)(look - line); |
829 | 7253 curwin->w_cursor.col = look_col + 1; |
7254 if ((trypos = findmatchlimit(NULL, ')', 0, ind_maxparen)) | |
7255 != NULL | |
7256 && trypos->lnum == our_paren_pos.lnum | |
7257 && trypos->col < our_paren_pos.col) | |
7258 ignore_paren_col = trypos->col + 1; | |
7259 | |
7260 curwin->w_cursor.lnum = save_lnum; | |
7261 look = ml_get(our_paren_pos.lnum) + look_col; | |
7262 } | |
3454 | 7263 if (theline[0] == ')' || (ind_unclosed == 0 && is_if_for_while == 0) |
829 | 7264 || (!ind_unclosed_noignore && *look == '(' |
7265 && ignore_paren_col == 0)) | |
7 | 7266 { |
7267 /* | |
7268 * If we're looking at a close paren, line up right there; | |
7269 * otherwise, line up with the next (non-white) character. | |
7270 * When ind_unclosed_wrapped is set and the matching paren is | |
7271 * the last nonwhite character of the line, use either the | |
7272 * indent of the current line or the indentation of the next | |
7273 * outer paren and add ind_unclosed_wrapped (for very long | |
7274 * lines). | |
7275 */ | |
7276 if (theline[0] != ')') | |
7277 { | |
7278 cur_amount = MAXCOL; | |
7279 l = ml_get(our_paren_pos.lnum); | |
7280 if (ind_unclosed_wrapped | |
7281 && cin_ends_in(l, (char_u *)"(", NULL)) | |
7282 { | |
7283 /* look for opening unmatched paren, indent one level | |
7284 * for each additional level */ | |
7285 n = 1; | |
7286 for (col = 0; col < our_paren_pos.col; ++col) | |
7287 { | |
7288 switch (l[col]) | |
7289 { | |
7290 case '(': | |
7291 case '{': ++n; | |
7292 break; | |
7293 | |
7294 case ')': | |
7295 case '}': if (n > 1) | |
7296 --n; | |
7297 break; | |
7298 } | |
7299 } | |
7300 | |
7301 our_paren_pos.col = 0; | |
7302 amount += n * ind_unclosed_wrapped; | |
7303 } | |
7304 else if (ind_unclosed_whiteok) | |
7305 our_paren_pos.col++; | |
7306 else | |
7307 { | |
7308 col = our_paren_pos.col + 1; | |
7309 while (vim_iswhite(l[col])) | |
7310 col++; | |
7311 if (l[col] != NUL) /* In case of trailing space */ | |
7312 our_paren_pos.col = col; | |
7313 else | |
7314 our_paren_pos.col++; | |
7315 } | |
7316 } | |
7317 | |
7318 /* | |
7319 * Find how indented the paren is, or the character after it | |
7320 * if we did the above "if". | |
7321 */ | |
7322 if (our_paren_pos.col > 0) | |
7323 { | |
7324 getvcol(curwin, &our_paren_pos, &col, NULL, NULL); | |
7325 if (cur_amount > (int)col) | |
7326 cur_amount = col; | |
7327 } | |
7328 } | |
7329 | |
7330 if (theline[0] == ')' && ind_matching_paren) | |
7331 { | |
7332 /* Line up with the start of the matching paren line. */ | |
7333 } | |
3454 | 7334 else if ((ind_unclosed == 0 && is_if_for_while == 0) |
7335 || (!ind_unclosed_noignore | |
829 | 7336 && *look == '(' && ignore_paren_col == 0)) |
7 | 7337 { |
7338 if (cur_amount != MAXCOL) | |
7339 amount = cur_amount; | |
7340 } | |
7341 else | |
7342 { | |
829 | 7343 /* Add ind_unclosed2 for each '(' before our matching one, but |
7344 * ignore (void) before the line (ignore_paren_col). */ | |
7 | 7345 col = our_paren_pos.col; |
834 | 7346 while ((int)our_paren_pos.col > ignore_paren_col) |
7 | 7347 { |
7348 --our_paren_pos.col; | |
7349 switch (*ml_get_pos(&our_paren_pos)) | |
7350 { | |
7351 case '(': amount += ind_unclosed2; | |
7352 col = our_paren_pos.col; | |
7353 break; | |
7354 case ')': amount -= ind_unclosed2; | |
7355 col = MAXCOL; | |
7356 break; | |
7357 } | |
7358 } | |
7359 | |
7360 /* Use ind_unclosed once, when the first '(' is not inside | |
7361 * braces */ | |
7362 if (col == MAXCOL) | |
7363 amount += ind_unclosed; | |
7364 else | |
7365 { | |
7366 curwin->w_cursor.lnum = our_paren_pos.lnum; | |
7367 curwin->w_cursor.col = col; | |
2762 | 7368 if (find_match_paren(ind_maxparen, ind_maxcomment) != NULL) |
7 | 7369 amount += ind_unclosed2; |
7370 else | |
3454 | 7371 { |
7372 if (is_if_for_while) | |
7373 amount += ind_if_for_while; | |
7374 else | |
7375 amount += ind_unclosed; | |
7376 } | |
7 | 7377 } |
7378 /* | |
7379 * For a line starting with ')' use the minimum of the two | |
7380 * positions, to avoid giving it more indent than the previous | |
7381 * lines: | |
7382 * func_long_name( if (x | |
7383 * arg && yy | |
7384 * ) ^ not here ) ^ not here | |
7385 */ | |
7386 if (cur_amount < amount) | |
7387 amount = cur_amount; | |
7388 } | |
7389 } | |
7390 | |
7391 /* add extra indent for a comment */ | |
7392 if (cin_iscomment(theline)) | |
7393 amount += ind_comment; | |
7394 } | |
7395 | |
7396 /* | |
7397 * Are we at least inside braces, then? | |
7398 */ | |
7399 else | |
7400 { | |
7401 trypos = tryposBrace; | |
7402 | |
7403 ourscope = trypos->lnum; | |
7404 start = ml_get(ourscope); | |
7405 | |
7406 /* | |
7407 * Now figure out how indented the line is in general. | |
7408 * If the brace was at the start of the line, we use that; | |
7409 * otherwise, check out the indentation of the line as | |
7410 * a whole and then add the "imaginary indent" to that. | |
7411 */ | |
7412 look = skipwhite(start); | |
7413 if (*look == '{') | |
7414 { | |
7415 getvcol(curwin, trypos, &col, NULL, NULL); | |
7416 amount = col; | |
7417 if (*start == '{') | |
7418 start_brace = BRACE_IN_COL0; | |
7419 else | |
7420 start_brace = BRACE_AT_START; | |
7421 } | |
7422 else | |
7423 { | |
7424 /* | |
7425 * that opening brace might have been on a continuation | |
7426 * line. if so, find the start of the line. | |
7427 */ | |
7428 curwin->w_cursor.lnum = ourscope; | |
7429 | |
7430 /* | |
7431 * position the cursor over the rightmost paren, so that | |
7432 * matching it will take us back to the start of the line. | |
7433 */ | |
7434 lnum = ourscope; | |
7435 if (find_last_paren(start, '(', ')') | |
7436 && (trypos = find_match_paren(ind_maxparen, | |
7437 ind_maxcomment)) != NULL) | |
7438 lnum = trypos->lnum; | |
7439 | |
7440 /* | |
7441 * It could have been something like | |
7442 * case 1: if (asdf && | |
7443 * ldfd) { | |
7444 * } | |
7445 */ | |
2879 | 7446 if (ind_js || (ind_keep_case_label |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7447 && cin_iscase(skipwhite(ml_get_curline()), FALSE))) |
7 | 7448 amount = get_indent(); |
7449 else | |
7450 amount = skip_label(lnum, &l, ind_maxcomment); | |
7451 | |
7452 start_brace = BRACE_AT_END; | |
7453 } | |
7454 | |
7455 /* | |
7456 * if we're looking at a closing brace, that's where | |
7457 * we want to be. otherwise, add the amount of room | |
7458 * that an indent is supposed to be. | |
7459 */ | |
7460 if (theline[0] == '}') | |
7461 { | |
7462 /* | |
7463 * they may want closing braces to line up with something | |
7464 * other than the open brace. indulge them, if so. | |
7465 */ | |
7466 amount += ind_close_extra; | |
7467 } | |
7468 else | |
7469 { | |
7470 /* | |
7471 * If we're looking at an "else", try to find an "if" | |
7472 * to match it with. | |
7473 * If we're looking at a "while", try to find a "do" | |
7474 * to match it with. | |
7475 */ | |
7476 lookfor = LOOKFOR_INITIAL; | |
7477 if (cin_iselse(theline)) | |
7478 lookfor = LOOKFOR_IF; | |
7479 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen)) | |
7480 /* XXX */ | |
7481 lookfor = LOOKFOR_DO; | |
7482 if (lookfor != LOOKFOR_INITIAL) | |
7483 { | |
7484 curwin->w_cursor.lnum = cur_curpos.lnum; | |
7485 if (find_match(lookfor, ourscope, ind_maxparen, | |
7486 ind_maxcomment) == OK) | |
7487 { | |
7488 amount = get_indent(); /* XXX */ | |
7489 goto theend; | |
7490 } | |
7491 } | |
7492 | |
7493 /* | |
7494 * We get here if we are not on an "while-of-do" or "else" (or | |
7495 * failed to find a matching "if"). | |
7496 * Search backwards for something to line up with. | |
7497 * First set amount for when we don't find anything. | |
7498 */ | |
7499 | |
7500 /* | |
7501 * if the '{' is _really_ at the left margin, use the imaginary | |
7502 * location of a left-margin brace. Otherwise, correct the | |
7503 * location for ind_open_extra. | |
7504 */ | |
7505 | |
7506 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ | |
7507 { | |
7508 amount = ind_open_left_imag; | |
2857 | 7509 lookfor_cpp_namespace = TRUE; |
7510 } | |
7511 else if (start_brace == BRACE_AT_START && | |
7512 lookfor_cpp_namespace) /* '{' is at start */ | |
7513 { | |
7514 | |
7515 lookfor_cpp_namespace = TRUE; | |
7 | 7516 } |
7517 else | |
7518 { | |
7519 if (start_brace == BRACE_AT_END) /* '{' is at end of line */ | |
2857 | 7520 { |
7 | 7521 amount += ind_open_imag; |
2857 | 7522 |
7523 l = skipwhite(ml_get_curline()); | |
7524 if (cin_is_cpp_namespace(l)) | |
7525 amount += ind_cpp_namespace; | |
7526 } | |
7 | 7527 else |
7528 { | |
7529 /* Compensate for adding ind_open_extra later. */ | |
7530 amount -= ind_open_extra; | |
7531 if (amount < 0) | |
7532 amount = 0; | |
7533 } | |
7534 } | |
7535 | |
7536 lookfor_break = FALSE; | |
7537 | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7538 if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
7 | 7539 { |
7540 lookfor = LOOKFOR_CASE; /* find a previous switch() label */ | |
7541 amount += ind_case; | |
7542 } | |
7543 else if (cin_isscopedecl(theline)) /* private:, ... */ | |
7544 { | |
7545 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ | |
7546 amount += ind_scopedecl; | |
7547 } | |
7548 else | |
7549 { | |
7550 if (ind_case_break && cin_isbreak(theline)) /* break; ... */ | |
7551 lookfor_break = TRUE; | |
7552 | |
7553 lookfor = LOOKFOR_INITIAL; | |
7554 amount += ind_level; /* ind_level from start of block */ | |
7555 } | |
7556 scope_amount = amount; | |
7557 whilelevel = 0; | |
7558 | |
7559 /* | |
7560 * Search backwards. If we find something we recognize, line up | |
7561 * with that. | |
7562 * | |
7563 * if we're looking at an open brace, indent | |
7564 * the usual amount relative to the conditional | |
7565 * that opens the block. | |
7566 */ | |
7567 curwin->w_cursor = cur_curpos; | |
7568 for (;;) | |
7569 { | |
7570 curwin->w_cursor.lnum--; | |
7571 curwin->w_cursor.col = 0; | |
7572 | |
7573 /* | |
7574 * If we went all the way back to the start of our scope, line | |
7575 * up with it. | |
7576 */ | |
7577 if (curwin->w_cursor.lnum <= ourscope) | |
7578 { | |
7579 /* we reached end of scope: | |
7580 * if looking for a enum or structure initialization | |
7581 * go further back: | |
7582 * if it is an initializer (enum xxx or xxx =), then | |
7583 * don't add ind_continuation, otherwise it is a variable | |
7584 * declaration: | |
7585 * int x, | |
7586 * here; <-- add ind_continuation | |
7587 */ | |
7588 if (lookfor == LOOKFOR_ENUM_OR_INIT) | |
7589 { | |
7590 if (curwin->w_cursor.lnum == 0 | |
7591 || curwin->w_cursor.lnum | |
7592 < ourscope - ind_maxparen) | |
7593 { | |
7594 /* nothing found (abuse ind_maxparen as limit) | |
7595 * assume terminated line (i.e. a variable | |
7596 * initialization) */ | |
7597 if (cont_amount > 0) | |
7598 amount = cont_amount; | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7599 else if (!ind_js) |
7 | 7600 amount += ind_continuation; |
7601 break; | |
7602 } | |
7603 | |
7604 l = ml_get_curline(); | |
7605 | |
7606 /* | |
7607 * If we're in a comment now, skip to the start of the | |
7608 * comment. | |
7609 */ | |
7610 trypos = find_start_comment(ind_maxcomment); | |
7611 if (trypos != NULL) | |
7612 { | |
7613 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7614 curwin->w_cursor.col = 0; |
7 | 7615 continue; |
7616 } | |
7617 | |
7618 /* | |
7619 * Skip preprocessor directives and blank lines. | |
7620 */ | |
7621 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) | |
7622 continue; | |
7623 | |
7624 if (cin_nocode(l)) | |
7625 continue; | |
7626 | |
7627 terminated = cin_isterminated(l, FALSE, TRUE); | |
7628 | |
7629 /* | |
7630 * If we are at top level and the line looks like a | |
7631 * function declaration, we are done | |
7632 * (it's a variable declaration). | |
7633 */ | |
7634 if (start_brace != BRACE_IN_COL0 | |
3220 | 7635 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, |
7636 0, ind_maxparen, ind_maxcomment)) | |
7 | 7637 { |
7638 /* if the line is terminated with another ',' | |
7639 * it is a continued variable initialization. | |
7640 * don't add extra indent. | |
7641 * TODO: does not work, if a function | |
7642 * declaration is split over multiple lines: | |
7643 * cin_isfuncdecl returns FALSE then. | |
7644 */ | |
7645 if (terminated == ',') | |
7646 break; | |
7647 | |
7648 /* if it es a enum declaration or an assignment, | |
7649 * we are done. | |
7650 */ | |
7651 if (terminated != ';' && cin_isinit()) | |
7652 break; | |
7653 | |
7654 /* nothing useful found */ | |
7655 if (terminated == 0 || terminated == '{') | |
7656 continue; | |
7657 } | |
7658 | |
7659 if (terminated != ';') | |
7660 { | |
7661 /* Skip parens and braces. Position the cursor | |
7662 * over the rightmost paren, so that matching it | |
7663 * will take us back to the start of the line. | |
7664 */ /* XXX */ | |
7665 trypos = NULL; | |
7666 if (find_last_paren(l, '(', ')')) | |
7667 trypos = find_match_paren(ind_maxparen, | |
7668 ind_maxcomment); | |
7669 | |
7670 if (trypos == NULL && find_last_paren(l, '{', '}')) | |
7671 trypos = find_start_brace(ind_maxcomment); | |
7672 | |
7673 if (trypos != NULL) | |
7674 { | |
7675 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7676 curwin->w_cursor.col = 0; |
7 | 7677 continue; |
7678 } | |
7679 } | |
7680 | |
7681 /* it's a variable declaration, add indentation | |
7682 * like in | |
7683 * int a, | |
7684 * b; | |
7685 */ | |
7686 if (cont_amount > 0) | |
7687 amount = cont_amount; | |
7688 else | |
7689 amount += ind_continuation; | |
7690 } | |
7691 else if (lookfor == LOOKFOR_UNTERM) | |
7692 { | |
7693 if (cont_amount > 0) | |
7694 amount = cont_amount; | |
7695 else | |
7696 amount += ind_continuation; | |
7697 } | |
3123 | 7698 else |
2857 | 7699 { |
3123 | 7700 if (lookfor != LOOKFOR_TERM |
7701 && lookfor != LOOKFOR_CPP_BASECLASS) | |
2857 | 7702 { |
3123 | 7703 amount = scope_amount; |
7704 if (theline[0] == '{') | |
7705 { | |
7706 amount += ind_open_extra; | |
7707 added_to_amount = ind_open_extra; | |
7708 } | |
2857 | 7709 } |
7710 | |
3123 | 7711 if (lookfor_cpp_namespace) |
2857 | 7712 { |
3123 | 7713 /* |
7714 * Looking for C++ namespace, need to look further | |
7715 * back. | |
7716 */ | |
7717 if (curwin->w_cursor.lnum == ourscope) | |
7718 continue; | |
7719 | |
7720 if (curwin->w_cursor.lnum == 0 | |
7721 || curwin->w_cursor.lnum | |
7722 < ourscope - FIND_NAMESPACE_LIM) | |
7723 break; | |
7724 | |
7725 l = ml_get_curline(); | |
7726 | |
7727 /* If we're in a comment now, skip to the start of | |
7728 * the comment. */ | |
7729 trypos = find_start_comment(ind_maxcomment); | |
7730 if (trypos != NULL) | |
7731 { | |
7732 curwin->w_cursor.lnum = trypos->lnum + 1; | |
7733 curwin->w_cursor.col = 0; | |
7734 continue; | |
7735 } | |
7736 | |
7737 /* Skip preprocessor directives and blank lines. */ | |
7738 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) | |
7739 continue; | |
7740 | |
7741 /* Finally the actual check for "namespace". */ | |
7742 if (cin_is_cpp_namespace(l)) | |
7743 { | |
7744 amount += ind_cpp_namespace - added_to_amount; | |
7745 break; | |
7746 } | |
7747 | |
7748 if (cin_nocode(l)) | |
7749 continue; | |
2857 | 7750 } |
7 | 7751 } |
7752 break; | |
7753 } | |
7754 | |
7755 /* | |
7756 * If we're in a comment now, skip to the start of the comment. | |
7757 */ /* XXX */ | |
7758 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) | |
7759 { | |
7760 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7761 curwin->w_cursor.col = 0; |
7 | 7762 continue; |
7763 } | |
7764 | |
7765 l = ml_get_curline(); | |
7766 | |
7767 /* | |
7768 * If this is a switch() label, may line up relative to that. | |
827 | 7769 * If this is a C++ scope declaration, do the same. |
7 | 7770 */ |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7771 iscase = cin_iscase(l, FALSE); |
7 | 7772 if (iscase || cin_isscopedecl(l)) |
7773 { | |
7774 /* we are only looking for cpp base class | |
7775 * declaration/initialization any longer */ | |
7776 if (lookfor == LOOKFOR_CPP_BASECLASS) | |
7777 break; | |
7778 | |
7779 /* When looking for a "do" we are not interested in | |
7780 * labels. */ | |
7781 if (whilelevel > 0) | |
7782 continue; | |
7783 | |
7784 /* | |
7785 * case xx: | |
7786 * c = 99 + <- this indent plus continuation | |
7787 *-> here; | |
7788 */ | |
7789 if (lookfor == LOOKFOR_UNTERM | |
7790 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
7791 { | |
7792 if (cont_amount > 0) | |
7793 amount = cont_amount; | |
7794 else | |
7795 amount += ind_continuation; | |
7796 break; | |
7797 } | |
7798 | |
7799 /* | |
7800 * case xx: <- line up with this case | |
7801 * x = 333; | |
7802 * case yy: | |
7803 */ | |
7804 if ( (iscase && lookfor == LOOKFOR_CASE) | |
7805 || (iscase && lookfor_break) | |
7806 || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) | |
7807 { | |
7808 /* | |
7809 * Check that this case label is not for another | |
7810 * switch() | |
7811 */ /* XXX */ | |
7812 if ((trypos = find_start_brace(ind_maxcomment)) == | |
7813 NULL || trypos->lnum == ourscope) | |
7814 { | |
7815 amount = get_indent(); /* XXX */ | |
7816 break; | |
7817 } | |
7818 continue; | |
7819 } | |
7820 | |
7821 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ | |
7822 | |
7823 /* | |
7824 * case xx: if (cond) <- line up with this if | |
7825 * y = y + 1; | |
7826 * -> s = 99; | |
7827 * | |
7828 * case xx: | |
7829 * if (cond) <- line up with this line | |
7830 * y = y + 1; | |
7831 * -> s = 99; | |
7832 */ | |
7833 if (lookfor == LOOKFOR_TERM) | |
7834 { | |
7835 if (n) | |
7836 amount = n; | |
7837 | |
7838 if (!lookfor_break) | |
7839 break; | |
7840 } | |
7841 | |
7842 /* | |
7843 * case xx: x = x + 1; <- line up with this x | |
7844 * -> y = y + 1; | |
7845 * | |
7846 * case xx: if (cond) <- line up with this if | |
7847 * -> y = y + 1; | |
7848 */ | |
7849 if (n) | |
7850 { | |
7851 amount = n; | |
7852 l = after_label(ml_get_curline()); | |
7853 if (l != NULL && cin_is_cinword(l)) | |
829 | 7854 { |
7855 if (theline[0] == '{') | |
7856 amount += ind_open_extra; | |
7857 else | |
7858 amount += ind_level + ind_no_brace; | |
7859 } | |
7 | 7860 break; |
7861 } | |
7862 | |
7863 /* | |
7864 * Try to get the indent of a statement before the switch | |
7865 * label. If nothing is found, line up relative to the | |
7866 * switch label. | |
7867 * break; <- may line up with this line | |
7868 * case xx: | |
7869 * -> y = 1; | |
7870 */ | |
7871 scope_amount = get_indent() + (iscase /* XXX */ | |
7872 ? ind_case_code : ind_scopedecl_code); | |
7873 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY; | |
7874 continue; | |
7875 } | |
7876 | |
7877 /* | |
7878 * Looking for a switch() label or C++ scope declaration, | |
7879 * ignore other lines, skip {}-blocks. | |
7880 */ | |
7881 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) | |
7882 { | |
7883 if (find_last_paren(l, '{', '}') && (trypos = | |
7884 find_start_brace(ind_maxcomment)) != NULL) | |
1550 | 7885 { |
7 | 7886 curwin->w_cursor.lnum = trypos->lnum + 1; |
1550 | 7887 curwin->w_cursor.col = 0; |
7888 } | |
7 | 7889 continue; |
7890 } | |
7891 | |
7892 /* | |
7893 * Ignore jump labels with nothing after them. | |
7894 */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7895 if (!ind_js && cin_islabel(ind_maxcomment)) |
7 | 7896 { |
7897 l = after_label(ml_get_curline()); | |
7898 if (l == NULL || cin_nocode(l)) | |
7899 continue; | |
7900 } | |
7901 | |
7902 /* | |
7903 * Ignore #defines, #if, etc. | |
7904 * Ignore comment and empty lines. | |
7905 * (need to get the line again, cin_islabel() may have | |
7906 * unlocked it) | |
7907 */ | |
7908 l = ml_get_curline(); | |
7909 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum) | |
7910 || cin_nocode(l)) | |
7911 continue; | |
7912 | |
7913 /* | |
7914 * Are we at the start of a cpp base class declaration or | |
7915 * constructor initialization? | |
7916 */ /* XXX */ | |
827 | 7917 n = FALSE; |
7918 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0) | |
7919 { | |
1336 | 7920 n = cin_is_cpp_baseclass(&col); |
827 | 7921 l = ml_get_curline(); |
7922 } | |
7923 if (n) | |
7 | 7924 { |
7925 if (lookfor == LOOKFOR_UNTERM) | |
7926 { | |
7927 if (cont_amount > 0) | |
7928 amount = cont_amount; | |
7929 else | |
7930 amount += ind_continuation; | |
7931 } | |
828 | 7932 else if (theline[0] == '{') |
7 | 7933 { |
828 | 7934 /* Need to find start of the declaration. */ |
7935 lookfor = LOOKFOR_UNTERM; | |
7936 ind_continuation = 0; | |
7937 continue; | |
7 | 7938 } |
7939 else | |
828 | 7940 /* XXX */ |
7941 amount = get_baseclass_amount(col, ind_maxparen, | |
7942 ind_maxcomment, ind_cpp_baseclass); | |
7 | 7943 break; |
7944 } | |
7945 else if (lookfor == LOOKFOR_CPP_BASECLASS) | |
7946 { | |
7947 /* only look, whether there is a cpp base class | |
827 | 7948 * declaration or initialization before the opening brace. |
7949 */ | |
7 | 7950 if (cin_isterminated(l, TRUE, FALSE)) |
7951 break; | |
7952 else | |
7953 continue; | |
7954 } | |
7955 | |
7956 /* | |
7957 * What happens next depends on the line being terminated. | |
7958 * If terminated with a ',' only consider it terminating if | |
1224 | 7959 * there is another unterminated statement behind, eg: |
7 | 7960 * 123, |
7961 * sizeof | |
7962 * here | |
7963 * Otherwise check whether it is a enumeration or structure | |
7964 * initialisation (not indented) or a variable declaration | |
7965 * (indented). | |
7966 */ | |
7967 terminated = cin_isterminated(l, FALSE, TRUE); | |
7968 | |
7969 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM | |
7970 && terminated == ',')) | |
7971 { | |
7972 /* | |
7973 * if we're in the middle of a paren thing, | |
7974 * go back to the line that starts it so | |
7975 * we can get the right prevailing indent | |
7976 * if ( foo && | |
7977 * bar ) | |
7978 */ | |
7979 /* | |
7980 * position the cursor over the rightmost paren, so that | |
7981 * matching it will take us back to the start of the line. | |
7982 */ | |
7983 (void)find_last_paren(l, '(', ')'); | |
7984 trypos = find_match_paren( | |
7985 corr_ind_maxparen(ind_maxparen, &cur_curpos), | |
7986 ind_maxcomment); | |
7987 | |
7988 /* | |
7989 * If we are looking for ',', we also look for matching | |
7990 * braces. | |
7991 */ | |
828 | 7992 if (trypos == NULL && terminated == ',' |
7993 && find_last_paren(l, '{', '}')) | |
7 | 7994 trypos = find_start_brace(ind_maxcomment); |
7995 | |
7996 if (trypos != NULL) | |
7997 { | |
7998 /* | |
7999 * Check if we are on a case label now. This is | |
8000 * handled above. | |
8001 * case xx: if ( asdf && | |
8002 * asdf) | |
8003 */ | |
1550 | 8004 curwin->w_cursor = *trypos; |
7 | 8005 l = ml_get_curline(); |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
8006 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
7 | 8007 { |
8008 ++curwin->w_cursor.lnum; | |
1550 | 8009 curwin->w_cursor.col = 0; |
7 | 8010 continue; |
8011 } | |
8012 } | |
8013 | |
8014 /* | |
8015 * Skip over continuation lines to find the one to get the | |
8016 * indent from | |
8017 * char *usethis = "bla\ | |
8018 * bla", | |
8019 * here; | |
8020 */ | |
8021 if (terminated == ',') | |
8022 { | |
8023 while (curwin->w_cursor.lnum > 1) | |
8024 { | |
8025 l = ml_get(curwin->w_cursor.lnum - 1); | |
8026 if (*l == NUL || l[STRLEN(l) - 1] != '\\') | |
8027 break; | |
8028 --curwin->w_cursor.lnum; | |
1550 | 8029 curwin->w_cursor.col = 0; |
7 | 8030 } |
8031 } | |
8032 | |
8033 /* | |
8034 * Get indent and pointer to text for current line, | |
8035 * ignoring any jump label. XXX | |
8036 */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
8037 if (!ind_js) |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
8038 cur_amount = skip_label(curwin->w_cursor.lnum, |
7 | 8039 &l, ind_maxcomment); |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
8040 else |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
8041 cur_amount = get_indent(); |
7 | 8042 /* |
8043 * If this is just above the line we are indenting, and it | |
8044 * starts with a '{', line it up with this line. | |
8045 * while (not) | |
8046 * -> { | |
8047 * } | |
8048 */ | |
8049 if (terminated != ',' && lookfor != LOOKFOR_TERM | |
8050 && theline[0] == '{') | |
8051 { | |
8052 amount = cur_amount; | |
8053 /* | |
8054 * Only add ind_open_extra when the current line | |
8055 * doesn't start with a '{', which must have a match | |
8056 * in the same line (scope is the same). Probably: | |
8057 * { 1, 2 }, | |
8058 * -> { 3, 4 } | |
8059 */ | |
8060 if (*skipwhite(l) != '{') | |
8061 amount += ind_open_extra; | |
8062 | |
8063 if (ind_cpp_baseclass) | |
8064 { | |
8065 /* have to look back, whether it is a cpp base | |
8066 * class declaration or initialization */ | |
8067 lookfor = LOOKFOR_CPP_BASECLASS; | |
8068 continue; | |
8069 } | |
8070 break; | |
8071 } | |
8072 | |
8073 /* | |
8074 * Check if we are after an "if", "while", etc. | |
8075 * Also allow " } else". | |
8076 */ | |
8077 if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) | |
8078 { | |
8079 /* | |
8080 * Found an unterminated line after an if (), line up | |
8081 * with the last one. | |
8082 * if (cond) | |
8083 * 100 + | |
8084 * -> here; | |
8085 */ | |
8086 if (lookfor == LOOKFOR_UNTERM | |
8087 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
8088 { | |
8089 if (cont_amount > 0) | |
8090 amount = cont_amount; | |
8091 else | |
8092 amount += ind_continuation; | |
8093 break; | |
8094 } | |
8095 | |
8096 /* | |
8097 * If this is just above the line we are indenting, we | |
8098 * are finished. | |
8099 * while (not) | |
8100 * -> here; | |
8101 * Otherwise this indent can be used when the line | |
8102 * before this is terminated. | |
8103 * yyy; | |
8104 * if (stat) | |
8105 * while (not) | |
8106 * xxx; | |
8107 * -> here; | |
8108 */ | |
8109 amount = cur_amount; | |
8110 if (theline[0] == '{') | |
8111 amount += ind_open_extra; | |
8112 if (lookfor != LOOKFOR_TERM) | |
8113 { | |
8114 amount += ind_level + ind_no_brace; | |
8115 break; | |
8116 } | |
8117 | |
8118 /* | |
8119 * Special trick: when expecting the while () after a | |
8120 * do, line up with the while() | |
8121 * do | |
8122 * x = 1; | |
8123 * -> here | |
8124 */ | |
8125 l = skipwhite(ml_get_curline()); | |
8126 if (cin_isdo(l)) | |
8127 { | |
8128 if (whilelevel == 0) | |
8129 break; | |
8130 --whilelevel; | |
8131 } | |
8132 | |
8133 /* | |
8134 * When searching for a terminated line, don't use the | |
2855 | 8135 * one between the "if" and the matching "else". |
7 | 8136 * Need to use the scope of this "else". XXX |
8137 * If whilelevel != 0 continue looking for a "do {". | |
8138 */ | |
2855 | 8139 if (cin_iselse(l) && whilelevel == 0) |
8140 { | |
8141 /* If we're looking at "} else", let's make sure we | |
8142 * find the opening brace of the enclosing scope, | |
8143 * not the one from "if () {". */ | |
8144 if (*l == '}') | |
8145 curwin->w_cursor.col = | |
2861 | 8146 (colnr_T)(l - ml_get_curline()) + 1; |
2855 | 8147 |
8148 if ((trypos = find_start_brace(ind_maxcomment)) | |
8149 == NULL | |
7 | 8150 || find_match(LOOKFOR_IF, trypos->lnum, |
2855 | 8151 ind_maxparen, ind_maxcomment) == FAIL) |
8152 break; | |
8153 } | |
7 | 8154 } |
8155 | |
8156 /* | |
8157 * If we're below an unterminated line that is not an | |
8158 * "if" or something, we may line up with this line or | |
1224 | 8159 * add something for a continuation line, depending on |
7 | 8160 * the line before this one. |
8161 */ | |
8162 else | |
8163 { | |
8164 /* | |
8165 * Found two unterminated lines on a row, line up with | |
8166 * the last one. | |
8167 * c = 99 + | |
8168 * 100 + | |
8169 * -> here; | |
8170 */ | |
8171 if (lookfor == LOOKFOR_UNTERM) | |
8172 { | |
8173 /* When line ends in a comma add extra indent */ | |
8174 if (terminated == ',') | |
8175 amount += ind_continuation; | |
8176 break; | |
8177 } | |
8178 | |
8179 if (lookfor == LOOKFOR_ENUM_OR_INIT) | |
8180 { | |
8181 /* Found two lines ending in ',', lineup with the | |
8182 * lowest one, but check for cpp base class | |
8183 * declaration/initialization, if it is an | |
8184 * opening brace or we are looking just for | |
8185 * enumerations/initializations. */ | |
8186 if (terminated == ',') | |
8187 { | |
8188 if (ind_cpp_baseclass == 0) | |
8189 break; | |
8190 | |
8191 lookfor = LOOKFOR_CPP_BASECLASS; | |
8192 continue; | |
8193 } | |
8194 | |
8195 /* Ignore unterminated lines in between, but | |
8196 * reduce indent. */ | |
8197 if (amount > cur_amount) | |
8198 amount = cur_amount; | |
8199 } | |
8200 else | |
8201 { | |
8202 /* | |
8203 * Found first unterminated line on a row, may | |
8204 * line up with this line, remember its indent | |
8205 * 100 + | |
8206 * -> here; | |
8207 */ | |
8208 amount = cur_amount; | |
8209 | |
8210 /* | |
8211 * If previous line ends in ',', check whether we | |
8212 * are in an initialization or enum | |
8213 * struct xxx = | |
8214 * { | |
8215 * sizeof a, | |
8216 * 124 }; | |
8217 * or a normal possible continuation line. | |
8218 * but only, of no other statement has been found | |
8219 * yet. | |
8220 */ | |
8221 if (lookfor == LOOKFOR_INITIAL && terminated == ',') | |
8222 { | |
8223 lookfor = LOOKFOR_ENUM_OR_INIT; | |
8224 cont_amount = cin_first_id_amount(); | |
8225 } | |
8226 else | |
8227 { | |
8228 if (lookfor == LOOKFOR_INITIAL | |
8229 && *l != NUL | |
8230 && l[STRLEN(l) - 1] == '\\') | |
8231 /* XXX */ | |
8232 cont_amount = cin_get_equal_amount( | |
8233 curwin->w_cursor.lnum); | |
8234 if (lookfor != LOOKFOR_TERM) | |
8235 lookfor = LOOKFOR_UNTERM; | |
8236 } | |
8237 } | |
8238 } | |
8239 } | |
8240 | |
8241 /* | |
8242 * Check if we are after a while (cond); | |
8243 * If so: Ignore until the matching "do". | |
8244 */ | |
8245 /* XXX */ | |
829 | 8246 else if (cin_iswhileofdo_end(terminated, ind_maxparen, |
8247 ind_maxcomment)) | |
7 | 8248 { |
8249 /* | |
8250 * Found an unterminated line after a while ();, line up | |
8251 * with the last one. | |
8252 * while (cond); | |
8253 * 100 + <- line up with this one | |
8254 * -> here; | |
8255 */ | |
8256 if (lookfor == LOOKFOR_UNTERM | |
8257 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
8258 { | |
8259 if (cont_amount > 0) | |
8260 amount = cont_amount; | |
8261 else | |
8262 amount += ind_continuation; | |
8263 break; | |
8264 } | |
8265 | |
8266 if (whilelevel == 0) | |
8267 { | |
8268 lookfor = LOOKFOR_TERM; | |
8269 amount = get_indent(); /* XXX */ | |
8270 if (theline[0] == '{') | |
8271 amount += ind_open_extra; | |
8272 } | |
8273 ++whilelevel; | |
8274 } | |
8275 | |
8276 /* | |
8277 * We are after a "normal" statement. | |
8278 * If we had another statement we can stop now and use the | |
8279 * indent of that other statement. | |
8280 * Otherwise the indent of the current statement may be used, | |
8281 * search backwards for the next "normal" statement. | |
8282 */ | |
8283 else | |
8284 { | |
8285 /* | |
8286 * Skip single break line, if before a switch label. It | |
8287 * may be lined up with the case label. | |
8288 */ | |
8289 if (lookfor == LOOKFOR_NOBREAK | |
8290 && cin_isbreak(skipwhite(ml_get_curline()))) | |
8291 { | |
8292 lookfor = LOOKFOR_ANY; | |
8293 continue; | |
8294 } | |
8295 | |
8296 /* | |
8297 * Handle "do {" line. | |
8298 */ | |
8299 if (whilelevel > 0) | |
8300 { | |
8301 l = cin_skipcomment(ml_get_curline()); | |
8302 if (cin_isdo(l)) | |
8303 { | |
8304 amount = get_indent(); /* XXX */ | |
8305 --whilelevel; | |
8306 continue; | |
8307 } | |
8308 } | |
8309 | |
8310 /* | |
8311 * Found a terminated line above an unterminated line. Add | |
8312 * the amount for a continuation line. | |
8313 * x = 1; | |
8314 * y = foo + | |
8315 * -> here; | |
8316 * or | |
8317 * int x = 1; | |
8318 * int foo, | |
8319 * -> here; | |
8320 */ | |
8321 if (lookfor == LOOKFOR_UNTERM | |
8322 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
8323 { | |
8324 if (cont_amount > 0) | |
8325 amount = cont_amount; | |
8326 else | |
8327 amount += ind_continuation; | |
8328 break; | |
8329 } | |
8330 | |
8331 /* | |
8332 * Found a terminated line above a terminated line or "if" | |
8333 * etc. line. Use the amount of the line below us. | |
8334 * x = 1; x = 1; | |
8335 * if (asdf) y = 2; | |
8336 * while (asdf) ->here; | |
8337 * here; | |
8338 * ->foo; | |
8339 */ | |
8340 if (lookfor == LOOKFOR_TERM) | |
8341 { | |
8342 if (!lookfor_break && whilelevel == 0) | |
8343 break; | |
8344 } | |
8345 | |
8346 /* | |
8347 * First line above the one we're indenting is terminated. | |
8348 * To know what needs to be done look further backward for | |
8349 * a terminated line. | |
8350 */ | |
8351 else | |
8352 { | |
8353 /* | |
8354 * position the cursor over the rightmost paren, so | |
8355 * that matching it will take us back to the start of | |
8356 * the line. Helps for: | |
8357 * func(asdr, | |
8358 * asdfasdf); | |
8359 * here; | |
8360 */ | |
8361 term_again: | |
8362 l = ml_get_curline(); | |
8363 if (find_last_paren(l, '(', ')') | |
8364 && (trypos = find_match_paren(ind_maxparen, | |
8365 ind_maxcomment)) != NULL) | |
8366 { | |
8367 /* | |
8368 * Check if we are on a case label now. This is | |
8369 * handled above. | |
8370 * case xx: if ( asdf && | |
8371 * asdf) | |
8372 */ | |
1550 | 8373 curwin->w_cursor = *trypos; |
7 | 8374 l = ml_get_curline(); |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
8375 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
7 | 8376 { |
8377 ++curwin->w_cursor.lnum; | |
1550 | 8378 curwin->w_cursor.col = 0; |
7 | 8379 continue; |
8380 } | |
8381 } | |
8382 | |
8383 /* When aligning with the case statement, don't align | |
8384 * with a statement after it. | |
8385 * case 1: { <-- don't use this { position | |
8386 * stat; | |
8387 * } | |
8388 * case 2: | |
8389 * stat; | |
8390 * } | |
8391 */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
8392 iscase = (ind_keep_case_label && cin_iscase(l, FALSE)); |
7 | 8393 |
8394 /* | |
8395 * Get indent and pointer to text for current line, | |
8396 * ignoring any jump label. | |
8397 */ | |
8398 amount = skip_label(curwin->w_cursor.lnum, | |
8399 &l, ind_maxcomment); | |
8400 | |
8401 if (theline[0] == '{') | |
8402 amount += ind_open_extra; | |
8403 /* See remark above: "Only add ind_open_extra.." */ | |
827 | 8404 l = skipwhite(l); |
8405 if (*l == '{') | |
7 | 8406 amount -= ind_open_extra; |
8407 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; | |
8408 | |
8409 /* | |
827 | 8410 * When a terminated line starts with "else" skip to |
8411 * the matching "if": | |
8412 * else 3; | |
856 | 8413 * indent this; |
827 | 8414 * Need to use the scope of this "else". XXX |
8415 * If whilelevel != 0 continue looking for a "do {". | |
8416 */ | |
8417 if (lookfor == LOOKFOR_TERM | |
8418 && *l != '}' | |
8419 && cin_iselse(l) | |
8420 && whilelevel == 0) | |
8421 { | |
8422 if ((trypos = find_start_brace(ind_maxcomment)) | |
8423 == NULL | |
8424 || find_match(LOOKFOR_IF, trypos->lnum, | |
8425 ind_maxparen, ind_maxcomment) == FAIL) | |
8426 break; | |
8427 continue; | |
8428 } | |
8429 | |
8430 /* | |
7 | 8431 * If we're at the end of a block, skip to the start of |
8432 * that block. | |
8433 */ | |
3172 | 8434 l = ml_get_curline(); |
2953 | 8435 if (find_last_paren(l, '{', '}') |
7 | 8436 && (trypos = find_start_brace(ind_maxcomment)) |
8437 != NULL) /* XXX */ | |
8438 { | |
1550 | 8439 curwin->w_cursor = *trypos; |
7 | 8440 /* if not "else {" check for terminated again */ |
8441 /* but skip block for "} else {" */ | |
8442 l = cin_skipcomment(ml_get_curline()); | |
8443 if (*l == '}' || !cin_iselse(l)) | |
8444 goto term_again; | |
8445 ++curwin->w_cursor.lnum; | |
1550 | 8446 curwin->w_cursor.col = 0; |
7 | 8447 } |
8448 } | |
8449 } | |
8450 } | |
8451 } | |
8452 } | |
8453 | |
8454 /* add extra indent for a comment */ | |
8455 if (cin_iscomment(theline)) | |
8456 amount += ind_comment; | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
8457 |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
8458 /* subtract extra left-shift for jump labels */ |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
8459 if (ind_jump_label > 0 && original_line_islabel) |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
8460 amount -= ind_jump_label; |
7 | 8461 } |
8462 | |
8463 /* | |
8464 * ok -- we're not inside any sort of structure at all! | |
8465 * | |
8466 * this means we're at the top level, and everything should | |
8467 * basically just match where the previous line is, except | |
8468 * for the lines immediately following a function declaration, | |
8469 * which are K&R-style parameters and need to be indented. | |
8470 */ | |
8471 else | |
8472 { | |
8473 /* | |
8474 * if our line starts with an open brace, forget about any | |
8475 * prevailing indent and make sure it looks like the start | |
8476 * of a function | |
8477 */ | |
8478 | |
8479 if (theline[0] == '{') | |
8480 { | |
8481 amount = ind_first_open; | |
8482 } | |
8483 | |
8484 /* | |
8485 * If the NEXT line is a function declaration, the current | |
8486 * line needs to be indented as a function type spec. | |
2094
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
8487 * Don't do this if the current line looks like a comment or if the |
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
8488 * current line is terminated, ie. ends in ';', or if the current line |
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
8489 * contains { or }: "void f() {\n if (1)" |
7 | 8490 */ |
8491 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count | |
8492 && !cin_nocode(theline) | |
2094
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
8493 && vim_strchr(theline, '{') == NULL |
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
8494 && vim_strchr(theline, '}') == NULL |
7 | 8495 && !cin_ends_in(theline, (char_u *)":", NULL) |
8496 && !cin_ends_in(theline, (char_u *)",", NULL) | |
3220 | 8497 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, |
8498 cur_curpos.lnum + 1, | |
8499 ind_maxparen, ind_maxcomment) | |
7 | 8500 && !cin_isterminated(theline, FALSE, TRUE)) |
8501 { | |
8502 amount = ind_func_type; | |
8503 } | |
8504 else | |
8505 { | |
8506 amount = 0; | |
8507 curwin->w_cursor = cur_curpos; | |
8508 | |
8509 /* search backwards until we find something we recognize */ | |
8510 | |
8511 while (curwin->w_cursor.lnum > 1) | |
8512 { | |
8513 curwin->w_cursor.lnum--; | |
8514 curwin->w_cursor.col = 0; | |
8515 | |
8516 l = ml_get_curline(); | |
8517 | |
8518 /* | |
8519 * If we're in a comment now, skip to the start of the comment. | |
8520 */ /* XXX */ | |
8521 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) | |
8522 { | |
8523 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 8524 curwin->w_cursor.col = 0; |
7 | 8525 continue; |
8526 } | |
8527 | |
8528 /* | |
827 | 8529 * Are we at the start of a cpp base class declaration or |
8530 * constructor initialization? | |
7 | 8531 */ /* XXX */ |
827 | 8532 n = FALSE; |
8533 if (ind_cpp_baseclass != 0 && theline[0] != '{') | |
8534 { | |
1336 | 8535 n = cin_is_cpp_baseclass(&col); |
827 | 8536 l = ml_get_curline(); |
8537 } | |
8538 if (n) | |
7 | 8539 { |
828 | 8540 /* XXX */ |
8541 amount = get_baseclass_amount(col, ind_maxparen, | |
8542 ind_maxcomment, ind_cpp_baseclass); | |
7 | 8543 break; |
8544 } | |
8545 | |
8546 /* | |
8547 * Skip preprocessor directives and blank lines. | |
8548 */ | |
8549 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) | |
8550 continue; | |
8551 | |
8552 if (cin_nocode(l)) | |
8553 continue; | |
8554 | |
8555 /* | |
8556 * If the previous line ends in ',', use one level of | |
8557 * indentation: | |
8558 * int foo, | |
8559 * bar; | |
8560 * do this before checking for '}' in case of eg. | |
8561 * enum foobar | |
8562 * { | |
8563 * ... | |
8564 * } foo, | |
8565 * bar; | |
8566 */ | |
8567 n = 0; | |
8568 if (cin_ends_in(l, (char_u *)",", NULL) | |
8569 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) | |
8570 { | |
8571 /* take us back to opening paren */ | |
8572 if (find_last_paren(l, '(', ')') | |
8573 && (trypos = find_match_paren(ind_maxparen, | |
8574 ind_maxcomment)) != NULL) | |
1550 | 8575 curwin->w_cursor = *trypos; |
7 | 8576 |
8577 /* For a line ending in ',' that is a continuation line go | |
8578 * back to the first line with a backslash: | |
8579 * char *foo = "bla\ | |
8580 * bla", | |
8581 * here; | |
8582 */ | |
8583 while (n == 0 && curwin->w_cursor.lnum > 1) | |
8584 { | |
8585 l = ml_get(curwin->w_cursor.lnum - 1); | |
8586 if (*l == NUL || l[STRLEN(l) - 1] != '\\') | |
8587 break; | |
8588 --curwin->w_cursor.lnum; | |
1550 | 8589 curwin->w_cursor.col = 0; |
7 | 8590 } |
8591 | |
8592 amount = get_indent(); /* XXX */ | |
8593 | |
8594 if (amount == 0) | |
8595 amount = cin_first_id_amount(); | |
8596 if (amount == 0) | |
8597 amount = ind_continuation; | |
8598 break; | |
8599 } | |
8600 | |
8601 /* | |
8602 * If the line looks like a function declaration, and we're | |
8603 * not in a comment, put it the left margin. | |
8604 */ | |
3220 | 8605 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0, |
8606 ind_maxparen, ind_maxcomment)) /* XXX */ | |
7 | 8607 break; |
8608 l = ml_get_curline(); | |
8609 | |
8610 /* | |
8611 * Finding the closing '}' of a previous function. Put | |
8612 * current line at the left margin. For when 'cino' has "fs". | |
8613 */ | |
8614 if (*skipwhite(l) == '}') | |
8615 break; | |
8616 | |
8617 /* (matching {) | |
8618 * If the previous line ends on '};' (maybe followed by | |
8619 * comments) align at column 0. For example: | |
8620 * char *string_array[] = { "foo", | |
8621 * / * x * / "b};ar" }; / * foobar * / | |
8622 */ | |
8623 if (cin_ends_in(l, (char_u *)"};", NULL)) | |
8624 break; | |
8625 | |
8626 /* | |
3186 | 8627 * Find a line only has a semicolon that belongs to a previous |
8628 * line ending in '}', e.g. before an #endif. Don't increase | |
8629 * indent then. | |
8630 */ | |
8631 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) | |
8632 { | |
8633 pos_T curpos_save = curwin->w_cursor; | |
8634 | |
8635 while (curwin->w_cursor.lnum > 1) | |
8636 { | |
8637 look = ml_get(--curwin->w_cursor.lnum); | |
8638 if (!(cin_nocode(look) || cin_ispreproc_cont( | |
8639 &look, &curwin->w_cursor.lnum))) | |
8640 break; | |
8641 } | |
8642 if (curwin->w_cursor.lnum > 0 | |
8643 && cin_ends_in(look, (char_u *)"}", NULL)) | |
8644 break; | |
8645 | |
8646 curwin->w_cursor = curpos_save; | |
8647 } | |
8648 | |
8649 /* | |
7 | 8650 * If the PREVIOUS line is a function declaration, the current |
8651 * line (and the ones that follow) needs to be indented as | |
8652 * parameters. | |
8653 */ | |
3220 | 8654 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0, |
8655 ind_maxparen, ind_maxcomment)) | |
7 | 8656 { |
8657 amount = ind_param; | |
8658 break; | |
8659 } | |
8660 | |
8661 /* | |
8662 * If the previous line ends in ';' and the line before the | |
8663 * previous line ends in ',' or '\', ident to column zero: | |
8664 * int foo, | |
8665 * bar; | |
8666 * indent_to_0 here; | |
8667 */ | |
828 | 8668 if (cin_ends_in(l, (char_u *)";", NULL)) |
7 | 8669 { |
8670 l = ml_get(curwin->w_cursor.lnum - 1); | |
8671 if (cin_ends_in(l, (char_u *)",", NULL) | |
8672 || (*l != NUL && l[STRLEN(l) - 1] == '\\')) | |
8673 break; | |
8674 l = ml_get_curline(); | |
8675 } | |
8676 | |
8677 /* | |
8678 * Doesn't look like anything interesting -- so just | |
8679 * use the indent of this line. | |
8680 * | |
8681 * Position the cursor over the rightmost paren, so that | |
8682 * matching it will take us back to the start of the line. | |
8683 */ | |
8684 find_last_paren(l, '(', ')'); | |
8685 | |
8686 if ((trypos = find_match_paren(ind_maxparen, | |
8687 ind_maxcomment)) != NULL) | |
1550 | 8688 curwin->w_cursor = *trypos; |
7 | 8689 amount = get_indent(); /* XXX */ |
8690 break; | |
8691 } | |
8692 | |
8693 /* add extra indent for a comment */ | |
8694 if (cin_iscomment(theline)) | |
8695 amount += ind_comment; | |
8696 | |
8697 /* add extra indent if the previous line ended in a backslash: | |
8698 * "asdfasdf\ | |
8699 * here"; | |
8700 * char *foo = "asdf\ | |
8701 * here"; | |
8702 */ | |
8703 if (cur_curpos.lnum > 1) | |
8704 { | |
8705 l = ml_get(cur_curpos.lnum - 1); | |
8706 if (*l != NUL && l[STRLEN(l) - 1] == '\\') | |
8707 { | |
8708 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); | |
8709 if (cur_amount > 0) | |
8710 amount = cur_amount; | |
8711 else if (cur_amount == 0) | |
8712 amount += ind_continuation; | |
8713 } | |
8714 } | |
8715 } | |
8716 } | |
8717 | |
8718 theend: | |
8719 /* put the cursor back where it belongs */ | |
8720 curwin->w_cursor = cur_curpos; | |
8721 | |
8722 vim_free(linecopy); | |
8723 | |
8724 if (amount < 0) | |
8725 return 0; | |
8726 return amount; | |
8727 } | |
8728 | |
8729 static int | |
8730 find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment) | |
8731 int lookfor; | |
8732 linenr_T ourscope; | |
8733 int ind_maxparen; | |
8734 int ind_maxcomment; | |
8735 { | |
8736 char_u *look; | |
8737 pos_T *theirscope; | |
8738 char_u *mightbeif; | |
8739 int elselevel; | |
8740 int whilelevel; | |
8741 | |
8742 if (lookfor == LOOKFOR_IF) | |
8743 { | |
8744 elselevel = 1; | |
8745 whilelevel = 0; | |
8746 } | |
8747 else | |
8748 { | |
8749 elselevel = 0; | |
8750 whilelevel = 1; | |
8751 } | |
8752 | |
8753 curwin->w_cursor.col = 0; | |
8754 | |
8755 while (curwin->w_cursor.lnum > ourscope + 1) | |
8756 { | |
8757 curwin->w_cursor.lnum--; | |
8758 curwin->w_cursor.col = 0; | |
8759 | |
8760 look = cin_skipcomment(ml_get_curline()); | |
8761 if (cin_iselse(look) | |
8762 || cin_isif(look) | |
8763 || cin_isdo(look) /* XXX */ | |
8764 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen)) | |
8765 { | |
8766 /* | |
8767 * if we've gone outside the braces entirely, | |
8768 * we must be out of scope... | |
8769 */ | |
8770 theirscope = find_start_brace(ind_maxcomment); /* XXX */ | |
8771 if (theirscope == NULL) | |
8772 break; | |
8773 | |
8774 /* | |
8775 * and if the brace enclosing this is further | |
8776 * back than the one enclosing the else, we're | |
8777 * out of luck too. | |
8778 */ | |
8779 if (theirscope->lnum < ourscope) | |
8780 break; | |
8781 | |
8782 /* | |
8783 * and if they're enclosed in a *deeper* brace, | |
8784 * then we can ignore it because it's in a | |
8785 * different scope... | |
8786 */ | |
8787 if (theirscope->lnum > ourscope) | |
8788 continue; | |
8789 | |
8790 /* | |
8791 * if it was an "else" (that's not an "else if") | |
8792 * then we need to go back to another if, so | |
8793 * increment elselevel | |
8794 */ | |
8795 look = cin_skipcomment(ml_get_curline()); | |
8796 if (cin_iselse(look)) | |
8797 { | |
8798 mightbeif = cin_skipcomment(look + 4); | |
8799 if (!cin_isif(mightbeif)) | |
8800 ++elselevel; | |
8801 continue; | |
8802 } | |
8803 | |
8804 /* | |
8805 * if it was a "while" then we need to go back to | |
8806 * another "do", so increment whilelevel. XXX | |
8807 */ | |
8808 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen)) | |
8809 { | |
8810 ++whilelevel; | |
8811 continue; | |
8812 } | |
8813 | |
8814 /* If it's an "if" decrement elselevel */ | |
8815 look = cin_skipcomment(ml_get_curline()); | |
8816 if (cin_isif(look)) | |
8817 { | |
8818 elselevel--; | |
8819 /* | |
8820 * When looking for an "if" ignore "while"s that | |
8821 * get in the way. | |
8822 */ | |
8823 if (elselevel == 0 && lookfor == LOOKFOR_IF) | |
8824 whilelevel = 0; | |
8825 } | |
8826 | |
8827 /* If it's a "do" decrement whilelevel */ | |
8828 if (cin_isdo(look)) | |
8829 whilelevel--; | |
8830 | |
8831 /* | |
8832 * if we've used up all the elses, then | |
8833 * this must be the if that we want! | |
8834 * match the indent level of that if. | |
8835 */ | |
8836 if (elselevel <= 0 && whilelevel <= 0) | |
8837 { | |
8838 return OK; | |
8839 } | |
8840 } | |
8841 } | |
8842 return FAIL; | |
8843 } | |
8844 | |
8845 # if defined(FEAT_EVAL) || defined(PROTO) | |
8846 /* | |
8847 * Get indent level from 'indentexpr'. | |
8848 */ | |
8849 int | |
8850 get_expr_indent() | |
8851 { | |
8852 int indent; | |
4117 | 8853 pos_T save_pos; |
8854 colnr_T save_curswant; | |
8855 int save_set_curswant; | |
7 | 8856 int save_State; |
681 | 8857 int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
8858 OPT_LOCAL); | |
7 | 8859 |
4117 | 8860 /* Save and restore cursor position and curswant, in case it was changed |
8861 * via :normal commands */ | |
8862 save_pos = curwin->w_cursor; | |
8863 save_curswant = curwin->w_curswant; | |
8864 save_set_curswant = curwin->w_set_curswant; | |
7 | 8865 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); |
634 | 8866 if (use_sandbox) |
8867 ++sandbox; | |
8868 ++textlock; | |
7 | 8869 indent = eval_to_number(curbuf->b_p_inde); |
634 | 8870 if (use_sandbox) |
8871 --sandbox; | |
8872 --textlock; | |
7 | 8873 |
8874 /* Restore the cursor position so that 'indentexpr' doesn't need to. | |
8875 * Pretend to be in Insert mode, allow cursor past end of line for "o" | |
8876 * command. */ | |
8877 save_State = State; | |
8878 State = INSERT; | |
4117 | 8879 curwin->w_cursor = save_pos; |
8880 curwin->w_curswant = save_curswant; | |
8881 curwin->w_set_curswant = save_set_curswant; | |
7 | 8882 check_cursor(); |
8883 State = save_State; | |
8884 | |
8885 /* If there is an error, just keep the current indent. */ | |
8886 if (indent < 0) | |
8887 indent = get_indent(); | |
8888 | |
8889 return indent; | |
8890 } | |
8891 # endif | |
8892 | |
8893 #endif /* FEAT_CINDENT */ | |
8894 | |
8895 #if defined(FEAT_LISP) || defined(PROTO) | |
8896 | |
8897 static int lisp_match __ARGS((char_u *p)); | |
8898 | |
8899 static int | |
8900 lisp_match(p) | |
8901 char_u *p; | |
8902 { | |
8903 char_u buf[LSIZE]; | |
8904 int len; | |
8905 char_u *word = p_lispwords; | |
8906 | |
8907 while (*word != NUL) | |
8908 { | |
8909 (void)copy_option_part(&word, buf, LSIZE, ","); | |
8910 len = (int)STRLEN(buf); | |
8911 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') | |
8912 return TRUE; | |
8913 } | |
8914 return FALSE; | |
8915 } | |
8916 | |
8917 /* | |
8918 * When 'p' is present in 'cpoptions, a Vi compatible method is used. | |
8919 * The incompatible newer method is quite a bit better at indenting | |
8920 * code in lisp-like languages than the traditional one; it's still | |
8921 * mostly heuristics however -- Dirk van Deun, dirk@rave.org | |
8922 * | |
8923 * TODO: | |
8924 * Findmatch() should be adapted for lisp, also to make showmatch | |
8925 * work correctly: now (v5.3) it seems all C/C++ oriented: | |
8926 * - it does not recognize the #\( and #\) notations as character literals | |
8927 * - it doesn't know about comments starting with a semicolon | |
8928 * - it incorrectly interprets '(' as a character literal | |
8929 * All this messes up get_lisp_indent in some rare cases. | |
14 | 8930 * Update from Sergey Khorev: |
8931 * I tried to fix the first two issues. | |
7 | 8932 */ |
8933 int | |
8934 get_lisp_indent() | |
8935 { | |
14 | 8936 pos_T *pos, realpos, paren; |
7 | 8937 int amount; |
8938 char_u *that; | |
8939 colnr_T col; | |
8940 colnr_T firsttry; | |
8941 int parencount, quotecount; | |
8942 int vi_lisp; | |
8943 | |
8944 /* Set vi_lisp to use the vi-compatible method */ | |
8945 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); | |
8946 | |
8947 realpos = curwin->w_cursor; | |
8948 curwin->w_cursor.col = 0; | |
8949 | |
14 | 8950 if ((pos = findmatch(NULL, '(')) == NULL) |
8951 pos = findmatch(NULL, '['); | |
8952 else | |
8953 { | |
8954 paren = *pos; | |
8955 pos = findmatch(NULL, '['); | |
8956 if (pos == NULL || ltp(pos, &paren)) | |
8957 pos = &paren; | |
8958 } | |
8959 if (pos != NULL) | |
7 | 8960 { |
8961 /* Extra trick: Take the indent of the first previous non-white | |
8962 * line that is at the same () level. */ | |
8963 amount = -1; | |
8964 parencount = 0; | |
8965 | |
8966 while (--curwin->w_cursor.lnum >= pos->lnum) | |
8967 { | |
8968 if (linewhite(curwin->w_cursor.lnum)) | |
8969 continue; | |
8970 for (that = ml_get_curline(); *that != NUL; ++that) | |
8971 { | |
8972 if (*that == ';') | |
8973 { | |
8974 while (*(that + 1) != NUL) | |
8975 ++that; | |
8976 continue; | |
8977 } | |
8978 if (*that == '\\') | |
8979 { | |
8980 if (*(that + 1) != NUL) | |
8981 ++that; | |
8982 continue; | |
8983 } | |
8984 if (*that == '"' && *(that + 1) != NUL) | |
8985 { | |
983 | 8986 while (*++that && *that != '"') |
8987 { | |
8988 /* skipping escaped characters in the string */ | |
8989 if (*that == '\\') | |
8990 { | |
8991 if (*++that == NUL) | |
8992 break; | |
8993 if (that[1] == NUL) | |
8994 { | |
8995 ++that; | |
8996 break; | |
8997 } | |
8998 } | |
8999 } | |
7 | 9000 } |
14 | 9001 if (*that == '(' || *that == '[') |
7 | 9002 ++parencount; |
14 | 9003 else if (*that == ')' || *that == ']') |
7 | 9004 --parencount; |
9005 } | |
9006 if (parencount == 0) | |
9007 { | |
9008 amount = get_indent(); | |
9009 break; | |
9010 } | |
9011 } | |
9012 | |
9013 if (amount == -1) | |
9014 { | |
9015 curwin->w_cursor.lnum = pos->lnum; | |
9016 curwin->w_cursor.col = pos->col; | |
9017 col = pos->col; | |
9018 | |
9019 that = ml_get_curline(); | |
9020 | |
9021 if (vi_lisp && get_indent() == 0) | |
9022 amount = 2; | |
9023 else | |
9024 { | |
9025 amount = 0; | |
9026 while (*that && col) | |
9027 { | |
9028 amount += lbr_chartabsize_adv(&that, (colnr_T)amount); | |
9029 col--; | |
9030 } | |
9031 | |
9032 /* | |
9033 * Some keywords require "body" indenting rules (the | |
9034 * non-standard-lisp ones are Scheme special forms): | |
9035 * | |
9036 * (let ((a 1)) instead (let ((a 1)) | |
9037 * (...)) of (...)) | |
9038 */ | |
9039 | |
14 | 9040 if (!vi_lisp && (*that == '(' || *that == '[') |
9041 && lisp_match(that + 1)) | |
7 | 9042 amount += 2; |
9043 else | |
9044 { | |
9045 that++; | |
9046 amount++; | |
9047 firsttry = amount; | |
9048 | |
9049 while (vim_iswhite(*that)) | |
9050 { | |
9051 amount += lbr_chartabsize(that, (colnr_T)amount); | |
9052 ++that; | |
9053 } | |
9054 | |
9055 if (*that && *that != ';') /* not a comment line */ | |
9056 { | |
1532 | 9057 /* test *that != '(' to accommodate first let/do |
7 | 9058 * argument if it is more than one line */ |
14 | 9059 if (!vi_lisp && *that != '(' && *that != '[') |
7 | 9060 firsttry++; |
9061 | |
9062 parencount = 0; | |
9063 quotecount = 0; | |
9064 | |
9065 if (vi_lisp | |
9066 || (*that != '"' | |
9067 && *that != '\'' | |
9068 && *that != '#' | |
9069 && (*that < '0' || *that > '9'))) | |
9070 { | |
9071 while (*that | |
9072 && (!vim_iswhite(*that) | |
9073 || quotecount | |
9074 || parencount) | |
14 | 9075 && (!((*that == '(' || *that == '[') |
7 | 9076 && !quotecount |
9077 && !parencount | |
9078 && vi_lisp))) | |
9079 { | |
9080 if (*that == '"') | |
9081 quotecount = !quotecount; | |
14 | 9082 if ((*that == '(' || *that == '[') |
9083 && !quotecount) | |
7 | 9084 ++parencount; |
14 | 9085 if ((*that == ')' || *that == ']') |
9086 && !quotecount) | |
7 | 9087 --parencount; |
9088 if (*that == '\\' && *(that+1) != NUL) | |
9089 amount += lbr_chartabsize_adv(&that, | |
9090 (colnr_T)amount); | |
9091 amount += lbr_chartabsize_adv(&that, | |
9092 (colnr_T)amount); | |
9093 } | |
9094 } | |
9095 while (vim_iswhite(*that)) | |
9096 { | |
9097 amount += lbr_chartabsize(that, (colnr_T)amount); | |
9098 that++; | |
9099 } | |
9100 if (!*that || *that == ';') | |
9101 amount = firsttry; | |
9102 } | |
9103 } | |
9104 } | |
9105 } | |
9106 } | |
9107 else | |
14 | 9108 amount = 0; /* no matching '(' or '[' found, use zero indent */ |
7 | 9109 |
9110 curwin->w_cursor = realpos; | |
9111 | |
9112 return amount; | |
9113 } | |
9114 #endif /* FEAT_LISP */ | |
9115 | |
9116 void | |
9117 prepare_to_exit() | |
9118 { | |
39 | 9119 #if defined(SIGHUP) && defined(SIG_IGN) |
9120 /* Ignore SIGHUP, because a dropped connection causes a read error, which | |
9121 * makes Vim exit and then handling SIGHUP causes various reentrance | |
9122 * problems. */ | |
36 | 9123 signal(SIGHUP, SIG_IGN); |
9124 #endif | |
9125 | |
7 | 9126 #ifdef FEAT_GUI |
9127 if (gui.in_use) | |
9128 { | |
9129 gui.dying = TRUE; | |
9130 out_trash(); /* trash any pending output */ | |
9131 } | |
9132 else | |
9133 #endif | |
9134 { | |
9135 windgoto((int)Rows - 1, 0); | |
9136 | |
9137 /* | |
9138 * Switch terminal mode back now, so messages end up on the "normal" | |
9139 * screen (if there are two screens). | |
9140 */ | |
9141 settmode(TMODE_COOK); | |
9142 #ifdef WIN3264 | |
9143 if (can_end_termcap_mode(FALSE) == TRUE) | |
9144 #endif | |
9145 stoptermcap(); | |
9146 out_flush(); | |
9147 } | |
9148 } | |
9149 | |
9150 /* | |
9151 * Preserve files and exit. | |
9152 * When called IObuff must contain a message. | |
9153 */ | |
9154 void | |
9155 preserve_exit() | |
9156 { | |
9157 buf_T *buf; | |
9158 | |
9159 prepare_to_exit(); | |
9160 | |
625 | 9161 /* Setting this will prevent free() calls. That avoids calling free() |
9162 * recursively when free() was invoked with a bad pointer. */ | |
9163 really_exiting = TRUE; | |
9164 | |
7 | 9165 out_str(IObuff); |
9166 screen_start(); /* don't know where cursor is now */ | |
9167 out_flush(); | |
9168 | |
9169 ml_close_notmod(); /* close all not-modified buffers */ | |
9170 | |
9171 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
9172 { | |
9173 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) | |
9174 { | |
9175 OUT_STR(_("Vim: preserving files...\n")); | |
9176 screen_start(); /* don't know where cursor is now */ | |
9177 out_flush(); | |
9178 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ | |
9179 break; | |
9180 } | |
9181 } | |
9182 | |
9183 ml_close_all(FALSE); /* close all memfiles, without deleting */ | |
9184 | |
9185 OUT_STR(_("Vim: Finished.\n")); | |
9186 | |
9187 getout(1); | |
9188 } | |
9189 | |
9190 /* | |
9191 * return TRUE if "fname" exists. | |
9192 */ | |
9193 int | |
9194 vim_fexists(fname) | |
9195 char_u *fname; | |
9196 { | |
9197 struct stat st; | |
9198 | |
9199 if (mch_stat((char *)fname, &st)) | |
9200 return FALSE; | |
9201 return TRUE; | |
9202 } | |
9203 | |
9204 /* | |
9205 * Check for CTRL-C pressed, but only once in a while. | |
9206 * Should be used instead of ui_breakcheck() for functions that check for | |
9207 * each line in the file. Calling ui_breakcheck() each time takes too much | |
9208 * time, because it can be a system call. | |
9209 */ | |
9210 | |
9211 #ifndef BREAKCHECK_SKIP | |
9212 # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ | |
9213 # define BREAKCHECK_SKIP 200 | |
9214 # else | |
9215 # define BREAKCHECK_SKIP 32 | |
9216 # endif | |
9217 #endif | |
9218 | |
9219 static int breakcheck_count = 0; | |
9220 | |
9221 void | |
9222 line_breakcheck() | |
9223 { | |
9224 if (++breakcheck_count >= BREAKCHECK_SKIP) | |
9225 { | |
9226 breakcheck_count = 0; | |
9227 ui_breakcheck(); | |
9228 } | |
9229 } | |
9230 | |
9231 /* | |
9232 * Like line_breakcheck() but check 10 times less often. | |
9233 */ | |
9234 void | |
9235 fast_breakcheck() | |
9236 { | |
9237 if (++breakcheck_count >= BREAKCHECK_SKIP * 10) | |
9238 { | |
9239 breakcheck_count = 0; | |
9240 ui_breakcheck(); | |
9241 } | |
9242 } | |
9243 | |
9244 /* | |
2016 | 9245 * Invoke expand_wildcards() for one pattern. |
9246 * Expand items like "%:h" before the expansion. | |
9247 * Returns OK or FAIL. | |
9248 */ | |
9249 int | |
9250 expand_wildcards_eval(pat, num_file, file, flags) | |
9251 char_u **pat; /* pointer to input pattern */ | |
9252 int *num_file; /* resulting number of files */ | |
9253 char_u ***file; /* array of resulting files */ | |
9254 int flags; /* EW_DIR, etc. */ | |
9255 { | |
9256 int ret = FAIL; | |
9257 char_u *eval_pat = NULL; | |
9258 char_u *exp_pat = *pat; | |
9259 char_u *ignored_msg; | |
9260 int usedlen; | |
9261 | |
9262 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') | |
9263 { | |
9264 ++emsg_off; | |
9265 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, | |
9266 NULL, &ignored_msg, NULL); | |
9267 --emsg_off; | |
9268 if (eval_pat != NULL) | |
9269 exp_pat = concat_str(eval_pat, exp_pat + usedlen); | |
9270 } | |
9271 | |
9272 if (exp_pat != NULL) | |
9273 ret = expand_wildcards(1, &exp_pat, num_file, file, flags); | |
9274 | |
9275 if (eval_pat != NULL) | |
9276 { | |
9277 vim_free(exp_pat); | |
9278 vim_free(eval_pat); | |
9279 } | |
9280 | |
9281 return ret; | |
9282 } | |
9283 | |
9284 /* | |
7 | 9285 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
9286 * 'wildignore'. | |
2354
f4440cdd59ae
Fixed: crash with ":find " completion, using uninitialized count.
Bram Moolenaar <bram@vim.org>
parents:
2328
diff
changeset
|
9287 * Returns OK or FAIL. When FAIL then "num_file" won't be set. |
7 | 9288 */ |
9289 int | |
9290 expand_wildcards(num_pat, pat, num_file, file, flags) | |
9291 int num_pat; /* number of input patterns */ | |
9292 char_u **pat; /* array of input patterns */ | |
9293 int *num_file; /* resulting number of files */ | |
9294 char_u ***file; /* array of resulting files */ | |
9295 int flags; /* EW_DIR, etc. */ | |
9296 { | |
9297 int retval; | |
9298 int i, j; | |
9299 char_u *p; | |
9300 int non_suf_match; /* number without matching suffix */ | |
9301 | |
9302 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags); | |
9303 | |
9304 /* When keeping all matches, return here */ | |
2354
f4440cdd59ae
Fixed: crash with ":find " completion, using uninitialized count.
Bram Moolenaar <bram@vim.org>
parents:
2328
diff
changeset
|
9305 if ((flags & EW_KEEPALL) || retval == FAIL) |
7 | 9306 return retval; |
9307 | |
9308 #ifdef FEAT_WILDIGN | |
9309 /* | |
9310 * Remove names that match 'wildignore'. | |
9311 */ | |
9312 if (*p_wig) | |
9313 { | |
9314 char_u *ffname; | |
9315 | |
9316 /* check all files in (*file)[] */ | |
9317 for (i = 0; i < *num_file; ++i) | |
9318 { | |
9319 ffname = FullName_save((*file)[i], FALSE); | |
9320 if (ffname == NULL) /* out of memory */ | |
9321 break; | |
9322 # ifdef VMS | |
9323 vms_remove_version(ffname); | |
9324 # endif | |
9325 if (match_file_list(p_wig, (*file)[i], ffname)) | |
9326 { | |
9327 /* remove this matching file from the list */ | |
9328 vim_free((*file)[i]); | |
9329 for (j = i; j + 1 < *num_file; ++j) | |
9330 (*file)[j] = (*file)[j + 1]; | |
9331 --*num_file; | |
9332 --i; | |
9333 } | |
9334 vim_free(ffname); | |
9335 } | |
9336 } | |
9337 #endif | |
9338 | |
9339 /* | |
9340 * Move the names where 'suffixes' match to the end. | |
9341 */ | |
9342 if (*num_file > 1) | |
9343 { | |
9344 non_suf_match = 0; | |
9345 for (i = 0; i < *num_file; ++i) | |
9346 { | |
9347 if (!match_suffix((*file)[i])) | |
9348 { | |
9349 /* | |
9350 * Move the name without matching suffix to the front | |
9351 * of the list. | |
9352 */ | |
9353 p = (*file)[i]; | |
9354 for (j = i; j > non_suf_match; --j) | |
9355 (*file)[j] = (*file)[j - 1]; | |
9356 (*file)[non_suf_match++] = p; | |
9357 } | |
9358 } | |
9359 } | |
9360 | |
9361 return retval; | |
9362 } | |
9363 | |
9364 /* | |
9365 * Return TRUE if "fname" matches with an entry in 'suffixes'. | |
9366 */ | |
9367 int | |
9368 match_suffix(fname) | |
9369 char_u *fname; | |
9370 { | |
9371 int fnamelen, setsuflen; | |
9372 char_u *setsuf; | |
9373 #define MAXSUFLEN 30 /* maximum length of a file suffix */ | |
9374 char_u suf_buf[MAXSUFLEN]; | |
9375 | |
9376 fnamelen = (int)STRLEN(fname); | |
9377 setsuflen = 0; | |
9378 for (setsuf = p_su; *setsuf; ) | |
9379 { | |
9380 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); | |
1937 | 9381 if (setsuflen == 0) |
9382 { | |
9383 char_u *tail = gettail(fname); | |
9384 | |
9385 /* empty entry: match name without a '.' */ | |
9386 if (vim_strchr(tail, '.') == NULL) | |
9387 { | |
9388 setsuflen = 1; | |
9389 break; | |
9390 } | |
9391 } | |
9392 else | |
9393 { | |
9394 if (fnamelen >= setsuflen | |
9395 && fnamencmp(suf_buf, fname + fnamelen - setsuflen, | |
9396 (size_t)setsuflen) == 0) | |
9397 break; | |
9398 setsuflen = 0; | |
9399 } | |
7 | 9400 } |
9401 return (setsuflen != 0); | |
9402 } | |
9403 | |
9404 #if !defined(NO_EXPANDPATH) || defined(PROTO) | |
9405 | |
9406 # ifdef VIM_BACKTICK | |
9407 static int vim_backtick __ARGS((char_u *p)); | |
9408 static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags)); | |
9409 # endif | |
9410 | |
9411 # if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264) | |
9412 /* | |
9413 * File name expansion code for MS-DOS, Win16 and Win32. It's here because | |
9414 * it's shared between these systems. | |
9415 */ | |
9416 # if defined(DJGPP) || defined(PROTO) | |
9417 # define _cdecl /* DJGPP doesn't have this */ | |
9418 # else | |
9419 # ifdef __BORLANDC__ | |
9420 # define _cdecl _RTLENTRYF | |
9421 # endif | |
9422 # endif | |
9423 | |
9424 /* | |
9425 * comparison function for qsort in dos_expandpath() | |
9426 */ | |
9427 static int _cdecl | |
9428 pstrcmp(const void *a, const void *b) | |
9429 { | |
39 | 9430 return (pathcmp(*(char **)a, *(char **)b, -1)); |
7 | 9431 } |
9432 | |
9433 # ifndef WIN3264 | |
9434 static void | |
9435 namelowcpy( | |
9436 char_u *d, | |
9437 char_u *s) | |
9438 { | |
9439 # ifdef DJGPP | |
9440 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */ | |
9441 while (*s) | |
9442 *d++ = *s++; | |
9443 else | |
9444 # endif | |
9445 while (*s) | |
9446 *d++ = TOLOWER_LOC(*s++); | |
9447 *d = NUL; | |
9448 } | |
9449 # endif | |
9450 | |
9451 /* | |
445 | 9452 * Recursively expand one path component into all matching files and/or |
9453 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
7 | 9454 * Return the number of matches found. |
9455 * "path" has backslashes before chars that are not to be expanded, starting | |
9456 * at "path[wildoff]". | |
445 | 9457 * Return the number of matches found. |
9458 * NOTE: much of this is identical to unix_expandpath(), keep in sync! | |
7 | 9459 */ |
9460 static int | |
9461 dos_expandpath( | |
9462 garray_T *gap, | |
9463 char_u *path, | |
9464 int wildoff, | |
445 | 9465 int flags, /* EW_* flags */ |
1224 | 9466 int didstar) /* expanded "**" once already */ |
445 | 9467 { |
9468 char_u *buf; | |
9469 char_u *path_end; | |
9470 char_u *p, *s, *e; | |
9471 int start_len = gap->ga_len; | |
9472 char_u *pat; | |
9473 regmatch_T regmatch; | |
9474 int starts_with_dot; | |
9475 int matches; | |
9476 int len; | |
9477 int starstar = FALSE; | |
9478 static int stardepth = 0; /* depth for "**" expansion */ | |
7 | 9479 #ifdef WIN3264 |
9480 WIN32_FIND_DATA fb; | |
9481 HANDLE hFind = (HANDLE)0; | |
9482 # ifdef FEAT_MBYTE | |
9483 WIN32_FIND_DATAW wfb; | |
9484 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ | |
9485 # endif | |
9486 #else | |
9487 struct ffblk fb; | |
9488 #endif | |
9489 char_u *matchname; | |
445 | 9490 int ok; |
9491 | |
9492 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
9493 if (stardepth > 0) | |
9494 { | |
9495 ui_breakcheck(); | |
9496 if (got_int) | |
9497 return 0; | |
9498 } | |
7 | 9499 |
9500 /* make room for file name */ | |
445 | 9501 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
7 | 9502 if (buf == NULL) |
9503 return 0; | |
9504 | |
9505 /* | |
9506 * Find the first part in the path name that contains a wildcard or a ~1. | |
9507 * Copy it into buf, including the preceding characters. | |
9508 */ | |
9509 p = buf; | |
9510 s = buf; | |
9511 e = NULL; | |
9512 path_end = path; | |
9513 while (*path_end != NUL) | |
9514 { | |
9515 /* May ignore a wildcard that has a backslash before it; it will | |
9516 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
9517 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
9518 *p++ = *path_end++; | |
9519 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') | |
9520 { | |
9521 if (e != NULL) | |
9522 break; | |
9523 s = p + 1; | |
9524 } | |
9525 else if (path_end >= path + wildoff | |
9526 && vim_strchr((char_u *)"*?[~", *path_end) != NULL) | |
9527 e = p; | |
9528 #ifdef FEAT_MBYTE | |
9529 if (has_mbyte) | |
9530 { | |
474 | 9531 len = (*mb_ptr2len)(path_end); |
7 | 9532 STRNCPY(p, path_end, len); |
9533 p += len; | |
9534 path_end += len; | |
9535 } | |
9536 else | |
9537 #endif | |
9538 *p++ = *path_end++; | |
9539 } | |
9540 e = p; | |
9541 *e = NUL; | |
9542 | |
9543 /* now we have one wildcard component between s and e */ | |
9544 /* Remove backslashes between "wildoff" and the start of the wildcard | |
9545 * component. */ | |
9546 for (p = buf + wildoff; p < s; ++p) | |
9547 if (rem_backslash(p)) | |
9548 { | |
1624 | 9549 STRMOVE(p, p + 1); |
7 | 9550 --e; |
9551 --s; | |
9552 } | |
9553 | |
445 | 9554 /* Check for "**" between "s" and "e". */ |
9555 for (p = s; p < e; ++p) | |
9556 if (p[0] == '*' && p[1] == '*') | |
9557 starstar = TRUE; | |
9558 | |
7 | 9559 starts_with_dot = (*s == '.'); |
9560 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
9561 if (pat == NULL) | |
9562 { | |
9563 vim_free(buf); | |
9564 return 0; | |
9565 } | |
9566 | |
9567 /* compile the regexp into a program */ | |
3261 | 9568 if (flags & (EW_NOERROR | EW_NOTWILD)) |
2966 | 9569 ++emsg_silent; |
7 | 9570 regmatch.rm_ic = TRUE; /* Always ignore case */ |
9571 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
3261 | 9572 if (flags & (EW_NOERROR | EW_NOTWILD)) |
2966 | 9573 --emsg_silent; |
7 | 9574 vim_free(pat); |
9575 | |
3261 | 9576 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
7 | 9577 { |
9578 vim_free(buf); | |
9579 return 0; | |
9580 } | |
9581 | |
9582 /* remember the pattern or file name being looked for */ | |
9583 matchname = vim_strsave(s); | |
9584 | |
445 | 9585 /* If "**" is by itself, this is the first time we encounter it and more |
9586 * is following then find matches without any directory. */ | |
9587 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
9588 && *path_end == '/') | |
9589 { | |
9590 STRCPY(s, path_end + 1); | |
9591 ++stardepth; | |
9592 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
9593 --stardepth; | |
9594 } | |
9595 | |
7 | 9596 /* Scan all files in the directory with "dir/ *.*" */ |
9597 STRCPY(s, "*.*"); | |
9598 #ifdef WIN3264 | |
9599 # ifdef FEAT_MBYTE | |
9600 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
9601 { | |
9602 /* The active codepage differs from 'encoding'. Attempt using the | |
9603 * wide function. If it fails because it is not implemented fall back | |
9604 * to the non-wide version (for Windows 98) */ | |
1752 | 9605 wn = enc_to_utf16(buf, NULL); |
7 | 9606 if (wn != NULL) |
9607 { | |
9608 hFind = FindFirstFileW(wn, &wfb); | |
9609 if (hFind == INVALID_HANDLE_VALUE | |
9610 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) | |
9611 { | |
9612 vim_free(wn); | |
9613 wn = NULL; | |
9614 } | |
9615 } | |
9616 } | |
9617 | |
9618 if (wn == NULL) | |
9619 # endif | |
9620 hFind = FindFirstFile(buf, &fb); | |
9621 ok = (hFind != INVALID_HANDLE_VALUE); | |
9622 #else | |
9623 /* If we are expanding wildcards we try both files and directories */ | |
9624 ok = (findfirst((char *)buf, &fb, | |
9625 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); | |
9626 #endif | |
9627 | |
9628 while (ok) | |
9629 { | |
9630 #ifdef WIN3264 | |
9631 # ifdef FEAT_MBYTE | |
9632 if (wn != NULL) | |
1752 | 9633 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */ |
7 | 9634 else |
9635 # endif | |
9636 p = (char_u *)fb.cFileName; | |
9637 #else | |
9638 p = (char_u *)fb.ff_name; | |
9639 #endif | |
9640 /* Ignore entries starting with a dot, unless when asked for. Accept | |
9641 * all entries found with "matchname". */ | |
9642 if ((p[0] != '.' || starts_with_dot) | |
9643 && (matchname == NULL | |
3261 | 9644 || (regmatch.regprog != NULL |
9645 && vim_regexec(®match, p, (colnr_T)0)) | |
2984 | 9646 || ((flags & EW_NOTWILD) |
9647 && fnamencmp(path + (s - buf), p, e - s) == 0))) | |
7 | 9648 { |
9649 #ifdef WIN3264 | |
9650 STRCPY(s, p); | |
9651 #else | |
9652 namelowcpy(s, p); | |
9653 #endif | |
9654 len = (int)STRLEN(buf); | |
445 | 9655 |
9656 if (starstar && stardepth < 100) | |
9657 { | |
9658 /* For "**" in the pattern first go deeper in the tree to | |
9659 * find matches. */ | |
9660 STRCPY(buf + len, "/**"); | |
9661 STRCPY(buf + len + 3, path_end); | |
9662 ++stardepth; | |
9663 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); | |
9664 --stardepth; | |
9665 } | |
9666 | |
7 | 9667 STRCPY(buf + len, path_end); |
9668 if (mch_has_exp_wildcard(path_end)) | |
9669 { | |
9670 /* need to expand another component of the path */ | |
9671 /* remove backslashes for the remaining components only */ | |
445 | 9672 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
7 | 9673 } |
9674 else | |
9675 { | |
9676 /* no more wildcards, check if there is a match */ | |
9677 /* remove backslashes for the remaining components only */ | |
9678 if (*path_end != 0) | |
9679 backslash_halve(buf + len + 1); | |
9680 if (mch_getperm(buf) >= 0) /* add existing file */ | |
9681 addfile(gap, buf, flags); | |
9682 } | |
9683 } | |
9684 | |
9685 #ifdef WIN3264 | |
9686 # ifdef FEAT_MBYTE | |
9687 if (wn != NULL) | |
9688 { | |
9689 vim_free(p); | |
9690 ok = FindNextFileW(hFind, &wfb); | |
9691 } | |
9692 else | |
9693 # endif | |
9694 ok = FindNextFile(hFind, &fb); | |
9695 #else | |
9696 ok = (findnext(&fb) == 0); | |
9697 #endif | |
9698 | |
9699 /* If no more matches and no match was used, try expanding the name | |
9700 * itself. Finds the long name of a short filename. */ | |
9701 if (!ok && matchname != NULL && gap->ga_len == start_len) | |
9702 { | |
9703 STRCPY(s, matchname); | |
9704 #ifdef WIN3264 | |
9705 FindClose(hFind); | |
9706 # ifdef FEAT_MBYTE | |
9707 if (wn != NULL) | |
9708 { | |
9709 vim_free(wn); | |
1752 | 9710 wn = enc_to_utf16(buf, NULL); |
7 | 9711 if (wn != NULL) |
9712 hFind = FindFirstFileW(wn, &wfb); | |
9713 } | |
9714 if (wn == NULL) | |
9715 # endif | |
9716 hFind = FindFirstFile(buf, &fb); | |
9717 ok = (hFind != INVALID_HANDLE_VALUE); | |
9718 #else | |
9719 ok = (findfirst((char *)buf, &fb, | |
9720 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); | |
9721 #endif | |
9722 vim_free(matchname); | |
9723 matchname = NULL; | |
9724 } | |
9725 } | |
9726 | |
9727 #ifdef WIN3264 | |
9728 FindClose(hFind); | |
9729 # ifdef FEAT_MBYTE | |
9730 vim_free(wn); | |
9731 # endif | |
9732 #endif | |
9733 vim_free(buf); | |
9734 vim_free(regmatch.regprog); | |
9735 vim_free(matchname); | |
9736 | |
9737 matches = gap->ga_len - start_len; | |
9738 if (matches > 0) | |
9739 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, | |
9740 sizeof(char_u *), pstrcmp); | |
9741 return matches; | |
9742 } | |
9743 | |
9744 int | |
9745 mch_expandpath( | |
9746 garray_T *gap, | |
9747 char_u *path, | |
9748 int flags) /* EW_* flags */ | |
9749 { | |
445 | 9750 return dos_expandpath(gap, path, 0, flags, FALSE); |
7 | 9751 } |
9752 # endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */ | |
9753 | |
445 | 9754 #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
9755 || defined(PROTO) | |
9756 /* | |
9757 * Unix style wildcard expansion code. | |
9758 * It's here because it's used both for Unix and Mac. | |
9759 */ | |
9760 static int pstrcmp __ARGS((const void *, const void *)); | |
9761 | |
9762 static int | |
9763 pstrcmp(a, b) | |
9764 const void *a, *b; | |
9765 { | |
9766 return (pathcmp(*(char **)a, *(char **)b, -1)); | |
9767 } | |
9768 | |
9769 /* | |
9770 * Recursively expand one path component into all matching files and/or | |
9771 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
9772 * "path" has backslashes before chars that are not to be expanded, starting | |
9773 * at "path + wildoff". | |
9774 * Return the number of matches found. | |
9775 * NOTE: much of this is identical to dos_expandpath(), keep in sync! | |
9776 */ | |
9777 int | |
9778 unix_expandpath(gap, path, wildoff, flags, didstar) | |
9779 garray_T *gap; | |
9780 char_u *path; | |
9781 int wildoff; | |
9782 int flags; /* EW_* flags */ | |
9783 int didstar; /* expanded "**" once already */ | |
9784 { | |
9785 char_u *buf; | |
9786 char_u *path_end; | |
9787 char_u *p, *s, *e; | |
9788 int start_len = gap->ga_len; | |
9789 char_u *pat; | |
9790 regmatch_T regmatch; | |
9791 int starts_with_dot; | |
9792 int matches; | |
9793 int len; | |
9794 int starstar = FALSE; | |
9795 static int stardepth = 0; /* depth for "**" expansion */ | |
9796 | |
9797 DIR *dirp; | |
9798 struct dirent *dp; | |
9799 | |
9800 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
9801 if (stardepth > 0) | |
9802 { | |
9803 ui_breakcheck(); | |
9804 if (got_int) | |
9805 return 0; | |
9806 } | |
9807 | |
9808 /* make room for file name */ | |
9809 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); | |
9810 if (buf == NULL) | |
9811 return 0; | |
9812 | |
9813 /* | |
9814 * Find the first part in the path name that contains a wildcard. | |
3505 | 9815 * When EW_ICASE is set every letter is considered to be a wildcard. |
445 | 9816 * Copy it into "buf", including the preceding characters. |
9817 */ | |
9818 p = buf; | |
9819 s = buf; | |
9820 e = NULL; | |
9821 path_end = path; | |
9822 while (*path_end != NUL) | |
9823 { | |
9824 /* May ignore a wildcard that has a backslash before it; it will | |
9825 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
9826 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
9827 *p++ = *path_end++; | |
9828 else if (*path_end == '/') | |
9829 { | |
9830 if (e != NULL) | |
9831 break; | |
9832 s = p + 1; | |
9833 } | |
9834 else if (path_end >= path + wildoff | |
3505 | 9835 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL |
9836 #ifndef CASE_INSENSITIVE_FILENAME | |
9837 || ((flags & EW_ICASE) | |
9838 && isalpha(PTR2CHAR(path_end))) | |
9839 #endif | |
9840 )) | |
445 | 9841 e = p; |
9842 #ifdef FEAT_MBYTE | |
9843 if (has_mbyte) | |
9844 { | |
474 | 9845 len = (*mb_ptr2len)(path_end); |
445 | 9846 STRNCPY(p, path_end, len); |
9847 p += len; | |
9848 path_end += len; | |
9849 } | |
9850 else | |
9851 #endif | |
9852 *p++ = *path_end++; | |
9853 } | |
9854 e = p; | |
9855 *e = NUL; | |
9856 | |
2984 | 9857 /* Now we have one wildcard component between "s" and "e". */ |
445 | 9858 /* Remove backslashes between "wildoff" and the start of the wildcard |
9859 * component. */ | |
9860 for (p = buf + wildoff; p < s; ++p) | |
9861 if (rem_backslash(p)) | |
9862 { | |
1624 | 9863 STRMOVE(p, p + 1); |
445 | 9864 --e; |
9865 --s; | |
9866 } | |
9867 | |
9868 /* Check for "**" between "s" and "e". */ | |
9869 for (p = s; p < e; ++p) | |
9870 if (p[0] == '*' && p[1] == '*') | |
9871 starstar = TRUE; | |
9872 | |
9873 /* convert the file pattern to a regexp pattern */ | |
9874 starts_with_dot = (*s == '.'); | |
9875 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
9876 if (pat == NULL) | |
9877 { | |
9878 vim_free(buf); | |
9879 return 0; | |
9880 } | |
9881 | |
9882 /* compile the regexp into a program */ | |
587 | 9883 #ifdef CASE_INSENSITIVE_FILENAME |
445 | 9884 regmatch.rm_ic = TRUE; /* Behave like Terminal.app */ |
9885 #else | |
2652 | 9886 if (flags & EW_ICASE) |
9887 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ | |
9888 else | |
9889 regmatch.rm_ic = FALSE; /* Don't ignore case */ | |
445 | 9890 #endif |
3261 | 9891 if (flags & (EW_NOERROR | EW_NOTWILD)) |
9892 ++emsg_silent; | |
445 | 9893 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); |
3261 | 9894 if (flags & (EW_NOERROR | EW_NOTWILD)) |
9895 --emsg_silent; | |
445 | 9896 vim_free(pat); |
9897 | |
3261 | 9898 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) |
445 | 9899 { |
9900 vim_free(buf); | |
9901 return 0; | |
9902 } | |
9903 | |
9904 /* If "**" is by itself, this is the first time we encounter it and more | |
9905 * is following then find matches without any directory. */ | |
9906 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
9907 && *path_end == '/') | |
9908 { | |
9909 STRCPY(s, path_end + 1); | |
9910 ++stardepth; | |
9911 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
9912 --stardepth; | |
9913 } | |
9914 | |
9915 /* open the directory for scanning */ | |
9916 *s = NUL; | |
9917 dirp = opendir(*buf == NUL ? "." : (char *)buf); | |
9918 | |
9919 /* Find all matching entries */ | |
9920 if (dirp != NULL) | |
9921 { | |
9922 for (;;) | |
9923 { | |
9924 dp = readdir(dirp); | |
9925 if (dp == NULL) | |
9926 break; | |
9927 if ((dp->d_name[0] != '.' || starts_with_dot) | |
3261 | 9928 && ((regmatch.regprog != NULL && vim_regexec(®match, |
9929 (char_u *)dp->d_name, (colnr_T)0)) | |
2984 | 9930 || ((flags & EW_NOTWILD) |
9931 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) | |
445 | 9932 { |
9933 STRCPY(s, dp->d_name); | |
9934 len = STRLEN(buf); | |
9935 | |
9936 if (starstar && stardepth < 100) | |
9937 { | |
9938 /* For "**" in the pattern first go deeper in the tree to | |
9939 * find matches. */ | |
9940 STRCPY(buf + len, "/**"); | |
9941 STRCPY(buf + len + 3, path_end); | |
9942 ++stardepth; | |
9943 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); | |
9944 --stardepth; | |
9945 } | |
9946 | |
9947 STRCPY(buf + len, path_end); | |
9948 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ | |
9949 { | |
9950 /* need to expand another component of the path */ | |
9951 /* remove backslashes for the remaining components only */ | |
9952 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); | |
9953 } | |
9954 else | |
9955 { | |
9956 /* no more wildcards, check if there is a match */ | |
9957 /* remove backslashes for the remaining components only */ | |
9958 if (*path_end != NUL) | |
9959 backslash_halve(buf + len + 1); | |
9960 if (mch_getperm(buf) >= 0) /* add existing file */ | |
9961 { | |
768 | 9962 #ifdef MACOS_CONVERT |
445 | 9963 size_t precomp_len = STRLEN(buf)+1; |
9964 char_u *precomp_buf = | |
9965 mac_precompose_path(buf, precomp_len, &precomp_len); | |
768 | 9966 |
445 | 9967 if (precomp_buf) |
9968 { | |
9969 mch_memmove(buf, precomp_buf, precomp_len); | |
9970 vim_free(precomp_buf); | |
9971 } | |
9972 #endif | |
9973 addfile(gap, buf, flags); | |
9974 } | |
9975 } | |
9976 } | |
9977 } | |
9978 | |
9979 closedir(dirp); | |
9980 } | |
9981 | |
9982 vim_free(buf); | |
9983 vim_free(regmatch.regprog); | |
9984 | |
9985 matches = gap->ga_len - start_len; | |
9986 if (matches > 0) | |
9987 qsort(((char_u **)gap->ga_data) + start_len, matches, | |
9988 sizeof(char_u *), pstrcmp); | |
9989 return matches; | |
9990 } | |
9991 #endif | |
9992 | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9993 #if defined(FEAT_SEARCHPATH) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9994 static int find_previous_pathsep __ARGS((char_u *path, char_u **psep)); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9995 static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i)); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9996 static void expand_path_option __ARGS((char_u *curdir, garray_T *gap)); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9997 static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap)); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9998 static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern)); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9999 static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags)); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10000 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10001 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10002 * Moves "*psep" back to the previous path separator in "path". |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10003 * Returns FAIL is "*psep" ends up at the beginning of "path". |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10004 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10005 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10006 find_previous_pathsep(path, psep) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10007 char_u *path; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10008 char_u **psep; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10009 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10010 /* skip the current separator */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10011 if (*psep > path && vim_ispathsep(**psep)) |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10012 --*psep; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10013 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10014 /* find the previous separator */ |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10015 while (*psep > path) |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10016 { |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10017 if (vim_ispathsep(**psep)) |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10018 return OK; |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10019 mb_ptr_back(path, *psep); |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10020 } |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10021 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10022 return FAIL; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10023 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10024 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10025 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10026 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap". |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10027 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]". |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10028 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10029 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10030 is_unique(maybe_unique, gap, i) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10031 char_u *maybe_unique; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10032 garray_T *gap; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10033 int i; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10034 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10035 int j; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10036 int candidate_len; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10037 int other_path_len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10038 char_u **other_paths = (char_u **)gap->ga_data; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10039 char_u *rival; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10040 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10041 for (j = 0; j < gap->ga_len; j++) |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10042 { |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10043 if (j == i) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10044 continue; /* don't compare it with itself */ |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10045 |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
10046 candidate_len = (int)STRLEN(maybe_unique); |
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
10047 other_path_len = (int)STRLEN(other_paths[j]); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10048 if (other_path_len < candidate_len) |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10049 continue; /* it's different when it's shorter */ |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10050 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10051 rival = other_paths[j] + other_path_len - candidate_len; |
2574 | 10052 if (fnamecmp(maybe_unique, rival) == 0 |
10053 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1)))) | |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10054 return FALSE; /* match */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10055 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10056 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10057 return TRUE; /* no match found */ |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10058 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10059 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10060 /* |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
10061 * Split the 'path' option into an array of strings in garray_T. Relative |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10062 * paths are expanded to their equivalent fullpath. This includes the "." |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10063 * (relative to current buffer directory) and empty path (relative to current |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10064 * directory) notations. |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10065 * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10066 * TODO: handle upward search (;) and path limiter (**N) notations by |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10067 * expanding each into their equivalent path(s). |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10068 */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10069 static void |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10070 expand_path_option(curdir, gap) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10071 char_u *curdir; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10072 garray_T *gap; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10073 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10074 char_u *path_option = *curbuf->b_p_path == NUL |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10075 ? p_path : curbuf->b_p_path; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10076 char_u *buf; |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10077 char_u *p; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10078 int len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10079 |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10080 if ((buf = alloc((int)MAXPATHL)) == NULL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10081 return; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10082 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10083 while (*path_option != NUL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10084 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10085 copy_option_part(&path_option, buf, MAXPATHL, " ,"); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10086 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10087 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1]))) |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10088 { |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10089 /* Relative to current buffer: |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10090 * "/path/file" + "." -> "/path/" |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10091 * "/path/file" + "./subdir" -> "/path/subdir" */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10092 if (curbuf->b_ffname == NULL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10093 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10094 p = gettail(curbuf->b_ffname); |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10095 len = (int)(p - curbuf->b_ffname); |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10096 if (len + (int)STRLEN(buf) >= MAXPATHL) |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10097 continue; |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10098 if (buf[1] == NUL) |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10099 buf[len] = NUL; |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10100 else |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10101 STRMOVE(buf + len, buf + 2); |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10102 mch_memmove(buf, curbuf->b_ffname, len); |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10103 simplify_filename(buf); |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10104 } |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10105 else if (buf[0] == NUL) |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10106 /* relative to current directory */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10107 STRCPY(buf, curdir); |
2495
a847363bf06e
Fix a few problems for :find completion. Test much more. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2487
diff
changeset
|
10108 else if (path_with_url(buf)) |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10109 /* URL can't be used here */ |
2495
a847363bf06e
Fix a few problems for :find completion. Test much more. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2487
diff
changeset
|
10110 continue; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10111 else if (!mch_isFullName(buf)) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10112 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10113 /* Expand relative path to their full path equivalent */ |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10114 len = (int)STRLEN(curdir); |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10115 if (len + (int)STRLEN(buf) + 3 > MAXPATHL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10116 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10117 STRMOVE(buf + len + 1, buf); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10118 STRCPY(buf, curdir); |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10119 buf[len] = PATHSEP; |
2476
6be86488996b
Fix: With 'path' set to relative directory ":find" completion didn't work.
Bram Moolenaar <bram@vim.org>
parents:
2471
diff
changeset
|
10120 simplify_filename(buf); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10121 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10122 |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10123 if (ga_grow(gap, 1) == FAIL) |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10124 break; |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10125 p = vim_strsave(buf); |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10126 if (p == NULL) |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10127 break; |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10128 ((char_u **)gap->ga_data)[gap->ga_len++] = p; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10129 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10130 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10131 vim_free(buf); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10132 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10133 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10134 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10135 * Returns a pointer to the file or directory name in "fname" that matches the |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10136 * longest path in "ga"p, or NULL if there is no match. For example: |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10137 * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10138 * path: /foo/bar/baz |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10139 * fname: /foo/bar/baz/quux.txt |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10140 * returns: ^this |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10141 */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10142 static char_u * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10143 get_path_cutoff(fname, gap) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10144 char_u *fname; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10145 garray_T *gap; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10146 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10147 int i; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10148 int maxlen = 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10149 char_u **path_part = (char_u **)gap->ga_data; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10150 char_u *cutoff = NULL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10151 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10152 for (i = 0; i < gap->ga_len; i++) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10153 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10154 int j = 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10155 |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10156 while ((fname[j] == path_part[i][j] |
2534
e27def2ed69b
Make :find completion work better with the DJGPP build. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
10157 # if defined(MSWIN) || defined(MSDOS) |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10158 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j])) |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10159 #endif |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10160 ) && fname[j] != NUL && path_part[i][j] != NUL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10161 j++; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10162 if (j > maxlen) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10163 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10164 maxlen = j; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10165 cutoff = &fname[j]; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10166 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10167 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10168 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10169 /* skip to the file or directory name */ |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10170 if (cutoff != NULL) |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10171 while (vim_ispathsep(*cutoff)) |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10172 mb_ptr_adv(cutoff); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10173 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10174 return cutoff; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10175 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10176 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10177 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10178 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10179 * that they are unique with respect to each other while conserving the part |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10180 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len". |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10181 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10182 static void |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10183 uniquefy_paths(gap, pattern) |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10184 garray_T *gap; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10185 char_u *pattern; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10186 { |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10187 int i; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10188 int len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10189 char_u **fnames = (char_u **)gap->ga_data; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10190 int sort_again = FALSE; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10191 char_u *pat; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10192 char_u *file_pattern; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10193 char_u *curdir; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10194 regmatch_T regmatch; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10195 garray_T path_ga; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10196 char_u **in_curdir = NULL; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10197 char_u *short_name; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10198 |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10199 remove_duplicates(gap); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10200 ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10201 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10202 /* |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10203 * We need to prepend a '*' at the beginning of file_pattern so that the |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10204 * regex matches anywhere in the path. FIXME: is this valid for all |
2387
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10205 * possible patterns? |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10206 */ |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
10207 len = (int)STRLEN(pattern); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10208 file_pattern = alloc(len + 2); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10209 if (file_pattern == NULL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10210 return; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10211 file_pattern[0] = '*'; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10212 file_pattern[1] = NUL; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10213 STRCAT(file_pattern, pattern); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10214 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10215 vim_free(file_pattern); |
2387
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10216 if (pat == NULL) |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10217 return; |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10218 |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10219 regmatch.rm_ic = TRUE; /* always ignore case */ |
2387
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10220 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10221 vim_free(pat); |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10222 if (regmatch.regprog == NULL) |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10223 return; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10224 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10225 if ((curdir = alloc((int)(MAXPATHL))) == NULL) |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10226 goto theend; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10227 mch_dirname(curdir, MAXPATHL); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10228 expand_path_option(curdir, &path_ga); |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10229 |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10230 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *)); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10231 if (in_curdir == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10232 goto theend; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10233 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10234 for (i = 0; i < gap->ga_len && !got_int; i++) |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10235 { |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10236 char_u *path = fnames[i]; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10237 int is_in_curdir; |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10238 char_u *dir_end = gettail_dir(path); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10239 char_u *pathsep_p; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10240 char_u *path_cutoff; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10241 |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
10242 len = (int)STRLEN(path); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10243 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10244 && curdir[dir_end - path] == NUL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10245 if (is_in_curdir) |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10246 in_curdir[i] = vim_strsave(path); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10247 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10248 /* Shorten the filename while maintaining its uniqueness */ |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10249 path_cutoff = get_path_cutoff(path, &path_ga); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10250 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10251 /* we start at the end of the path */ |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10252 pathsep_p = path + len - 1; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10253 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10254 while (find_previous_pathsep(path, &pathsep_p)) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10255 if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10256 && is_unique(pathsep_p + 1, gap, i) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10257 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10258 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10259 sort_again = TRUE; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10260 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p)); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10261 break; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10262 } |
2538 | 10263 |
10264 if (mch_isFullName(path)) | |
10265 { | |
10266 /* | |
10267 * Last resort: shorten relative to curdir if possible. | |
10268 * 'possible' means: | |
10269 * 1. It is under the current directory. | |
10270 * 2. The result is actually shorter than the original. | |
10271 * | |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10272 * Before curdir After |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10273 * /foo/bar/file.txt /foo/bar ./file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10274 * c:\foo\bar\file.txt c:\foo\bar .\file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10275 * /file.txt / /file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10276 * c:\file.txt c:\ .\file.txt |
2538 | 10277 */ |
10278 short_name = shorten_fname(path, curdir); | |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10279 if (short_name != NULL && short_name > path + 1 |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10280 #if defined(MSWIN) || defined(MSDOS) |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10281 /* On windows, |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10282 * shorten_fname("c:\a\a.txt", "c:\a\b") |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10283 * returns "\a\a.txt", which is not really the short |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10284 * name, hence: */ |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10285 && !vim_ispathsep(*short_name) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10286 #endif |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
10287 ) |
2538 | 10288 { |
10289 STRCPY(path, "."); | |
10290 add_pathsep(path); | |
2562
19f4f5ee3d1f
Fix: strcat() on overlapping string. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2559
diff
changeset
|
10291 STRMOVE(path + STRLEN(path), short_name); |
2538 | 10292 } |
10293 } | |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10294 ui_breakcheck(); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10295 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10296 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10297 /* Shorten filenames in /in/current/directory/{filename} */ |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10298 for (i = 0; i < gap->ga_len && !got_int; i++) |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10299 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10300 char_u *rel_path; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10301 char_u *path = in_curdir[i]; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10302 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10303 if (path == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10304 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10305 |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10306 /* If the {filename} is not unique, change it to ./{filename}. |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10307 * Else reduce it to {filename} */ |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10308 short_name = shorten_fname(path, curdir); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10309 if (short_name == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10310 short_name = path; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10311 if (is_unique(short_name, gap, i)) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10312 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10313 STRCPY(fnames[i], short_name); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10314 continue; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10315 } |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10316 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10317 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2)); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10318 if (rel_path == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10319 goto theend; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10320 STRCPY(rel_path, "."); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10321 add_pathsep(rel_path); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10322 STRCAT(rel_path, short_name); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10323 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10324 vim_free(fnames[i]); |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10325 fnames[i] = rel_path; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10326 sort_again = TRUE; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10327 ui_breakcheck(); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10328 } |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10329 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10330 theend: |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10331 vim_free(curdir); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10332 if (in_curdir != NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10333 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10334 for (i = 0; i < gap->ga_len; i++) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10335 vim_free(in_curdir[i]); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10336 vim_free(in_curdir); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10337 } |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10338 ga_clear_strings(&path_ga); |
2387
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
10339 vim_free(regmatch.regprog); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10340 |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10341 if (sort_again) |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
10342 remove_duplicates(gap); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10343 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10344 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10345 /* |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10346 * Calls globpath() with 'path' values for the given pattern and stores the |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10347 * result in "gap". |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10348 * Returns the total number of matches. |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10349 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10350 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10351 expand_in_path(gap, pattern, flags) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10352 garray_T *gap; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10353 char_u *pattern; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10354 int flags; /* EW_* flags */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10355 { |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10356 char_u *curdir; |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10357 garray_T path_ga; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10358 char_u *files = NULL; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10359 char_u *s; /* start */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10360 char_u *e; /* end */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10361 char_u *paths = NULL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10362 |
2477
81bf2aaa979a
Fix crash in :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2476
diff
changeset
|
10363 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10364 return 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10365 mch_dirname(curdir, MAXPATHL); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10366 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
10367 ga_init2(&path_ga, (int)sizeof(char_u *), 1); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10368 expand_path_option(curdir, &path_ga); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10369 vim_free(curdir); |
2481
734196b073e0
Fix crash when no item in 'path' is used for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2477
diff
changeset
|
10370 if (path_ga.ga_len == 0) |
734196b073e0
Fix crash when no item in 'path' is used for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2477
diff
changeset
|
10371 return 0; |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10372 |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10373 paths = ga_concat_strings(&path_ga); |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10374 ga_clear_strings(&path_ga); |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10375 if (paths == NULL) |
2477
81bf2aaa979a
Fix crash in :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2476
diff
changeset
|
10376 return 0; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10377 |
2652 | 10378 files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10379 vim_free(paths); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10380 if (files == NULL) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10381 return 0; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10382 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10383 /* Copy each path in files into gap */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10384 s = e = files; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10385 while (*s != NUL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10386 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10387 while (*e != '\n' && *e != NUL) |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10388 e++; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10389 if (*e == NUL) |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10390 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10391 addfile(gap, s, flags); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10392 break; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10393 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10394 else |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10395 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10396 /* *e is '\n' */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10397 *e = NUL; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10398 addfile(gap, s, flags); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10399 e++; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10400 s = e; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10401 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10402 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10403 vim_free(files); |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10404 |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
10405 return gap->ga_len; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10406 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10407 #endif |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10408 |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10409 #if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10410 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10411 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10412 * list of file names in allocated memory. |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10413 */ |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10414 void |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10415 remove_duplicates(gap) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10416 garray_T *gap; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10417 { |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10418 int i; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10419 int j; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10420 char_u **fnames = (char_u **)gap->ga_data; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10421 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
10422 sort_strings(fnames, gap->ga_len); |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10423 for (i = gap->ga_len - 1; i > 0; --i) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10424 if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10425 { |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10426 vim_free(fnames[i]); |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10427 for (j = i + 1; j < gap->ga_len; ++j) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10428 fnames[j - 1] = fnames[j]; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10429 --gap->ga_len; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10430 } |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10431 } |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10432 #endif |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
10433 |
7 | 10434 /* |
10435 * Generic wildcard expansion code. | |
10436 * | |
10437 * Characters in "pat" that should not be expanded must be preceded with a | |
10438 * backslash. E.g., "/path\ with\ spaces/my\*star*" | |
10439 * | |
10440 * Return FAIL when no single file was found. In this case "num_file" is not | |
10441 * set, and "file" may contain an error message. | |
10442 * Return OK when some files found. "num_file" is set to the number of | |
10443 * matches, "file" to the array of matches. Call FreeWild() later. | |
10444 */ | |
10445 int | |
10446 gen_expand_wildcards(num_pat, pat, num_file, file, flags) | |
10447 int num_pat; /* number of input patterns */ | |
10448 char_u **pat; /* array of input patterns */ | |
10449 int *num_file; /* resulting number of files */ | |
10450 char_u ***file; /* array of resulting files */ | |
10451 int flags; /* EW_* flags */ | |
10452 { | |
10453 int i; | |
10454 garray_T ga; | |
10455 char_u *p; | |
10456 static int recursive = FALSE; | |
10457 int add_pat; | |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10458 #if defined(FEAT_SEARCHPATH) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10459 int did_expand_in_path = FALSE; |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10460 #endif |
7 | 10461 |
10462 /* | |
10463 * expand_env() is called to expand things like "~user". If this fails, | |
10464 * it calls ExpandOne(), which brings us back here. In this case, always | |
10465 * call the machine specific expansion function, if possible. Otherwise, | |
10466 * return FAIL. | |
10467 */ | |
10468 if (recursive) | |
10469 #ifdef SPECIAL_WILDCHAR | |
10470 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
10471 #else | |
10472 return FAIL; | |
10473 #endif | |
10474 | |
10475 #ifdef SPECIAL_WILDCHAR | |
10476 /* | |
10477 * If there are any special wildcard characters which we cannot handle | |
10478 * here, call machine specific function for all the expansion. This | |
10479 * avoids starting the shell for each argument separately. | |
10480 * For `=expr` do use the internal function. | |
10481 */ | |
10482 for (i = 0; i < num_pat; i++) | |
10483 { | |
10484 if (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL | |
10485 # ifdef VIM_BACKTICK | |
10486 && !(vim_backtick(pat[i]) && pat[i][1] == '=') | |
10487 # endif | |
10488 ) | |
10489 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
10490 } | |
10491 #endif | |
10492 | |
10493 recursive = TRUE; | |
10494 | |
10495 /* | |
10496 * The matching file names are stored in a growarray. Init it empty. | |
10497 */ | |
10498 ga_init2(&ga, (int)sizeof(char_u *), 30); | |
10499 | |
10500 for (i = 0; i < num_pat; ++i) | |
10501 { | |
10502 add_pat = -1; | |
10503 p = pat[i]; | |
10504 | |
10505 #ifdef VIM_BACKTICK | |
10506 if (vim_backtick(p)) | |
10507 add_pat = expand_backtick(&ga, p, flags); | |
10508 else | |
10509 #endif | |
10510 { | |
10511 /* | |
10512 * First expand environment variables, "~/" and "~user/". | |
10513 */ | |
2538 | 10514 if (vim_strchr(p, '$') != NULL || *p == '~') |
7 | 10515 { |
1408 | 10516 p = expand_env_save_opt(p, TRUE); |
7 | 10517 if (p == NULL) |
10518 p = pat[i]; | |
10519 #ifdef UNIX | |
10520 /* | |
10521 * On Unix, if expand_env() can't expand an environment | |
10522 * variable, use the shell to do that. Discard previously | |
10523 * found file names and start all over again. | |
10524 */ | |
2538 | 10525 else if (vim_strchr(p, '$') != NULL || *p == '~') |
7 | 10526 { |
10527 vim_free(p); | |
1914 | 10528 ga_clear_strings(&ga); |
7 | 10529 i = mch_expand_wildcards(num_pat, pat, num_file, file, |
10530 flags); | |
10531 recursive = FALSE; | |
10532 return i; | |
10533 } | |
10534 #endif | |
10535 } | |
10536 | |
10537 /* | |
10538 * If there are wildcards: Expand file names and add each match to | |
10539 * the list. If there is no match, and EW_NOTFOUND is given, add | |
10540 * the pattern. | |
10541 * If there are no wildcards: Add the file name if it exists or | |
10542 * when EW_NOTFOUND is given. | |
10543 */ | |
10544 if (mch_has_exp_wildcard(p)) | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10545 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10546 #if defined(FEAT_SEARCHPATH) |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10547 if ((flags & EW_PATH) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10548 && !mch_isFullName(p) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10549 && !(p[0] == '.' |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10550 && (vim_ispathsep(p[1]) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10551 || (p[1] == '.' && vim_ispathsep(p[2])))) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10552 ) |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10553 { |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10554 /* :find completion where 'path' is used. |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10555 * Recursiveness is OK here. */ |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10556 recursive = FALSE; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10557 add_pat = expand_in_path(&ga, p, flags); |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10558 recursive = TRUE; |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10559 did_expand_in_path = TRUE; |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
10560 } |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10561 else |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10562 #endif |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10563 add_pat = mch_expandpath(&ga, p, flags); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
10564 } |
7 | 10565 } |
10566 | |
10567 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) | |
10568 { | |
10569 char_u *t = backslash_halve_save(p); | |
10570 | |
10571 #if defined(MACOS_CLASSIC) | |
10572 slash_to_colon(t); | |
10573 #endif | |
10574 /* When EW_NOTFOUND is used, always add files and dirs. Makes | |
10575 * "vim c:/" work. */ | |
10576 if (flags & EW_NOTFOUND) | |
10577 addfile(&ga, t, flags | EW_DIR | EW_FILE); | |
10578 else if (mch_getperm(t) >= 0) | |
10579 addfile(&ga, t, flags); | |
10580 vim_free(t); | |
10581 } | |
10582 | |
2313
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
10583 #if defined(FEAT_SEARCHPATH) |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
10584 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) |
2313
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
10585 uniquefy_paths(&ga, p); |
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
10586 #endif |
7 | 10587 if (p != pat[i]) |
10588 vim_free(p); | |
10589 } | |
10590 | |
10591 *num_file = ga.ga_len; | |
10592 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; | |
10593 | |
10594 recursive = FALSE; | |
10595 | |
10596 return (ga.ga_data != NULL) ? OK : FAIL; | |
10597 } | |
10598 | |
10599 # ifdef VIM_BACKTICK | |
10600 | |
10601 /* | |
10602 * Return TRUE if we can expand this backtick thing here. | |
10603 */ | |
10604 static int | |
10605 vim_backtick(p) | |
10606 char_u *p; | |
10607 { | |
10608 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); | |
10609 } | |
10610 | |
10611 /* | |
10612 * Expand an item in `backticks` by executing it as a command. | |
10613 * Currently only works when pat[] starts and ends with a `. | |
10614 * Returns number of file names found. | |
10615 */ | |
10616 static int | |
10617 expand_backtick(gap, pat, flags) | |
10618 garray_T *gap; | |
10619 char_u *pat; | |
10620 int flags; /* EW_* flags */ | |
10621 { | |
10622 char_u *p; | |
10623 char_u *cmd; | |
10624 char_u *buffer; | |
10625 int cnt = 0; | |
10626 int i; | |
10627 | |
10628 /* Create the command: lop off the backticks. */ | |
10629 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); | |
10630 if (cmd == NULL) | |
10631 return 0; | |
10632 | |
10633 #ifdef FEAT_EVAL | |
10634 if (*cmd == '=') /* `={expr}`: Expand expression */ | |
714 | 10635 buffer = eval_to_string(cmd + 1, &p, TRUE); |
7 | 10636 else |
10637 #endif | |
24 | 10638 buffer = get_cmd_output(cmd, NULL, |
10639 (flags & EW_SILENT) ? SHELL_SILENT : 0); | |
7 | 10640 vim_free(cmd); |
10641 if (buffer == NULL) | |
10642 return 0; | |
10643 | |
10644 cmd = buffer; | |
10645 while (*cmd != NUL) | |
10646 { | |
10647 cmd = skipwhite(cmd); /* skip over white space */ | |
10648 p = cmd; | |
10649 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ | |
10650 ++p; | |
10651 /* add an entry if it is not empty */ | |
10652 if (p > cmd) | |
10653 { | |
10654 i = *p; | |
10655 *p = NUL; | |
10656 addfile(gap, cmd, flags); | |
10657 *p = i; | |
10658 ++cnt; | |
10659 } | |
10660 cmd = p; | |
10661 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) | |
10662 ++cmd; | |
10663 } | |
10664 | |
10665 vim_free(buffer); | |
10666 return cnt; | |
10667 } | |
10668 # endif /* VIM_BACKTICK */ | |
10669 | |
10670 /* | |
10671 * Add a file to a file list. Accepted flags: | |
10672 * EW_DIR add directories | |
10673 * EW_FILE add files | |
716 | 10674 * EW_EXEC add executable files |
7 | 10675 * EW_NOTFOUND add even when it doesn't exist |
10676 * EW_ADDSLASH add slash after directory name | |
10677 */ | |
10678 void | |
10679 addfile(gap, f, flags) | |
10680 garray_T *gap; | |
10681 char_u *f; /* filename */ | |
10682 int flags; | |
10683 { | |
10684 char_u *p; | |
10685 int isdir; | |
10686 | |
10687 /* if the file/dir doesn't exist, may not add it */ | |
10688 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0) | |
10689 return; | |
10690 | |
10691 #ifdef FNAME_ILLEGAL | |
10692 /* if the file/dir contains illegal characters, don't add it */ | |
10693 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) | |
10694 return; | |
10695 #endif | |
10696 | |
10697 isdir = mch_isdir(f); | |
10698 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) | |
10699 return; | |
10700 | |
716 | 10701 /* If the file isn't executable, may not add it. Do accept directories. */ |
10702 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f)) | |
10703 return; | |
10704 | |
7 | 10705 /* Make room for another item in the file list. */ |
10706 if (ga_grow(gap, 1) == FAIL) | |
10707 return; | |
10708 | |
10709 p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); | |
10710 if (p == NULL) | |
10711 return; | |
10712 | |
10713 STRCPY(p, f); | |
10714 #ifdef BACKSLASH_IN_FILENAME | |
10715 slash_adjust(p); | |
10716 #endif | |
10717 /* | |
10718 * Append a slash or backslash after directory names if none is present. | |
10719 */ | |
10720 #ifndef DONT_ADD_PATHSEP_TO_DIR | |
10721 if (isdir && (flags & EW_ADDSLASH)) | |
10722 add_pathsep(p); | |
10723 #endif | |
10724 ((char_u **)gap->ga_data)[gap->ga_len++] = p; | |
10725 } | |
10726 #endif /* !NO_EXPANDPATH */ | |
10727 | |
10728 #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) | |
10729 | |
10730 #ifndef SEEK_SET | |
10731 # define SEEK_SET 0 | |
10732 #endif | |
10733 #ifndef SEEK_END | |
10734 # define SEEK_END 2 | |
10735 #endif | |
10736 | |
10737 /* | |
10738 * Get the stdout of an external command. | |
10739 * Returns an allocated string, or NULL for error. | |
10740 */ | |
10741 char_u * | |
24 | 10742 get_cmd_output(cmd, infile, flags) |
7 | 10743 char_u *cmd; |
24 | 10744 char_u *infile; /* optional input file name */ |
7 | 10745 int flags; /* can be SHELL_SILENT */ |
10746 { | |
10747 char_u *tempname; | |
10748 char_u *command; | |
10749 char_u *buffer = NULL; | |
10750 int len; | |
10751 int i = 0; | |
10752 FILE *fd; | |
10753 | |
10754 if (check_restricted() || check_secure()) | |
10755 return NULL; | |
10756 | |
10757 /* get a name for the temp file */ | |
10758 if ((tempname = vim_tempname('o')) == NULL) | |
10759 { | |
10760 EMSG(_(e_notmp)); | |
10761 return NULL; | |
10762 } | |
10763 | |
10764 /* Add the redirection stuff */ | |
24 | 10765 command = make_filter_cmd(cmd, infile, tempname); |
7 | 10766 if (command == NULL) |
10767 goto done; | |
10768 | |
10769 /* | |
10770 * Call the shell to execute the command (errors are ignored). | |
10771 * Don't check timestamps here. | |
10772 */ | |
10773 ++no_check_timestamps; | |
10774 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); | |
10775 --no_check_timestamps; | |
10776 | |
10777 vim_free(command); | |
10778 | |
10779 /* | |
10780 * read the names from the file into memory | |
10781 */ | |
10782 # ifdef VMS | |
1224 | 10783 /* created temporary file is not always readable as binary */ |
7 | 10784 fd = mch_fopen((char *)tempname, "r"); |
10785 # else | |
10786 fd = mch_fopen((char *)tempname, READBIN); | |
10787 # endif | |
10788 | |
10789 if (fd == NULL) | |
10790 { | |
10791 EMSG2(_(e_notopen), tempname); | |
10792 goto done; | |
10793 } | |
10794 | |
10795 fseek(fd, 0L, SEEK_END); | |
10796 len = ftell(fd); /* get size of temp file */ | |
10797 fseek(fd, 0L, SEEK_SET); | |
10798 | |
10799 buffer = alloc(len + 1); | |
10800 if (buffer != NULL) | |
10801 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); | |
10802 fclose(fd); | |
10803 mch_remove(tempname); | |
10804 if (buffer == NULL) | |
10805 goto done; | |
10806 #ifdef VMS | |
10807 len = i; /* VMS doesn't give us what we asked for... */ | |
10808 #endif | |
10809 if (i != len) | |
10810 { | |
10811 EMSG2(_(e_notread), tempname); | |
10812 vim_free(buffer); | |
10813 buffer = NULL; | |
10814 } | |
10815 else | |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10816 buffer[len] = NUL; /* make sure the buffer is terminated */ |
7 | 10817 |
10818 done: | |
10819 vim_free(tempname); | |
10820 return buffer; | |
10821 } | |
10822 #endif | |
10823 | |
10824 /* | |
10825 * Free the list of files returned by expand_wildcards() or other expansion | |
10826 * functions. | |
10827 */ | |
10828 void | |
10829 FreeWild(count, files) | |
10830 int count; | |
10831 char_u **files; | |
10832 { | |
838 | 10833 if (count <= 0 || files == NULL) |
7 | 10834 return; |
10835 #if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */ | |
10836 /* | |
10837 * Is this still OK for when other functions than expand_wildcards() have | |
10838 * been used??? | |
10839 */ | |
10840 _fnexplodefree((char **)files); | |
10841 #else | |
10842 while (count--) | |
10843 vim_free(files[count]); | |
10844 vim_free(files); | |
10845 #endif | |
10846 } | |
10847 | |
10848 /* | |
2302
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2297
diff
changeset
|
10849 * Return TRUE when need to go to Insert mode because of 'insertmode'. |
7 | 10850 * Don't do this when still processing a command or a mapping. |
10851 * Don't do this when inside a ":normal" command. | |
10852 */ | |
10853 int | |
10854 goto_im() | |
10855 { | |
10856 return (p_im && stuff_empty() && typebuf_typed()); | |
10857 } |