Mercurial > vim
annotate src/misc1.c @ 2823:8bd38abda314 v7.3.187
updated for version 7.3.187
Problem: The RISC OS port has obvious errors and is not being maintained.
Solution: Remove the RISC OS files and code.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Tue, 10 May 2011 16:41:25 +0200 |
parents | 21346361ce6c |
children | 8dbea5adc937 |
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 | |
21 /* | |
22 * Count the size (in window cells) of the indent in the current line. | |
23 */ | |
24 int | |
25 get_indent() | |
26 { | |
27 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts); | |
28 } | |
29 | |
30 /* | |
31 * Count the size (in window cells) of the indent in line "lnum". | |
32 */ | |
33 int | |
34 get_indent_lnum(lnum) | |
35 linenr_T lnum; | |
36 { | |
37 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts); | |
38 } | |
39 | |
40 #if defined(FEAT_FOLDING) || defined(PROTO) | |
41 /* | |
42 * Count the size (in window cells) of the indent in line "lnum" of buffer | |
43 * "buf". | |
44 */ | |
45 int | |
46 get_indent_buf(buf, lnum) | |
47 buf_T *buf; | |
48 linenr_T lnum; | |
49 { | |
50 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts); | |
51 } | |
52 #endif | |
53 | |
54 /* | |
55 * count the size (in window cells) of the indent in line "ptr", with | |
56 * 'tabstop' at "ts" | |
57 */ | |
164 | 58 int |
7 | 59 get_indent_str(ptr, ts) |
60 char_u *ptr; | |
61 int ts; | |
62 { | |
63 int count = 0; | |
64 | |
65 for ( ; *ptr; ++ptr) | |
66 { | |
67 if (*ptr == TAB) /* count a tab for what it is worth */ | |
68 count += ts - (count % ts); | |
69 else if (*ptr == ' ') | |
70 ++count; /* count a space for one */ | |
71 else | |
72 break; | |
73 } | |
164 | 74 return count; |
7 | 75 } |
76 | |
77 /* | |
78 * Set the indent of the current line. | |
79 * Leaves the cursor on the first non-blank in the line. | |
80 * Caller must take care of undo. | |
81 * "flags": | |
82 * SIN_CHANGED: call changed_bytes() if the line was changed. | |
83 * SIN_INSERT: insert the indent in front of the line. | |
84 * SIN_UNDO: save line for undo before changing it. | |
85 * Returns TRUE if the line was changed. | |
86 */ | |
87 int | |
88 set_indent(size, flags) | |
1324 | 89 int size; /* measured in spaces */ |
7 | 90 int flags; |
91 { | |
92 char_u *p; | |
93 char_u *newline; | |
94 char_u *oldline; | |
95 char_u *s; | |
96 int todo; | |
1324 | 97 int ind_len; /* measured in characters */ |
7 | 98 int line_len; |
99 int doit = FALSE; | |
1324 | 100 int ind_done = 0; /* measured in spaces */ |
7 | 101 int tab_pad; |
217 | 102 int retval = FALSE; |
1359 | 103 int orig_char_len = -1; /* number of initial whitespace chars when |
1324 | 104 'et' and 'pi' are both set */ |
7 | 105 |
106 /* | |
107 * First check if there is anything to do and compute the number of | |
108 * characters needed for the indent. | |
109 */ | |
110 todo = size; | |
111 ind_len = 0; | |
112 p = oldline = ml_get_curline(); | |
113 | |
114 /* Calculate the buffer size for the new indent, and check to see if it | |
115 * isn't already set */ | |
116 | |
1324 | 117 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and |
118 * 'preserveindent' are set count the number of characters at the | |
119 * beginning of the line to be copied */ | |
120 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) | |
7 | 121 { |
122 /* If 'preserveindent' is set then reuse as much as possible of | |
123 * the existing indent structure for the new indent */ | |
124 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
125 { | |
126 ind_done = 0; | |
127 | |
128 /* count as many characters as we can use */ | |
129 while (todo > 0 && vim_iswhite(*p)) | |
130 { | |
131 if (*p == TAB) | |
132 { | |
133 tab_pad = (int)curbuf->b_p_ts | |
134 - (ind_done % (int)curbuf->b_p_ts); | |
135 /* stop if this tab will overshoot the target */ | |
136 if (todo < tab_pad) | |
137 break; | |
138 todo -= tab_pad; | |
139 ++ind_len; | |
140 ind_done += tab_pad; | |
141 } | |
142 else | |
143 { | |
144 --todo; | |
145 ++ind_len; | |
146 ++ind_done; | |
147 } | |
148 ++p; | |
149 } | |
150 | |
1324 | 151 /* Set initial number of whitespace chars to copy if we are |
152 * preserving indent but expandtab is set */ | |
153 if (curbuf->b_p_et) | |
154 orig_char_len = ind_len; | |
155 | |
7 | 156 /* Fill to next tabstop with a tab, if possible */ |
157 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
1359 | 158 if (todo >= tab_pad && orig_char_len == -1) |
7 | 159 { |
160 doit = TRUE; | |
161 todo -= tab_pad; | |
162 ++ind_len; | |
163 /* ind_done += tab_pad; */ | |
164 } | |
165 } | |
166 | |
167 /* count tabs required for indent */ | |
168 while (todo >= (int)curbuf->b_p_ts) | |
169 { | |
170 if (*p != TAB) | |
171 doit = TRUE; | |
172 else | |
173 ++p; | |
174 todo -= (int)curbuf->b_p_ts; | |
175 ++ind_len; | |
176 /* ind_done += (int)curbuf->b_p_ts; */ | |
177 } | |
178 } | |
179 /* count spaces required for indent */ | |
180 while (todo > 0) | |
181 { | |
182 if (*p != ' ') | |
183 doit = TRUE; | |
184 else | |
185 ++p; | |
186 --todo; | |
187 ++ind_len; | |
188 /* ++ind_done; */ | |
189 } | |
190 | |
191 /* Return if the indent is OK already. */ | |
192 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT)) | |
193 return FALSE; | |
194 | |
195 /* Allocate memory for the new line. */ | |
196 if (flags & SIN_INSERT) | |
197 p = oldline; | |
198 else | |
199 p = skipwhite(p); | |
200 line_len = (int)STRLEN(p) + 1; | |
1324 | 201 |
202 /* If 'preserveindent' and 'expandtab' are both set keep the original | |
203 * characters and allocate accordingly. We will fill the rest with spaces | |
204 * after the if (!curbuf->b_p_et) below. */ | |
1359 | 205 if (orig_char_len != -1) |
1324 | 206 { |
207 newline = alloc(orig_char_len + size - ind_done + line_len); | |
208 if (newline == NULL) | |
209 return FALSE; | |
1359 | 210 todo = size - ind_done; |
211 ind_len = orig_char_len + todo; /* Set total length of indent in | |
212 * characters, which may have been | |
213 * undercounted until now */ | |
1324 | 214 p = oldline; |
215 s = newline; | |
216 while (orig_char_len > 0) | |
217 { | |
218 *s++ = *p++; | |
219 orig_char_len--; | |
220 } | |
1474 | 221 |
1324 | 222 /* Skip over any additional white space (useful when newindent is less |
223 * than old) */ | |
224 while (vim_iswhite(*p)) | |
1474 | 225 ++p; |
1348 | 226 |
1324 | 227 } |
228 else | |
229 { | |
230 todo = size; | |
231 newline = alloc(ind_len + line_len); | |
232 if (newline == NULL) | |
233 return FALSE; | |
234 s = newline; | |
235 } | |
7 | 236 |
237 /* Put the characters in the new line. */ | |
238 /* if 'expandtab' isn't set: use TABs */ | |
239 if (!curbuf->b_p_et) | |
240 { | |
241 /* If 'preserveindent' is set then reuse as much as possible of | |
242 * the existing indent structure for the new indent */ | |
243 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
244 { | |
245 p = oldline; | |
246 ind_done = 0; | |
247 | |
248 while (todo > 0 && vim_iswhite(*p)) | |
249 { | |
250 if (*p == TAB) | |
251 { | |
252 tab_pad = (int)curbuf->b_p_ts | |
253 - (ind_done % (int)curbuf->b_p_ts); | |
254 /* stop if this tab will overshoot the target */ | |
255 if (todo < tab_pad) | |
256 break; | |
257 todo -= tab_pad; | |
258 ind_done += tab_pad; | |
259 } | |
260 else | |
261 { | |
262 --todo; | |
263 ++ind_done; | |
264 } | |
265 *s++ = *p++; | |
266 } | |
267 | |
268 /* Fill to next tabstop with a tab, if possible */ | |
269 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
270 if (todo >= tab_pad) | |
271 { | |
272 *s++ = TAB; | |
273 todo -= tab_pad; | |
274 } | |
275 | |
276 p = skipwhite(p); | |
277 } | |
278 | |
279 while (todo >= (int)curbuf->b_p_ts) | |
280 { | |
281 *s++ = TAB; | |
282 todo -= (int)curbuf->b_p_ts; | |
283 } | |
284 } | |
285 while (todo > 0) | |
286 { | |
287 *s++ = ' '; | |
288 --todo; | |
289 } | |
290 mch_memmove(s, p, (size_t)line_len); | |
291 | |
292 /* Replace the line (unless undo fails). */ | |
293 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) | |
294 { | |
295 ml_replace(curwin->w_cursor.lnum, newline, FALSE); | |
296 if (flags & SIN_CHANGED) | |
297 changed_bytes(curwin->w_cursor.lnum, 0); | |
298 /* Correct saved cursor position if it's after the indent. */ | |
299 if (saved_cursor.lnum == curwin->w_cursor.lnum | |
300 && saved_cursor.col >= (colnr_T)(p - oldline)) | |
835 | 301 saved_cursor.col += ind_len - (colnr_T)(p - oldline); |
217 | 302 retval = TRUE; |
7 | 303 } |
304 else | |
305 vim_free(newline); | |
306 | |
307 curwin->w_cursor.col = ind_len; | |
217 | 308 return retval; |
7 | 309 } |
310 | |
311 /* | |
312 * Copy the indent from ptr to the current line (and fill to size) | |
313 * Leaves the cursor on the first non-blank in the line. | |
314 * Returns TRUE if the line was changed. | |
315 */ | |
316 static int | |
317 copy_indent(size, src) | |
318 int size; | |
319 char_u *src; | |
320 { | |
321 char_u *p = NULL; | |
322 char_u *line = NULL; | |
323 char_u *s; | |
324 int todo; | |
325 int ind_len; | |
326 int line_len = 0; | |
327 int tab_pad; | |
328 int ind_done; | |
329 int round; | |
330 | |
331 /* Round 1: compute the number of characters needed for the indent | |
332 * Round 2: copy the characters. */ | |
333 for (round = 1; round <= 2; ++round) | |
334 { | |
335 todo = size; | |
336 ind_len = 0; | |
337 ind_done = 0; | |
338 s = src; | |
339 | |
340 /* Count/copy the usable portion of the source line */ | |
341 while (todo > 0 && vim_iswhite(*s)) | |
342 { | |
343 if (*s == TAB) | |
344 { | |
345 tab_pad = (int)curbuf->b_p_ts | |
346 - (ind_done % (int)curbuf->b_p_ts); | |
347 /* Stop if this tab will overshoot the target */ | |
348 if (todo < tab_pad) | |
349 break; | |
350 todo -= tab_pad; | |
351 ind_done += tab_pad; | |
352 } | |
353 else | |
354 { | |
355 --todo; | |
356 ++ind_done; | |
357 } | |
358 ++ind_len; | |
840 | 359 if (p != NULL) |
7 | 360 *p++ = *s; |
361 ++s; | |
362 } | |
363 | |
364 /* Fill to next tabstop with a tab, if possible */ | |
365 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
366 if (todo >= tab_pad) | |
367 { | |
368 todo -= tab_pad; | |
369 ++ind_len; | |
840 | 370 if (p != NULL) |
7 | 371 *p++ = TAB; |
372 } | |
373 | |
374 /* Add tabs required for indent */ | |
375 while (todo >= (int)curbuf->b_p_ts) | |
376 { | |
377 todo -= (int)curbuf->b_p_ts; | |
378 ++ind_len; | |
840 | 379 if (p != NULL) |
7 | 380 *p++ = TAB; |
381 } | |
382 | |
383 /* Count/add spaces required for indent */ | |
384 while (todo > 0) | |
385 { | |
386 --todo; | |
387 ++ind_len; | |
840 | 388 if (p != NULL) |
7 | 389 *p++ = ' '; |
390 } | |
391 | |
840 | 392 if (p == NULL) |
7 | 393 { |
394 /* Allocate memory for the result: the copied indent, new indent | |
395 * and the rest of the line. */ | |
396 line_len = (int)STRLEN(ml_get_curline()) + 1; | |
397 line = alloc(ind_len + line_len); | |
398 if (line == NULL) | |
399 return FALSE; | |
400 p = line; | |
401 } | |
402 } | |
403 | |
404 /* Append the original line */ | |
405 mch_memmove(p, ml_get_curline(), (size_t)line_len); | |
406 | |
407 /* Replace the line */ | |
408 ml_replace(curwin->w_cursor.lnum, line, FALSE); | |
409 | |
410 /* Put the cursor after the indent. */ | |
411 curwin->w_cursor.col = ind_len; | |
412 return TRUE; | |
413 } | |
414 | |
415 /* | |
416 * Return the indent of the current line after a number. Return -1 if no | |
417 * number was found. Used for 'n' in 'formatoptions': numbered list. | |
41 | 418 * Since a pattern is used it can actually handle more than numbers. |
7 | 419 */ |
420 int | |
421 get_number_indent(lnum) | |
422 linenr_T lnum; | |
423 { | |
424 colnr_T col; | |
425 pos_T pos; | |
41 | 426 regmmatch_T regmatch; |
7 | 427 |
428 if (lnum > curbuf->b_ml.ml_line_count) | |
429 return -1; | |
41 | 430 pos.lnum = 0; |
431 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); | |
432 if (regmatch.regprog != NULL) | |
433 { | |
434 regmatch.rmm_ic = FALSE; | |
410 | 435 regmatch.rmm_maxcol = 0; |
1521 | 436 if (vim_regexec_multi(®match, curwin, curbuf, lnum, |
437 (colnr_T)0, NULL)) | |
41 | 438 { |
439 pos.lnum = regmatch.endpos[0].lnum + lnum; | |
440 pos.col = regmatch.endpos[0].col; | |
441 #ifdef FEAT_VIRTUALEDIT | |
442 pos.coladd = 0; | |
443 #endif | |
444 } | |
445 vim_free(regmatch.regprog); | |
446 } | |
447 | |
448 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) | |
7 | 449 return -1; |
450 getvcol(curwin, &pos, &col, NULL, NULL); | |
451 return (int)col; | |
452 } | |
453 | |
454 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) | |
455 | |
456 static int cin_is_cinword __ARGS((char_u *line)); | |
457 | |
458 /* | |
459 * Return TRUE if the string "line" starts with a word from 'cinwords'. | |
460 */ | |
461 static int | |
462 cin_is_cinword(line) | |
463 char_u *line; | |
464 { | |
465 char_u *cinw; | |
466 char_u *cinw_buf; | |
467 int cinw_len; | |
468 int retval = FALSE; | |
469 int len; | |
470 | |
471 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; | |
472 cinw_buf = alloc((unsigned)cinw_len); | |
473 if (cinw_buf != NULL) | |
474 { | |
475 line = skipwhite(line); | |
476 for (cinw = curbuf->b_p_cinw; *cinw; ) | |
477 { | |
478 len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); | |
479 if (STRNCMP(line, cinw_buf, len) == 0 | |
480 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) | |
481 { | |
482 retval = TRUE; | |
483 break; | |
484 } | |
485 } | |
486 vim_free(cinw_buf); | |
487 } | |
488 return retval; | |
489 } | |
490 #endif | |
491 | |
492 /* | |
493 * open_line: Add a new line below or above the current line. | |
494 * | |
495 * For VREPLACE mode, we only add a new line when we get to the end of the | |
496 * file, otherwise we just start replacing the next line. | |
497 * | |
498 * Caller must take care of undo. Since VREPLACE may affect any number of | |
499 * lines however, it may call u_save_cursor() again when starting to change a | |
500 * new line. | |
501 * "flags": OPENLINE_DELSPACES delete spaces after cursor | |
502 * OPENLINE_DO_COM format comments | |
503 * OPENLINE_KEEPTRAIL keep trailing spaces | |
504 * OPENLINE_MARKFIX adjust mark positions after the line break | |
505 * | |
506 * Return TRUE for success, FALSE for failure | |
507 */ | |
508 int | |
509 open_line(dir, flags, old_indent) | |
510 int dir; /* FORWARD or BACKWARD */ | |
511 int flags; | |
512 int old_indent; /* indent for after ^^D in Insert mode */ | |
513 { | |
514 char_u *saved_line; /* copy of the original line */ | |
515 char_u *next_line = NULL; /* copy of the next line */ | |
516 char_u *p_extra = NULL; /* what goes to next line */ | |
517 int less_cols = 0; /* less columns for mark in new line */ | |
518 int less_cols_off = 0; /* columns to skip for mark adjust */ | |
519 pos_T old_cursor; /* old cursor position */ | |
520 int newcol = 0; /* new cursor column */ | |
521 int newindent = 0; /* auto-indent of the new line */ | |
522 int n; | |
523 int trunc_line = FALSE; /* truncate current line afterwards */ | |
524 int retval = FALSE; /* return value, default is FAIL */ | |
525 #ifdef FEAT_COMMENTS | |
526 int extra_len = 0; /* length of p_extra string */ | |
527 int lead_len; /* length of comment leader */ | |
528 char_u *lead_flags; /* position in 'comments' for comment leader */ | |
529 char_u *leader = NULL; /* copy of comment leader */ | |
530 #endif | |
531 char_u *allocated = NULL; /* allocated memory */ | |
532 #if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \ | |
533 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS) | |
534 char_u *p; | |
535 #endif | |
536 int saved_char = NUL; /* init for GCC */ | |
537 #if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS) | |
538 pos_T *pos; | |
539 #endif | |
540 #ifdef FEAT_SMARTINDENT | |
541 int do_si = (!p_paste && curbuf->b_p_si | |
542 # ifdef FEAT_CINDENT | |
543 && !curbuf->b_p_cin | |
544 # endif | |
545 ); | |
546 int no_si = FALSE; /* reset did_si afterwards */ | |
547 int first_char = NUL; /* init for GCC */ | |
548 #endif | |
549 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
550 int vreplace_mode; | |
551 #endif | |
552 int did_append; /* appended a new line */ | |
553 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */ | |
554 | |
555 /* | |
556 * make a copy of the current line so we can mess with it | |
557 */ | |
558 saved_line = vim_strsave(ml_get_curline()); | |
559 if (saved_line == NULL) /* out of memory! */ | |
560 return FALSE; | |
561 | |
562 #ifdef FEAT_VREPLACE | |
563 if (State & VREPLACE_FLAG) | |
564 { | |
565 /* | |
566 * With VREPLACE we make a copy of the next line, which we will be | |
567 * starting to replace. First make the new line empty and let vim play | |
568 * with the indenting and comment leader to its heart's content. Then | |
569 * we grab what it ended up putting on the new line, put back the | |
570 * original line, and call ins_char() to put each new character onto | |
571 * the line, replacing what was there before and pushing the right | |
572 * stuff onto the replace stack. -- webb. | |
573 */ | |
574 if (curwin->w_cursor.lnum < orig_line_count) | |
575 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); | |
576 else | |
577 next_line = vim_strsave((char_u *)""); | |
578 if (next_line == NULL) /* out of memory! */ | |
579 goto theend; | |
580 | |
581 /* | |
582 * In VREPLACE mode, a NL replaces the rest of the line, and starts | |
583 * replacing the next line, so push all of the characters left on the | |
584 * line onto the replace stack. We'll push any other characters that | |
585 * might be replaced at the start of the next line (due to autoindent | |
586 * etc) a bit later. | |
587 */ | |
588 replace_push(NUL); /* Call twice because BS over NL expects it */ | |
589 replace_push(NUL); | |
590 p = saved_line + curwin->w_cursor.col; | |
591 while (*p != NUL) | |
1470 | 592 { |
593 #ifdef FEAT_MBYTE | |
594 if (has_mbyte) | |
595 p += replace_push_mb(p); | |
596 else | |
597 #endif | |
598 replace_push(*p++); | |
599 } | |
7 | 600 saved_line[curwin->w_cursor.col] = NUL; |
601 } | |
602 #endif | |
603 | |
604 if ((State & INSERT) | |
605 #ifdef FEAT_VREPLACE | |
606 && !(State & VREPLACE_FLAG) | |
607 #endif | |
608 ) | |
609 { | |
610 p_extra = saved_line + curwin->w_cursor.col; | |
611 #ifdef FEAT_SMARTINDENT | |
612 if (do_si) /* need first char after new line break */ | |
613 { | |
614 p = skipwhite(p_extra); | |
615 first_char = *p; | |
616 } | |
617 #endif | |
618 #ifdef FEAT_COMMENTS | |
619 extra_len = (int)STRLEN(p_extra); | |
620 #endif | |
621 saved_char = *p_extra; | |
622 *p_extra = NUL; | |
623 } | |
624 | |
625 u_clearline(); /* cannot do "U" command when adding lines */ | |
626 #ifdef FEAT_SMARTINDENT | |
627 did_si = FALSE; | |
628 #endif | |
629 ai_col = 0; | |
630 | |
631 /* | |
632 * If we just did an auto-indent, then we didn't type anything on | |
633 * the prior line, and it should be truncated. Do this even if 'ai' is not | |
634 * set because automatically inserting a comment leader also sets did_ai. | |
635 */ | |
636 if (dir == FORWARD && did_ai) | |
637 trunc_line = TRUE; | |
638 | |
639 /* | |
640 * If 'autoindent' and/or 'smartindent' is set, try to figure out what | |
641 * indent to use for the new line. | |
642 */ | |
643 if (curbuf->b_p_ai | |
644 #ifdef FEAT_SMARTINDENT | |
645 || do_si | |
646 #endif | |
647 ) | |
648 { | |
649 /* | |
650 * count white space on current line | |
651 */ | |
652 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts); | |
653 if (newindent == 0) | |
654 newindent = old_indent; /* for ^^D command in insert mode */ | |
655 | |
656 #ifdef FEAT_SMARTINDENT | |
657 /* | |
658 * Do smart indenting. | |
659 * In insert/replace mode (only when dir == FORWARD) | |
660 * we may move some text to the next line. If it starts with '{' | |
661 * don't add an indent. Fixes inserting a NL before '{' in line | |
662 * "if (condition) {" | |
663 */ | |
664 if (!trunc_line && do_si && *saved_line != NUL | |
665 && (p_extra == NULL || first_char != '{')) | |
666 { | |
667 char_u *ptr; | |
668 char_u last_char; | |
669 | |
670 old_cursor = curwin->w_cursor; | |
671 ptr = saved_line; | |
672 # ifdef FEAT_COMMENTS | |
673 if (flags & OPENLINE_DO_COM) | |
674 lead_len = get_leader_len(ptr, NULL, FALSE); | |
675 else | |
676 lead_len = 0; | |
677 # endif | |
678 if (dir == FORWARD) | |
679 { | |
680 /* | |
681 * Skip preprocessor directives, unless they are | |
682 * recognised as comments. | |
683 */ | |
684 if ( | |
685 # ifdef FEAT_COMMENTS | |
686 lead_len == 0 && | |
687 # endif | |
688 ptr[0] == '#') | |
689 { | |
690 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) | |
691 ptr = ml_get(--curwin->w_cursor.lnum); | |
692 newindent = get_indent(); | |
693 } | |
694 # ifdef FEAT_COMMENTS | |
695 if (flags & OPENLINE_DO_COM) | |
696 lead_len = get_leader_len(ptr, NULL, FALSE); | |
697 else | |
698 lead_len = 0; | |
699 if (lead_len > 0) | |
700 { | |
701 /* | |
702 * This case gets the following right: | |
703 * \* | |
704 * * A comment (read '\' as '/'). | |
705 * *\ | |
706 * #define IN_THE_WAY | |
707 * This should line up here; | |
708 */ | |
709 p = skipwhite(ptr); | |
710 if (p[0] == '/' && p[1] == '*') | |
711 p++; | |
712 if (p[0] == '*') | |
713 { | |
714 for (p++; *p; p++) | |
715 { | |
716 if (p[0] == '/' && p[-1] == '*') | |
717 { | |
718 /* | |
719 * End of C comment, indent should line up | |
720 * with the line containing the start of | |
721 * the comment | |
722 */ | |
723 curwin->w_cursor.col = (colnr_T)(p - ptr); | |
724 if ((pos = findmatch(NULL, NUL)) != NULL) | |
725 { | |
726 curwin->w_cursor.lnum = pos->lnum; | |
727 newindent = get_indent(); | |
728 } | |
729 } | |
730 } | |
731 } | |
732 } | |
733 else /* Not a comment line */ | |
734 # endif | |
735 { | |
736 /* Find last non-blank in line */ | |
737 p = ptr + STRLEN(ptr) - 1; | |
738 while (p > ptr && vim_iswhite(*p)) | |
739 --p; | |
740 last_char = *p; | |
741 | |
742 /* | |
743 * find the character just before the '{' or ';' | |
744 */ | |
745 if (last_char == '{' || last_char == ';') | |
746 { | |
747 if (p > ptr) | |
748 --p; | |
749 while (p > ptr && vim_iswhite(*p)) | |
750 --p; | |
751 } | |
752 /* | |
753 * Try to catch lines that are split over multiple | |
754 * lines. eg: | |
755 * if (condition && | |
756 * condition) { | |
757 * Should line up here! | |
758 * } | |
759 */ | |
760 if (*p == ')') | |
761 { | |
762 curwin->w_cursor.col = (colnr_T)(p - ptr); | |
763 if ((pos = findmatch(NULL, '(')) != NULL) | |
764 { | |
765 curwin->w_cursor.lnum = pos->lnum; | |
766 newindent = get_indent(); | |
767 ptr = ml_get_curline(); | |
768 } | |
769 } | |
770 /* | |
771 * If last character is '{' do indent, without | |
772 * checking for "if" and the like. | |
773 */ | |
774 if (last_char == '{') | |
775 { | |
776 did_si = TRUE; /* do indent */ | |
777 no_si = TRUE; /* don't delete it when '{' typed */ | |
778 } | |
779 /* | |
780 * Look for "if" and the like, use 'cinwords'. | |
781 * Don't do this if the previous line ended in ';' or | |
782 * '}'. | |
783 */ | |
784 else if (last_char != ';' && last_char != '}' | |
785 && cin_is_cinword(ptr)) | |
786 did_si = TRUE; | |
787 } | |
788 } | |
789 else /* dir == BACKWARD */ | |
790 { | |
791 /* | |
792 * Skip preprocessor directives, unless they are | |
793 * recognised as comments. | |
794 */ | |
795 if ( | |
796 # ifdef FEAT_COMMENTS | |
797 lead_len == 0 && | |
798 # endif | |
799 ptr[0] == '#') | |
800 { | |
801 int was_backslashed = FALSE; | |
802 | |
803 while ((ptr[0] == '#' || was_backslashed) && | |
804 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) | |
805 { | |
806 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') | |
807 was_backslashed = TRUE; | |
808 else | |
809 was_backslashed = FALSE; | |
810 ptr = ml_get(++curwin->w_cursor.lnum); | |
811 } | |
812 if (was_backslashed) | |
813 newindent = 0; /* Got to end of file */ | |
814 else | |
815 newindent = get_indent(); | |
816 } | |
817 p = skipwhite(ptr); | |
818 if (*p == '}') /* if line starts with '}': do indent */ | |
819 did_si = TRUE; | |
820 else /* can delete indent when '{' typed */ | |
821 can_si_back = TRUE; | |
822 } | |
823 curwin->w_cursor = old_cursor; | |
824 } | |
825 if (do_si) | |
826 can_si = TRUE; | |
827 #endif /* FEAT_SMARTINDENT */ | |
828 | |
829 did_ai = TRUE; | |
830 } | |
831 | |
832 #ifdef FEAT_COMMENTS | |
833 /* | |
834 * Find out if the current line starts with a comment leader. | |
835 * This may then be inserted in front of the new line. | |
836 */ | |
837 end_comment_pending = NUL; | |
838 if (flags & OPENLINE_DO_COM) | |
839 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD); | |
840 else | |
841 lead_len = 0; | |
842 if (lead_len > 0) | |
843 { | |
844 char_u *lead_repl = NULL; /* replaces comment leader */ | |
845 int lead_repl_len = 0; /* length of *lead_repl */ | |
846 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ | |
847 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ | |
848 char_u *comment_end = NULL; /* where lead_end has been found */ | |
849 int extra_space = FALSE; /* append extra space */ | |
850 int current_flag; | |
851 int require_blank = FALSE; /* requires blank after middle */ | |
852 char_u *p2; | |
853 | |
854 /* | |
855 * If the comment leader has the start, middle or end flag, it may not | |
856 * be used or may be replaced with the middle leader. | |
857 */ | |
858 for (p = lead_flags; *p && *p != ':'; ++p) | |
859 { | |
860 if (*p == COM_BLANK) | |
861 { | |
862 require_blank = TRUE; | |
863 continue; | |
864 } | |
865 if (*p == COM_START || *p == COM_MIDDLE) | |
866 { | |
867 current_flag = *p; | |
868 if (*p == COM_START) | |
869 { | |
870 /* | |
871 * Doing "O" on a start of comment does not insert leader. | |
872 */ | |
873 if (dir == BACKWARD) | |
874 { | |
875 lead_len = 0; | |
876 break; | |
877 } | |
878 | |
879 /* find start of middle part */ | |
880 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); | |
881 require_blank = FALSE; | |
882 } | |
883 | |
884 /* | |
885 * Isolate the strings of the middle and end leader. | |
886 */ | |
887 while (*p && p[-1] != ':') /* find end of middle flags */ | |
888 { | |
889 if (*p == COM_BLANK) | |
890 require_blank = TRUE; | |
891 ++p; | |
892 } | |
893 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); | |
894 | |
895 while (*p && p[-1] != ':') /* find end of end flags */ | |
896 { | |
897 /* Check whether we allow automatic ending of comments */ | |
898 if (*p == COM_AUTO_END) | |
899 end_comment_pending = -1; /* means we want to set it */ | |
900 ++p; | |
901 } | |
902 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
903 | |
904 if (end_comment_pending == -1) /* we can set it now */ | |
905 end_comment_pending = lead_end[n - 1]; | |
906 | |
907 /* | |
908 * If the end of the comment is in the same line, don't use | |
909 * the comment leader. | |
910 */ | |
911 if (dir == FORWARD) | |
912 { | |
913 for (p = saved_line + lead_len; *p; ++p) | |
914 if (STRNCMP(p, lead_end, n) == 0) | |
915 { | |
916 comment_end = p; | |
917 lead_len = 0; | |
918 break; | |
919 } | |
920 } | |
921 | |
922 /* | |
923 * Doing "o" on a start of comment inserts the middle leader. | |
924 */ | |
925 if (lead_len > 0) | |
926 { | |
927 if (current_flag == COM_START) | |
928 { | |
929 lead_repl = lead_middle; | |
930 lead_repl_len = (int)STRLEN(lead_middle); | |
931 } | |
932 | |
933 /* | |
934 * If we have hit RETURN immediately after the start | |
935 * comment leader, then put a space after the middle | |
936 * comment leader on the next line. | |
937 */ | |
938 if (!vim_iswhite(saved_line[lead_len - 1]) | |
939 && ((p_extra != NULL | |
940 && (int)curwin->w_cursor.col == lead_len) | |
941 || (p_extra == NULL | |
942 && saved_line[lead_len] == NUL) | |
943 || require_blank)) | |
944 extra_space = TRUE; | |
945 } | |
946 break; | |
947 } | |
948 if (*p == COM_END) | |
949 { | |
950 /* | |
951 * Doing "o" on the end of a comment does not insert leader. | |
952 * Remember where the end is, might want to use it to find the | |
953 * start (for C-comments). | |
954 */ | |
955 if (dir == FORWARD) | |
956 { | |
957 comment_end = skipwhite(saved_line); | |
958 lead_len = 0; | |
959 break; | |
960 } | |
961 | |
962 /* | |
963 * Doing "O" on the end of a comment inserts the middle leader. | |
964 * Find the string for the middle leader, searching backwards. | |
965 */ | |
966 while (p > curbuf->b_p_com && *p != ',') | |
967 --p; | |
968 for (lead_repl = p; lead_repl > curbuf->b_p_com | |
969 && lead_repl[-1] != ':'; --lead_repl) | |
970 ; | |
971 lead_repl_len = (int)(p - lead_repl); | |
972 | |
973 /* We can probably always add an extra space when doing "O" on | |
974 * the comment-end */ | |
975 extra_space = TRUE; | |
976 | |
977 /* Check whether we allow automatic ending of comments */ | |
978 for (p2 = p; *p2 && *p2 != ':'; p2++) | |
979 { | |
980 if (*p2 == COM_AUTO_END) | |
981 end_comment_pending = -1; /* means we want to set it */ | |
982 } | |
983 if (end_comment_pending == -1) | |
984 { | |
985 /* Find last character in end-comment string */ | |
986 while (*p2 && *p2 != ',') | |
987 p2++; | |
988 end_comment_pending = p2[-1]; | |
989 } | |
990 break; | |
991 } | |
992 if (*p == COM_FIRST) | |
993 { | |
994 /* | |
995 * Comment leader for first line only: Don't repeat leader | |
996 * when using "O", blank out leader when using "o". | |
997 */ | |
998 if (dir == BACKWARD) | |
999 lead_len = 0; | |
1000 else | |
1001 { | |
1002 lead_repl = (char_u *)""; | |
1003 lead_repl_len = 0; | |
1004 } | |
1005 break; | |
1006 } | |
1007 } | |
1008 if (lead_len) | |
1009 { | |
1010 /* allocate buffer (may concatenate p_exta later) */ | |
1011 leader = alloc(lead_len + lead_repl_len + extra_space + | |
1012 extra_len + 1); | |
1013 allocated = leader; /* remember to free it later */ | |
1014 | |
1015 if (leader == NULL) | |
1016 lead_len = 0; | |
1017 else | |
1018 { | |
419 | 1019 vim_strncpy(leader, saved_line, lead_len); |
7 | 1020 |
1021 /* | |
1022 * Replace leader with lead_repl, right or left adjusted | |
1023 */ | |
1024 if (lead_repl != NULL) | |
1025 { | |
1026 int c = 0; | |
1027 int off = 0; | |
1028 | |
1996 | 1029 for (p = lead_flags; *p != NUL && *p != ':'; ) |
7 | 1030 { |
1031 if (*p == COM_RIGHT || *p == COM_LEFT) | |
1996 | 1032 c = *p++; |
7 | 1033 else if (VIM_ISDIGIT(*p) || *p == '-') |
1034 off = getdigits(&p); | |
1996 | 1035 else |
1036 ++p; | |
7 | 1037 } |
1038 if (c == COM_RIGHT) /* right adjusted leader */ | |
1039 { | |
1040 /* find last non-white in the leader to line up with */ | |
1041 for (p = leader + lead_len - 1; p > leader | |
1042 && vim_iswhite(*p); --p) | |
1043 ; | |
1044 ++p; | |
17 | 1045 |
1046 #ifdef FEAT_MBYTE | |
1047 /* Compute the length of the replaced characters in | |
1048 * screen characters, not bytes. */ | |
1049 { | |
1050 int repl_size = vim_strnsize(lead_repl, | |
1051 lead_repl_len); | |
1052 int old_size = 0; | |
1053 char_u *endp = p; | |
1054 int l; | |
1055 | |
1056 while (old_size < repl_size && p > leader) | |
1057 { | |
39 | 1058 mb_ptr_back(leader, p); |
17 | 1059 old_size += ptr2cells(p); |
1060 } | |
835 | 1061 l = lead_repl_len - (int)(endp - p); |
17 | 1062 if (l != 0) |
1063 mch_memmove(endp + l, endp, | |
1064 (size_t)((leader + lead_len) - endp)); | |
1065 lead_len += l; | |
1066 } | |
1067 #else | |
7 | 1068 if (p < leader + lead_repl_len) |
1069 p = leader; | |
1070 else | |
1071 p -= lead_repl_len; | |
17 | 1072 #endif |
7 | 1073 mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
1074 if (p + lead_repl_len > leader + lead_len) | |
1075 p[lead_repl_len] = NUL; | |
1076 | |
1077 /* blank-out any other chars from the old leader. */ | |
1078 while (--p >= leader) | |
17 | 1079 { |
1080 #ifdef FEAT_MBYTE | |
1081 int l = mb_head_off(leader, p); | |
1082 | |
1083 if (l > 1) | |
1084 { | |
1085 p -= l; | |
1086 if (ptr2cells(p) > 1) | |
1087 { | |
1088 p[1] = ' '; | |
1089 --l; | |
1090 } | |
1091 mch_memmove(p + 1, p + l + 1, | |
1092 (size_t)((leader + lead_len) - (p + l + 1))); | |
1093 lead_len -= l; | |
1094 *p = ' '; | |
1095 } | |
1096 else | |
1097 #endif | |
7 | 1098 if (!vim_iswhite(*p)) |
1099 *p = ' '; | |
17 | 1100 } |
7 | 1101 } |
1102 else /* left adjusted leader */ | |
1103 { | |
1104 p = skipwhite(leader); | |
17 | 1105 #ifdef FEAT_MBYTE |
1106 /* Compute the length of the replaced characters in | |
1107 * screen characters, not bytes. Move the part that is | |
1108 * not to be overwritten. */ | |
1109 { | |
1110 int repl_size = vim_strnsize(lead_repl, | |
1111 lead_repl_len); | |
1112 int i; | |
1113 int l; | |
1114 | |
1115 for (i = 0; p[i] != NUL && i < lead_len; i += l) | |
1116 { | |
474 | 1117 l = (*mb_ptr2len)(p + i); |
17 | 1118 if (vim_strnsize(p, i + l) > repl_size) |
1119 break; | |
1120 } | |
1121 if (i != lead_repl_len) | |
1122 { | |
1123 mch_memmove(p + lead_repl_len, p + i, | |
2002 | 1124 (size_t)(lead_len - i - (p - leader))); |
17 | 1125 lead_len += lead_repl_len - i; |
1126 } | |
1127 } | |
1128 #endif | |
7 | 1129 mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
1130 | |
1131 /* Replace any remaining non-white chars in the old | |
1132 * leader by spaces. Keep Tabs, the indent must | |
1133 * remain the same. */ | |
1134 for (p += lead_repl_len; p < leader + lead_len; ++p) | |
1135 if (!vim_iswhite(*p)) | |
1136 { | |
1137 /* Don't put a space before a TAB. */ | |
1138 if (p + 1 < leader + lead_len && p[1] == TAB) | |
1139 { | |
1140 --lead_len; | |
1141 mch_memmove(p, p + 1, | |
1142 (leader + lead_len) - p); | |
1143 } | |
1144 else | |
17 | 1145 { |
1146 #ifdef FEAT_MBYTE | |
474 | 1147 int l = (*mb_ptr2len)(p); |
17 | 1148 |
1149 if (l > 1) | |
1150 { | |
1151 if (ptr2cells(p) > 1) | |
1152 { | |
1153 /* Replace a double-wide char with | |
1154 * two spaces */ | |
1155 --l; | |
1156 *p++ = ' '; | |
1157 } | |
1158 mch_memmove(p + 1, p + l, | |
1159 (leader + lead_len) - p); | |
1160 lead_len -= l - 1; | |
1161 } | |
1162 #endif | |
7 | 1163 *p = ' '; |
17 | 1164 } |
7 | 1165 } |
1166 *p = NUL; | |
1167 } | |
1168 | |
1169 /* Recompute the indent, it may have changed. */ | |
1170 if (curbuf->b_p_ai | |
1171 #ifdef FEAT_SMARTINDENT | |
1172 || do_si | |
1173 #endif | |
1174 ) | |
1175 newindent = get_indent_str(leader, (int)curbuf->b_p_ts); | |
1176 | |
1177 /* Add the indent offset */ | |
1178 if (newindent + off < 0) | |
1179 { | |
1180 off = -newindent; | |
1181 newindent = 0; | |
1182 } | |
1183 else | |
1184 newindent += off; | |
1185 | |
1186 /* Correct trailing spaces for the shift, so that | |
1187 * alignment remains equal. */ | |
1188 while (off > 0 && lead_len > 0 | |
1189 && leader[lead_len - 1] == ' ') | |
1190 { | |
1191 /* Don't do it when there is a tab before the space */ | |
1192 if (vim_strchr(skipwhite(leader), '\t') != NULL) | |
1193 break; | |
1194 --lead_len; | |
1195 --off; | |
1196 } | |
1197 | |
1198 /* If the leader ends in white space, don't add an | |
1199 * extra space */ | |
1200 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1])) | |
1201 extra_space = FALSE; | |
1202 leader[lead_len] = NUL; | |
1203 } | |
1204 | |
1205 if (extra_space) | |
1206 { | |
1207 leader[lead_len++] = ' '; | |
1208 leader[lead_len] = NUL; | |
1209 } | |
1210 | |
1211 newcol = lead_len; | |
1212 | |
1213 /* | |
1214 * if a new indent will be set below, remove the indent that | |
1215 * is in the comment leader | |
1216 */ | |
1217 if (newindent | |
1218 #ifdef FEAT_SMARTINDENT | |
1219 || did_si | |
1220 #endif | |
1221 ) | |
1222 { | |
1223 while (lead_len && vim_iswhite(*leader)) | |
1224 { | |
1225 --lead_len; | |
1226 --newcol; | |
1227 ++leader; | |
1228 } | |
1229 } | |
1230 | |
1231 } | |
1232 #ifdef FEAT_SMARTINDENT | |
1233 did_si = can_si = FALSE; | |
1234 #endif | |
1235 } | |
1236 else if (comment_end != NULL) | |
1237 { | |
1238 /* | |
1239 * We have finished a comment, so we don't use the leader. | |
1240 * If this was a C-comment and 'ai' or 'si' is set do a normal | |
1241 * indent to align with the line containing the start of the | |
1242 * comment. | |
1243 */ | |
1244 if (comment_end[0] == '*' && comment_end[1] == '/' && | |
1245 (curbuf->b_p_ai | |
1246 #ifdef FEAT_SMARTINDENT | |
1247 || do_si | |
1248 #endif | |
1249 )) | |
1250 { | |
1251 old_cursor = curwin->w_cursor; | |
1252 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); | |
1253 if ((pos = findmatch(NULL, NUL)) != NULL) | |
1254 { | |
1255 curwin->w_cursor.lnum = pos->lnum; | |
1256 newindent = get_indent(); | |
1257 } | |
1258 curwin->w_cursor = old_cursor; | |
1259 } | |
1260 } | |
1261 } | |
1262 #endif | |
1263 | |
1264 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */ | |
1265 if (p_extra != NULL) | |
1266 { | |
1267 *p_extra = saved_char; /* restore char that NUL replaced */ | |
1268 | |
1269 /* | |
1270 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first | |
1271 * non-blank. | |
1272 * | |
1273 * When in REPLACE mode, put the deleted blanks on the replace stack, | |
1274 * preceded by a NUL, so they can be put back when a BS is entered. | |
1275 */ | |
1276 if (REPLACE_NORMAL(State)) | |
1277 replace_push(NUL); /* end of extra blanks */ | |
1278 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) | |
1279 { | |
1280 while ((*p_extra == ' ' || *p_extra == '\t') | |
1281 #ifdef FEAT_MBYTE | |
1282 && (!enc_utf8 | |
1283 || !utf_iscomposing(utf_ptr2char(p_extra + 1))) | |
1284 #endif | |
1285 ) | |
1286 { | |
1287 if (REPLACE_NORMAL(State)) | |
1288 replace_push(*p_extra); | |
1289 ++p_extra; | |
1290 ++less_cols_off; | |
1291 } | |
1292 } | |
1293 if (*p_extra != NUL) | |
1294 did_ai = FALSE; /* append some text, don't truncate now */ | |
1295 | |
1296 /* columns for marks adjusted for removed columns */ | |
1297 less_cols = (int)(p_extra - saved_line); | |
1298 } | |
1299 | |
1300 if (p_extra == NULL) | |
1301 p_extra = (char_u *)""; /* append empty line */ | |
1302 | |
1303 #ifdef FEAT_COMMENTS | |
1304 /* concatenate leader and p_extra, if there is a leader */ | |
1305 if (lead_len) | |
1306 { | |
1307 STRCAT(leader, p_extra); | |
1308 p_extra = leader; | |
1309 did_ai = TRUE; /* So truncating blanks works with comments */ | |
1310 less_cols -= lead_len; | |
1311 } | |
1312 else | |
1313 end_comment_pending = NUL; /* turns out there was no leader */ | |
1314 #endif | |
1315 | |
1316 old_cursor = curwin->w_cursor; | |
1317 if (dir == BACKWARD) | |
1318 --curwin->w_cursor.lnum; | |
1319 #ifdef FEAT_VREPLACE | |
1320 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) | |
1321 #endif | |
1322 { | |
1323 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) | |
1324 == FAIL) | |
1325 goto theend; | |
1326 /* Postpone calling changed_lines(), because it would mess up folding | |
1327 * with markers. */ | |
1328 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); | |
1329 did_append = TRUE; | |
1330 } | |
1331 #ifdef FEAT_VREPLACE | |
1332 else | |
1333 { | |
1334 /* | |
1335 * In VREPLACE mode we are starting to replace the next line. | |
1336 */ | |
1337 curwin->w_cursor.lnum++; | |
1338 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) | |
1339 { | |
1340 /* In case we NL to a new line, BS to the previous one, and NL | |
1341 * again, we don't want to save the new line for undo twice. | |
1342 */ | |
1343 (void)u_save_cursor(); /* errors are ignored! */ | |
1344 vr_lines_changed++; | |
1345 } | |
1346 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); | |
1347 changed_bytes(curwin->w_cursor.lnum, 0); | |
1348 curwin->w_cursor.lnum--; | |
1349 did_append = FALSE; | |
1350 } | |
1351 #endif | |
1352 | |
1353 if (newindent | |
1354 #ifdef FEAT_SMARTINDENT | |
1355 || did_si | |
1356 #endif | |
1357 ) | |
1358 { | |
1359 ++curwin->w_cursor.lnum; | |
1360 #ifdef FEAT_SMARTINDENT | |
1361 if (did_si) | |
1362 { | |
1363 if (p_sr) | |
1364 newindent -= newindent % (int)curbuf->b_p_sw; | |
1365 newindent += (int)curbuf->b_p_sw; | |
1366 } | |
1367 #endif | |
1324 | 1368 /* Copy the indent */ |
1369 if (curbuf->b_p_ci) | |
7 | 1370 { |
1371 (void)copy_indent(newindent, saved_line); | |
1372 | |
1373 /* | |
1374 * Set the 'preserveindent' option so that any further screwing | |
1375 * with the line doesn't entirely destroy our efforts to preserve | |
1376 * it. It gets restored at the function end. | |
1377 */ | |
1378 curbuf->b_p_pi = TRUE; | |
1379 } | |
1380 else | |
1381 (void)set_indent(newindent, SIN_INSERT); | |
1382 less_cols -= curwin->w_cursor.col; | |
1383 | |
1384 ai_col = curwin->w_cursor.col; | |
1385 | |
1386 /* | |
1387 * In REPLACE mode, for each character in the new indent, there must | |
1388 * be a NUL on the replace stack, for when it is deleted with BS | |
1389 */ | |
1390 if (REPLACE_NORMAL(State)) | |
1391 for (n = 0; n < (int)curwin->w_cursor.col; ++n) | |
1392 replace_push(NUL); | |
1393 newcol += curwin->w_cursor.col; | |
1394 #ifdef FEAT_SMARTINDENT | |
1395 if (no_si) | |
1396 did_si = FALSE; | |
1397 #endif | |
1398 } | |
1399 | |
1400 #ifdef FEAT_COMMENTS | |
1401 /* | |
1402 * In REPLACE mode, for each character in the extra leader, there must be | |
1403 * a NUL on the replace stack, for when it is deleted with BS. | |
1404 */ | |
1405 if (REPLACE_NORMAL(State)) | |
1406 while (lead_len-- > 0) | |
1407 replace_push(NUL); | |
1408 #endif | |
1409 | |
1410 curwin->w_cursor = old_cursor; | |
1411 | |
1412 if (dir == FORWARD) | |
1413 { | |
1414 if (trunc_line || (State & INSERT)) | |
1415 { | |
1416 /* truncate current line at cursor */ | |
1417 saved_line[curwin->w_cursor.col] = NUL; | |
1418 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */ | |
1419 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) | |
1420 truncate_spaces(saved_line); | |
1421 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); | |
1422 saved_line = NULL; | |
1423 if (did_append) | |
1424 { | |
1425 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, | |
1426 curwin->w_cursor.lnum + 1, 1L); | |
1427 did_append = FALSE; | |
1428 | |
1429 /* Move marks after the line break to the new line. */ | |
1430 if (flags & OPENLINE_MARKFIX) | |
1431 mark_col_adjust(curwin->w_cursor.lnum, | |
1432 curwin->w_cursor.col + less_cols_off, | |
1433 1L, (long)-less_cols); | |
1434 } | |
1435 else | |
1436 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); | |
1437 } | |
1438 | |
1439 /* | |
1440 * Put the cursor on the new line. Careful: the scrollup() above may | |
1441 * have moved w_cursor, we must use old_cursor. | |
1442 */ | |
1443 curwin->w_cursor.lnum = old_cursor.lnum + 1; | |
1444 } | |
1445 if (did_append) | |
1446 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); | |
1447 | |
1448 curwin->w_cursor.col = newcol; | |
1449 #ifdef FEAT_VIRTUALEDIT | |
1450 curwin->w_cursor.coladd = 0; | |
1451 #endif | |
1452 | |
1453 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
1454 /* | |
1455 * In VREPLACE mode, we are handling the replace stack ourselves, so stop | |
1456 * fixthisline() from doing it (via change_indent()) by telling it we're in | |
1457 * normal INSERT mode. | |
1458 */ | |
1459 if (State & VREPLACE_FLAG) | |
1460 { | |
1461 vreplace_mode = State; /* So we know to put things right later */ | |
1462 State = INSERT; | |
1463 } | |
1464 else | |
1465 vreplace_mode = 0; | |
1466 #endif | |
1467 #ifdef FEAT_LISP | |
1468 /* | |
1469 * May do lisp indenting. | |
1470 */ | |
1471 if (!p_paste | |
1472 # ifdef FEAT_COMMENTS | |
1473 && leader == NULL | |
1474 # endif | |
1475 && curbuf->b_p_lisp | |
1476 && curbuf->b_p_ai) | |
1477 { | |
1478 fixthisline(get_lisp_indent); | |
1479 p = ml_get_curline(); | |
1480 ai_col = (colnr_T)(skipwhite(p) - p); | |
1481 } | |
1482 #endif | |
1483 #ifdef FEAT_CINDENT | |
1484 /* | |
1485 * May do indenting after opening a new line. | |
1486 */ | |
1487 if (!p_paste | |
1488 && (curbuf->b_p_cin | |
1489 # ifdef FEAT_EVAL | |
1490 || *curbuf->b_p_inde != NUL | |
1491 # endif | |
1492 ) | |
1493 && in_cinkeys(dir == FORWARD | |
1494 ? KEY_OPEN_FORW | |
1495 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) | |
1496 { | |
1497 do_c_expr_indent(); | |
1498 p = ml_get_curline(); | |
1499 ai_col = (colnr_T)(skipwhite(p) - p); | |
1500 } | |
1501 #endif | |
1502 #if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT)) | |
1503 if (vreplace_mode != 0) | |
1504 State = vreplace_mode; | |
1505 #endif | |
1506 | |
1507 #ifdef FEAT_VREPLACE | |
1508 /* | |
1509 * Finally, VREPLACE gets the stuff on the new line, then puts back the | |
1510 * original line, and inserts the new stuff char by char, pushing old stuff | |
1511 * onto the replace stack (via ins_char()). | |
1512 */ | |
1513 if (State & VREPLACE_FLAG) | |
1514 { | |
1515 /* Put new line in p_extra */ | |
1516 p_extra = vim_strsave(ml_get_curline()); | |
1517 if (p_extra == NULL) | |
1518 goto theend; | |
1519 | |
1520 /* Put back original line */ | |
1521 ml_replace(curwin->w_cursor.lnum, next_line, FALSE); | |
1522 | |
1523 /* Insert new stuff into line again */ | |
1524 curwin->w_cursor.col = 0; | |
1525 #ifdef FEAT_VIRTUALEDIT | |
1526 curwin->w_cursor.coladd = 0; | |
1527 #endif | |
1528 ins_bytes(p_extra); /* will call changed_bytes() */ | |
1529 vim_free(p_extra); | |
1530 next_line = NULL; | |
1531 } | |
1532 #endif | |
1533 | |
1534 retval = TRUE; /* success! */ | |
1535 theend: | |
1536 curbuf->b_p_pi = saved_pi; | |
1537 vim_free(saved_line); | |
1538 vim_free(next_line); | |
1539 vim_free(allocated); | |
1540 return retval; | |
1541 } | |
1542 | |
1543 #if defined(FEAT_COMMENTS) || defined(PROTO) | |
1544 /* | |
1545 * get_leader_len() returns the length of the prefix of the given string | |
1546 * which introduces a comment. If this string is not a comment then 0 is | |
1547 * returned. | |
1548 * When "flags" is not NULL, it is set to point to the flags of the recognized | |
1549 * comment leader. | |
1550 * "backward" must be true for the "O" command. | |
1551 */ | |
1552 int | |
1553 get_leader_len(line, flags, backward) | |
1554 char_u *line; | |
1555 char_u **flags; | |
1556 int backward; | |
1557 { | |
1558 int i, j; | |
1559 int got_com = FALSE; | |
1560 int found_one; | |
1561 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */ | |
1562 char_u *string; /* pointer to comment string */ | |
1563 char_u *list; | |
2809 | 1564 int middle_match_len = 0; |
1565 char_u *prev_list; | |
2813 | 1566 char_u *saved_flags = NULL; |
7 | 1567 |
1568 i = 0; | |
1569 while (vim_iswhite(line[i])) /* leading white space is ignored */ | |
1570 ++i; | |
1571 | |
1572 /* | |
1573 * Repeat to match several nested comment strings. | |
1574 */ | |
2809 | 1575 while (line[i] != NUL) |
7 | 1576 { |
1577 /* | |
1578 * scan through the 'comments' option for a match | |
1579 */ | |
1580 found_one = FALSE; | |
1581 for (list = curbuf->b_p_com; *list; ) | |
1582 { | |
2809 | 1583 /* Get one option part into part_buf[]. Advance "list" to next |
1584 * one. Put "string" at start of string. */ | |
1585 if (!got_com && flags != NULL) | |
1586 *flags = list; /* remember where flags started */ | |
1587 prev_list = list; | |
7 | 1588 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ","); |
1589 string = vim_strchr(part_buf, ':'); | |
1590 if (string == NULL) /* missing ':', ignore this part */ | |
1591 continue; | |
1592 *string++ = NUL; /* isolate flags from string */ | |
1593 | |
2809 | 1594 /* If we found a middle match previously, use that match when this |
1595 * is not a middle or end. */ | |
1596 if (middle_match_len != 0 | |
1597 && vim_strchr(part_buf, COM_MIDDLE) == NULL | |
1598 && vim_strchr(part_buf, COM_END) == NULL) | |
1599 break; | |
1600 | |
1601 /* When we already found a nested comment, only accept further | |
1602 * nested comments. */ | |
7 | 1603 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL) |
1604 continue; | |
1605 | |
2809 | 1606 /* When 'O' flag present and using "O" command skip this one. */ |
7 | 1607 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL) |
1608 continue; | |
1609 | |
2809 | 1610 /* Line contents and string must match. |
7 | 1611 * When string starts with white space, must have some white space |
1612 * (but the amount does not need to match, there might be a mix of | |
2809 | 1613 * TABs and spaces). */ |
7 | 1614 if (vim_iswhite(string[0])) |
1615 { | |
1616 if (i == 0 || !vim_iswhite(line[i - 1])) | |
2809 | 1617 continue; /* missing shite space */ |
7 | 1618 while (vim_iswhite(string[0])) |
1619 ++string; | |
1620 } | |
1621 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j) | |
1622 ; | |
1623 if (string[j] != NUL) | |
2809 | 1624 continue; /* string doesn't match */ |
1625 | |
1626 /* When 'b' flag used, there must be white space or an | |
1627 * end-of-line after the string in the line. */ | |
7 | 1628 if (vim_strchr(part_buf, COM_BLANK) != NULL |
1629 && !vim_iswhite(line[i + j]) && line[i + j] != NUL) | |
1630 continue; | |
1631 | |
2809 | 1632 /* We have found a match, stop searching unless this is a middle |
1633 * comment. The middle comment can be a substring of the end | |
1634 * comment in which case it's better to return the length of the | |
1635 * end comment and its flags. Thus we keep searching with middle | |
1636 * and end matches and use an end match if it matches better. */ | |
1637 if (vim_strchr(part_buf, COM_MIDDLE) != NULL) | |
1638 { | |
1639 if (middle_match_len == 0) | |
1640 { | |
1641 middle_match_len = j; | |
1642 saved_flags = prev_list; | |
1643 } | |
1644 continue; | |
1645 } | |
1646 if (middle_match_len != 0 && j > middle_match_len) | |
1647 /* Use this match instead of the middle match, since it's a | |
1648 * longer thus better match. */ | |
1649 middle_match_len = 0; | |
1650 | |
1651 if (middle_match_len == 0) | |
1652 i += j; | |
7 | 1653 found_one = TRUE; |
1654 break; | |
1655 } | |
1656 | |
2809 | 1657 if (middle_match_len != 0) |
1658 { | |
1659 /* Use the previously found middle match after failing to find a | |
1660 * match with an end. */ | |
1661 if (!got_com && flags != NULL) | |
1662 *flags = saved_flags; | |
1663 i += middle_match_len; | |
1664 found_one = TRUE; | |
1665 } | |
1666 | |
1667 /* No match found, stop scanning. */ | |
7 | 1668 if (!found_one) |
1669 break; | |
1670 | |
2809 | 1671 /* Include any trailing white space. */ |
7 | 1672 while (vim_iswhite(line[i])) |
1673 ++i; | |
1674 | |
2809 | 1675 /* If this comment doesn't nest, stop here. */ |
1676 got_com = TRUE; | |
7 | 1677 if (vim_strchr(part_buf, COM_NEST) == NULL) |
1678 break; | |
1679 } | |
2809 | 1680 |
7 | 1681 return (got_com ? i : 0); |
1682 } | |
1683 #endif | |
1684 | |
1685 /* | |
1686 * Return the number of window lines occupied by buffer line "lnum". | |
1687 */ | |
1688 int | |
1689 plines(lnum) | |
1690 linenr_T lnum; | |
1691 { | |
1692 return plines_win(curwin, lnum, TRUE); | |
1693 } | |
1694 | |
1695 int | |
1696 plines_win(wp, lnum, winheight) | |
1697 win_T *wp; | |
1698 linenr_T lnum; | |
1699 int winheight; /* when TRUE limit to window height */ | |
1700 { | |
1701 #if defined(FEAT_DIFF) || defined(PROTO) | |
1702 /* Check for filler lines above this buffer line. When folded the result | |
1703 * is one line anyway. */ | |
1704 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum); | |
1705 } | |
1706 | |
1707 int | |
1708 plines_nofill(lnum) | |
1709 linenr_T lnum; | |
1710 { | |
1711 return plines_win_nofill(curwin, lnum, TRUE); | |
1712 } | |
1713 | |
1714 int | |
1715 plines_win_nofill(wp, lnum, winheight) | |
1716 win_T *wp; | |
1717 linenr_T lnum; | |
1718 int winheight; /* when TRUE limit to window height */ | |
1719 { | |
1720 #endif | |
1721 int lines; | |
1722 | |
1723 if (!wp->w_p_wrap) | |
1724 return 1; | |
1725 | |
1726 #ifdef FEAT_VERTSPLIT | |
1727 if (wp->w_width == 0) | |
1728 return 1; | |
1729 #endif | |
1730 | |
1731 #ifdef FEAT_FOLDING | |
1732 /* A folded lines is handled just like an empty line. */ | |
1733 /* NOTE: Caller must handle lines that are MAYBE folded. */ | |
1734 if (lineFolded(wp, lnum) == TRUE) | |
1735 return 1; | |
1736 #endif | |
1737 | |
1738 lines = plines_win_nofold(wp, lnum); | |
1739 if (winheight > 0 && lines > wp->w_height) | |
1740 return (int)wp->w_height; | |
1741 return lines; | |
1742 } | |
1743 | |
1744 /* | |
1745 * Return number of window lines physical line "lnum" will occupy in window | |
1746 * "wp". Does not care about folding, 'wrap' or 'diff'. | |
1747 */ | |
1748 int | |
1749 plines_win_nofold(wp, lnum) | |
1750 win_T *wp; | |
1751 linenr_T lnum; | |
1752 { | |
1753 char_u *s; | |
1754 long col; | |
1755 int width; | |
1756 | |
1757 s = ml_get_buf(wp->w_buffer, lnum, FALSE); | |
1758 if (*s == NUL) /* empty line */ | |
1759 return 1; | |
1760 col = win_linetabsize(wp, s, (colnr_T)MAXCOL); | |
1761 | |
1762 /* | |
1763 * If list mode is on, then the '$' at the end of the line may take up one | |
1764 * extra column. | |
1765 */ | |
1766 if (wp->w_p_list && lcs_eol != NUL) | |
1767 col += 1; | |
1768 | |
1769 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
1770 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'. |
7 | 1771 */ |
1772 width = W_WIDTH(wp) - win_col_off(wp); | |
1773 if (width <= 0) | |
1774 return 32000; | |
1775 if (col <= width) | |
1776 return 1; | |
1777 col -= width; | |
1778 width += win_col_off2(wp); | |
1779 return (col + (width - 1)) / width + 1; | |
1780 } | |
1781 | |
1782 /* | |
1783 * Like plines_win(), but only reports the number of physical screen lines | |
1784 * used from the start of the line to the given column number. | |
1785 */ | |
1786 int | |
1787 plines_win_col(wp, lnum, column) | |
1788 win_T *wp; | |
1789 linenr_T lnum; | |
1790 long column; | |
1791 { | |
1792 long col; | |
1793 char_u *s; | |
1794 int lines = 0; | |
1795 int width; | |
1796 | |
1797 #ifdef FEAT_DIFF | |
1798 /* Check for filler lines above this buffer line. When folded the result | |
1799 * is one line anyway. */ | |
1800 lines = diff_check_fill(wp, lnum); | |
1801 #endif | |
1802 | |
1803 if (!wp->w_p_wrap) | |
1804 return lines + 1; | |
1805 | |
1806 #ifdef FEAT_VERTSPLIT | |
1807 if (wp->w_width == 0) | |
1808 return lines + 1; | |
1809 #endif | |
1810 | |
1811 s = ml_get_buf(wp->w_buffer, lnum, FALSE); | |
1812 | |
1813 col = 0; | |
1814 while (*s != NUL && --column >= 0) | |
1815 { | |
1816 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL); | |
39 | 1817 mb_ptr_adv(s); |
7 | 1818 } |
1819 | |
1820 /* | |
1821 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in | |
1822 * INSERT mode, then col must be adjusted so that it represents the last | |
1823 * screen position of the TAB. This only fixes an error when the TAB wraps | |
1824 * from one screen line to the next (when 'columns' is not a multiple of | |
1825 * 'ts') -- webb. | |
1826 */ | |
1827 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1)) | |
1828 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1; | |
1829 | |
1830 /* | |
2178
c6f1aa1e9f32
Add 'relativenumber' patch from Markus Heidelberg.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
1831 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc. |
7 | 1832 */ |
1833 width = W_WIDTH(wp) - win_col_off(wp); | |
1023 | 1834 if (width <= 0) |
1835 return 9999; | |
1836 | |
1837 lines += 1; | |
1838 if (col > width) | |
1839 lines += (col - width) / (width + win_col_off2(wp)) + 1; | |
1840 return lines; | |
7 | 1841 } |
1842 | |
1843 int | |
1844 plines_m_win(wp, first, last) | |
1845 win_T *wp; | |
1846 linenr_T first, last; | |
1847 { | |
1848 int count = 0; | |
1849 | |
1850 while (first <= last) | |
1851 { | |
1852 #ifdef FEAT_FOLDING | |
1853 int x; | |
1854 | |
1855 /* Check if there are any really folded lines, but also included lines | |
1856 * that are maybe folded. */ | |
1857 x = foldedCount(wp, first, NULL); | |
1858 if (x > 0) | |
1859 { | |
1860 ++count; /* count 1 for "+-- folded" line */ | |
1861 first += x; | |
1862 } | |
1863 else | |
1864 #endif | |
1865 { | |
1866 #ifdef FEAT_DIFF | |
1867 if (first == wp->w_topline) | |
1868 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill; | |
1869 else | |
1870 #endif | |
1871 count += plines_win(wp, first, TRUE); | |
1872 ++first; | |
1873 } | |
1874 } | |
1875 return (count); | |
1876 } | |
1877 | |
1878 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO) | |
1879 /* | |
1880 * Insert string "p" at the cursor position. Stops at a NUL byte. | |
1881 * Handles Replace mode and multi-byte characters. | |
1882 */ | |
1883 void | |
1884 ins_bytes(p) | |
1885 char_u *p; | |
1886 { | |
1887 ins_bytes_len(p, (int)STRLEN(p)); | |
1888 } | |
1889 #endif | |
1890 | |
1891 #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ | |
1892 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO) | |
1893 /* | |
1894 * Insert string "p" with length "len" at the cursor position. | |
1895 * Handles Replace mode and multi-byte characters. | |
1896 */ | |
1897 void | |
1898 ins_bytes_len(p, len) | |
1899 char_u *p; | |
1900 int len; | |
1901 { | |
1902 int i; | |
1903 # ifdef FEAT_MBYTE | |
1904 int n; | |
1905 | |
1617 | 1906 if (has_mbyte) |
1907 for (i = 0; i < len; i += n) | |
1908 { | |
1909 if (enc_utf8) | |
1910 /* avoid reading past p[len] */ | |
1911 n = utfc_ptr2len_len(p + i, len - i); | |
1912 else | |
1913 n = (*mb_ptr2len)(p + i); | |
1914 ins_char_bytes(p + i, n); | |
1915 } | |
1916 else | |
7 | 1917 # endif |
1617 | 1918 for (i = 0; i < len; ++i) |
1919 ins_char(p[i]); | |
7 | 1920 } |
1921 #endif | |
1922 | |
1923 /* | |
1924 * Insert or replace a single character at the cursor position. | |
1925 * When in REPLACE or VREPLACE mode, replace any existing character. | |
1926 * Caller must have prepared for undo. | |
1927 * For multi-byte characters we get the whole character, the caller must | |
1928 * convert bytes to a character. | |
1929 */ | |
1930 void | |
1931 ins_char(c) | |
1932 int c; | |
1933 { | |
1934 #if defined(FEAT_MBYTE) || defined(PROTO) | |
1935 char_u buf[MB_MAXBYTES]; | |
1936 int n; | |
1937 | |
1938 n = (*mb_char2bytes)(c, buf); | |
1939 | |
1940 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. | |
1941 * Happens for CTRL-Vu9900. */ | |
1942 if (buf[0] == 0) | |
1943 buf[0] = '\n'; | |
1944 | |
1945 ins_char_bytes(buf, n); | |
1946 } | |
1947 | |
1948 void | |
1949 ins_char_bytes(buf, charlen) | |
1950 char_u *buf; | |
1951 int charlen; | |
1952 { | |
1953 int c = buf[0]; | |
1954 #endif | |
1955 int newlen; /* nr of bytes inserted */ | |
1956 int oldlen; /* nr of bytes deleted (0 when not replacing) */ | |
1957 char_u *p; | |
1958 char_u *newp; | |
1959 char_u *oldp; | |
1960 int linelen; /* length of old line including NUL */ | |
1961 colnr_T col; | |
1962 linenr_T lnum = curwin->w_cursor.lnum; | |
1963 int i; | |
1964 | |
1965 #ifdef FEAT_VIRTUALEDIT | |
1966 /* Break tabs if needed. */ | |
1967 if (virtual_active() && curwin->w_cursor.coladd > 0) | |
1968 coladvance_force(getviscol()); | |
1969 #endif | |
1970 | |
1971 col = curwin->w_cursor.col; | |
1972 oldp = ml_get(lnum); | |
1973 linelen = (int)STRLEN(oldp) + 1; | |
1974 | |
1975 /* The lengths default to the values for when not replacing. */ | |
1976 oldlen = 0; | |
1977 #ifdef FEAT_MBYTE | |
1978 newlen = charlen; | |
1979 #else | |
1980 newlen = 1; | |
1981 #endif | |
1982 | |
1983 if (State & REPLACE_FLAG) | |
1984 { | |
1985 #ifdef FEAT_VREPLACE | |
1986 if (State & VREPLACE_FLAG) | |
1987 { | |
1988 colnr_T new_vcol = 0; /* init for GCC */ | |
1989 colnr_T vcol; | |
1990 int old_list; | |
1991 #ifndef FEAT_MBYTE | |
1992 char_u buf[2]; | |
1993 #endif | |
1994 | |
1995 /* | |
1996 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. | |
1997 * Returns the old value of list, so when finished, | |
1998 * curwin->w_p_list should be set back to this. | |
1999 */ | |
2000 old_list = curwin->w_p_list; | |
2001 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) | |
2002 curwin->w_p_list = FALSE; | |
2003 | |
2004 /* | |
2005 * In virtual replace mode each character may replace one or more | |
2006 * characters (zero if it's a TAB). Count the number of bytes to | |
2007 * be deleted to make room for the new character, counting screen | |
2008 * cells. May result in adding spaces to fill a gap. | |
2009 */ | |
2010 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); | |
2011 #ifndef FEAT_MBYTE | |
2012 buf[0] = c; | |
2013 buf[1] = NUL; | |
2014 #endif | |
2015 new_vcol = vcol + chartabsize(buf, vcol); | |
2016 while (oldp[col + oldlen] != NUL && vcol < new_vcol) | |
2017 { | |
2018 vcol += chartabsize(oldp + col + oldlen, vcol); | |
2019 /* Don't need to remove a TAB that takes us to the right | |
2020 * position. */ | |
2021 if (vcol > new_vcol && oldp[col + oldlen] == TAB) | |
2022 break; | |
2023 #ifdef FEAT_MBYTE | |
474 | 2024 oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
7 | 2025 #else |
2026 ++oldlen; | |
2027 #endif | |
2028 /* Deleted a bit too much, insert spaces. */ | |
2029 if (vcol > new_vcol) | |
2030 newlen += vcol - new_vcol; | |
2031 } | |
2032 curwin->w_p_list = old_list; | |
2033 } | |
2034 else | |
2035 #endif | |
2036 if (oldp[col] != NUL) | |
2037 { | |
2038 /* normal replace */ | |
2039 #ifdef FEAT_MBYTE | |
474 | 2040 oldlen = (*mb_ptr2len)(oldp + col); |
7 | 2041 #else |
2042 oldlen = 1; | |
2043 #endif | |
2044 } | |
2045 | |
2046 | |
2047 /* Push the replaced bytes onto the replace stack, so that they can be | |
2048 * put back when BS is used. The bytes of a multi-byte character are | |
2049 * done the other way around, so that the first byte is popped off | |
2050 * first (it tells the byte length of the character). */ | |
2051 replace_push(NUL); | |
2052 for (i = 0; i < oldlen; ++i) | |
2053 { | |
2054 #ifdef FEAT_MBYTE | |
1470 | 2055 if (has_mbyte) |
2056 i += replace_push_mb(oldp + col + i) - 1; | |
2057 else | |
2058 #endif | |
2059 replace_push(oldp[col + i]); | |
7 | 2060 } |
2061 } | |
2062 | |
2063 newp = alloc_check((unsigned)(linelen + newlen - oldlen)); | |
2064 if (newp == NULL) | |
2065 return; | |
2066 | |
2067 /* Copy bytes before the cursor. */ | |
2068 if (col > 0) | |
2069 mch_memmove(newp, oldp, (size_t)col); | |
2070 | |
2071 /* Copy bytes after the changed character(s). */ | |
2072 p = newp + col; | |
2073 mch_memmove(p + newlen, oldp + col + oldlen, | |
2074 (size_t)(linelen - col - oldlen)); | |
2075 | |
2076 /* Insert or overwrite the new character. */ | |
2077 #ifdef FEAT_MBYTE | |
2078 mch_memmove(p, buf, charlen); | |
2079 i = charlen; | |
2080 #else | |
2081 *p = c; | |
2082 i = 1; | |
2083 #endif | |
2084 | |
2085 /* Fill with spaces when necessary. */ | |
2086 while (i < newlen) | |
2087 p[i++] = ' '; | |
2088 | |
2089 /* Replace the line in the buffer. */ | |
2090 ml_replace(lnum, newp, FALSE); | |
2091 | |
2092 /* mark the buffer as changed and prepare for displaying */ | |
2093 changed_bytes(lnum, col); | |
2094 | |
2095 /* | |
2096 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly | |
2097 * show the match for right parens and braces. | |
2098 */ | |
2099 if (p_sm && (State & INSERT) | |
2100 && msg_silent == 0 | |
2101 #ifdef FEAT_MBYTE | |
2102 && charlen == 1 | |
2103 #endif | |
674 | 2104 #ifdef FEAT_INS_EXPAND |
2105 && !ins_compl_active() | |
2106 #endif | |
7 | 2107 ) |
2108 showmatch(c); | |
2109 | |
2110 #ifdef FEAT_RIGHTLEFT | |
2111 if (!p_ri || (State & REPLACE_FLAG)) | |
2112 #endif | |
2113 { | |
2114 /* Normal insert: move cursor right */ | |
2115 #ifdef FEAT_MBYTE | |
2116 curwin->w_cursor.col += charlen; | |
2117 #else | |
2118 ++curwin->w_cursor.col; | |
2119 #endif | |
2120 } | |
2121 /* | |
2122 * TODO: should try to update w_row here, to avoid recomputing it later. | |
2123 */ | |
2124 } | |
2125 | |
2126 /* | |
2127 * Insert a string at the cursor position. | |
2128 * Note: Does NOT handle Replace mode. | |
2129 * Caller must have prepared for undo. | |
2130 */ | |
2131 void | |
2132 ins_str(s) | |
2133 char_u *s; | |
2134 { | |
2135 char_u *oldp, *newp; | |
2136 int newlen = (int)STRLEN(s); | |
2137 int oldlen; | |
2138 colnr_T col; | |
2139 linenr_T lnum = curwin->w_cursor.lnum; | |
2140 | |
2141 #ifdef FEAT_VIRTUALEDIT | |
2142 if (virtual_active() && curwin->w_cursor.coladd > 0) | |
2143 coladvance_force(getviscol()); | |
2144 #endif | |
2145 | |
2146 col = curwin->w_cursor.col; | |
2147 oldp = ml_get(lnum); | |
2148 oldlen = (int)STRLEN(oldp); | |
2149 | |
2150 newp = alloc_check((unsigned)(oldlen + newlen + 1)); | |
2151 if (newp == NULL) | |
2152 return; | |
2153 if (col > 0) | |
2154 mch_memmove(newp, oldp, (size_t)col); | |
2155 mch_memmove(newp + col, s, (size_t)newlen); | |
2156 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); | |
2157 ml_replace(lnum, newp, FALSE); | |
2158 changed_bytes(lnum, col); | |
2159 curwin->w_cursor.col += newlen; | |
2160 } | |
2161 | |
2162 /* | |
2163 * Delete one character under the cursor. | |
2164 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. | |
2165 * Caller must have prepared for undo. | |
2166 * | |
2167 * return FAIL for failure, OK otherwise | |
2168 */ | |
2169 int | |
2170 del_char(fixpos) | |
2171 int fixpos; | |
2172 { | |
2173 #ifdef FEAT_MBYTE | |
2174 if (has_mbyte) | |
2175 { | |
2176 /* Make sure the cursor is at the start of a character. */ | |
2177 mb_adjust_cursor(); | |
2178 if (*ml_get_cursor() == NUL) | |
2179 return FAIL; | |
2180 return del_chars(1L, fixpos); | |
2181 } | |
2182 #endif | |
610 | 2183 return del_bytes(1L, fixpos, TRUE); |
7 | 2184 } |
2185 | |
2186 #if defined(FEAT_MBYTE) || defined(PROTO) | |
2187 /* | |
2188 * Like del_bytes(), but delete characters instead of bytes. | |
2189 */ | |
2190 int | |
2191 del_chars(count, fixpos) | |
2192 long count; | |
2193 int fixpos; | |
2194 { | |
2195 long bytes = 0; | |
2196 long i; | |
2197 char_u *p; | |
2198 int l; | |
2199 | |
2200 p = ml_get_cursor(); | |
2201 for (i = 0; i < count && *p != NUL; ++i) | |
2202 { | |
474 | 2203 l = (*mb_ptr2len)(p); |
7 | 2204 bytes += l; |
2205 p += l; | |
2206 } | |
610 | 2207 return del_bytes(bytes, fixpos, TRUE); |
7 | 2208 } |
2209 #endif | |
2210 | |
2211 /* | |
2212 * Delete "count" bytes under the cursor. | |
2213 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. | |
2214 * Caller must have prepared for undo. | |
2215 * | |
2216 * return FAIL for failure, OK otherwise | |
2217 */ | |
2218 int | |
777 | 2219 del_bytes(count, fixpos_arg, use_delcombine) |
7 | 2220 long count; |
777 | 2221 int fixpos_arg; |
1877 | 2222 int use_delcombine UNUSED; /* 'delcombine' option applies */ |
7 | 2223 { |
2224 char_u *oldp, *newp; | |
2225 colnr_T oldlen; | |
2226 linenr_T lnum = curwin->w_cursor.lnum; | |
2227 colnr_T col = curwin->w_cursor.col; | |
2228 int was_alloced; | |
2229 long movelen; | |
777 | 2230 int fixpos = fixpos_arg; |
7 | 2231 |
2232 oldp = ml_get(lnum); | |
2233 oldlen = (int)STRLEN(oldp); | |
2234 | |
2235 /* | |
2236 * Can't do anything when the cursor is on the NUL after the line. | |
2237 */ | |
2238 if (col >= oldlen) | |
2239 return FAIL; | |
2240 | |
2241 #ifdef FEAT_MBYTE | |
2242 /* If 'delcombine' is set and deleting (less than) one character, only | |
2243 * delete the last combining character. */ | |
610 | 2244 if (p_deco && use_delcombine && enc_utf8 |
2245 && utfc_ptr2len(oldp + col) >= count) | |
7 | 2246 { |
714 | 2247 int cc[MAX_MCO]; |
7 | 2248 int n; |
2249 | |
714 | 2250 (void)utfc_ptr2char(oldp + col, cc); |
2251 if (cc[0] != NUL) | |
7 | 2252 { |
2253 /* Find the last composing char, there can be several. */ | |
2254 n = col; | |
2255 do | |
2256 { | |
2257 col = n; | |
474 | 2258 count = utf_ptr2len(oldp + n); |
7 | 2259 n += count; |
2260 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); | |
2261 fixpos = 0; | |
2262 } | |
2263 } | |
2264 #endif | |
2265 | |
2266 /* | |
2267 * When count is too big, reduce it. | |
2268 */ | |
2269 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */ | |
2270 if (movelen <= 1) | |
2271 { | |
2272 /* | |
2273 * If we just took off the last character of a non-blank line, and | |
777 | 2274 * fixpos is TRUE, we don't want to end up positioned at the NUL, |
2275 * unless "restart_edit" is set or 'virtualedit' contains "onemore". | |
7 | 2276 */ |
777 | 2277 if (col > 0 && fixpos && restart_edit == 0 |
2278 #ifdef FEAT_VIRTUALEDIT | |
2279 && (ve_flags & VE_ONEMORE) == 0 | |
2280 #endif | |
2281 ) | |
7 | 2282 { |
2283 --curwin->w_cursor.col; | |
2284 #ifdef FEAT_VIRTUALEDIT | |
2285 curwin->w_cursor.coladd = 0; | |
2286 #endif | |
2287 #ifdef FEAT_MBYTE | |
2288 if (has_mbyte) | |
2289 curwin->w_cursor.col -= | |
2290 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); | |
2291 #endif | |
2292 } | |
2293 count = oldlen - col; | |
2294 movelen = 1; | |
2295 } | |
2296 | |
2297 /* | |
2298 * If the old line has been allocated the deletion can be done in the | |
2299 * existing line. Otherwise a new line has to be allocated | |
1532 | 2300 * Can't do this when using Netbeans, because we would need to invoke |
2301 * 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
|
2302 * care of notifying Netbeans. |
7 | 2303 */ |
2304 #ifdef FEAT_NETBEANS_INTG | |
2210 | 2305 if (netbeans_active()) |
1532 | 2306 was_alloced = FALSE; |
2307 else | |
2308 #endif | |
2309 was_alloced = ml_line_alloced(); /* check if oldp was allocated */ | |
7 | 2310 if (was_alloced) |
2311 newp = oldp; /* use same allocated memory */ | |
2312 else | |
2313 { /* need to allocate a new line */ | |
2314 newp = alloc((unsigned)(oldlen + 1 - count)); | |
2315 if (newp == NULL) | |
2316 return FAIL; | |
2317 mch_memmove(newp, oldp, (size_t)col); | |
2318 } | |
2319 mch_memmove(newp + col, oldp + col + count, (size_t)movelen); | |
2320 if (!was_alloced) | |
2321 ml_replace(lnum, newp, FALSE); | |
2322 | |
2323 /* mark the buffer as changed and prepare for displaying */ | |
2324 changed_bytes(lnum, curwin->w_cursor.col); | |
2325 | |
2326 return OK; | |
2327 } | |
2328 | |
2329 /* | |
2330 * Delete from cursor to end of line. | |
2331 * Caller must have prepared for undo. | |
2332 * | |
2333 * return FAIL for failure, OK otherwise | |
2334 */ | |
2335 int | |
2336 truncate_line(fixpos) | |
2337 int fixpos; /* if TRUE fix the cursor position when done */ | |
2338 { | |
2339 char_u *newp; | |
2340 linenr_T lnum = curwin->w_cursor.lnum; | |
2341 colnr_T col = curwin->w_cursor.col; | |
2342 | |
2343 if (col == 0) | |
2344 newp = vim_strsave((char_u *)""); | |
2345 else | |
2346 newp = vim_strnsave(ml_get(lnum), col); | |
2347 | |
2348 if (newp == NULL) | |
2349 return FAIL; | |
2350 | |
2351 ml_replace(lnum, newp, FALSE); | |
2352 | |
2353 /* mark the buffer as changed and prepare for displaying */ | |
2354 changed_bytes(lnum, curwin->w_cursor.col); | |
2355 | |
2356 /* | |
2357 * If "fixpos" is TRUE we don't want to end up positioned at the NUL. | |
2358 */ | |
2359 if (fixpos && curwin->w_cursor.col > 0) | |
2360 --curwin->w_cursor.col; | |
2361 | |
2362 return OK; | |
2363 } | |
2364 | |
2365 /* | |
2366 * Delete "nlines" lines at the cursor. | |
2367 * Saves the lines for undo first if "undo" is TRUE. | |
2368 */ | |
2369 void | |
2370 del_lines(nlines, undo) | |
2371 long nlines; /* number of lines to delete */ | |
2372 int undo; /* if TRUE, prepare for undo */ | |
2373 { | |
2374 long n; | |
1929 | 2375 linenr_T first = curwin->w_cursor.lnum; |
7 | 2376 |
2377 if (nlines <= 0) | |
2378 return; | |
2379 | |
2380 /* save the deleted lines for undo */ | |
1929 | 2381 if (undo && u_savedel(first, nlines) == FAIL) |
7 | 2382 return; |
2383 | |
2384 for (n = 0; n < nlines; ) | |
2385 { | |
2386 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ | |
2387 break; | |
2388 | |
1929 | 2389 ml_delete(first, TRUE); |
7 | 2390 ++n; |
2391 | |
2392 /* If we delete the last line in the file, stop */ | |
1929 | 2393 if (first > curbuf->b_ml.ml_line_count) |
7 | 2394 break; |
2395 } | |
1929 | 2396 |
2397 /* Correct the cursor position before calling deleted_lines_mark(), it may | |
2398 * trigger a callback to display the cursor. */ | |
7 | 2399 curwin->w_cursor.col = 0; |
2400 check_cursor_lnum(); | |
1929 | 2401 |
2402 /* adjust marks, mark the buffer as changed and prepare for displaying */ | |
2403 deleted_lines_mark(first, n); | |
7 | 2404 } |
2405 | |
2406 int | |
2407 gchar_pos(pos) | |
2408 pos_T *pos; | |
2409 { | |
2410 char_u *ptr = ml_get_pos(pos); | |
2411 | |
2412 #ifdef FEAT_MBYTE | |
2413 if (has_mbyte) | |
2414 return (*mb_ptr2char)(ptr); | |
2415 #endif | |
2416 return (int)*ptr; | |
2417 } | |
2418 | |
2419 int | |
2420 gchar_cursor() | |
2421 { | |
2422 #ifdef FEAT_MBYTE | |
2423 if (has_mbyte) | |
2424 return (*mb_ptr2char)(ml_get_cursor()); | |
2425 #endif | |
2426 return (int)*ml_get_cursor(); | |
2427 } | |
2428 | |
2429 /* | |
2430 * Write a character at the current cursor position. | |
2431 * It is directly written into the block. | |
2432 */ | |
2433 void | |
2434 pchar_cursor(c) | |
2435 int c; | |
2436 { | |
2437 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE) | |
2438 + curwin->w_cursor.col) = c; | |
2439 } | |
2440 | |
2441 /* | |
2442 * When extra == 0: Return TRUE if the cursor is before or on the first | |
2443 * non-blank in the line. | |
2444 * When extra == 1: Return TRUE if the cursor is before the first non-blank in | |
2445 * the line. | |
2446 */ | |
2447 int | |
2448 inindent(extra) | |
2449 int extra; | |
2450 { | |
2451 char_u *ptr; | |
2452 colnr_T col; | |
2453 | |
2454 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col) | |
2455 ++ptr; | |
2456 if (col >= curwin->w_cursor.col + extra) | |
2457 return TRUE; | |
2458 else | |
2459 return FALSE; | |
2460 } | |
2461 | |
2462 /* | |
2463 * Skip to next part of an option argument: Skip space and comma. | |
2464 */ | |
2465 char_u * | |
2466 skip_to_option_part(p) | |
2467 char_u *p; | |
2468 { | |
2469 if (*p == ',') | |
2470 ++p; | |
2471 while (*p == ' ') | |
2472 ++p; | |
2473 return p; | |
2474 } | |
2475 | |
2476 /* | |
2189 | 2477 * Call this function when something in the current buffer is changed. |
7 | 2478 * |
2479 * Most often called through changed_bytes() and changed_lines(), which also | |
2480 * mark the area of the display to be redrawn. | |
2189 | 2481 * |
2482 * Careful: may trigger autocommands that reload the buffer. | |
7 | 2483 */ |
2484 void | |
2485 changed() | |
2486 { | |
2487 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) | |
2488 /* The text of the preediting area is inserted, but this doesn't | |
2489 * mean a change of the buffer yet. That is delayed until the | |
2490 * text is committed. (this means preedit becomes empty) */ | |
2491 if (im_is_preediting() && !xim_changed_while_preediting) | |
2492 return; | |
2493 xim_changed_while_preediting = FALSE; | |
2494 #endif | |
2495 | |
2496 if (!curbuf->b_changed) | |
2497 { | |
2498 int save_msg_scroll = msg_scroll; | |
2499 | |
819 | 2500 /* Give a warning about changing a read-only file. This may also |
2501 * check-out the file, thus change "curbuf"! */ | |
7 | 2502 change_warning(0); |
819 | 2503 |
7 | 2504 /* Create a swap file if that is wanted. |
2505 * Don't do this for "nofile" and "nowrite" buffer types. */ | |
2506 if (curbuf->b_may_swap | |
2507 #ifdef FEAT_QUICKFIX | |
2508 && !bt_dontwrite(curbuf) | |
2509 #endif | |
2510 ) | |
2511 { | |
2512 ml_open_file(curbuf); | |
2513 | |
2514 /* The ml_open_file() can cause an ATTENTION message. | |
2515 * Wait two seconds, to make sure the user reads this unexpected | |
2516 * message. Since we could be anywhere, call wait_return() now, | |
2517 * and don't let the emsg() set msg_scroll. */ | |
2518 if (need_wait_return && emsg_silent == 0) | |
2519 { | |
2520 out_flush(); | |
2521 ui_delay(2000L, TRUE); | |
2522 wait_return(TRUE); | |
2523 msg_scroll = save_msg_scroll; | |
2524 } | |
2525 } | |
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
|
2526 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
|
2527 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2528 ++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
|
2529 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2530 |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2531 /* |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2532 * 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
|
2533 */ |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2534 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
|
2535 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
|
2536 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2537 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
|
2538 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
|
2539 #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
|
2540 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
|
2541 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
|
2542 #endif |
733f0dc510c3
Undo changes that are meant for the Vim 7.3 branch.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
2543 #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
|
2544 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
|
2545 #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
|
2546 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2129
diff
changeset
|
2547 |
265 | 2548 static void changedOneline __ARGS((buf_T *buf, linenr_T lnum)); |
2549 static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra)); | |
7 | 2550 static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra)); |
2551 | |
2552 /* | |
2553 * Changed bytes within a single line for the current buffer. | |
2554 * - marks the windows on this buffer to be redisplayed | |
2555 * - marks the buffer changed by calling changed() | |
2556 * - invalidates cached values | |
2189 | 2557 * Careful: may trigger autocommands that reload the buffer. |
7 | 2558 */ |
2559 void | |
2560 changed_bytes(lnum, col) | |
2561 linenr_T lnum; | |
2562 colnr_T col; | |
2563 { | |
265 | 2564 changedOneline(curbuf, lnum); |
7 | 2565 changed_common(lnum, col, lnum + 1, 0L); |
265 | 2566 |
2567 #ifdef FEAT_DIFF | |
2568 /* Diff highlighting in other diff windows may need to be updated too. */ | |
2569 if (curwin->w_p_diff) | |
2570 { | |
2571 win_T *wp; | |
2572 linenr_T wlnum; | |
2573 | |
2574 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2575 if (wp->w_p_diff && wp != curwin) | |
2576 { | |
2577 redraw_win_later(wp, VALID); | |
2578 wlnum = diff_lnum_win(lnum, wp); | |
2579 if (wlnum > 0) | |
2580 changedOneline(wp->w_buffer, wlnum); | |
2581 } | |
2582 } | |
2583 #endif | |
7 | 2584 } |
2585 | |
2586 static void | |
265 | 2587 changedOneline(buf, lnum) |
2588 buf_T *buf; | |
7 | 2589 linenr_T lnum; |
2590 { | |
265 | 2591 if (buf->b_mod_set) |
7 | 2592 { |
2593 /* find the maximum area that must be redisplayed */ | |
265 | 2594 if (lnum < buf->b_mod_top) |
2595 buf->b_mod_top = lnum; | |
2596 else if (lnum >= buf->b_mod_bot) | |
2597 buf->b_mod_bot = lnum + 1; | |
7 | 2598 } |
2599 else | |
2600 { | |
2601 /* set the area that must be redisplayed to one line */ | |
265 | 2602 buf->b_mod_set = TRUE; |
2603 buf->b_mod_top = lnum; | |
2604 buf->b_mod_bot = lnum + 1; | |
2605 buf->b_mod_xlines = 0; | |
7 | 2606 } |
2607 } | |
2608 | |
2609 /* | |
2610 * Appended "count" lines below line "lnum" in the current buffer. | |
2611 * Must be called AFTER the change and after mark_adjust(). | |
2612 * Takes care of marking the buffer to be redrawn and sets the changed flag. | |
2613 */ | |
2614 void | |
2615 appended_lines(lnum, count) | |
2616 linenr_T lnum; | |
2617 long count; | |
2618 { | |
2619 changed_lines(lnum + 1, 0, lnum + 1, count); | |
2620 } | |
2621 | |
2622 /* | |
2623 * Like appended_lines(), but adjust marks first. | |
2624 */ | |
2625 void | |
2626 appended_lines_mark(lnum, count) | |
2627 linenr_T lnum; | |
2628 long count; | |
2629 { | |
2630 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); | |
2631 changed_lines(lnum + 1, 0, lnum + 1, count); | |
2632 } | |
2633 | |
2634 /* | |
2635 * Deleted "count" lines at line "lnum" in the current buffer. | |
2636 * Must be called AFTER the change and after mark_adjust(). | |
2637 * Takes care of marking the buffer to be redrawn and sets the changed flag. | |
2638 */ | |
2639 void | |
2640 deleted_lines(lnum, count) | |
2641 linenr_T lnum; | |
2642 long count; | |
2643 { | |
2644 changed_lines(lnum, 0, lnum + count, -count); | |
2645 } | |
2646 | |
2647 /* | |
2648 * Like deleted_lines(), but adjust marks first. | |
1929 | 2649 * Make sure the cursor is on a valid line before calling, a GUI callback may |
2650 * be triggered to display the cursor. | |
7 | 2651 */ |
2652 void | |
2653 deleted_lines_mark(lnum, count) | |
2654 linenr_T lnum; | |
2655 long count; | |
2656 { | |
2657 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); | |
2658 changed_lines(lnum, 0, lnum + count, -count); | |
2659 } | |
2660 | |
2661 /* | |
2662 * Changed lines for the current buffer. | |
2663 * Must be called AFTER the change and after mark_adjust(). | |
2664 * - mark the buffer changed by calling changed() | |
2665 * - mark the windows on this buffer to be redisplayed | |
2666 * - invalidate cached values | |
2667 * "lnum" is the first line that needs displaying, "lnume" the first line | |
2668 * below the changed lines (BEFORE the change). | |
2669 * When only inserting lines, "lnum" and "lnume" are equal. | |
2670 * Takes care of calling changed() and updating b_mod_*. | |
2189 | 2671 * Careful: may trigger autocommands that reload the buffer. |
7 | 2672 */ |
2673 void | |
2674 changed_lines(lnum, col, lnume, xtra) | |
2675 linenr_T lnum; /* first line with change */ | |
2676 colnr_T col; /* column in first line with change */ | |
2677 linenr_T lnume; /* line below last changed line */ | |
2678 long xtra; /* number of extra lines (negative when deleting) */ | |
2679 { | |
265 | 2680 changed_lines_buf(curbuf, lnum, lnume, xtra); |
2681 | |
2682 #ifdef FEAT_DIFF | |
2683 if (xtra == 0 && curwin->w_p_diff) | |
2684 { | |
2685 /* When the number of lines doesn't change then mark_adjust() isn't | |
2686 * called and other diff buffers still need to be marked for | |
2687 * displaying. */ | |
2688 win_T *wp; | |
2689 linenr_T wlnum; | |
2690 | |
2691 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2692 if (wp->w_p_diff && wp != curwin) | |
2693 { | |
2694 redraw_win_later(wp, VALID); | |
2695 wlnum = diff_lnum_win(lnum, wp); | |
2696 if (wlnum > 0) | |
2697 changed_lines_buf(wp->w_buffer, wlnum, | |
2698 lnume - lnum + wlnum, 0L); | |
2699 } | |
2700 } | |
2701 #endif | |
2702 | |
2703 changed_common(lnum, col, lnume, xtra); | |
2704 } | |
2705 | |
2706 static void | |
2707 changed_lines_buf(buf, lnum, lnume, xtra) | |
2708 buf_T *buf; | |
2709 linenr_T lnum; /* first line with change */ | |
2710 linenr_T lnume; /* line below last changed line */ | |
2711 long xtra; /* number of extra lines (negative when deleting) */ | |
2712 { | |
2713 if (buf->b_mod_set) | |
7 | 2714 { |
2715 /* find the maximum area that must be redisplayed */ | |
265 | 2716 if (lnum < buf->b_mod_top) |
2717 buf->b_mod_top = lnum; | |
2718 if (lnum < buf->b_mod_bot) | |
7 | 2719 { |
2720 /* adjust old bot position for xtra lines */ | |
265 | 2721 buf->b_mod_bot += xtra; |
2722 if (buf->b_mod_bot < lnum) | |
2723 buf->b_mod_bot = lnum; | |
7 | 2724 } |
265 | 2725 if (lnume + xtra > buf->b_mod_bot) |
2726 buf->b_mod_bot = lnume + xtra; | |
2727 buf->b_mod_xlines += xtra; | |
7 | 2728 } |
2729 else | |
2730 { | |
2731 /* set the area that must be redisplayed */ | |
265 | 2732 buf->b_mod_set = TRUE; |
2733 buf->b_mod_top = lnum; | |
2734 buf->b_mod_bot = lnume + xtra; | |
2735 buf->b_mod_xlines = xtra; | |
2736 } | |
7 | 2737 } |
2738 | |
2189 | 2739 /* |
2740 * Common code for when a change is was made. | |
2741 * See changed_lines() for the arguments. | |
2742 * Careful: may trigger autocommands that reload the buffer. | |
2743 */ | |
7 | 2744 static void |
2745 changed_common(lnum, col, lnume, xtra) | |
2746 linenr_T lnum; | |
2747 colnr_T col; | |
2748 linenr_T lnume; | |
2749 long xtra; | |
2750 { | |
2751 win_T *wp; | |
1863 | 2752 #ifdef FEAT_WINDOWS |
2753 tabpage_T *tp; | |
2754 #endif | |
7 | 2755 int i; |
2756 #ifdef FEAT_JUMPLIST | |
2757 int cols; | |
2758 pos_T *p; | |
2759 int add; | |
2760 #endif | |
2761 | |
2762 /* mark the buffer as modified */ | |
2763 changed(); | |
2764 | |
2765 /* set the '. mark */ | |
2766 if (!cmdmod.keepjumps) | |
2767 { | |
2768 curbuf->b_last_change.lnum = lnum; | |
2769 curbuf->b_last_change.col = col; | |
2770 | |
2771 #ifdef FEAT_JUMPLIST | |
2772 /* Create a new entry if a new undo-able change was started or we | |
2773 * don't have an entry yet. */ | |
2774 if (curbuf->b_new_change || curbuf->b_changelistlen == 0) | |
2775 { | |
2776 if (curbuf->b_changelistlen == 0) | |
2777 add = TRUE; | |
2778 else | |
2779 { | |
2780 /* Don't create a new entry when the line number is the same | |
2781 * as the last one and the column is not too far away. Avoids | |
2782 * creating many entries for typing "xxxxx". */ | |
2783 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; | |
2784 if (p->lnum != lnum) | |
2785 add = TRUE; | |
2786 else | |
2787 { | |
2788 cols = comp_textwidth(FALSE); | |
2789 if (cols == 0) | |
2790 cols = 79; | |
2791 add = (p->col + cols < col || col + cols < p->col); | |
2792 } | |
2793 } | |
2794 if (add) | |
2795 { | |
2796 /* This is the first of a new sequence of undo-able changes | |
2797 * and it's at some distance of the last change. Use a new | |
2798 * position in the changelist. */ | |
2799 curbuf->b_new_change = FALSE; | |
2800 | |
2801 if (curbuf->b_changelistlen == JUMPLISTSIZE) | |
2802 { | |
2803 /* changelist is full: remove oldest entry */ | |
2804 curbuf->b_changelistlen = JUMPLISTSIZE - 1; | |
2805 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, | |
2806 sizeof(pos_T) * (JUMPLISTSIZE - 1)); | |
1863 | 2807 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 2808 { |
2809 /* Correct position in changelist for other windows on | |
2810 * this buffer. */ | |
2811 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) | |
2812 --wp->w_changelistidx; | |
2813 } | |
2814 } | |
1863 | 2815 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 2816 { |
2817 /* For other windows, if the position in the changelist is | |
2818 * at the end it stays at the end. */ | |
2819 if (wp->w_buffer == curbuf | |
2820 && wp->w_changelistidx == curbuf->b_changelistlen) | |
2821 ++wp->w_changelistidx; | |
2822 } | |
2823 ++curbuf->b_changelistlen; | |
2824 } | |
2825 } | |
2826 curbuf->b_changelist[curbuf->b_changelistlen - 1] = | |
2827 curbuf->b_last_change; | |
2828 /* The current window is always after the last change, so that "g," | |
2829 * takes you back to it. */ | |
2830 curwin->w_changelistidx = curbuf->b_changelistlen; | |
2831 #endif | |
2832 } | |
2833 | |
1863 | 2834 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 2835 { |
2836 if (wp->w_buffer == curbuf) | |
2837 { | |
2838 /* Mark this window to be redrawn later. */ | |
2839 if (wp->w_redr_type < VALID) | |
2840 wp->w_redr_type = VALID; | |
2841 | |
2842 /* Check if a change in the buffer has invalidated the cached | |
2843 * values for the cursor. */ | |
2844 #ifdef FEAT_FOLDING | |
2845 /* | |
2846 * Update the folds for this window. Can't postpone this, because | |
2847 * a following operator might work on the whole fold: ">>dd". | |
2848 */ | |
2849 foldUpdate(wp, lnum, lnume + xtra - 1); | |
2850 | |
2851 /* The change may cause lines above or below the change to become | |
2852 * included in a fold. Set lnum/lnume to the first/last line that | |
2853 * might be displayed differently. | |
2854 * Set w_cline_folded here as an efficient way to update it when | |
2855 * inserting lines just above a closed fold. */ | |
2856 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); | |
2857 if (wp->w_cursor.lnum == lnum) | |
2858 wp->w_cline_folded = i; | |
2859 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL); | |
2860 if (wp->w_cursor.lnum == lnume) | |
2861 wp->w_cline_folded = i; | |
2862 | |
2863 /* If the changed line is in a range of previously folded lines, | |
2864 * compare with the first line in that range. */ | |
2865 if (wp->w_cursor.lnum <= lnum) | |
2866 { | |
2867 i = find_wl_entry(wp, lnum); | |
2868 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) | |
2869 changed_line_abv_curs_win(wp); | |
2870 } | |
2871 #endif | |
2872 | |
2873 if (wp->w_cursor.lnum > lnum) | |
2874 changed_line_abv_curs_win(wp); | |
2875 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) | |
2876 changed_cline_bef_curs_win(wp); | |
2877 if (wp->w_botline >= lnum) | |
2878 { | |
2879 /* Assume that botline doesn't change (inserted lines make | |
2880 * other lines scroll down below botline). */ | |
2881 approximate_botline_win(wp); | |
2882 } | |
2883 | |
2884 /* Check if any w_lines[] entries have become invalid. | |
2885 * For entries below the change: Correct the lnums for | |
2886 * inserted/deleted lines. Makes it possible to stop displaying | |
2887 * after the change. */ | |
2888 for (i = 0; i < wp->w_lines_valid; ++i) | |
2889 if (wp->w_lines[i].wl_valid) | |
2890 { | |
2891 if (wp->w_lines[i].wl_lnum >= lnum) | |
2892 { | |
2893 if (wp->w_lines[i].wl_lnum < lnume) | |
2894 { | |
2895 /* line included in change */ | |
2896 wp->w_lines[i].wl_valid = FALSE; | |
2897 } | |
2898 else if (xtra != 0) | |
2899 { | |
2900 /* line below change */ | |
2901 wp->w_lines[i].wl_lnum += xtra; | |
2902 #ifdef FEAT_FOLDING | |
2903 wp->w_lines[i].wl_lastlnum += xtra; | |
2904 #endif | |
2905 } | |
2906 } | |
2907 #ifdef FEAT_FOLDING | |
2908 else if (wp->w_lines[i].wl_lastlnum >= lnum) | |
2909 { | |
2910 /* change somewhere inside this range of folded lines, | |
2911 * may need to be redrawn */ | |
2912 wp->w_lines[i].wl_valid = FALSE; | |
2913 } | |
2914 #endif | |
2915 } | |
1987 | 2916 |
2917 #ifdef FEAT_FOLDING | |
2918 /* Take care of side effects for setting w_topline when folds have | |
2919 * changed. Esp. when the buffer was changed in another window. */ | |
2920 if (hasAnyFolding(wp)) | |
2921 set_topline(wp, wp->w_topline); | |
2922 #endif | |
7 | 2923 } |
2924 } | |
2925 | |
2926 /* Call update_screen() later, which checks out what needs to be redrawn, | |
2927 * since it notices b_mod_set and then uses b_mod_*. */ | |
2928 if (must_redraw < VALID) | |
2929 must_redraw = VALID; | |
694 | 2930 |
2931 #ifdef FEAT_AUTOCMD | |
2932 /* when the cursor line is changed always trigger CursorMoved */ | |
1010 | 2933 if (lnum <= curwin->w_cursor.lnum |
2934 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) | |
694 | 2935 last_cursormoved.lnum = 0; |
2936 #endif | |
7 | 2937 } |
2938 | |
2939 /* | |
2940 * unchanged() is called when the changed flag must be reset for buffer 'buf' | |
2941 */ | |
2942 void | |
2943 unchanged(buf, ff) | |
2944 buf_T *buf; | |
2945 int ff; /* also reset 'fileformat' */ | |
2946 { | |
2685 | 2947 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
7 | 2948 { |
2949 buf->b_changed = 0; | |
39 | 2950 ml_setflags(buf); |
7 | 2951 if (ff) |
2952 save_file_ff(buf); | |
2953 #ifdef FEAT_WINDOWS | |
2954 check_status(buf); | |
673 | 2955 redraw_tabline = TRUE; |
7 | 2956 #endif |
2957 #ifdef FEAT_TITLE | |
2958 need_maketitle = TRUE; /* set window title later */ | |
2959 #endif | |
2960 } | |
2961 ++buf->b_changedtick; | |
2962 #ifdef FEAT_NETBEANS_INTG | |
2963 netbeans_unmodified(buf); | |
2964 #endif | |
2965 } | |
2966 | |
2967 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
2968 /* | |
2969 * check_status: called when the status bars for the buffer 'buf' | |
2970 * need to be updated | |
2971 */ | |
2972 void | |
2973 check_status(buf) | |
2974 buf_T *buf; | |
2975 { | |
2976 win_T *wp; | |
2977 | |
2978 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2979 if (wp->w_buffer == buf && wp->w_status_height) | |
2980 { | |
2981 wp->w_redr_status = TRUE; | |
2982 if (must_redraw < VALID) | |
2983 must_redraw = VALID; | |
2984 } | |
2985 } | |
2986 #endif | |
2987 | |
2988 /* | |
2989 * If the file is readonly, give a warning message with the first change. | |
2990 * Don't do this for autocommands. | |
2991 * Don't use emsg(), because it flushes the macro buffer. | |
548 | 2992 * If we have undone all changes b_changed will be FALSE, but "b_did_warn" |
7 | 2993 * will be TRUE. |
2189 | 2994 * Careful: may trigger autocommands that reload the buffer. |
7 | 2995 */ |
2996 void | |
2997 change_warning(col) | |
2998 int col; /* column for message; non-zero when in insert | |
2999 mode and 'showmode' is on */ | |
3000 { | |
1848 | 3001 static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
3002 | |
7 | 3003 if (curbuf->b_did_warn == FALSE |
3004 && curbufIsChanged() == 0 | |
3005 #ifdef FEAT_AUTOCMD | |
3006 && !autocmd_busy | |
3007 #endif | |
3008 && curbuf->b_p_ro) | |
3009 { | |
3010 #ifdef FEAT_AUTOCMD | |
819 | 3011 ++curbuf_lock; |
7 | 3012 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
819 | 3013 --curbuf_lock; |
7 | 3014 if (!curbuf->b_p_ro) |
3015 return; | |
3016 #endif | |
3017 /* | |
3018 * Do what msg() does, but with a column offset if the warning should | |
3019 * be after the mode message. | |
3020 */ | |
3021 msg_start(); | |
3022 if (msg_row == Rows - 1) | |
3023 msg_col = col; | |
16 | 3024 msg_source(hl_attr(HLF_W)); |
1848 | 3025 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST); |
3026 #ifdef FEAT_EVAL | |
3027 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); | |
3028 #endif | |
7 | 3029 msg_clr_eos(); |
3030 (void)msg_end(); | |
3031 if (msg_silent == 0 && !silent_mode) | |
3032 { | |
3033 out_flush(); | |
3034 ui_delay(1000L, TRUE); /* give the user time to think about it */ | |
3035 } | |
3036 curbuf->b_did_warn = TRUE; | |
3037 redraw_cmdline = FALSE; /* don't redraw and erase the message */ | |
3038 if (msg_row < Rows - 1) | |
3039 showmode(); | |
3040 } | |
3041 } | |
3042 | |
3043 /* | |
3044 * Ask for a reply from the user, a 'y' or a 'n'. | |
3045 * No other characters are accepted, the message is repeated until a valid | |
3046 * reply is entered or CTRL-C is hit. | |
3047 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters | |
3048 * from any buffers but directly from the user. | |
3049 * | |
3050 * return the 'y' or 'n' | |
3051 */ | |
3052 int | |
3053 ask_yesno(str, direct) | |
3054 char_u *str; | |
3055 int direct; | |
3056 { | |
3057 int r = ' '; | |
3058 int save_State = State; | |
3059 | |
3060 if (exiting) /* put terminal in raw mode for this question */ | |
3061 settmode(TMODE_RAW); | |
3062 ++no_wait_return; | |
3063 #ifdef USE_ON_FLY_SCROLL | |
3064 dont_scroll = TRUE; /* disallow scrolling here */ | |
3065 #endif | |
3066 State = CONFIRM; /* mouse behaves like with :confirm */ | |
3067 #ifdef FEAT_MOUSE | |
3068 setmouse(); /* disables mouse for xterm */ | |
3069 #endif | |
3070 ++no_mapping; | |
3071 ++allow_keys; /* no mapping here, but recognize keys */ | |
3072 | |
3073 while (r != 'y' && r != 'n') | |
3074 { | |
3075 /* same highlighting as for wait_return */ | |
3076 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str); | |
3077 if (direct) | |
3078 r = get_keystroke(); | |
3079 else | |
1474 | 3080 r = plain_vgetc(); |
7 | 3081 if (r == Ctrl_C || r == ESC) |
3082 r = 'n'; | |
3083 msg_putchar(r); /* show what you typed */ | |
3084 out_flush(); | |
3085 } | |
3086 --no_wait_return; | |
3087 State = save_State; | |
3088 #ifdef FEAT_MOUSE | |
3089 setmouse(); | |
3090 #endif | |
3091 --no_mapping; | |
3092 --allow_keys; | |
3093 | |
3094 return r; | |
3095 } | |
3096 | |
3097 /* | |
3098 * Get a key stroke directly from the user. | |
3099 * Ignores mouse clicks and scrollbar events, except a click for the left | |
3100 * button (used at the more prompt). | |
3101 * Doesn't use vgetc(), because it syncs undo and eats mapped characters. | |
3102 * Disadvantage: typeahead is ignored. | |
3103 * Translates the interrupt character for unix to ESC. | |
3104 */ | |
3105 int | |
3106 get_keystroke() | |
3107 { | |
3108 #define CBUFLEN 151 | |
3109 char_u buf[CBUFLEN]; | |
3110 int len = 0; | |
3111 int n; | |
3112 int save_mapped_ctrl_c = mapped_ctrl_c; | |
964 | 3113 int waited = 0; |
7 | 3114 |
3115 mapped_ctrl_c = FALSE; /* mappings are not used here */ | |
3116 for (;;) | |
3117 { | |
3118 cursor_on(); | |
3119 out_flush(); | |
3120 | |
3121 /* First time: blocking wait. Second time: wait up to 100ms for a | |
3122 * terminal code to complete. Leave some room for check_termcode() to | |
3123 * insert a key code into (max 5 chars plus NUL). And | |
3124 * fix_input_buffer() can triple the number of bytes. */ | |
3125 n = ui_inchar(buf + len, (CBUFLEN - 6 - len) / 3, | |
3126 len == 0 ? -1L : 100L, 0); | |
3127 if (n > 0) | |
3128 { | |
3129 /* Replace zero and CSI by a special key code. */ | |
3130 n = fix_input_buffer(buf + len, n, FALSE); | |
3131 len += n; | |
964 | 3132 waited = 0; |
3133 } | |
3134 else if (len > 0) | |
3135 ++waited; /* keep track of the waiting time */ | |
3136 | |
3137 /* Incomplete termcode and not timed out yet: get more characters */ | |
3138 if ((n = check_termcode(1, buf, len)) < 0 | |
3139 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm))) | |
7 | 3140 continue; |
964 | 3141 |
2672 | 3142 if (n == KEYLEN_REMOVED) /* key code removed */ |
2721 | 3143 { |
2723 | 3144 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0) |
2721 | 3145 { |
3146 /* Redrawing was postponed, do it now. */ | |
3147 update_screen(0); | |
3148 setcursor(); /* put cursor back where it belongs */ | |
3149 } | |
2672 | 3150 continue; |
2721 | 3151 } |
2672 | 3152 if (n > 0) /* found a termcode: adjust length */ |
7 | 3153 len = n; |
2672 | 3154 if (len == 0) /* nothing typed yet */ |
7 | 3155 continue; |
3156 | |
3157 /* Handle modifier and/or special key code. */ | |
3158 n = buf[0]; | |
3159 if (n == K_SPECIAL) | |
3160 { | |
3161 n = TO_SPECIAL(buf[1], buf[2]); | |
3162 if (buf[1] == KS_MODIFIER | |
3163 || n == K_IGNORE | |
3164 #ifdef FEAT_MOUSE | |
3165 || n == K_LEFTMOUSE_NM | |
3166 || n == K_LEFTDRAG | |
3167 || n == K_LEFTRELEASE | |
3168 || n == K_LEFTRELEASE_NM | |
3169 || n == K_MIDDLEMOUSE | |
3170 || n == K_MIDDLEDRAG | |
3171 || n == K_MIDDLERELEASE | |
3172 || n == K_RIGHTMOUSE | |
3173 || n == K_RIGHTDRAG | |
3174 || n == K_RIGHTRELEASE | |
3175 || n == K_MOUSEDOWN | |
3176 || n == K_MOUSEUP | |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2387
diff
changeset
|
3177 || n == K_MOUSELEFT |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2387
diff
changeset
|
3178 || n == K_MOUSERIGHT |
7 | 3179 || n == K_X1MOUSE |
3180 || n == K_X1DRAG | |
3181 || n == K_X1RELEASE | |
3182 || n == K_X2MOUSE | |
3183 || n == K_X2DRAG | |
3184 || n == K_X2RELEASE | |
3185 # ifdef FEAT_GUI | |
3186 || n == K_VER_SCROLLBAR | |
3187 || n == K_HOR_SCROLLBAR | |
3188 # endif | |
3189 #endif | |
3190 ) | |
3191 { | |
3192 if (buf[1] == KS_MODIFIER) | |
3193 mod_mask = buf[2]; | |
3194 len -= 3; | |
3195 if (len > 0) | |
3196 mch_memmove(buf, buf + 3, (size_t)len); | |
3197 continue; | |
3198 } | |
828 | 3199 break; |
7 | 3200 } |
3201 #ifdef FEAT_MBYTE | |
3202 if (has_mbyte) | |
3203 { | |
3204 if (MB_BYTE2LEN(n) > len) | |
3205 continue; /* more bytes to get */ | |
3206 buf[len >= CBUFLEN ? CBUFLEN - 1 : len] = NUL; | |
3207 n = (*mb_ptr2char)(buf); | |
3208 } | |
3209 #endif | |
3210 #ifdef UNIX | |
3211 if (n == intr_char) | |
3212 n = ESC; | |
3213 #endif | |
3214 break; | |
3215 } | |
3216 | |
3217 mapped_ctrl_c = save_mapped_ctrl_c; | |
3218 return n; | |
3219 } | |
3220 | |
3221 /* | |
374 | 3222 * Get a number from the user. |
3223 * When "mouse_used" is not NULL allow using the mouse. | |
7 | 3224 */ |
3225 int | |
374 | 3226 get_number(colon, mouse_used) |
3227 int colon; /* allow colon to abort */ | |
3228 int *mouse_used; | |
7 | 3229 { |
3230 int n = 0; | |
3231 int c; | |
810 | 3232 int typed = 0; |
7 | 3233 |
374 | 3234 if (mouse_used != NULL) |
3235 *mouse_used = FALSE; | |
3236 | |
7 | 3237 /* When not printing messages, the user won't know what to type, return a |
3238 * zero (as if CR was hit). */ | |
3239 if (msg_silent != 0) | |
3240 return 0; | |
3241 | |
3242 #ifdef USE_ON_FLY_SCROLL | |
3243 dont_scroll = TRUE; /* disallow scrolling here */ | |
3244 #endif | |
3245 ++no_mapping; | |
3246 ++allow_keys; /* no mapping here, but recognize keys */ | |
3247 for (;;) | |
3248 { | |
3249 windgoto(msg_row, msg_col); | |
3250 c = safe_vgetc(); | |
3251 if (VIM_ISDIGIT(c)) | |
3252 { | |
3253 n = n * 10 + c - '0'; | |
3254 msg_putchar(c); | |
810 | 3255 ++typed; |
7 | 3256 } |
3257 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H) | |
3258 { | |
810 | 3259 if (typed > 0) |
3260 { | |
3261 MSG_PUTS("\b \b"); | |
3262 --typed; | |
3263 } | |
7 | 3264 n /= 10; |
3265 } | |
374 | 3266 #ifdef FEAT_MOUSE |
3267 else if (mouse_used != NULL && c == K_LEFTMOUSE) | |
3268 { | |
3269 *mouse_used = TRUE; | |
3270 n = mouse_row + 1; | |
3271 break; | |
3272 } | |
3273 #endif | |
7 | 3274 else if (n == 0 && c == ':' && colon) |
3275 { | |
3276 stuffcharReadbuff(':'); | |
3277 if (!exmode_active) | |
3278 cmdline_row = msg_row; | |
3279 skip_redraw = TRUE; /* skip redraw once */ | |
3280 do_redraw = FALSE; | |
3281 break; | |
3282 } | |
3283 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC) | |
3284 break; | |
3285 } | |
3286 --no_mapping; | |
3287 --allow_keys; | |
3288 return n; | |
3289 } | |
3290 | |
323 | 3291 /* |
3292 * Ask the user to enter a number. | |
374 | 3293 * When "mouse_used" is not NULL allow using the mouse and in that case return |
3294 * the line number. | |
323 | 3295 */ |
3296 int | |
374 | 3297 prompt_for_number(mouse_used) |
3298 int *mouse_used; | |
323 | 3299 { |
3300 int i; | |
344 | 3301 int save_cmdline_row; |
3302 int save_State; | |
323 | 3303 |
3304 /* When using ":silent" assume that <CR> was entered. */ | |
375 | 3305 if (mouse_used != NULL) |
1733 | 3306 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): ")); |
375 | 3307 else |
1733 | 3308 MSG_PUTS(_("Type number and <Enter> (empty cancels): ")); |
344 | 3309 |
957 | 3310 /* Set the state such that text can be selected/copied/pasted and we still |
3311 * get mouse events. */ | |
344 | 3312 save_cmdline_row = cmdline_row; |
957 | 3313 cmdline_row = 0; |
344 | 3314 save_State = State; |
957 | 3315 State = CMDLINE; |
374 | 3316 |
3317 i = get_number(TRUE, mouse_used); | |
3318 if (KeyTyped) | |
3319 { | |
3320 /* don't call wait_return() now */ | |
3321 /* msg_putchar('\n'); */ | |
323 | 3322 cmdline_row = msg_row - 1; |
3323 need_wait_return = FALSE; | |
3324 msg_didany = FALSE; | |
1938 | 3325 msg_didout = FALSE; |
323 | 3326 } |
344 | 3327 else |
3328 cmdline_row = save_cmdline_row; | |
3329 State = save_State; | |
3330 | |
323 | 3331 return i; |
3332 } | |
3333 | |
7 | 3334 void |
3335 msgmore(n) | |
3336 long n; | |
3337 { | |
3338 long pn; | |
3339 | |
3340 if (global_busy /* no messages now, wait until global is finished */ | |
3341 || !messaging()) /* 'lazyredraw' set, don't do messages now */ | |
3342 return; | |
3343 | |
135 | 3344 /* We don't want to overwrite another important message, but do overwrite |
3345 * a previous "more lines" or "fewer lines" message, so that "5dd" and | |
3346 * then "put" reports the last action. */ | |
3347 if (keep_msg != NULL && !keep_msg_more) | |
3348 return; | |
3349 | |
7 | 3350 if (n > 0) |
3351 pn = n; | |
3352 else | |
3353 pn = -n; | |
3354 | |
3355 if (pn > p_report) | |
3356 { | |
3357 if (pn == 1) | |
3358 { | |
3359 if (n > 0) | |
2768 | 3360 vim_strncpy(msg_buf, (char_u *)_("1 more line"), |
3361 MSG_BUF_LEN - 1); | |
7 | 3362 else |
2768 | 3363 vim_strncpy(msg_buf, (char_u *)_("1 line less"), |
3364 MSG_BUF_LEN - 1); | |
7 | 3365 } |
3366 else | |
3367 { | |
3368 if (n > 0) | |
2768 | 3369 vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
3370 _("%ld more lines"), pn); | |
7 | 3371 else |
2768 | 3372 vim_snprintf((char *)msg_buf, MSG_BUF_LEN, |
3373 _("%ld fewer lines"), pn); | |
7 | 3374 } |
3375 if (got_int) | |
2768 | 3376 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN); |
7 | 3377 if (msg(msg_buf)) |
3378 { | |
680 | 3379 set_keep_msg(msg_buf, 0); |
135 | 3380 keep_msg_more = TRUE; |
7 | 3381 } |
3382 } | |
3383 } | |
3384 | |
3385 /* | |
3386 * flush map and typeahead buffers and give a warning for an error | |
3387 */ | |
3388 void | |
3389 beep_flush() | |
3390 { | |
3391 if (emsg_silent == 0) | |
3392 { | |
3393 flush_buffers(FALSE); | |
3394 vim_beep(); | |
3395 } | |
3396 } | |
3397 | |
3398 /* | |
3399 * give a warning for an error | |
3400 */ | |
3401 void | |
3402 vim_beep() | |
3403 { | |
3404 if (emsg_silent == 0) | |
3405 { | |
3406 if (p_vb | |
3407 #ifdef FEAT_GUI | |
3408 /* While the GUI is starting up the termcap is set for the GUI | |
3409 * but the output still goes to a terminal. */ | |
3410 && !(gui.in_use && gui.starting) | |
3411 #endif | |
3412 ) | |
3413 { | |
3414 out_str(T_VB); | |
3415 } | |
3416 else | |
3417 { | |
3418 #ifdef MSDOS | |
3419 /* | |
3420 * The number of beeps outputted is reduced to avoid having to wait | |
3421 * for all the beeps to finish. This is only a problem on systems | |
3422 * where the beeps don't overlap. | |
3423 */ | |
3424 if (beep_count == 0 || beep_count == 10) | |
3425 { | |
3426 out_char(BELL); | |
3427 beep_count = 1; | |
3428 } | |
3429 else | |
3430 ++beep_count; | |
3431 #else | |
3432 out_char(BELL); | |
3433 #endif | |
3434 } | |
169 | 3435 |
3436 /* When 'verbose' is set and we are sourcing a script or executing a | |
3437 * function give the user a hint where the beep comes from. */ | |
3438 if (vim_strchr(p_debug, 'e') != NULL) | |
3439 { | |
3440 msg_source(hl_attr(HLF_W)); | |
3441 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W)); | |
3442 } | |
7 | 3443 } |
3444 } | |
3445 | |
3446 /* | |
3447 * To get the "real" home directory: | |
3448 * - get value of $HOME | |
3449 * For Unix: | |
3450 * - go to that directory | |
3451 * - do mch_dirname() to get the real name of that directory. | |
3452 * This also works with mounts and links. | |
3453 * Don't do this for MS-DOS, it will change the "current dir" for a drive. | |
3454 */ | |
3455 static char_u *homedir = NULL; | |
3456 | |
3457 void | |
3458 init_homedir() | |
3459 { | |
3460 char_u *var; | |
3461 | |
170 | 3462 /* In case we are called a second time (when 'encoding' changes). */ |
3463 vim_free(homedir); | |
3464 homedir = NULL; | |
3465 | |
7 | 3466 #ifdef VMS |
3467 var = mch_getenv((char_u *)"SYS$LOGIN"); | |
3468 #else | |
3469 var = mch_getenv((char_u *)"HOME"); | |
3470 #endif | |
3471 | |
3472 if (var != NULL && *var == NUL) /* empty is same as not set */ | |
3473 var = NULL; | |
3474 | |
3475 #ifdef WIN3264 | |
3476 /* | |
3477 * Weird but true: $HOME may contain an indirect reference to another | |
3478 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set | |
3479 * when $HOME is being set. | |
3480 */ | |
3481 if (var != NULL && *var == '%') | |
3482 { | |
3483 char_u *p; | |
3484 char_u *exp; | |
3485 | |
3486 p = vim_strchr(var + 1, '%'); | |
3487 if (p != NULL) | |
3488 { | |
419 | 3489 vim_strncpy(NameBuff, var + 1, p - (var + 1)); |
7 | 3490 exp = mch_getenv(NameBuff); |
3491 if (exp != NULL && *exp != NUL | |
3492 && STRLEN(exp) + STRLEN(p) < MAXPATHL) | |
3493 { | |
274 | 3494 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1); |
7 | 3495 var = NameBuff; |
3496 /* Also set $HOME, it's needed for _viminfo. */ | |
3497 vim_setenv((char_u *)"HOME", NameBuff); | |
3498 } | |
3499 } | |
3500 } | |
3501 | |
3502 /* | |
3503 * Typically, $HOME is not defined on Windows, unless the user has | |
3504 * specifically defined it for Vim's sake. However, on Windows NT | |
3505 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for | |
3506 * each user. Try constructing $HOME from these. | |
3507 */ | |
3508 if (var == NULL) | |
3509 { | |
3510 char_u *homedrive, *homepath; | |
3511 | |
3512 homedrive = mch_getenv((char_u *)"HOMEDRIVE"); | |
3513 homepath = mch_getenv((char_u *)"HOMEPATH"); | |
2030
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3514 if (homepath == NULL || *homepath == NUL) |
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3515 homepath = "\\"; |
66b1822ba176
updated for version 7.2.326
Bram Moolenaar <bram@zimbu.org>
parents:
2017
diff
changeset
|
3516 if (homedrive != NULL |
7 | 3517 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL) |
3518 { | |
3519 sprintf((char *)NameBuff, "%s%s", homedrive, homepath); | |
3520 if (NameBuff[0] != NUL) | |
3521 { | |
3522 var = NameBuff; | |
3523 /* Also set $HOME, it's needed for _viminfo. */ | |
3524 vim_setenv((char_u *)"HOME", NameBuff); | |
3525 } | |
3526 } | |
3527 } | |
170 | 3528 |
3529 # if defined(FEAT_MBYTE) | |
3530 if (enc_utf8 && var != NULL) | |
3531 { | |
3532 int len; | |
2786 | 3533 char_u *pp = NULL; |
170 | 3534 |
3535 /* Convert from active codepage to UTF-8. Other conversions are | |
3536 * not done, because they would fail for non-ASCII characters. */ | |
835 | 3537 acp_to_enc(var, (int)STRLEN(var), &pp, &len); |
170 | 3538 if (pp != NULL) |
3539 { | |
3540 homedir = pp; | |
3541 return; | |
3542 } | |
3543 } | |
3544 # endif | |
7 | 3545 #endif |
3546 | |
3547 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) | |
3548 /* | |
3549 * Default home dir is C:/ | |
3550 * Best assumption we can make in such a situation. | |
3551 */ | |
3552 if (var == NULL) | |
3553 var = "C:/"; | |
3554 #endif | |
3555 if (var != NULL) | |
3556 { | |
3557 #ifdef UNIX | |
3558 /* | |
3559 * Change to the directory and get the actual path. This resolves | |
3560 * links. Don't do it when we can't return. | |
3561 */ | |
3562 if (mch_dirname(NameBuff, MAXPATHL) == OK | |
3563 && mch_chdir((char *)NameBuff) == 0) | |
3564 { | |
3565 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK) | |
3566 var = IObuff; | |
3567 if (mch_chdir((char *)NameBuff) != 0) | |
3568 EMSG(_(e_prev_dir)); | |
3569 } | |
3570 #endif | |
3571 homedir = vim_strsave(var); | |
3572 } | |
3573 } | |
3574 | |
359 | 3575 #if defined(EXITFREE) || defined(PROTO) |
3576 void | |
3577 free_homedir() | |
3578 { | |
3579 vim_free(homedir); | |
3580 } | |
3581 #endif | |
3582 | |
7 | 3583 /* |
1408 | 3584 * Call expand_env() and store the result in an allocated string. |
3585 * This is not very memory efficient, this expects the result to be freed | |
3586 * again soon. | |
3587 */ | |
3588 char_u * | |
3589 expand_env_save(src) | |
3590 char_u *src; | |
3591 { | |
3592 return expand_env_save_opt(src, FALSE); | |
3593 } | |
3594 | |
3595 /* | |
3596 * Idem, but when "one" is TRUE handle the string as one file name, only | |
3597 * expand "~" at the start. | |
3598 */ | |
3599 char_u * | |
3600 expand_env_save_opt(src, one) | |
3601 char_u *src; | |
3602 int one; | |
3603 { | |
3604 char_u *p; | |
3605 | |
3606 p = alloc(MAXPATHL); | |
3607 if (p != NULL) | |
3608 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL); | |
3609 return p; | |
3610 } | |
3611 | |
3612 /* | |
7 | 3613 * Expand environment variable with path name. |
3614 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded. | |
1408 | 3615 * Skips over "\ ", "\~" and "\$" (not for Win32 though). |
7 | 3616 * If anything fails no expansion is done and dst equals src. |
3617 */ | |
3618 void | |
3619 expand_env(src, dst, dstlen) | |
3620 char_u *src; /* input string e.g. "$HOME/vim.hlp" */ | |
3621 char_u *dst; /* where to put the result */ | |
3622 int dstlen; /* maximum length of the result */ | |
3623 { | |
1408 | 3624 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL); |
7 | 3625 } |
3626 | |
3627 void | |
1408 | 3628 expand_env_esc(srcp, dst, dstlen, esc, one, startstr) |
374 | 3629 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */ |
7 | 3630 char_u *dst; /* where to put the result */ |
3631 int dstlen; /* maximum length of the result */ | |
3632 int esc; /* escape spaces in expanded variables */ | |
1408 | 3633 int one; /* "srcp" is one file name */ |
374 | 3634 char_u *startstr; /* start again after this (can be NULL) */ |
3635 { | |
3636 char_u *src; | |
7 | 3637 char_u *tail; |
3638 int c; | |
3639 char_u *var; | |
3640 int copy_char; | |
3641 int mustfree; /* var was allocated, need to free it later */ | |
3642 int at_start = TRUE; /* at start of a name */ | |
374 | 3643 int startstr_len = 0; |
3644 | |
3645 if (startstr != NULL) | |
835 | 3646 startstr_len = (int)STRLEN(startstr); |
374 | 3647 |
3648 src = skipwhite(srcp); | |
7 | 3649 --dstlen; /* leave one char space for "\," */ |
3650 while (*src && dstlen > 0) | |
3651 { | |
3652 copy_char = TRUE; | |
22 | 3653 if ((*src == '$' |
3654 #ifdef VMS | |
3655 && at_start | |
3656 #endif | |
3657 ) | |
7 | 3658 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
3659 || *src == '%' | |
3660 #endif | |
3661 || (*src == '~' && at_start)) | |
3662 { | |
3663 mustfree = FALSE; | |
3664 | |
3665 /* | |
3666 * The variable name is copied into dst temporarily, because it may | |
3667 * be a string in read-only memory and a NUL needs to be appended. | |
3668 */ | |
3669 if (*src != '~') /* environment var */ | |
3670 { | |
3671 tail = src + 1; | |
3672 var = dst; | |
3673 c = dstlen - 1; | |
3674 | |
3675 #ifdef UNIX | |
3676 /* Unix has ${var-name} type environment vars */ | |
3677 if (*tail == '{' && !vim_isIDc('{')) | |
3678 { | |
3679 tail++; /* ignore '{' */ | |
3680 while (c-- > 0 && *tail && *tail != '}') | |
3681 *var++ = *tail++; | |
3682 } | |
3683 else | |
3684 #endif | |
3685 { | |
3686 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail)) | |
3687 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
3688 || (*src == '%' && *tail != '%') | |
3689 #endif | |
3690 )) | |
3691 { | |
3692 #ifdef OS2 /* env vars only in uppercase */ | |
3693 *var++ = TOUPPER_LOC(*tail); | |
3694 tail++; /* toupper() may be a macro! */ | |
3695 #else | |
3696 *var++ = *tail++; | |
3697 #endif | |
3698 } | |
3699 } | |
3700 | |
3701 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) | |
3702 # ifdef UNIX | |
3703 if (src[1] == '{' && *tail != '}') | |
3704 # else | |
3705 if (*src == '%' && *tail != '%') | |
3706 # endif | |
3707 var = NULL; | |
3708 else | |
3709 { | |
3710 # ifdef UNIX | |
3711 if (src[1] == '{') | |
3712 # else | |
3713 if (*src == '%') | |
3714 #endif | |
3715 ++tail; | |
3716 #endif | |
3717 *var = NUL; | |
3718 var = vim_getenv(dst, &mustfree); | |
3719 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX) | |
3720 } | |
3721 #endif | |
3722 } | |
3723 /* home directory */ | |
3724 else if ( src[1] == NUL | |
3725 || vim_ispathsep(src[1]) | |
3726 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL) | |
3727 { | |
3728 var = homedir; | |
3729 tail = src + 1; | |
3730 } | |
3731 else /* user directory */ | |
3732 { | |
3733 #if defined(UNIX) || (defined(VMS) && defined(USER_HOME)) | |
3734 /* | |
3735 * Copy ~user to dst[], so we can put a NUL after it. | |
3736 */ | |
3737 tail = src; | |
3738 var = dst; | |
3739 c = dstlen - 1; | |
3740 while ( c-- > 0 | |
3741 && *tail | |
3742 && vim_isfilec(*tail) | |
3743 && !vim_ispathsep(*tail)) | |
3744 *var++ = *tail++; | |
3745 *var = NUL; | |
3746 # ifdef UNIX | |
3747 /* | |
3748 * If the system supports getpwnam(), use it. | |
3749 * Otherwise, or if getpwnam() fails, the shell is used to | |
3750 * expand ~user. This is slower and may fail if the shell | |
3751 * does not support ~user (old versions of /bin/sh). | |
3752 */ | |
3753 # if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H) | |
3754 { | |
3755 struct passwd *pw; | |
3756 | |
626 | 3757 /* Note: memory allocated by getpwnam() is never freed. |
3758 * Calling endpwent() apparently doesn't help. */ | |
7 | 3759 pw = getpwnam((char *)dst + 1); |
3760 if (pw != NULL) | |
3761 var = (char_u *)pw->pw_dir; | |
3762 else | |
3763 var = NULL; | |
3764 } | |
3765 if (var == NULL) | |
3766 # endif | |
3767 { | |
3768 expand_T xpc; | |
3769 | |
3770 ExpandInit(&xpc); | |
3771 xpc.xp_context = EXPAND_FILES; | |
3772 var = ExpandOne(&xpc, dst, NULL, | |
3773 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE); | |
3774 mustfree = TRUE; | |
3775 } | |
3776 | |
3777 # else /* !UNIX, thus VMS */ | |
3778 /* | |
3779 * USER_HOME is a comma-separated list of | |
3780 * directories to search for the user account in. | |
3781 */ | |
3782 { | |
3783 char_u test[MAXPATHL], paths[MAXPATHL]; | |
3784 char_u *path, *next_path, *ptr; | |
3785 struct stat st; | |
3786 | |
3787 STRCPY(paths, USER_HOME); | |
3788 next_path = paths; | |
3789 while (*next_path) | |
3790 { | |
3791 for (path = next_path; *next_path && *next_path != ','; | |
3792 next_path++); | |
3793 if (*next_path) | |
3794 *next_path++ = NUL; | |
3795 STRCPY(test, path); | |
3796 STRCAT(test, "/"); | |
3797 STRCAT(test, dst + 1); | |
3798 if (mch_stat(test, &st) == 0) | |
3799 { | |
3800 var = alloc(STRLEN(test) + 1); | |
3801 STRCPY(var, test); | |
3802 mustfree = TRUE; | |
3803 break; | |
3804 } | |
3805 } | |
3806 } | |
3807 # endif /* UNIX */ | |
3808 #else | |
3809 /* cannot expand user's home directory, so don't try */ | |
3810 var = NULL; | |
3811 tail = (char_u *)""; /* for gcc */ | |
3812 #endif /* UNIX || VMS */ | |
3813 } | |
3814 | |
3815 #ifdef BACKSLASH_IN_FILENAME | |
3816 /* If 'shellslash' is set change backslashes to forward slashes. | |
3817 * Can't use slash_adjust(), p_ssl may be set temporarily. */ | |
3818 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL) | |
3819 { | |
3820 char_u *p = vim_strsave(var); | |
3821 | |
3822 if (p != NULL) | |
3823 { | |
3824 if (mustfree) | |
3825 vim_free(var); | |
3826 var = p; | |
3827 mustfree = TRUE; | |
3828 forward_slash(var); | |
3829 } | |
3830 } | |
3831 #endif | |
3832 | |
3833 /* If "var" contains white space, escape it with a backslash. | |
3834 * Required for ":e ~/tt" when $HOME includes a space. */ | |
3835 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL) | |
3836 { | |
3837 char_u *p = vim_strsave_escaped(var, (char_u *)" \t"); | |
3838 | |
3839 if (p != NULL) | |
3840 { | |
3841 if (mustfree) | |
3842 vim_free(var); | |
3843 var = p; | |
3844 mustfree = TRUE; | |
3845 } | |
3846 } | |
3847 | |
3848 if (var != NULL && *var != NUL | |
3849 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen)) | |
3850 { | |
3851 STRCPY(dst, var); | |
3852 dstlen -= (int)STRLEN(var); | |
835 | 3853 c = (int)STRLEN(var); |
7 | 3854 /* if var[] ends in a path separator and tail[] starts |
3855 * with it, skip a character */ | |
39 | 3856 if (*var != NUL && after_pathsep(dst, dst + c) |
7 | 3857 #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) |
3858 && dst[-1] != ':' | |
3859 #endif | |
3860 && vim_ispathsep(*tail)) | |
3861 ++tail; | |
39 | 3862 dst += c; |
7 | 3863 src = tail; |
3864 copy_char = FALSE; | |
3865 } | |
3866 if (mustfree) | |
3867 vim_free(var); | |
3868 } | |
3869 | |
3870 if (copy_char) /* copy at least one char */ | |
3871 { | |
3872 /* | |
1224 | 3873 * Recognize the start of a new name, for '~'. |
1408 | 3874 * Don't do this when "one" is TRUE, to avoid expanding "~" in |
3875 * ":edit foo ~ foo". | |
7 | 3876 */ |
3877 at_start = FALSE; | |
3878 if (src[0] == '\\' && src[1] != NUL) | |
3879 { | |
3880 *dst++ = *src++; | |
3881 --dstlen; | |
3882 } | |
1408 | 3883 else if ((src[0] == ' ' || src[0] == ',') && !one) |
7 | 3884 at_start = TRUE; |
3885 *dst++ = *src++; | |
3886 --dstlen; | |
374 | 3887 |
3888 if (startstr != NULL && src - startstr_len >= srcp | |
3889 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0) | |
3890 at_start = TRUE; | |
7 | 3891 } |
3892 } | |
3893 *dst = NUL; | |
3894 } | |
3895 | |
3896 /* | |
3897 * Vim's version of getenv(). | |
3898 * Special handling of $HOME, $VIM and $VIMRUNTIME. | |
196 | 3899 * Also does ACP to 'enc' conversion for Win32. |
2786 | 3900 * "mustfree" is set to TRUE when returned is allocated, it must be |
3901 * initialized to FALSE by the caller. | |
7 | 3902 */ |
3903 char_u * | |
3904 vim_getenv(name, mustfree) | |
3905 char_u *name; | |
2786 | 3906 int *mustfree; |
7 | 3907 { |
3908 char_u *p; | |
3909 char_u *pend; | |
3910 int vimruntime; | |
3911 | |
3912 #if defined(OS2) || defined(MSDOS) || defined(MSWIN) | |
3913 /* use "C:/" when $HOME is not set */ | |
3914 if (STRCMP(name, "HOME") == 0) | |
3915 return homedir; | |
3916 #endif | |
3917 | |
3918 p = mch_getenv(name); | |
3919 if (p != NULL && *p == NUL) /* empty is the same as not set */ | |
3920 p = NULL; | |
3921 | |
3922 if (p != NULL) | |
170 | 3923 { |
3924 #if defined(FEAT_MBYTE) && defined(WIN3264) | |
3925 if (enc_utf8) | |
3926 { | |
3927 int len; | |
2786 | 3928 char_u *pp = NULL; |
170 | 3929 |
3930 /* Convert from active codepage to UTF-8. Other conversions are | |
3931 * not done, because they would fail for non-ASCII characters. */ | |
835 | 3932 acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
170 | 3933 if (pp != NULL) |
3934 { | |
3935 p = pp; | |
3936 *mustfree = TRUE; | |
3937 } | |
3938 } | |
3939 #endif | |
7 | 3940 return p; |
170 | 3941 } |
7 | 3942 |
3943 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); | |
3944 if (!vimruntime && STRCMP(name, "VIM") != 0) | |
3945 return NULL; | |
3946 | |
3947 /* | |
3948 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM. | |
3949 * Don't do this when default_vimruntime_dir is non-empty. | |
3950 */ | |
3951 if (vimruntime | |
3952 #ifdef HAVE_PATHDEF | |
3953 && *default_vimruntime_dir == NUL | |
3954 #endif | |
3955 ) | |
3956 { | |
3957 p = mch_getenv((char_u *)"VIM"); | |
3958 if (p != NULL && *p == NUL) /* empty is the same as not set */ | |
3959 p = NULL; | |
3960 if (p != NULL) | |
3961 { | |
3962 p = vim_version_dir(p); | |
3963 if (p != NULL) | |
3964 *mustfree = TRUE; | |
3965 else | |
3966 p = mch_getenv((char_u *)"VIM"); | |
170 | 3967 |
3968 #if defined(FEAT_MBYTE) && defined(WIN3264) | |
3969 if (enc_utf8) | |
3970 { | |
3971 int len; | |
2786 | 3972 char_u *pp = NULL; |
170 | 3973 |
3974 /* Convert from active codepage to UTF-8. Other conversions | |
3975 * are not done, because they would fail for non-ASCII | |
3976 * characters. */ | |
835 | 3977 acp_to_enc(p, (int)STRLEN(p), &pp, &len); |
170 | 3978 if (pp != NULL) |
3979 { | |
2786 | 3980 if (*mustfree) |
170 | 3981 vim_free(p); |
3982 p = pp; | |
3983 *mustfree = TRUE; | |
3984 } | |
3985 } | |
3986 #endif | |
7 | 3987 } |
3988 } | |
3989 | |
3990 /* | |
3991 * When expanding $VIM or $VIMRUNTIME fails, try using: | |
3992 * - the directory name from 'helpfile' (unless it contains '$') | |
3993 * - the executable name from argv[0] | |
3994 */ | |
3995 if (p == NULL) | |
3996 { | |
3997 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL) | |
3998 p = p_hf; | |
3999 #ifdef USE_EXE_NAME | |
4000 /* | |
4001 * Use the name of the executable, obtained from argv[0]. | |
4002 */ | |
4003 else | |
4004 p = exe_name; | |
4005 #endif | |
4006 if (p != NULL) | |
4007 { | |
4008 /* remove the file name */ | |
4009 pend = gettail(p); | |
4010 | |
4011 /* remove "doc/" from 'helpfile', if present */ | |
4012 if (p == p_hf) | |
4013 pend = remove_tail(p, pend, (char_u *)"doc"); | |
4014 | |
4015 #ifdef USE_EXE_NAME | |
4016 # ifdef MACOS_X | |
768 | 4017 /* remove "MacOS" from exe_name and add "Resources/vim" */ |
7 | 4018 if (p == exe_name) |
4019 { | |
4020 char_u *pend1; | |
768 | 4021 char_u *pnew; |
4022 | |
4023 pend1 = remove_tail(p, pend, (char_u *)"MacOS"); | |
4024 if (pend1 != pend) | |
4025 { | |
4026 pnew = alloc((unsigned)(pend1 - p) + 15); | |
4027 if (pnew != NULL) | |
4028 { | |
4029 STRNCPY(pnew, p, (pend1 - p)); | |
4030 STRCPY(pnew + (pend1 - p), "Resources/vim"); | |
4031 p = pnew; | |
4032 pend = p + STRLEN(p); | |
4033 } | |
4034 } | |
7 | 4035 } |
4036 # endif | |
4037 /* remove "src/" from exe_name, if present */ | |
4038 if (p == exe_name) | |
4039 pend = remove_tail(p, pend, (char_u *)"src"); | |
4040 #endif | |
4041 | |
4042 /* for $VIM, remove "runtime/" or "vim54/", if present */ | |
4043 if (!vimruntime) | |
4044 { | |
4045 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME); | |
4046 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT); | |
4047 } | |
4048 | |
4049 /* remove trailing path separator */ | |
4050 #ifndef MACOS_CLASSIC | |
4051 /* With MacOS path (with colons) the final colon is required */ | |
1532 | 4052 /* to avoid confusion between absolute and relative path */ |
39 | 4053 if (pend > p && after_pathsep(p, pend)) |
7 | 4054 --pend; |
4055 #endif | |
4056 | |
768 | 4057 #ifdef MACOS_X |
4058 if (p == exe_name || p == p_hf) | |
4059 #endif | |
4060 /* check that the result is a directory name */ | |
4061 p = vim_strnsave(p, (int)(pend - p)); | |
7 | 4062 |
4063 if (p != NULL && !mch_isdir(p)) | |
4064 { | |
4065 vim_free(p); | |
4066 p = NULL; | |
4067 } | |
4068 else | |
4069 { | |
4070 #ifdef USE_EXE_NAME | |
4071 /* may add "/vim54" or "/runtime" if it exists */ | |
4072 if (vimruntime && (pend = vim_version_dir(p)) != NULL) | |
4073 { | |
4074 vim_free(p); | |
4075 p = pend; | |
4076 } | |
4077 #endif | |
4078 *mustfree = TRUE; | |
4079 } | |
4080 } | |
4081 } | |
4082 | |
4083 #ifdef HAVE_PATHDEF | |
4084 /* When there is a pathdef.c file we can use default_vim_dir and | |
4085 * default_vimruntime_dir */ | |
4086 if (p == NULL) | |
4087 { | |
4088 /* Only use default_vimruntime_dir when it is not empty */ | |
4089 if (vimruntime && *default_vimruntime_dir != NUL) | |
4090 { | |
4091 p = default_vimruntime_dir; | |
4092 *mustfree = FALSE; | |
4093 } | |
4094 else if (*default_vim_dir != NUL) | |
4095 { | |
4096 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) | |
4097 *mustfree = TRUE; | |
4098 else | |
4099 { | |
4100 p = default_vim_dir; | |
4101 *mustfree = FALSE; | |
4102 } | |
4103 } | |
4104 } | |
4105 #endif | |
4106 | |
4107 /* | |
4108 * Set the environment variable, so that the new value can be found fast | |
4109 * next time, and others can also use it (e.g. Perl). | |
4110 */ | |
4111 if (p != NULL) | |
4112 { | |
4113 if (vimruntime) | |
4114 { | |
4115 vim_setenv((char_u *)"VIMRUNTIME", p); | |
4116 didset_vimruntime = TRUE; | |
4117 #ifdef FEAT_GETTEXT | |
4118 { | |
115 | 4119 char_u *buf = concat_str(p, (char_u *)"/lang"); |
7 | 4120 |
4121 if (buf != NULL) | |
4122 { | |
4123 bindtextdomain(VIMPACKAGE, (char *)buf); | |
4124 vim_free(buf); | |
4125 } | |
4126 } | |
4127 #endif | |
4128 } | |
4129 else | |
4130 { | |
4131 vim_setenv((char_u *)"VIM", p); | |
4132 didset_vim = TRUE; | |
4133 } | |
4134 } | |
4135 return p; | |
4136 } | |
4137 | |
4138 /* | |
4139 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists. | |
4140 * Return NULL if not, return its name in allocated memory otherwise. | |
4141 */ | |
4142 static char_u * | |
4143 vim_version_dir(vimdir) | |
4144 char_u *vimdir; | |
4145 { | |
4146 char_u *p; | |
4147 | |
4148 if (vimdir == NULL || *vimdir == NUL) | |
4149 return NULL; | |
4150 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE); | |
4151 if (p != NULL && mch_isdir(p)) | |
4152 return p; | |
4153 vim_free(p); | |
4154 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE); | |
4155 if (p != NULL && mch_isdir(p)) | |
4156 return p; | |
4157 vim_free(p); | |
4158 return NULL; | |
4159 } | |
4160 | |
4161 /* | |
4162 * If the string between "p" and "pend" ends in "name/", return "pend" minus | |
4163 * the length of "name/". Otherwise return "pend". | |
4164 */ | |
4165 static char_u * | |
4166 remove_tail(p, pend, name) | |
4167 char_u *p; | |
4168 char_u *pend; | |
4169 char_u *name; | |
4170 { | |
4171 int len = (int)STRLEN(name) + 1; | |
4172 char_u *newend = pend - len; | |
4173 | |
4174 if (newend >= p | |
4175 && fnamencmp(newend, name, len - 1) == 0 | |
39 | 4176 && (newend == p || after_pathsep(p, newend))) |
7 | 4177 return newend; |
4178 return pend; | |
4179 } | |
4180 | |
4181 /* | |
4182 * Our portable version of setenv. | |
4183 */ | |
4184 void | |
4185 vim_setenv(name, val) | |
4186 char_u *name; | |
4187 char_u *val; | |
4188 { | |
4189 #ifdef HAVE_SETENV | |
4190 mch_setenv((char *)name, (char *)val, 1); | |
4191 #else | |
4192 char_u *envbuf; | |
4193 | |
4194 /* | |
4195 * Putenv does not copy the string, it has to remain | |
4196 * valid. The allocated memory will never be freed. | |
4197 */ | |
4198 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2)); | |
4199 if (envbuf != NULL) | |
4200 { | |
4201 sprintf((char *)envbuf, "%s=%s", name, val); | |
4202 putenv((char *)envbuf); | |
4203 } | |
4204 #endif | |
4205 } | |
4206 | |
4207 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
4208 /* | |
4209 * Function given to ExpandGeneric() to obtain an environment variable name. | |
4210 */ | |
4211 char_u * | |
4212 get_env_name(xp, idx) | |
1883 | 4213 expand_T *xp UNUSED; |
7 | 4214 int idx; |
4215 { | |
4216 # if defined(AMIGA) || defined(__MRC__) || defined(__SC__) | |
4217 /* | |
4218 * No environ[] on the Amiga and on the Mac (using MPW). | |
4219 */ | |
4220 return NULL; | |
4221 # else | |
4222 # ifndef __WIN32__ | |
4223 /* Borland C++ 5.2 has this in a header file. */ | |
4224 extern char **environ; | |
4225 # endif | |
17 | 4226 # define ENVNAMELEN 100 |
4227 static char_u name[ENVNAMELEN]; | |
7 | 4228 char_u *str; |
4229 int n; | |
4230 | |
4231 str = (char_u *)environ[idx]; | |
4232 if (str == NULL) | |
4233 return NULL; | |
4234 | |
17 | 4235 for (n = 0; n < ENVNAMELEN - 1; ++n) |
7 | 4236 { |
4237 if (str[n] == '=' || str[n] == NUL) | |
4238 break; | |
4239 name[n] = str[n]; | |
4240 } | |
4241 name[n] = NUL; | |
4242 return name; | |
4243 # endif | |
4244 } | |
4245 #endif | |
4246 | |
4247 /* | |
4248 * Replace home directory by "~" in each space or comma separated file name in | |
4249 * 'src'. | |
4250 * If anything fails (except when out of space) dst equals src. | |
4251 */ | |
4252 void | |
4253 home_replace(buf, src, dst, dstlen, one) | |
4254 buf_T *buf; /* when not NULL, check for help files */ | |
4255 char_u *src; /* input file name */ | |
4256 char_u *dst; /* where to put the result */ | |
4257 int dstlen; /* maximum length of the result */ | |
4258 int one; /* if TRUE, only replace one file name, include | |
4259 spaces and commas in the file name. */ | |
4260 { | |
4261 size_t dirlen = 0, envlen = 0; | |
4262 size_t len; | |
4263 char_u *homedir_env; | |
4264 char_u *p; | |
4265 | |
4266 if (src == NULL) | |
4267 { | |
4268 *dst = NUL; | |
4269 return; | |
4270 } | |
4271 | |
4272 /* | |
4273 * If the file is a help file, remove the path completely. | |
4274 */ | |
4275 if (buf != NULL && buf->b_help) | |
4276 { | |
4277 STRCPY(dst, gettail(src)); | |
4278 return; | |
4279 } | |
4280 | |
4281 /* | |
4282 * We check both the value of the $HOME environment variable and the | |
4283 * "real" home directory. | |
4284 */ | |
4285 if (homedir != NULL) | |
4286 dirlen = STRLEN(homedir); | |
4287 | |
4288 #ifdef VMS | |
4289 homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); | |
4290 #else | |
4291 homedir_env = mch_getenv((char_u *)"HOME"); | |
4292 #endif | |
4293 | |
4294 if (homedir_env != NULL && *homedir_env == NUL) | |
4295 homedir_env = NULL; | |
4296 if (homedir_env != NULL) | |
4297 envlen = STRLEN(homedir_env); | |
4298 | |
4299 if (!one) | |
4300 src = skipwhite(src); | |
4301 while (*src && dstlen > 0) | |
4302 { | |
4303 /* | |
4304 * Here we are at the beginning of a file name. | |
4305 * First, check to see if the beginning of the file name matches | |
4306 * $HOME or the "real" home directory. Check that there is a '/' | |
4307 * after the match (so that if e.g. the file is "/home/pieter/bla", | |
4308 * and the home directory is "/home/piet", the file does not end up | |
4309 * as "~er/bla" (which would seem to indicate the file "bla" in user | |
4310 * er's home directory)). | |
4311 */ | |
4312 p = homedir; | |
4313 len = dirlen; | |
4314 for (;;) | |
4315 { | |
4316 if ( len | |
4317 && fnamencmp(src, p, len) == 0 | |
4318 && (vim_ispathsep(src[len]) | |
4319 || (!one && (src[len] == ',' || src[len] == ' ')) | |
4320 || src[len] == NUL)) | |
4321 { | |
4322 src += len; | |
4323 if (--dstlen > 0) | |
4324 *dst++ = '~'; | |
4325 | |
4326 /* | |
4327 * If it's just the home directory, add "/". | |
4328 */ | |
4329 if (!vim_ispathsep(src[0]) && --dstlen > 0) | |
4330 *dst++ = '/'; | |
4331 break; | |
4332 } | |
4333 if (p == homedir_env) | |
4334 break; | |
4335 p = homedir_env; | |
4336 len = envlen; | |
4337 } | |
4338 | |
4339 /* if (!one) skip to separator: space or comma */ | |
4340 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) | |
4341 *dst++ = *src++; | |
4342 /* skip separator */ | |
4343 while ((*src == ' ' || *src == ',') && --dstlen > 0) | |
4344 *dst++ = *src++; | |
4345 } | |
4346 /* if (dstlen == 0) out of space, what to do??? */ | |
4347 | |
4348 *dst = NUL; | |
4349 } | |
4350 | |
4351 /* | |
4352 * Like home_replace, store the replaced string in allocated memory. | |
4353 * When something fails, NULL is returned. | |
4354 */ | |
4355 char_u * | |
4356 home_replace_save(buf, src) | |
4357 buf_T *buf; /* when not NULL, check for help files */ | |
4358 char_u *src; /* input file name */ | |
4359 { | |
4360 char_u *dst; | |
4361 unsigned len; | |
4362 | |
4363 len = 3; /* space for "~/" and trailing NUL */ | |
4364 if (src != NULL) /* just in case */ | |
4365 len += (unsigned)STRLEN(src); | |
4366 dst = alloc(len); | |
4367 if (dst != NULL) | |
4368 home_replace(buf, src, dst, len, TRUE); | |
4369 return dst; | |
4370 } | |
4371 | |
4372 /* | |
4373 * Compare two file names and return: | |
4374 * FPC_SAME if they both exist and are the same file. | |
4375 * FPC_SAMEX if they both don't exist and have the same file name. | |
4376 * FPC_DIFF if they both exist and are different files. | |
4377 * FPC_NOTX if they both don't exist. | |
4378 * FPC_DIFFX if one of them doesn't exist. | |
4379 * For the first name environment variables are expanded | |
4380 */ | |
4381 int | |
4382 fullpathcmp(s1, s2, checkname) | |
4383 char_u *s1, *s2; | |
4384 int checkname; /* when both don't exist, check file names */ | |
4385 { | |
4386 #ifdef UNIX | |
4387 char_u exp1[MAXPATHL]; | |
4388 char_u full1[MAXPATHL]; | |
4389 char_u full2[MAXPATHL]; | |
4390 struct stat st1, st2; | |
4391 int r1, r2; | |
4392 | |
4393 expand_env(s1, exp1, MAXPATHL); | |
4394 r1 = mch_stat((char *)exp1, &st1); | |
4395 r2 = mch_stat((char *)s2, &st2); | |
4396 if (r1 != 0 && r2 != 0) | |
4397 { | |
4398 /* if mch_stat() doesn't work, may compare the names */ | |
4399 if (checkname) | |
4400 { | |
4401 if (fnamecmp(exp1, s2) == 0) | |
4402 return FPC_SAMEX; | |
4403 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
4404 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
4405 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) | |
4406 return FPC_SAMEX; | |
4407 } | |
4408 return FPC_NOTX; | |
4409 } | |
4410 if (r1 != 0 || r2 != 0) | |
4411 return FPC_DIFFX; | |
4412 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) | |
4413 return FPC_SAME; | |
4414 return FPC_DIFF; | |
4415 #else | |
4416 char_u *exp1; /* expanded s1 */ | |
4417 char_u *full1; /* full path of s1 */ | |
4418 char_u *full2; /* full path of s2 */ | |
4419 int retval = FPC_DIFF; | |
4420 int r1, r2; | |
4421 | |
4422 /* allocate one buffer to store three paths (alloc()/free() is slow!) */ | |
4423 if ((exp1 = alloc(MAXPATHL * 3)) != NULL) | |
4424 { | |
4425 full1 = exp1 + MAXPATHL; | |
4426 full2 = full1 + MAXPATHL; | |
4427 | |
4428 expand_env(s1, exp1, MAXPATHL); | |
4429 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
4430 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
4431 | |
4432 /* If vim_FullName() fails, the file probably doesn't exist. */ | |
4433 if (r1 != OK && r2 != OK) | |
4434 { | |
4435 if (checkname && fnamecmp(exp1, s2) == 0) | |
4436 retval = FPC_SAMEX; | |
4437 else | |
4438 retval = FPC_NOTX; | |
4439 } | |
4440 else if (r1 != OK || r2 != OK) | |
4441 retval = FPC_DIFFX; | |
4442 else if (fnamecmp(full1, full2)) | |
4443 retval = FPC_DIFF; | |
4444 else | |
4445 retval = FPC_SAME; | |
4446 vim_free(exp1); | |
4447 } | |
4448 return retval; | |
4449 #endif | |
4450 } | |
4451 | |
4452 /* | |
10 | 4453 * 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
|
4454 * When the path ends in a path separator the tail is the NUL after it. |
10 | 4455 * Fail safe: never returns NULL. |
7 | 4456 */ |
4457 char_u * | |
4458 gettail(fname) | |
4459 char_u *fname; | |
4460 { | |
4461 char_u *p1, *p2; | |
4462 | |
4463 if (fname == NULL) | |
4464 return (char_u *)""; | |
4465 for (p1 = p2 = fname; *p2; ) /* find last part of path */ | |
4466 { | |
4467 if (vim_ispathsep(*p2)) | |
4468 p1 = p2 + 1; | |
39 | 4469 mb_ptr_adv(p2); |
7 | 4470 } |
4471 return p1; | |
4472 } | |
4473 | |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4474 #if defined(FEAT_SEARCHPATH) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4475 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
|
4476 |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4477 /* |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4478 * 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
|
4479 * separator: |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4480 * "/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
|
4481 * ^ ^ ^ ^ |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4482 */ |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4483 static char_u * |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4484 gettail_dir(fname) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4485 char_u *fname; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4486 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4487 char_u *dir_end = fname; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4488 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
|
4489 int look_for_sep = TRUE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4490 char_u *p; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4491 |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4492 for (p = fname; *p != NUL; ) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4493 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4494 if (vim_ispathsep(*p)) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4495 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4496 if (look_for_sep) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4497 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4498 next_dir_end = p; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4499 look_for_sep = FALSE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4500 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4501 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4502 else |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4503 { |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4504 if (!look_for_sep) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4505 dir_end = next_dir_end; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4506 look_for_sep = TRUE; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4507 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4508 mb_ptr_adv(p); |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4509 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4510 return dir_end; |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4511 } |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4512 #endif |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
4513 |
7 | 4514 /* |
39 | 4515 * Get pointer to tail of "fname", including path separators. Putting a NUL |
4516 * here leaves the directory name. Takes care of "c:/" and "//". | |
4517 * Always returns a valid pointer. | |
4518 */ | |
4519 char_u * | |
4520 gettail_sep(fname) | |
4521 char_u *fname; | |
4522 { | |
4523 char_u *p; | |
4524 char_u *t; | |
4525 | |
4526 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */ | |
4527 t = gettail(fname); | |
4528 while (t > p && after_pathsep(fname, t)) | |
4529 --t; | |
4530 #ifdef VMS | |
4531 /* path separator is part of the path */ | |
4532 ++t; | |
4533 #endif | |
4534 return t; | |
4535 } | |
4536 | |
4537 /* | |
7 | 4538 * get the next path component (just after the next path separator). |
4539 */ | |
4540 char_u * | |
4541 getnextcomp(fname) | |
4542 char_u *fname; | |
4543 { | |
4544 while (*fname && !vim_ispathsep(*fname)) | |
39 | 4545 mb_ptr_adv(fname); |
7 | 4546 if (*fname) |
4547 ++fname; | |
4548 return fname; | |
4549 } | |
4550 | |
4551 /* | |
4552 * Get a pointer to one character past the head of a path name. | |
4553 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. | |
4554 * If there is no head, path is returned. | |
4555 */ | |
4556 char_u * | |
4557 get_past_head(path) | |
4558 char_u *path; | |
4559 { | |
4560 char_u *retval; | |
4561 | |
4562 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
4563 /* may skip "c:" */ | |
4564 if (isalpha(path[0]) && path[1] == ':') | |
4565 retval = path + 2; | |
4566 else | |
4567 retval = path; | |
4568 #else | |
4569 # if defined(AMIGA) | |
4570 /* may skip "label:" */ | |
4571 retval = vim_strchr(path, ':'); | |
4572 if (retval == NULL) | |
4573 retval = path; | |
4574 # else /* Unix */ | |
4575 retval = path; | |
4576 # endif | |
4577 #endif | |
4578 | |
4579 while (vim_ispathsep(*retval)) | |
4580 ++retval; | |
4581 | |
4582 return retval; | |
4583 } | |
4584 | |
4585 /* | |
4586 * return TRUE if 'c' is a path separator. | |
4587 */ | |
4588 int | |
4589 vim_ispathsep(c) | |
4590 int c; | |
4591 { | |
2823 | 4592 #ifdef UNIX |
7 | 4593 return (c == '/'); /* UNIX has ':' inside file names */ |
2823 | 4594 #else |
4595 # ifdef BACKSLASH_IN_FILENAME | |
7 | 4596 return (c == ':' || c == '/' || c == '\\'); |
2823 | 4597 # else |
4598 # ifdef VMS | |
7 | 4599 /* server"user passwd"::device:[full.path.name]fname.extension;version" */ |
4600 return (c == ':' || c == '[' || c == ']' || c == '/' | |
4601 || c == '<' || c == '>' || c == '"' ); | |
2823 | 4602 # else |
7 | 4603 return (c == ':' || c == '/'); |
2823 | 4604 # endif /* VMS */ |
7 | 4605 # endif |
2823 | 4606 #endif |
7 | 4607 } |
4608 | |
4609 #if defined(FEAT_SEARCHPATH) || defined(PROTO) | |
4610 /* | |
4611 * return TRUE if 'c' is a path list separator. | |
4612 */ | |
4613 int | |
4614 vim_ispathlistsep(c) | |
4615 int c; | |
4616 { | |
4617 #ifdef UNIX | |
4618 return (c == ':'); | |
4619 #else | |
1224 | 4620 return (c == ';'); /* might not be right for every system... */ |
7 | 4621 #endif |
4622 } | |
4623 #endif | |
4624 | |
819 | 4625 #if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \ |
4626 || defined(FEAT_EVAL) || defined(PROTO) | |
4627 /* | |
4628 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" | |
4629 * It's done in-place. | |
4630 */ | |
4631 void | |
4632 shorten_dir(str) | |
4633 char_u *str; | |
4634 { | |
4635 char_u *tail, *s, *d; | |
4636 int skip = FALSE; | |
4637 | |
4638 tail = gettail(str); | |
4639 d = str; | |
4640 for (s = str; ; ++s) | |
4641 { | |
4642 if (s >= tail) /* copy the whole tail */ | |
4643 { | |
4644 *d++ = *s; | |
4645 if (*s == NUL) | |
4646 break; | |
4647 } | |
4648 else if (vim_ispathsep(*s)) /* copy '/' and next char */ | |
4649 { | |
4650 *d++ = *s; | |
4651 skip = FALSE; | |
4652 } | |
4653 else if (!skip) | |
4654 { | |
4655 *d++ = *s; /* copy next char */ | |
4656 if (*s != '~' && *s != '.') /* and leading "~" and "." */ | |
4657 skip = TRUE; | |
4658 # ifdef FEAT_MBYTE | |
4659 if (has_mbyte) | |
4660 { | |
4661 int l = mb_ptr2len(s); | |
4662 | |
4663 while (--l > 0) | |
927 | 4664 *d++ = *++s; |
819 | 4665 } |
4666 # endif | |
4667 } | |
4668 } | |
4669 } | |
4670 #endif | |
4671 | |
594 | 4672 /* |
4673 * Return TRUE if the directory of "fname" exists, FALSE otherwise. | |
4674 * Also returns TRUE if there is no directory name. | |
4675 * "fname" must be writable!. | |
4676 */ | |
4677 int | |
4678 dir_of_file_exists(fname) | |
4679 char_u *fname; | |
4680 { | |
4681 char_u *p; | |
4682 int c; | |
4683 int retval; | |
4684 | |
4685 p = gettail_sep(fname); | |
4686 if (p == fname) | |
4687 return TRUE; | |
4688 c = *p; | |
4689 *p = NUL; | |
4690 retval = mch_isdir(fname); | |
4691 *p = c; | |
4692 return retval; | |
4693 } | |
4694 | |
7 | 4695 #if (defined(CASE_INSENSITIVE_FILENAME) && defined(BACKSLASH_IN_FILENAME)) \ |
4696 || defined(PROTO) | |
4697 /* | |
4698 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally. | |
4699 */ | |
4700 int | |
4701 vim_fnamecmp(x, y) | |
4702 char_u *x, *y; | |
4703 { | |
4704 return vim_fnamencmp(x, y, MAXPATHL); | |
4705 } | |
4706 | |
4707 int | |
4708 vim_fnamencmp(x, y, len) | |
4709 char_u *x, *y; | |
4710 size_t len; | |
4711 { | |
4712 while (len > 0 && *x && *y) | |
4713 { | |
4714 if (TOLOWER_LOC(*x) != TOLOWER_LOC(*y) | |
4715 && !(*x == '/' && *y == '\\') | |
4716 && !(*x == '\\' && *y == '/')) | |
4717 break; | |
4718 ++x; | |
4719 ++y; | |
4720 --len; | |
4721 } | |
4722 if (len == 0) | |
4723 return 0; | |
4724 return (*x - *y); | |
4725 } | |
4726 #endif | |
4727 | |
4728 /* | |
4729 * Concatenate file names fname1 and fname2 into allocated memory. | |
1224 | 4730 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. |
7 | 4731 */ |
4732 char_u * | |
4733 concat_fnames(fname1, fname2, sep) | |
4734 char_u *fname1; | |
4735 char_u *fname2; | |
4736 int sep; | |
4737 { | |
4738 char_u *dest; | |
4739 | |
4740 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3)); | |
4741 if (dest != NULL) | |
4742 { | |
4743 STRCPY(dest, fname1); | |
4744 if (sep) | |
4745 add_pathsep(dest); | |
4746 STRCAT(dest, fname2); | |
4747 } | |
4748 return dest; | |
4749 } | |
4750 | |
115 | 4751 /* |
4752 * Concatenate two strings and return the result in allocated memory. | |
4753 * Returns NULL when out of memory. | |
4754 */ | |
4755 char_u * | |
4756 concat_str(str1, str2) | |
4757 char_u *str1; | |
4758 char_u *str2; | |
4759 { | |
4760 char_u *dest; | |
4761 size_t l = STRLEN(str1); | |
4762 | |
4763 dest = alloc((unsigned)(l + STRLEN(str2) + 1L)); | |
4764 if (dest != NULL) | |
4765 { | |
4766 STRCPY(dest, str1); | |
4767 STRCPY(dest + l, str2); | |
4768 } | |
4769 return dest; | |
4770 } | |
4771 | |
7 | 4772 /* |
4773 * Add a path separator to a file name, unless it already ends in a path | |
4774 * separator. | |
4775 */ | |
4776 void | |
4777 add_pathsep(p) | |
4778 char_u *p; | |
4779 { | |
39 | 4780 if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) |
7 | 4781 STRCAT(p, PATHSEPSTR); |
4782 } | |
4783 | |
4784 /* | |
4785 * FullName_save - Make an allocated copy of a full file name. | |
4786 * Returns NULL when out of memory. | |
4787 */ | |
4788 char_u * | |
4789 FullName_save(fname, force) | |
4790 char_u *fname; | |
4791 int force; /* force expansion, even when it already looks | |
4792 like a full path name */ | |
4793 { | |
4794 char_u *buf; | |
4795 char_u *new_fname = NULL; | |
4796 | |
4797 if (fname == NULL) | |
4798 return NULL; | |
4799 | |
4800 buf = alloc((unsigned)MAXPATHL); | |
4801 if (buf != NULL) | |
4802 { | |
4803 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) | |
4804 new_fname = vim_strsave(buf); | |
4805 else | |
4806 new_fname = vim_strsave(fname); | |
4807 vim_free(buf); | |
4808 } | |
4809 return new_fname; | |
4810 } | |
4811 | |
4812 #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) | |
4813 | |
4814 static char_u *skip_string __ARGS((char_u *p)); | |
4815 | |
4816 /* | |
4817 * Find the start of a comment, not knowing if we are in a comment right now. | |
4818 * Search starts at w_cursor.lnum and goes backwards. | |
4819 */ | |
4820 pos_T * | |
4821 find_start_comment(ind_maxcomment) /* XXX */ | |
4822 int ind_maxcomment; | |
4823 { | |
4824 pos_T *pos; | |
4825 char_u *line; | |
4826 char_u *p; | |
829 | 4827 int cur_maxcomment = ind_maxcomment; |
4828 | |
4829 for (;;) | |
4830 { | |
4831 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); | |
4832 if (pos == NULL) | |
4833 break; | |
4834 | |
4835 /* | |
4836 * Check if the comment start we found is inside a string. | |
4837 * If it is then restrict the search to below this line and try again. | |
4838 */ | |
4839 line = ml_get(pos->lnum); | |
1883 | 4840 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) |
829 | 4841 p = skip_string(p); |
1883 | 4842 if ((colnr_T)(p - line) <= pos->col) |
829 | 4843 break; |
4844 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; | |
4845 if (cur_maxcomment <= 0) | |
4846 { | |
4847 pos = NULL; | |
4848 break; | |
4849 } | |
4850 } | |
7 | 4851 return pos; |
4852 } | |
4853 | |
4854 /* | |
4855 * Skip to the end of a "string" and a 'c' character. | |
4856 * If there is no string or character, return argument unmodified. | |
4857 */ | |
4858 static char_u * | |
4859 skip_string(p) | |
4860 char_u *p; | |
4861 { | |
4862 int i; | |
4863 | |
4864 /* | |
4865 * We loop, because strings may be concatenated: "date""time". | |
4866 */ | |
4867 for ( ; ; ++p) | |
4868 { | |
4869 if (p[0] == '\'') /* 'c' or '\n' or '\000' */ | |
4870 { | |
4871 if (!p[1]) /* ' at end of line */ | |
4872 break; | |
4873 i = 2; | |
4874 if (p[1] == '\\') /* '\n' or '\000' */ | |
4875 { | |
4876 ++i; | |
4877 while (vim_isdigit(p[i - 1])) /* '\000' */ | |
4878 ++i; | |
4879 } | |
4880 if (p[i] == '\'') /* check for trailing ' */ | |
4881 { | |
4882 p += i; | |
4883 continue; | |
4884 } | |
4885 } | |
4886 else if (p[0] == '"') /* start of string */ | |
4887 { | |
4888 for (++p; p[0]; ++p) | |
4889 { | |
4890 if (p[0] == '\\' && p[1] != NUL) | |
4891 ++p; | |
4892 else if (p[0] == '"') /* end of string */ | |
4893 break; | |
4894 } | |
4895 if (p[0] == '"') | |
4896 continue; | |
4897 } | |
4898 break; /* no string found */ | |
4899 } | |
4900 if (!*p) | |
4901 --p; /* backup from NUL */ | |
4902 return p; | |
4903 } | |
4904 #endif /* FEAT_CINDENT || FEAT_SYN_HL */ | |
4905 | |
4906 #if defined(FEAT_CINDENT) || defined(PROTO) | |
4907 | |
4908 /* | |
4909 * Do C or expression indenting on the current line. | |
4910 */ | |
4911 void | |
4912 do_c_expr_indent() | |
4913 { | |
4914 # ifdef FEAT_EVAL | |
4915 if (*curbuf->b_p_inde != NUL) | |
4916 fixthisline(get_expr_indent); | |
4917 else | |
4918 # endif | |
4919 fixthisline(get_c_indent); | |
4920 } | |
4921 | |
4922 /* | |
4923 * Functions for C-indenting. | |
4924 * Most of this originally comes from Eric Fischer. | |
4925 */ | |
4926 /* | |
4927 * Below "XXX" means that this function may unlock the current line. | |
4928 */ | |
4929 | |
4930 static char_u *cin_skipcomment __ARGS((char_u *)); | |
4931 static int cin_nocode __ARGS((char_u *)); | |
4932 static pos_T *find_line_comment __ARGS((void)); | |
4933 static int cin_islabel_skip __ARGS((char_u **)); | |
4934 static int cin_isdefault __ARGS((char_u *)); | |
4935 static char_u *after_label __ARGS((char_u *l)); | |
4936 static int get_indent_nolabel __ARGS((linenr_T lnum)); | |
4937 static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment)); | |
4938 static int cin_first_id_amount __ARGS((void)); | |
4939 static int cin_get_equal_amount __ARGS((linenr_T lnum)); | |
4940 static int cin_ispreproc __ARGS((char_u *)); | |
4941 static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump)); | |
4942 static int cin_iscomment __ARGS((char_u *)); | |
4943 static int cin_islinecomment __ARGS((char_u *)); | |
4944 static int cin_isterminated __ARGS((char_u *, int, int)); | |
4945 static int cin_isinit __ARGS((void)); | |
4946 static int cin_isfuncdecl __ARGS((char_u **, linenr_T)); | |
4947 static int cin_isif __ARGS((char_u *)); | |
4948 static int cin_iselse __ARGS((char_u *)); | |
4949 static int cin_isdo __ARGS((char_u *)); | |
4950 static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int)); | |
829 | 4951 static int cin_iswhileofdo_end __ARGS((int terminated, int ind_maxparen, int ind_maxcomment)); |
7 | 4952 static int cin_isbreak __ARGS((char_u *)); |
1336 | 4953 static int cin_is_cpp_baseclass __ARGS((colnr_T *col)); |
828 | 4954 static int get_baseclass_amount __ARGS((int col, int ind_maxparen, int ind_maxcomment, int ind_cpp_baseclass)); |
7 | 4955 static int cin_ends_in __ARGS((char_u *, char_u *, char_u *)); |
4956 static int cin_skip2pos __ARGS((pos_T *trypos)); | |
4957 static pos_T *find_start_brace __ARGS((int)); | |
4958 static pos_T *find_match_paren __ARGS((int, int)); | |
4959 static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos)); | |
4960 static int find_last_paren __ARGS((char_u *l, int start, int end)); | |
4961 static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment)); | |
4962 | |
1096 | 4963 static int ind_hash_comment = 0; /* # starts a comment */ |
4964 | |
7 | 4965 /* |
4966 * Skip over white space and C comments within the line. | |
1096 | 4967 * Also skip over Perl/shell comments if desired. |
7 | 4968 */ |
4969 static char_u * | |
4970 cin_skipcomment(s) | |
4971 char_u *s; | |
4972 { | |
4973 while (*s) | |
4974 { | |
1096 | 4975 char_u *prev_s = s; |
4976 | |
7 | 4977 s = skipwhite(s); |
1096 | 4978 |
4979 /* Perl/shell # comment comment continues until eol. Require a space | |
4980 * before # to avoid recognizing $#array. */ | |
4981 if (ind_hash_comment != 0 && s != prev_s && *s == '#') | |
4982 { | |
4983 s += STRLEN(s); | |
4984 break; | |
4985 } | |
7 | 4986 if (*s != '/') |
4987 break; | |
4988 ++s; | |
4989 if (*s == '/') /* slash-slash comment continues till eol */ | |
4990 { | |
4991 s += STRLEN(s); | |
4992 break; | |
4993 } | |
4994 if (*s != '*') | |
4995 break; | |
4996 for (++s; *s; ++s) /* skip slash-star comment */ | |
4997 if (s[0] == '*' && s[1] == '/') | |
4998 { | |
4999 s += 2; | |
5000 break; | |
5001 } | |
5002 } | |
5003 return s; | |
5004 } | |
5005 | |
5006 /* | |
2805 | 5007 * Return TRUE if there is no code at *s. White space and comments are |
7 | 5008 * not considered code. |
5009 */ | |
5010 static int | |
5011 cin_nocode(s) | |
5012 char_u *s; | |
5013 { | |
5014 return *cin_skipcomment(s) == NUL; | |
5015 } | |
5016 | |
5017 /* | |
5018 * Check previous lines for a "//" line comment, skipping over blank lines. | |
5019 */ | |
5020 static pos_T * | |
5021 find_line_comment() /* XXX */ | |
5022 { | |
5023 static pos_T pos; | |
5024 char_u *line; | |
5025 char_u *p; | |
5026 | |
5027 pos = curwin->w_cursor; | |
5028 while (--pos.lnum > 0) | |
5029 { | |
5030 line = ml_get(pos.lnum); | |
5031 p = skipwhite(line); | |
5032 if (cin_islinecomment(p)) | |
5033 { | |
5034 pos.col = (int)(p - line); | |
5035 return &pos; | |
5036 } | |
5037 if (*p != NUL) | |
5038 break; | |
5039 } | |
5040 return NULL; | |
5041 } | |
5042 | |
5043 /* | |
5044 * Check if string matches "label:"; move to character after ':' if true. | |
5045 */ | |
5046 static int | |
5047 cin_islabel_skip(s) | |
5048 char_u **s; | |
5049 { | |
5050 if (!vim_isIDc(**s)) /* need at least one ID character */ | |
5051 return FALSE; | |
5052 | |
5053 while (vim_isIDc(**s)) | |
5054 (*s)++; | |
5055 | |
5056 *s = cin_skipcomment(*s); | |
5057 | |
5058 /* "::" is not a label, it's C++ */ | |
5059 return (**s == ':' && *++*s != ':'); | |
5060 } | |
5061 | |
5062 /* | |
5063 * Recognize a label: "label:". | |
5064 * Note: curwin->w_cursor must be where we are looking for the label. | |
5065 */ | |
5066 int | |
5067 cin_islabel(ind_maxcomment) /* XXX */ | |
5068 int ind_maxcomment; | |
5069 { | |
5070 char_u *s; | |
5071 | |
5072 s = cin_skipcomment(ml_get_curline()); | |
5073 | |
5074 /* | |
5075 * Exclude "default" from labels, since it should be indented | |
5076 * like a switch label. Same for C++ scope declarations. | |
5077 */ | |
5078 if (cin_isdefault(s)) | |
5079 return FALSE; | |
5080 if (cin_isscopedecl(s)) | |
5081 return FALSE; | |
5082 | |
5083 if (cin_islabel_skip(&s)) | |
5084 { | |
5085 /* | |
5086 * Only accept a label if the previous line is terminated or is a case | |
5087 * label. | |
5088 */ | |
5089 pos_T cursor_save; | |
5090 pos_T *trypos; | |
5091 char_u *line; | |
5092 | |
5093 cursor_save = curwin->w_cursor; | |
5094 while (curwin->w_cursor.lnum > 1) | |
5095 { | |
5096 --curwin->w_cursor.lnum; | |
5097 | |
5098 /* | |
5099 * If we're in a comment now, skip to the start of the comment. | |
5100 */ | |
5101 curwin->w_cursor.col = 0; | |
5102 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */ | |
5103 curwin->w_cursor = *trypos; | |
5104 | |
5105 line = ml_get_curline(); | |
5106 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */ | |
5107 continue; | |
5108 if (*(line = cin_skipcomment(line)) == NUL) | |
5109 continue; | |
5110 | |
5111 curwin->w_cursor = cursor_save; | |
5112 if (cin_isterminated(line, TRUE, FALSE) | |
5113 || 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
|
5114 || cin_iscase(line, TRUE) |
7 | 5115 || (cin_islabel_skip(&line) && cin_nocode(line))) |
5116 return TRUE; | |
5117 return FALSE; | |
5118 } | |
5119 curwin->w_cursor = cursor_save; | |
5120 return TRUE; /* label at start of file??? */ | |
5121 } | |
5122 return FALSE; | |
5123 } | |
5124 | |
5125 /* | |
5126 * Recognize structure initialization and enumerations. | |
5127 * Q&D-Implementation: | |
5128 * check for "=" at end or "[typedef] enum" at beginning of line. | |
5129 */ | |
5130 static int | |
5131 cin_isinit(void) | |
5132 { | |
5133 char_u *s; | |
5134 | |
5135 s = cin_skipcomment(ml_get_curline()); | |
5136 | |
5137 if (STRNCMP(s, "typedef", 7) == 0 && !vim_isIDc(s[7])) | |
5138 s = cin_skipcomment(s + 7); | |
5139 | |
5140 if (STRNCMP(s, "enum", 4) == 0 && !vim_isIDc(s[4])) | |
5141 return TRUE; | |
5142 | |
5143 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) | |
5144 return TRUE; | |
5145 | |
5146 return FALSE; | |
5147 } | |
5148 | |
5149 /* | |
5150 * Recognize a switch label: "case .*:" or "default:". | |
5151 */ | |
5152 int | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5153 cin_iscase(s, strict) |
7 | 5154 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
|
5155 int strict; /* Allow relaxed check of case statement for JS */ |
7 | 5156 { |
5157 s = cin_skipcomment(s); | |
5158 if (STRNCMP(s, "case", 4) == 0 && !vim_isIDc(s[4])) | |
5159 { | |
5160 for (s += 4; *s; ++s) | |
5161 { | |
5162 s = cin_skipcomment(s); | |
5163 if (*s == ':') | |
5164 { | |
5165 if (s[1] == ':') /* skip over "::" for C++ */ | |
5166 ++s; | |
5167 else | |
5168 return TRUE; | |
5169 } | |
5170 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
|
5171 s += 2; /* skip over ':' */ |
7 | 5172 else if (*s == '/' && (s[1] == '*' || s[1] == '/')) |
5173 return FALSE; /* stop at comment */ | |
5174 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
|
5175 { |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5176 /* JS etc. */ |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5177 if (strict) |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5178 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
|
5179 else |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5180 return TRUE; |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5181 } |
7 | 5182 } |
5183 return FALSE; | |
5184 } | |
5185 | |
5186 if (cin_isdefault(s)) | |
5187 return TRUE; | |
5188 return FALSE; | |
5189 } | |
5190 | |
5191 /* | |
5192 * Recognize a "default" switch label. | |
5193 */ | |
5194 static int | |
5195 cin_isdefault(s) | |
5196 char_u *s; | |
5197 { | |
5198 return (STRNCMP(s, "default", 7) == 0 | |
5199 && *(s = cin_skipcomment(s + 7)) == ':' | |
5200 && s[1] != ':'); | |
5201 } | |
5202 | |
5203 /* | |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
5204 * Recognize a "public/private/protected" scope declaration label. |
7 | 5205 */ |
5206 int | |
5207 cin_isscopedecl(s) | |
5208 char_u *s; | |
5209 { | |
5210 int i; | |
5211 | |
5212 s = cin_skipcomment(s); | |
5213 if (STRNCMP(s, "public", 6) == 0) | |
5214 i = 6; | |
5215 else if (STRNCMP(s, "protected", 9) == 0) | |
5216 i = 9; | |
5217 else if (STRNCMP(s, "private", 7) == 0) | |
5218 i = 7; | |
5219 else | |
5220 return FALSE; | |
5221 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); | |
5222 } | |
5223 | |
5224 /* | |
5225 * Return a pointer to the first non-empty non-comment character after a ':'. | |
5226 * Return NULL if not found. | |
5227 * case 234: a = b; | |
5228 * ^ | |
5229 */ | |
5230 static char_u * | |
5231 after_label(l) | |
5232 char_u *l; | |
5233 { | |
5234 for ( ; *l; ++l) | |
5235 { | |
5236 if (*l == ':') | |
5237 { | |
5238 if (l[1] == ':') /* skip over "::" for C++ */ | |
5239 ++l; | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5240 else if (!cin_iscase(l + 1, FALSE)) |
7 | 5241 break; |
5242 } | |
5243 else if (*l == '\'' && l[1] && l[2] == '\'') | |
5244 l += 2; /* skip over 'x' */ | |
5245 } | |
5246 if (*l == NUL) | |
5247 return NULL; | |
5248 l = cin_skipcomment(l + 1); | |
5249 if (*l == NUL) | |
5250 return NULL; | |
5251 return l; | |
5252 } | |
5253 | |
5254 /* | |
5255 * Get indent of line "lnum", skipping a label. | |
5256 * Return 0 if there is nothing after the label. | |
5257 */ | |
5258 static int | |
5259 get_indent_nolabel(lnum) /* XXX */ | |
5260 linenr_T lnum; | |
5261 { | |
5262 char_u *l; | |
5263 pos_T fp; | |
5264 colnr_T col; | |
5265 char_u *p; | |
5266 | |
5267 l = ml_get(lnum); | |
5268 p = after_label(l); | |
5269 if (p == NULL) | |
5270 return 0; | |
5271 | |
5272 fp.col = (colnr_T)(p - l); | |
5273 fp.lnum = lnum; | |
5274 getvcol(curwin, &fp, &col, NULL, NULL); | |
5275 return (int)col; | |
5276 } | |
5277 | |
5278 /* | |
5279 * Find indent for line "lnum", ignoring any case or jump label. | |
829 | 5280 * Also return a pointer to the text (after the label) in "pp". |
7 | 5281 * label: if (asdf && asdfasdf) |
5282 * ^ | |
5283 */ | |
5284 static int | |
5285 skip_label(lnum, pp, ind_maxcomment) | |
5286 linenr_T lnum; | |
5287 char_u **pp; | |
5288 int ind_maxcomment; | |
5289 { | |
5290 char_u *l; | |
5291 int amount; | |
5292 pos_T cursor_save; | |
5293 | |
5294 cursor_save = curwin->w_cursor; | |
5295 curwin->w_cursor.lnum = lnum; | |
5296 l = ml_get_curline(); | |
5297 /* XXX */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
5298 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
|
5299 || cin_islabel(ind_maxcomment)) |
7 | 5300 { |
5301 amount = get_indent_nolabel(lnum); | |
5302 l = after_label(ml_get_curline()); | |
5303 if (l == NULL) /* just in case */ | |
5304 l = ml_get_curline(); | |
5305 } | |
5306 else | |
5307 { | |
5308 amount = get_indent(); | |
5309 l = ml_get_curline(); | |
5310 } | |
5311 *pp = l; | |
5312 | |
5313 curwin->w_cursor = cursor_save; | |
5314 return amount; | |
5315 } | |
5316 | |
5317 /* | |
5318 * Return the indent of the first variable name after a type in a declaration. | |
5319 * int a, indent of "a" | |
5320 * static struct foo b, indent of "b" | |
5321 * enum bla c, indent of "c" | |
5322 * Returns zero when it doesn't look like a declaration. | |
5323 */ | |
5324 static int | |
5325 cin_first_id_amount() | |
5326 { | |
5327 char_u *line, *p, *s; | |
5328 int len; | |
5329 pos_T fp; | |
5330 colnr_T col; | |
5331 | |
5332 line = ml_get_curline(); | |
5333 p = skipwhite(line); | |
835 | 5334 len = (int)(skiptowhite(p) - p); |
7 | 5335 if (len == 6 && STRNCMP(p, "static", 6) == 0) |
5336 { | |
5337 p = skipwhite(p + 6); | |
856 | 5338 len = (int)(skiptowhite(p) - p); |
7 | 5339 } |
5340 if (len == 6 && STRNCMP(p, "struct", 6) == 0) | |
5341 p = skipwhite(p + 6); | |
5342 else if (len == 4 && STRNCMP(p, "enum", 4) == 0) | |
5343 p = skipwhite(p + 4); | |
5344 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) | |
5345 || (len == 6 && STRNCMP(p, "signed", 6) == 0)) | |
5346 { | |
5347 s = skipwhite(p + len); | |
5348 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3])) | |
5349 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4])) | |
5350 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5])) | |
5351 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4]))) | |
5352 p = s; | |
5353 } | |
5354 for (len = 0; vim_isIDc(p[len]); ++len) | |
5355 ; | |
5356 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p)) | |
5357 return 0; | |
5358 | |
5359 p = skipwhite(p + len); | |
5360 fp.lnum = curwin->w_cursor.lnum; | |
5361 fp.col = (colnr_T)(p - line); | |
5362 getvcol(curwin, &fp, &col, NULL, NULL); | |
5363 return (int)col; | |
5364 } | |
5365 | |
5366 /* | |
5367 * Return the indent of the first non-blank after an equal sign. | |
5368 * char *foo = "here"; | |
5369 * Return zero if no (useful) equal sign found. | |
5370 * Return -1 if the line above "lnum" ends in a backslash. | |
5371 * foo = "asdf\ | |
5372 * asdf\ | |
5373 * here"; | |
5374 */ | |
5375 static int | |
5376 cin_get_equal_amount(lnum) | |
5377 linenr_T lnum; | |
5378 { | |
5379 char_u *line; | |
5380 char_u *s; | |
5381 colnr_T col; | |
5382 pos_T fp; | |
5383 | |
5384 if (lnum > 1) | |
5385 { | |
5386 line = ml_get(lnum - 1); | |
5387 if (*line != NUL && line[STRLEN(line) - 1] == '\\') | |
5388 return -1; | |
5389 } | |
5390 | |
5391 line = s = ml_get(lnum); | |
5392 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) | |
5393 { | |
5394 if (cin_iscomment(s)) /* ignore comments */ | |
5395 s = cin_skipcomment(s); | |
5396 else | |
5397 ++s; | |
5398 } | |
5399 if (*s != '=') | |
5400 return 0; | |
5401 | |
5402 s = skipwhite(s + 1); | |
5403 if (cin_nocode(s)) | |
5404 return 0; | |
5405 | |
5406 if (*s == '"') /* nice alignment for continued strings */ | |
5407 ++s; | |
5408 | |
5409 fp.lnum = lnum; | |
5410 fp.col = (colnr_T)(s - line); | |
5411 getvcol(curwin, &fp, &col, NULL, NULL); | |
5412 return (int)col; | |
5413 } | |
5414 | |
5415 /* | |
5416 * Recognize a preprocessor statement: Any line that starts with '#'. | |
5417 */ | |
5418 static int | |
5419 cin_ispreproc(s) | |
5420 char_u *s; | |
5421 { | |
2776 | 5422 if (*skipwhite(s) == '#') |
7 | 5423 return TRUE; |
5424 return FALSE; | |
5425 } | |
5426 | |
5427 /* | |
5428 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a | |
5429 * continuation line of a preprocessor statement. Decrease "*lnump" to the | |
5430 * start and return the line in "*pp". | |
5431 */ | |
5432 static int | |
5433 cin_ispreproc_cont(pp, lnump) | |
5434 char_u **pp; | |
5435 linenr_T *lnump; | |
5436 { | |
5437 char_u *line = *pp; | |
5438 linenr_T lnum = *lnump; | |
5439 int retval = FALSE; | |
5440 | |
408 | 5441 for (;;) |
7 | 5442 { |
5443 if (cin_ispreproc(line)) | |
5444 { | |
5445 retval = TRUE; | |
5446 *lnump = lnum; | |
5447 break; | |
5448 } | |
5449 if (lnum == 1) | |
5450 break; | |
5451 line = ml_get(--lnum); | |
5452 if (*line == NUL || line[STRLEN(line) - 1] != '\\') | |
5453 break; | |
5454 } | |
5455 | |
5456 if (lnum != *lnump) | |
5457 *pp = ml_get(*lnump); | |
5458 return retval; | |
5459 } | |
5460 | |
5461 /* | |
5462 * Recognize the start of a C or C++ comment. | |
5463 */ | |
5464 static int | |
5465 cin_iscomment(p) | |
5466 char_u *p; | |
5467 { | |
5468 return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); | |
5469 } | |
5470 | |
5471 /* | |
5472 * Recognize the start of a "//" comment. | |
5473 */ | |
5474 static int | |
5475 cin_islinecomment(p) | |
5476 char_u *p; | |
5477 { | |
5478 return (p[0] == '/' && p[1] == '/'); | |
5479 } | |
5480 | |
5481 /* | |
2805 | 5482 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or |
5483 * '}'. | |
7 | 5484 * Don't consider "} else" a terminated line. |
2805 | 5485 * Don't consider a line where there are unmatched opening braces before '}', |
5486 * ';' or ',' a terminated line. | |
7 | 5487 * Return the character terminating the line (ending char's have precedence if |
5488 * both apply in order to determine initializations). | |
5489 */ | |
5490 static int | |
5491 cin_isterminated(s, incl_open, incl_comma) | |
5492 char_u *s; | |
5493 int incl_open; /* include '{' at the end as terminator */ | |
5494 int incl_comma; /* recognize a trailing comma */ | |
5495 { | |
5496 char_u found_start = 0; | |
2805 | 5497 unsigned n_open = 0; |
7 | 5498 |
5499 s = cin_skipcomment(s); | |
5500 | |
5501 if (*s == '{' || (*s == '}' && !cin_iselse(s))) | |
5502 found_start = *s; | |
5503 | |
5504 while (*s) | |
5505 { | |
5506 /* skip over comments, "" strings and 'c'haracters */ | |
5507 s = skip_string(cin_skipcomment(s)); | |
2805 | 5508 if (*s == '}' && n_open > 0) |
5509 --n_open; | |
5510 if (n_open == 0 | |
5511 && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) | |
7 | 5512 && cin_nocode(s + 1)) |
5513 return *s; | |
2805 | 5514 else if (*s == '{') |
5515 { | |
5516 if (incl_open && cin_nocode(s + 1)) | |
5517 return *s; | |
5518 else | |
5519 ++n_open; | |
5520 } | |
7 | 5521 |
5522 if (*s) | |
5523 s++; | |
5524 } | |
5525 return found_start; | |
5526 } | |
5527 | |
5528 /* | |
5529 * Recognize the basic picture of a function declaration -- it needs to | |
5530 * have an open paren somewhere and a close paren at the end of the line and | |
5531 * no semicolons anywhere. | |
5532 * When a line ends in a comma we continue looking in the next line. | |
5533 * "sp" points to a string with the line. When looking at other lines it must | |
5534 * be restored to the line. When it's NULL fetch lines here. | |
5535 * "lnum" is where we start looking. | |
5536 */ | |
5537 static int | |
5538 cin_isfuncdecl(sp, first_lnum) | |
5539 char_u **sp; | |
5540 linenr_T first_lnum; | |
5541 { | |
5542 char_u *s; | |
5543 linenr_T lnum = first_lnum; | |
5544 int retval = FALSE; | |
5545 | |
5546 if (sp == NULL) | |
5547 s = ml_get(lnum); | |
5548 else | |
5549 s = *sp; | |
5550 | |
2776 | 5551 /* Ignore line starting with #. */ |
5552 if (cin_ispreproc(s)) | |
5553 return FALSE; | |
5554 | |
7 | 5555 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') |
5556 { | |
5557 if (cin_iscomment(s)) /* ignore comments */ | |
5558 s = cin_skipcomment(s); | |
5559 else | |
5560 ++s; | |
5561 } | |
5562 if (*s != '(') | |
5563 return FALSE; /* ';', ' or " before any () or no '(' */ | |
5564 | |
5565 while (*s && *s != ';' && *s != '\'' && *s != '"') | |
5566 { | |
5567 if (*s == ')' && cin_nocode(s + 1)) | |
5568 { | |
5569 /* ')' at the end: may have found a match | |
5570 * Check for he previous line not to end in a backslash: | |
5571 * #if defined(x) && \ | |
5572 * defined(y) | |
5573 */ | |
5574 lnum = first_lnum - 1; | |
5575 s = ml_get(lnum); | |
5576 if (*s == NUL || s[STRLEN(s) - 1] != '\\') | |
5577 retval = TRUE; | |
5578 goto done; | |
5579 } | |
2776 | 5580 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) |
5581 { | |
5582 int comma = (*s == ','); | |
5583 | |
5584 /* ',' at the end: continue looking in the next line. | |
5585 * At the end: check for ',' in the next line, for this style: | |
5586 * func(arg1 | |
5587 * , arg2) */ | |
5588 for (;;) | |
5589 { | |
5590 if (lnum >= curbuf->b_ml.ml_line_count) | |
5591 break; | |
5592 s = ml_get(++lnum); | |
5593 if (!cin_ispreproc(s)) | |
5594 break; | |
5595 } | |
7 | 5596 if (lnum >= curbuf->b_ml.ml_line_count) |
5597 break; | |
2776 | 5598 /* Require a comma at end of the line or a comma or ')' at the |
5599 * start of next line. */ | |
5600 s = skipwhite(s); | |
5601 if (!comma && *s != ',' && *s != ')') | |
5602 break; | |
7 | 5603 } |
5604 else if (cin_iscomment(s)) /* ignore comments */ | |
5605 s = cin_skipcomment(s); | |
5606 else | |
5607 ++s; | |
5608 } | |
5609 | |
5610 done: | |
5611 if (lnum != first_lnum && sp != NULL) | |
5612 *sp = ml_get(first_lnum); | |
5613 | |
5614 return retval; | |
5615 } | |
5616 | |
5617 static int | |
5618 cin_isif(p) | |
5619 char_u *p; | |
5620 { | |
5621 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); | |
5622 } | |
5623 | |
5624 static int | |
5625 cin_iselse(p) | |
5626 char_u *p; | |
5627 { | |
5628 if (*p == '}') /* accept "} else" */ | |
5629 p = cin_skipcomment(p + 1); | |
5630 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); | |
5631 } | |
5632 | |
5633 static int | |
5634 cin_isdo(p) | |
5635 char_u *p; | |
5636 { | |
5637 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); | |
5638 } | |
5639 | |
5640 /* | |
5641 * Check if this is a "while" that should have a matching "do". | |
5642 * We only accept a "while (condition) ;", with only white space between the | |
5643 * ')' and ';'. The condition may be spread over several lines. | |
5644 */ | |
5645 static int | |
5646 cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */ | |
5647 char_u *p; | |
5648 linenr_T lnum; | |
5649 int ind_maxparen; | |
5650 { | |
5651 pos_T cursor_save; | |
5652 pos_T *trypos; | |
5653 int retval = FALSE; | |
5654 | |
5655 p = cin_skipcomment(p); | |
5656 if (*p == '}') /* accept "} while (cond);" */ | |
5657 p = cin_skipcomment(p + 1); | |
5658 if (STRNCMP(p, "while", 5) == 0 && !vim_isIDc(p[5])) | |
5659 { | |
5660 cursor_save = curwin->w_cursor; | |
5661 curwin->w_cursor.lnum = lnum; | |
5662 curwin->w_cursor.col = 0; | |
5663 p = ml_get_curline(); | |
5664 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */ | |
5665 { | |
5666 ++p; | |
5667 ++curwin->w_cursor.col; | |
5668 } | |
5669 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL | |
5670 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') | |
5671 retval = TRUE; | |
5672 curwin->w_cursor = cursor_save; | |
5673 } | |
5674 return retval; | |
5675 } | |
5676 | |
829 | 5677 /* |
5678 * Return TRUE if we are at the end of a do-while. | |
5679 * do | |
5680 * nothing; | |
5681 * while (foo | |
856 | 5682 * && bar); <-- here |
829 | 5683 * Adjust the cursor to the line with "while". |
5684 */ | |
5685 static int | |
5686 cin_iswhileofdo_end(terminated, ind_maxparen, ind_maxcomment) | |
5687 int terminated; | |
5688 int ind_maxparen; | |
5689 int ind_maxcomment; | |
5690 { | |
5691 char_u *line; | |
5692 char_u *p; | |
5693 char_u *s; | |
5694 pos_T *trypos; | |
5695 int i; | |
5696 | |
5697 if (terminated != ';') /* there must be a ';' at the end */ | |
5698 return FALSE; | |
5699 | |
5700 p = line = ml_get_curline(); | |
5701 while (*p != NUL) | |
5702 { | |
5703 p = cin_skipcomment(p); | |
5704 if (*p == ')') | |
5705 { | |
5706 s = skipwhite(p + 1); | |
5707 if (*s == ';' && cin_nocode(s + 1)) | |
5708 { | |
5709 /* Found ");" at end of the line, now check there is "while" | |
5710 * before the matching '('. XXX */ | |
835 | 5711 i = (int)(p - line); |
829 | 5712 curwin->w_cursor.col = i; |
5713 trypos = find_match_paren(ind_maxparen, ind_maxcomment); | |
5714 if (trypos != NULL) | |
5715 { | |
5716 s = cin_skipcomment(ml_get(trypos->lnum)); | |
5717 if (*s == '}') /* accept "} while (cond);" */ | |
5718 s = cin_skipcomment(s + 1); | |
5719 if (STRNCMP(s, "while", 5) == 0 && !vim_isIDc(s[5])) | |
5720 { | |
5721 curwin->w_cursor.lnum = trypos->lnum; | |
5722 return TRUE; | |
5723 } | |
5724 } | |
5725 | |
5726 /* Searching may have made "line" invalid, get it again. */ | |
5727 line = ml_get_curline(); | |
5728 p = line + i; | |
5729 } | |
5730 } | |
5731 if (*p != NUL) | |
5732 ++p; | |
5733 } | |
5734 return FALSE; | |
5735 } | |
5736 | |
7 | 5737 static int |
5738 cin_isbreak(p) | |
5739 char_u *p; | |
5740 { | |
5741 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); | |
5742 } | |
5743 | |
828 | 5744 /* |
5745 * Find the position of a C++ base-class declaration or | |
7 | 5746 * constructor-initialization. eg: |
5747 * | |
5748 * class MyClass : | |
5749 * baseClass <-- here | |
5750 * class MyClass : public baseClass, | |
5751 * anotherBaseClass <-- here (should probably lineup ??) | |
5752 * MyClass::MyClass(...) : | |
5753 * baseClass(...) <-- here (constructor-initialization) | |
827 | 5754 * |
5755 * This is a lot of guessing. Watch out for "cond ? func() : foo". | |
7 | 5756 */ |
5757 static int | |
1336 | 5758 cin_is_cpp_baseclass(col) |
828 | 5759 colnr_T *col; /* return: column to align with */ |
7 | 5760 { |
5761 char_u *s; | |
5762 int class_or_struct, lookfor_ctor_init, cpp_base_class; | |
828 | 5763 linenr_T lnum = curwin->w_cursor.lnum; |
1336 | 5764 char_u *line = ml_get_curline(); |
7 | 5765 |
5766 *col = 0; | |
5767 | |
17 | 5768 s = skipwhite(line); |
5769 if (*s == '#') /* skip #define FOO x ? (x) : x */ | |
5770 return FALSE; | |
5771 s = cin_skipcomment(s); | |
7 | 5772 if (*s == NUL) |
5773 return FALSE; | |
5774 | |
5775 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; | |
5776 | |
828 | 5777 /* Search for a line starting with '#', empty, ending in ';' or containing |
5778 * '{' or '}' and start below it. This handles the following situations: | |
5779 * a = cond ? | |
5780 * func() : | |
856 | 5781 * asdf; |
828 | 5782 * func::foo() |
5783 * : something | |
5784 * {} | |
5785 * Foo::Foo (int one, int two) | |
5786 * : something(4), | |
5787 * somethingelse(3) | |
5788 * {} | |
5789 */ | |
5790 while (lnum > 1) | |
5791 { | |
1336 | 5792 line = ml_get(lnum - 1); |
5793 s = skipwhite(line); | |
828 | 5794 if (*s == '#' || *s == NUL) |
5795 break; | |
5796 while (*s != NUL) | |
5797 { | |
5798 s = cin_skipcomment(s); | |
5799 if (*s == '{' || *s == '}' | |
5800 || (*s == ';' && cin_nocode(s + 1))) | |
5801 break; | |
5802 if (*s != NUL) | |
5803 ++s; | |
5804 } | |
5805 if (*s != NUL) | |
5806 break; | |
5807 --lnum; | |
5808 } | |
5809 | |
1336 | 5810 line = ml_get(lnum); |
5811 s = cin_skipcomment(line); | |
828 | 5812 for (;;) |
5813 { | |
5814 if (*s == NUL) | |
5815 { | |
5816 if (lnum == curwin->w_cursor.lnum) | |
5817 break; | |
5818 /* Continue in the cursor line. */ | |
1336 | 5819 line = ml_get(++lnum); |
5820 s = cin_skipcomment(line); | |
5821 if (*s == NUL) | |
5822 continue; | |
828 | 5823 } |
5824 | |
2807 | 5825 if (s[0] == '"') |
5826 s = skip_string(s) + 1; | |
5827 else if (s[0] == ':') | |
7 | 5828 { |
5829 if (s[1] == ':') | |
5830 { | |
5831 /* skip double colon. It can't be a constructor | |
5832 * initialization any more */ | |
5833 lookfor_ctor_init = FALSE; | |
5834 s = cin_skipcomment(s + 2); | |
5835 } | |
5836 else if (lookfor_ctor_init || class_or_struct) | |
5837 { | |
5838 /* we have something found, that looks like the start of | |
1532 | 5839 * cpp-base-class-declaration or constructor-initialization */ |
7 | 5840 cpp_base_class = TRUE; |
5841 lookfor_ctor_init = class_or_struct = FALSE; | |
5842 *col = 0; | |
5843 s = cin_skipcomment(s + 1); | |
5844 } | |
5845 else | |
5846 s = cin_skipcomment(s + 1); | |
5847 } | |
5848 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) | |
5849 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) | |
5850 { | |
5851 class_or_struct = TRUE; | |
5852 lookfor_ctor_init = FALSE; | |
5853 | |
5854 if (*s == 'c') | |
5855 s = cin_skipcomment(s + 5); | |
5856 else | |
5857 s = cin_skipcomment(s + 6); | |
5858 } | |
5859 else | |
5860 { | |
5861 if (s[0] == '{' || s[0] == '}' || s[0] == ';') | |
5862 { | |
5863 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; | |
5864 } | |
5865 else if (s[0] == ')') | |
5866 { | |
5867 /* Constructor-initialization is assumed if we come across | |
5868 * something like "):" */ | |
5869 class_or_struct = FALSE; | |
5870 lookfor_ctor_init = TRUE; | |
5871 } | |
827 | 5872 else if (s[0] == '?') |
5873 { | |
5874 /* Avoid seeing '() :' after '?' as constructor init. */ | |
5875 return FALSE; | |
5876 } | |
7 | 5877 else if (!vim_isIDc(s[0])) |
5878 { | |
5879 /* if it is not an identifier, we are wrong */ | |
5880 class_or_struct = FALSE; | |
5881 lookfor_ctor_init = FALSE; | |
5882 } | |
5883 else if (*col == 0) | |
5884 { | |
5885 /* it can't be a constructor-initialization any more */ | |
5886 lookfor_ctor_init = FALSE; | |
5887 | |
5888 /* the first statement starts here: lineup with this one... */ | |
828 | 5889 if (cpp_base_class) |
7 | 5890 *col = (colnr_T)(s - line); |
5891 } | |
5892 | |
828 | 5893 /* When the line ends in a comma don't align with it. */ |
5894 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) | |
5895 *col = 0; | |
5896 | |
7 | 5897 s = cin_skipcomment(s + 1); |
5898 } | |
5899 } | |
5900 | |
5901 return cpp_base_class; | |
5902 } | |
5903 | |
828 | 5904 static int |
5905 get_baseclass_amount(col, ind_maxparen, ind_maxcomment, ind_cpp_baseclass) | |
5906 int col; | |
5907 int ind_maxparen; | |
5908 int ind_maxcomment; | |
5909 int ind_cpp_baseclass; | |
5910 { | |
5911 int amount; | |
5912 colnr_T vcol; | |
5913 pos_T *trypos; | |
5914 | |
5915 if (col == 0) | |
5916 { | |
5917 amount = get_indent(); | |
5918 if (find_last_paren(ml_get_curline(), '(', ')') | |
5919 && (trypos = find_match_paren(ind_maxparen, | |
5920 ind_maxcomment)) != NULL) | |
5921 amount = get_indent_lnum(trypos->lnum); /* XXX */ | |
5922 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) | |
5923 amount += ind_cpp_baseclass; | |
5924 } | |
5925 else | |
5926 { | |
5927 curwin->w_cursor.col = col; | |
5928 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); | |
5929 amount = (int)vcol; | |
5930 } | |
5931 if (amount < ind_cpp_baseclass) | |
5932 amount = ind_cpp_baseclass; | |
5933 return amount; | |
5934 } | |
5935 | |
7 | 5936 /* |
5937 * Return TRUE if string "s" ends with the string "find", possibly followed by | |
5938 * white space and comments. Skip strings and comments. | |
5939 * Ignore "ignore" after "find" if it's not NULL. | |
5940 */ | |
5941 static int | |
5942 cin_ends_in(s, find, ignore) | |
5943 char_u *s; | |
5944 char_u *find; | |
5945 char_u *ignore; | |
5946 { | |
5947 char_u *p = s; | |
5948 char_u *r; | |
5949 int len = (int)STRLEN(find); | |
5950 | |
5951 while (*p != NUL) | |
5952 { | |
5953 p = cin_skipcomment(p); | |
5954 if (STRNCMP(p, find, len) == 0) | |
5955 { | |
5956 r = skipwhite(p + len); | |
5957 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) | |
5958 r = skipwhite(r + STRLEN(ignore)); | |
5959 if (cin_nocode(r)) | |
5960 return TRUE; | |
5961 } | |
5962 if (*p != NUL) | |
5963 ++p; | |
5964 } | |
5965 return FALSE; | |
5966 } | |
5967 | |
5968 /* | |
5969 * Skip strings, chars and comments until at or past "trypos". | |
5970 * Return the column found. | |
5971 */ | |
5972 static int | |
5973 cin_skip2pos(trypos) | |
5974 pos_T *trypos; | |
5975 { | |
5976 char_u *line; | |
5977 char_u *p; | |
5978 | |
5979 p = line = ml_get(trypos->lnum); | |
5980 while (*p && (colnr_T)(p - line) < trypos->col) | |
5981 { | |
5982 if (cin_iscomment(p)) | |
5983 p = cin_skipcomment(p); | |
5984 else | |
5985 { | |
5986 p = skip_string(p); | |
5987 ++p; | |
5988 } | |
5989 } | |
5990 return (int)(p - line); | |
5991 } | |
5992 | |
5993 /* | |
5994 * Find the '{' at the start of the block we are in. | |
5995 * Return NULL if no match found. | |
5996 * Ignore a '{' that is in a comment, makes indenting the next three lines | |
5997 * work. */ | |
5998 /* foo() */ | |
5999 /* { */ | |
6000 /* } */ | |
6001 | |
6002 static pos_T * | |
6003 find_start_brace(ind_maxcomment) /* XXX */ | |
6004 int ind_maxcomment; | |
6005 { | |
6006 pos_T cursor_save; | |
6007 pos_T *trypos; | |
6008 pos_T *pos; | |
6009 static pos_T pos_copy; | |
6010 | |
6011 cursor_save = curwin->w_cursor; | |
6012 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) | |
6013 { | |
6014 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */ | |
6015 trypos = &pos_copy; | |
6016 curwin->w_cursor = *trypos; | |
6017 pos = NULL; | |
829 | 6018 /* ignore the { if it's in a // or / * * / comment */ |
7 | 6019 if ((colnr_T)cin_skip2pos(trypos) == trypos->col |
6020 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */ | |
6021 break; | |
6022 if (pos != NULL) | |
6023 curwin->w_cursor.lnum = pos->lnum; | |
6024 } | |
6025 curwin->w_cursor = cursor_save; | |
6026 return trypos; | |
6027 } | |
6028 | |
6029 /* | |
6030 * Find the matching '(', failing if it is in a comment. | |
6031 * Return NULL of no match found. | |
6032 */ | |
6033 static pos_T * | |
6034 find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */ | |
6035 int ind_maxparen; | |
6036 int ind_maxcomment; | |
6037 { | |
6038 pos_T cursor_save; | |
6039 pos_T *trypos; | |
829 | 6040 static pos_T pos_copy; |
7 | 6041 |
6042 cursor_save = curwin->w_cursor; | |
6043 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL) | |
6044 { | |
6045 /* check if the ( is in a // comment */ | |
6046 if ((colnr_T)cin_skip2pos(trypos) > trypos->col) | |
6047 trypos = NULL; | |
6048 else | |
6049 { | |
6050 pos_copy = *trypos; /* copy trypos, findmatch will change it */ | |
6051 trypos = &pos_copy; | |
6052 curwin->w_cursor = *trypos; | |
6053 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */ | |
6054 trypos = NULL; | |
6055 } | |
6056 } | |
6057 curwin->w_cursor = cursor_save; | |
6058 return trypos; | |
6059 } | |
6060 | |
6061 /* | |
6062 * Return ind_maxparen corrected for the difference in line number between the | |
6063 * cursor position and "startpos". This makes sure that searching for a | |
6064 * matching paren above the cursor line doesn't find a match because of | |
6065 * looking a few lines further. | |
6066 */ | |
6067 static int | |
6068 corr_ind_maxparen(ind_maxparen, startpos) | |
6069 int ind_maxparen; | |
6070 pos_T *startpos; | |
6071 { | |
6072 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; | |
6073 | |
6074 if (n > 0 && n < ind_maxparen / 2) | |
6075 return ind_maxparen - (int)n; | |
6076 return ind_maxparen; | |
6077 } | |
6078 | |
6079 /* | |
6080 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in | |
6081 * line "l". | |
6082 */ | |
6083 static int | |
6084 find_last_paren(l, start, end) | |
6085 char_u *l; | |
6086 int start, end; | |
6087 { | |
6088 int i; | |
6089 int retval = FALSE; | |
6090 int open_count = 0; | |
6091 | |
6092 curwin->w_cursor.col = 0; /* default is start of line */ | |
6093 | |
6094 for (i = 0; l[i]; i++) | |
6095 { | |
6096 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */ | |
6097 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */ | |
6098 if (l[i] == start) | |
6099 ++open_count; | |
6100 else if (l[i] == end) | |
6101 { | |
6102 if (open_count > 0) | |
6103 --open_count; | |
6104 else | |
6105 { | |
6106 curwin->w_cursor.col = i; | |
6107 retval = TRUE; | |
6108 } | |
6109 } | |
6110 } | |
6111 return retval; | |
6112 } | |
6113 | |
6114 int | |
6115 get_c_indent() | |
6116 { | |
6117 /* | |
6118 * spaces from a block's opening brace the prevailing indent for that | |
6119 * block should be | |
6120 */ | |
6121 int ind_level = curbuf->b_p_sw; | |
6122 | |
6123 /* | |
6124 * spaces from the edge of the line an open brace that's at the end of a | |
6125 * line is imagined to be. | |
6126 */ | |
6127 int ind_open_imag = 0; | |
6128 | |
6129 /* | |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
6130 * spaces from the prevailing indent for a line that is not preceded by |
7 | 6131 * an opening brace. |
6132 */ | |
6133 int ind_no_brace = 0; | |
6134 | |
6135 /* | |
6136 * column where the first { of a function should be located } | |
6137 */ | |
6138 int ind_first_open = 0; | |
6139 | |
6140 /* | |
6141 * spaces from the prevailing indent a leftmost open brace should be | |
6142 * located | |
6143 */ | |
6144 int ind_open_extra = 0; | |
6145 | |
6146 /* | |
6147 * spaces from the matching open brace (real location for one at the left | |
6148 * edge; imaginary location from one that ends a line) the matching close | |
6149 * brace should be located | |
6150 */ | |
6151 int ind_close_extra = 0; | |
6152 | |
6153 /* | |
6154 * spaces from the edge of the line an open brace sitting in the leftmost | |
6155 * column is imagined to be | |
6156 */ | |
6157 int ind_open_left_imag = 0; | |
6158 | |
6159 /* | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6160 * 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
|
6161 * 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
|
6162 */ |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6163 int ind_jump_label = -1; |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6164 |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6165 /* |
7 | 6166 * spaces from the switch() indent a "case xx" label should be located |
6167 */ | |
6168 int ind_case = curbuf->b_p_sw; | |
6169 | |
6170 /* | |
6171 * spaces from the "case xx:" code after a switch() should be located | |
6172 */ | |
6173 int ind_case_code = curbuf->b_p_sw; | |
6174 | |
6175 /* | |
6176 * lineup break at end of case in switch() with case label | |
6177 */ | |
6178 int ind_case_break = 0; | |
6179 | |
6180 /* | |
6181 * spaces from the class declaration indent a scope declaration label | |
6182 * should be located | |
6183 */ | |
6184 int ind_scopedecl = curbuf->b_p_sw; | |
6185 | |
6186 /* | |
6187 * spaces from the scope declaration label code should be located | |
6188 */ | |
6189 int ind_scopedecl_code = curbuf->b_p_sw; | |
6190 | |
6191 /* | |
6192 * amount K&R-style parameters should be indented | |
6193 */ | |
6194 int ind_param = curbuf->b_p_sw; | |
6195 | |
6196 /* | |
6197 * amount a function type spec should be indented | |
6198 */ | |
6199 int ind_func_type = curbuf->b_p_sw; | |
6200 | |
6201 /* | |
6202 * amount a cpp base class declaration or constructor initialization | |
6203 * should be indented | |
6204 */ | |
6205 int ind_cpp_baseclass = curbuf->b_p_sw; | |
6206 | |
6207 /* | |
6208 * additional spaces beyond the prevailing indent a continuation line | |
6209 * should be located | |
6210 */ | |
6211 int ind_continuation = curbuf->b_p_sw; | |
6212 | |
6213 /* | |
6214 * spaces from the indent of the line with an unclosed parentheses | |
6215 */ | |
6216 int ind_unclosed = curbuf->b_p_sw * 2; | |
6217 | |
6218 /* | |
6219 * spaces from the indent of the line with an unclosed parentheses, which | |
6220 * itself is also unclosed | |
6221 */ | |
6222 int ind_unclosed2 = curbuf->b_p_sw; | |
6223 | |
6224 /* | |
6225 * suppress ignoring spaces from the indent of a line starting with an | |
6226 * unclosed parentheses. | |
6227 */ | |
6228 int ind_unclosed_noignore = 0; | |
6229 | |
6230 /* | |
6231 * If the opening paren is the last nonwhite character on the line, and | |
6232 * ind_unclosed_wrapped is nonzero, use this indent relative to the outer | |
6233 * context (for very long lines). | |
6234 */ | |
6235 int ind_unclosed_wrapped = 0; | |
6236 | |
6237 /* | |
6238 * suppress ignoring white space when lining up with the character after | |
6239 * an unclosed parentheses. | |
6240 */ | |
6241 int ind_unclosed_whiteok = 0; | |
6242 | |
6243 /* | |
6244 * indent a closing parentheses under the line start of the matching | |
6245 * opening parentheses. | |
6246 */ | |
6247 int ind_matching_paren = 0; | |
6248 | |
6249 /* | |
829 | 6250 * indent a closing parentheses under the previous line. |
6251 */ | |
6252 int ind_paren_prev = 0; | |
6253 | |
6254 /* | |
7 | 6255 * Extra indent for comments. |
6256 */ | |
6257 int ind_comment = 0; | |
6258 | |
6259 /* | |
6260 * spaces from the comment opener when there is nothing after it. | |
6261 */ | |
6262 int ind_in_comment = 3; | |
6263 | |
6264 /* | |
6265 * boolean: if non-zero, use ind_in_comment even if there is something | |
6266 * after the comment opener. | |
6267 */ | |
6268 int ind_in_comment2 = 0; | |
6269 | |
6270 /* | |
6271 * max lines to search for an open paren | |
6272 */ | |
6273 int ind_maxparen = 20; | |
6274 | |
6275 /* | |
6276 * max lines to search for an open comment | |
6277 */ | |
6278 int ind_maxcomment = 70; | |
6279 | |
6280 /* | |
6281 * handle braces for java code | |
6282 */ | |
6283 int ind_java = 0; | |
6284 | |
6285 /* | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6286 * 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
|
6287 */ |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6288 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
|
6289 |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6290 /* |
7 | 6291 * handle blocked cases correctly |
6292 */ | |
6293 int ind_keep_case_label = 0; | |
6294 | |
6295 pos_T cur_curpos; | |
6296 int amount; | |
6297 int scope_amount; | |
834 | 6298 int cur_amount = MAXCOL; |
7 | 6299 colnr_T col; |
6300 char_u *theline; | |
6301 char_u *linecopy; | |
6302 pos_T *trypos; | |
6303 pos_T *tryposBrace = NULL; | |
6304 pos_T our_paren_pos; | |
6305 char_u *start; | |
6306 int start_brace; | |
1532 | 6307 #define BRACE_IN_COL0 1 /* '{' is in column 0 */ |
7 | 6308 #define BRACE_AT_START 2 /* '{' is at start of line */ |
6309 #define BRACE_AT_END 3 /* '{' is at end of line */ | |
6310 linenr_T ourscope; | |
6311 char_u *l; | |
6312 char_u *look; | |
6313 char_u terminated; | |
6314 int lookfor; | |
6315 #define LOOKFOR_INITIAL 0 | |
6316 #define LOOKFOR_IF 1 | |
6317 #define LOOKFOR_DO 2 | |
6318 #define LOOKFOR_CASE 3 | |
6319 #define LOOKFOR_ANY 4 | |
6320 #define LOOKFOR_TERM 5 | |
6321 #define LOOKFOR_UNTERM 6 | |
6322 #define LOOKFOR_SCOPEDECL 7 | |
6323 #define LOOKFOR_NOBREAK 8 | |
6324 #define LOOKFOR_CPP_BASECLASS 9 | |
6325 #define LOOKFOR_ENUM_OR_INIT 10 | |
6326 | |
6327 int whilelevel; | |
6328 linenr_T lnum; | |
6329 char_u *options; | |
6330 int fraction = 0; /* init for GCC */ | |
6331 int divider; | |
6332 int n; | |
6333 int iscase; | |
6334 int lookfor_break; | |
6335 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
|
6336 int original_line_islabel; |
7 | 6337 |
6338 for (options = curbuf->b_p_cino; *options; ) | |
6339 { | |
6340 l = options++; | |
6341 if (*options == '-') | |
6342 ++options; | |
6343 n = getdigits(&options); | |
6344 divider = 0; | |
6345 if (*options == '.') /* ".5s" means a fraction */ | |
6346 { | |
6347 fraction = atol((char *)++options); | |
6348 while (VIM_ISDIGIT(*options)) | |
6349 { | |
6350 ++options; | |
6351 if (divider) | |
6352 divider *= 10; | |
6353 else | |
6354 divider = 10; | |
6355 } | |
6356 } | |
6357 if (*options == 's') /* "2s" means two times 'shiftwidth' */ | |
6358 { | |
6359 if (n == 0 && fraction == 0) | |
6360 n = curbuf->b_p_sw; /* just "s" is one 'shiftwidth' */ | |
6361 else | |
6362 { | |
6363 n *= curbuf->b_p_sw; | |
6364 if (divider) | |
6365 n += (curbuf->b_p_sw * fraction + divider / 2) / divider; | |
6366 } | |
6367 ++options; | |
6368 } | |
6369 if (l[1] == '-') | |
6370 n = -n; | |
6371 /* When adding an entry here, also update the default 'cinoptions' in | |
1096 | 6372 * doc/indent.txt, and add explanation for it! */ |
7 | 6373 switch (*l) |
6374 { | |
6375 case '>': ind_level = n; break; | |
6376 case 'e': ind_open_imag = n; break; | |
6377 case 'n': ind_no_brace = n; break; | |
6378 case 'f': ind_first_open = n; break; | |
6379 case '{': ind_open_extra = n; break; | |
6380 case '}': ind_close_extra = n; break; | |
6381 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
|
6382 case 'L': ind_jump_label = n; break; |
7 | 6383 case ':': ind_case = n; break; |
6384 case '=': ind_case_code = n; break; | |
6385 case 'b': ind_case_break = n; break; | |
6386 case 'p': ind_param = n; break; | |
6387 case 't': ind_func_type = n; break; | |
6388 case '/': ind_comment = n; break; | |
6389 case 'c': ind_in_comment = n; break; | |
6390 case 'C': ind_in_comment2 = n; break; | |
6391 case 'i': ind_cpp_baseclass = n; break; | |
6392 case '+': ind_continuation = n; break; | |
6393 case '(': ind_unclosed = n; break; | |
6394 case 'u': ind_unclosed2 = n; break; | |
6395 case 'U': ind_unclosed_noignore = n; break; | |
6396 case 'W': ind_unclosed_wrapped = n; break; | |
6397 case 'w': ind_unclosed_whiteok = n; break; | |
6398 case 'm': ind_matching_paren = n; break; | |
829 | 6399 case 'M': ind_paren_prev = n; break; |
7 | 6400 case ')': ind_maxparen = n; break; |
6401 case '*': ind_maxcomment = n; break; | |
6402 case 'g': ind_scopedecl = n; break; | |
6403 case 'h': ind_scopedecl_code = n; break; | |
6404 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
|
6405 case 'J': ind_js = n; break; |
7 | 6406 case 'l': ind_keep_case_label = n; break; |
1096 | 6407 case '#': ind_hash_comment = n; break; |
7 | 6408 } |
2129
24100651daa9
updated for version 7.2.411
Bram Moolenaar <bram@zimbu.org>
parents:
2094
diff
changeset
|
6409 if (*options == ',') |
24100651daa9
updated for version 7.2.411
Bram Moolenaar <bram@zimbu.org>
parents:
2094
diff
changeset
|
6410 ++options; |
7 | 6411 } |
6412 | |
6413 /* remember where the cursor was when we started */ | |
6414 cur_curpos = curwin->w_cursor; | |
6415 | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6416 /* 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
|
6417 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
|
6418 return 0; |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6419 |
7 | 6420 /* Get a copy of the current contents of the line. |
6421 * This is required, because only the most recent line obtained with | |
6422 * ml_get is valid! */ | |
6423 linecopy = vim_strsave(ml_get(cur_curpos.lnum)); | |
6424 if (linecopy == NULL) | |
6425 return 0; | |
6426 | |
6427 /* | |
6428 * In insert mode and the cursor is on a ')' truncate the line at the | |
6429 * cursor position. We don't want to line up with the matching '(' when | |
6430 * inserting new stuff. | |
6431 * For unknown reasons the cursor might be past the end of the line, thus | |
6432 * check for that. | |
6433 */ | |
6434 if ((State & INSERT) | |
1883 | 6435 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) |
7 | 6436 && linecopy[curwin->w_cursor.col] == ')') |
6437 linecopy[curwin->w_cursor.col] = NUL; | |
6438 | |
6439 theline = skipwhite(linecopy); | |
6440 | |
6441 /* move the cursor to the start of the line */ | |
6442 | |
6443 curwin->w_cursor.col = 0; | |
6444 | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6445 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
|
6446 |
7 | 6447 /* |
6448 * #defines and so on always go at the left when included in 'cinkeys'. | |
6449 */ | |
6450 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) | |
6451 { | |
6452 amount = 0; | |
6453 } | |
6454 | |
6455 /* | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6456 * 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
|
6457 * - JS flag is set. |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6458 * - 'L' item has a positive value. |
7 | 6459 */ |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
6460 else if (original_line_islabel && !ind_js && ind_jump_label < 0) |
7 | 6461 { |
6462 amount = 0; | |
6463 } | |
6464 | |
6465 /* | |
6466 * If we're inside a "//" comment and there is a "//" comment in a | |
6467 * previous line, lineup with that one. | |
6468 */ | |
6469 else if (cin_islinecomment(theline) | |
6470 && (trypos = find_line_comment()) != NULL) /* XXX */ | |
6471 { | |
6472 /* find how indented the line beginning the comment is */ | |
6473 getvcol(curwin, trypos, &col, NULL, NULL); | |
6474 amount = col; | |
6475 } | |
6476 | |
6477 /* | |
6478 * If we're inside a comment and not looking at the start of the | |
6479 * comment, try using the 'comments' option. | |
6480 */ | |
6481 else if (!cin_iscomment(theline) | |
6482 && (trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */ | |
6483 { | |
6484 int lead_start_len = 2; | |
6485 int lead_middle_len = 1; | |
6486 char_u lead_start[COM_MAX_LEN]; /* start-comment string */ | |
6487 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */ | |
6488 char_u lead_end[COM_MAX_LEN]; /* end-comment string */ | |
6489 char_u *p; | |
6490 int start_align = 0; | |
6491 int start_off = 0; | |
6492 int done = FALSE; | |
6493 | |
6494 /* find how indented the line beginning the comment is */ | |
6495 getvcol(curwin, trypos, &col, NULL, NULL); | |
6496 amount = col; | |
2764 | 6497 *lead_start = NUL; |
6498 *lead_middle = NUL; | |
7 | 6499 |
6500 p = curbuf->b_p_com; | |
6501 while (*p != NUL) | |
6502 { | |
6503 int align = 0; | |
6504 int off = 0; | |
6505 int what = 0; | |
6506 | |
6507 while (*p != NUL && *p != ':') | |
6508 { | |
6509 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) | |
6510 what = *p++; | |
6511 else if (*p == COM_LEFT || *p == COM_RIGHT) | |
6512 align = *p++; | |
6513 else if (VIM_ISDIGIT(*p) || *p == '-') | |
6514 off = getdigits(&p); | |
6515 else | |
6516 ++p; | |
6517 } | |
6518 | |
6519 if (*p == ':') | |
6520 ++p; | |
6521 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
6522 if (what == COM_START) | |
6523 { | |
6524 STRCPY(lead_start, lead_end); | |
6525 lead_start_len = (int)STRLEN(lead_start); | |
6526 start_off = off; | |
6527 start_align = align; | |
6528 } | |
6529 else if (what == COM_MIDDLE) | |
6530 { | |
6531 STRCPY(lead_middle, lead_end); | |
6532 lead_middle_len = (int)STRLEN(lead_middle); | |
6533 } | |
6534 else if (what == COM_END) | |
6535 { | |
6536 /* If our line starts with the middle comment string, line it | |
6537 * up with the comment opener per the 'comments' option. */ | |
6538 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 | |
6539 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) | |
6540 { | |
6541 done = TRUE; | |
6542 if (curwin->w_cursor.lnum > 1) | |
6543 { | |
6544 /* If the start comment string matches in the previous | |
1532 | 6545 * line, use the indent of that line plus offset. If |
7 | 6546 * the middle comment string matches in the previous |
6547 * line, use the indent of that line. XXX */ | |
6548 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); | |
6549 if (STRNCMP(look, lead_start, lead_start_len) == 0) | |
6550 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
6551 else if (STRNCMP(look, lead_middle, | |
6552 lead_middle_len) == 0) | |
6553 { | |
6554 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
6555 break; | |
6556 } | |
6557 /* If the start comment string doesn't match with the | |
6558 * start of the comment, skip this entry. XXX */ | |
6559 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col, | |
6560 lead_start, lead_start_len) != 0) | |
6561 continue; | |
6562 } | |
6563 if (start_off != 0) | |
6564 amount += start_off; | |
6565 else if (start_align == COM_RIGHT) | |
17 | 6566 amount += vim_strsize(lead_start) |
6567 - vim_strsize(lead_middle); | |
7 | 6568 break; |
6569 } | |
6570 | |
6571 /* If our line starts with the end comment string, line it up | |
6572 * with the middle comment */ | |
6573 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 | |
6574 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) | |
6575 { | |
6576 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
6577 /* XXX */ | |
6578 if (off != 0) | |
6579 amount += off; | |
6580 else if (align == COM_RIGHT) | |
17 | 6581 amount += vim_strsize(lead_start) |
6582 - vim_strsize(lead_middle); | |
7 | 6583 done = TRUE; |
6584 break; | |
6585 } | |
6586 } | |
6587 } | |
6588 | |
6589 /* If our line starts with an asterisk, line up with the | |
6590 * asterisk in the comment opener; otherwise, line up | |
6591 * with the first character of the comment text. | |
6592 */ | |
6593 if (done) | |
6594 ; | |
6595 else if (theline[0] == '*') | |
6596 amount += 1; | |
6597 else | |
6598 { | |
6599 /* | |
6600 * If we are more than one line away from the comment opener, take | |
6601 * the indent of the previous non-empty line. If 'cino' has "CO" | |
6602 * and we are just below the comment opener and there are any | |
6603 * white characters after it line up with the text after it; | |
6604 * otherwise, add the amount specified by "c" in 'cino' | |
6605 */ | |
6606 amount = -1; | |
6607 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum) | |
6608 { | |
6609 if (linewhite(lnum)) /* skip blank lines */ | |
6610 continue; | |
6611 amount = get_indent_lnum(lnum); /* XXX */ | |
6612 break; | |
6613 } | |
6614 if (amount == -1) /* use the comment opener */ | |
6615 { | |
6616 if (!ind_in_comment2) | |
6617 { | |
6618 start = ml_get(trypos->lnum); | |
6619 look = start + trypos->col + 2; /* skip / and * */ | |
6620 if (*look != NUL) /* if something after it */ | |
6621 trypos->col = (colnr_T)(skipwhite(look) - start); | |
6622 } | |
6623 getvcol(curwin, trypos, &col, NULL, NULL); | |
6624 amount = col; | |
6625 if (ind_in_comment2 || *look == NUL) | |
6626 amount += ind_in_comment; | |
6627 } | |
6628 } | |
6629 } | |
6630 | |
6631 /* | |
6632 * Are we inside parentheses or braces? | |
6633 */ /* XXX */ | |
6634 else if (((trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL | |
6635 && ind_java == 0) | |
6636 || (tryposBrace = find_start_brace(ind_maxcomment)) != NULL | |
6637 || trypos != NULL) | |
6638 { | |
6639 if (trypos != NULL && tryposBrace != NULL) | |
6640 { | |
6641 /* Both an unmatched '(' and '{' is found. Use the one which is | |
6642 * closer to the current cursor position, set the other to NULL. */ | |
6643 if (trypos->lnum != tryposBrace->lnum | |
6644 ? trypos->lnum < tryposBrace->lnum | |
6645 : trypos->col < tryposBrace->col) | |
6646 trypos = NULL; | |
6647 else | |
6648 tryposBrace = NULL; | |
6649 } | |
6650 | |
6651 if (trypos != NULL) | |
6652 { | |
6653 /* | |
6654 * If the matching paren is more than one line away, use the indent of | |
6655 * a previous non-empty line that matches the same paren. | |
6656 */ | |
829 | 6657 if (theline[0] == ')' && ind_paren_prev) |
6658 { | |
6659 /* Line up with the start of the matching paren line. */ | |
6660 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */ | |
6661 } | |
6662 else | |
6663 { | |
6664 amount = -1; | |
6665 our_paren_pos = *trypos; | |
6666 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) | |
7 | 6667 { |
829 | 6668 l = skipwhite(ml_get(lnum)); |
6669 if (cin_nocode(l)) /* skip comment lines */ | |
6670 continue; | |
6671 if (cin_ispreproc_cont(&l, &lnum)) | |
6672 continue; /* ignore #define, #if, etc. */ | |
6673 curwin->w_cursor.lnum = lnum; | |
6674 | |
6675 /* Skip a comment. XXX */ | |
6676 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) | |
6677 { | |
6678 lnum = trypos->lnum + 1; | |
6679 continue; | |
6680 } | |
6681 | |
6682 /* XXX */ | |
6683 if ((trypos = find_match_paren( | |
6684 corr_ind_maxparen(ind_maxparen, &cur_curpos), | |
7 | 6685 ind_maxcomment)) != NULL |
829 | 6686 && trypos->lnum == our_paren_pos.lnum |
6687 && trypos->col == our_paren_pos.col) | |
6688 { | |
6689 amount = get_indent_lnum(lnum); /* XXX */ | |
6690 | |
6691 if (theline[0] == ')') | |
6692 { | |
6693 if (our_paren_pos.lnum != lnum | |
6694 && cur_amount > amount) | |
6695 cur_amount = amount; | |
6696 amount = -1; | |
6697 } | |
6698 break; | |
6699 } | |
7 | 6700 } |
6701 } | |
6702 | |
6703 /* | |
6704 * Line up with line where the matching paren is. XXX | |
6705 * If the line starts with a '(' or the indent for unclosed | |
6706 * parentheses is zero, line up with the unclosed parentheses. | |
6707 */ | |
6708 if (amount == -1) | |
6709 { | |
829 | 6710 int ignore_paren_col = 0; |
6711 | |
7 | 6712 amount = skip_label(our_paren_pos.lnum, &look, ind_maxcomment); |
829 | 6713 look = skipwhite(look); |
6714 if (*look == '(') | |
6715 { | |
6716 linenr_T save_lnum = curwin->w_cursor.lnum; | |
6717 char_u *line; | |
6718 int look_col; | |
6719 | |
6720 /* Ignore a '(' in front of the line that has a match before | |
6721 * our matching '('. */ | |
6722 curwin->w_cursor.lnum = our_paren_pos.lnum; | |
6723 line = ml_get_curline(); | |
835 | 6724 look_col = (int)(look - line); |
829 | 6725 curwin->w_cursor.col = look_col + 1; |
6726 if ((trypos = findmatchlimit(NULL, ')', 0, ind_maxparen)) | |
6727 != NULL | |
6728 && trypos->lnum == our_paren_pos.lnum | |
6729 && trypos->col < our_paren_pos.col) | |
6730 ignore_paren_col = trypos->col + 1; | |
6731 | |
6732 curwin->w_cursor.lnum = save_lnum; | |
6733 look = ml_get(our_paren_pos.lnum) + look_col; | |
6734 } | |
7 | 6735 if (theline[0] == ')' || ind_unclosed == 0 |
829 | 6736 || (!ind_unclosed_noignore && *look == '(' |
6737 && ignore_paren_col == 0)) | |
7 | 6738 { |
6739 /* | |
6740 * If we're looking at a close paren, line up right there; | |
6741 * otherwise, line up with the next (non-white) character. | |
6742 * When ind_unclosed_wrapped is set and the matching paren is | |
6743 * the last nonwhite character of the line, use either the | |
6744 * indent of the current line or the indentation of the next | |
6745 * outer paren and add ind_unclosed_wrapped (for very long | |
6746 * lines). | |
6747 */ | |
6748 if (theline[0] != ')') | |
6749 { | |
6750 cur_amount = MAXCOL; | |
6751 l = ml_get(our_paren_pos.lnum); | |
6752 if (ind_unclosed_wrapped | |
6753 && cin_ends_in(l, (char_u *)"(", NULL)) | |
6754 { | |
6755 /* look for opening unmatched paren, indent one level | |
6756 * for each additional level */ | |
6757 n = 1; | |
6758 for (col = 0; col < our_paren_pos.col; ++col) | |
6759 { | |
6760 switch (l[col]) | |
6761 { | |
6762 case '(': | |
6763 case '{': ++n; | |
6764 break; | |
6765 | |
6766 case ')': | |
6767 case '}': if (n > 1) | |
6768 --n; | |
6769 break; | |
6770 } | |
6771 } | |
6772 | |
6773 our_paren_pos.col = 0; | |
6774 amount += n * ind_unclosed_wrapped; | |
6775 } | |
6776 else if (ind_unclosed_whiteok) | |
6777 our_paren_pos.col++; | |
6778 else | |
6779 { | |
6780 col = our_paren_pos.col + 1; | |
6781 while (vim_iswhite(l[col])) | |
6782 col++; | |
6783 if (l[col] != NUL) /* In case of trailing space */ | |
6784 our_paren_pos.col = col; | |
6785 else | |
6786 our_paren_pos.col++; | |
6787 } | |
6788 } | |
6789 | |
6790 /* | |
6791 * Find how indented the paren is, or the character after it | |
6792 * if we did the above "if". | |
6793 */ | |
6794 if (our_paren_pos.col > 0) | |
6795 { | |
6796 getvcol(curwin, &our_paren_pos, &col, NULL, NULL); | |
6797 if (cur_amount > (int)col) | |
6798 cur_amount = col; | |
6799 } | |
6800 } | |
6801 | |
6802 if (theline[0] == ')' && ind_matching_paren) | |
6803 { | |
6804 /* Line up with the start of the matching paren line. */ | |
6805 } | |
6806 else if (ind_unclosed == 0 || (!ind_unclosed_noignore | |
829 | 6807 && *look == '(' && ignore_paren_col == 0)) |
7 | 6808 { |
6809 if (cur_amount != MAXCOL) | |
6810 amount = cur_amount; | |
6811 } | |
6812 else | |
6813 { | |
829 | 6814 /* Add ind_unclosed2 for each '(' before our matching one, but |
6815 * ignore (void) before the line (ignore_paren_col). */ | |
7 | 6816 col = our_paren_pos.col; |
834 | 6817 while ((int)our_paren_pos.col > ignore_paren_col) |
7 | 6818 { |
6819 --our_paren_pos.col; | |
6820 switch (*ml_get_pos(&our_paren_pos)) | |
6821 { | |
6822 case '(': amount += ind_unclosed2; | |
6823 col = our_paren_pos.col; | |
6824 break; | |
6825 case ')': amount -= ind_unclosed2; | |
6826 col = MAXCOL; | |
6827 break; | |
6828 } | |
6829 } | |
6830 | |
6831 /* Use ind_unclosed once, when the first '(' is not inside | |
6832 * braces */ | |
6833 if (col == MAXCOL) | |
6834 amount += ind_unclosed; | |
6835 else | |
6836 { | |
6837 curwin->w_cursor.lnum = our_paren_pos.lnum; | |
6838 curwin->w_cursor.col = col; | |
2762 | 6839 if (find_match_paren(ind_maxparen, ind_maxcomment) != NULL) |
7 | 6840 amount += ind_unclosed2; |
6841 else | |
6842 amount += ind_unclosed; | |
6843 } | |
6844 /* | |
6845 * For a line starting with ')' use the minimum of the two | |
6846 * positions, to avoid giving it more indent than the previous | |
6847 * lines: | |
6848 * func_long_name( if (x | |
6849 * arg && yy | |
6850 * ) ^ not here ) ^ not here | |
6851 */ | |
6852 if (cur_amount < amount) | |
6853 amount = cur_amount; | |
6854 } | |
6855 } | |
6856 | |
6857 /* add extra indent for a comment */ | |
6858 if (cin_iscomment(theline)) | |
6859 amount += ind_comment; | |
6860 } | |
6861 | |
6862 /* | |
6863 * Are we at least inside braces, then? | |
6864 */ | |
6865 else | |
6866 { | |
6867 trypos = tryposBrace; | |
6868 | |
6869 ourscope = trypos->lnum; | |
6870 start = ml_get(ourscope); | |
6871 | |
6872 /* | |
6873 * Now figure out how indented the line is in general. | |
6874 * If the brace was at the start of the line, we use that; | |
6875 * otherwise, check out the indentation of the line as | |
6876 * a whole and then add the "imaginary indent" to that. | |
6877 */ | |
6878 look = skipwhite(start); | |
6879 if (*look == '{') | |
6880 { | |
6881 getvcol(curwin, trypos, &col, NULL, NULL); | |
6882 amount = col; | |
6883 if (*start == '{') | |
6884 start_brace = BRACE_IN_COL0; | |
6885 else | |
6886 start_brace = BRACE_AT_START; | |
6887 } | |
6888 else | |
6889 { | |
6890 /* | |
6891 * that opening brace might have been on a continuation | |
6892 * line. if so, find the start of the line. | |
6893 */ | |
6894 curwin->w_cursor.lnum = ourscope; | |
6895 | |
6896 /* | |
6897 * position the cursor over the rightmost paren, so that | |
6898 * matching it will take us back to the start of the line. | |
6899 */ | |
6900 lnum = ourscope; | |
6901 if (find_last_paren(start, '(', ')') | |
6902 && (trypos = find_match_paren(ind_maxparen, | |
6903 ind_maxcomment)) != NULL) | |
6904 lnum = trypos->lnum; | |
6905 | |
6906 /* | |
6907 * It could have been something like | |
6908 * case 1: if (asdf && | |
6909 * ldfd) { | |
6910 * } | |
6911 */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6912 if ((ind_keep_case_label |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6913 && cin_iscase(skipwhite(ml_get_curline()), FALSE))) |
7 | 6914 amount = get_indent(); |
6915 else | |
6916 amount = skip_label(lnum, &l, ind_maxcomment); | |
6917 | |
6918 start_brace = BRACE_AT_END; | |
6919 } | |
6920 | |
6921 /* | |
6922 * if we're looking at a closing brace, that's where | |
6923 * we want to be. otherwise, add the amount of room | |
6924 * that an indent is supposed to be. | |
6925 */ | |
6926 if (theline[0] == '}') | |
6927 { | |
6928 /* | |
6929 * they may want closing braces to line up with something | |
6930 * other than the open brace. indulge them, if so. | |
6931 */ | |
6932 amount += ind_close_extra; | |
6933 } | |
6934 else | |
6935 { | |
6936 /* | |
6937 * If we're looking at an "else", try to find an "if" | |
6938 * to match it with. | |
6939 * If we're looking at a "while", try to find a "do" | |
6940 * to match it with. | |
6941 */ | |
6942 lookfor = LOOKFOR_INITIAL; | |
6943 if (cin_iselse(theline)) | |
6944 lookfor = LOOKFOR_IF; | |
6945 else if (cin_iswhileofdo(theline, cur_curpos.lnum, ind_maxparen)) | |
6946 /* XXX */ | |
6947 lookfor = LOOKFOR_DO; | |
6948 if (lookfor != LOOKFOR_INITIAL) | |
6949 { | |
6950 curwin->w_cursor.lnum = cur_curpos.lnum; | |
6951 if (find_match(lookfor, ourscope, ind_maxparen, | |
6952 ind_maxcomment) == OK) | |
6953 { | |
6954 amount = get_indent(); /* XXX */ | |
6955 goto theend; | |
6956 } | |
6957 } | |
6958 | |
6959 /* | |
6960 * We get here if we are not on an "while-of-do" or "else" (or | |
6961 * failed to find a matching "if"). | |
6962 * Search backwards for something to line up with. | |
6963 * First set amount for when we don't find anything. | |
6964 */ | |
6965 | |
6966 /* | |
6967 * if the '{' is _really_ at the left margin, use the imaginary | |
6968 * location of a left-margin brace. Otherwise, correct the | |
6969 * location for ind_open_extra. | |
6970 */ | |
6971 | |
6972 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */ | |
6973 { | |
6974 amount = ind_open_left_imag; | |
6975 } | |
6976 else | |
6977 { | |
6978 if (start_brace == BRACE_AT_END) /* '{' is at end of line */ | |
6979 amount += ind_open_imag; | |
6980 else | |
6981 { | |
6982 /* Compensate for adding ind_open_extra later. */ | |
6983 amount -= ind_open_extra; | |
6984 if (amount < 0) | |
6985 amount = 0; | |
6986 } | |
6987 } | |
6988 | |
6989 lookfor_break = FALSE; | |
6990 | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
6991 if (cin_iscase(theline, FALSE)) /* it's a switch() label */ |
7 | 6992 { |
6993 lookfor = LOOKFOR_CASE; /* find a previous switch() label */ | |
6994 amount += ind_case; | |
6995 } | |
6996 else if (cin_isscopedecl(theline)) /* private:, ... */ | |
6997 { | |
6998 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */ | |
6999 amount += ind_scopedecl; | |
7000 } | |
7001 else | |
7002 { | |
7003 if (ind_case_break && cin_isbreak(theline)) /* break; ... */ | |
7004 lookfor_break = TRUE; | |
7005 | |
7006 lookfor = LOOKFOR_INITIAL; | |
7007 amount += ind_level; /* ind_level from start of block */ | |
7008 } | |
7009 scope_amount = amount; | |
7010 whilelevel = 0; | |
7011 | |
7012 /* | |
7013 * Search backwards. If we find something we recognize, line up | |
7014 * with that. | |
7015 * | |
7016 * if we're looking at an open brace, indent | |
7017 * the usual amount relative to the conditional | |
7018 * that opens the block. | |
7019 */ | |
7020 curwin->w_cursor = cur_curpos; | |
7021 for (;;) | |
7022 { | |
7023 curwin->w_cursor.lnum--; | |
7024 curwin->w_cursor.col = 0; | |
7025 | |
7026 /* | |
7027 * If we went all the way back to the start of our scope, line | |
7028 * up with it. | |
7029 */ | |
7030 if (curwin->w_cursor.lnum <= ourscope) | |
7031 { | |
7032 /* we reached end of scope: | |
7033 * if looking for a enum or structure initialization | |
7034 * go further back: | |
7035 * if it is an initializer (enum xxx or xxx =), then | |
7036 * don't add ind_continuation, otherwise it is a variable | |
7037 * declaration: | |
7038 * int x, | |
7039 * here; <-- add ind_continuation | |
7040 */ | |
7041 if (lookfor == LOOKFOR_ENUM_OR_INIT) | |
7042 { | |
7043 if (curwin->w_cursor.lnum == 0 | |
7044 || curwin->w_cursor.lnum | |
7045 < ourscope - ind_maxparen) | |
7046 { | |
7047 /* nothing found (abuse ind_maxparen as limit) | |
7048 * assume terminated line (i.e. a variable | |
7049 * initialization) */ | |
7050 if (cont_amount > 0) | |
7051 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
|
7052 else if (!ind_js) |
7 | 7053 amount += ind_continuation; |
7054 break; | |
7055 } | |
7056 | |
7057 l = ml_get_curline(); | |
7058 | |
7059 /* | |
7060 * If we're in a comment now, skip to the start of the | |
7061 * comment. | |
7062 */ | |
7063 trypos = find_start_comment(ind_maxcomment); | |
7064 if (trypos != NULL) | |
7065 { | |
7066 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7067 curwin->w_cursor.col = 0; |
7 | 7068 continue; |
7069 } | |
7070 | |
7071 /* | |
7072 * Skip preprocessor directives and blank lines. | |
7073 */ | |
7074 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) | |
7075 continue; | |
7076 | |
7077 if (cin_nocode(l)) | |
7078 continue; | |
7079 | |
7080 terminated = cin_isterminated(l, FALSE, TRUE); | |
7081 | |
7082 /* | |
7083 * If we are at top level and the line looks like a | |
7084 * function declaration, we are done | |
7085 * (it's a variable declaration). | |
7086 */ | |
7087 if (start_brace != BRACE_IN_COL0 | |
7088 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum)) | |
7089 { | |
7090 /* if the line is terminated with another ',' | |
7091 * it is a continued variable initialization. | |
7092 * don't add extra indent. | |
7093 * TODO: does not work, if a function | |
7094 * declaration is split over multiple lines: | |
7095 * cin_isfuncdecl returns FALSE then. | |
7096 */ | |
7097 if (terminated == ',') | |
7098 break; | |
7099 | |
7100 /* if it es a enum declaration or an assignment, | |
7101 * we are done. | |
7102 */ | |
7103 if (terminated != ';' && cin_isinit()) | |
7104 break; | |
7105 | |
7106 /* nothing useful found */ | |
7107 if (terminated == 0 || terminated == '{') | |
7108 continue; | |
7109 } | |
7110 | |
7111 if (terminated != ';') | |
7112 { | |
7113 /* Skip parens and braces. Position the cursor | |
7114 * over the rightmost paren, so that matching it | |
7115 * will take us back to the start of the line. | |
7116 */ /* XXX */ | |
7117 trypos = NULL; | |
7118 if (find_last_paren(l, '(', ')')) | |
7119 trypos = find_match_paren(ind_maxparen, | |
7120 ind_maxcomment); | |
7121 | |
7122 if (trypos == NULL && find_last_paren(l, '{', '}')) | |
7123 trypos = find_start_brace(ind_maxcomment); | |
7124 | |
7125 if (trypos != NULL) | |
7126 { | |
7127 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7128 curwin->w_cursor.col = 0; |
7 | 7129 continue; |
7130 } | |
7131 } | |
7132 | |
7133 /* it's a variable declaration, add indentation | |
7134 * like in | |
7135 * int a, | |
7136 * b; | |
7137 */ | |
7138 if (cont_amount > 0) | |
7139 amount = cont_amount; | |
7140 else | |
7141 amount += ind_continuation; | |
7142 } | |
7143 else if (lookfor == LOOKFOR_UNTERM) | |
7144 { | |
7145 if (cont_amount > 0) | |
7146 amount = cont_amount; | |
7147 else | |
7148 amount += ind_continuation; | |
7149 } | |
7150 else if (lookfor != LOOKFOR_TERM | |
7151 && lookfor != LOOKFOR_CPP_BASECLASS) | |
7152 { | |
7153 amount = scope_amount; | |
7154 if (theline[0] == '{') | |
7155 amount += ind_open_extra; | |
7156 } | |
7157 break; | |
7158 } | |
7159 | |
7160 /* | |
7161 * If we're in a comment now, skip to the start of the comment. | |
7162 */ /* XXX */ | |
7163 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) | |
7164 { | |
7165 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7166 curwin->w_cursor.col = 0; |
7 | 7167 continue; |
7168 } | |
7169 | |
7170 l = ml_get_curline(); | |
7171 | |
7172 /* | |
7173 * If this is a switch() label, may line up relative to that. | |
827 | 7174 * If this is a C++ scope declaration, do the same. |
7 | 7175 */ |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7176 iscase = cin_iscase(l, FALSE); |
7 | 7177 if (iscase || cin_isscopedecl(l)) |
7178 { | |
7179 /* we are only looking for cpp base class | |
7180 * declaration/initialization any longer */ | |
7181 if (lookfor == LOOKFOR_CPP_BASECLASS) | |
7182 break; | |
7183 | |
7184 /* When looking for a "do" we are not interested in | |
7185 * labels. */ | |
7186 if (whilelevel > 0) | |
7187 continue; | |
7188 | |
7189 /* | |
7190 * case xx: | |
7191 * c = 99 + <- this indent plus continuation | |
7192 *-> here; | |
7193 */ | |
7194 if (lookfor == LOOKFOR_UNTERM | |
7195 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
7196 { | |
7197 if (cont_amount > 0) | |
7198 amount = cont_amount; | |
7199 else | |
7200 amount += ind_continuation; | |
7201 break; | |
7202 } | |
7203 | |
7204 /* | |
7205 * case xx: <- line up with this case | |
7206 * x = 333; | |
7207 * case yy: | |
7208 */ | |
7209 if ( (iscase && lookfor == LOOKFOR_CASE) | |
7210 || (iscase && lookfor_break) | |
7211 || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) | |
7212 { | |
7213 /* | |
7214 * Check that this case label is not for another | |
7215 * switch() | |
7216 */ /* XXX */ | |
7217 if ((trypos = find_start_brace(ind_maxcomment)) == | |
7218 NULL || trypos->lnum == ourscope) | |
7219 { | |
7220 amount = get_indent(); /* XXX */ | |
7221 break; | |
7222 } | |
7223 continue; | |
7224 } | |
7225 | |
7226 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */ | |
7227 | |
7228 /* | |
7229 * case xx: if (cond) <- line up with this if | |
7230 * y = y + 1; | |
7231 * -> s = 99; | |
7232 * | |
7233 * case xx: | |
7234 * if (cond) <- line up with this line | |
7235 * y = y + 1; | |
7236 * -> s = 99; | |
7237 */ | |
7238 if (lookfor == LOOKFOR_TERM) | |
7239 { | |
7240 if (n) | |
7241 amount = n; | |
7242 | |
7243 if (!lookfor_break) | |
7244 break; | |
7245 } | |
7246 | |
7247 /* | |
7248 * case xx: x = x + 1; <- line up with this x | |
7249 * -> y = y + 1; | |
7250 * | |
7251 * case xx: if (cond) <- line up with this if | |
7252 * -> y = y + 1; | |
7253 */ | |
7254 if (n) | |
7255 { | |
7256 amount = n; | |
7257 l = after_label(ml_get_curline()); | |
7258 if (l != NULL && cin_is_cinword(l)) | |
829 | 7259 { |
7260 if (theline[0] == '{') | |
7261 amount += ind_open_extra; | |
7262 else | |
7263 amount += ind_level + ind_no_brace; | |
7264 } | |
7 | 7265 break; |
7266 } | |
7267 | |
7268 /* | |
7269 * Try to get the indent of a statement before the switch | |
7270 * label. If nothing is found, line up relative to the | |
7271 * switch label. | |
7272 * break; <- may line up with this line | |
7273 * case xx: | |
7274 * -> y = 1; | |
7275 */ | |
7276 scope_amount = get_indent() + (iscase /* XXX */ | |
7277 ? ind_case_code : ind_scopedecl_code); | |
7278 lookfor = ind_case_break ? LOOKFOR_NOBREAK : LOOKFOR_ANY; | |
7279 continue; | |
7280 } | |
7281 | |
7282 /* | |
7283 * Looking for a switch() label or C++ scope declaration, | |
7284 * ignore other lines, skip {}-blocks. | |
7285 */ | |
7286 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) | |
7287 { | |
7288 if (find_last_paren(l, '{', '}') && (trypos = | |
7289 find_start_brace(ind_maxcomment)) != NULL) | |
1550 | 7290 { |
7 | 7291 curwin->w_cursor.lnum = trypos->lnum + 1; |
1550 | 7292 curwin->w_cursor.col = 0; |
7293 } | |
7 | 7294 continue; |
7295 } | |
7296 | |
7297 /* | |
7298 * Ignore jump labels with nothing after them. | |
7299 */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7300 if (!ind_js && cin_islabel(ind_maxcomment)) |
7 | 7301 { |
7302 l = after_label(ml_get_curline()); | |
7303 if (l == NULL || cin_nocode(l)) | |
7304 continue; | |
7305 } | |
7306 | |
7307 /* | |
7308 * Ignore #defines, #if, etc. | |
7309 * Ignore comment and empty lines. | |
7310 * (need to get the line again, cin_islabel() may have | |
7311 * unlocked it) | |
7312 */ | |
7313 l = ml_get_curline(); | |
7314 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum) | |
7315 || cin_nocode(l)) | |
7316 continue; | |
7317 | |
7318 /* | |
7319 * Are we at the start of a cpp base class declaration or | |
7320 * constructor initialization? | |
7321 */ /* XXX */ | |
827 | 7322 n = FALSE; |
7323 if (lookfor != LOOKFOR_TERM && ind_cpp_baseclass > 0) | |
7324 { | |
1336 | 7325 n = cin_is_cpp_baseclass(&col); |
827 | 7326 l = ml_get_curline(); |
7327 } | |
7328 if (n) | |
7 | 7329 { |
7330 if (lookfor == LOOKFOR_UNTERM) | |
7331 { | |
7332 if (cont_amount > 0) | |
7333 amount = cont_amount; | |
7334 else | |
7335 amount += ind_continuation; | |
7336 } | |
828 | 7337 else if (theline[0] == '{') |
7 | 7338 { |
828 | 7339 /* Need to find start of the declaration. */ |
7340 lookfor = LOOKFOR_UNTERM; | |
7341 ind_continuation = 0; | |
7342 continue; | |
7 | 7343 } |
7344 else | |
828 | 7345 /* XXX */ |
7346 amount = get_baseclass_amount(col, ind_maxparen, | |
7347 ind_maxcomment, ind_cpp_baseclass); | |
7 | 7348 break; |
7349 } | |
7350 else if (lookfor == LOOKFOR_CPP_BASECLASS) | |
7351 { | |
7352 /* only look, whether there is a cpp base class | |
827 | 7353 * declaration or initialization before the opening brace. |
7354 */ | |
7 | 7355 if (cin_isterminated(l, TRUE, FALSE)) |
7356 break; | |
7357 else | |
7358 continue; | |
7359 } | |
7360 | |
7361 /* | |
7362 * What happens next depends on the line being terminated. | |
7363 * If terminated with a ',' only consider it terminating if | |
1224 | 7364 * there is another unterminated statement behind, eg: |
7 | 7365 * 123, |
7366 * sizeof | |
7367 * here | |
7368 * Otherwise check whether it is a enumeration or structure | |
7369 * initialisation (not indented) or a variable declaration | |
7370 * (indented). | |
7371 */ | |
7372 terminated = cin_isterminated(l, FALSE, TRUE); | |
7373 | |
7374 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM | |
7375 && terminated == ',')) | |
7376 { | |
7377 /* | |
7378 * if we're in the middle of a paren thing, | |
7379 * go back to the line that starts it so | |
7380 * we can get the right prevailing indent | |
7381 * if ( foo && | |
7382 * bar ) | |
7383 */ | |
7384 /* | |
7385 * position the cursor over the rightmost paren, so that | |
7386 * matching it will take us back to the start of the line. | |
7387 */ | |
7388 (void)find_last_paren(l, '(', ')'); | |
7389 trypos = find_match_paren( | |
7390 corr_ind_maxparen(ind_maxparen, &cur_curpos), | |
7391 ind_maxcomment); | |
7392 | |
7393 /* | |
7394 * If we are looking for ',', we also look for matching | |
7395 * braces. | |
7396 */ | |
828 | 7397 if (trypos == NULL && terminated == ',' |
7398 && find_last_paren(l, '{', '}')) | |
7 | 7399 trypos = find_start_brace(ind_maxcomment); |
7400 | |
7401 if (trypos != NULL) | |
7402 { | |
7403 /* | |
7404 * Check if we are on a case label now. This is | |
7405 * handled above. | |
7406 * case xx: if ( asdf && | |
7407 * asdf) | |
7408 */ | |
1550 | 7409 curwin->w_cursor = *trypos; |
7 | 7410 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
|
7411 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
7 | 7412 { |
7413 ++curwin->w_cursor.lnum; | |
1550 | 7414 curwin->w_cursor.col = 0; |
7 | 7415 continue; |
7416 } | |
7417 } | |
7418 | |
7419 /* | |
7420 * Skip over continuation lines to find the one to get the | |
7421 * indent from | |
7422 * char *usethis = "bla\ | |
7423 * bla", | |
7424 * here; | |
7425 */ | |
7426 if (terminated == ',') | |
7427 { | |
7428 while (curwin->w_cursor.lnum > 1) | |
7429 { | |
7430 l = ml_get(curwin->w_cursor.lnum - 1); | |
7431 if (*l == NUL || l[STRLEN(l) - 1] != '\\') | |
7432 break; | |
7433 --curwin->w_cursor.lnum; | |
1550 | 7434 curwin->w_cursor.col = 0; |
7 | 7435 } |
7436 } | |
7437 | |
7438 /* | |
7439 * Get indent and pointer to text for current line, | |
7440 * ignoring any jump label. XXX | |
7441 */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7442 if (!ind_js) |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7443 cur_amount = skip_label(curwin->w_cursor.lnum, |
7 | 7444 &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
|
7445 else |
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7446 cur_amount = get_indent(); |
7 | 7447 /* |
7448 * If this is just above the line we are indenting, and it | |
7449 * starts with a '{', line it up with this line. | |
7450 * while (not) | |
7451 * -> { | |
7452 * } | |
7453 */ | |
7454 if (terminated != ',' && lookfor != LOOKFOR_TERM | |
7455 && theline[0] == '{') | |
7456 { | |
7457 amount = cur_amount; | |
7458 /* | |
7459 * Only add ind_open_extra when the current line | |
7460 * doesn't start with a '{', which must have a match | |
7461 * in the same line (scope is the same). Probably: | |
7462 * { 1, 2 }, | |
7463 * -> { 3, 4 } | |
7464 */ | |
7465 if (*skipwhite(l) != '{') | |
7466 amount += ind_open_extra; | |
7467 | |
7468 if (ind_cpp_baseclass) | |
7469 { | |
7470 /* have to look back, whether it is a cpp base | |
7471 * class declaration or initialization */ | |
7472 lookfor = LOOKFOR_CPP_BASECLASS; | |
7473 continue; | |
7474 } | |
7475 break; | |
7476 } | |
7477 | |
7478 /* | |
7479 * Check if we are after an "if", "while", etc. | |
7480 * Also allow " } else". | |
7481 */ | |
7482 if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) | |
7483 { | |
7484 /* | |
7485 * Found an unterminated line after an if (), line up | |
7486 * with the last one. | |
7487 * if (cond) | |
7488 * 100 + | |
7489 * -> here; | |
7490 */ | |
7491 if (lookfor == LOOKFOR_UNTERM | |
7492 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
7493 { | |
7494 if (cont_amount > 0) | |
7495 amount = cont_amount; | |
7496 else | |
7497 amount += ind_continuation; | |
7498 break; | |
7499 } | |
7500 | |
7501 /* | |
7502 * If this is just above the line we are indenting, we | |
7503 * are finished. | |
7504 * while (not) | |
7505 * -> here; | |
7506 * Otherwise this indent can be used when the line | |
7507 * before this is terminated. | |
7508 * yyy; | |
7509 * if (stat) | |
7510 * while (not) | |
7511 * xxx; | |
7512 * -> here; | |
7513 */ | |
7514 amount = cur_amount; | |
7515 if (theline[0] == '{') | |
7516 amount += ind_open_extra; | |
7517 if (lookfor != LOOKFOR_TERM) | |
7518 { | |
7519 amount += ind_level + ind_no_brace; | |
7520 break; | |
7521 } | |
7522 | |
7523 /* | |
7524 * Special trick: when expecting the while () after a | |
7525 * do, line up with the while() | |
7526 * do | |
7527 * x = 1; | |
7528 * -> here | |
7529 */ | |
7530 l = skipwhite(ml_get_curline()); | |
7531 if (cin_isdo(l)) | |
7532 { | |
7533 if (whilelevel == 0) | |
7534 break; | |
7535 --whilelevel; | |
7536 } | |
7537 | |
7538 /* | |
7539 * When searching for a terminated line, don't use the | |
7540 * one between the "if" and the "else". | |
7541 * Need to use the scope of this "else". XXX | |
7542 * If whilelevel != 0 continue looking for a "do {". | |
7543 */ | |
7544 if (cin_iselse(l) | |
7545 && whilelevel == 0 | |
7546 && ((trypos = find_start_brace(ind_maxcomment)) | |
7547 == NULL | |
7548 || find_match(LOOKFOR_IF, trypos->lnum, | |
7549 ind_maxparen, ind_maxcomment) == FAIL)) | |
7550 break; | |
7551 } | |
7552 | |
7553 /* | |
7554 * If we're below an unterminated line that is not an | |
7555 * "if" or something, we may line up with this line or | |
1224 | 7556 * add something for a continuation line, depending on |
7 | 7557 * the line before this one. |
7558 */ | |
7559 else | |
7560 { | |
7561 /* | |
7562 * Found two unterminated lines on a row, line up with | |
7563 * the last one. | |
7564 * c = 99 + | |
7565 * 100 + | |
7566 * -> here; | |
7567 */ | |
7568 if (lookfor == LOOKFOR_UNTERM) | |
7569 { | |
7570 /* When line ends in a comma add extra indent */ | |
7571 if (terminated == ',') | |
7572 amount += ind_continuation; | |
7573 break; | |
7574 } | |
7575 | |
7576 if (lookfor == LOOKFOR_ENUM_OR_INIT) | |
7577 { | |
7578 /* Found two lines ending in ',', lineup with the | |
7579 * lowest one, but check for cpp base class | |
7580 * declaration/initialization, if it is an | |
7581 * opening brace or we are looking just for | |
7582 * enumerations/initializations. */ | |
7583 if (terminated == ',') | |
7584 { | |
7585 if (ind_cpp_baseclass == 0) | |
7586 break; | |
7587 | |
7588 lookfor = LOOKFOR_CPP_BASECLASS; | |
7589 continue; | |
7590 } | |
7591 | |
7592 /* Ignore unterminated lines in between, but | |
7593 * reduce indent. */ | |
7594 if (amount > cur_amount) | |
7595 amount = cur_amount; | |
7596 } | |
7597 else | |
7598 { | |
7599 /* | |
7600 * Found first unterminated line on a row, may | |
7601 * line up with this line, remember its indent | |
7602 * 100 + | |
7603 * -> here; | |
7604 */ | |
7605 amount = cur_amount; | |
7606 | |
7607 /* | |
7608 * If previous line ends in ',', check whether we | |
7609 * are in an initialization or enum | |
7610 * struct xxx = | |
7611 * { | |
7612 * sizeof a, | |
7613 * 124 }; | |
7614 * or a normal possible continuation line. | |
7615 * but only, of no other statement has been found | |
7616 * yet. | |
7617 */ | |
7618 if (lookfor == LOOKFOR_INITIAL && terminated == ',') | |
7619 { | |
7620 lookfor = LOOKFOR_ENUM_OR_INIT; | |
7621 cont_amount = cin_first_id_amount(); | |
7622 } | |
7623 else | |
7624 { | |
7625 if (lookfor == LOOKFOR_INITIAL | |
7626 && *l != NUL | |
7627 && l[STRLEN(l) - 1] == '\\') | |
7628 /* XXX */ | |
7629 cont_amount = cin_get_equal_amount( | |
7630 curwin->w_cursor.lnum); | |
7631 if (lookfor != LOOKFOR_TERM) | |
7632 lookfor = LOOKFOR_UNTERM; | |
7633 } | |
7634 } | |
7635 } | |
7636 } | |
7637 | |
7638 /* | |
7639 * Check if we are after a while (cond); | |
7640 * If so: Ignore until the matching "do". | |
7641 */ | |
7642 /* XXX */ | |
829 | 7643 else if (cin_iswhileofdo_end(terminated, ind_maxparen, |
7644 ind_maxcomment)) | |
7 | 7645 { |
7646 /* | |
7647 * Found an unterminated line after a while ();, line up | |
7648 * with the last one. | |
7649 * while (cond); | |
7650 * 100 + <- line up with this one | |
7651 * -> here; | |
7652 */ | |
7653 if (lookfor == LOOKFOR_UNTERM | |
7654 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
7655 { | |
7656 if (cont_amount > 0) | |
7657 amount = cont_amount; | |
7658 else | |
7659 amount += ind_continuation; | |
7660 break; | |
7661 } | |
7662 | |
7663 if (whilelevel == 0) | |
7664 { | |
7665 lookfor = LOOKFOR_TERM; | |
7666 amount = get_indent(); /* XXX */ | |
7667 if (theline[0] == '{') | |
7668 amount += ind_open_extra; | |
7669 } | |
7670 ++whilelevel; | |
7671 } | |
7672 | |
7673 /* | |
7674 * We are after a "normal" statement. | |
7675 * If we had another statement we can stop now and use the | |
7676 * indent of that other statement. | |
7677 * Otherwise the indent of the current statement may be used, | |
7678 * search backwards for the next "normal" statement. | |
7679 */ | |
7680 else | |
7681 { | |
7682 /* | |
7683 * Skip single break line, if before a switch label. It | |
7684 * may be lined up with the case label. | |
7685 */ | |
7686 if (lookfor == LOOKFOR_NOBREAK | |
7687 && cin_isbreak(skipwhite(ml_get_curline()))) | |
7688 { | |
7689 lookfor = LOOKFOR_ANY; | |
7690 continue; | |
7691 } | |
7692 | |
7693 /* | |
7694 * Handle "do {" line. | |
7695 */ | |
7696 if (whilelevel > 0) | |
7697 { | |
7698 l = cin_skipcomment(ml_get_curline()); | |
7699 if (cin_isdo(l)) | |
7700 { | |
7701 amount = get_indent(); /* XXX */ | |
7702 --whilelevel; | |
7703 continue; | |
7704 } | |
7705 } | |
7706 | |
7707 /* | |
7708 * Found a terminated line above an unterminated line. Add | |
7709 * the amount for a continuation line. | |
7710 * x = 1; | |
7711 * y = foo + | |
7712 * -> here; | |
7713 * or | |
7714 * int x = 1; | |
7715 * int foo, | |
7716 * -> here; | |
7717 */ | |
7718 if (lookfor == LOOKFOR_UNTERM | |
7719 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
7720 { | |
7721 if (cont_amount > 0) | |
7722 amount = cont_amount; | |
7723 else | |
7724 amount += ind_continuation; | |
7725 break; | |
7726 } | |
7727 | |
7728 /* | |
7729 * Found a terminated line above a terminated line or "if" | |
7730 * etc. line. Use the amount of the line below us. | |
7731 * x = 1; x = 1; | |
7732 * if (asdf) y = 2; | |
7733 * while (asdf) ->here; | |
7734 * here; | |
7735 * ->foo; | |
7736 */ | |
7737 if (lookfor == LOOKFOR_TERM) | |
7738 { | |
7739 if (!lookfor_break && whilelevel == 0) | |
7740 break; | |
7741 } | |
7742 | |
7743 /* | |
7744 * First line above the one we're indenting is terminated. | |
7745 * To know what needs to be done look further backward for | |
7746 * a terminated line. | |
7747 */ | |
7748 else | |
7749 { | |
7750 /* | |
7751 * position the cursor over the rightmost paren, so | |
7752 * that matching it will take us back to the start of | |
7753 * the line. Helps for: | |
7754 * func(asdr, | |
7755 * asdfasdf); | |
7756 * here; | |
7757 */ | |
7758 term_again: | |
7759 l = ml_get_curline(); | |
7760 if (find_last_paren(l, '(', ')') | |
7761 && (trypos = find_match_paren(ind_maxparen, | |
7762 ind_maxcomment)) != NULL) | |
7763 { | |
7764 /* | |
7765 * Check if we are on a case label now. This is | |
7766 * handled above. | |
7767 * case xx: if ( asdf && | |
7768 * asdf) | |
7769 */ | |
1550 | 7770 curwin->w_cursor = *trypos; |
7 | 7771 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
|
7772 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) |
7 | 7773 { |
7774 ++curwin->w_cursor.lnum; | |
1550 | 7775 curwin->w_cursor.col = 0; |
7 | 7776 continue; |
7777 } | |
7778 } | |
7779 | |
7780 /* When aligning with the case statement, don't align | |
7781 * with a statement after it. | |
7782 * case 1: { <-- don't use this { position | |
7783 * stat; | |
7784 * } | |
7785 * case 2: | |
7786 * stat; | |
7787 * } | |
7788 */ | |
2297
5ffe000a9ecf
Improve Javascript indenting. Add "J" flag to 'cino'. (Hari Kumar G)
Bram Moolenaar <bram@vim.org>
parents:
2288
diff
changeset
|
7789 iscase = (ind_keep_case_label && cin_iscase(l, FALSE)); |
7 | 7790 |
7791 /* | |
7792 * Get indent and pointer to text for current line, | |
7793 * ignoring any jump label. | |
7794 */ | |
7795 amount = skip_label(curwin->w_cursor.lnum, | |
7796 &l, ind_maxcomment); | |
7797 | |
7798 if (theline[0] == '{') | |
7799 amount += ind_open_extra; | |
7800 /* See remark above: "Only add ind_open_extra.." */ | |
827 | 7801 l = skipwhite(l); |
7802 if (*l == '{') | |
7 | 7803 amount -= ind_open_extra; |
7804 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; | |
7805 | |
7806 /* | |
827 | 7807 * When a terminated line starts with "else" skip to |
7808 * the matching "if": | |
7809 * else 3; | |
856 | 7810 * indent this; |
827 | 7811 * Need to use the scope of this "else". XXX |
7812 * If whilelevel != 0 continue looking for a "do {". | |
7813 */ | |
7814 if (lookfor == LOOKFOR_TERM | |
7815 && *l != '}' | |
7816 && cin_iselse(l) | |
7817 && whilelevel == 0) | |
7818 { | |
7819 if ((trypos = find_start_brace(ind_maxcomment)) | |
7820 == NULL | |
7821 || find_match(LOOKFOR_IF, trypos->lnum, | |
7822 ind_maxparen, ind_maxcomment) == FAIL) | |
7823 break; | |
7824 continue; | |
7825 } | |
7826 | |
7827 /* | |
7 | 7828 * If we're at the end of a block, skip to the start of |
7829 * that block. | |
7830 */ | |
7831 curwin->w_cursor.col = 0; | |
7832 if (*cin_skipcomment(l) == '}' | |
7833 && (trypos = find_start_brace(ind_maxcomment)) | |
7834 != NULL) /* XXX */ | |
7835 { | |
1550 | 7836 curwin->w_cursor = *trypos; |
7 | 7837 /* if not "else {" check for terminated again */ |
7838 /* but skip block for "} else {" */ | |
7839 l = cin_skipcomment(ml_get_curline()); | |
7840 if (*l == '}' || !cin_iselse(l)) | |
7841 goto term_again; | |
7842 ++curwin->w_cursor.lnum; | |
1550 | 7843 curwin->w_cursor.col = 0; |
7 | 7844 } |
7845 } | |
7846 } | |
7847 } | |
7848 } | |
7849 } | |
7850 | |
7851 /* add extra indent for a comment */ | |
7852 if (cin_iscomment(theline)) | |
7853 amount += ind_comment; | |
2328
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
7854 |
15379284e55a
Add the 'L' item to 'cinoptions'. (Manuel Konig)
Bram Moolenaar <bram@vim.org>
parents:
2322
diff
changeset
|
7855 /* 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
|
7856 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
|
7857 amount -= ind_jump_label; |
7 | 7858 } |
7859 | |
7860 /* | |
7861 * ok -- we're not inside any sort of structure at all! | |
7862 * | |
7863 * this means we're at the top level, and everything should | |
7864 * basically just match where the previous line is, except | |
7865 * for the lines immediately following a function declaration, | |
7866 * which are K&R-style parameters and need to be indented. | |
7867 */ | |
7868 else | |
7869 { | |
7870 /* | |
7871 * if our line starts with an open brace, forget about any | |
7872 * prevailing indent and make sure it looks like the start | |
7873 * of a function | |
7874 */ | |
7875 | |
7876 if (theline[0] == '{') | |
7877 { | |
7878 amount = ind_first_open; | |
7879 } | |
7880 | |
7881 /* | |
7882 * If the NEXT line is a function declaration, the current | |
7883 * 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
|
7884 * 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
|
7885 * 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
|
7886 * contains { or }: "void f() {\n if (1)" |
7 | 7887 */ |
7888 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count | |
7889 && !cin_nocode(theline) | |
2094
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
7890 && vim_strchr(theline, '{') == NULL |
e319a4fe6e32
updated for version 7.2.378
Bram Moolenaar <bram@zimbu.org>
parents:
2030
diff
changeset
|
7891 && vim_strchr(theline, '}') == NULL |
7 | 7892 && !cin_ends_in(theline, (char_u *)":", NULL) |
7893 && !cin_ends_in(theline, (char_u *)",", NULL) | |
7894 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1) | |
7895 && !cin_isterminated(theline, FALSE, TRUE)) | |
7896 { | |
7897 amount = ind_func_type; | |
7898 } | |
7899 else | |
7900 { | |
7901 amount = 0; | |
7902 curwin->w_cursor = cur_curpos; | |
7903 | |
7904 /* search backwards until we find something we recognize */ | |
7905 | |
7906 while (curwin->w_cursor.lnum > 1) | |
7907 { | |
7908 curwin->w_cursor.lnum--; | |
7909 curwin->w_cursor.col = 0; | |
7910 | |
7911 l = ml_get_curline(); | |
7912 | |
7913 /* | |
7914 * If we're in a comment now, skip to the start of the comment. | |
7915 */ /* XXX */ | |
7916 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) | |
7917 { | |
7918 curwin->w_cursor.lnum = trypos->lnum + 1; | |
1550 | 7919 curwin->w_cursor.col = 0; |
7 | 7920 continue; |
7921 } | |
7922 | |
7923 /* | |
827 | 7924 * Are we at the start of a cpp base class declaration or |
7925 * constructor initialization? | |
7 | 7926 */ /* XXX */ |
827 | 7927 n = FALSE; |
7928 if (ind_cpp_baseclass != 0 && theline[0] != '{') | |
7929 { | |
1336 | 7930 n = cin_is_cpp_baseclass(&col); |
827 | 7931 l = ml_get_curline(); |
7932 } | |
7933 if (n) | |
7 | 7934 { |
828 | 7935 /* XXX */ |
7936 amount = get_baseclass_amount(col, ind_maxparen, | |
7937 ind_maxcomment, ind_cpp_baseclass); | |
7 | 7938 break; |
7939 } | |
7940 | |
7941 /* | |
7942 * Skip preprocessor directives and blank lines. | |
7943 */ | |
7944 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)) | |
7945 continue; | |
7946 | |
7947 if (cin_nocode(l)) | |
7948 continue; | |
7949 | |
7950 /* | |
7951 * If the previous line ends in ',', use one level of | |
7952 * indentation: | |
7953 * int foo, | |
7954 * bar; | |
7955 * do this before checking for '}' in case of eg. | |
7956 * enum foobar | |
7957 * { | |
7958 * ... | |
7959 * } foo, | |
7960 * bar; | |
7961 */ | |
7962 n = 0; | |
7963 if (cin_ends_in(l, (char_u *)",", NULL) | |
7964 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) | |
7965 { | |
7966 /* take us back to opening paren */ | |
7967 if (find_last_paren(l, '(', ')') | |
7968 && (trypos = find_match_paren(ind_maxparen, | |
7969 ind_maxcomment)) != NULL) | |
1550 | 7970 curwin->w_cursor = *trypos; |
7 | 7971 |
7972 /* For a line ending in ',' that is a continuation line go | |
7973 * back to the first line with a backslash: | |
7974 * char *foo = "bla\ | |
7975 * bla", | |
7976 * here; | |
7977 */ | |
7978 while (n == 0 && curwin->w_cursor.lnum > 1) | |
7979 { | |
7980 l = ml_get(curwin->w_cursor.lnum - 1); | |
7981 if (*l == NUL || l[STRLEN(l) - 1] != '\\') | |
7982 break; | |
7983 --curwin->w_cursor.lnum; | |
1550 | 7984 curwin->w_cursor.col = 0; |
7 | 7985 } |
7986 | |
7987 amount = get_indent(); /* XXX */ | |
7988 | |
7989 if (amount == 0) | |
7990 amount = cin_first_id_amount(); | |
7991 if (amount == 0) | |
7992 amount = ind_continuation; | |
7993 break; | |
7994 } | |
7995 | |
7996 /* | |
7997 * If the line looks like a function declaration, and we're | |
7998 * not in a comment, put it the left margin. | |
7999 */ | |
8000 if (cin_isfuncdecl(NULL, cur_curpos.lnum)) /* XXX */ | |
8001 break; | |
8002 l = ml_get_curline(); | |
8003 | |
8004 /* | |
8005 * Finding the closing '}' of a previous function. Put | |
8006 * current line at the left margin. For when 'cino' has "fs". | |
8007 */ | |
8008 if (*skipwhite(l) == '}') | |
8009 break; | |
8010 | |
8011 /* (matching {) | |
8012 * If the previous line ends on '};' (maybe followed by | |
8013 * comments) align at column 0. For example: | |
8014 * char *string_array[] = { "foo", | |
8015 * / * x * / "b};ar" }; / * foobar * / | |
8016 */ | |
8017 if (cin_ends_in(l, (char_u *)"};", NULL)) | |
8018 break; | |
8019 | |
8020 /* | |
8021 * If the PREVIOUS line is a function declaration, the current | |
8022 * line (and the ones that follow) needs to be indented as | |
8023 * parameters. | |
8024 */ | |
8025 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum)) | |
8026 { | |
8027 amount = ind_param; | |
8028 break; | |
8029 } | |
8030 | |
8031 /* | |
8032 * If the previous line ends in ';' and the line before the | |
8033 * previous line ends in ',' or '\', ident to column zero: | |
8034 * int foo, | |
8035 * bar; | |
8036 * indent_to_0 here; | |
8037 */ | |
828 | 8038 if (cin_ends_in(l, (char_u *)";", NULL)) |
7 | 8039 { |
8040 l = ml_get(curwin->w_cursor.lnum - 1); | |
8041 if (cin_ends_in(l, (char_u *)",", NULL) | |
8042 || (*l != NUL && l[STRLEN(l) - 1] == '\\')) | |
8043 break; | |
8044 l = ml_get_curline(); | |
8045 } | |
8046 | |
8047 /* | |
8048 * Doesn't look like anything interesting -- so just | |
8049 * use the indent of this line. | |
8050 * | |
8051 * Position the cursor over the rightmost paren, so that | |
8052 * matching it will take us back to the start of the line. | |
8053 */ | |
8054 find_last_paren(l, '(', ')'); | |
8055 | |
8056 if ((trypos = find_match_paren(ind_maxparen, | |
8057 ind_maxcomment)) != NULL) | |
1550 | 8058 curwin->w_cursor = *trypos; |
7 | 8059 amount = get_indent(); /* XXX */ |
8060 break; | |
8061 } | |
8062 | |
8063 /* add extra indent for a comment */ | |
8064 if (cin_iscomment(theline)) | |
8065 amount += ind_comment; | |
8066 | |
8067 /* add extra indent if the previous line ended in a backslash: | |
8068 * "asdfasdf\ | |
8069 * here"; | |
8070 * char *foo = "asdf\ | |
8071 * here"; | |
8072 */ | |
8073 if (cur_curpos.lnum > 1) | |
8074 { | |
8075 l = ml_get(cur_curpos.lnum - 1); | |
8076 if (*l != NUL && l[STRLEN(l) - 1] == '\\') | |
8077 { | |
8078 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); | |
8079 if (cur_amount > 0) | |
8080 amount = cur_amount; | |
8081 else if (cur_amount == 0) | |
8082 amount += ind_continuation; | |
8083 } | |
8084 } | |
8085 } | |
8086 } | |
8087 | |
8088 theend: | |
8089 /* put the cursor back where it belongs */ | |
8090 curwin->w_cursor = cur_curpos; | |
8091 | |
8092 vim_free(linecopy); | |
8093 | |
8094 if (amount < 0) | |
8095 return 0; | |
8096 return amount; | |
8097 } | |
8098 | |
8099 static int | |
8100 find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment) | |
8101 int lookfor; | |
8102 linenr_T ourscope; | |
8103 int ind_maxparen; | |
8104 int ind_maxcomment; | |
8105 { | |
8106 char_u *look; | |
8107 pos_T *theirscope; | |
8108 char_u *mightbeif; | |
8109 int elselevel; | |
8110 int whilelevel; | |
8111 | |
8112 if (lookfor == LOOKFOR_IF) | |
8113 { | |
8114 elselevel = 1; | |
8115 whilelevel = 0; | |
8116 } | |
8117 else | |
8118 { | |
8119 elselevel = 0; | |
8120 whilelevel = 1; | |
8121 } | |
8122 | |
8123 curwin->w_cursor.col = 0; | |
8124 | |
8125 while (curwin->w_cursor.lnum > ourscope + 1) | |
8126 { | |
8127 curwin->w_cursor.lnum--; | |
8128 curwin->w_cursor.col = 0; | |
8129 | |
8130 look = cin_skipcomment(ml_get_curline()); | |
8131 if (cin_iselse(look) | |
8132 || cin_isif(look) | |
8133 || cin_isdo(look) /* XXX */ | |
8134 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen)) | |
8135 { | |
8136 /* | |
8137 * if we've gone outside the braces entirely, | |
8138 * we must be out of scope... | |
8139 */ | |
8140 theirscope = find_start_brace(ind_maxcomment); /* XXX */ | |
8141 if (theirscope == NULL) | |
8142 break; | |
8143 | |
8144 /* | |
8145 * and if the brace enclosing this is further | |
8146 * back than the one enclosing the else, we're | |
8147 * out of luck too. | |
8148 */ | |
8149 if (theirscope->lnum < ourscope) | |
8150 break; | |
8151 | |
8152 /* | |
8153 * and if they're enclosed in a *deeper* brace, | |
8154 * then we can ignore it because it's in a | |
8155 * different scope... | |
8156 */ | |
8157 if (theirscope->lnum > ourscope) | |
8158 continue; | |
8159 | |
8160 /* | |
8161 * if it was an "else" (that's not an "else if") | |
8162 * then we need to go back to another if, so | |
8163 * increment elselevel | |
8164 */ | |
8165 look = cin_skipcomment(ml_get_curline()); | |
8166 if (cin_iselse(look)) | |
8167 { | |
8168 mightbeif = cin_skipcomment(look + 4); | |
8169 if (!cin_isif(mightbeif)) | |
8170 ++elselevel; | |
8171 continue; | |
8172 } | |
8173 | |
8174 /* | |
8175 * if it was a "while" then we need to go back to | |
8176 * another "do", so increment whilelevel. XXX | |
8177 */ | |
8178 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen)) | |
8179 { | |
8180 ++whilelevel; | |
8181 continue; | |
8182 } | |
8183 | |
8184 /* If it's an "if" decrement elselevel */ | |
8185 look = cin_skipcomment(ml_get_curline()); | |
8186 if (cin_isif(look)) | |
8187 { | |
8188 elselevel--; | |
8189 /* | |
8190 * When looking for an "if" ignore "while"s that | |
8191 * get in the way. | |
8192 */ | |
8193 if (elselevel == 0 && lookfor == LOOKFOR_IF) | |
8194 whilelevel = 0; | |
8195 } | |
8196 | |
8197 /* If it's a "do" decrement whilelevel */ | |
8198 if (cin_isdo(look)) | |
8199 whilelevel--; | |
8200 | |
8201 /* | |
8202 * if we've used up all the elses, then | |
8203 * this must be the if that we want! | |
8204 * match the indent level of that if. | |
8205 */ | |
8206 if (elselevel <= 0 && whilelevel <= 0) | |
8207 { | |
8208 return OK; | |
8209 } | |
8210 } | |
8211 } | |
8212 return FAIL; | |
8213 } | |
8214 | |
8215 # if defined(FEAT_EVAL) || defined(PROTO) | |
8216 /* | |
8217 * Get indent level from 'indentexpr'. | |
8218 */ | |
8219 int | |
8220 get_expr_indent() | |
8221 { | |
8222 int indent; | |
8223 pos_T pos; | |
8224 int save_State; | |
681 | 8225 int use_sandbox = was_set_insecurely((char_u *)"indentexpr", |
8226 OPT_LOCAL); | |
7 | 8227 |
8228 pos = curwin->w_cursor; | |
8229 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum); | |
634 | 8230 if (use_sandbox) |
8231 ++sandbox; | |
8232 ++textlock; | |
7 | 8233 indent = eval_to_number(curbuf->b_p_inde); |
634 | 8234 if (use_sandbox) |
8235 --sandbox; | |
8236 --textlock; | |
7 | 8237 |
8238 /* Restore the cursor position so that 'indentexpr' doesn't need to. | |
8239 * Pretend to be in Insert mode, allow cursor past end of line for "o" | |
8240 * command. */ | |
8241 save_State = State; | |
8242 State = INSERT; | |
8243 curwin->w_cursor = pos; | |
8244 check_cursor(); | |
8245 State = save_State; | |
8246 | |
8247 /* If there is an error, just keep the current indent. */ | |
8248 if (indent < 0) | |
8249 indent = get_indent(); | |
8250 | |
8251 return indent; | |
8252 } | |
8253 # endif | |
8254 | |
8255 #endif /* FEAT_CINDENT */ | |
8256 | |
8257 #if defined(FEAT_LISP) || defined(PROTO) | |
8258 | |
8259 static int lisp_match __ARGS((char_u *p)); | |
8260 | |
8261 static int | |
8262 lisp_match(p) | |
8263 char_u *p; | |
8264 { | |
8265 char_u buf[LSIZE]; | |
8266 int len; | |
8267 char_u *word = p_lispwords; | |
8268 | |
8269 while (*word != NUL) | |
8270 { | |
8271 (void)copy_option_part(&word, buf, LSIZE, ","); | |
8272 len = (int)STRLEN(buf); | |
8273 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ') | |
8274 return TRUE; | |
8275 } | |
8276 return FALSE; | |
8277 } | |
8278 | |
8279 /* | |
8280 * When 'p' is present in 'cpoptions, a Vi compatible method is used. | |
8281 * The incompatible newer method is quite a bit better at indenting | |
8282 * code in lisp-like languages than the traditional one; it's still | |
8283 * mostly heuristics however -- Dirk van Deun, dirk@rave.org | |
8284 * | |
8285 * TODO: | |
8286 * Findmatch() should be adapted for lisp, also to make showmatch | |
8287 * work correctly: now (v5.3) it seems all C/C++ oriented: | |
8288 * - it does not recognize the #\( and #\) notations as character literals | |
8289 * - it doesn't know about comments starting with a semicolon | |
8290 * - it incorrectly interprets '(' as a character literal | |
8291 * All this messes up get_lisp_indent in some rare cases. | |
14 | 8292 * Update from Sergey Khorev: |
8293 * I tried to fix the first two issues. | |
7 | 8294 */ |
8295 int | |
8296 get_lisp_indent() | |
8297 { | |
14 | 8298 pos_T *pos, realpos, paren; |
7 | 8299 int amount; |
8300 char_u *that; | |
8301 colnr_T col; | |
8302 colnr_T firsttry; | |
8303 int parencount, quotecount; | |
8304 int vi_lisp; | |
8305 | |
8306 /* Set vi_lisp to use the vi-compatible method */ | |
8307 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL); | |
8308 | |
8309 realpos = curwin->w_cursor; | |
8310 curwin->w_cursor.col = 0; | |
8311 | |
14 | 8312 if ((pos = findmatch(NULL, '(')) == NULL) |
8313 pos = findmatch(NULL, '['); | |
8314 else | |
8315 { | |
8316 paren = *pos; | |
8317 pos = findmatch(NULL, '['); | |
8318 if (pos == NULL || ltp(pos, &paren)) | |
8319 pos = &paren; | |
8320 } | |
8321 if (pos != NULL) | |
7 | 8322 { |
8323 /* Extra trick: Take the indent of the first previous non-white | |
8324 * line that is at the same () level. */ | |
8325 amount = -1; | |
8326 parencount = 0; | |
8327 | |
8328 while (--curwin->w_cursor.lnum >= pos->lnum) | |
8329 { | |
8330 if (linewhite(curwin->w_cursor.lnum)) | |
8331 continue; | |
8332 for (that = ml_get_curline(); *that != NUL; ++that) | |
8333 { | |
8334 if (*that == ';') | |
8335 { | |
8336 while (*(that + 1) != NUL) | |
8337 ++that; | |
8338 continue; | |
8339 } | |
8340 if (*that == '\\') | |
8341 { | |
8342 if (*(that + 1) != NUL) | |
8343 ++that; | |
8344 continue; | |
8345 } | |
8346 if (*that == '"' && *(that + 1) != NUL) | |
8347 { | |
983 | 8348 while (*++that && *that != '"') |
8349 { | |
8350 /* skipping escaped characters in the string */ | |
8351 if (*that == '\\') | |
8352 { | |
8353 if (*++that == NUL) | |
8354 break; | |
8355 if (that[1] == NUL) | |
8356 { | |
8357 ++that; | |
8358 break; | |
8359 } | |
8360 } | |
8361 } | |
7 | 8362 } |
14 | 8363 if (*that == '(' || *that == '[') |
7 | 8364 ++parencount; |
14 | 8365 else if (*that == ')' || *that == ']') |
7 | 8366 --parencount; |
8367 } | |
8368 if (parencount == 0) | |
8369 { | |
8370 amount = get_indent(); | |
8371 break; | |
8372 } | |
8373 } | |
8374 | |
8375 if (amount == -1) | |
8376 { | |
8377 curwin->w_cursor.lnum = pos->lnum; | |
8378 curwin->w_cursor.col = pos->col; | |
8379 col = pos->col; | |
8380 | |
8381 that = ml_get_curline(); | |
8382 | |
8383 if (vi_lisp && get_indent() == 0) | |
8384 amount = 2; | |
8385 else | |
8386 { | |
8387 amount = 0; | |
8388 while (*that && col) | |
8389 { | |
8390 amount += lbr_chartabsize_adv(&that, (colnr_T)amount); | |
8391 col--; | |
8392 } | |
8393 | |
8394 /* | |
8395 * Some keywords require "body" indenting rules (the | |
8396 * non-standard-lisp ones are Scheme special forms): | |
8397 * | |
8398 * (let ((a 1)) instead (let ((a 1)) | |
8399 * (...)) of (...)) | |
8400 */ | |
8401 | |
14 | 8402 if (!vi_lisp && (*that == '(' || *that == '[') |
8403 && lisp_match(that + 1)) | |
7 | 8404 amount += 2; |
8405 else | |
8406 { | |
8407 that++; | |
8408 amount++; | |
8409 firsttry = amount; | |
8410 | |
8411 while (vim_iswhite(*that)) | |
8412 { | |
8413 amount += lbr_chartabsize(that, (colnr_T)amount); | |
8414 ++that; | |
8415 } | |
8416 | |
8417 if (*that && *that != ';') /* not a comment line */ | |
8418 { | |
1532 | 8419 /* test *that != '(' to accommodate first let/do |
7 | 8420 * argument if it is more than one line */ |
14 | 8421 if (!vi_lisp && *that != '(' && *that != '[') |
7 | 8422 firsttry++; |
8423 | |
8424 parencount = 0; | |
8425 quotecount = 0; | |
8426 | |
8427 if (vi_lisp | |
8428 || (*that != '"' | |
8429 && *that != '\'' | |
8430 && *that != '#' | |
8431 && (*that < '0' || *that > '9'))) | |
8432 { | |
8433 while (*that | |
8434 && (!vim_iswhite(*that) | |
8435 || quotecount | |
8436 || parencount) | |
14 | 8437 && (!((*that == '(' || *that == '[') |
7 | 8438 && !quotecount |
8439 && !parencount | |
8440 && vi_lisp))) | |
8441 { | |
8442 if (*that == '"') | |
8443 quotecount = !quotecount; | |
14 | 8444 if ((*that == '(' || *that == '[') |
8445 && !quotecount) | |
7 | 8446 ++parencount; |
14 | 8447 if ((*that == ')' || *that == ']') |
8448 && !quotecount) | |
7 | 8449 --parencount; |
8450 if (*that == '\\' && *(that+1) != NUL) | |
8451 amount += lbr_chartabsize_adv(&that, | |
8452 (colnr_T)amount); | |
8453 amount += lbr_chartabsize_adv(&that, | |
8454 (colnr_T)amount); | |
8455 } | |
8456 } | |
8457 while (vim_iswhite(*that)) | |
8458 { | |
8459 amount += lbr_chartabsize(that, (colnr_T)amount); | |
8460 that++; | |
8461 } | |
8462 if (!*that || *that == ';') | |
8463 amount = firsttry; | |
8464 } | |
8465 } | |
8466 } | |
8467 } | |
8468 } | |
8469 else | |
14 | 8470 amount = 0; /* no matching '(' or '[' found, use zero indent */ |
7 | 8471 |
8472 curwin->w_cursor = realpos; | |
8473 | |
8474 return amount; | |
8475 } | |
8476 #endif /* FEAT_LISP */ | |
8477 | |
8478 void | |
8479 prepare_to_exit() | |
8480 { | |
39 | 8481 #if defined(SIGHUP) && defined(SIG_IGN) |
8482 /* Ignore SIGHUP, because a dropped connection causes a read error, which | |
8483 * makes Vim exit and then handling SIGHUP causes various reentrance | |
8484 * problems. */ | |
36 | 8485 signal(SIGHUP, SIG_IGN); |
8486 #endif | |
8487 | |
7 | 8488 #ifdef FEAT_GUI |
8489 if (gui.in_use) | |
8490 { | |
8491 gui.dying = TRUE; | |
8492 out_trash(); /* trash any pending output */ | |
8493 } | |
8494 else | |
8495 #endif | |
8496 { | |
8497 windgoto((int)Rows - 1, 0); | |
8498 | |
8499 /* | |
8500 * Switch terminal mode back now, so messages end up on the "normal" | |
8501 * screen (if there are two screens). | |
8502 */ | |
8503 settmode(TMODE_COOK); | |
8504 #ifdef WIN3264 | |
8505 if (can_end_termcap_mode(FALSE) == TRUE) | |
8506 #endif | |
8507 stoptermcap(); | |
8508 out_flush(); | |
8509 } | |
8510 } | |
8511 | |
8512 /* | |
8513 * Preserve files and exit. | |
8514 * When called IObuff must contain a message. | |
8515 */ | |
8516 void | |
8517 preserve_exit() | |
8518 { | |
8519 buf_T *buf; | |
8520 | |
8521 prepare_to_exit(); | |
8522 | |
625 | 8523 /* Setting this will prevent free() calls. That avoids calling free() |
8524 * recursively when free() was invoked with a bad pointer. */ | |
8525 really_exiting = TRUE; | |
8526 | |
7 | 8527 out_str(IObuff); |
8528 screen_start(); /* don't know where cursor is now */ | |
8529 out_flush(); | |
8530 | |
8531 ml_close_notmod(); /* close all not-modified buffers */ | |
8532 | |
8533 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
8534 { | |
8535 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL) | |
8536 { | |
8537 OUT_STR(_("Vim: preserving files...\n")); | |
8538 screen_start(); /* don't know where cursor is now */ | |
8539 out_flush(); | |
8540 ml_sync_all(FALSE, FALSE); /* preserve all swap files */ | |
8541 break; | |
8542 } | |
8543 } | |
8544 | |
8545 ml_close_all(FALSE); /* close all memfiles, without deleting */ | |
8546 | |
8547 OUT_STR(_("Vim: Finished.\n")); | |
8548 | |
8549 getout(1); | |
8550 } | |
8551 | |
8552 /* | |
8553 * return TRUE if "fname" exists. | |
8554 */ | |
8555 int | |
8556 vim_fexists(fname) | |
8557 char_u *fname; | |
8558 { | |
8559 struct stat st; | |
8560 | |
8561 if (mch_stat((char *)fname, &st)) | |
8562 return FALSE; | |
8563 return TRUE; | |
8564 } | |
8565 | |
8566 /* | |
8567 * Check for CTRL-C pressed, but only once in a while. | |
8568 * Should be used instead of ui_breakcheck() for functions that check for | |
8569 * each line in the file. Calling ui_breakcheck() each time takes too much | |
8570 * time, because it can be a system call. | |
8571 */ | |
8572 | |
8573 #ifndef BREAKCHECK_SKIP | |
8574 # ifdef FEAT_GUI /* assume the GUI only runs on fast computers */ | |
8575 # define BREAKCHECK_SKIP 200 | |
8576 # else | |
8577 # define BREAKCHECK_SKIP 32 | |
8578 # endif | |
8579 #endif | |
8580 | |
8581 static int breakcheck_count = 0; | |
8582 | |
8583 void | |
8584 line_breakcheck() | |
8585 { | |
8586 if (++breakcheck_count >= BREAKCHECK_SKIP) | |
8587 { | |
8588 breakcheck_count = 0; | |
8589 ui_breakcheck(); | |
8590 } | |
8591 } | |
8592 | |
8593 /* | |
8594 * Like line_breakcheck() but check 10 times less often. | |
8595 */ | |
8596 void | |
8597 fast_breakcheck() | |
8598 { | |
8599 if (++breakcheck_count >= BREAKCHECK_SKIP * 10) | |
8600 { | |
8601 breakcheck_count = 0; | |
8602 ui_breakcheck(); | |
8603 } | |
8604 } | |
8605 | |
8606 /* | |
2016 | 8607 * Invoke expand_wildcards() for one pattern. |
8608 * Expand items like "%:h" before the expansion. | |
8609 * Returns OK or FAIL. | |
8610 */ | |
8611 int | |
8612 expand_wildcards_eval(pat, num_file, file, flags) | |
8613 char_u **pat; /* pointer to input pattern */ | |
8614 int *num_file; /* resulting number of files */ | |
8615 char_u ***file; /* array of resulting files */ | |
8616 int flags; /* EW_DIR, etc. */ | |
8617 { | |
8618 int ret = FAIL; | |
8619 char_u *eval_pat = NULL; | |
8620 char_u *exp_pat = *pat; | |
8621 char_u *ignored_msg; | |
8622 int usedlen; | |
8623 | |
8624 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') | |
8625 { | |
8626 ++emsg_off; | |
8627 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, | |
8628 NULL, &ignored_msg, NULL); | |
8629 --emsg_off; | |
8630 if (eval_pat != NULL) | |
8631 exp_pat = concat_str(eval_pat, exp_pat + usedlen); | |
8632 } | |
8633 | |
8634 if (exp_pat != NULL) | |
8635 ret = expand_wildcards(1, &exp_pat, num_file, file, flags); | |
8636 | |
8637 if (eval_pat != NULL) | |
8638 { | |
8639 vim_free(exp_pat); | |
8640 vim_free(eval_pat); | |
8641 } | |
8642 | |
8643 return ret; | |
8644 } | |
8645 | |
8646 /* | |
7 | 8647 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching |
8648 * 'wildignore'. | |
2354
f4440cdd59ae
Fixed: crash with ":find " completion, using uninitialized count.
Bram Moolenaar <bram@vim.org>
parents:
2328
diff
changeset
|
8649 * Returns OK or FAIL. When FAIL then "num_file" won't be set. |
7 | 8650 */ |
8651 int | |
8652 expand_wildcards(num_pat, pat, num_file, file, flags) | |
8653 int num_pat; /* number of input patterns */ | |
8654 char_u **pat; /* array of input patterns */ | |
8655 int *num_file; /* resulting number of files */ | |
8656 char_u ***file; /* array of resulting files */ | |
8657 int flags; /* EW_DIR, etc. */ | |
8658 { | |
8659 int retval; | |
8660 int i, j; | |
8661 char_u *p; | |
8662 int non_suf_match; /* number without matching suffix */ | |
8663 | |
8664 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags); | |
8665 | |
8666 /* 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
|
8667 if ((flags & EW_KEEPALL) || retval == FAIL) |
7 | 8668 return retval; |
8669 | |
8670 #ifdef FEAT_WILDIGN | |
8671 /* | |
8672 * Remove names that match 'wildignore'. | |
8673 */ | |
8674 if (*p_wig) | |
8675 { | |
8676 char_u *ffname; | |
8677 | |
8678 /* check all files in (*file)[] */ | |
8679 for (i = 0; i < *num_file; ++i) | |
8680 { | |
8681 ffname = FullName_save((*file)[i], FALSE); | |
8682 if (ffname == NULL) /* out of memory */ | |
8683 break; | |
8684 # ifdef VMS | |
8685 vms_remove_version(ffname); | |
8686 # endif | |
8687 if (match_file_list(p_wig, (*file)[i], ffname)) | |
8688 { | |
8689 /* remove this matching file from the list */ | |
8690 vim_free((*file)[i]); | |
8691 for (j = i; j + 1 < *num_file; ++j) | |
8692 (*file)[j] = (*file)[j + 1]; | |
8693 --*num_file; | |
8694 --i; | |
8695 } | |
8696 vim_free(ffname); | |
8697 } | |
8698 } | |
8699 #endif | |
8700 | |
8701 /* | |
8702 * Move the names where 'suffixes' match to the end. | |
8703 */ | |
8704 if (*num_file > 1) | |
8705 { | |
8706 non_suf_match = 0; | |
8707 for (i = 0; i < *num_file; ++i) | |
8708 { | |
8709 if (!match_suffix((*file)[i])) | |
8710 { | |
8711 /* | |
8712 * Move the name without matching suffix to the front | |
8713 * of the list. | |
8714 */ | |
8715 p = (*file)[i]; | |
8716 for (j = i; j > non_suf_match; --j) | |
8717 (*file)[j] = (*file)[j - 1]; | |
8718 (*file)[non_suf_match++] = p; | |
8719 } | |
8720 } | |
8721 } | |
8722 | |
8723 return retval; | |
8724 } | |
8725 | |
8726 /* | |
8727 * Return TRUE if "fname" matches with an entry in 'suffixes'. | |
8728 */ | |
8729 int | |
8730 match_suffix(fname) | |
8731 char_u *fname; | |
8732 { | |
8733 int fnamelen, setsuflen; | |
8734 char_u *setsuf; | |
8735 #define MAXSUFLEN 30 /* maximum length of a file suffix */ | |
8736 char_u suf_buf[MAXSUFLEN]; | |
8737 | |
8738 fnamelen = (int)STRLEN(fname); | |
8739 setsuflen = 0; | |
8740 for (setsuf = p_su; *setsuf; ) | |
8741 { | |
8742 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); | |
1937 | 8743 if (setsuflen == 0) |
8744 { | |
8745 char_u *tail = gettail(fname); | |
8746 | |
8747 /* empty entry: match name without a '.' */ | |
8748 if (vim_strchr(tail, '.') == NULL) | |
8749 { | |
8750 setsuflen = 1; | |
8751 break; | |
8752 } | |
8753 } | |
8754 else | |
8755 { | |
8756 if (fnamelen >= setsuflen | |
8757 && fnamencmp(suf_buf, fname + fnamelen - setsuflen, | |
8758 (size_t)setsuflen) == 0) | |
8759 break; | |
8760 setsuflen = 0; | |
8761 } | |
7 | 8762 } |
8763 return (setsuflen != 0); | |
8764 } | |
8765 | |
8766 #if !defined(NO_EXPANDPATH) || defined(PROTO) | |
8767 | |
8768 # ifdef VIM_BACKTICK | |
8769 static int vim_backtick __ARGS((char_u *p)); | |
8770 static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags)); | |
8771 # endif | |
8772 | |
8773 # if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264) | |
8774 /* | |
8775 * File name expansion code for MS-DOS, Win16 and Win32. It's here because | |
8776 * it's shared between these systems. | |
8777 */ | |
8778 # if defined(DJGPP) || defined(PROTO) | |
8779 # define _cdecl /* DJGPP doesn't have this */ | |
8780 # else | |
8781 # ifdef __BORLANDC__ | |
8782 # define _cdecl _RTLENTRYF | |
8783 # endif | |
8784 # endif | |
8785 | |
8786 /* | |
8787 * comparison function for qsort in dos_expandpath() | |
8788 */ | |
8789 static int _cdecl | |
8790 pstrcmp(const void *a, const void *b) | |
8791 { | |
39 | 8792 return (pathcmp(*(char **)a, *(char **)b, -1)); |
7 | 8793 } |
8794 | |
8795 # ifndef WIN3264 | |
8796 static void | |
8797 namelowcpy( | |
8798 char_u *d, | |
8799 char_u *s) | |
8800 { | |
8801 # ifdef DJGPP | |
8802 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */ | |
8803 while (*s) | |
8804 *d++ = *s++; | |
8805 else | |
8806 # endif | |
8807 while (*s) | |
8808 *d++ = TOLOWER_LOC(*s++); | |
8809 *d = NUL; | |
8810 } | |
8811 # endif | |
8812 | |
8813 /* | |
445 | 8814 * Recursively expand one path component into all matching files and/or |
8815 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
7 | 8816 * Return the number of matches found. |
8817 * "path" has backslashes before chars that are not to be expanded, starting | |
8818 * at "path[wildoff]". | |
445 | 8819 * Return the number of matches found. |
8820 * NOTE: much of this is identical to unix_expandpath(), keep in sync! | |
7 | 8821 */ |
8822 static int | |
8823 dos_expandpath( | |
8824 garray_T *gap, | |
8825 char_u *path, | |
8826 int wildoff, | |
445 | 8827 int flags, /* EW_* flags */ |
1224 | 8828 int didstar) /* expanded "**" once already */ |
445 | 8829 { |
8830 char_u *buf; | |
8831 char_u *path_end; | |
8832 char_u *p, *s, *e; | |
8833 int start_len = gap->ga_len; | |
8834 char_u *pat; | |
8835 regmatch_T regmatch; | |
8836 int starts_with_dot; | |
8837 int matches; | |
8838 int len; | |
8839 int starstar = FALSE; | |
8840 static int stardepth = 0; /* depth for "**" expansion */ | |
7 | 8841 #ifdef WIN3264 |
8842 WIN32_FIND_DATA fb; | |
8843 HANDLE hFind = (HANDLE)0; | |
8844 # ifdef FEAT_MBYTE | |
8845 WIN32_FIND_DATAW wfb; | |
8846 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */ | |
8847 # endif | |
8848 #else | |
8849 struct ffblk fb; | |
8850 #endif | |
8851 char_u *matchname; | |
445 | 8852 int ok; |
8853 | |
8854 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
8855 if (stardepth > 0) | |
8856 { | |
8857 ui_breakcheck(); | |
8858 if (got_int) | |
8859 return 0; | |
8860 } | |
7 | 8861 |
8862 /* make room for file name */ | |
445 | 8863 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); |
7 | 8864 if (buf == NULL) |
8865 return 0; | |
8866 | |
8867 /* | |
8868 * Find the first part in the path name that contains a wildcard or a ~1. | |
8869 * Copy it into buf, including the preceding characters. | |
8870 */ | |
8871 p = buf; | |
8872 s = buf; | |
8873 e = NULL; | |
8874 path_end = path; | |
8875 while (*path_end != NUL) | |
8876 { | |
8877 /* May ignore a wildcard that has a backslash before it; it will | |
8878 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
8879 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
8880 *p++ = *path_end++; | |
8881 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') | |
8882 { | |
8883 if (e != NULL) | |
8884 break; | |
8885 s = p + 1; | |
8886 } | |
8887 else if (path_end >= path + wildoff | |
8888 && vim_strchr((char_u *)"*?[~", *path_end) != NULL) | |
8889 e = p; | |
8890 #ifdef FEAT_MBYTE | |
8891 if (has_mbyte) | |
8892 { | |
474 | 8893 len = (*mb_ptr2len)(path_end); |
7 | 8894 STRNCPY(p, path_end, len); |
8895 p += len; | |
8896 path_end += len; | |
8897 } | |
8898 else | |
8899 #endif | |
8900 *p++ = *path_end++; | |
8901 } | |
8902 e = p; | |
8903 *e = NUL; | |
8904 | |
8905 /* now we have one wildcard component between s and e */ | |
8906 /* Remove backslashes between "wildoff" and the start of the wildcard | |
8907 * component. */ | |
8908 for (p = buf + wildoff; p < s; ++p) | |
8909 if (rem_backslash(p)) | |
8910 { | |
1624 | 8911 STRMOVE(p, p + 1); |
7 | 8912 --e; |
8913 --s; | |
8914 } | |
8915 | |
445 | 8916 /* Check for "**" between "s" and "e". */ |
8917 for (p = s; p < e; ++p) | |
8918 if (p[0] == '*' && p[1] == '*') | |
8919 starstar = TRUE; | |
8920 | |
7 | 8921 starts_with_dot = (*s == '.'); |
8922 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
8923 if (pat == NULL) | |
8924 { | |
8925 vim_free(buf); | |
8926 return 0; | |
8927 } | |
8928 | |
8929 /* compile the regexp into a program */ | |
8930 regmatch.rm_ic = TRUE; /* Always ignore case */ | |
8931 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
8932 vim_free(pat); | |
8933 | |
8934 if (regmatch.regprog == NULL) | |
8935 { | |
8936 vim_free(buf); | |
8937 return 0; | |
8938 } | |
8939 | |
8940 /* remember the pattern or file name being looked for */ | |
8941 matchname = vim_strsave(s); | |
8942 | |
445 | 8943 /* If "**" is by itself, this is the first time we encounter it and more |
8944 * is following then find matches without any directory. */ | |
8945 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
8946 && *path_end == '/') | |
8947 { | |
8948 STRCPY(s, path_end + 1); | |
8949 ++stardepth; | |
8950 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
8951 --stardepth; | |
8952 } | |
8953 | |
7 | 8954 /* Scan all files in the directory with "dir/ *.*" */ |
8955 STRCPY(s, "*.*"); | |
8956 #ifdef WIN3264 | |
8957 # ifdef FEAT_MBYTE | |
8958 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | |
8959 { | |
8960 /* The active codepage differs from 'encoding'. Attempt using the | |
8961 * wide function. If it fails because it is not implemented fall back | |
8962 * to the non-wide version (for Windows 98) */ | |
1752 | 8963 wn = enc_to_utf16(buf, NULL); |
7 | 8964 if (wn != NULL) |
8965 { | |
8966 hFind = FindFirstFileW(wn, &wfb); | |
8967 if (hFind == INVALID_HANDLE_VALUE | |
8968 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) | |
8969 { | |
8970 vim_free(wn); | |
8971 wn = NULL; | |
8972 } | |
8973 } | |
8974 } | |
8975 | |
8976 if (wn == NULL) | |
8977 # endif | |
8978 hFind = FindFirstFile(buf, &fb); | |
8979 ok = (hFind != INVALID_HANDLE_VALUE); | |
8980 #else | |
8981 /* If we are expanding wildcards we try both files and directories */ | |
8982 ok = (findfirst((char *)buf, &fb, | |
8983 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); | |
8984 #endif | |
8985 | |
8986 while (ok) | |
8987 { | |
8988 #ifdef WIN3264 | |
8989 # ifdef FEAT_MBYTE | |
8990 if (wn != NULL) | |
1752 | 8991 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */ |
7 | 8992 else |
8993 # endif | |
8994 p = (char_u *)fb.cFileName; | |
8995 #else | |
8996 p = (char_u *)fb.ff_name; | |
8997 #endif | |
8998 /* Ignore entries starting with a dot, unless when asked for. Accept | |
8999 * all entries found with "matchname". */ | |
9000 if ((p[0] != '.' || starts_with_dot) | |
9001 && (matchname == NULL | |
9002 || vim_regexec(®match, p, (colnr_T)0))) | |
9003 { | |
9004 #ifdef WIN3264 | |
9005 STRCPY(s, p); | |
9006 #else | |
9007 namelowcpy(s, p); | |
9008 #endif | |
9009 len = (int)STRLEN(buf); | |
445 | 9010 |
9011 if (starstar && stardepth < 100) | |
9012 { | |
9013 /* For "**" in the pattern first go deeper in the tree to | |
9014 * find matches. */ | |
9015 STRCPY(buf + len, "/**"); | |
9016 STRCPY(buf + len + 3, path_end); | |
9017 ++stardepth; | |
9018 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); | |
9019 --stardepth; | |
9020 } | |
9021 | |
7 | 9022 STRCPY(buf + len, path_end); |
9023 if (mch_has_exp_wildcard(path_end)) | |
9024 { | |
9025 /* need to expand another component of the path */ | |
9026 /* remove backslashes for the remaining components only */ | |
445 | 9027 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); |
7 | 9028 } |
9029 else | |
9030 { | |
9031 /* no more wildcards, check if there is a match */ | |
9032 /* remove backslashes for the remaining components only */ | |
9033 if (*path_end != 0) | |
9034 backslash_halve(buf + len + 1); | |
9035 if (mch_getperm(buf) >= 0) /* add existing file */ | |
9036 addfile(gap, buf, flags); | |
9037 } | |
9038 } | |
9039 | |
9040 #ifdef WIN3264 | |
9041 # ifdef FEAT_MBYTE | |
9042 if (wn != NULL) | |
9043 { | |
9044 vim_free(p); | |
9045 ok = FindNextFileW(hFind, &wfb); | |
9046 } | |
9047 else | |
9048 # endif | |
9049 ok = FindNextFile(hFind, &fb); | |
9050 #else | |
9051 ok = (findnext(&fb) == 0); | |
9052 #endif | |
9053 | |
9054 /* If no more matches and no match was used, try expanding the name | |
9055 * itself. Finds the long name of a short filename. */ | |
9056 if (!ok && matchname != NULL && gap->ga_len == start_len) | |
9057 { | |
9058 STRCPY(s, matchname); | |
9059 #ifdef WIN3264 | |
9060 FindClose(hFind); | |
9061 # ifdef FEAT_MBYTE | |
9062 if (wn != NULL) | |
9063 { | |
9064 vim_free(wn); | |
1752 | 9065 wn = enc_to_utf16(buf, NULL); |
7 | 9066 if (wn != NULL) |
9067 hFind = FindFirstFileW(wn, &wfb); | |
9068 } | |
9069 if (wn == NULL) | |
9070 # endif | |
9071 hFind = FindFirstFile(buf, &fb); | |
9072 ok = (hFind != INVALID_HANDLE_VALUE); | |
9073 #else | |
9074 ok = (findfirst((char *)buf, &fb, | |
9075 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0); | |
9076 #endif | |
9077 vim_free(matchname); | |
9078 matchname = NULL; | |
9079 } | |
9080 } | |
9081 | |
9082 #ifdef WIN3264 | |
9083 FindClose(hFind); | |
9084 # ifdef FEAT_MBYTE | |
9085 vim_free(wn); | |
9086 # endif | |
9087 #endif | |
9088 vim_free(buf); | |
9089 vim_free(regmatch.regprog); | |
9090 vim_free(matchname); | |
9091 | |
9092 matches = gap->ga_len - start_len; | |
9093 if (matches > 0) | |
9094 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, | |
9095 sizeof(char_u *), pstrcmp); | |
9096 return matches; | |
9097 } | |
9098 | |
9099 int | |
9100 mch_expandpath( | |
9101 garray_T *gap, | |
9102 char_u *path, | |
9103 int flags) /* EW_* flags */ | |
9104 { | |
445 | 9105 return dos_expandpath(gap, path, 0, flags, FALSE); |
7 | 9106 } |
9107 # endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */ | |
9108 | |
445 | 9109 #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ |
9110 || defined(PROTO) | |
9111 /* | |
9112 * Unix style wildcard expansion code. | |
9113 * It's here because it's used both for Unix and Mac. | |
9114 */ | |
9115 static int pstrcmp __ARGS((const void *, const void *)); | |
9116 | |
9117 static int | |
9118 pstrcmp(a, b) | |
9119 const void *a, *b; | |
9120 { | |
9121 return (pathcmp(*(char **)a, *(char **)b, -1)); | |
9122 } | |
9123 | |
9124 /* | |
9125 * Recursively expand one path component into all matching files and/or | |
9126 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
9127 * "path" has backslashes before chars that are not to be expanded, starting | |
9128 * at "path + wildoff". | |
9129 * Return the number of matches found. | |
9130 * NOTE: much of this is identical to dos_expandpath(), keep in sync! | |
9131 */ | |
9132 int | |
9133 unix_expandpath(gap, path, wildoff, flags, didstar) | |
9134 garray_T *gap; | |
9135 char_u *path; | |
9136 int wildoff; | |
9137 int flags; /* EW_* flags */ | |
9138 int didstar; /* expanded "**" once already */ | |
9139 { | |
9140 char_u *buf; | |
9141 char_u *path_end; | |
9142 char_u *p, *s, *e; | |
9143 int start_len = gap->ga_len; | |
9144 char_u *pat; | |
9145 regmatch_T regmatch; | |
9146 int starts_with_dot; | |
9147 int matches; | |
9148 int len; | |
9149 int starstar = FALSE; | |
9150 static int stardepth = 0; /* depth for "**" expansion */ | |
9151 | |
9152 DIR *dirp; | |
9153 struct dirent *dp; | |
9154 | |
9155 /* Expanding "**" may take a long time, check for CTRL-C. */ | |
9156 if (stardepth > 0) | |
9157 { | |
9158 ui_breakcheck(); | |
9159 if (got_int) | |
9160 return 0; | |
9161 } | |
9162 | |
9163 /* make room for file name */ | |
9164 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5); | |
9165 if (buf == NULL) | |
9166 return 0; | |
9167 | |
9168 /* | |
9169 * Find the first part in the path name that contains a wildcard. | |
9170 * Copy it into "buf", including the preceding characters. | |
9171 */ | |
9172 p = buf; | |
9173 s = buf; | |
9174 e = NULL; | |
9175 path_end = path; | |
9176 while (*path_end != NUL) | |
9177 { | |
9178 /* May ignore a wildcard that has a backslash before it; it will | |
9179 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */ | |
9180 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
9181 *p++ = *path_end++; | |
9182 else if (*path_end == '/') | |
9183 { | |
9184 if (e != NULL) | |
9185 break; | |
9186 s = p + 1; | |
9187 } | |
9188 else if (path_end >= path + wildoff | |
9189 && vim_strchr((char_u *)"*?[{~$", *path_end) != NULL) | |
9190 e = p; | |
9191 #ifdef FEAT_MBYTE | |
9192 if (has_mbyte) | |
9193 { | |
474 | 9194 len = (*mb_ptr2len)(path_end); |
445 | 9195 STRNCPY(p, path_end, len); |
9196 p += len; | |
9197 path_end += len; | |
9198 } | |
9199 else | |
9200 #endif | |
9201 *p++ = *path_end++; | |
9202 } | |
9203 e = p; | |
9204 *e = NUL; | |
9205 | |
9206 /* now we have one wildcard component between "s" and "e" */ | |
9207 /* Remove backslashes between "wildoff" and the start of the wildcard | |
9208 * component. */ | |
9209 for (p = buf + wildoff; p < s; ++p) | |
9210 if (rem_backslash(p)) | |
9211 { | |
1624 | 9212 STRMOVE(p, p + 1); |
445 | 9213 --e; |
9214 --s; | |
9215 } | |
9216 | |
9217 /* Check for "**" between "s" and "e". */ | |
9218 for (p = s; p < e; ++p) | |
9219 if (p[0] == '*' && p[1] == '*') | |
9220 starstar = TRUE; | |
9221 | |
9222 /* convert the file pattern to a regexp pattern */ | |
9223 starts_with_dot = (*s == '.'); | |
9224 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
9225 if (pat == NULL) | |
9226 { | |
9227 vim_free(buf); | |
9228 return 0; | |
9229 } | |
9230 | |
9231 /* compile the regexp into a program */ | |
587 | 9232 #ifdef CASE_INSENSITIVE_FILENAME |
445 | 9233 regmatch.rm_ic = TRUE; /* Behave like Terminal.app */ |
9234 #else | |
2652 | 9235 if (flags & EW_ICASE) |
9236 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */ | |
9237 else | |
9238 regmatch.rm_ic = FALSE; /* Don't ignore case */ | |
445 | 9239 #endif |
9240 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
9241 vim_free(pat); | |
9242 | |
9243 if (regmatch.regprog == NULL) | |
9244 { | |
9245 vim_free(buf); | |
9246 return 0; | |
9247 } | |
9248 | |
9249 /* If "**" is by itself, this is the first time we encounter it and more | |
9250 * is following then find matches without any directory. */ | |
9251 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
9252 && *path_end == '/') | |
9253 { | |
9254 STRCPY(s, path_end + 1); | |
9255 ++stardepth; | |
9256 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
9257 --stardepth; | |
9258 } | |
9259 | |
9260 /* open the directory for scanning */ | |
9261 *s = NUL; | |
9262 dirp = opendir(*buf == NUL ? "." : (char *)buf); | |
9263 | |
9264 /* Find all matching entries */ | |
9265 if (dirp != NULL) | |
9266 { | |
9267 for (;;) | |
9268 { | |
9269 dp = readdir(dirp); | |
9270 if (dp == NULL) | |
9271 break; | |
9272 if ((dp->d_name[0] != '.' || starts_with_dot) | |
9273 && vim_regexec(®match, (char_u *)dp->d_name, (colnr_T)0)) | |
9274 { | |
9275 STRCPY(s, dp->d_name); | |
9276 len = STRLEN(buf); | |
9277 | |
9278 if (starstar && stardepth < 100) | |
9279 { | |
9280 /* For "**" in the pattern first go deeper in the tree to | |
9281 * find matches. */ | |
9282 STRCPY(buf + len, "/**"); | |
9283 STRCPY(buf + len + 3, path_end); | |
9284 ++stardepth; | |
9285 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); | |
9286 --stardepth; | |
9287 } | |
9288 | |
9289 STRCPY(buf + len, path_end); | |
9290 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */ | |
9291 { | |
9292 /* need to expand another component of the path */ | |
9293 /* remove backslashes for the remaining components only */ | |
9294 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); | |
9295 } | |
9296 else | |
9297 { | |
9298 /* no more wildcards, check if there is a match */ | |
9299 /* remove backslashes for the remaining components only */ | |
9300 if (*path_end != NUL) | |
9301 backslash_halve(buf + len + 1); | |
9302 if (mch_getperm(buf) >= 0) /* add existing file */ | |
9303 { | |
768 | 9304 #ifdef MACOS_CONVERT |
445 | 9305 size_t precomp_len = STRLEN(buf)+1; |
9306 char_u *precomp_buf = | |
9307 mac_precompose_path(buf, precomp_len, &precomp_len); | |
768 | 9308 |
445 | 9309 if (precomp_buf) |
9310 { | |
9311 mch_memmove(buf, precomp_buf, precomp_len); | |
9312 vim_free(precomp_buf); | |
9313 } | |
9314 #endif | |
9315 addfile(gap, buf, flags); | |
9316 } | |
9317 } | |
9318 } | |
9319 } | |
9320 | |
9321 closedir(dirp); | |
9322 } | |
9323 | |
9324 vim_free(buf); | |
9325 vim_free(regmatch.regprog); | |
9326 | |
9327 matches = gap->ga_len - start_len; | |
9328 if (matches > 0) | |
9329 qsort(((char_u **)gap->ga_data) + start_len, matches, | |
9330 sizeof(char_u *), pstrcmp); | |
9331 return matches; | |
9332 } | |
9333 #endif | |
9334 | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9335 #if defined(FEAT_SEARCHPATH) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9336 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
|
9337 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
|
9338 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
|
9339 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
|
9340 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
|
9341 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
|
9342 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9343 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9344 * 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
|
9345 * 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
|
9346 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9347 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9348 find_previous_pathsep(path, psep) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9349 char_u *path; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9350 char_u **psep; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9351 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9352 /* skip the current separator */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9353 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
|
9354 --*psep; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9355 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9356 /* 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
|
9357 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
|
9358 { |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9359 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
|
9360 return OK; |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9361 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
|
9362 } |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9363 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9364 return FAIL; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9365 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9366 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9367 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9368 * 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
|
9369 * "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
|
9370 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9371 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9372 is_unique(maybe_unique, gap, i) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9373 char_u *maybe_unique; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9374 garray_T *gap; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9375 int i; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9376 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9377 int j; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9378 int candidate_len; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9379 int other_path_len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9380 char_u **other_paths = (char_u **)gap->ga_data; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9381 char_u *rival; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9382 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9383 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
|
9384 { |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9385 if (j == i) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9386 continue; /* don't compare it with itself */ |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9387 |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
9388 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
|
9389 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
|
9390 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
|
9391 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
|
9392 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9393 rival = other_paths[j] + other_path_len - candidate_len; |
2574 | 9394 if (fnamecmp(maybe_unique, rival) == 0 |
9395 && (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
|
9396 return FALSE; /* match */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9397 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9398 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9399 return TRUE; /* no match found */ |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9400 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9401 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9402 /* |
2460
b8eb7c0f74c2
Remove unused variable and STRLEN(). (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2435
diff
changeset
|
9403 * 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
|
9404 * 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
|
9405 * (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
|
9406 * directory) notations. |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9407 * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9408 * 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
|
9409 * expanding each into their equivalent path(s). |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9410 */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9411 static void |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9412 expand_path_option(curdir, gap) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9413 char_u *curdir; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9414 garray_T *gap; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9415 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9416 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
|
9417 ? p_path : curbuf->b_p_path; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9418 char_u *buf; |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9419 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
|
9420 int len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9421 |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
9422 if ((buf = alloc((int)MAXPATHL)) == NULL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9423 return; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9424 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9425 while (*path_option != NUL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9426 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9427 copy_option_part(&path_option, buf, MAXPATHL, " ,"); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9428 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9429 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
|
9430 { |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9431 /* 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
|
9432 * "/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
|
9433 * "/path/file" + "./subdir" -> "/path/subdir" */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9434 if (curbuf->b_ffname == NULL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9435 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9436 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
|
9437 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
|
9438 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
|
9439 continue; |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9440 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
|
9441 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
|
9442 else |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9443 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
|
9444 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
|
9445 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
|
9446 } |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9447 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
|
9448 /* relative to current directory */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9449 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
|
9450 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
|
9451 /* 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
|
9452 continue; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9453 else if (!mch_isFullName(buf)) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9454 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9455 /* 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
|
9456 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
|
9457 if (len + (int)STRLEN(buf) + 3 > MAXPATHL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9458 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9459 STRMOVE(buf + len + 1, buf); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9460 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
|
9461 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
|
9462 simplify_filename(buf); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9463 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9464 |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9465 if (ga_grow(gap, 1) == FAIL) |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9466 break; |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9467 p = vim_strsave(buf); |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9468 if (p == NULL) |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9469 break; |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9470 ((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
|
9471 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9472 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9473 vim_free(buf); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9474 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9475 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9476 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9477 * 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
|
9478 * 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
|
9479 * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9480 * path: /foo/bar/baz |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9481 * fname: /foo/bar/baz/quux.txt |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9482 * returns: ^this |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9483 */ |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9484 static char_u * |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9485 get_path_cutoff(fname, gap) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9486 char_u *fname; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9487 garray_T *gap; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9488 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9489 int i; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9490 int maxlen = 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9491 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
|
9492 char_u *cutoff = NULL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9493 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9494 for (i = 0; i < gap->ga_len; i++) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9495 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9496 int j = 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9497 |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9498 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
|
9499 # if defined(MSWIN) || defined(MSDOS) |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9500 || (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
|
9501 #endif |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9502 ) && 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
|
9503 j++; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9504 if (j > maxlen) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9505 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9506 maxlen = j; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9507 cutoff = &fname[j]; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9508 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9509 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9510 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9511 /* skip to the file or directory name */ |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9512 if (cutoff != NULL) |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
9513 while (vim_ispathsep(*cutoff)) |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9514 mb_ptr_adv(cutoff); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9515 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9516 return cutoff; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9517 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9518 |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9519 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9520 * 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
|
9521 * 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
|
9522 * 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
|
9523 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9524 static void |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9525 uniquefy_paths(gap, pattern) |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
9526 garray_T *gap; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
9527 char_u *pattern; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
9528 { |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
9529 int i; |
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
9530 int len; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9531 char_u **fnames = (char_u **)gap->ga_data; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9532 int sort_again = FALSE; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9533 char_u *pat; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9534 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
|
9535 char_u *curdir; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9536 regmatch_T regmatch; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9537 garray_T path_ga; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9538 char_u **in_curdir = NULL; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9539 char_u *short_name; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9540 |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
9541 remove_duplicates(gap); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9542 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
|
9543 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9544 /* |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9545 * 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
|
9546 * 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
|
9547 * possible patterns? |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9548 */ |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
9549 len = (int)STRLEN(pattern); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9550 file_pattern = alloc(len + 2); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9551 if (file_pattern == NULL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9552 return; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9553 file_pattern[0] = '*'; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9554 file_pattern[1] = NUL; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9555 STRCAT(file_pattern, pattern); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9556 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
|
9557 vim_free(file_pattern); |
2387
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
9558 if (pat == NULL) |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
9559 return; |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
9560 |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9561 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
|
9562 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
|
9563 vim_free(pat); |
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
9564 if (regmatch.regprog == NULL) |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9565 return; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9566 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9567 if ((curdir = alloc((int)(MAXPATHL))) == NULL) |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9568 goto theend; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9569 mch_dirname(curdir, MAXPATHL); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9570 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
|
9571 |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9572 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
|
9573 if (in_curdir == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9574 goto theend; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9575 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9576 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
|
9577 { |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9578 char_u *path = fnames[i]; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9579 int is_in_curdir; |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
9580 char_u *dir_end = gettail_dir(path); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9581 char_u *pathsep_p; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9582 char_u *path_cutoff; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9583 |
2322
70be008dff4f
Avoid compiler warnings for size_t to int conversions.
Bram Moolenaar <bram@vim.org>
parents:
2313
diff
changeset
|
9584 len = (int)STRLEN(path); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9585 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
|
9586 && curdir[dir_end - path] == NUL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9587 if (is_in_curdir) |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9588 in_curdir[i] = vim_strsave(path); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9589 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9590 /* Shorten the filename while maintaining its uniqueness */ |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9591 path_cutoff = get_path_cutoff(path, &path_ga); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9592 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9593 /* we start at the end of the path */ |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9594 pathsep_p = path + len - 1; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9595 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9596 while (find_previous_pathsep(path, &pathsep_p)) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9597 if (vim_regexec(®match, pathsep_p + 1, (colnr_T)0) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9598 && is_unique(pathsep_p + 1, gap, i) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9599 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9600 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9601 sort_again = TRUE; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9602 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p)); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9603 break; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9604 } |
2538 | 9605 |
9606 if (mch_isFullName(path)) | |
9607 { | |
9608 /* | |
9609 * Last resort: shorten relative to curdir if possible. | |
9610 * 'possible' means: | |
9611 * 1. It is under the current directory. | |
9612 * 2. The result is actually shorter than the original. | |
9613 * | |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9614 * Before curdir After |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9615 * /foo/bar/file.txt /foo/bar ./file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9616 * c:\foo\bar\file.txt c:\foo\bar .\file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9617 * /file.txt / /file.txt |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9618 * c:\file.txt c:\ .\file.txt |
2538 | 9619 */ |
9620 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
|
9621 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
|
9622 #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
|
9623 /* On windows, |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
9624 * 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
|
9625 * 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
|
9626 * name, hence: */ |
2549
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
9627 && !vim_ispathsep(*short_name) |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
9628 #endif |
c210e31a2ec5
More fixes for :find completion. (mostly by Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2542
diff
changeset
|
9629 ) |
2538 | 9630 { |
9631 STRCPY(path, "."); | |
9632 add_pathsep(path); | |
2562
19f4f5ee3d1f
Fix: strcat() on overlapping string. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2559
diff
changeset
|
9633 STRMOVE(path + STRLEN(path), short_name); |
2538 | 9634 } |
9635 } | |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9636 ui_breakcheck(); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9637 } |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9638 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9639 /* 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
|
9640 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
|
9641 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9642 char_u *rel_path; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9643 char_u *path = in_curdir[i]; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9644 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9645 if (path == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9646 continue; |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9647 |
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9648 /* 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
|
9649 * Else reduce it to {filename} */ |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9650 short_name = shorten_fname(path, curdir); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9651 if (short_name == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9652 short_name = path; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9653 if (is_unique(short_name, gap, i)) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9654 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9655 STRCPY(fnames[i], short_name); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9656 continue; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9657 } |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9658 |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9659 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2)); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9660 if (rel_path == NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9661 goto theend; |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9662 STRCPY(rel_path, "."); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9663 add_pathsep(rel_path); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9664 STRCAT(rel_path, short_name); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9665 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9666 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
|
9667 fnames[i] = rel_path; |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9668 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
|
9669 ui_breakcheck(); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9670 } |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9671 |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9672 theend: |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9673 vim_free(curdir); |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9674 if (in_curdir != NULL) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9675 { |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9676 for (i = 0; i < gap->ga_len; i++) |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9677 vim_free(in_curdir[i]); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9678 vim_free(in_curdir); |
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9679 } |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9680 ga_clear_strings(&path_ga); |
2387
797f7b467b03
Fix memory leak in :find completion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2361
diff
changeset
|
9681 vim_free(regmatch.regprog); |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9682 |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9683 if (sort_again) |
2361
e12685c50982
Fix some duplicates in ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2354
diff
changeset
|
9684 remove_duplicates(gap); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9685 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9686 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9687 /* |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
9688 * 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
|
9689 * result in "gap". |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9690 * Returns the total number of matches. |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9691 */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9692 static int |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9693 expand_in_path(gap, pattern, flags) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9694 garray_T *gap; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9695 char_u *pattern; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9696 int flags; /* EW_* flags */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9697 { |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9698 char_u *curdir; |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9699 garray_T path_ga; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9700 char_u *files = NULL; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9701 char_u *s; /* start */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9702 char_u *e; /* end */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9703 char_u *paths = NULL; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9704 |
2477
81bf2aaa979a
Fix crash in :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2476
diff
changeset
|
9705 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL) |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9706 return 0; |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9707 mch_dirname(curdir, MAXPATHL); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9708 |
2542
7a547db7070d
Improvements for :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2538
diff
changeset
|
9709 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
|
9710 expand_path_option(curdir, &path_ga); |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9711 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
|
9712 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
|
9713 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
|
9714 |
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
9715 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
|
9716 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
|
9717 if (paths == NULL) |
2477
81bf2aaa979a
Fix crash in :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2476
diff
changeset
|
9718 return 0; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9719 |
2652 | 9720 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
|
9721 vim_free(paths); |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9722 if (files == NULL) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9723 return 0; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9724 |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9725 /* Copy each path in files into gap */ |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9726 s = e = files; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9727 while (*s != NUL) |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9728 { |
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9729 while (*e != '\n' && *e != NUL) |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9730 e++; |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9731 if (*e == NUL) |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9732 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9733 addfile(gap, s, flags); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9734 break; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9735 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9736 else |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9737 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9738 /* *e is '\n' */ |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
9739 *e = NUL; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9740 addfile(gap, s, flags); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9741 e++; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9742 s = e; |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9743 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9744 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9745 vim_free(files); |
2471
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9746 |
6570c136be32
Improvements for :find completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2460
diff
changeset
|
9747 return gap->ga_len; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9748 } |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9749 #endif |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9750 |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9751 #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
|
9752 /* |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9753 * 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
|
9754 * 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
|
9755 */ |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9756 void |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9757 remove_duplicates(gap) |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9758 garray_T *gap; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9759 { |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9760 int i; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9761 int j; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9762 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
|
9763 |
2559
7850c8c12347
Cleanup in :find completion code. Make it work for "./subdir" in 'path'.
Bram Moolenaar <bram@vim.org>
parents:
2549
diff
changeset
|
9764 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
|
9765 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
|
9766 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
|
9767 { |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9768 vim_free(fnames[i]); |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9769 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
|
9770 fnames[j - 1] = fnames[j]; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9771 --gap->ga_len; |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9772 } |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9773 } |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9774 #endif |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
9775 |
7 | 9776 /* |
9777 * Generic wildcard expansion code. | |
9778 * | |
9779 * Characters in "pat" that should not be expanded must be preceded with a | |
9780 * backslash. E.g., "/path\ with\ spaces/my\*star*" | |
9781 * | |
9782 * Return FAIL when no single file was found. In this case "num_file" is not | |
9783 * set, and "file" may contain an error message. | |
9784 * Return OK when some files found. "num_file" is set to the number of | |
9785 * matches, "file" to the array of matches. Call FreeWild() later. | |
9786 */ | |
9787 int | |
9788 gen_expand_wildcards(num_pat, pat, num_file, file, flags) | |
9789 int num_pat; /* number of input patterns */ | |
9790 char_u **pat; /* array of input patterns */ | |
9791 int *num_file; /* resulting number of files */ | |
9792 char_u ***file; /* array of resulting files */ | |
9793 int flags; /* EW_* flags */ | |
9794 { | |
9795 int i; | |
9796 garray_T ga; | |
9797 char_u *p; | |
9798 static int recursive = FALSE; | |
9799 int add_pat; | |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
9800 #if defined(FEAT_SEARCHPATH) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
9801 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
|
9802 #endif |
7 | 9803 |
9804 /* | |
9805 * expand_env() is called to expand things like "~user". If this fails, | |
9806 * it calls ExpandOne(), which brings us back here. In this case, always | |
9807 * call the machine specific expansion function, if possible. Otherwise, | |
9808 * return FAIL. | |
9809 */ | |
9810 if (recursive) | |
9811 #ifdef SPECIAL_WILDCHAR | |
9812 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
9813 #else | |
9814 return FAIL; | |
9815 #endif | |
9816 | |
9817 #ifdef SPECIAL_WILDCHAR | |
9818 /* | |
9819 * If there are any special wildcard characters which we cannot handle | |
9820 * here, call machine specific function for all the expansion. This | |
9821 * avoids starting the shell for each argument separately. | |
9822 * For `=expr` do use the internal function. | |
9823 */ | |
9824 for (i = 0; i < num_pat; i++) | |
9825 { | |
9826 if (vim_strpbrk(pat[i], (char_u *)SPECIAL_WILDCHAR) != NULL | |
9827 # ifdef VIM_BACKTICK | |
9828 && !(vim_backtick(pat[i]) && pat[i][1] == '=') | |
9829 # endif | |
9830 ) | |
9831 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
9832 } | |
9833 #endif | |
9834 | |
9835 recursive = TRUE; | |
9836 | |
9837 /* | |
9838 * The matching file names are stored in a growarray. Init it empty. | |
9839 */ | |
9840 ga_init2(&ga, (int)sizeof(char_u *), 30); | |
9841 | |
9842 for (i = 0; i < num_pat; ++i) | |
9843 { | |
9844 add_pat = -1; | |
9845 p = pat[i]; | |
9846 | |
9847 #ifdef VIM_BACKTICK | |
9848 if (vim_backtick(p)) | |
9849 add_pat = expand_backtick(&ga, p, flags); | |
9850 else | |
9851 #endif | |
9852 { | |
9853 /* | |
9854 * First expand environment variables, "~/" and "~user/". | |
9855 */ | |
2538 | 9856 if (vim_strchr(p, '$') != NULL || *p == '~') |
7 | 9857 { |
1408 | 9858 p = expand_env_save_opt(p, TRUE); |
7 | 9859 if (p == NULL) |
9860 p = pat[i]; | |
9861 #ifdef UNIX | |
9862 /* | |
9863 * On Unix, if expand_env() can't expand an environment | |
9864 * variable, use the shell to do that. Discard previously | |
9865 * found file names and start all over again. | |
9866 */ | |
2538 | 9867 else if (vim_strchr(p, '$') != NULL || *p == '~') |
7 | 9868 { |
9869 vim_free(p); | |
1914 | 9870 ga_clear_strings(&ga); |
7 | 9871 i = mch_expand_wildcards(num_pat, pat, num_file, file, |
9872 flags); | |
9873 recursive = FALSE; | |
9874 return i; | |
9875 } | |
9876 #endif | |
9877 } | |
9878 | |
9879 /* | |
9880 * If there are wildcards: Expand file names and add each match to | |
9881 * the list. If there is no match, and EW_NOTFOUND is given, add | |
9882 * the pattern. | |
9883 * If there are no wildcards: Add the file name if it exists or | |
9884 * when EW_NOTFOUND is given. | |
9885 */ | |
9886 if (mch_has_exp_wildcard(p)) | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9887 { |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9888 #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
|
9889 if ((flags & EW_PATH) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
9890 && !mch_isFullName(p) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
9891 && !(p[0] == '.' |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
9892 && (vim_ispathsep(p[1]) |
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
9893 || (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
|
9894 ) |
2487
7ec9ada2cd81
Make :find completion consistent between Unix and MS-Windows. Add a test.
Bram Moolenaar <bram@vim.org>
parents:
2481
diff
changeset
|
9895 { |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
9896 /* :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
|
9897 * 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
|
9898 recursive = FALSE; |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9899 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
|
9900 recursive = TRUE; |
2568
1ead15c2ffd0
Two fixes for :find completion and more testing. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2562
diff
changeset
|
9901 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
|
9902 } |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9903 else |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9904 #endif |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9905 add_pat = mch_expandpath(&ga, p, flags); |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
9906 } |
7 | 9907 } |
9908 | |
9909 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) | |
9910 { | |
9911 char_u *t = backslash_halve_save(p); | |
9912 | |
9913 #if defined(MACOS_CLASSIC) | |
9914 slash_to_colon(t); | |
9915 #endif | |
9916 /* When EW_NOTFOUND is used, always add files and dirs. Makes | |
9917 * "vim c:/" work. */ | |
9918 if (flags & EW_NOTFOUND) | |
9919 addfile(&ga, t, flags | EW_DIR | EW_FILE); | |
9920 else if (mch_getperm(t) >= 0) | |
9921 addfile(&ga, t, flags); | |
9922 vim_free(t); | |
9923 } | |
9924 | |
2313
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
9925 #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
|
9926 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
|
9927 uniquefy_paths(&ga, p); |
e382b66b936d
Fix using freed memory in :find completion.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
9928 #endif |
7 | 9929 if (p != pat[i]) |
9930 vim_free(p); | |
9931 } | |
9932 | |
9933 *num_file = ga.ga_len; | |
9934 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; | |
9935 | |
9936 recursive = FALSE; | |
9937 | |
9938 return (ga.ga_data != NULL) ? OK : FAIL; | |
9939 } | |
9940 | |
9941 # ifdef VIM_BACKTICK | |
9942 | |
9943 /* | |
9944 * Return TRUE if we can expand this backtick thing here. | |
9945 */ | |
9946 static int | |
9947 vim_backtick(p) | |
9948 char_u *p; | |
9949 { | |
9950 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); | |
9951 } | |
9952 | |
9953 /* | |
9954 * Expand an item in `backticks` by executing it as a command. | |
9955 * Currently only works when pat[] starts and ends with a `. | |
9956 * Returns number of file names found. | |
9957 */ | |
9958 static int | |
9959 expand_backtick(gap, pat, flags) | |
9960 garray_T *gap; | |
9961 char_u *pat; | |
9962 int flags; /* EW_* flags */ | |
9963 { | |
9964 char_u *p; | |
9965 char_u *cmd; | |
9966 char_u *buffer; | |
9967 int cnt = 0; | |
9968 int i; | |
9969 | |
9970 /* Create the command: lop off the backticks. */ | |
9971 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); | |
9972 if (cmd == NULL) | |
9973 return 0; | |
9974 | |
9975 #ifdef FEAT_EVAL | |
9976 if (*cmd == '=') /* `={expr}`: Expand expression */ | |
714 | 9977 buffer = eval_to_string(cmd + 1, &p, TRUE); |
7 | 9978 else |
9979 #endif | |
24 | 9980 buffer = get_cmd_output(cmd, NULL, |
9981 (flags & EW_SILENT) ? SHELL_SILENT : 0); | |
7 | 9982 vim_free(cmd); |
9983 if (buffer == NULL) | |
9984 return 0; | |
9985 | |
9986 cmd = buffer; | |
9987 while (*cmd != NUL) | |
9988 { | |
9989 cmd = skipwhite(cmd); /* skip over white space */ | |
9990 p = cmd; | |
9991 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */ | |
9992 ++p; | |
9993 /* add an entry if it is not empty */ | |
9994 if (p > cmd) | |
9995 { | |
9996 i = *p; | |
9997 *p = NUL; | |
9998 addfile(gap, cmd, flags); | |
9999 *p = i; | |
10000 ++cnt; | |
10001 } | |
10002 cmd = p; | |
10003 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) | |
10004 ++cmd; | |
10005 } | |
10006 | |
10007 vim_free(buffer); | |
10008 return cnt; | |
10009 } | |
10010 # endif /* VIM_BACKTICK */ | |
10011 | |
10012 /* | |
10013 * Add a file to a file list. Accepted flags: | |
10014 * EW_DIR add directories | |
10015 * EW_FILE add files | |
716 | 10016 * EW_EXEC add executable files |
7 | 10017 * EW_NOTFOUND add even when it doesn't exist |
10018 * EW_ADDSLASH add slash after directory name | |
10019 */ | |
10020 void | |
10021 addfile(gap, f, flags) | |
10022 garray_T *gap; | |
10023 char_u *f; /* filename */ | |
10024 int flags; | |
10025 { | |
10026 char_u *p; | |
10027 int isdir; | |
10028 | |
10029 /* if the file/dir doesn't exist, may not add it */ | |
10030 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0) | |
10031 return; | |
10032 | |
10033 #ifdef FNAME_ILLEGAL | |
10034 /* if the file/dir contains illegal characters, don't add it */ | |
10035 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) | |
10036 return; | |
10037 #endif | |
10038 | |
10039 isdir = mch_isdir(f); | |
10040 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) | |
10041 return; | |
10042 | |
716 | 10043 /* If the file isn't executable, may not add it. Do accept directories. */ |
10044 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f)) | |
10045 return; | |
10046 | |
7 | 10047 /* Make room for another item in the file list. */ |
10048 if (ga_grow(gap, 1) == FAIL) | |
10049 return; | |
10050 | |
10051 p = alloc((unsigned)(STRLEN(f) + 1 + isdir)); | |
10052 if (p == NULL) | |
10053 return; | |
10054 | |
10055 STRCPY(p, f); | |
10056 #ifdef BACKSLASH_IN_FILENAME | |
10057 slash_adjust(p); | |
10058 #endif | |
10059 /* | |
10060 * Append a slash or backslash after directory names if none is present. | |
10061 */ | |
10062 #ifndef DONT_ADD_PATHSEP_TO_DIR | |
10063 if (isdir && (flags & EW_ADDSLASH)) | |
10064 add_pathsep(p); | |
10065 #endif | |
10066 ((char_u **)gap->ga_data)[gap->ga_len++] = p; | |
10067 } | |
10068 #endif /* !NO_EXPANDPATH */ | |
10069 | |
10070 #if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO) | |
10071 | |
10072 #ifndef SEEK_SET | |
10073 # define SEEK_SET 0 | |
10074 #endif | |
10075 #ifndef SEEK_END | |
10076 # define SEEK_END 2 | |
10077 #endif | |
10078 | |
10079 /* | |
10080 * Get the stdout of an external command. | |
10081 * Returns an allocated string, or NULL for error. | |
10082 */ | |
10083 char_u * | |
24 | 10084 get_cmd_output(cmd, infile, flags) |
7 | 10085 char_u *cmd; |
24 | 10086 char_u *infile; /* optional input file name */ |
7 | 10087 int flags; /* can be SHELL_SILENT */ |
10088 { | |
10089 char_u *tempname; | |
10090 char_u *command; | |
10091 char_u *buffer = NULL; | |
10092 int len; | |
10093 int i = 0; | |
10094 FILE *fd; | |
10095 | |
10096 if (check_restricted() || check_secure()) | |
10097 return NULL; | |
10098 | |
10099 /* get a name for the temp file */ | |
10100 if ((tempname = vim_tempname('o')) == NULL) | |
10101 { | |
10102 EMSG(_(e_notmp)); | |
10103 return NULL; | |
10104 } | |
10105 | |
10106 /* Add the redirection stuff */ | |
24 | 10107 command = make_filter_cmd(cmd, infile, tempname); |
7 | 10108 if (command == NULL) |
10109 goto done; | |
10110 | |
10111 /* | |
10112 * Call the shell to execute the command (errors are ignored). | |
10113 * Don't check timestamps here. | |
10114 */ | |
10115 ++no_check_timestamps; | |
10116 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags); | |
10117 --no_check_timestamps; | |
10118 | |
10119 vim_free(command); | |
10120 | |
10121 /* | |
10122 * read the names from the file into memory | |
10123 */ | |
10124 # ifdef VMS | |
1224 | 10125 /* created temporary file is not always readable as binary */ |
7 | 10126 fd = mch_fopen((char *)tempname, "r"); |
10127 # else | |
10128 fd = mch_fopen((char *)tempname, READBIN); | |
10129 # endif | |
10130 | |
10131 if (fd == NULL) | |
10132 { | |
10133 EMSG2(_(e_notopen), tempname); | |
10134 goto done; | |
10135 } | |
10136 | |
10137 fseek(fd, 0L, SEEK_END); | |
10138 len = ftell(fd); /* get size of temp file */ | |
10139 fseek(fd, 0L, SEEK_SET); | |
10140 | |
10141 buffer = alloc(len + 1); | |
10142 if (buffer != NULL) | |
10143 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd); | |
10144 fclose(fd); | |
10145 mch_remove(tempname); | |
10146 if (buffer == NULL) | |
10147 goto done; | |
10148 #ifdef VMS | |
10149 len = i; /* VMS doesn't give us what we asked for... */ | |
10150 #endif | |
10151 if (i != len) | |
10152 { | |
10153 EMSG2(_(e_notread), tempname); | |
10154 vim_free(buffer); | |
10155 buffer = NULL; | |
10156 } | |
10157 else | |
2429
7ce8b24450dc
Improvements for ":find" completion. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2409
diff
changeset
|
10158 buffer[len] = NUL; /* make sure the buffer is terminated */ |
7 | 10159 |
10160 done: | |
10161 vim_free(tempname); | |
10162 return buffer; | |
10163 } | |
10164 #endif | |
10165 | |
10166 /* | |
10167 * Free the list of files returned by expand_wildcards() or other expansion | |
10168 * functions. | |
10169 */ | |
10170 void | |
10171 FreeWild(count, files) | |
10172 int count; | |
10173 char_u **files; | |
10174 { | |
838 | 10175 if (count <= 0 || files == NULL) |
7 | 10176 return; |
10177 #if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */ | |
10178 /* | |
10179 * Is this still OK for when other functions than expand_wildcards() have | |
10180 * been used??? | |
10181 */ | |
10182 _fnexplodefree((char **)files); | |
10183 #else | |
10184 while (count--) | |
10185 vim_free(files[count]); | |
10186 vim_free(files); | |
10187 #endif | |
10188 } | |
10189 | |
10190 /* | |
2302
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2297
diff
changeset
|
10191 * Return TRUE when need to go to Insert mode because of 'insertmode'. |
7 | 10192 * Don't do this when still processing a command or a mapping. |
10193 * Don't do this when inside a ":normal" command. | |
10194 */ | |
10195 int | |
10196 goto_im() | |
10197 { | |
10198 return (p_im && stuff_empty() && typebuf_typed()); | |
10199 } |